我如何比较(相等)Coq 中同一 Set 的两个元素?
How can I compare (equality) of two elements of same Set in Coq?
Inductive ty: Set :=
| I
| O.
Definition f (x: ty) (y: ty): nat :=
if x = y then 0 else 1.
我希望函数 f
比较两个 ty
类型的术语,但它无法编译,我看到了这个错误:
The term x = y
has type Prop
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
。
Inductive ty: Set :=
| I
| O.
Definition f (x: ty) (y: ty): nat :=
if x = y then 0 else 1.
我希望函数 f
比较两个 ty
类型的术语,但它无法编译,我看到了这个错误:
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
。