I have defined the type of a polymorphic list and its constructors, and now trying to write a fonction that concatenate 2 lists but my function concat does not work
Definition listA A :Set :=
forall T :Set, T -> (A -> T -> T) -> T.
Definition pnil (A :Set) : listA A :=
fun T: Set => fun (x : T ) => fun (c : A -> T -> T) => x.
Definition pcons (A :Set): A -> (listA A) -> (listA A):=
fun (a : A) (q : listA A) => fun T : Set => fun (x : T ) => fun (c : A -> T -> T) => c a (q T x c).
Definition concat (A :Set ) (a b:listA A): listA A :=
a A b ( pcons A a b).
error I get for the function concat
In environment
A : Set
a : listeA A
b : listeA A
The term "b" has type "listeA A" while it is expected to have type "A".
There are two issues with your function: first, you use
ato build somelistA Aand not someA, so its first argument should belistA Aand notA. Second, its third argument is also incorrect: in theconscase, you simply want to reuseconsagain, rather than usingb, so this third argument should bepcons Ainstead ofpcons A a b.In the end, you get
If you want to test it and convince yourself it does the right thing, you might want to play with
that convert from your impredicative encoding to the usual datatype and back. For instance, you can do
and see that the result is indeed
[0;1;2;3].