forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgramScript.sml
More file actions
238 lines (213 loc) · 8.92 KB
/
gramScript.sml
File metadata and controls
238 lines (213 loc) · 8.92 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
open HolKernel Parse boolLib bossLib
open tokensTheory grammarTheory locationTheory
open lcsymtacs grammarLib
val _ = new_theory "gram"
val _ = set_grammar_ancestry ["tokens", "grammar", "location"]
(* ----------------------------------------------------------------------
Define the CakeML Context-Free Grammar
---------------------------------------------------------------------- *)
val tokmap0 =
List.foldl (fn ((s,t), acc) => Binarymap.insert(acc,s,t))
(Binarymap.mkDict String.compare)
[("(", ``LparT``), (")", ``RparT``), (",", ``CommaT``),
("[", ``LbrackT``),
("]", ``RbrackT``),
(";", ``SemicolonT``), (":=", ``SymbolT ":="``),
(":>", ``SealT``),
("::", ``SymbolT "::"``), ("@", ``SymbolT "@"``),
("->", ``ArrowT``), ("=>", ``DarrowT``),
("*", ``StarT``),
("|", ``BarT``), ("=", ``EqualsT``), (":", ``ColonT``),
("_", ``UnderbarT``),
("and", ``AndT``),
("andalso", ``AndalsoT``),
("before", ``AlphaT "before"``),
("Bind", ``AlphaT "Bind"``),
("case", ``CaseT``),
("datatype", ``DatatypeT``),
("Div", ``AlphaT "Div"``),
("else", ``ElseT``),
("end", ``EndT``),
("exception", ``ExceptionT``),
("false", ``AlphaT "false"``),
("fn", ``FnT``),
("fun", ``FunT``),
("handle", ``HandleT``),
("if", ``IfT``),
("in", ``InT``),
("IntError", ``AlphaT "IntError"``),
("let", ``LetT``),
("nil", ``AlphaT "nil"``),
("o", ``AlphaT "o"``),
("of", ``OfT``),
("op", ``OpT``),
("orelse", ``OrelseT``),
("raise", ``RaiseT``),
("ref", ``RefT``),
("sig", ``SigT``),
("struct", ``StructT``),
("structure", ``StructureT``),
("then", ``ThenT``),
("true", ``AlphaT "true"``),
("type", ``TypeT``),
("val", ``ValT``)]
fun tokmap s =
case Binarymap.peek(tokmap0, s) of
NONE => raise Fail ("No token binding for "^s)
| SOME t => t
val ginfo = { tokmap = tokmap,
tokty = ``:token``, nt_tyname = "MMLnonT",
start = "TopLevelDecs",
gname = "cmlG", mkntname = (fn s => "n" ^ s) }
val cmlG_def = mk_grammar_def ginfo
`(* types *)
UQTyOp ::= <AlphaT> | <SymbolT> | "ref" ;
TyvarN ::= <TyvarT>;
TyOp ::= UQTyOp | <LongidT>;
TypeList1 ::= Type | Type "," TypeList1;
TypeList2 ::= Type "," TypeList1;
Tbase ::= <TyvarT> | TyOp | "(" TypeList2 ")" TyOp | "(" Type ")";
DType ::= DType TyOp | Tbase;
PType ::= DType "*" PType | DType;
Type ::= PType | PType "->" Type;
TbaseList ::= | PTbase TbaseList ;
PTbase ::= <TyvarT> | TyOp | "(" Type ")" ;
(* type declarations *)
TypeName ::= UQTyOp | "(" TyVarList ")" UQTyOp | <TyvarT> UQTyOp ;
TyVarList ::= TyvarN | TyVarList "," TyvarN;
Dconstructor ::= UQConstructorName "of" Type
| UQConstructorName TbaseList;
DtypeCons ::= Dconstructor | DtypeCons "|" Dconstructor;
DtypeDecl ::= TypeName "=" DtypeCons ;
DtypeDecls ::= DtypeDecl | DtypeDecls "and" DtypeDecl;
TypeDec ::= "datatype" DtypeDecls;
TypeAbbrevDec ::= "type" TypeName "=" Type;
(* expressions - base cases and function applications *)
UQConstructorName ::= ^(``{AlphaT s | s ≠ "" ∧ isUpper (HD s)}``)
| "true" | "false" | "nil";
ConstructorName ::=
UQConstructorName
| ^(``{LongidT str s | str,s | s ≠ "" ∧ isAlpha (HD s) ∧ isUpper (HD s) ∨
s ∈ {"true"; "false"; "nil"}}``);
V ::= ^(``{AlphaT s | s ∉ {"before"; "div"; "mod"; "o"; "true"; "false";
"nil" } ∧
s ≠ "" ∧ ¬isUpper (HD s)}``)
| ^(``{SymbolT s |
s ∉ {"+"; "*"; "-"; "/"; "<"; ">"; "<="; ">="; "<>"; ":=";
"::"; "@"; "\094"}}``);
FQV ::= V
| ^(``{LongidT str s | str,s |
s ≠ "" ∧ (isAlpha (HD s) ⇒ ¬isUpper (HD s)) ∧
s ∉ {"true"; "false"; "nil"}}``) ;
OpID ::= ^(``{LongidT str s | str,s | s ≠ ""}``)
| ^(``{AlphaT s | s ≠ ""}``)
| ^(``{SymbolT s | s ≠ ""}``)
| "*" | "=" | "ref" ;
Eliteral ::= <IntT> | <CharT> | <StringT> | <WordT> | <FFIT> ;
Ebase ::= "(" Eseq ")" | Etuple | "(" ")" | FQV | ConstructorName | Eliteral
| "let" LetDecs "in" Eseq "end" | "[" "]"
| "[" Elist1 "]" | "op" OpID | "ref" ;
Eseq ::= E ";" Eseq | E;
Etuple ::= "(" Elist2 ")";
Elist2 ::= E "," Elist1;
Elist1 ::= E | E "," Elist1;
Eapp ::= Eapp Ebase | Ebase;
(* expressions - binary operators *)
MultOps ::= ^(``{AlphaT "div"; AlphaT "mod"; StarT; SymbolT "/"}``);
AddOps ::= ^(``{SymbolT "+"; SymbolT "-"; SymbolT "\094" }``);
RelOps ::= ^(``{SymbolT s | s ∈ {"<"; ">"; "<="; ">="; "<>"}}``) | "=";
CompOps ::= "o" | ":=";
ListOps ::= "@" | "::";
Emult ::= Emult MultOps Eapp | Eapp;
Eadd ::= Eadd AddOps Emult | Emult;
Elistop ::= Eadd ListOps Elistop | Eadd;
Erel ::= Erel RelOps Elistop | Elistop;
Ecomp ::= Ecomp CompOps Erel | Erel;
Ebefore ::= Ebefore "before" Ecomp | Ecomp;
Etyped ::= Ebefore | Ebefore ":" Type;
ElogicAND ::= ElogicAND "andalso" Etyped | Etyped;
ElogicOR ::= ElogicOR "orelse" ElogicAND | ElogicAND;
Ehandle ::= ElogicOR | ElogicOR "handle" PEs ;
E ::= "if" E "then" E "else" E | "case" E "of" PEs | "fn" Pattern "=>" E
| "raise" E | Ehandle;
E' ::= "if" E "then" E "else" E' | "raise" E' | ElogicOR ;
(* function and value declarations *)
FDecl ::= V PbaseList1 "=" E ;
AndFDecls ::= FDecl | AndFDecls "and" FDecl;
Decl ::= "val" Pattern "=" E | "fun" AndFDecls | TypeDec
| "exception" Dconstructor
| TypeAbbrevDec ;
Decls ::= Decl Decls | ";" Decls | ;
LetDec ::= "val" V "=" E | "fun" AndFDecls ;
LetDecs ::= LetDec LetDecs | ";" LetDecs | ;
(* patterns *)
Pbase ::= V | ConstructorName | <IntT> | <StringT> | <CharT> | Ptuple | "_"
| "[" "]" | "[" PatternList "]" | "op" OpID;
PConApp ::= ConstructorName | "ref" | PConApp Pbase ;
Papp ::= PConApp Pbase | Pbase ;
Pcons ::= Papp "::" Pcons | Papp ;
Pattern ::= Pcons | Pcons ":" Type ;
Ptuple ::= "(" ")" | "(" PatternList ")";
PatternList ::= Pattern | Pattern "," PatternList ;
PbaseList1 ::= Pbase | Pbase PbaseList1 ;
PE ::= Pattern "=>" E;
PE' ::= Pattern "=>" E';
PEs ::= PE | PE' "|" PEs;
(* modules *)
StructName ::= ^(``{AlphaT s | s ≠ ""}``) ;
SpecLine ::= "val" V ":" Type
| "type" TypeName OptTypEqn
| "exception" Dconstructor
| TypeDec ;
OptTypEqn ::= "=" Type | ;
SpecLineList ::= SpecLine SpecLineList | ";" SpecLineList | ;
SignatureValue ::= "sig" SpecLineList "end" ;
OptionalSignatureAscription ::= ":>" SignatureValue | ;
Structure ::= "structure" StructName OptionalSignatureAscription "=" "struct" Decls "end";
TopLevelDec ::= Structure | Decl;
TopLevelDecs ::= E ";" TopLevelDecs | TopLevelDec NonETopLevelDecs | ";" TopLevelDecs | ;
NonETopLevelDecs ::= TopLevelDec NonETopLevelDecs | ";" TopLevelDecs | ;
`;
val _ = type_abbrev("NT", ``:MMLnonT inf``)
val _ = overload_on("mkNT", ``INL : MMLnonT -> NT``)
val _ = overload_on ("NN", ``\nt. NT (mkNT nt)``)
val _ = overload_on ("TK", ``TOK : token -> (token,MMLnonT)symbol``)
val _ = type_abbrev("mlptree", ``:(token, MMLnonT, locs) parsetree``)
val nt_distinct_ths = let
val ntlist = TypeBase.constructors_of ``:MMLnonT``
fun recurse [] = []
| recurse (t::ts) = let
val eqns = map (fn t' => mk_eq(t,t')) ts
val ths0 = map (SIMP_CONV (srw_ss()) []) eqns
val ths1 = map (CONV_RULE (LAND_CONV (REWR_CONV EQ_SYM_EQ))) ths0
in
ths0 @ ths1 @ recurse ts
end
in
save_thm("nt_distinct_ths", LIST_CONJ (recurse ntlist))
end
val Ndl_def = Define`
(Ndl n l = Nd (n, unknown_loc) l)`
val Lfl_def = Define`
(Lfl t = Lf (t, unknown_loc))`
val _ = computeLib.add_persistent_funs ["nt_distinct_ths"]
val ast =
``let mkI = λn. Ndl (mkNT nEbase) [Ndl (mkNT nEliteral) [Lfl (TK (IntT n))]]
in
Ndl (mkNT nEmult) [
Ndl (mkNT nEmult) [
Ndl (mkNT nEmult) [Ndl (mkNT nEapp) [mkI 3]];
Ndl (mkNT nMultOps) [Lfl (TK StarT)];
Ndl (mkNT nEapp) [mkI 4]
];
Ndl (mkNT nMultOps) [Lfl (TK (SymbolT "/"))];
Ndl (mkNT nEapp) [mkI 5]
]``
val check_results =
time (SIMP_CONV (srw_ss())
[valid_ptree_def, cmlG_def,DISJ_IMP_THM, FORALL_AND_THM,
finite_mapTheory.FAPPLY_FUPDATE_THM, LET_THM,Ndl_def,Lfl_def])
``valid_ptree cmlG ^ast``
val _ = if aconv (rhs (concl check_results)) T then print "valid_ptree: OK\n"
else raise Fail "valid_ptree: failed"
val _ = export_theory()