1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(*i $Id: Streams.v 9967 2007-07-11 15:25:03Z roconnor $ i*)
11
Set Implicit Arguments.
19
CoInductive Stream : Type :=
20
Cons : A -> Stream -> Stream.
23
Definition hd (x:Stream) := match x with
27
Definition tl (x:Stream) := match x with
32
Fixpoint Str_nth_tl (n:nat) (s:Stream) {struct n} : Stream :=
35
| S m => Str_nth_tl m (tl s)
38
Definition Str_nth (n:nat) (s:Stream) : A := hd (Str_nth_tl n s).
42
forall x:Stream, x = match x with
43
| Cons a s => Cons a s
52
forall (n:nat) (s:Stream), tl (Str_nth_tl n s) = Str_nth_tl n (tl s).
54
simple induction n; simpl in |- *; auto.
56
Hint Resolve tl_nth_tl: datatypes v62.
58
Lemma Str_nth_tl_plus :
59
forall (n m:nat) (s:Stream),
60
Str_nth_tl n (Str_nth_tl m s) = Str_nth_tl (n + m) s.
61
simple induction n; simpl in |- *; intros; auto with datatypes.
63
rewrite tl_nth_tl; trivial with datatypes.
67
forall (n m:nat) (s:Stream), Str_nth n (Str_nth_tl m s) = Str_nth (n + m) s.
68
intros; unfold Str_nth in |- *; rewrite Str_nth_tl_plus;
69
trivial with datatypes.
72
(** Extensional Equality between two streams *)
74
CoInductive EqSt (s1 s2: Stream) : Prop :=
76
hd s1 = hd s2 -> EqSt (tl s1) (tl s2) -> EqSt s1 s2.
78
(** A coinduction principle *)
80
Ltac coinduction proof :=
81
cofix proof; intros; constructor;
82
[ clear proof | try (apply proof; clear proof) ].
85
(** Extensional equality is an equivalence relation *)
87
Theorem EqSt_reflex : forall s:Stream, EqSt s s.
88
coinduction EqSt_reflex.
92
Theorem sym_EqSt : forall s1 s2:Stream, EqSt s1 s2 -> EqSt s2 s1.
94
case H; intros; symmetry in |- *; assumption.
95
case H; intros; assumption.
100
forall s1 s2 s3:Stream, EqSt s1 s2 -> EqSt s2 s3 -> EqSt s1 s3.
101
coinduction Eq_trans.
102
transitivity (hd s2).
103
case H; intros; assumption.
104
case H0; intros; assumption.
105
apply (Eq_trans (tl s1) (tl s2) (tl s3)).
106
case H; trivial with datatypes.
107
case H0; trivial with datatypes.
110
(** The definition given is equivalent to require the elements at each
111
position to be equal *)
114
forall (n:nat) (s1 s2:Stream), EqSt s1 s2 -> Str_nth n s1 = Str_nth n s2.
115
unfold Str_nth in |- *; simple induction n.
116
intros s1 s2 H; case H; trivial with datatypes.
121
case H; trivial with datatypes.
126
(forall n:nat, Str_nth n s1 = Str_nth n s2) -> EqSt s1 s2.
129
intros n; apply (H (S n)).
132
Section Stream_Properties.
134
Variable P : Stream -> Prop.
137
Inductive Exists : Stream -> Prop :=
138
| Here : forall x:Stream, P x -> Exists x
139
| Further : forall x:Stream, ~ P x -> Exists (tl x) -> Exists x.
142
Inductive Exists ( x: Stream ) : Prop :=
143
| Here : P x -> Exists x
144
| Further : Exists (tl x) -> Exists x.
146
CoInductive ForAll (x: Stream) : Prop :=
147
HereAndFurther : P x -> ForAll (tl x) -> ForAll x.
149
Lemma ForAll_Str_nth_tl : forall m x, ForAll x -> ForAll (Str_nth_tl m x).
159
Section Co_Induction_ForAll.
160
Variable Inv : Stream -> Prop.
161
Hypothesis InvThenP : forall x:Stream, Inv x -> P x.
162
Hypothesis InvIsStable : forall x:Stream, Inv x -> Inv (tl x).
164
Theorem ForAll_coind : forall x:Stream, Inv x -> ForAll x.
165
coinduction ForAll_coind; auto.
167
End Co_Induction_ForAll.
169
End Stream_Properties.
174
Variables A B : Type.
176
CoFixpoint map (s:Stream A) : Stream B := Cons (f (hd s)) (map (tl s)).
178
Lemma Str_nth_tl_map : forall n s, Str_nth_tl n (map s)= map (Str_nth_tl n s).
187
Lemma Str_nth_map : forall n s, Str_nth n (map s)= f (Str_nth n s).
191
rewrite Str_nth_tl_map.
195
Lemma ForAll_map : forall (P:Stream B -> Prop) (S:Stream A), ForAll (fun s => P
196
(map s)) S <-> ForAll P (map S).
199
split; generalize S; clear S; cofix; intros S; constructor;
200
destruct H as [H0 H]; firstorder.
203
Lemma Exists_map : forall (P:Stream B -> Prop) (S:Stream A), Exists (fun s => P
204
(map s)) S -> Exists P (map S).
207
(induction H;[left|right]); firstorder.
212
Section Constant_Stream.
215
CoFixpoint const : Stream A := Cons a const.
220
Variable A B C : Type.
221
Variable f: A -> B -> C.
223
CoFixpoint zipWith (a:Stream A) (b:Stream B) : Stream C :=
224
Cons (f (hd a) (hd b)) (zipWith (tl a) (tl b)).
226
Lemma Str_nth_tl_zipWith : forall n (a:Stream A) (b:Stream B),
227
Str_nth_tl n (zipWith a b)= zipWith (Str_nth_tl n a) (Str_nth_tl n b).
231
intros [x xs] [y ys].
237
Lemma Str_nth_zipWith : forall n (a:Stream A) (b:Stream B), Str_nth n (zipWith a
238
b)= f (Str_nth n a) (Str_nth n b).
242
rewrite Str_nth_tl_zipWith.
248
Unset Implicit Arguments.