-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdafny_astScript.sml
448 lines (403 loc) · 11.6 KB
/
dafny_astScript.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
(*
Definition of Dafny abstract syntax (AST).
*)
open preamble
val _ = new_theory "dafny_ast";
Datatype:
name =
| Name string
End
Datatype:
varName =
| VarName string
End
Datatype:
ident =
| Ident name
End
Datatype:
attribute =
(* Attribute name args *)
| Attribute string (string list)
End
Datatype:
primitive =
| Int
| Real
| String
| Bool
| Char
| Native
End
Datatype:
collKind =
| CollKind_Seq
| CollKind_Array
| CollKind_Map
End
Datatype:
typeArgBound =
| SupportsEquality
| SupportsDefault
End
Datatype:
variance =
| Nonvariant | Covariant | Contravariant
End
Datatype:
typeArgDecl =
(* TypeArgDecl name bounds variance *)
| TypeArgDecl ident (typeArgBound list) variance
End
(* From Dafny:
* USIZE is for whatever target considers that native arrays can be
* indexed with *)
Datatype:
newtypeRange =
| U8 | I8
| U16 | I16
| U32 | I32
| U64 | I64
| U128 | I128
| BigInt | USIZE
| NoRange
End
Datatype:
unaryOp =
| Not | BitwiseNot
| Cardinality
End
Datatype:
binOp =
(* Eq referential *)
| Eq bool
| Div | EuclidianDiv
| Mod | EuclidianMod
(* a ≤ b is !(b < a) *)
| Lt | LtChar
| Plus | Minus | Times
| BitwiseAnd | BitwiseOr | BitwiseXor
| BitwiseShiftRight | BitwiseShiftLeft
| And | Or
| In
| SeqProperPrefix | SeqPrefix
| SetMerge | SetSubtraction | SetIntersection
| Subset | ProperSubset | SetDisjoint
| MapMerge | MapSubtraction
| MultisetMerge | MultisetSubtraction | MultisetIntersection
| Submultiset | ProperSubmultiset | MultisetDisjoint
| Concat
| BinOp_Passthrough string
End
Datatype:
datatypeType = DatatypeType
End
Datatype:
traitType = TraitType
End
Datatype:
resolvedTypeBase =
| ResolvedTypeBase_Class
| ResolvedTypeBase_Datatype (variance list)
| ResolvedTypeBase_Trait
(* Newtype baseType range erase *)
| ResolvedTypeBase_Newtype type newtypeRange bool ;
resolvedType =
(* ResolvedType path typeArgs kind attributes properMethods extendedTypes *)
| ResolvedType (ident list) (type list) resolvedTypeBase (attribute list)
(name list) (type list) ;
type =
| UserDefined resolvedType
| Tuple (type list)
(* Array element dims *)
| Array type num
| Seq type
| Set type
| Multiset type
(* Map key value *)
| Map type type
| SetBuilder type
(* MapBuilder key value *)
| MapBuilder type type
(* Arrow args result *)
| Arrow (type list) type
| Primitive primitive | Passthrough string
| TypeArg ident | Object
End
Datatype:
newtypeType =
(* NewtypeType baseType range erase *)
| NewtypeType type newtypeRange bool
End
Datatype:
literal =
| BoolLiteral bool
| IntLiteral string type
| DecLiteral string string type
(* StringLiteral str verbatim *)
| StringLiteral string bool
| CharLiteral char
| CharLiteralUTF16 num
| Null type
End
Datatype:
formal =
| Formal varName type (attribute list)
End
Datatype:
callSignature =
(* CallSignature parameters *)
| CallSignature (formal list)
End
Datatype:
callName =
(* CallName name onType receiverArg receiverAsArgument signature *)
| CallName name (type option) (formal option) bool callSignature
| MapBuilderAdd
| MapBuilderBuild
| SetBuilderAdd
| SetBuilderBuild
End
Datatype:
assignLhs =
| AssignLhs_Ident varName
(* Select expr field *)
| AssignLhs_Select expression varName
(* Index expr indices *)
| AssignLhs_Index expression (expression list) ;
expression =
| Literal literal
| Expression_Ident varName
(* Companion ids typeArgs *)
| Companion (ident list) (type list)
| ExternCompanion (ident list)
| Expression_Tuple (expression list)
(* New path typeArgs args *)
| New (ident list) (type list) (expression list)
(* NewUninitArray dims typ *)
| NewUninitArray (expression list) type
| ArrayIndexToInt expression
| FinalizeNewArray expression type
(* DatatypeValue datatypeType typeArgs variant isCo contents *)
| DatatypeValue resolvedType (type list) name bool
((varName # expression) list)
(* Convert value from type *)
| Convert expression type type
(* SeqConstruct length elem *)
| SeqConstruct expression expression
| SeqValue (expression list) type
| SetValue (expression list)
| MultisetValue (expression list)
| MapValue ((expression # expression) list)
(* MapBuilder keyType valueType *)
| Expression_MapBuilder type type
(* SeqUpdate expr indexExpr value *)
| SeqUpdate expression expression expression
(* MapUpdate expr indexExpr value *)
| MapUpdate expression expression expression
(* SetBuilder elemType *)
| Expression_SetBuilder type
| ToMultiset expression
| This
(* Ite cond thn els *)
| Ite expression expression expression
(* In contract to Dafny, we do not include formatting information *)
| UnOp unaryOp expression
(* BinOp op left right *)
| BinOp binOp expression expression
(* ArrayLen expr exprType dim native *)
| ArrayLen expression type num bool
| MapKeys expression
| MapValues expression
| MapItems expression
(* Select expr field isConstant onDatatype fieldType *)
| Select expression varName bool bool type
(* SelectFn expr field onDatatype isStatic isConstant arguments *)
| SelectFn expression varName bool bool bool (type list)
(* Index expr collKind indices *)
| Index expression collKind (expression list)
(* IndexRange expr isArray low high *)
| IndexRange expression bool (expression option) (expression option)
(* TupleSelect expr index fieldType *)
| TupleSelect expression num type
(* Call on callName typeArgs args *)
| Expression_Call expression callName (type list) (expression list)
(* Lambda params retType body *)
| Lambda (formal list) type (statement list)
(* BetaRedex values retType expr *)
| BetaRedex ((formal # expression) list) type expression
(* IIFE name typ value iifeBody *)
| IIFE varName type expression expression
(* Apply expr args *)
| Apply expression (expression list)
(* TypeTest on dType variant *)
| TypeTest expression (ident list) name
(* Is expr fromType toType *)
| Is expression type type
| InitializationValue type
| BoolBoundedPool
(* SetBoundedPool of *)
| SetBoundedPool expression
(* MapBoundedPool of *)
| MapBoundedPool expression
(* SeqBoundedPool of includeDuplicates *)
| SeqBoundedPool expression bool
(* ExactBoundedPool of *)
| ExactBoundedPool expression
(* IntRange elemType lo hi up *)
| IntRange type expression expression bool
(* UnboundedIntRange start up *)
| UnboundedIntRange expression bool
(* Quantifier elemType collection is_forall lambda *)
| Quantifier type expression bool expression ;
statement =
| DeclareVar varName type (expression option)
| Assign assignLhs expression
(* If cond thn els *)
| If expression (statement list) (statement list)
(* Labeled lbl body *)
| Labeled string (statement list)
| While expression (statement list)
(* Foreach boundName boundType over body *)
| Foreach varName type expression (statement list)
(* Call on callName typeArgs args outs *)
| Call expression callName (type list) (expression list)
((varName list) option)
| Return expression
| EarlyReturn
(* Break toLabel *)
| Break (string option)
(* TailRecurisve body *)
| TailRecursive (statement list)
| JumpTailCallStart
| Halt
| Print expression
(* ConstructorNewSeparator fields *)
| ConstructorNewSeparator (formal list)
End
(* Dafny comment:
* At this point, constraints have been entirely removed,
* but synonym types might have different witnesses to use for by the compiler
*)
Datatype:
synonymType =
| SynonymType name (typeArgDecl list) type (statement list)
(expression option) (attribute list)
End
Datatype:
method =
(* Method attributes isStatic hasBody outVarsAreUninitFieldsToAssign
wasFunction overridingPath name typeParams params body outTypes
outVars *)
(* Comments from Dafny (probably Rust specific) *)
(* outVarsAreUninitFieldsToAssign: for constructors *)
(* wasFunction: to choose between "&self" and "&mut self" *)
| Method (attribute list) bool bool bool bool ((ident list) option) name
(typeArgDecl list) (formal list) (statement list)
(type list) ((varName list) option)
End
Datatype:
field =
(* Field formal defaultValue *)
| Field formal (expression option)
End
Datatype:
classItem =
| ClassItem_Method(method)
End
Datatype:
trait =
(* Trait name typeParams parents body attributes *)
| Trait name (typeArgDecl list) (type list) (classItem list) (attribute list)
End
Datatype:
class =
(* Class name enclosingModule typeParams superClasses fields
body attributes *)
| Class name ident (typeArgDecl list) (type list) (field list)
(classItem list) (attribute list)
End
Datatype:
newtypeConstraint =
(* NewtypeConstraint variable constraintStmts *)
| NewtypeConstraint formal (statement list)
End
Datatype:
newtype =
(* Newtype name typeParams base range constraint
witnessStmts witnessExpr attributes *)
| Newtype name (typeArgDecl list) type newtypeRange (newtypeConstraint option)
(statement list) (expression option) (attribute list)
End
Datatype:
datatypeDtor =
(* DatatypeDtor formal callName *)
| DatatypeDtor formal (string option)
End
Datatype:
datatypeCtor =
(* DatatypeCtor name args hasAnyArgs (* includes ghost *) *)
| DatatypeCtor name (datatypeDtor list) bool
End
Datatype:
datatype =
(* Datatype name enclosingModule typeParams ctors
body isCo attributes *)
| Datatype name ident (typeArgDecl list) (datatypeCtor list)
(classItem list) bool (attribute list)
End
Datatype:
module =
(* Module name attributes requiresExterns body *)
| Module name (attribute list) bool ((moduleItem list) option) ;
moduleItem =
| ModuleItem_Module module
| ModuleItem_Class class
| ModuleItem_Trait trait
| ModuleItem_Newtype newtype
| ModuleItem_SynonymType synonymType
| ModuleItem_Datatype datatype
End
Definition dest_Ident_def:
dest_Ident (Ident n) = n
End
Definition dest_varName_def:
dest_varName (VarName s) = s
End
Definition dest_Method_def:
dest_Method (Method attributes isStatic hasBody outVarsAreUninitFieldsToAssign
wasFunction overridingPath nam typeParams params body
outTypes outVars) =
(attributes, isStatic, hasBody, outVarsAreUninitFieldsToAssign, wasFunction,
overridingPath, nam, typeParams, params, body, outTypes, outVars)
End
(* Adapted from
* https://github.com/dafny-lang/dafny/blob/bc6b587e264e3c531c4d6698abd421abdff3aea9/Source/DafnyCore/Generic/Util.cs#L341
*)
Definition unescape_string_def:
(unescape_string (c1::c2::rest) verbatim =
if verbatim then
if c1 = #"\"" ∧ c2 = #"\"" then
#"\""::(unescape_string rest verbatim)
else
c1::(unescape_string (c2::rest) verbatim)
else if c1 = #"\\" ∧ c2 = #"'" then
#"'"::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"\"" then
#"\""::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"\\" then
#"\\"::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"0" then
#"\000"::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"n" then
#"\n"::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"r" then
#"\r"::(unescape_string rest verbatim)
else if c1 = #"\\" ∧ c2 = #"t" then
#"\t"::(unescape_string rest verbatim)
else
c1::(unescape_string (c2::rest) verbatim)) ∧
(unescape_string (c::rest) verbatim = c::(unescape_string rest verbatim)) ∧
(unescape_string "" _ = "")
End
val _ = export_theory();