-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathastScript.sml
More file actions
338 lines (307 loc) · 8.44 KB
/
Copy pathastScript.sml
File metadata and controls
338 lines (307 loc) · 8.44 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
(*
Definition of CakeML abstract syntax (AST).
*)
Theory ast
Ancestors
integer[qualified] words[qualified] string[qualified] mlstring[qualified] namespace
(* Literal constants *)
Datatype:
lit =
IntLit int
| Char char
| StrLit mlstring
| Word8 word8
| Word64 word64
| Float64 word64
End
Datatype:
shift = Lsl | Lsr | Asr | Ror
End
Datatype:
arith = Add | Sub | Mul | Div | Mod | Neg | And | Xor | Or | Not | Abs | Sqrt | FMA
| Shift shift
End
(* Module names *)
Type modN = “:mlstring”
(* Variable names *)
Type varN = “:mlstring”
(* Constructor names (from datatype definitions) *)
Type conN = ``: mlstring``
(* Type names *)
Type typeN = ``: mlstring``
(* Type variable names *)
Type tvarN = ``: mlstring``
Datatype:
word_size = W8 | W64
End
Datatype:
thunk_mode = Evaluated | NotEvaluated
End
Datatype:
thunk_op =
AllocThunk thunk_mode
| UpdateThunk thunk_mode
| ForceThunk
End
Datatype:
opb = Lt | Gt | Leq | Geq
End
Datatype:
test = Equal | Compare opb | AltCompare opb
End
Datatype:
prim_type = BoolT
| IntT
| CharT
| StrT
| WordT word_size
| Float64T
End
Datatype:
op =
(* primitive operations for the primitive types: +, -, and, sqrt, etc. *)
Arith arith prim_type
(* conversions between primitive types: char<->int, word<->double, word<->int *)
| FromTo prim_type prim_type
(* Equality and comparisons *)
| Equality
| Test test prim_type
(* Function application *)
| Opapp
(* Reference operations *)
| Opassign
| Opref
| Opderef
(* Word8Array operations *)
| Aw8alloc
| Aw8sub
| Aw8length
| Aw8update
| Aw8subBit
| Aw8updateBit
(* string/bytearray conversions *)
| CopyStrStr
| CopyStrAw8
| CopyAw8Str
| CopyAw8Aw8
| XorAw8Str_unsafe
(* String operations *)
| Implode
| Explode
| Strsub
| Strlen
| Strcat
(* Vector operations *)
| VfromList
| Vsub
| Vlength
(* Array operations *)
| Aalloc
| AallocEmpty
| AallocFixed
| Asub
| Alength
| Aupdate
(* Unsafe vector/array accesses *)
| Vsub_unsafe
| Asub_unsafe
| Aupdate_unsafe
| Aw8sub_unsafe
| Aw8update_unsafe
| Aw8subBit_unsafe
| Aw8updateBit_unsafe
(* thunk operations *)
| ThunkOp thunk_op
(* List operations *)
| ListAppend
(* Configure the GC *)
| ConfigGC
(* Call a given foreign function *)
| FFI mlstring
(* Evaluate new code in a given env *)
| Eval
(* Get the identifier of an env object *)
| Env_id
End
(* Define operator classes, that allow to group their behavior later *)
Datatype:
op_class =
EvalOp (* Eval primitive *)
| FunApp (* function application *)
| Force (* forcing a thunk *)
| Simple (* arithmetic operation, no finite-precision/reals *)
End
Definition getOpClass_def[simp]:
getOpClass op =
case op of
| Opapp => FunApp
| Eval => EvalOp
| ThunkOp t => (if t = ForceThunk then Force else Simple)
| _ => Simple
End
(* Types used in type annotations *)
Datatype:
ast_t =
(* Type variables that the user writes down ('a, 'b, etc.) *)
Atvar tvarN
(* Function type *)
| Atfun ast_t ast_t
(* Tuple type *)
| Attup (ast_t list)
(* Type constructor applications.
0-ary type applications represent unparameterised types (e.g., num or string) *)
| Atapp (ast_t list) ((modN, typeN) id)
End
(* Patterns *)
Datatype:
pat =
Pany
| Pvar varN
| Plit lit
(* Constructor applications.
A Nothing constructor indicates a tuple pattern. *)
| Pcon (((modN, conN) id) option) (pat list)
| Pref pat
(* Pattern alias. *)
| Pas pat varN
| Ptannot pat ast_t
End
(* Short circuiting logical operations *)
Datatype:
lop = Andalso | Orelse
End
Datatype:
locs = NoLocs | Locs (int # int) (int # int)
End
(* Expressions *)
Datatype:
exp =
Raise exp
| Handle exp ((pat # exp) list)
| Lit lit
(* Constructor application.
A Nothing constructor indicates a tuple pattern. *)
| Con (((modN, conN)id)option) (exp list)
| Ident ((modN, varN) id)
| Fun varN exp
(* Application a primitive operator to arguments.
Includes function application. *)
| App op (exp list)
(* Logical operations (and, or) *)
| Log lop exp exp
| If exp exp exp
(* Pattern matching *)
| Mat exp ((pat # exp) list)
(* A let expression
A Nothing value for the binding indicates that this is a
sequencing expression, that is: (e1; e2). *)
| Let (varN option) exp exp
(* Local definition (potentially) mutually recursive
functions.
The first varN is the function's name, and the second varN
is its parameter. *)
| Letrec ((varN # varN # exp) list) exp
| Tannot exp ast_t
(* Location annotated expressions, not expected in source programs *)
| Lannot exp locs
(* Open one non-empty module path for the lexical scope of the body. *)
| Open (modN list) exp
End
Overload Var = “Ident”
Type type_def = ``: ( tvarN list # typeN # (conN # ast_t list) list) list``
(* Declarations *)
Datatype:
dec =
(* Top-level bindings
* The pattern allows several names to be bound at once *)
Dlet locs pat exp
(* Mutually recursive function definition *)
| Dletrec locs ((varN # varN # exp) list)
(* Type definition
Defines several data types, each which has several
named variants, which can in turn have several arguments.
*)
| Dtype locs type_def
(* Type abbreviations *)
| Dtabbrev locs (tvarN list) typeN ast_t
(* New exceptions *)
| Dexn locs conN (ast_t list)
(* Module *)
| Dmod modN (dec list)
(* Local: local part, visible part *)
| Dlocal (dec list) (dec list)
(* Store current lexical env in an env value *)
| Denv tvarN
(* Expose the contents of a non-empty module path as a declaration delta *)
| Dopen locs (modN list)
End
(* No declaration opens, including inside structures and local declarations.
Expression-local Open is deliberately allowed. *)
Definition dopen_free_dec_def:
(dopen_free_dec (Dlet locs p e) = T) /\
(dopen_free_dec (Dletrec locs funs) = T) /\
(dopen_free_dec (Dtype locs tdefs) = T) /\
(dopen_free_dec (Dtabbrev locs tvs tn t) = T) /\
(dopen_free_dec (Dexn locs cn ts) = T) /\
(dopen_free_dec (Denv n) = T) /\
(dopen_free_dec (Dopen locs path) = F) /\
(dopen_free_dec (Dmod mn ds) = EVERY dopen_free_dec ds) /\
(dopen_free_dec (Dlocal lds ds) =
(EVERY dopen_free_dec lds /\ EVERY dopen_free_dec ds))
End
(* Computes the bindings of a pattern *)
Definition pat_bindings_def:
pat_bindings Pany = [] ∧
pat_bindings (Pvar n) = [n] ∧
pat_bindings (Plit l) = [] ∧
pat_bindings (Pcon v0 ps) = pats_bindings ps ∧
pat_bindings (Pref p) = pat_bindings p ∧
pat_bindings (Pas p i) = pat_bindings p ++ [i] ∧
pat_bindings (Ptannot p v1) = pat_bindings p ∧
pats_bindings [] = [] ∧
pats_bindings (p::ps) = pats_bindings ps ++ pat_bindings p
End
Definition every_exp_def[simp]:
(every_exp p (Raise e) ⇔
p (Raise e) ∧ every_exp p e) ∧
(every_exp p (Handle e pes) ⇔
p (Handle e pes) ∧ every_exp p e ∧ EVERY (λ(pat,e). every_exp p e) pes) ∧
(every_exp p (ast$Lit l) ⇔
p (ast$Lit l)) ∧
(every_exp p (Con cn es) ⇔
p (Con cn es) ∧ EVERY (every_exp p) es) ∧
(every_exp p (Ident v) ⇔
p (Ident v)) ∧
(every_exp p (Fun x e) ⇔
p (Fun x e) ∧ every_exp p e) ∧
(every_exp p (App op es) ⇔
p (App op es) ∧ EVERY (every_exp p) es) ∧
(every_exp p (Log lop e1 e2) ⇔
p (Log lop e1 e2) ∧ every_exp p e1 ∧ every_exp p e2) ∧
(every_exp p (If e1 e2 e3) ⇔
p (If e1 e2 e3) ∧ every_exp p e1 ∧ every_exp p e2 ∧ every_exp p e3) ∧
(every_exp p (Mat e pes) ⇔
p (Mat e pes) ∧ every_exp p e ∧ EVERY (λ(pat,e). every_exp p e) pes) ∧
(every_exp p (Let x e1 e2) ⇔
p (Let x e1 e2) ∧ every_exp p e1 ∧ every_exp p e2) ∧
(every_exp p (Tannot e a) ⇔
p (Tannot e a) ∧ every_exp p e) ∧
(every_exp p (Lannot e a) ⇔
p (Lannot e a) ∧ every_exp p e) ∧
(every_exp p (Letrec funs e) ⇔
p (Letrec funs e) ∧ every_exp p e ∧ EVERY (λ(n,v,e). every_exp p e) funs) ∧
(every_exp p (Open path e) ⇔
p (Open path e) ∧ every_exp p e)
End
Definition Seqs_def:
Seqs [] = Con NONE [] ∧
Seqs (x::xs) = Let NONE x (Seqs xs)
End
Definition Apps_def:
Apps f [] = f ∧
Apps f (x::xs) = Apps (App Opapp [f; x]) xs
End
Definition Funs_def:
Funs [] e = e ∧
Funs (x::xs) e = Fun x (Funs xs e)
End