-
Notifications
You must be signed in to change notification settings - Fork 85
/
ArrayProgScript.sml
370 lines (296 loc) · 8.38 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
(*
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 cakeml = append_prog o process_topdecs;
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")])))) ]``;
Quote cakeml:
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
End
Quote cakeml:
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
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun copy src dst di =
copy_aux src dst di (length src) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun copyVec src dst di =
copyVec_aux src dst di (Vector.length src) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
fun app_aux f arr max n =
if n = max
then ()
else (f (sub arr n); app_aux f arr max (n + 1))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun app f arr =
app_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun appi f arr =
appi_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun modify f arr =
modify_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun modifyi f arr =
modifyi_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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)
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun foldli f init arr =
foldli_aux f init arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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)
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun foldl f init arr =
foldl_aux f init arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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)
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun foldri f init arr =
foldri_aux f init arr (length arr)
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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)
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun foldr f init arr =
foldr_aux f init arr (length arr)
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun find f arr =
find_aux f arr (length arr) 0
End
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;
Quote cakeml:
fun findi f arr =
findi_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun exists f arr =
exists_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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)
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
fun all f arr =
all_aux f arr (length arr) 0
End
val _ = ml_prog_update open_local_block;
Quote cakeml:
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))
End
val _ = ml_prog_update open_local_in_block;
Quote cakeml:
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
End
Quote cakeml:
val lookup = fn arr => fn default => fn n =>
sub arr n handle _ => default
End
Quote cakeml:
val updateResize = fn arr => fn default => fn n => fn v =>
(update arr n v; arr) handle _ =>
let
val arr' = array (2*n+1) default
in
(copy arr arr' 0;
update arr' n v;
arr')
end
End
val _ = ml_prog_update close_local_blocks;
val _ = ml_prog_update (close_module NONE);
val _ = export_theory ()