如何在 Coq 中编写不带参数的定义?

发布于 2024-09-18 02:05:09 字数 742 浏览 1 评论 0原文

我在 Coq 中定义了以下归纳类型。

Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.

Notation "x :: l" := (cons x l) (at level 60, right associativity). 
Notation "[ ]" := nil.
Notation "[ x , .. , y ]" := (cons x .. (cons y  nil) ..).

natlist 基本上是一个自然数列表(类似于 Python 中的列表)。我试图使用下面的定义找到两个 natlist 的并集。

定义 union_of_lists : natlist ->纳特列表 -> natlist

在 (union_of_lists [1,2,3] [1,4,1]) 中计算 simpl 应该返回 [1,2,3,1,4,1]

我有以下疑问。

  • 由于这个定义没有参数,我如何实际获取输入并处理它们?
  • union_of_lists 的定义到底返回什么?它只是一个 natlist 吗?

非常感谢任何帮助或提示。

I have the following Inductive type defined in Coq.

Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.

Notation "x :: l" := (cons x l) (at level 60, right associativity). 
Notation "[ ]" := nil.
Notation "[ x , .. , y ]" := (cons x .. (cons y  nil) ..).

The natlist is basically a list of natural numbers (similar to lists in Python). I am trying to find the union of two natlist using the definition below.

Definition union_of_lists : natlist -> natlist -> natlist

i.e
Eval simpl in (union_of_lists [1,2,3] [1,4,1])
should return [1,2,3,1,4,1]

I have the following doubts.

  • Since there are no arguments to this definition, how do I actually get the inputs and handle them?
  • What does the definition union_of_lists return exactly? Is it just a natlist?

Any help or hints are highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

七七 2024-09-25 02:05:09

我自己找到了答案:)我所做的是,我编写了一个单独的 Fixpoint 函数 append ,然后将其分配给 union_of_lists 的定义。

Fixpoint append(l1 l2 : natlist) : natlist :=
  match l1 with
  | nil => l2
  | (h :: t) => h :: app t l2
  end.`

然后

Definition union_of_lists : natlist -> natlist -> natlist := append.

Eval simpl in (append [1,2,3] [1,2,3]) (* returns [1,2,3,1,2,3] *)

定义 union_of_lists 返回一个函数,该函数以 natlist 作为参数,并返回另一个 natlist -> 类型的函数natlist(即接受一个natlist参数并返回一个natlist的函数)。

union_of_lists 的这个定义类似于函数式编程中的函数,它可以返回一个函数或一个值。

I found the answer myself :) What I did was, I wrote a separate Fixpoint function append and then assigned it to the definition of union_of_lists.

Fixpoint append(l1 l2 : natlist) : natlist :=
  match l1 with
  | nil => l2
  | (h :: t) => h :: app t l2
  end.`

and then

Definition union_of_lists : natlist -> natlist -> natlist := append.

Eval simpl in (append [1,2,3] [1,2,3]) (* returns [1,2,3,1,2,3] *)

The definition union_of_lists returns a function which takes natlist as an argument and returns another function of type natlist -> natlist (i.e function taking a natlist argument and returning a natlist).

This definition of union_of_lists resembles functions in Functional Programming which can either return a function or a value.

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