Zulip Chat Archive

Stream: lean4

Topic: Defining Vec as inductive with length as first argument


ohhaimark (Jul 14 2022 at 09:27):

I have Vec defined like this

inductive Vec' (α : Type)  :  Nat  Type :=
  | unit : Vec' α 0
  | cons : α  Vec' α n  Vec' α (n+1)

def Vec := flip Vec'

but I would like to have Vec : Nat -> Type -> Type as an inductive type directly. Is this possible?

François G. Dorais (Jul 14 2022 at 10:34):

The syntax for inductive types requires all parameters to occur before indices. In your definition α is a parameter and n is an index. If you reverse the order, then α is forced to be an index and that causes a universe bump:

inductive A.Vec : Type _  Nat  Type _ :=
  | unit : Vec α 0
  | cons : α  Vec α n  Vec α (n+1)

inductive B.Vec :  Nat  Type _  Type _ :=
  | unit : Vec 0 α
  | cons : α  Vec n α  Vec (n+1) α

#check A.Vec -- Type u_1 → Nat → Type u_1
#check B.Vec -- Nat → Type u_1 → Type (u_1+1)

Tomas Skrivan (Jul 14 2022 at 10:35):

But that results in an universe bump, which is quite undesirable.

François G. Dorais (Jul 14 2022 at 10:43):

In ancient times, both A.Vec and B.Vec would have a universe bump since the colon was the division between parameters and indices. The current behavior is already much improved.

François G. Dorais (Jul 14 2022 at 10:48):

Having mixed parameters and indices would require some deep changes. For example, in the InductiveVal structure, only the number of indices is recorded, which is sufficient since they are necessarily last.

Tomas Skrivan (Jul 14 2022 at 12:38):

I see, there are probably some deep theoretical reasons for it but for me such universe bumb would be unacceptable.

Mac (Jul 14 2022 at 16:19):

ohhaimark said:

but I would like to have Vec : Nat -> Type -> Type as an inductive type directly. Is this possible?

Why not just use an abbreviation? That is:

abbrev Vec (n : Nat) (α : Type) := Vec' α n

Henrik Böving (Jul 14 2022 at 16:19):

That's isomorphic to the flip suggestion they made no?

Mac (Jul 14 2022 at 16:22):

No, flip is a def not an abbrev (and is not marked @[reducible]) and neither is their def Vec, so their shorthand is not reducible, whereas the abbrev is (which, of course, matters for things like typeclasses).

ohhaimark (Jul 14 2022 at 22:54):

The only thing is that you don't get the generated definitions. Speaking of which, is there a wildcard export like:

namespace Vec
  export Vec' (*)
end Vec

Mac (Jul 14 2022 at 23:00):

Sadly, no


Last updated: Dec 20 2023 at 11:08 UTC