-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmllistScript.sml
347 lines (291 loc) · 7.99 KB
/
mllistScript.sml
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
(*
Pure functions for the List module.
*)
open preamble
val _ = new_theory"mllist"
val _ = set_grammar_ancestry ["indexedLists", "toto"]
Definition getItem_def:
(getItem [] = NONE) /\
(getItem (h::t) = SOME(h, t))
End
Definition nth_def:
nth l i = EL i l
End
Definition take_def:
take l i = TAKE i l
End
Definition drop_def:
drop l i = DROP i l
End
Definition takeUntil_def:
(takeUntil p [] = []) /\
(takeUntil p (x::xs) = if p x then [] else x :: takeUntil p xs)
End
Definition dropUntil_def:
(dropUntil p [] = []) /\
(dropUntil p (x::xs) = if p x then x::xs else dropUntil p xs)
End
Definition mapi_def:
(mapi f (n: num) [] = []) /\
(mapi f n (h::t) = (let y = f n h in (y::(mapi f (n + 1) t))))
End
Theorem MAPI_thm_gen[local]:
∀f l x. MAPi (\a. f (a + x)) l = mapi f x l
Proof
Induct_on `l` \\ rw [MAPi_def, mapi_def] \\
rw [o_DEF, ADD1] \\
fs [] \\
pop_assum (fn th => rw[GSYM th] ) \\
rw[AC ADD_ASSOC ADD_COMM] \\
match_mp_tac MAPi_CONG \\ rw[]
QED
Theorem MAPI_thm:
!f l. MAPi f l = mapi f 0 l
Proof
rw [(MAPI_thm_gen |> Q.SPECL[`f`,`l`,`0`]
|> SIMP_RULE (srw_ss()++ETA_ss) [])]
QED
Definition mapPartial_def:
(mapPartial f [] = []) /\
(mapPartial f (h::t) = case (f h) of
NONE => mapPartial f t
|(SOME x) => x::mapPartial f t)
End
Theorem mapPartial_thm:
!f l. mapPartial f l = MAP THE (FILTER IS_SOME (MAP f l))
Proof
Induct_on `l` \\ rw [mapPartial_def] \\ Cases_on `f h` \\ rw [THE_DEF] \\ fs [IS_SOME_DEF]
QED
Theorem index_find_thm:
!x y. OPTION_MAP SND (INDEX_FIND x f l) = OPTION_MAP SND (INDEX_FIND y f l)
Proof
Induct_on`l` \\ rw[INDEX_FIND_def]
QED
Theorem FIND_thm:
(FIND f [] = NONE) ∧
(∀h t. FIND f (h::t) = if f h then SOME h else FIND f t)
Proof
rw[FIND_def,INDEX_FIND_def,index_find_thm]
QED
Definition partition_aux_def:
(partition_aux f [] pos neg =
(REVERSE pos, REVERSE neg)) /\
(partition_aux f (h::t) pos neg = if f h then partition_aux f t (h::pos) neg
else partition_aux f t pos (h::neg))
End
Definition partition_def:
partition (f : 'a -> bool) l = partition_aux f l [] []
End
Theorem partition_aux_thm[local]:
∀f l l1 l2.
partition_aux f l l1 l2 =
(REVERSE l1++(FILTER f l), REVERSE l2++(FILTER ($~ o f) l))
Proof
Induct_on `l` \\
rw [partition_aux_def] \\
rw [partition_aux_def]
QED
Theorem partition_pos_thm:
!f l. FST (partition f l) = FILTER f l
Proof
rw [partition_def, FILTER, partition_aux_thm]
QED
Theorem partition_neg_thm:
!f l. SND (partition f l) = FILTER ($~ o f) l
Proof
rw [partition_def, FILTER, partition_aux_thm]
QED
Definition foldl_def:
(foldl f e [] = e) /\
(foldl f e (h::t) = foldl f (f h e) t)
End
Definition foldli_aux_def:
(foldli_aux f e n [] = e) /\
(foldli_aux f e n (h::t) = foldli_aux f (f n h e) (SUC n) t)
End
Definition foldli_def:
foldli f e l = foldli_aux f e 0 l
End
Theorem foldli_aux_thm[local]:
∀l f e n. foldli_aux f e n l = FOLDRi (\n'. f (LENGTH l + n - (SUC n'))) e (REVERSE l)
Proof
Induct_on `l` \\
rw [foldli_aux_def] \\
rw [FOLDRi_APPEND] \\
rw [ADD1]
QED
Theorem foldl_intro:
∀xs x f. FOLDL f x xs = foldl (λx acc. f acc x) x xs
Proof
Induct \\ fs [foldl_def]
QED
Theorem foldli_thm:
!f e l. foldli f e l = FOLDRi (\n. f (LENGTH l - (SUC n))) e (REVERSE l)
Proof
rw [foldli_def, foldli_aux_thm]
QED
(* these definitions are in A normal form in
order to be able to prove CF specs about them
(in addition to the translator-generated ones)
(the CF specs allow more general arguments f) *)
Definition tabulate_aux_def:
tabulate_aux n m f acc =
let b = (n >= m) in
if b then REVERSE acc
else
let v = f n in
let n = n+1n in
let acc = v::acc in
tabulate_aux n m f acc
Termination
wf_rel_tac`measure (λ(n,m,_,_). m-n)`
End
Definition tabulate_def:
tabulate n f = let l = [] in tabulate_aux 0 n f l
End
Theorem tabulate_aux_GENLIST:
∀n m f acc. tabulate_aux n m f acc = REVERSE acc ++ GENLIST (f o FUNPOW SUC n) (m-n)
Proof
recInduct(theorem"tabulate_aux_ind") \\ rw[]
\\ rw[Once tabulate_aux_def]
>- ( `m - n = 0` by simp[] \\ rw[] )
\\ Cases_on`m` \\ fs[]
\\ rename1`SUC m - n` \\ `SUC m - n = SUC (m - n)` by simp[]
\\ simp[GENLIST_CONS,FUNPOW,o_DEF,FUNPOW_SUC_PLUS]
\\ simp[ADD1]
QED
Theorem tabulate_GENLIST:
!n. tabulate n f = GENLIST f n
Proof
rw[tabulate_def,tabulate_aux_GENLIST,FUNPOW_SUC_PLUS,o_DEF,ETA_AX]
QED
Definition collate_def:
(collate f [] [] = EQUAL) /\
(collate f [] (h::t) = LESS) /\
(collate f (h::t) [] = GREATER) /\
(collate f (h1::t1) (h2::t2) =
if f h1 h2 = EQUAL
then collate f t1 t2
else f h1 h2)
End
Theorem collate_equal_thm:
!l. (!x. MEM x l ==> (f x x = EQUAL)) ==> (collate f l l = EQUAL)
Proof
Induct_on `l` \\ rw [collate_def] \\ rw [collate_def]
QED
Theorem collate_short_thm:
!f l1 l2. (!x. f x x = EQUAL) ∧ (l1 ≠ l2) /\ (l1 ≼ l2) ==>
(collate f l1 l2 = LESS)
Proof
ho_match_mp_tac collate_ind
\\ rw[collate_def] \\ fs[]
QED
Theorem collate_long_thm:
!f l1 l2. (!x. f x x = EQUAL) ∧ (l1 ≠ l2) /\ (l2 ≼ l1) ==>
(collate f l1 l2 = GREATER)
Proof
ho_match_mp_tac collate_ind
\\ rw[collate_def] \\ fs[]
QED
Definition cpn_to_reln_def:
cpn_to_reln f x1 x2 = (f x1 x2 = LESS)
End
Theorem collate_cpn_reln_thm:
!f l1 l2. (!x1 x2. (f x1 x2 = EQUAL) <=>
(x1 = x2)) ==> (cpn_to_reln (collate f) l1 l2 = LLEX (cpn_to_reln f) l1 l2)
Proof
ho_match_mp_tac collate_ind \\ rw [collate_def, cpn_to_reln_def, LLEX_def] \\
first_assum (qspecl_then [`h1`, `h1`] (fn th => assume_tac (GSYM th))) \\
`(h1 = h1) = T` by DECIDE_TAC \\ rw[] \\`EQUAL ≠ LESS` by fs[] \\ rw[]
QED
(* from std_preludeLib *)
Definition LENGTH_AUX_def:
(LENGTH_AUX [] n = (n:num)) /\
(LENGTH_AUX (x::xs) n = LENGTH_AUX xs (n+1))
End
Theorem LENGTH_AUX_THM = Q.prove(`
!xs n. LENGTH_AUX xs n = LENGTH xs + n`,
Induct THEN ASM_SIMP_TAC std_ss [LENGTH_AUX_def,LENGTH,ADD1,AC ADD_COMM ADD_ASSOC])
|> Q.SPECL [`xs`,`0`] |> GSYM |> SIMP_RULE std_ss [];
Theorem list_compare_def =
ternaryComparisonsTheory.list_compare_def
(* tail-recursive MAP *)
Definition map_rev'_def:
map_rev' f [] acc = acc ∧
map_rev' f (x :: xs) acc = map_rev' f xs (f x :: acc)
End
Definition map_rev_def:
map_rev f xs = map_rev' f xs []
End
Theorem map_rev'_lemma[local]:
∀acc. map_rev' f xs acc = (map_rev' f xs []) ⧺ acc
Proof
Induct_on ‘xs’
>- rw[map_rev'_def]
\\ rpt strip_tac
\\ rewrite_tac[map_rev'_def]
\\ first_assum $ once_rewrite_tac o single
\\ rw[]
QED
Theorem map_rev_thm:
map_rev f xs = REVERSE (MAP f xs)
Proof
rw[map_rev_def]
\\ Induct_on ‘xs’
\\ rw[map_rev'_def, map_rev'_lemma]
QED
(* tail-recursive FILTER *)
Definition filter_rev'_def:
filter_rev' P [] acc = acc ∧
filter_rev' P (h::t) acc = if P h then filter_rev' P t (h::acc) else filter_rev' P t acc
End
Definition filter_rev_def:
filter_rev P xs = filter_rev' P xs []
End
Theorem filter_rev'_lemma[local]:
∀acc. filter_rev' P xs acc = (filter_rev' P xs []) ⧺ acc
Proof
Induct_on ‘xs’
>- rw[filter_rev'_def]
\\ rpt strip_tac
\\ rewrite_tac[filter_rev'_def]
\\ Cases_on ‘P h’
>- (last_x_assum $ once_rewrite_tac o single
\\ rw[])
\\ metis_tac[]
QED
Theorem filter_rev_thm:
filter_rev f xs = REVERSE (FILTER f xs)
Proof
rw[filter_rev_def]
\\ Induct_on ‘xs’
>- rw[filter_rev'_def]
\\ strip_tac
\\ rw[filter_rev'_def, filter_rev'_lemma]
QED
(* tail-recursive FLAT *)
Definition flat_rev'_def:
flat_rev' [] acc = acc ∧
flat_rev' (x::xs) acc = flat_rev' xs (REV x acc)
End
Definition flat_rev_def:
flat_rev xs = flat_rev' xs []
End
Theorem flat_rev'_lemma[local]:
∀acc. flat_rev' l acc = (flat_rev' l []) ⧺ acc
Proof
Induct_on ‘l’
>- rw[flat_rev'_def]
\\ rpt strip_tac
\\ rewrite_tac[flat_rev'_def]
\\ first_assum $ once_rewrite_tac o single
\\ rw[REV_REVERSE_LEM]
QED
Theorem flat_rev_thm:
flat_rev xs = REVERSE (FLAT xs)
Proof
rw[flat_rev_def]
\\ Induct_on ‘xs’
\\ rw[flat_rev'_def, flat_rev'_lemma, REV_REVERSE_LEM]
QED
val _ = export_theory()