二阶统一与重写

Second order unification with rewrite

我有如下引理,带有高阶参数:

Require Import Coq.Lists.List.

Lemma map_fst_combine:
  forall A B C (f : A -> C) (xs : list A) (ys : list B),
  length xs = length ys ->
  map (fun p => f (fst p)) (combine xs ys) = map f xs. 
Proof.
  induction xs; intros.
  * destruct ys; try inversion H.
    simpl. auto.
  * destruct ys; try inversion H.
    simpl. rewrite IHxs; auto.
Qed.

我想像 rewrite 一样使用它。如果我直接指定 f 它会起作用:

Parameter list_fun : forall {A}, list A -> list A.
Parameter length_list_fun : forall A (xs : list A), length (list_fun xs) = length xs.

Lemma this_works:
  forall (xs : list bool),
  map (fun p => negb (negb (fst p))) (combine xs (list_fun xs)) =  xs.
Proof.
  intros.
  rewrite map_fst_combine with (f := fun x => negb (negb x))
    by (symmetry; apply length_list_fun).
Admitted. 

但我真的不想那样做(在我的例子中,我想使用这个引理作为 autorewrite 集合的一部分)。但是

Lemma this_does_not:
  forall (xs : list bool),
  map (fun p => negb (negb (fst p))) (combine xs (list_fun xs)) =  xs.
Proof.
  intros.
  rewrite map_fst_combine.

失败

(* 
Error:
Found no subterm matching "map (fun p : ?M928 * ?M929 => ?M931 (fst p))
                             (combine ?M932 ?M933)" in the current goal.
*)

我是不是期望太高了,还是有什么办法可以做到这一点?

让我们定义合成运算符(或者您可能想重用 Coq.Program.Basics 中定义的运算符):

Definition comp {A B C} (g : B -> C) (f : A -> B) :=
  fun x : A => g (f x).
Infix "∘" := comp (at level 90, right associativity).

现在,让我们根据组合来表述 map_fst_combine 引理:

Lemma map_fst_combine:
  forall A B C (f : A -> C) (xs : list A) (ys : list B),
  length xs = length ys ->
  map (f ∘ fst) (combine xs ys) = map f xs.
Admitted.   (* the proof remains the same *)

现在我们需要一些辅助引理 autorewrite:

Lemma map_comp_lassoc A B C D xs (f : A -> B) (g : B -> C) (h : C -> D) :
  map (fun x => h (g (f x))) xs = map ((h ∘ g) ∘ f) xs.
Proof. reflexivity. Qed.

Lemma map_comp_lassoc' A B C D E xs (f : A -> B) (g : B -> C) (h : C -> D) (i : D -> E) :
  map (i ∘ (fun x => h (g (f x)))) xs = map ((i ∘ h) ∘ (fun x => g (f x))) xs.
Proof. reflexivity. Qed.

有以下提示

Hint Rewrite map_comp_lassoc map_comp_lassoc' map_fst_combine : mapdb.

我们能够进行自动重写并摆脱 fstcombine:

Lemma autorewrite_works xs :
  map (fun p => negb (negb (fst p))) (combine xs (list_fun xs)) = xs.
Proof.
  autorewrite with mapdb.
  (* 1st subgoal: map (negb ∘ negb) xs = xs *)
Admitted.

Lemma autorewrite_works' xs :
  map (fun p => negb (negb (negb (negb (fst p))))) (combine xs (list_fun xs)) = xs.
Proof.
  autorewrite with mapdb.
  (* 1st subgoal: map (((negb ∘ negb) ∘ negb) ∘ negb) xs = xs *)
Admitted.