我试图证明排序列表的尾部在Coq中排序,使用模式匹配而不是策略:
Require Import Coq.Sorting.Sorted.
Definition tail_also_sorted {A : Prop} {R : relation A} {h : A} {t : list A}
(H: Sorted R (h::t)) : Sorted R t :=
match H in Sorted _ (h::t) return Sorted _ t with
| Sorted_nil _ => Sorted_nil R
| Sorted_cons rest_sorted _ => rest_sorted
end.
然而,这失败了:
Error:
Incorrect elimination of "H" in the inductive type "Sorted":
the return type has sort "Type" while it should be "Prop".
Elimination of an inductive object of sort Prop
is not allowed on a predicate in sort Type
because proofs can be eliminated only to build proofs.
我怀疑它可能存在于底层微积分中,如下面的精益代码类型检查,而精益也建立在CIC上:
inductive is_sorted {α: Type} [decidable_linear_order α] : list α -> Prop
| is_sorted_zero : is_sorted []
| is_sorted_one : ∀ (x: α), is_sorted [x]
| is_sorted_many : ∀ {x y: α} {ys: list α}, x < y -> is_sorted (y::ys) -> is_sorted (x::y::ys)
lemma tail_also_sorted {α: Type} [decidable_linear_order α] : ∀ {h: α} {t: list α},
is_sorted (h::t) -> is_sorted t
| _ [] _ := is_sorted.is_sorted_zero
| _ (y::ys) (is_sorted.is_sorted_many _ rest_sorted) := rest_sorted
答案 0 :(得分:2)
这似乎是一个错误。我认为,问题在于以下部分:
in Sorted _ (h::t)
在纯CIC中,不允许在match
表达式上使用此类注释。相反,你需要写这样的东西:
Definition tail_also_sorted {A : Prop} {R : relation A} {h : A} {t : list A}
(H: Sorted R (h::t)) : Sorted R t :=
match H in Sorted _ t'
return match t' return Prop with
| [] => True
| h :: t => Sorted R t
end with
| Sorted_nil _ => I
| Sorted_cons rest_sorted _ => rest_sorted
end.
不同之处在于in
子句中的索引现在是一个绑定在return
子句中的新变量。为了减轻您编写这些可怕程序的麻烦,Coq允许您在in
子句中放置比通用变量稍微复杂的表达式,就像您拥有的那样。为避免损害稳健性,此扩展实际上已编译为核心CIC术语。我想这个翻译的某个地方有一个错误就是生成以下术语:
Definition tail_also_sorted {A : Prop} {R : relation A} {h : A} {t : list A}
(H: Sorted R (h::t)) : Sorted R t :=
match H in Sorted _ t'
return match t' return Type with
| [] => True
| h :: t => Sorted R t
end with
| Sorted_nil _ => I
| Sorted_cons rest_sorted _ => rest_sorted
end.
注意return Type
注释。实际上,如果您尝试在Coq中输入此代码段,则会得到与您所看到的完全相同的错误消息。