Coq 对一个术语应用替换的证明

Coq 对一个术语应用替换的证明,coq,coq-tactic,Coq,Coq Tactic,我试图证明对一个项应用空替换等于给定的项。 代码如下: Require Import Coq.Strings.String. Require Import Coq.Lists.List. Require Import Coq.Arith.EqNat. Require Import Recdef. Require Import Omega. Import ListNotations. Set Implicit Arguments. Inductive Term : Type := |

我试图证明对一个项应用空替换等于给定的项。 代码如下:

Require Import Coq.Strings.String.
Require Import Coq.Lists.List.
Require Import Coq.Arith.EqNat.
Require Import Recdef.
Require Import Omega.
Import ListNotations.
Set Implicit Arguments.



Inductive Term : Type :=
   | Var  : nat -> Term
   | Fun  : string  -> list Term -> Term.


Definition Subst : Type := list (nat*Term).



Definition maybe{X Y: Type} (x : X) (f : Y -> X) (o : option Y): X :=
   match o with
    |None   => x
    |Some a => f a
   end.

Fixpoint lookup {A B : Type} (eqA : A -> A -> bool) (kvs : list (A * B)) (k : A) : option B :=
   match kvs with
    |[]          => None
    |(x,y) :: xs => if eqA k x then Some y else lookup eqA  xs k
   end.
我试图证明这个函数的一些性质

Fixpoint apply (s : Subst) (t : Term) : Term :=
   match t with
    | Var x     => maybe (Var x) id (lookup beq_nat s x )
    | Fun f ts => Fun f (map (apply s ) ts)
   end.


Lemma empty_apply_on_term:
  forall t, apply [] t = t.
Proof.
intros.
induction t.
reflexivity.
我在自反之后被卡住了。我想在一个学期内对列表构建进行归纳,但如果我这样做,我会陷入一个循环。
我将非常感谢您的帮助。

这是初学者的典型陷阱。问题在于,您对
术语的定义在另一个归纳类型中有递归出现——在本例中为
list
。不幸的是,Coq并没有为这些类型生成有用的归纳原理;你必须自己编程。Adam Chlipala的CDPT描述了这个问题。只需寻找“嵌套归纳类型”。

问题在于,
术语
类型的自动生成归纳原则太弱,因为它内部有另一个归纳类型
列表
(具体来说,
列表
应用于正在构造的类型)。Adam Chlipala的CPDT很好地解释了正在发生的事情,并举例说明了如何手动构建一个更好的归纳原则,以用于未来的此类类型。我已经将他的示例
nat\u tree\u ind'
原则改编为您的
术语
归纳,使用内置的
而不是自定义定义。有了它,你的定理变得很容易证明:

Section Term_ind'.
  Variable P : Term -> Prop.

  Hypothesis Var_case : forall (n:nat), P (Var n).

  Hypothesis Fun_case : forall (s : string) (ls : list Term),
      Forall P ls -> P (Fun s ls).

  Fixpoint Term_ind' (tr : Term) : P tr :=
    match tr with
    | Var n => Var_case n
    | Fun s ls =>
      Fun_case s
               ((fix list_Term_ind (ls : list Term) : Forall P ls :=
                   match ls with
                   | [] => Forall_nil _
                   | tr'::rest => Forall_cons tr' (Term_ind' tr') (list_Term_ind rest)
                   end) ls)
    end.

End Term_ind'.


Lemma empty_apply_on_term:
  forall t, apply [] t = t.
Proof.
  intros.
  induction t using Term_ind'; simpl; auto.
  f_equal.
  induction H; simpl; auto.
  congruence.
Qed.