[英]Boolean equality of lists in Coq?
I want to be able to compare two items of type "list" in Coq and get a boolean "true" or "false" for their equivalence. 我希望能够在Coq中比较两个类型为“list”的项,并为它们的等价获得一个布尔“true”或“false”。
Right now, I'm comparing the two lists this way: 现在,我正在用这种方式比较两个列表:
Eval vm_compute in (list 1 = list 2).
I get a Prop of the form: 我得到了表格的支柱:
= nil
:: (2 :: 3 :: nil)
:: (2 :: nil)
:: (3 :: nil) :: nil =
nil
:: (2 :: 3 :: nil)
:: (2 :: nil)
:: (3 :: nil) :: nil
: Prop
Obviously list1 = list2, so how do I get it to just return true or false? 显然list1 = list2,那么如何让它返回true或false?
I use the Mathematical Components Library boolean equality operators : 我使用数学组件库布尔等于运算符 :
From mathcomp Require Import all_ssreflect.
...
Eval vm_compute in list 1 == list 2
You can generate a boolean list equality function that takes as input a boolean equality over the elements automatically using Coq's commands: 您可以生成一个布尔列表相等函数,该函数使用Coq命令自动将元素的布尔相等性作为输入:
Require Import Coq.Lists.List Coq.Bool.Bool.
Import Coq.Lists.List.ListNotations.
Scheme Equality for list.
This prints: 这打印:
list_beq is defined
list_eq_dec is defined
where list_beq
is a boolean equality function on lists that takes as first parameter a comparison function for the lists elements and then two lists: 其中
list_beq
是列表上的布尔相等函数,它将列表元素的比较函数作为第一个参数,然后是两个列表:
Print list_beq.
Gives 给
list_beq =
fun (A : Type) (eq_A : A -> A -> bool) =>
fix list_eqrec (X Y : list A) {struct X} : bool :=
match X with
| [] => match Y with
| [] => true
| _ :: _ => false
end
| x :: x0 => match Y with
| [] => false
| x1 :: x2 => eq_A x x1 && list_eqrec x0 x2
end
end
: forall A : Type, (A -> A -> bool) -> list A -> list A -> bool
and 和
Check list_eq_dec
gives 给
list_eq_dec
: forall (A : Type) (eq_A : A -> A -> bool),
(forall x y : A, eq_A x y = true -> x = y) ->
(forall x y : A, x = y -> eq_A x y = true) -> forall x y : list A, {x = y} + {x <> y}
showing that list equality is decidable if the underlying types equality is agrees with leibniz equality. 如果底层类型相等与leibniz相等,则表明列表相等是可判定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.