1 (* Title: Pure/Syntax/syn_ext.ML
3 Author: Markus Wenzel and Carsten Clasohm, TU Muenchen
5 Syntax extension (internal interface).
10 val dddot_indexname: indexname
11 val constrainC: string
14 val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp)
15 val mk_trfun: string * 'a -> string * ('a * stamp)
16 val eq_trfun: ('a * stamp) * ('a * stamp) -> bool
27 val typ_to_nonterm: typ -> string
30 Argument of string * int |
32 Bg of int | Brk of int | En
33 datatype xprod = XProd of string * xsymb list * string * int
35 val delims_of: xprod list -> string list list
36 datatype mfix = Mfix of string * typ * string * int list * int
42 parse_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list,
43 parse_rules: (Ast.ast * Ast.ast) list,
44 parse_translation: (string * ((term list -> term) * stamp)) list,
45 print_translation: (string * ((bool -> typ -> term list -> term) * stamp)) list,
46 print_rules: (Ast.ast * Ast.ast) list,
47 print_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list,
48 token_translation: (string * string * (string -> string * real)) list}
49 val mfix_args: string -> int
50 val escape_mfix: string -> string
51 val syn_ext': bool -> (string -> bool) -> mfix list ->
52 string list -> (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
53 (string * ((term list -> term) * stamp)) list *
54 (string * ((bool -> typ -> term list -> term) * stamp)) list *
55 (string * ((Ast.ast list -> Ast.ast) * stamp)) list
56 -> (string * string * (string -> string * real)) list
57 -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
58 val syn_ext: mfix list -> string list ->
59 (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
60 (string * ((term list -> term) * stamp)) list *
61 (string * ((bool -> typ -> term list -> term) * stamp)) list *
62 (string * ((Ast.ast list -> Ast.ast) * stamp)) list
63 -> (string * string * (string -> string * real)) list
64 -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
65 val syn_ext_const_names: string list -> syn_ext
66 val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext
68 (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
69 (string * ((term list -> term) * stamp)) list *
70 (string * ((bool -> typ -> term list -> term) * stamp)) list *
71 (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext
72 val syn_ext_tokentrfuns: (string * string * (string -> string * real)) list -> syn_ext
76 structure SynExt: SYN_EXT =
80 (** misc definitions **)
82 val dddot_indexname = ("dddot", 0);
83 val constrainC = "_constrain";
86 (* syntactic categories *)
89 val logicT = Type (logic, []);
94 val typeT = Type ("type", []);
97 val spropT = Type (sprop, []);
100 val anyT = Type (any, []);
104 (** datatype xprod **)
106 (*Delim s: delimiter s
107 Argument (s, p): nonterminal s requiring priority >= p, or valued token
108 Space s: some white space for printing
109 Bg, Brk, En: blocks and breaks for pretty printing*)
113 Argument of string * int |
115 Bg of int | Brk of int | En;
117 fun is_delim (Delim _) = true
118 | is_delim _ = false;
120 fun is_terminal (Delim _) = true
121 | is_terminal (Argument (s, _)) = Lexicon.is_terminal s
122 | is_terminal _ = false;
124 fun is_argument (Argument _) = true
125 | is_argument _ = false;
127 fun is_index (Argument ("index", _)) = true
128 | is_index _ = false;
130 val index = Argument ("index", 1000);
133 (*XProd (lhs, syms, c, p):
134 lhs: name of nonterminal on the lhs of the production
135 syms: list of symbols on the rhs of the production
136 c: head of parse tree
137 p: priority of this production*)
139 datatype xprod = XProd of string * xsymb list * string * int;
141 val max_pri = 1000; (*maximum legal priority*)
142 val chain_pri = ~1; (*dummy for chain productions*)
147 fun delims_of xprods =
149 fun del_of (Delim s) = SOME s
152 fun dels_of (XProd (_, xsymbs, _, _)) =
153 List.mapPartial del_of xsymbs;
155 map Symbol.explode (distinct (List.concat (map dels_of xprods)))
160 (** datatype mfix **)
162 (*Mfix (sy, ty, c, ps, p):
163 sy: rhs of production as symbolic string
164 ty: type description of production
165 c: head of parse tree
166 ps: priorities of arguments in sy
167 p: priority of production*)
169 datatype mfix = Mfix of string * typ * string * int list * int;
171 fun err_in_mfix msg (Mfix (sy, _, const, _, _)) =
172 error ((if msg = "" then "" else msg ^ "\n") ^
173 "in mixfix annotation " ^ quote sy ^ " for " ^ quote const);
178 fun typ_to_nt _ (Type (c, _)) = c
179 | typ_to_nt default _ = default;
181 (*get nonterminal for rhs*)
182 val typ_to_nonterm = typ_to_nt any;
184 (*get nonterminal for lhs*)
185 val typ_to_nonterm1 = typ_to_nt logic;
191 fun is_meta c = c mem ["(", ")", "/", "_", "\\<index>"];
193 val scan_delim_char =
194 $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) ||
195 Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof);
197 fun read_int ["0", "0"] = ~1
198 | read_int cs = #1 (Library.read_int cs);
201 $$ "_" >> K (Argument ("", 0)) ||
202 $$ "\\<index>" >> K index ||
203 $$ "(" |-- Scan.any Symbol.is_digit >> (Bg o read_int) ||
205 $$ "/" -- $$ "/" >> K (Brk ~1) ||
206 $$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) ||
207 Scan.any1 Symbol.is_blank >> (Space o implode) ||
208 Scan.repeat1 scan_delim_char >> (Delim o implode);
212 $$ "'" -- Scan.one Symbol.is_blank >> K NONE;
214 val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (Scan.one (not_equal "'"));
215 val read_symbs = List.mapPartial I o valOf o Scan.read Symbol.stopper scan_symbs;
217 fun unique_index xsymbs =
218 if length (List.filter is_index xsymbs) <= 1 then xsymbs
219 else error "Duplicate index arguments (\\<index>)";
221 val read_mixfix = unique_index o read_symbs o Symbol.explode;
222 val mfix_args = length o List.filter is_argument o read_mixfix;
223 val escape_mfix = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode;
229 fun mfix_to_xprod convert is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) =
232 if p >= 0 andalso p <= max_pri then ()
233 else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix;
235 fun blocks_ok [] 0 = true
236 | blocks_ok [] _ = false
237 | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1)
238 | blocks_ok (En :: _) 0 = false
239 | blocks_ok (En :: syms) n = blocks_ok syms (n - 1)
240 | blocks_ok (_ :: syms) n = blocks_ok syms n;
242 fun check_blocks syms =
243 if blocks_ok syms 0 then ()
244 else err_in_mfix "Unbalanced block parentheses" mfix;
247 val cons_fst = apfst o cons;
249 fun add_args [] ty [] = ([], typ_to_nonterm1 ty)
250 | add_args [] _ _ = err_in_mfix "Too many precedences" mfix
251 | add_args ((arg as Argument ("index", _)) :: syms) ty ps =
252 cons_fst arg (add_args syms ty ps)
253 | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
254 cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys [])
255 | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
256 cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps)
257 | add_args (Argument _ :: _) _ _ =
258 err_in_mfix "More arguments than in corresponding type" mfix
259 | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps);
261 fun rem_pri (Argument (s, _)) = Argument (s, chain_pri)
264 fun logify_types copy_prod (a as (Argument (s, p))) =
265 if s <> "prop" andalso is_logtype s then Argument (logic, p) else a
266 | logify_types _ a = a;
269 val raw_symbs = read_mixfix sy handle ERROR => err_in_mfix "" mfix;
270 val args = List.filter (fn Argument _ => true | _ => false) raw_symbs;
271 val (const', typ', parse_rules) =
272 if not (exists is_index args) then (const, typ, [])
275 val indexed_const = if const <> "" then "_indexed_" ^ const
276 else err_in_mfix "Missing constant name for indexed syntax" mfix;
277 val rangeT = Term.range_type typ handle Match =>
278 err_in_mfix "Missing structure argument for indexed syntax" mfix;
280 val xs = map Ast.Variable (Term.invent_names [] "xa" (length args - 1));
281 val (xs1, xs2) = Library.splitAt (Library.find_index is_index args, xs);
282 val i = Ast.Variable "i";
283 val lhs = Ast.mk_appl (Ast.Constant indexed_const)
284 (xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2);
285 val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs);
286 in (indexed_const, rangeT, [(lhs, rhs)]) end;
288 val (symbs, lhs) = add_args raw_symbs typ' pris;
291 lhs mem ["prop", "logic"]
293 andalso not (null symbs)
294 andalso not (exists is_delim symbs);
296 if convert andalso not copy_prod then
297 (if lhs = "prop" then sprop else if is_logtype lhs then logic else lhs)
299 val symbs' = map (logify_types copy_prod) symbs;
300 val xprod = XProd (lhs', symbs', const', pri);
302 val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs');
304 if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix
305 else if const <> "" then xprod
306 else if length (List.filter is_argument symbs') <> 1 then
307 err_in_mfix "Copy production must have exactly one argument" mfix
308 else if exists is_terminal symbs' then xprod
309 else XProd (lhs', map rem_pri symbs', "", chain_pri);
311 in (xprod', parse_rules) end;
315 (** datatype syn_ext **)
321 prmodes: string list,
322 parse_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list,
323 parse_rules: (Ast.ast * Ast.ast) list,
324 parse_translation: (string * ((term list -> term) * stamp)) list,
325 print_translation: (string * ((bool -> typ -> term list -> term) * stamp)) list,
326 print_rules: (Ast.ast * Ast.ast) list,
327 print_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list,
328 token_translation: (string * string * (string -> string * real)) list};
333 fun syn_ext' convert is_logtype mfixes consts trfuns tokentrfuns (parse_rules, print_rules) =
335 val (parse_ast_translation, parse_translation, print_translation,
336 print_ast_translation) = trfuns;
338 val (xprods, parse_rules') = map (mfix_to_xprod convert is_logtype) mfixes
339 |> split_list |> apsnd (rev o List.concat);
340 val mfix_consts = distinct (map (fn Mfix x => #3 x) mfixes @ map (fn XProd x => #3 x) xprods);
344 consts = consts union_string mfix_consts,
345 prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns),
346 parse_ast_translation = parse_ast_translation,
347 parse_rules = parse_rules' @ parse_rules,
348 parse_translation = parse_translation,
349 print_translation = print_translation,
350 print_rules = map swap parse_rules' @ print_rules,
351 print_ast_translation = print_ast_translation,
352 token_translation = tokentrfuns}
356 val syn_ext = syn_ext' true (K false);
358 fun syn_ext_const_names cs = syn_ext [] cs ([], [], [], []) [] ([], []);
359 fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) [] rules;
360 fun syn_ext_trfuns trfuns = syn_ext [] [] trfuns [] ([], []);
361 fun syn_ext_tokentrfuns trfuns = syn_ext [] [] ([], [], [], []) trfuns ([], []);
363 fun stamp_trfun s (c, f) = (c, (f, s));
364 fun mk_trfun tr = stamp_trfun (stamp ()) tr;
365 fun eq_trfun ((_, s1:stamp), (_, s2)) = s1 = s2;
370 val pure_ext = syn_ext' false (K false)
371 [Mfix ("_", spropT --> propT, "", [0], 0),
372 Mfix ("_", logicT --> anyT, "", [0], 0),
373 Mfix ("_", spropT --> anyT, "", [0], 0),
374 Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
375 Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
376 Mfix ("_::_", [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
377 Mfix ("_::_", [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]