使用Coq

时间:2017-09-24 13:19:30

标签: coq

假设我们有一个归纳数据结构和一些谓词:

Inductive A : EClass :=
   X | Y .

Definition P (a: A) : bool :=
 match a with
   X => true
 | Y => false
 end.

然后,我制定一个定理,说明存在一个元素a,使P a返回true:

Theorem test :
 exists a: A, P a.

可能有各种各样的方法,我正在考虑如何使用案例分析来证明它,在我看来,它的工作原理如下:

  • 记住有两种方法A可以构建
  • 逐一尝试,如果我们找到P a持有的证人,请停止。

我的Coq代码如下:

evar (a: A). (* introduce a candidate to manipulate *)
destruct a eqn: case_A. (* case analysis *)
  - (* case where a = X *) 
    exists a.
    rewrite case_A.
    done.  
  - (* case where a = Y *) 
    (* stuck *)

我的问题是,

  • 我的证明策略在逻辑上存在缺陷吗?
  • 如果没有,我的Coq是问题,我怎么能向Coq传达我的工作已经完成,我找到了一个证人?可能我不应该destruct

谢谢!

2 个答案:

答案 0 :(得分:3)

是的,你的证据有缺陷!您需要的只是首先提供证人:

Inductive A := X | Y .

Definition P (a: A) : bool := match a with X => true | Y => false end.

Theorem test : exists a: A, P a = true.
Proof. now exists X. Qed.

如果您先进行案例分析,那么您将陷入死胡同。

答案 1 :(得分:3)

这是一个粗略的框架,演示了如何编写Coq策略来尝试将有限类型的所有元素作为证人。

(* Typeclass to register an enumeration of elements of a type. *)
Class Enumeration (A:Type) :=
enumerate : list A.
Arguments enumerate A [Enumeration].

(* Typeclass to register decision procedures to determine whether
   a given proposition is true or false. *)
Class Decision (P:Prop) :=
decide : {P} + {~P}.
Arguments decide P [Decision].

(* Given a Coq list l, execute tactic t on every element of
   l until we get a success. *)
Ltac try_list l t :=
match (eval hnf in l) with
| @cons _ ?hd ?tl => (t hd || try_list tl t)
end.
(* Tactic for "proof by reflection": use a decision procedure, and
   if it returns "true", then extract the proof from the result. *)
Ltac by_decision :=
match goal with
|- ?P => let res := (eval hnf in (decide P)) in
         match res with
         | left ?p => exact p
         end
end.
(* Combination to try to prove an (exists x:A, P) goal by trying
   to prove P by reflection for each element in an enumeration of A. *)
Ltac try_enumerate :=
match goal with
|- @ex ?A ?P =>
  try_list (enumerate A)
    ltac:(fun x => exists x; by_decision)
end.

(* Demonstration on your example *)
Inductive A := X | Y.
Instance A_enum : Enumeration A :=
cons X (cons Y nil).
Instance bool_eq_dec : forall x y:bool,
  Decision (x = y).
Proof.
intros. red. decide equality.
Defined.

Definition P (a:A) : bool :=
match a with
| X => true
| Y => false
end.

Goal exists a:A, P a = true.
Proof.
try_enumerate.
Qed.
相关问题