简单证明Coq中的流

时间:2014-10-22 21:21:50

标签: stream coq proof

从CPDT获取代码,我想为简单流ones证明一个属性,该属性始终返回1

CoFixpoint ones : Stream Z := Cons 1 ones.

同样来自CPDT,我使用此函数从流中检索列表:

Fixpoint approx A (s:Stream A) (n:nat) : list A :=
    match n with
    | O => nil
    | S p => match s with
        | Cons h t => h :: approx A t p
        end
    end.

获取五个1的列表,例如:

Eval compute in approx Z ones 5.
= 1 :: 1 :: 1 :: 1 :: 1 :: nil
  : list Z

我如何证明,对于n的所有approx,该列表仅包含1?我甚至不确定如何制定这个。我是否应该使用nth n list之类的帮助功能作为列表,从n返回元素编号list?那个

forall (n length : nat), nth n1 (approx Z ones length) = 1

(或者可以使用Zeq代替=。)

我正朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:1)

我认为拥有比列表的逐点nth视图更一般的视图将更容易处理。这就是我要去的方式(证明是0自动化,以确保你看到一切):

Inductive all_ones : list Z -> Prop :=
  | nil_is_ones : all_ones nil (* nil is only made of ones *)
  (* if l is only made of ones, 1 :: l is too *)
  | cons_is_ones : forall l, all_ones l -> all_ones (cons 1%Z l)
  (* and these are the only option to build a list only made of ones
.

CoFixpoint ones : Stream Z := Cons 1%Z ones.

Fixpoint approx A (s:Stream A) (n:nat) : list A :=
    match n with
    | O => nil
    | S p => match s with
        | Cons h t => h :: approx A t p
        end
    end.

Lemma approx_to_ones : forall n, all_ones (approx _ ones n).
Proof.
induction n as [ | n hi]; simpl in *.
- now constructor.
- constructor.
  now apply hi.
Qed. 

如果您更喜欢all_ones的更实用的定义,则以下是一些等效的定义:

Fixpoint fix_all_ones (l: list Z) : Prop := match l with
  | nil => True
  | 1%Z :: tl => fix_all_ones tl
  | _ => False
end.

Fixpoint fix_bool_all_ones (l: list Z) : bool := match l with
  | nil => true
  | 1%Z :: tl => fix_bool_all_ones tl
  | _ => false
end.

Lemma equiv1 : forall l, all_ones l <-> fix_all_ones l.
Proof.
induction l as [ | hd tl hi]; split; intros h; simpl in *.
- now idtac.
- now constructor.
- destruct hd; simpl in *.
  + now inversion h; subst; clear h.
  + inversion h; subst; clear h. 
    now apply hi.
  + now inversion h; subst; clear h.
- destruct hd; simpl in *.
  + now case h.
  + destruct p; simpl in *.
    * now case h.
    * now case h.
    * constructor; now apply hi.
  + now case h.
Qed.

Lemma equiv2 : forall l, fix_all_ones l <-> fix_bool_all_ones l = true.
Proof.
induction l as [ | hd tl hi]; split; intros h; simpl in *.
- reflexivity.
- now idtac.
- destruct hd; simpl in *.
  + now case h.
  + destruct p; simpl in *.
    * now case h.
    * now case h.
    * now apply hi.
  + now case h.
- destruct hd; simpl in *.
  + discriminate h.
  + destruct p; simpl in *.
    * now case h.
    * now case h.
    * now apply hi.
  + discriminate h. 
Qed.

最佳,

V