-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascal.v
500 lines (432 loc) · 14 KB
/
pascal.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
Require Import HashTable HashTablePositive Int_utils.
Require Import ZArith Bool.
Require Import Coq.Numbers.Cyclic.Int63.Uint63.
Require Import Coq.FSets.FMapAVL.
Open Scope uint63_scope.
(* normal pascal function *)
Fixpoint pascal (n m: nat) : int :=
match n, m with
| 0, 0 => 1
| 0, _ => 0
| _, 0 => 1
| S n', S m' => pascal n' m' + pascal n' m
end.
Compute pascal 10 5.
Lemma pascal_zero :
forall n m, n < m -> pascal n m = 0.
Proof.
induction n; simpl.
+ destruct m; easy.
+ destruct m. easy. intros H.
apply Nat.succ_lt_mono in H.
rewrite 2!IHn. easy.
apply Nat.lt_lt_succ_r; easy. easy.
Qed.
Theorem pascal_one :
forall n, pascal n n = 1.
Proof.
induction n. reflexivity.
simpl. rewrite IHn.
rewrite pascal_zero. reflexivity.
apply Nat.lt_succ_diag_r.
Qed.
Inductive intint :=
| I : int -> int -> intint.
Definition eqb_ii (n m: intint) :=
let (n1, n2) := n in
let (m1, m2) := m in
(n1 =? m1) && (n2 =? m2).
Theorem eq_spec_ii:
forall n m : intint, reflect (n = m) (eqb_ii n m).
Proof.
intros [n1 n2] [m1 m2]. case (eqb_ii _ _) eqn:H.
+ apply ReflectT. unfold eqb_ii in H.
rewrite andb_true_iff in H. destruct H as [H1 H2].
apply eqb_spec in H1, H2. now rewrite H1, H2.
+ apply ReflectF. unfold eqb_ii in H.
rewrite andb_false_iff in H.
destruct H as [H | H];
intros H0; rewrite eqb_false_spec in H; apply H;
now inversion H0.
Qed.
(* free hash funtion *)
Module Eq.
Definition A := intint.
Definition eq n m := eqb_ii n m.
Definition eq_spec := eq_spec_ii.
End Eq.
Module INTINT <: HashI.
Include Eq.
Definition hash (i: A): int :=
let (n1, n2) := i in
n1 + n2 * 345.
End INTINT.
Module INTINTP <: HashP.
Include Eq.
Definition hash (i: A): positive :=
let hi := INTINT.hash i in
match to_Z hi with
| Z.pos p => p
| _ => xH
end.
End INTINTP.
(* nat couple *)
Inductive natnat :=
| N : nat -> nat -> natnat.
Fixpoint nat_to_int (n: nat) (acc: int) : int :=
match n with
| O => acc
| S n => nat_to_int n (acc + 1)
end.
Definition hash (n : nat) (h : int) : int :=
nat_to_int n 0 + h * 345.
Definition eqb_nn (n m: natnat) :=
let (n1, n2) := n in
let (m1, m2) := m in
(n1 =? m1)%nat && (n2 =? m2)%nat.
Theorem eq_spec:
forall n m : natnat, reflect (n = m) (eqb_nn n m).
Proof.
intros [n1 n2] [m1 m2]. case (eqb_nn _ _) eqn:H.
+ apply ReflectT. unfold eqb_nn in H.
rewrite andb_true_iff in H. destruct H as [H1 H2].
apply Nat.eqb_eq in H1, H2. now rewrite H1, H2.
+ apply ReflectF. unfold eqb_nn in H.
rewrite andb_false_iff in H. destruct H as [H | H];
intros H0; rewrite Nat.eqb_neq in H; apply H;
now inversion H0.
Qed.
(* expensive hash funtion *)
Module Eqnn.
Definition A := natnat.
Definition eq n m := eqb_nn n m.
Definition eq_spec := eq_spec.
End Eqnn.
Module NATNAT <: HashI.
Include Eqnn.
Definition hash (i: A): int :=
let (n1, n2) := i in
hash n1 (hash n2 0).
End NATNAT.
Module NATNATP <: HashP.
Include Eqnn.
Definition hash (i: A): positive :=
let (n1, n2) := i in
let hi := hash n1 (hash n2 0) in
match to_Z hi with
| Z.pos p => p
| _ => xH
end.
End NATNATP.
(* Pascal memo with tuple of int *)
Module HashtableITest.
Module H := HashTable INTINT.
Fixpoint pascal_memo' (n m: nat) (ni mi : int) (h: H.t int) : (int * H.t int) :=
match H.find h (I ni mi) with
| Some v => (v, h)
| None =>
match n, m with
| 0, 0 => (1, h)
| 0, _ => (0, h)
| _, 0 => (1, h)
| S n', S m' =>
let (v1, h1) := pascal_memo' n' m' (ni-1) (mi -1) h in
let (v2, h2) := pascal_memo' n' m (ni-1) mi h1 in
let r := v1 + v2 in
(r, H.add h2 (I ni mi) r)
end
end.
Definition pascal_memo n m :=
fst (pascal_memo' (Z.to_nat n) (Z.to_nat m) (of_Z n) (of_Z m) (H.create int 16)).
End HashtableITest.
Module HashTablePositiveITest.
Module H := HashTablePositive INTINTP.
Fixpoint pascal_memo' (n m: nat) (ni mi : int) (h: H.t int) : (int * H.t int) :=
match H.find h (I ni mi) with
| Some v => (v, h)
| None =>
match n, m with
| 0, 0 => (1, h)
| 0, _ => (0, h)
| _, 0 => (1, h)
| S n', S m' =>
let (v1, h1) := pascal_memo' n' m' (ni-1) (mi -1) h in
let (v2, h2) := pascal_memo' n' m (ni-1) mi h1 in
let r := v1 + v2 in
(r, H.add h2 (I ni mi) r)
end
end.
Definition pascal_memo n m :=
fst (pascal_memo' (Z.to_nat n) (Z.to_nat m) (of_Z n) (of_Z m) (H.create int)).
End HashTablePositiveITest.
(* Pascal memo with tuple of nat *)
Module HashTableNTest.
Module H := HashTable NATNAT.
Fixpoint pascal_memo' (n m: nat) (h: H.t int) : (int * H.t int) :=
match H.find h (N n m) with
| Some v => (v, h)
| None =>
match n, m with
| 0, 0 => (1, h)
| 0, _ => (0, h)
| _, 0 => (1, h)
| S n', S m' =>
let (v1, h1) := pascal_memo' n' m' h in
let (v2, h2) := pascal_memo' n' m h1 in
let r := v1 + v2 in
(r, H.add h2 (N n m) r)
end
end.
Definition pascal_memo n m :=
fst (pascal_memo' (Z.to_nat n) (Z.to_nat m) (H.create int 16)).
Theorem pascal_memo_correct:
forall n m,
pascal_memo (Z.of_nat n) (Z.of_nat m) = pascal n m.
Proof.
intros n m. unfold pascal_memo.
rewrite 2!Nat2Z.id.
set (ok ht := forall n' m' i, H.find ht (N n' m') = Some i -> i = pascal n' m').
cut (forall ht, ok ht -> ok (snd (pascal_memo' n m ht))
/\ fst (pascal_memo' n m ht) = pascal n m). intros H.
apply H. unfold ok. intros n' m' i.
rewrite H.find_empty. easy.
revert m. induction n.
+ intros [| m] ht Hht; simpl.
- case H.find eqn:Hf. simpl. split. easy.
unfold ok in Hht. rewrite (Hht 0%nat 0%nat) at 1. reflexivity.
easy. simpl. easy.
- case H.find eqn:Hf. simpl. split. easy.
unfold ok in Hht. rewrite (Hht 0%nat (S m)) at 1. reflexivity.
easy. simpl. easy.
+ intros [| m] ht Oht.
- simpl. case H.find eqn:Heq.
simpl.
split. easy. unfold ok in Oht. apply (Oht (S n) 0%nat). easy.
easy.
- simpl. case H.find eqn:Heq. simpl. split. easy.
unfold ok in Oht. apply (Oht (S n) (S m)). easy.
generalize (IHn m ht). destruct (pascal_memo' n m ht).
generalize (IHn (S m) t). destruct (pascal_memo' n (S m) t).
simpl. intros Hm Hsm.
specialize (Hsm Oht) as [Ot ->]. specialize (Hm Ot) as [Ot0 ->].
split. 2: reflexivity.
intros n' m' i'.
case (NATNAT.eq (N n' m') (N (S n) (S m))) eqn:Hnn; rewrite H.find_spec.
* unfold NATNAT.eq in Hnn.
case (NATNAT.eq_spec (N n' m') (N (S n) (S m))) as [Hn|] in Hnn.
2:discriminate. rewrite Hn, H.add_same.
simpl. injection Hn. intros -> ->. simpl. intros H. injection H.
intros <-. reflexivity.
* rewrite H.add_other. intros Hf. rewrite (Ot0 n' m') at 1.
reflexivity. rewrite H.find_spec. easy.
unfold NATNAT.eq in Hnn.
case (NATNAT.eq_spec (N n' m') (N (S n) (S m))) as [|Hn] in Hnn.
discriminate. easy.
Qed.
End HashTableNTest.
Module HashTablePositiveNTest.
Module H := HashTablePositive NATNATP.
Fixpoint pascal_memo' (n m: nat) (h: H.t int) : (int * H.t int) :=
match H.find h (N n m) with
| Some v => (v, h)
| None =>
match n, m with
| 0, 0 => (1, h)
| 0, _ => (0, h)
| _, 0 => (1, h)
| S n', S m' =>
let (v1, h1) := pascal_memo' n' m' h in
let (v2, h2) := pascal_memo' n' m h1 in
let r := v1 + v2 in
(r, H.add h2 (N n m) r)
end
end.
Definition pascal_memo n m :=
fst (pascal_memo' (Z.to_nat n) (Z.to_nat m) (H.create int)).
End HashTablePositiveNTest.
(* FINTINT Key type for FMap AVL in stdlib *)
Module FINTINT.
Definition t := intint.
Definition eq (n m: t) := eqb_ii n m = true.
Lemma eq_refl:
forall i, eq i i.
Proof.
unfold eq, eqb_ii. intros [n1 n2].
rewrite 2!eqb_refl. reflexivity.
Qed.
Lemma eq_sym:
forall n m : t, eq n m -> eq m n.
Proof.
intros [n1 n2] [m1 m2].
unfold eq, eqb_ii. intros H.
apply andb_true_intro. rewrite andb_true_iff in H.
rewrite 2!eqb_spec in H. destruct H as [<- <-].
rewrite 2!eqb_refl. easy.
Qed.
Lemma eq_trans:
forall x y z : t, eq x y -> eq y z -> eq x z.
Proof.
intros [x1 x2] [y1 y2] [z1 z2] Hxy Hyz.
unfold eq, eqb_ii in *.
rewrite andb_true_iff, 2!eqb_spec in *.
now destruct Hxy as [<- <-].
Qed.
Definition eq_spec := eq_spec_ii.
(* Lexicographic order *)
Definition lt (n m: t) :=
let (n1, n2) := n in
let (m1, m2) := m in
(n1 <? m1) || ((n1 =? m1) && (n2 <? m2)) = true.
Lemma lt_trans:
forall x y z : t, lt x y -> lt y z -> lt x z.
Proof.
intros [x1 x2] [y1 y2] [z1 z2] Hxy Hyz.
unfold lt in *.
rewrite orb_true_iff, andb_true_iff,
2!ltb_spec, eqb_spec in *.
destruct Hxy as [Hxy1 | [Heq1 Hxy2]].
+ destruct Hyz as [Hyz1 | [Heq2 Hyz2]].
- left. now apply Z.lt_trans with (m:=to_Z y1).
- rewrite <- Heq2. now left.
+ destruct Hyz as [Hyz1 | [Heq2 Hyz2]].
- rewrite Heq1. now left.
- rewrite Heq1, Heq2. right. split. reflexivity.
now apply Z.lt_trans with (m:=to_Z y2).
Qed.
Lemma lt_not_eq:
forall x y : t, lt x y -> ~ eq x y.
Proof.
intros [x1 x2] [y1 y2] Hlt Heq.
unfold eq, eqb_ii in Heq. rewrite andb_true_iff, 2!eqb_spec in Heq.
destruct Heq as [<- <-].
unfold lt in Hlt. rewrite orb_true_iff, andb_true_iff in Hlt.
destruct Hlt as [Hlt | [_ Hlt]];
rewrite ltb_spec in Hlt.
+ contradiction (Z.lt_irrefl (to_Z x1)).
+ contradiction (Z.lt_irrefl (to_Z x2)).
Qed.
Definition compare_def (x y: t) :=
let (x1, x2) := x in
let (y1, y2) := y in
let cmp := compare x1 y1 in
match cmp with
| Eq => compare x2 y2
| _ => cmp
end.
Lemma compare_eq:
forall x y, compare_def x y = Eq -> eq x y.
Proof.
intros [x1 x2] [y1 y2] H.
unfold compare_def in H.
unfold eq, eqb_ii.
apply andb_true_iff. rewrite 2!eqbPT_to_Z.
case (x1 ?= y1) eqn:H1; try discriminate.
rewrite compare_spec in *.
split; now apply Z.compare_eq.
Qed.
Lemma compare_lt:
forall x y, compare_def x y = Lt -> lt x y.
Proof.
intros [x1 x2] [y1 y2] H.
unfold compare_def in H. unfold lt.
apply orb_true_iff. rewrite andb_true_iff, eqbPT_to_Z.
case (x1 ?= y1) eqn:H1; try discriminate.
+ rewrite compare_spec in *. right. split.
now apply Z.compare_eq in H1.
apply ltb_spec. now rewrite <- Z.compare_lt_iff.
+ left. rewrite compare_spec in H1. rewrite ltb_spec.
now rewrite <- Z.compare_lt_iff.
Qed.
Lemma compare_gt:
forall x y, compare_def x y = Gt -> lt y x.
Proof.
intros [x1 x2] [y1 y2] H.
unfold compare_def in H. unfold lt.
apply orb_true_iff. rewrite andb_true_iff, eqbPT_to_Z.
case (x1 ?= y1) eqn:H1; try discriminate.
+ right. rewrite compare_spec in *. split.
now apply Z.compare_eq in H1.
rewrite Zcompare_Gt_Lt_antisym in H.
apply ltb_spec. now rewrite <- Z.compare_lt_iff.
+ left. rewrite compare_spec in *.
rewrite Zcompare_Gt_Lt_antisym in H1.
apply ltb_spec. now rewrite <- Z.compare_lt_iff.
Qed.
Definition compare (x y: t):
OrderedType.Compare lt eq x y.
Proof.
case (compare_def x y) eqn:Hc.
+ apply OrderedType.EQ. now apply compare_eq.
+ apply OrderedType.LT. now apply compare_lt.
+ apply OrderedType.GT. now apply compare_gt.
Defined.
Definition eq_dec:
forall x y : t, {eq x y} + {~ eq x y}.
Proof.
intros [x1 x2] [y1 y2]. unfold eq, eqb_ii.
case (x1 =? y1) eqn:H1. case (x2 =? y2) eqn:H2.
1: now left.
all: now right.
Qed.
End FINTINT.
(* Pascal memo with tuple of int and Fmap *)
Module FTest.
Module Import M := FMapAVL.Make(FINTINT).
Fixpoint pascal_memo' (n m: nat) (ni mi : int) (h: M.t int) : (int * M.t int) :=
match M.find (I ni mi) h with
| Some v => (v, h)
| None =>
match n, m with
| 0, 0 => (1, h)
| 0, _ => (0, h)
| _, 0 => (1, h)
| S n', S m' =>
let (v1, h1) := pascal_memo' n' m' (ni-1) (mi -1) h in
let (v2, h2) := pascal_memo' n' m (ni-1) mi h1 in
let r := v1 + v2 in
(r, M.add (I ni mi) r h2)
end
end.
Definition pascal_memo n m :=
fst (pascal_memo' (Z.to_nat n) (Z.to_nat m) (of_Z n) (of_Z m) (M.empty int)).
End FTest.
(* tests *)
Time Compute HashtableITest.pascal_memo 500 250.
Time Compute HashTablePositiveITest.pascal_memo 500 250.
Time Compute HashTableNTest.pascal_memo 500 250.
Time Compute HashTablePositiveNTest.pascal_memo 500 250.
Time Compute FTest.pascal_memo 500 250.
(* results:
N{Radix Tree, Bucket} = pascal memo with natnat (expensive hash)
I{Radix Tree, Bucket} = pascal memo with intint (free hash)
+-------------+--------+--------+--------+--------+
| pascal 2n n | n=50 | n=100 | n=150 | n=200 |
+-------------+--------+--------+--------+--------+
| NRadix Tree | 0.093s | 0.331s | 0.919s | 1.917s |
+-------------+--------+--------+--------+--------+
| NBucket | 0.032s | 0.167s | 0.462s | 1.003s | ~ / 2
+-------------+--------+--------+--------+--------+
| FMap Avl | 0.084s | 0.35s | 0.723s | 1.413 |
+-------------+--------+--------+--------+--------+
| IRadix Tree | 0.068s | 0.206s | 0.423s | 0.788s |
+-------------+--------+--------+--------+--------+
| IBucket | 0.009s | 0.026s | 0.069s | 0.091s | ~ / 10
+-------------+--------+--------+--------+--------+
*)
(*@ test_pascal
for n : 20 -> 300
{s
Require Import pascal.
Definition ii := (2 * n)%Z.
s}
(* FMap *)
{ Time Compute FTest.pascal_memo ii n. }
(* NRadix *)
{ Time Compute HashTablePositiveNTest.pascal_memo ii n. }
(* NBucket *)
{ Time Compute HashtableNTest.pascal_memo ii n. }
(* IRadix *)
{ Time Compute HashTablePositiveITest.pascal_memo ii n. }
(* IBucket *)
{ Time Compute HashtableITest.pascal_memo ii n. }
@*)