代码之家  ›  专栏  ›  技术社区  ›  Attila Karoly

证明列表的性质

coq
  •  0
  • Attila Karoly  · 技术社区  · 8 年前

    我的目的是证明生成列表的某些性质成立。 例如,一个生成器函数生成一个1的列表,列表的长度作为参数给出;我想证明这个列表的长度是参数指定的。到目前为止,我的情况是:

    Require Import List.
    
    Fixpoint list_gen lng acc :=
    match lng with
    0 => acc
    | S lng_1 => list_gen (lng_1) (1::acc)
    end.
    
    Lemma lm0 : length(list_gen 0 nil) = 0.
    intuition.
    Qed.
    
    Lemma lm1 : forall lng:nat, length(list_gen lng nil) = lng.
    induction lng.
    apply lm0.
    

    1 subgoal
    lng : nat
    IHlng : length (list_gen lng nil) = lng
    ______________________________________(1/1)
    length (list_gen (S lng) nil) = S lng
    

    我原希望这一步的证明能从list\u gen的代码中推断出来,但这很可能是一个错误的概念。如何证明这个子目标?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Anton Trunov    8 年前

    我同意丹尼尔的方法,不过更一般的方法是写一个 list_gen ,例如使用 repeat 功能:

    Require Import List Arith.
    Import ListNotations.
    
    Lemma list_gen_spec : forall lng acc, list_gen lng acc = repeat 1 lng ++ acc.
    Proof.
      induction lng as [| lng IH]; intros xs; simpl; trivial.
      rewrite IH.
      now rewrite app_cons_middle, repeat_singleton, repeat_app, Nat.add_comm.
    Qed.
    

    重复 与一些标准列表函数的交互。

    Lemma repeat_singleton {A} (x : A) :
      [x] = repeat x 1.
    Admitted.
    
    Lemma repeat_app {A} (x : A) n m :
      repeat x n ++ repeat x m = repeat x (n + m).
    Admitted.
    
    Lemma app_cons {A} (x : A) xs :
      x :: xs = [x] ++ xs.
    Admitted.  (* this is a convenience lemma for the next one *)
    
    Lemma app_cons_middle {A} (y : A) xs ys :
      xs ++ y :: ys = (xs ++ [y]) ++ ys.
    Admitted.
    

    我把这些引理的证明留作练习。

    在证明了规范之后,你的引理可以用一些重写来证明。

    Lemma lm1 lng : length(list_gen lng nil) = lng.
    Proof.
      now rewrite list_gen_spec, app_nil_r, repeat_length.
    Qed.