如何在不消除类型的情况下在 Coq 中证明时在 Prop 上进行模式匹配

How to pattern match on a Prop when proving in Coq without elimination on Type

我试图证明排序列表的尾部在 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

这似乎是一个错误。我认为问题出在以下部分:

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 中输入此代码段,您会收到与您看到的完全相同的错误消息。