lambda 演算中 2 个列表的串联
我已经定义了多态列表的类型及其构造函数,现在尝试 编写一个连接 2 个列表的函数,但我的函数 concat 不起作用
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).
我收到函数 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".
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的函数有两个问题:首先,您使用
a
构建一些listA A
而不是一些A
,因此它的第一个参数应该是listA A
而不是A
。其次,它的第三个参数也是不正确的:在cons
情况下,您只是想再次重用cons
,而不是使用b
,所以这第三个参数应该是pcons A
而不是pcons A a b
。最后,
如果您想测试它并说服自己它做了正确的事情,您可能想尝试
从命令式编码到常用数据类型的转换,然后再转换回来。
例如,您可以执行
并看到结果确实是
[0;1;2;3]
。There are two issues with your function: first, you use
a
to build somelistA A
and not someA
, so its first argument should belistA A
and notA
. Second, its third argument is also incorrect: in thecons
case, you simply want to reusecons
again, rather than usingb
, so this third argument should bepcons A
instead 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]
.