-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_HughesLists.v
442 lines (372 loc) · 8.75 KB
/
08_HughesLists.v
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
(*
HUGHES LISTS
Evgeny V Ivashkevich
E-mail: ivashkev@yandex.ru
March 2, 2019
Abstract: In this file, we formalize the short paper by R. John Muir Hughes,
"A novel repesentation of lists and its application to the function
reverse". Information Pocessing Letters 22 (1986) 141-144,
where fast algorithm for lists reverse function was proposed.
*)
Require Import FunctionalExtensionality.
Require Import List.
Import ListNotations.
Set Implicit Arguments.
Module ArrowType.
Section Hughes.
Variable T : Type.
Definition A := list T.
Definition R := A -> A.
Definition rep (x : A) : R := app x.
Check rep.
Definition abs (f : R) : A := f [].
Check abs.
Print abs.
Theorem abs_rep :
forall a : A,
abs (rep a) = a.
Proof.
unfold abs, rep.
intros a.
rewrite app_nil_r.
reflexivity.
Qed.
Parameter find_rep : forall f : R, { F : A | f = rep F }.
Theorem rep_abs :
forall f : R,
rep (abs f) = f.
Proof.
intros f.
destruct (find_rep f) as [ F Hf ].
rewrite Hf.
rewrite abs_rep.
reflexivity.
Qed.
Definition appendR (f g : R) : R := fun x => f (g x).
Check appendR.
Theorem appendR_rep :
forall (a b : A),
appendR (rep a) (rep b) = rep (a ++ b).
Proof.
intros.
apply functional_extensionality.
intros.
unfold appendR, rep; simpl.
apply app_assoc.
Qed.
Theorem abs_appendR :
forall (f g : R),
abs (appendR f g) = (abs f) ++ (abs g).
Proof.
intros.
destruct (find_rep f) as [ F Hf ].
destruct (find_rep g) as [ G Hg ].
rewrite Hf. rewrite Hg. rewrite appendR_rep.
unfold abs, rep; simpl.
repeat rewrite app_nil_r.
reflexivity.
Qed.
Fixpoint rev (x : A) : R :=
match x with
| nil => id
| a :: y => fun (t : A) => (rev y) (a :: t)
end.
Definition reverse (x : A) := (rev x) [].
Theorem rev_app :
forall (x y : A),
rev x y = rev x [] ++ y.
Proof.
intros.
induction y as [| h t ].
{ simpl. rewrite app_nil_r. reflexivity. }
{ assert (H1 : rev x [] = abs (rev x)). reflexivity.
assert (H2 : h :: t = abs (app (h :: t))).
{ unfold abs. rewrite app_nil_r. reflexivity. }
rewrite H1. rewrite H2 at 2.
rewrite <- abs_appendR.
unfold appendR, abs, rep; simpl.
rewrite app_nil_r. reflexivity.
}
Qed.
Theorem rev_app_distr:
forall (x y : A),
reverse (x ++ y) = reverse y ++ reverse x.
Proof.
intros.
unfold reverse.
induction x as [| h t ].
{ simpl. rewrite app_nil_r. reflexivity. }
{ simpl.
rewrite (rev_app t [h]).
rewrite rev_app.
rewrite IHt.
rewrite app_assoc.
reflexivity.
}
Qed.
Theorem app_length :
forall x y : A,
length (x ++ y) = length x + length y.
Proof.
intros x y.
induction x as [| h t ].
{ reflexivity. }
{ simpl. rewrite -> IHt. reflexivity. }
Qed.
Theorem reverse_length :
forall x : A,
length (reverse x) = length x.
Proof.
intros x.
unfold reverse.
induction x as [| h t ].
{ reflexivity. }
{ assert (H : rev (h :: t) [] = rev t [] ++ [h]).
{ simpl. rewrite rev_app. reflexivity. }
rewrite H.
rewrite app_length.
rewrite IHt.
rewrite PeanoNat.Nat.add_comm.
simpl. reflexivity.
}
Qed.
Theorem reverse_involutive :
forall x : A,
reverse (reverse x) = x.
Proof.
induction x.
{ reflexivity. }
{ simpl.
replace (a :: x) with ([a] ++ x).
{ repeat rewrite rev_app_distr.
rewrite IHx. reflexivity.
}
{ reflexivity. }
}
Qed.
Theorem rev_injective :
forall (x y : A),
reverse x = reverse y -> x = y.
Proof.
intros x y reveq.
rewrite <- reverse_involutive.
rewrite <- reveq.
rewrite -> reverse_involutive.
reflexivity.
Qed.
End Hughes.
Compute reverse [2;3;6;3;6;9].
Compute reverse [true;true;true;false;false;false].
End ArrowType.
(* In the next module we change representation type from
functional (R := A -> A) to inductive type with additional condition
that we consider only those functions on lists that can be generated
with the help of the function append.
*)
Module InductiveType.
Section Hughes.
Variable T : Type.
Definition A := list T.
Record R : Type
:= build { func : A -> A ; prop : exists x : A, func = app x }.
Definition rep (x : A) : R.
Proof.
apply build with (app x).
exists x; reflexivity.
Defined.
Check rep.
Print rep.
Definition abs (F : R) : A.
Proof.
destruct F as [ f _ ].
apply (f []).
Defined.
Check abs.
Print abs.
Theorem abs_rep :
forall a : A,
abs (rep a) = a.
Proof.
unfold abs, rep.
intros a.
rewrite app_nil_r.
reflexivity.
Qed.
Definition find_rep :
forall F : R,
{ x : A | F = rep x }.
Proof.
intros [ f Hf ].
exists (f []).
destruct Hf as [ x H ].
subst f. unfold rep.
rewrite app_nil_r.
reflexivity.
Defined.
Theorem rep_abs :
forall F : R,
rep (abs F) = F.
Proof.
intros F.
destruct (find_rep F) as [ x H ].
subst F. rewrite abs_rep.
reflexivity.
Qed.
Theorem func_unique :
forall F G : R,
func F = func G -> F = G.
Proof.
intros.
rewrite <- (rep_abs F).
rewrite <- (rep_abs G).
destruct F as [ f [ x Hf ]].
destruct G as [ g [ y Hg ]].
simpl in *. rewrite H.
reflexivity.
Qed.
Definition appendR (F G : R) : R.
Proof.
destruct F as [ f Hf ].
destruct G as [ g Hg ].
exists (fun y => f (g y)).
destruct Hf as [ F Hf ].
destruct Hg as [ G Hg ].
subst f. subst g.
exists (app F G).
apply functional_extensionality.
intros x.
rewrite app_assoc.
reflexivity.
Defined.
Check appendR.
Print appendR.
Theorem appendR_rep :
forall (a b : A),
appendR (rep a) (rep b) = rep (a ++ b).
Proof.
intros.
apply func_unique. simpl.
apply functional_extensionality.
intros t. rewrite app_assoc. reflexivity.
Qed.
Theorem abs_appendR :
forall (f g : R),
abs (appendR f g) = (abs f) ++ (abs g).
Proof.
intros.
destruct (find_rep f) as [ F Hf ].
destruct (find_rep g) as [ G Hg ].
rewrite Hf. rewrite Hg. rewrite appendR_rep.
unfold abs, rep; simpl.
repeat rewrite app_nil_r.
reflexivity.
Qed.
Definition rev (x : A) : R.
Proof.
set (f := (fix rev (x : A) : A -> A :=
match x with
| [] => id
| a :: y => fun (t : A) => rev y (a :: t)
end)).
exists (f x).
exists ((f x) []).
induction x.
{ simpl. reflexivity. }
{ simpl. rewrite IHx.
apply functional_extensionality.
intros t; simpl. rewrite <- app_assoc. reflexivity.
}
Defined.
Definition reverse (x : A) := func (rev x) [].
Theorem rev_app :
forall (x y : A),
func (rev x) y = func (rev x) [] ++ y.
Proof.
intros.
induction y as [| h t ].
{ rewrite app_nil_r. reflexivity. }
{ assert (H1 : func (rev x) [] = abs (rev x)). reflexivity.
assert (H2 : h :: t = abs (rep (h :: t))).
{ unfold abs, rep. rewrite app_nil_r. reflexivity. }
rewrite H1. rewrite H2 at 2.
rewrite <- abs_appendR.
unfold appendR, abs, rep; simpl.
rewrite app_nil_r. reflexivity.
}
Qed.
Theorem rev_app_distr:
forall (x y : A),
reverse (x ++ y) = reverse y ++ reverse x.
Proof.
intros.
unfold reverse.
induction x as [| h t ].
{ simpl. rewrite app_nil_r. reflexivity. }
{ replace (func (rev ((h :: t) ++ y)) []) with (func (rev (t ++ y)) [h]).
replace (func (rev (h :: t)) []) with (func (rev t) [h]).
rewrite (rev_app t [h]).
rewrite rev_app.
rewrite IHt.
rewrite app_assoc.
reflexivity.
reflexivity.
reflexivity.
}
Qed.
Theorem app_length :
forall x y : A,
length (x ++ y) = length x + length y.
Proof.
intros x y.
induction x as [| h t ].
{ reflexivity. }
{ simpl. rewrite -> IHt. reflexivity. }
Qed.
Theorem reverse_length :
forall x : A,
length (reverse x) = length x.
Proof.
intros x.
unfold reverse.
induction x as [| h t ].
{ reflexivity. }
{ assert (H : func (rev (h :: t)) [] = func (rev t) [] ++ [h]).
{ replace (func (rev (h :: t)) []) with (func (rev t) [h]).
rewrite rev_app. reflexivity.
reflexivity.
}
rewrite H.
rewrite app_length.
rewrite IHt.
rewrite PeanoNat.Nat.add_comm.
simpl. reflexivity.
}
Qed.
Theorem reverse_involutive :
forall x : A,
reverse (reverse x) = x.
Proof.
induction x.
{ reflexivity. }
{ simpl.
replace (a :: x) with ([a] ++ x).
{ repeat rewrite rev_app_distr.
rewrite IHx. reflexivity.
}
{ reflexivity. }
}
Qed.
Theorem rev_injective :
forall (x y : A),
reverse x = reverse y -> x = y.
Proof.
intros x y reveq.
rewrite <- reverse_involutive.
rewrite <- reveq.
rewrite -> reverse_involutive.
reflexivity.
Qed.
End Hughes.
Compute reverse [2;3;6;3;6;9].
Compute reverse [true;true;true;false;false;false].
End InductiveType.