Zulip Chat Archive

Stream: lean4

Topic: Define an inductive type within a proof?


Heather Macbeth (Mar 04 2023 at 21:36):

Is it possible to define an inductive type on the fly within a def/theorem?

For example, here is a working snippet:

def Foo : Nat  Type
  | 0 => Empty
  | n + 1 => Option (Foo n)

Suppose I wanted to do the same thing but with a custom Option-like type which I define on the fly, like this (which doesn't compile):

def Foo : Nat  Type
  | 0 => Empty
  | n + 1 =>
    | myNone : Foo (n + 1)
    | mySome (i : Foo n) : Foo (n + 1)

Is there a syntax for this?

Reid Barton (Mar 04 2023 at 21:43):

This seems sort of awkward, e.g., what would the name of the recursor for this new type be (imagine that there are several of these, in different branches)

Heather Macbeth (Mar 04 2023 at 21:44):

OK, so no :-) Thanks!

Reid Barton (Mar 04 2023 at 21:45):

In this case you could also use an inductive family, but maybe you have good reasons to prefer a recursive definition.

Heather Macbeth (Mar 04 2023 at 21:46):

I'm not sure what you mean by an inductive family (here) -- can you explain?

Reid Barton (Mar 04 2023 at 21:48):

inductive Foo : Nat  Type
| myNone : (n : Nat)  Foo (n + 1)
| mySucc : (n : Nat)  Foo n  Foo (n + 1)

Heather Macbeth (Mar 04 2023 at 21:49):

Ah! In fact I was asking this question to think about variants on docs4#Fin2, and it seems that you have just explained how one would invent that definition.

Adam Topaz (Mar 05 2023 at 02:59):

Another natural variant is the inductive def of docs#vector which is very similar to this version of fin, as follows:

inductive Vector (α : Type u) : Nat  Type u
  | nil : Vector _ 0
  | cons : α  Vector α n  Vector α (n+1)

Adam Topaz (Mar 05 2023 at 03:04):

If I understand correctly, we avoid such defs because they're not as (computationally) efficient as the def in terms of subtypes? Is that right?

Reid Barton (Mar 05 2023 at 05:41):

Well to be fair, Peano numerals and (to a lesser extent) linked lists are not that computationally efficient in the first place.

Reid Barton (Mar 05 2023 at 05:48):

Another downside of these inductive families is that the representation of the "data" (like a natural number) is so tightly coupled to the "property" (like being less than n). For example if you want to do fin.cast_succ on Foo, you need to do recursion (and it takes O(m)O(m) time, where mm is the number).

Reid Barton (Mar 05 2023 at 05:48):

In some contexts that tight coupling could be a good thing, but mostly it seems like a bad thing.


Last updated: Dec 20 2023 at 11:08 UTC