forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackSetScript.sml
More file actions
405 lines (351 loc) · 11.5 KB
/
RedBlackSetScript.sml
File metadata and controls
405 lines (351 loc) · 11.5 KB
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
(*
This is an example of applying the translator to the Red-Black
Set algorithm from Chris Okasaki's book.
*)
open preamble;
open okasaki_miscTheory pred_setTheory pred_setSimps;
open ml_translatorLib ListProgTheory
val _ = new_theory "RedBlackSet"
val _ = translation_extends "ListProg";
(* Okasaki page 28 *)
Datatype:
color = Red | Black
End
Datatype:
tree = Empty | Tree color tree 'a tree
End
val tree_distinct = fetch "-" "tree_distinct"
val tree_nchotomy = fetch "-" "tree_nchotomy"
val tree_to_set_def = Define `
(tree_to_set Empty = {}) ∧
(tree_to_set (Tree _ t1 x t2) = {x} ∪ tree_to_set t1 ∪ tree_to_set t2)`;
(* The tree is a binary search tree *)
val is_bst_def = Define `
(is_bst lt Empty <=> T) ∧
(is_bst lt (Tree _ t1 x t2) <=>
is_bst lt t1 ∧
is_bst lt t2 ∧
(!y. y ∈ tree_to_set t1 ⇒ lt y x) ∧
(!y. y ∈ tree_to_set t2 ⇒ lt x y))`;
val not_red_def = Define `
(not_red (Tree Red t1 x t2) = F) ∧
(not_red _ = T)`;
val red_black_invariant1_def = Define `
(red_black_invariant1 Empty <=> T) ∧
(red_black_invariant1 (Tree Black t1 x t2) <=>
red_black_invariant1 t1 ∧ red_black_invariant1 t2) ∧
(red_black_invariant1 (Tree Red t1 x t2) <=>
red_black_invariant1 t1 ∧ red_black_invariant1 t2 ∧
not_red t1 ∧ not_red t2)`;
(* Count the number of black nodes along every path to the root.
* Return NONE, if this number isn't the same along every path. *)
val red_black_invariant2_def = Define `
(red_black_invariant2 Empty = SOME 0) ∧
(red_black_invariant2 (Tree c t1 x t2) =
case red_black_invariant2 t1 of
| NONE => NONE
| SOME n =>
case red_black_invariant2 t2 of
| NONE => NONE
| SOME n' =>
if n = n' then
if c = Black then
SOME (n + 1)
else
SOME n
else
NONE)`;
val empty_def = mlDefine `
empty = Empty`;
val member_def = mlDefine `
(member lt x Empty = F) ∧
(member lt x (Tree _ a y b) =
if lt x y then
member lt x a
else if lt y x then
member lt x b
else
T)`;
val balance_def = Define `
(balance Black (Tree Red (Tree Red a x b) y c) z d =
Tree Red (Tree Black a x b) y (Tree Black c z d)) ∧
(balance Black (Tree Red a x (Tree Red b y c)) z d =
Tree Red (Tree Black a x b) y (Tree Black c z d)) ∧
(balance Black a x (Tree Red (Tree Red b y c) z d) =
Tree Red (Tree Black a x b) y (Tree Black c z d)) ∧
(balance Black a x (Tree Red b y (Tree Red c z d)) =
Tree Red (Tree Black a x b) y (Tree Black c z d)) ∧
(balance col a x b = Tree col a x b)`;
val balance_ind = fetch "-" "balance_ind"
(* HOL expands the above balance into over 50 cases, so this alternate
* definition works better for the current translator. *)
val balance_left_left_def = mlDefine `
balance_left_left a z d =
case a of
| (Tree Red (Tree Red a x b) y c) =>
SOME (Tree Red (Tree Black a x b) y (Tree Black c z d))
| _ => NONE`
val balance_left_right_def = mlDefine `
balance_left_right a z d =
case a of
| (Tree Red a x (Tree Red b y c)) =>
SOME (Tree Red (Tree Black a x b) y (Tree Black c z d))
| _ => NONE`;
val balance_right_left_def = mlDefine `
balance_right_left a x b =
case b of
| (Tree Red (Tree Red b y c) z d) =>
SOME (Tree Red (Tree Black a x b) y (Tree Black c z d))
| _ => NONE`;
val balance_right_right_def = mlDefine `
balance_right_right a x b =
case b of
| (Tree Red b y (Tree Red c z d)) =>
SOME (Tree Red (Tree Black a x b) y (Tree Black c z d))
| _ => NONE`
val balance'_def = mlDefine `
balance' c a x b =
if c = Black then
case balance_left_left a x b of
| SOME t => t
| NONE =>
case balance_left_right a x b of
| SOME t => t
| NONE =>
case balance_right_left a x b of
| SOME t => t
| NONE =>
case balance_right_right a x b of
| SOME t => t
| NONE => Tree c a x b
else
Tree c a x b`;
val ins_def = mlDefine `
(ins lt x Empty = Tree Red Empty x Empty) ∧
(ins lt x (Tree col a y b) =
if lt x y then
balance' col (ins lt x a) y b
else if lt y x then
balance' col a y (ins lt x b)
else
Tree col a y b)`;
val insert_def = mlDefine `
insert lt x s =
case ins lt x s of
| Tree _ a y b => Tree Black a y b`;
(* Proof of functional correctness *)
val balance'_correct = Q.prove (
`!c a x b. balance' c a x b = balance c a x b`,
recInduct balance_ind >>
rw [balance'_def, balance_def, balance_left_left_def, balance_left_right_def,
balance_right_left_def, balance_right_right_def] >>
REPEAT (BasicProvers.FULL_CASE_TAC));
val balance'_tree = Q.prove (
`!c t1 x t2. ∃c' t1' x' t2'. (balance' c t1 x t2 = Tree c' t1' x' t2')`,
recInduct balance_ind >>
rw [balance'_def, balance_left_left_def, balance_left_right_def,
balance_right_left_def, balance_right_right_def] >>
REPEAT BasicProvers.FULL_CASE_TAC);
val balance'_set = Q.prove (
`!c t1 x t2. tree_to_set (balance' c t1 x t2) = tree_to_set (Tree c t1 x t2)`,
recInduct balance_ind >>
srw_tac [PRED_SET_AC_ss]
[balance'_def, balance_left_left_def, balance_left_right_def,
balance_right_left_def, balance_right_right_def,
tree_to_set_def] >>
REPEAT BasicProvers.FULL_CASE_TAC >>
srw_tac [PRED_SET_AC_ss] [tree_to_set_def]);
val balance'_bst = Q.prove (
`!c t1 x t2.
transitive lt ∧ is_bst lt (Tree c t1 x t2)
⇒
is_bst lt (balance' c t1 x t2)`,
recInduct balance_ind >>
rw [transitive_def, balance'_def, balance_left_left_def,
balance_left_right_def, balance_right_left_def,
balance_right_right_def, is_bst_def, tree_to_set_def] >>
fs [transitive_def, balance'_def, balance_left_left_def,
balance_left_right_def, balance_right_left_def,
balance_right_right_def, is_bst_def, tree_to_set_def] >>
REPEAT BasicProvers.FULL_CASE_TAC >>
fs [transitive_def, balance'_def, balance_left_left_def,
balance_left_right_def, balance_right_left_def,
balance_right_right_def, is_bst_def, tree_to_set_def] >>
metis_tac []);
val ins_tree = Q.prove (
`!lt x t. ?c t1 y t2. (ins lt x t = Tree c t1 y t2)`,
cases_on `t` >>
rw [ins_def] >>
metis_tac [balance'_tree]);
val ins_set = Q.prove (
`∀lt x t.
StrongLinearOrder lt
⇒
(tree_to_set (ins lt x t) = {x} ∪ tree_to_set t)`,
induct_on `t` >>
rw [tree_to_set_def, ins_def, balance'_set] >>
fs [] >>
srw_tac [PRED_SET_AC_ss] [] >>
`x = a` by (fs [StrongLinearOrder, StrongOrder, irreflexive_def,
transitive_def, trichotomous] >>
metis_tac []) >>
rw []);
val ins_bst = Q.prove (
`!lt x t. StrongLinearOrder lt ∧ is_bst lt t ⇒ is_bst lt (ins lt x t)`,
induct_on `t` >>
rw [is_bst_def, ins_def, tree_to_set_def] >>
match_mp_tac balance'_bst >>
rw [is_bst_def] >>
imp_res_tac ins_set >>
fs [StrongLinearOrder, StrongOrder]);
Theorem insert_set:
∀lt x t.
StrongLinearOrder lt
⇒
(tree_to_set (insert lt x t) = {x} ∪ tree_to_set t)
Proof
rw [insert_def] >>
`?c t1 y t2. ins lt x t = Tree c t1 y t2` by metis_tac [ins_tree] >>
rw [tree_to_set_def] >>
`tree_to_set (ins lt x t) = tree_to_set (Tree c t1 y t2)`
by metis_tac [] >>
fs [] >>
imp_res_tac ins_set >>
fs [tree_to_set_def]
QED
Theorem insert_bst:
!lt x t.
StrongLinearOrder lt ∧ is_bst lt t
⇒
is_bst lt (insert lt x t)
Proof
rw [insert_def] >>
`?c t1 y t2. ins lt x t = Tree c t1 y t2` by metis_tac [ins_tree] >>
rw [] >>
`is_bst lt (Tree c t1 y t2)` by metis_tac [ins_bst] >>
fs [is_bst_def]
QED
Theorem member_correct:
!lt t x.
StrongLinearOrder lt ∧
is_bst lt t
⇒
(member lt x t <=> x ∈ tree_to_set t)
Proof
strip_tac >> induct_on `t` >>
rw [member_def, is_bst_def, tree_to_set_def] >>
fs [StrongLinearOrder, StrongOrder, irreflexive_def, transitive_def,
trichotomous] >>
metis_tac []
QED
(* Prove the two red-black invariants that no red node has a red child,
* and that the number of black nodes is the same on each path from
* the root to the leaves. *)
val case_opt_lem = Q.prove (
`!x f z.
((case x of NONE => NONE | SOME y => f y) = SOME z) =
(?y. (x = SOME y) ∧ (f y = SOME z))`,
cases_on `x` >>
rw []);
val balance_inv2_black = Q.prove (
`!c t1 a t2 n.
(red_black_invariant2 t1 = SOME n) ∧
(red_black_invariant2 t2 = SOME n) ∧
(c = Black)
⇒
(red_black_invariant2 (balance c t1 a t2) = SOME (n+1))`,
recInduct balance_ind >>
rw [balance_def, red_black_invariant2_def, case_opt_lem] >>
metis_tac []);
val ins_inv2 = Q.prove (
`!leq x t n.
(red_black_invariant2 t = SOME n)
⇒
(red_black_invariant2 (ins leq x t) = SOME n)`,
induct_on `t` >>
rw [red_black_invariant2_def, ins_def, case_opt_lem] >>
every_case_tac >>
cases_on `c` >>
fs [] >>
rw [] >|
[metis_tac [balance_inv2_black, balance'_correct],
rw [balance'_def, red_black_invariant2_def, case_opt_lem],
metis_tac [balance_inv2_black, balance'_correct],
rw [balance'_def, red_black_invariant2_def, case_opt_lem]]);
Theorem insert_invariant2:
!leq x t n.
(red_black_invariant2 t = SOME n)
⇒
(red_black_invariant2 (insert leq x t) = SOME n) ∨
(red_black_invariant2 (insert leq x t) = SOME (n + 1))
Proof
rw [insert_def] >>
cases_on `ins leq x t` >>
rw [] >-
metis_tac [ins_tree, tree_distinct] >>
`red_black_invariant2 (ins leq x t) = SOME n` by metis_tac [ins_inv2] >>
POP_ASSUM MP_TAC >>
rw [red_black_invariant2_def, case_opt_lem] >>
cases_on `n = n''` >>
cases_on `c` >>
fs []
QED
(* Invariant one hold everywhere except for the root node,
* where it may or may not. *)
val rbinv1_root_def = Define `
(rbinv1_root Empty <=> T) ∧
(rbinv1_root (Tree c t1 x t2) <=>
red_black_invariant1 t1 ∧ red_black_invariant1 t2)`;
val balance_inv1_black = Q.prove (
`!c t1 a t2 n.
red_black_invariant1 t1 ∧ rbinv1_root t2 ∧ (c = Black)
⇒
red_black_invariant1 (balance c t1 a t2) ∧
red_black_invariant1 (balance c t2 a t1)`,
recInduct balance_ind >>
rw [balance_def, red_black_invariant1_def, rbinv1_root_def, not_red_def]);
val inv1_lemma = Q.prove (
`!t. red_black_invariant1 t ⇒ rbinv1_root t`,
cases_on `t` >>
rw [red_black_invariant1_def, rbinv1_root_def] >>
cases_on `c` >>
fs [red_black_invariant1_def]);
val ins_inv1 = Q.prove (
`!leq x t.
red_black_invariant1 t
⇒
(not_red t ⇒ red_black_invariant1 (ins leq x t)) ∧
(¬not_red t ⇒ rbinv1_root (ins leq x t))`,
induct_on `t` >>
rw [red_black_invariant1_def, rbinv1_root_def, ins_def, not_red_def] >>
cases_on `c` >>
fs [red_black_invariant1_def, not_red_def] >|
[metis_tac [balance_inv1_black, balance'_correct, inv1_lemma],
rw [balance'_def, rbinv1_root_def],
metis_tac [balance_inv1_black, balance'_correct, inv1_lemma],
rw [balance'_def, rbinv1_root_def]]);
Theorem insert_invariant1:
!leq x t. red_black_invariant1 t ⇒ red_black_invariant1 (insert leq x t)
Proof
rw [insert_def] >>
cases_on `ins leq x t` >>
rw [] >-
metis_tac [ins_tree, tree_distinct] >>
`not_red t ⇒ red_black_invariant1 (ins leq x t)` by metis_tac [ins_inv1] >>
`¬not_red t ⇒ rbinv1_root (ins leq x t)` by metis_tac [ins_inv1] >>
POP_ASSUM MP_TAC >>
POP_ASSUM MP_TAC >>
cases_on `not_red t` >>
rw [] >>
cases_on `c` >>
fs [red_black_invariant1_def, rbinv1_root_def]
QED
(* Simplify the side conditions on the generated certificate theorems,
* based on the verification. *)
val insert_side_def = fetch "-" "insert_side_def"
val insert_side = Q.prove (
`∀leq x t. insert_side leq x t`,
rw [insert_side_def] >>
`?c t1 y t2. ins leq x t = Tree c t1 y t2` by metis_tac [ins_tree] >>
rw []);
val _ = export_theory ();