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

时间:2015-05-31 22:10:44

标签: coq theorem-proving

我正在尝试从依赖类型的认证编程first chapter中修改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进行归纳,正在编译表达式,并处理Binop的{​​{1}}构造函数。我不明白为什么我收到此错误,因为一旦我将exp应用于IHe1,就没有绑定变量。这似乎是应用重写规则不起作用的常见问题。我还检查了我正在尝试创建的术语:

expDenote e2 :: s

类型检查。

重写规则还有什么问题,当它所抱怨的子表达式明显出现在目标中时?

编辑:根据建议,我将coqide中的显示设置更改为相当于Set Printing All。这揭示了问题是fold_left instrDenote' (iBinop b :: nil) (Some (expDenote e1 :: expDenote e2 :: s)) = Some (binopDenote b (expDenote e1) (expDenote e2) :: s) 的定义已经在目标的一个地方展开到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))

1 个答案:

答案 0 :(得分:6)

即使使用默认显示设置,子项似乎也会出现在目标中,但启用了Set Printing All,很明显子项与目标不匹配,因为在目标中,stack已经展开到list nat。因此,需要fold stack才能将list nat转回目标中的stack

似乎作为初学者,我被以下组合绊倒了:

  • unfold策略展开的定义多于初学者所期望的定义。

  • 默认显示设置(在我的情况下为CoqIDE)可以隐藏这一点,因为它们会折叠一些术语。

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