lambda 演算中 2 个列表的串联

发布于 2025-01-14 01:44:14 字数 738 浏览 4 评论 0原文

我已经定义了多态列表的类型及其构造函数,现在尝试 编写一个连接 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2025-01-21 01:44:14

您的函数有两个问题:首先,您使用 a 构建一些 listA A 而不是一些 A,因此它的第一个参数应该是listA A 而不是 A。其次,它的第三个参数也是不正确的:在 cons 情况下,您只是想再次重用 cons,而不是使用 b,所以这第三个参数应该是 pcons A 而不是 pcons A a b

最后,

Definition concat (A :Set) (a b:listA A): listA A :=
    a (listA A) b (pcons A).

如果您想测试它并说服自己它做了正确的事情,您可能想尝试


Definition tolist (A : Set) (l : listA A) : list A :=
  l (list A) nil cons.

Fixpoint fromlist (A : Set) (l : list A) : listA A :=
  match l with
  | nil => pnil A
  | cons h t => pcons A h (fromlist A t)
  end.

从命令式编码到常用数据类型的转换,然后再转换回来。
例如,您可以执行

Eval compute in tolist nat (concat nat (fromlist nat [0 ; 1]) (fromlist nat [2;3])).

并看到结果确实是[0;1;2;3]

There are two issues with your function: first, you use a to build some listA A and not some A, so its first argument should be listA A and not A. Second, its third argument is also incorrect: in the cons case, you simply want to reuse cons again, rather than using b, so this third argument should be pcons A instead of pcons A a b.

In the end, you get

Definition concat (A :Set) (a b:listA A): listA A :=
    a (listA A) b (pcons A).

If you want to test it and convince yourself it does the right thing, you might want to play with


Definition tolist (A : Set) (l : listA A) : list A :=
  l (list A) nil cons.

Fixpoint fromlist (A : Set) (l : list A) : listA A :=
  match l with
  | nil => pnil A
  | cons h t => pcons A h (fromlist A t)
  end.

that convert from your impredicative encoding to the usual datatype and back.
For instance, you can do

Eval compute in tolist nat (concat nat (fromlist nat [0 ; 1]) (fromlist nat [2;3])).

and see that the result is indeed [0;1;2;3].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文