src/Pure/Syntax/syntax.ML
author wenzelm
Mon Jul 23 14:06:14 2007 +0200 (2007-07-23)
changeset 23923 8c10f3515633
parent 23660 18765718cf62
child 24247 9d0bb01f6634
permissions -rw-r--r--
hide internal structures (again);
     1 (*  Title:      Pure/Syntax/syntax.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Root of Isabelle's syntax module.
     6 *)
     7 
     8 signature BASIC_SYNTAX =
     9 sig
    10   include AST0
    11   include SYN_TRANS0
    12   include MIXFIX0
    13   include PRINTER0
    14 end;
    15 
    16 signature SYNTAX =
    17 sig
    18   include AST1
    19   include LEXICON0
    20   include SYN_EXT0
    21   include TYPE_EXT0
    22   include SYN_TRANS1
    23   include MIXFIX1
    24   include PRINTER0
    25   val extend_trtab: string -> (string * ('a * stamp)) list ->
    26     ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
    27   val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
    28     ('a * stamp) Symtab.table
    29   val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    30     -> ('a * stamp) list Symtab.table
    31   val extend_tr'tab: (string * ('a * stamp)) list ->
    32     ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    33   datatype 'a trrule =
    34     ParseRule of 'a * 'a |
    35     PrintRule of 'a * 'a |
    36     ParsePrintRule of 'a * 'a
    37   type syntax
    38   val eq_syntax: syntax * syntax -> bool
    39   val is_keyword: syntax -> string -> bool
    40   type mode
    41   val default_mode: mode
    42   val input_mode: mode
    43   val internalM: string
    44   val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
    45   val extend_const_gram: (string -> bool) ->
    46     mode -> (string * typ * mixfix) list -> syntax -> syntax
    47   val extend_consts: string list -> syntax -> syntax
    48   val extend_trfuns:
    49     (string * ((ast list -> ast) * stamp)) list *
    50     (string * ((term list -> term) * stamp)) list *
    51     (string * ((bool -> typ -> term list -> term) * stamp)) list *
    52     (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
    53   val extend_advanced_trfuns:
    54     (string * ((Proof.context -> ast list -> ast) * stamp)) list *
    55     (string * ((Proof.context -> term list -> term) * stamp)) list *
    56     (string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list *
    57     (string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax
    58   val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax
    59   val remove_const_gram: (string -> bool) ->
    60     mode -> (string * typ * mixfix) list -> syntax -> syntax
    61   val extend_trrules: Proof.context -> (string -> bool) -> syntax ->
    62     (string * string) trrule list -> syntax -> syntax
    63   val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
    64     (string * string) trrule list -> syntax -> syntax
    65   val extend_trrules_i: ast trrule list -> syntax -> syntax
    66   val remove_trrules_i: ast trrule list -> syntax -> syntax
    67   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    68   val merge_syntaxes: syntax -> syntax -> syntax
    69   val basic_syn: syntax
    70   val basic_nonterms: string list
    71   val appl_syntax: (string * string * mixfix) list
    72   val applC_syntax: (string * string * mixfix) list
    73   val print_gram: syntax -> unit
    74   val print_trans: syntax -> unit
    75   val print_syntax: syntax -> unit
    76   val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list
    77   val read_term:
    78     (((string * int) * sort) list -> string * int -> Term.sort) ->
    79     (string -> string option) -> (string -> string option) ->
    80     (typ -> typ) -> (sort -> sort) -> Proof.context ->
    81     (string -> bool) -> syntax -> typ -> string -> term list
    82   val read_typ: Proof.context -> syntax -> ((indexname * sort) list -> indexname -> sort) ->
    83     (sort -> sort) -> string -> typ
    84   val read_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort
    85   val pretty_term: (string -> xstring) -> Proof.context -> syntax -> bool -> term -> Pretty.T
    86   val pretty_typ: Proof.context -> syntax -> typ -> Pretty.T
    87   val pretty_sort: Proof.context -> syntax -> sort -> Pretty.T
    88   val ambiguity_level: int ref
    89   val ambiguity_is_error: bool ref
    90 end;
    91 
    92 structure Syntax: SYNTAX =
    93 struct
    94 
    95 (** tables of translation functions **)
    96 
    97 (* parse (ast) translations *)
    98 
    99 fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
   100 
   101 fun err_dup_trfun name c =
   102   error ("More than one " ^ name ^ " for " ^ quote c);
   103 
   104 fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
   105 
   106 fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns)
   107   handle Symtab.DUP c => err_dup_trfun name c;
   108 
   109 fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
   110   handle Symtab.DUP c => err_dup_trfun name c;
   111 
   112 
   113 (* print (ast) translations *)
   114 
   115 fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
   116 fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns;
   117 fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
   118 fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
   119 
   120 
   121 
   122 (** tables of token translation functions **)
   123 
   124 fun lookup_tokentr tabs modes =
   125   let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
   126   in fn c => Option.map fst (AList.lookup (op =) trs c) end;
   127 
   128 fun merge_tokentrtabs tabs1 tabs2 =
   129   let
   130     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   131 
   132     fun name (s, _) = implode (tl (Symbol.explode s));
   133 
   134     fun merge mode =
   135       let
   136         val trs1 = these (AList.lookup (op =) tabs1 mode);
   137         val trs2 = these (AList.lookup (op =) tabs2 mode);
   138         val trs = distinct eq_tr (trs1 @ trs2);
   139       in
   140         (case duplicates (eq_fst (op =)) trs of
   141           [] => (mode, trs)
   142         | dups => error ("More than one token translation function in mode " ^
   143             quote mode ^ " for " ^ commas_quote (map name dups)))
   144       end;
   145   in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
   146 
   147 fun extend_tokentrtab tokentrs tabs =
   148   let
   149     fun ins_tokentr (m, c, f) =
   150       AList.default (op =) (m, [])
   151       #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
   152   in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
   153 
   154 
   155 
   156 (** tables of translation rules **)
   157 
   158 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   159 
   160 fun dest_ruletab tab = maps snd (Symtab.dest tab);
   161 
   162 
   163 (* empty, extend, merge ruletabs *)
   164 
   165 val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r));
   166 val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
   167 fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
   168 
   169 
   170 
   171 (** datatype syntax **)
   172 
   173 datatype syntax =
   174   Syntax of {
   175     input: SynExt.xprod list,
   176     lexicon: Scan.lexicon,
   177     gram: Parser.gram,
   178     consts: string list,
   179     prmodes: string list,
   180     parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   181     parse_ruletab: ruletab,
   182     parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
   183     print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
   184     print_ruletab: ruletab,
   185     print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   186     tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list,
   187     prtabs: Printer.prtabs} * stamp;
   188 
   189 fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
   190 
   191 fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
   192 
   193 type mode = string * bool;
   194 val default_mode = ("", true);
   195 val input_mode = ("input", true);
   196 val internalM = "internal";
   197 
   198 
   199 (* empty_syntax *)
   200 
   201 val empty_syntax = Syntax
   202   ({input = [],
   203     lexicon = Scan.empty_lexicon,
   204     gram = Parser.empty_gram,
   205     consts = [],
   206     prmodes = [],
   207     parse_ast_trtab = Symtab.empty,
   208     parse_ruletab = Symtab.empty,
   209     parse_trtab = Symtab.empty,
   210     print_trtab = Symtab.empty,
   211     print_ruletab = Symtab.empty,
   212     print_ast_trtab = Symtab.empty,
   213     tokentrtab = [],
   214     prtabs = Printer.empty_prtabs}, stamp ());
   215 
   216 
   217 (* extend_syntax *)
   218 
   219 fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   220   let
   221     val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
   222       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   223       print_ast_trtab, tokentrtab, prtabs} = tabs;
   224     val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
   225       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   226       print_ast_translation, token_translation} = syn_ext;
   227     fun if_inout xs = if inout then xs else [];
   228   in
   229     Syntax
   230     ({input = if_inout xprods @ input,
   231       lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon,
   232       gram = Parser.extend_gram gram (if_inout xprods),
   233       consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2),
   234       prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
   235       parse_ast_trtab =
   236         extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
   237       parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab,
   238       parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab,
   239       print_trtab = extend_tr'tab print_translation print_trtab,
   240       print_ruletab = extend_ruletab print_rules print_ruletab,
   241       print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
   242       tokentrtab = extend_tokentrtab token_translation tokentrtab,
   243       prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ())
   244   end;
   245 
   246 
   247 (* remove_syntax *)
   248 
   249 fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   250   let
   251     val SynExt.SynExt {xprods, consts = _, prmodes = _,
   252       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   253       print_ast_translation, token_translation = _} = syn_ext;
   254     val {input, lexicon, gram, consts, prmodes,
   255       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   256       print_ast_trtab, tokentrtab, prtabs} = tabs;
   257     val input' = if inout then subtract (op =) xprods input else input;
   258     fun if_inout xs = if inout then xs else [];
   259   in
   260     Syntax
   261     ({input = input',
   262       lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
   263       gram = if inout then Parser.make_gram input' else gram,
   264       consts = consts,
   265       prmodes = prmodes,
   266       parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
   267       parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
   268       parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
   269       print_trtab = remove_tr'tab print_translation print_trtab,
   270       print_ruletab = remove_ruletab print_rules print_ruletab,
   271       print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   272       tokentrtab = tokentrtab,
   273       prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
   274   end;
   275 
   276 
   277 (* merge_syntaxes *)
   278 
   279 fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
   280   let
   281     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   282       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
   283       parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
   284       print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   285       print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   286 
   287     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   288       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
   289       parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
   290       print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   291       print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   292   in
   293     Syntax
   294     ({input = Library.merge (op =) (input1, input2),
   295       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   296       gram = Parser.merge_grams gram1 gram2,
   297       consts = sort_distinct string_ord (consts1 @ consts2),
   298       prmodes = Library.merge (op =) (prmodes1, prmodes2),
   299       parse_ast_trtab =
   300         merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   301       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   302       parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   303       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   304       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   305       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   306       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   307       prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
   308   end;
   309 
   310 
   311 (* basic syntax *)
   312 
   313 val basic_syn =
   314   empty_syntax
   315   |> extend_syntax default_mode TypeExt.type_ext
   316   |> extend_syntax default_mode SynExt.pure_ext;
   317 
   318 val basic_nonterms =
   319   (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
   320     SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
   321     "asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]);
   322 
   323 val appl_syntax =
   324  [("_appl", "[('b => 'a), args] => logic", Mixfix.Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
   325   ("_appl", "[('b => 'a), args] => aprop", Mixfix.Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
   326 
   327 val applC_syntax =
   328  [("",       "'a => cargs",                  Mixfix.Delimfix "_"),
   329   ("_cargs", "['a, cargs] => cargs",         Mixfix.Mixfix ("_/ _", [1000, 1000], 1000)),
   330   ("_applC", "[('b => 'a), cargs] => logic", Mixfix.Mixfix ("(1_/ _)", [1000, 1000], 999)),
   331   ("_applC", "[('b => 'a), cargs] => aprop", Mixfix.Mixfix ("(1_/ _)", [1000, 1000], 999))];
   332 
   333 
   334 
   335 (** print syntax **)
   336 
   337 local
   338 
   339 fun pretty_strs_qs name strs =
   340   Pretty.strs (name :: map Library.quote (sort_strings strs));
   341 
   342 fun pretty_gram (Syntax (tabs, _)) =
   343   let
   344     val {lexicon, prmodes, gram, prtabs, ...} = tabs;
   345     val prmodes' = sort_strings (filter_out (equal "") prmodes);
   346   in
   347     [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
   348       Pretty.big_list "prods:" (Parser.pretty_gram gram),
   349       pretty_strs_qs "print modes:" prmodes']
   350   end;
   351 
   352 fun pretty_trans (Syntax (tabs, _)) =
   353   let
   354     fun pretty_trtab name tab =
   355       pretty_strs_qs name (Symtab.keys tab);
   356 
   357     fun pretty_ruletab name tab =
   358       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   359 
   360     fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
   361 
   362     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   363       print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
   364   in
   365     [pretty_strs_qs "consts:" consts,
   366       pretty_trtab "parse_ast_translation:" parse_ast_trtab,
   367       pretty_ruletab "parse_rules:" parse_ruletab,
   368       pretty_trtab "parse_translation:" parse_trtab,
   369       pretty_trtab "print_translation:" print_trtab,
   370       pretty_ruletab "print_rules:" print_ruletab,
   371       pretty_trtab "print_ast_translation:" print_ast_trtab,
   372       Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
   373   end;
   374 
   375 in
   376 
   377 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
   378 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
   379 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
   380 
   381 end;
   382 
   383 
   384 
   385 (** read **)
   386 
   387 (* read_ast *)
   388 
   389 val ambiguity_level = ref 1;
   390 val ambiguity_is_error = ref false
   391 
   392 fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str =
   393   let
   394     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   395     val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
   396     val chars = Symbol.explode str;
   397     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   398 
   399     fun show_pt pt =
   400       Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
   401   in
   402     if length pts > ! ambiguity_level then
   403       if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str)
   404       else (warning ("Ambiguous input " ^ quote str ^ "\n" ^
   405           "produces " ^ string_of_int (length pts) ^ " parse trees.");
   406          List.app (warning o show_pt) pts)
   407     else ();
   408     SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
   409   end;
   410 
   411 
   412 (* read *)
   413 
   414 fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str =
   415   let
   416     val {parse_ruletab, parse_trtab, ...} = tabs;
   417     val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str;
   418   in
   419     SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
   420       (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
   421   end;
   422 
   423 
   424 (* read terms *)
   425 
   426 fun read_term get_sort map_const map_free map_type map_sort ctxt is_logtype syn ty str =
   427   map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
   428     (read ctxt is_logtype syn ty str);
   429 
   430 
   431 (* read types *)
   432 
   433 fun read_typ ctxt syn get_sort map_sort str =
   434   (case read ctxt (K false) syn SynExt.typeT str of
   435     [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
   436   | _ => error "read_typ: ambiguous syntax");
   437 
   438 
   439 (* read sorts *)
   440 
   441 fun read_sort ctxt syn map_sort str =
   442   (case read ctxt (K false) syn TypeExt.sortT str of
   443     [t] => TypeExt.sort_of_term map_sort t
   444   | _ => error "read_sort: ambiguous syntax");
   445 
   446 
   447 
   448 (** prepare translation rules **)
   449 
   450 datatype 'a trrule =
   451   ParseRule of 'a * 'a |
   452   PrintRule of 'a * 'a |
   453   ParsePrintRule of 'a * 'a;
   454 
   455 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   456   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   457   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   458 
   459 fun parse_rule (ParseRule pats) = SOME pats
   460   | parse_rule (PrintRule _) = NONE
   461   | parse_rule (ParsePrintRule pats) = SOME pats;
   462 
   463 fun print_rule (ParseRule _) = NONE
   464   | print_rule (PrintRule pats) = SOME (swap pats)
   465   | print_rule (ParsePrintRule pats) = SOME (swap pats);
   466 
   467 
   468 local
   469 
   470 fun check_rule (rule as (lhs, rhs)) =
   471   (case Ast.rule_error rule of
   472     SOME msg =>
   473       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   474         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   475   | NONE => rule);
   476 
   477 
   478 fun read_pattern ctxt is_logtype syn (root, str) =
   479   let
   480     val Syntax ({consts, ...}, _) = syn;
   481 
   482     fun constify (ast as Ast.Constant _) = ast
   483       | constify (ast as Ast.Variable x) =
   484           if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x
   485           else ast
   486       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   487   in
   488     (case read_asts ctxt is_logtype syn true root str of
   489       [ast] => constify ast
   490     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   491   end handle ERROR msg =>
   492     cat_error msg ("The error(s) above occurred in translation pattern " ^
   493       quote str);
   494 
   495 fun prep_rules rd_pat raw_rules =
   496   let val rules = map (map_trrule rd_pat) raw_rules in
   497     (map check_rule (map_filter parse_rule rules),
   498       map check_rule (map_filter print_rule rules))
   499   end
   500 
   501 in
   502 
   503 val cert_rules = prep_rules I;
   504 
   505 fun read_rules ctxt is_logtype syn =
   506   prep_rules (read_pattern ctxt is_logtype syn);
   507 
   508 end;
   509 
   510 
   511 
   512 (** pretty terms, typs, sorts **)
   513 
   514 fun pretty_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
   515   let
   516     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   517     val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
   518   in
   519     Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
   520       (lookup_tokentr tokentrtab (! print_mode))
   521       (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
   522   end;
   523 
   524 fun pretty_term extern =
   525   pretty_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
   526 fun pretty_typ ctxt syn =
   527   pretty_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
   528 fun pretty_sort ctxt syn =
   529   pretty_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
   530 
   531 
   532 
   533 (** modify syntax **)
   534 
   535 fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
   536 fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
   537 
   538 val extend_type_gram       = ext_syntax Mixfix.syn_ext_types;
   539 val extend_const_gram      = ext_syntax' Mixfix.syn_ext_consts;
   540 val extend_consts          = ext_syntax SynExt.syn_ext_const_names;
   541 val extend_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
   542 val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
   543 val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
   544 
   545 fun remove_const_gram is_logtype prmode decls =
   546   remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
   547 
   548 fun extend_trrules ctxt is_logtype syn =
   549   ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   550 
   551 fun remove_trrules ctxt is_logtype syn =
   552   remove_syntax default_mode o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   553 
   554 val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
   555 val remove_trrules_i = remove_syntax default_mode o SynExt.syn_ext_rules o cert_rules;
   556 
   557 
   558 (*export parts of internal Syntax structures*)
   559 open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   560 
   561 end;
   562 
   563 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   564 open BasicSyntax;
   565 
   566 structure Hidden = struct end;
   567 structure Lexicon = Hidden;
   568 structure Ast = Hidden;
   569 structure SynExt = Hidden;
   570 structure Parser = Hidden;
   571 structure TypeExt = Hidden;
   572 structure SynTrans = Hidden;
   573 structure Mixfix = Hidden;
   574 structure Printer = Hidden;