Inductive ty: Set :=
| I
| O.
Definition f (x: ty) (y: ty): nat :=
if x = y then 0 else 1.
我想要的功能f
比较ty
类型的两个方面,但它并不能编译,我看到这个错误:如何比较Coq中相同Set的两个元素(相等)?
The term
x = y
has typeProp
which is not a (co-)inductive type.
Inductive ty: Set :=
| I
| O.
Definition f (x: ty) (y: ty): nat :=
if x = y then 0 else 1.
我想要的功能f
比较ty
类型的两个方面,但它并不能编译,我看到这个错误:如何比较Coq中相同Set的两个元素(相等)?
The term
x = y
has typeProp
which is not a (co-)inductive type.
你需要证明平等是可判定为ty
(这是可以做到自动使用decide equality
),然后在if ... then ... else ...
语句中使用该定义。具体来说:
Inductive ty: Set :=
| I
| O.
Definition ty_eq_dec : forall (x y : ty), { x = y } + { x <> y }.
Proof.
decide equality.
Defined.
Definition f (x: ty) (y: ty): nat :=
if ty_eq_dec x y then 0 else 1.
您可以使用match
来感应数据类型的元素进行比较。
Definition f x y := match x,y with I, I | O, O => 0 | _,_ => 1 end.
decide equality
是一个更普遍的策略,并适用于无限集,但它是很好的知道,这是match
是做实际工作。
你可以通过注意到这个函数满足所有xy,fxy = 0 <-> x = y在这种情况下可以很容易地证明这一点。 – Yves
你能告诉我更多关于{} + {}的信息吗?我认为它就像Proposition1或Prop2。但是这种方式不行。有没有像他们分裂? – abhishek
@abhishek请参阅[sumbool](https://coq.inria.fr/library/Coq.Bool.Sumbool.html)。例如,更多解释[本博客文章](http://seb.mondet.org/blog/post/coqtests-02-sumbools.html) – gallais