-
Notifications
You must be signed in to change notification settings - Fork 81
/
ArrayProgScript.sml
385 lines (282 loc) · 9.82 KB
/
ArrayProgScript.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
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
(*
A module about Arrays for the CakeML standard basis library.
*)
open preamble ml_translatorLib ml_progLib basisFunctionsLib
Word8ArrayProgTheory
val _ = new_theory"ArrayProg"
val _ = translation_extends"Word8ArrayProg"
val () = ml_prog_update (open_module "Array");
val _ = ml_prog_update (add_dec
``Dtabbrev unknown_loc ["'a"] "array" (Atapp [Atvar "'a"] (Short "array"))`` I);
val () = append_decs
``[mk_binop "array" Aalloc;
mk_unop "arrayEmpty" AallocEmpty;
mk_binop "sub" Asub;
mk_unop "length" Alength;
Dlet unknown_loc (Pvar "update")
(Fun "x" (Fun "y" (Fun "z"
(App Aupdate [Var (Short "x"); Var (Short "y"); Var (Short "z")])))) ]``;
val array_fromList = process_topdecs
`fun fromList l =
let fun f arr l i =
case l of
[] => arr
| (h::t) => (update arr i h; f arr t (i + 1))
in
case l of
[] => arrayEmpty ()
| h::t => f (array (List.length l) h) t 1
end`;
val _ = append_prog array_fromList;
val array_tabulate = process_topdecs
`fun tabulate n f =
let fun u arr x =
if x = n then arr
else (update arr x (f x); u arr (x + 1))
in
if n = 0 then
arrayEmpty ()
else
u (array n (f 0)) 1
end`;
val _ = append_prog array_tabulate;
(*val array_vector = process_topdecs
`fun vector arr = Vector.tabulate (length arr) (fn i => sub arr i)`*)
val _ = ml_prog_update open_local_block;
val array_copy_aux = process_topdecs
`fun copy_aux src dst di max n =
if n = max
then ()
else (update dst di (sub src n); copy_aux src dst (di + 1) max (n + 1))`
val _ = append_prog array_copy_aux;
val _ = ml_prog_update open_local_in_block;
val array_copy = process_topdecs
`fun copy src dst di =
copy_aux src dst di (length src) 0`
val _ = append_prog array_copy;
val _ = ml_prog_update open_local_block;
val array_copyVec_aux = process_topdecs
`fun copyVec_aux src dst di max n =
if n = max
then ()
else (update dst (di + n) (Vector.sub src n); copyVec_aux src dst di max (n + 1))`
val _ = append_prog array_copyVec_aux;
val _ = ml_prog_update open_local_in_block;
val array_copyVec = process_topdecs
`fun copyVec src dst di =
copyVec_aux src dst di (Vector.length src) 0`
val _ = append_prog array_copyVec;
val _ = ml_prog_update open_local_block;
val array_app_aux = process_topdecs
`fun app_aux f arr max n =
if n = max
then ()
else (f (sub arr n); app_aux f arr max (n + 1))`
val _ = append_prog array_app_aux;
val _ = ml_prog_update open_local_in_block;
val array_app = process_topdecs
`fun app f arr =
app_aux f arr (length arr) 0`
val _ = append_prog array_app;
val _ = ml_prog_update open_local_block;
val array_appi_aux = process_topdecs
`fun appi_aux f arr max n =
if n = max
then ()
else (f n (sub arr n): unit;
appi_aux f arr max (n + 1))`
val _ = append_prog array_appi_aux;
val _ = ml_prog_update open_local_in_block;
val array_appi = process_topdecs
`fun appi f arr =
appi_aux f arr (length arr) 0`
val _ = append_prog array_appi;
val _ = ml_prog_update open_local_block;
val array_modify_aux = process_topdecs
`fun modify_aux f arr max n =
if n = max
then ()
else (update arr n (f (sub arr n)); modify_aux f arr max (n + 1))`
val _ = append_prog array_modify_aux;
val _ = ml_prog_update open_local_in_block;
val array_modify = process_topdecs
`fun modify f arr =
modify_aux f arr (length arr) 0`
val _ = append_prog array_modify;
val _ = ml_prog_update open_local_block;
val array_modifyi_aux = process_topdecs
`fun modifyi_aux f arr max n =
if n = max
then ()
else (update arr n (f n (sub arr n)); modifyi_aux f arr max (n + 1))`
val _ = append_prog array_modifyi_aux;
val _ = ml_prog_update open_local_in_block;
val array_modifyi = process_topdecs
`fun modifyi f arr =
modifyi_aux f arr (length arr) 0`
val _ = append_prog array_modifyi;
val _ = ml_prog_update open_local_block;
val array_foldli_aux = process_topdecs
`fun foldli_aux f init arr max n =
if n = max
then init
else foldli_aux f (f n (sub arr n) init ) arr max (n + 1)`
val _ = append_prog array_foldli_aux;
val _ = ml_prog_update open_local_in_block;
val array_foldli = process_topdecs
`fun foldli f init arr =
foldli_aux f init arr (length arr) 0`
val _ = append_prog array_foldli;
val _ = ml_prog_update open_local_block;
val array_foldl_aux = process_topdecs
`fun foldl_aux f init arr max n =
if n = max
then init
else foldl_aux f (f (sub arr n) init ) arr max (n + 1)`
val _ = append_prog array_foldl_aux;
val _ = ml_prog_update open_local_in_block;
val array_foldl = process_topdecs
`fun foldl f init arr =
foldl_aux f init arr (length arr) 0`
val _ = append_prog array_foldl;
val _ = ml_prog_update open_local_block;
val array_foldri_aux = process_topdecs
`fun foldri_aux f init arr n =
if n = 0
then init
else foldri_aux f (f (n - 1) (sub arr (n - 1)) init) arr (n - 1)`
val _ = append_prog array_foldri_aux;
val _ = ml_prog_update open_local_in_block;
val array_foldri = process_topdecs
`fun foldri f init arr =
foldri_aux f init arr (length arr)`
val _ = append_prog array_foldri;
val _ = ml_prog_update open_local_block;
val array_foldr_aux = process_topdecs
`fun foldr_aux f init arr n =
if n = 0
then init
else foldr_aux f (f (sub arr (n - 1)) init) arr (n - 1)`
val _ = append_prog array_foldr_aux;
val _ = ml_prog_update open_local_in_block;
val array_foldr = process_topdecs
`fun foldr f init arr =
foldr_aux f init arr (length arr)`
val _ = append_prog array_foldr;
val _ = ml_prog_update open_local_block;
val array_find_aux = process_topdecs
`fun find_aux f arr max n =
if n = max
then None
else (if f (sub arr n)
then Some(sub arr n)
else find_aux f arr max (n + 1))`
val _ = append_prog array_find_aux;
val _ = ml_prog_update open_local_in_block;
val array_find = process_topdecs
`fun find f arr =
find_aux f arr (length arr) 0`
val _ = append_prog array_find;
val _ = ml_prog_update open_local_block;
(* Parser bug, see Issue #25 *)
val array_findi_aux =
``[(Dletrec unknown_loc
[("findi_aux","f",
Fun "arr"
(Fun "max"
(Fun "n"
(Let (SOME "a")
(App Opapp
[App Opapp [Var (Short "="); Var (Short "n")];
Var (Short "max")])
(If (Var (Short "a")) (Con (SOME (Short "None")) [])
(Let (SOME "b")
(App Opapp
[App Opapp
[Var (Short "sub"); Var (Short "arr")];
Var (Short "n")])
(Let (SOME "c")
(App Opapp
[App Opapp
[Var (Short "f"); Var (Short "n")];
Var (Short "b")])
(If (Var (Short "c"))
(Let (SOME "d")
(App Opapp
[App Opapp
[Var (Short "sub");
Var (Short "arr")];
Var (Short "n")])
(Con (SOME (Short "Some"))
[Con NONE [Var (Short "n");
Var (Short "d")]]))
(Let (SOME "e")
(App Opapp
[App Opapp
[Var (Short "+");
Var (Short "n")];
Lit (IntLit 1)])
(App Opapp
[App Opapp
[App Opapp
[App Opapp
[Var (Short "findi_aux");
Var (Short "f")];
Var (Short "arr")];
Var (Short "max")];
Var (Short "e")]))))))))))])]``
val _ = append_prog array_findi_aux;
val _ = ml_prog_update open_local_in_block;
val array_findi = process_topdecs
`fun findi f arr =
findi_aux f arr (length arr) 0`
val _ = append_prog array_findi;
val _ = ml_prog_update open_local_block;
val array_exists_aux = process_topdecs
`fun exists_aux f arr max n =
if n = max
then False
else (if f (sub arr n)
then True
else exists_aux f arr max (n + 1))`
val _ = append_prog array_exists_aux;
val _ = ml_prog_update open_local_in_block;
val array_exists = process_topdecs
`fun exists f arr =
exists_aux f arr (length arr) 0`
val _ = append_prog array_exists;
val _ = ml_prog_update open_local_block;
val array_all_aux = process_topdecs
`fun all_aux f arr max n =
if n = max
then True
else (if f (sub arr n)
then all_aux f arr max (n + 1)
else False)`
val _ = append_prog array_all_aux;
val _ = ml_prog_update open_local_in_block;
val array_all = process_topdecs
`fun all f arr =
all_aux f arr (length arr) 0`
val _ = append_prog array_all;
val _ = ml_prog_update open_local_block;
val array_collate_aux = process_topdecs
`fun collate_aux f a1 a2 max ord n =
if n = max
then ord
else (if f (sub a1 n) (sub a2 n) = Equal
then collate_aux f a1 a2 max ord (n + 1)
else f (sub a1 n) (sub a2 n))`
val _ = append_prog array_collate_aux;
val _ = ml_prog_update open_local_in_block;
val array_collate = process_topdecs
`fun collate f a1 a2 =
if (length a1) < (length a2)
then collate_aux f a1 a2 (length a1) Less 0
else if (length a2) < (length a1)
then collate_aux f a1 a2 (length a2) Greater 0
else collate_aux f a1 a2 (length a2) Equal 0`
val _ = append_prog array_collate;
val _ = ml_prog_update close_local_blocks;
val _ = ml_prog_update (close_module NONE);
val _ = export_theory ()