[英]Equality in COQ for enumerated types
I have a finite enumerated type (say T) in COQ and want to check elements for equality. 我在COQ中有一个有限的枚举类型(例如T),想检查元素是否相等。 This means, I need a function
这意味着我需要一个功能
bool beq_T(x:T,y:T)
The only way I managed to define such a function, is with a case by case analysis. 我设法定义这种功能的唯一方法是逐案分析。 This results in lots of match statements and looks very cumbersome.
这导致出现许多匹配语句,并且看起来非常麻烦。 Therefore, I wonder if there is not a simpler way to achieve this.
因此,我想知道是否没有更简单的方法来实现这一目标。
甚至更简单:...的Scheme Equality for ...
生成两个函数,分别返回布尔值和sumbool。
The bad news is that the program that implements beq_T
must necessarily consist of a large match statement on both of its arguments. 坏消息是,实现
beq_T
的程序必须在其两个参数上都必须包含一个大的match语句。 The good news is that you can automatically generate/synthesize this program using Coq's tactic language . 好消息是,您可以使用Coq的战术语言自动生成/合成该程序。 For example, given the type:
例如,给定类型:
Inductive T := t0 | t1 | t2 | t3.
You can define beq_T
as follows. 您可以如下定义
beq_T
。 The first two destruct
tactics synthesize the code necessary to match on both x
and y
. 前两个
destruct
策略合成,以匹配在两个所必需的代码x
和y
。 The match
tactic inspects the branch of the match that it is in, and depending on whether x = y
, the tactic either synthesises the program that returns true
or false
. match
策略检查match
的分支,并且根据x = y
,该策略综合返回true
或false
的程序。
Definition beq_T (x y:T) : bool.
destruct x eqn:?;
destruct y eqn:?;
match goal with
| _:x = ?T, _:y = ?T |- _ => exact true
| _ => exact false
end.
Defined.
If you want to see the synthesized program, run: 如果要查看综合程序,请运行:
Print beq_T.
Thankfully, Coq already comes with a tactic that does almost what you want. 值得庆幸的是,Coq已经提供了一种几乎可以满足您所需的策略。 It is called
decide equality
. 这称为
decide equality
。 It automatically synthesizes a program that decides whether two elements of a type T
are equal. 它自动合成一个程序,该程序确定类型
T
两个元素是否相等。 But instead of just returning a boolean value, the synthesized program returns a proof of the (in)equality of the two elements. 但是,合成程序不仅返回布尔值,还返回两个元素(不等式)的证明。
Definition eqDec_T (x y:T) : {x = y} + {x <> y}.
decide equality.
Defined.
With that program synthesized, it is easy to implement beq_T
. 通过综合该程序,可以轻松实现
beq_T
。
Definition beq_T' {x y:T} : bool := if eqDec_T x y then true else false.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.