File: ListQueue.v

package info (click to toggle)
paramcoq 1.1.3%2Bcoq8.16-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 444 kB
  • sloc: ml: 1,677; python: 112; sh: 61; makefile: 54
file content (228 lines) | stat: -rw-r--r-- 4,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Require Import Parametricity.

Require Import List.
Import ListNotations.

Definition bind_option {A B} (f : A -> option B) (x : option A) : 
  option B := 
  match x with 
   | Some x => f x
   | None => None
  end.

Notation "'do' X <- A 'in' B" := (bind_option (fun X  => B) A)
 (at level 200, X ident, A at level 100, B at level 200).

Definition bind_option2 {A B C} (f : A -> B -> option C) 
   (x : option (A * B)) : option C := 
  do yz <- x in let (y, z) := yz : A * B in f y z.

Notation "'do' X , Y   <- A 'in' B" := (bind_option2 (fun X Y => B) A)
 (at level 200, X ident, Y ident, A at level 100, B at level 200).


      
Require Import List.


Record Queue := {
  t :> Type;
  empty : t;
  push : nat -> t -> t;
  pop : t -> option (nat * t) 
}.


Definition program (Q : Queue) (n : nat) : option nat :=
   (* q := 0::1::2::...::n *)
   let q : Q := 
     nat_rect (fun _ => Q) Q.(empty) Q.(push) (S n)
   in 
   let q : option Q := nat_rect (fun _ => option Q) (Some q)
     (fun _ (q : option Q) => 
         do q <- q in
         do x, q <- Q.(pop) q in
         do y, q <- Q.(pop) q in
         Some (Q.(push) (x + y) q)) n 
   in 
   do q <- q in 
   option_map fst (Q.(pop) q).

Definition ListQueue := {|
  t := list nat; 
  empty := nil; 
  push := @cons nat;
  pop := fun l => 
    match rev l with
      | nil => None 
      | hd :: tl => Some (hd, rev tl) end
|}.

Definition DListQueue := {|
  t := list nat * list nat; 
  empty := (nil, nil);
  push x l := 
    let (back, front) := l in 
    (cons x back,front);
  pop := fun l =>
    let (back, front) := l in 
    match front with 
      | [] => 
         match rev back with
            | [] => None
            | hd :: tl => Some (hd, (nil, tl))
         end
      | hd :: tl => Some (hd, (back, tl))
    end
|}.

Parametricity Recursive nat.

Print nat_R.

Lemma nat_R_equal : 
  forall x y, nat_R x y -> x = y.
intros x y H; induction H; subst; trivial.
Defined.
Lemma equal_nat_R : 
  forall x y, x = y -> nat_R x y.
intros x y H; subst.
induction y; constructor; trivial.
Defined.

Parametricity Recursive option.

Lemma option_nat_R_equal : 
  forall x y, option_R nat nat nat_R x y -> x = y.
intros x1 x2 H; destruct H as [x1 x2 x_R | ].
rewrite (nat_R_equal _ _ x_R); reflexivity.
reflexivity.
Defined.
Lemma equal_option_nat_R : 
  forall x y, x = y -> option_R nat nat nat_R x y.
intros x y H; subst.
destruct y; constructor; apply equal_nat_R; reflexivity.
Defined.

Parametricity Recursive prod.
Parametricity Recursive Queue.

Print Queue_R.
Check Queue_R.

Notation Bisimilar := Queue_R.

Print Queue_R.


Definition R (l1 : list nat) (l2 : list nat * list nat) :=
 let (back, front) := l2 in 
  l1 = back ++ rev front.

Lemma rev_app : 
  forall A (l1 l2 : list A), 
    rev (l1 ++ l2) = rev l2 ++ rev l1.
induction l1.
intro; symmetry; apply app_nil_r.
intro; simpl; rewrite IHl1; rewrite app_ass.
reflexivity.
Defined.

Lemma rev_list_rect A  :
      forall P:list A-> Type,
	P [] ->
	(forall (a:A) (l:list A), P (rev l) -> P (rev (a :: l))) ->
	forall l:list A, P (rev l).
Proof.
  induction l; auto.
Defined.

Theorem rev_rect A :
  forall P:list A -> Type,
	P [] ->
	(forall (x:A) (l:list A), P l -> P (l ++ [x])) ->
  forall l:list A, P l.
Proof.
  intros.
  generalize (rev_involutive l).
  intros E; rewrite <- E.
  apply (rev_list_rect _ P).
  auto.

  simpl.
  intros.
  apply (X0 a (rev l0)).
  auto.
Defined.


Lemma bisim_list_dlist : Bisimilar ListQueue DListQueue.
apply (Queue_R_Build_Queue_R _ _ R).

* reflexivity.

* intros n1 n2 n_R.
pose (nat_R_equal _ _ n_R) as H.
destruct H. clear n_R.
intros l [back front].
unfold R.
simpl.
intro; subst.
simpl.
reflexivity.

* intros l  [back front].
generalize l. clear l.
unfold R; fold R.
pattern back.

apply rev_rect.

intros l H; subst.
rewrite rev_app.
simpl.
rewrite app_nil_r.
rewrite rev_involutive.

destruct front.
constructor.
repeat constructor.
apply equal_nat_R; reflexivity.

clear back; intros hd back IHR l H.
subst.
rewrite rev_app.
rewrite rev_involutive.
rewrite rev_app.
simpl.
destruct front.
simpl.
repeat constructor.
apply equal_nat_R; reflexivity.
simpl.
repeat constructor.
apply equal_nat_R; reflexivity.
unfold R.
rewrite rev_app.
simpl.
rewrite rev_involutive.
reflexivity.
Defined.

Print program.
Check program.
Parametricity Recursive program.
Check program_R.

Lemma program_independent : 
 forall n, 
  program ListQueue n = program DListQueue n.
intro n.
apply option_nat_R_equal.
apply program_R.
apply bisim_list_dlist.
apply equal_nat_R.
reflexivity.
Defined.
Print program.
Print program_R.