使用重写策略时,Coq找不到子项

我试图从第一章使用依赖类型的认证编程中对compile_correct进行修改证明。 在我的版本中,我尝试使用progDenote是一个折叠的事实,并且使用较弱的归纳假设来证明compile_correct中的主要引理。

与本书相同的代码是:

Require Import Bool Arith List.
Set Implicit Arguments.

Inductive binop : Set := Plus | Times.

Inductive exp : Set :=
  | Const : nat -> exp
  | Binop : binop -> exp -> exp -> exp.

Definition binopDenote (b : binop) : nat -> nat -> nat :=
  match b with
    | Plus => plus
    | Times => mult
  end.

Fixpoint expDenote (e : exp) : nat :=
  match e with
    | Const n => n
    | Binop b e1 e2 => (binopDenote b) (expDenote e1) (expDenote e2)
  end.

Inductive instr : Set :=
  | iConst : nat -> instr
  | iBinop : binop -> instr.

Definition prog := list instr.
Definition stack := list nat.

Definition instrDenote (i : instr) (s : stack) : option stack :=
  match i with
    | iConst n => Some (n :: s)
    | iBinop b =>
      match s with
        | arg1 :: arg2 :: s' => Some ((binopDenote b) arg1 arg2 :: s')
        | _ => None
      end
  end.

Fixpoint compile (e : exp) : prog :=
  match e with
    | Const n => iConst n :: nil
    | Binop b e1 e2 => compile e2 ++ compile e1 ++ iBinop b :: nil
  end.

然后,我定义了我自己的prog_denote版本,它是程序中指令列表的折叠对象:

Definition bind {A B : Type} (a : option A) (f : A -> option B) : option B :=
  match a with
    | Some x => f x
    | None => None
  end.

Definition instrDenote' (s : option stack) (i : instr) : option stack :=
  bind s (instrDenote i).

Definition progDenote (p : prog) (s : stack) : option stack :=
  fold_left instrDenote' p (Some s).

然后我尝试从书中证明compile_correct的较弱版本:

Lemma compile_correct' : forall e s,
  progDenote (compile e) s = Some (expDenote e :: s).
induction e.
intro s.
unfold compile.
unfold expDenote.
unfold progDenote at 1.
simpl.
reflexivity.
intro s.
unfold compile.
fold compile.
unfold expDenote.
fold expDenote.
unfold progDenote.
rewrite fold_left_app.
rewrite fold_left_app.
unfold progDenote in IHe2.
rewrite (IHe2 s).
unfold progDenote in IHe1.
rewrite (IHe1 (expDenote e2 :: s)).

我的证明在最后一行打破了证明状态

1 subgoal
b : binop
e1 : exp
e2 : exp
IHe1 : forall s : stack,
       fold_left instrDenote' (compile e1) (Some s) =
       Some (expDenote e1 :: s)
IHe2 : forall s : stack,
       fold_left instrDenote' (compile e2) (Some s) =
       Some (expDenote e2 :: s)
s : stack
______________________________________(1/1)
fold_left instrDenote' (iBinop b :: nil)
  (fold_left instrDenote' (compile e1) (Some (expDenote e2 :: s))) =
Some (binopDenote b (expDenote e1) (expDenote e2) :: s)

而错误是

Error:
Found no subterm matching "fold_left instrDenote' (compile e1)
                             (Some (expDenote e2 :: s))" in the current goal.

在证明的这个阶段,我正在对e进行归纳,编译表达式,并处理expBinop构造函数。 我不明白为什么我得到这个错误,因为一旦我将IHe1应用于expDenote e2 :: s ,就没有绑定变量。 这似乎是应用重写规则不起作用的常见问题。 我还检查了我试图创建的术语:

fold_left instrDenote' (iBinop b :: nil)
  (Some (expDenote e1 :: expDenote e2 :: s)) =
Some (binopDenote b (expDenote e1) (expDenote e2) :: s)

键入检查。

重写规则还有什么可能出错,当它所抱怨的子表达式显然存在于目标中时?

编辑:如所暗示的,我将coqide中的显示设置更改为等同于“设置全部打印”。 这表明问题在于stack的定义已经展开,以便在目标中的某个位置list nat ,从而避免了子目录被识别。 用新设置打印的目标是

1 subgoal
b : binop
e1 : exp
e2 : exp
IHe1 : forall s : stack,
       @eq (option stack)
         (@fold_left (option stack) instr instrDenote' (compile e1)
            (@Some stack s)) (@Some (list nat) (@cons nat (expDenote e1) s))
IHe2 : forall s : stack,
       @eq (option stack)
         (@fold_left (option stack) instr instrDenote' (compile e2)
            (@Some stack s)) (@Some (list nat) (@cons nat (expDenote e2) s))
s : stack
______________________________________(1/1)
@eq (option stack)
  (@fold_left (option stack) instr instrDenote'
     (@cons instr (iBinop b) (@nil instr))
     (@fold_left (option stack) instr instrDenote' (compile e1)
        (@Some (list nat) (@cons nat (expDenote e2) s))))
  (@Some (list nat)
     (@cons nat (binopDenote b (expDenote e1) (expDenote e2)) s))

而错误是

Error:
Found no subterm matching "@fold_left (option stack) instr instrDenote'
                             (compile e1)
                             (@Some stack (@cons nat (expDenote e2) s))" in the current goal.

尽管使用默认显示设置,但子目录似乎出现在目标中,并且启用了“ Set Printing All功能,但显然子目录与目标不匹配,因为在目标中, stack已展开到list nat 。 因此需要fold stack来将list nat重新转换为目标stack

这看起来像是一个初学者,我被以下的组合绊倒了:

  • unfold策略展现了比初学者期望更多的定义。

  • 默认显示设置(在我的案例中是CoqIDE)可以隐藏这个,因为它们折叠了一些术语。

  • 感谢Arthur Azevedo De Amorim建议启用Set Printing All

    链接地址: http://www.djcxy.com/p/26709.html

    上一篇: Coq can't find subterm when using rewrite tactic

    下一篇: virtual inheritance constructor order