I'm trying to understand the vector type from the FStar tutorial:
type vector (a: Type) : nat -> Type =
| Nil : vector a 0
| Cons : hd: a -> n: nat -> tl: vector a n -> vector a (n + 1)
Constructing a vector - similarly to constructing ordinary lists - by Cons nat 3 Nil fails, while Cons nat 3 is accepted. Could someone explain to me where I'm wrong by reading Cons as requiring a tail parameter? Furthermore, how vectors with actual elements are created - or is it an "empty vector" type?
There is quite a bit of confusion here, sorry :)
The type argument
aofvectoris (for reasons that are also unclear to me) implicit forNilandCons. So when you writeCons natthenatargument is really thehdargument and F* infersato beType0(the type ofnat). So you're constructing a vector of types, which might not be what you intended.The reason
Cons nat 3 Nilfails is because the3argument is wrong and doesn't correspond to the length of theNillist.Cons nat 0 Nilworks and is the vector of size 1 containing the single typenat.The reason
Cons nat 3works is because you didn't yet give it thetlargument, so this is a partially applied constructor,Consapplied to 2 of its 3 arguments. The type ofCons nat 3isvector Type0 3 -> vector Type0 4, so a function that expects a vector of size 3 in order to produce a vector of type 4.Hope this helps.