关于具有Coq编程经验,
我想知道是否还有其他编码方式,而不是我的编码方式,是在检查a function is non-decreasing时使用它们的前置关系。我在下面的代码中将程序的数据类型和函数替换为简单的数据类型和函数。
mydata
是具有前置关系(即具有自反和传递关系)的集合,并且是将{nat数字映射到mappingFunction
的函数mydata
的范围。我在对定理mappingFunction_isIncreasing
进行编码时无法在mydata上定义前置关系,因此,作为一种解决方案,我通过mydata
将dataparts_toNat
映射到nat数字以使我能够在它们之间定义前置关系。我想知道是否还有其他方法可以执行此程序,例如,没有dataparts_toNat
。谢谢。
(*defining the datatype*)
Inductive mydata : Set :=
| part1 : mydata
| part2 : mydata
|part3 :mydata.
(* mapping to nats to have
preorder relation(part1<part2<part3 and part1=part1 and part2=part2 and part3=part3)*)
Definition dataparts_toNat (n:mydata):nat :=
match n with
|part1 => 0
|part2 => 1
|part3 => 2
end.
(* a sample function from nat to mydata which is always increasing or not changing*)
Definition mappingFunction
(a1:nat): mydata :=
match a1 with
|0=> part1
|S(0) => part2
|_ => part3
end.
Theorem mappingFunction_isIncreasing: forall(a1 a2: nat)(data1 data2: mydata),
a1<=a2 -> (mappingFunction a1= data1 )/\(mappingFunction a2=
data2) -> ((dataparts_toNat data1) <= dataparts_toNat(data2)).
Proof.
答案 0 :(得分:1)
(* The definition of mydata again, for completeness *)
Inductive mydata : Set :=
| part1 : mydata
| part2 : mydata
| part3 : mydata.
您可以将比较定义为布尔函数mydata -> mydata -> bool
:
Definition le_mydata_dec (d1 d2 : mydata) : bool :=
match d1, d2 with
| part1, _ => true
| part2, (part2 | part3) => true
| part3, part3 => true
| _, _ => false
end.
然后从中得出比较关系mydata -> mydata -> Prop
(这只是一种方法,有时将le_mydata
定义为Inductive
命题会更方便)。
Definition le_mydata (d1 d2 : mydata) : Prop :=
le_mydata_dec d1 d2 = true.
映射功能相同(为简便起见,更名为f
)
(* a sample function from nat to mydata which is always increasing or not changing*)
Definition f
(a1:nat): mydata :=
match a1 with
|0=> part1
|S(0) => part2
|_ => part3
end.
现在这是单调的:
Theorem f_isMonotonic: forall(a1 a2: nat),
a1<=a2 -> le_mydata (f a1) (f a2).
Proof.
Abort.
您可以使用符号将le_mydata
替换为更漂亮的<=
。在此,我们通过将新的符号分配给以键<=
分隔的新作用域nat
来小心,不要隐藏用于比较mydata_scope
的预先存在的符号mydata
。 / p>
Infix "<=" := le_mydata : mydata_scope.
Delimit Scope mydata_scope with mydata.
(* now we can write (x <= y)%mydata instead of le_mydata x y *)
再次使用该符号表示单调性定理:
Theorem f_isMonotonic: forall(a1 a2: nat),
a1<=a2 -> (f a1 <= f a2)%mydata.
Proof.
Abort.