The list_rec function has the type:
list_rec
: forall (A : Type) (P : list A -> Set),
P nil ->
(forall (a : A) (l : list A), P l -> P (a :: l)%list) ->
forall l : list A, P l
In all of the examples I've come up with, P is just a constant function that ignores the input list and returns the same type no matter what. For example, P might be fun _ : list A => nat or fun _ : list A => list B. What are some use cases for making the output of P dependent on the input? Why is the type of P list A -> Set instead of just Set?
We can, for example, use
list_recwith a non-constantPfunction to implement a function that converts a list to a vector (a length-indexed list).You can compare it with the standard definition of the
Vector.of_listfunction, which does exactly the same (tmeansVector.tin the following code), using explicit recursion instead of hiding it behind a recursion principle:A simple test:
Both function calls return the same result: