如何在Coq中编写没有参数的定义?

时间:2010-09-06 15:59:27

标签: coq

我在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的联合。

Definition union_of_lists : natlist -> natlist -> natlist

Eval simpl in (union_of_lists [1,2,3] [1,4,1])       应该返回[1,2,3,1,4,1]

我有以下疑惑。

  • 由于此定义没有参数,我如何实际获取输入并处理它们?
  • union_of_lists定义的确切含义是什么?它只是一个natlist吗?

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

1 个答案:

答案 0 :(得分:1)

我自己找到了答案:)我做的是,我写了一个单独的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参数并返回{{ 1}})。

natlist的这个定义类似Functional Programming中的函数,可以返回函数或值。