我一直在尝试使用coinductive类型,并决定定义自然数和矢量的共同版本(类型中的大小列表)。我将它们和无限数量定义为:
CoInductive conat : Set :=
| cozero : conat
| cosuc : conat -> conat.
CoInductive covec (A : Set) : conat -> Set :=
| conil : covec A cozero
| cocons : forall (n : conat), A -> covec A n -> covec A (cosuc n).
CoFixpoint infnum : conat := cosuc infnum.
除了我为无限飞行器提供的定义外,这一切都有效。
CoFixpoint ones : covec nat infnum := cocons 1 ones.
给出了以下类型不匹配
Error:
In environment
ones : covec nat infnum
The term "cocons 1 ones" has type "covec nat (cosuc infnum)" while it is expected to have type
"covec nat infnum".
我认为编译器会接受这个定义,因为根据定义,infnum = cosuc infnum。如何让编译器理解这些表达式是否相同?
答案 0 :(得分:1)
Adam Chlipala的CPDT中描述了解决此问题的标准方法(参见Coinduction章节)。
Definition frob (c : conat) :=
match c with
| cozero => cozero
| cosuc c' => cosuc c'
end.
Lemma frob_eq (c : conat) : c = frob c.
Proof. now destruct c. Qed.
您可以使用上述定义:
CoFixpoint ones : covec nat infnum.
Proof. rewrite frob_eq; exact (cocons 1 ones). Defined.
或者,或许,更可读的方式:
Require Import Coq.Program.Tactics.
Program CoFixpoint ones : covec nat infnum := cocons 1 ones.
Next Obligation. now rewrite frob_eq. Qed.