src/Pure/Syntax/syntax.ML
author wenzelm
Mon Jan 16 21:50:15 2012 +0100 (2012-01-16)
changeset 46236 ae79f2978a67
parent 46126 bab00660539d
child 46483 10a9c31a22b4
permissions -rw-r--r--
position constraints for numerals enable PIDE markup;
     1 (*  Title:      Pure/Syntax/syntax.ML
     2     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     3 
     4 Standard Isabelle syntax, based on arbitrary context-free grammars
     5 (specified by mixfix declarations).
     6 *)
     7 
     8 signature SYNTAX =
     9 sig
    10   type operations
    11   val install_operations: operations -> unit
    12   val root: string Config.T
    13   val ambiguity_enabled: bool Config.T
    14   val ambiguity_level_raw: Config.raw
    15   val ambiguity_level: int Config.T
    16   val ambiguity_limit: int Config.T
    17   val ambiguity_warnings_raw: Config.raw
    18   val ambiguity_warnings: bool Config.T
    19   val read_token: string -> Symbol_Pos.T list * Position.T
    20   val parse_token: Proof.context -> (XML.tree list -> 'a) ->
    21     Markup.T -> (Symbol_Pos.T list * Position.T -> 'a) -> string -> 'a
    22   val parse_sort: Proof.context -> string -> sort
    23   val parse_typ: Proof.context -> string -> typ
    24   val parse_term: Proof.context -> string -> term
    25   val parse_prop: Proof.context -> string -> term
    26   val unparse_sort: Proof.context -> sort -> Pretty.T
    27   val unparse_classrel: Proof.context -> class list -> Pretty.T
    28   val unparse_arity: Proof.context -> arity -> Pretty.T
    29   val unparse_typ: Proof.context -> typ -> Pretty.T
    30   val unparse_term: Proof.context -> term -> Pretty.T
    31   val check_sort: Proof.context -> sort -> sort
    32   val check_typ: Proof.context -> typ -> typ
    33   val check_term: Proof.context -> term -> term
    34   val check_prop: Proof.context -> term -> term
    35   val check_typs: Proof.context -> typ list -> typ list
    36   val check_terms: Proof.context -> term list -> term list
    37   val check_props: Proof.context -> term list -> term list
    38   val uncheck_sort: Proof.context -> sort -> sort
    39   val uncheck_arity: Proof.context -> arity -> arity
    40   val uncheck_classrel: Proof.context -> class list -> class list
    41   val uncheck_typs: Proof.context -> typ list -> typ list
    42   val uncheck_terms: Proof.context -> term list -> term list
    43   val read_sort: Proof.context -> string -> sort
    44   val read_typ: Proof.context -> string -> typ
    45   val read_term: Proof.context -> string -> term
    46   val read_prop: Proof.context -> string -> term
    47   val read_terms: Proof.context -> string list -> term list
    48   val read_props: Proof.context -> string list -> term list
    49   val read_sort_global: theory -> string -> sort
    50   val read_typ_global: theory -> string -> typ
    51   val read_term_global: theory -> string -> term
    52   val read_prop_global: theory -> string -> term
    53   val pretty_term: Proof.context -> term -> Pretty.T
    54   val pretty_typ: Proof.context -> typ -> Pretty.T
    55   val pretty_sort: Proof.context -> sort -> Pretty.T
    56   val pretty_classrel: Proof.context -> class list -> Pretty.T
    57   val pretty_arity: Proof.context -> arity -> Pretty.T
    58   val string_of_term: Proof.context -> term -> string
    59   val string_of_typ: Proof.context -> typ -> string
    60   val string_of_sort: Proof.context -> sort -> string
    61   val string_of_classrel: Proof.context -> class list -> string
    62   val string_of_arity: Proof.context -> arity -> string
    63   val is_pretty_global: Proof.context -> bool
    64   val set_pretty_global: bool -> Proof.context -> Proof.context
    65   val init_pretty_global: theory -> Proof.context
    66   val init_pretty: Context.pretty -> Proof.context
    67   val pretty_term_global: theory -> term -> Pretty.T
    68   val pretty_typ_global: theory -> typ -> Pretty.T
    69   val pretty_sort_global: theory -> sort -> Pretty.T
    70   val string_of_term_global: theory -> term -> string
    71   val string_of_typ_global: theory -> typ -> string
    72   val string_of_sort_global: theory -> sort -> string
    73   type syntax
    74   val eq_syntax: syntax * syntax -> bool
    75   val force_syntax: syntax -> unit
    76   val lookup_const: syntax -> string -> string option
    77   val is_keyword: syntax -> string -> bool
    78   val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
    79   val parse: syntax -> string -> Lexicon.token list -> Parser.parsetree list
    80   val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
    81   val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list
    82   val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
    83   val print_translation: syntax -> string ->
    84     Proof.context -> typ -> term list -> term  (*exception Match*)
    85   val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
    86   val print_ast_translation: syntax -> string ->
    87     Proof.context -> Ast.ast list -> Ast.ast  (*exception Match*)
    88   val prtabs: syntax -> Printer.prtabs
    89   type mode
    90   val mode_default: mode
    91   val mode_input: mode
    92   val empty_syntax: syntax
    93   val merge_syntax: syntax * syntax -> syntax
    94   val token_markers: string list
    95   val basic_nonterms: string list
    96   val print_gram: syntax -> unit
    97   val print_trans: syntax -> unit
    98   val print_syntax: syntax -> unit
    99   val guess_infix: syntax -> string -> mixfix option
   100   datatype 'a trrule =
   101     Parse_Rule of 'a * 'a |
   102     Print_Rule of 'a * 'a |
   103     Parse_Print_Rule of 'a * 'a
   104   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
   105   val update_trfuns:
   106     (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
   107     (string * ((term list -> term) * stamp)) list *
   108     (string * ((typ -> term list -> term) * stamp)) list *
   109     (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
   110   val update_advanced_trfuns:
   111     (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
   112     (string * ((Proof.context -> term list -> term) * stamp)) list *
   113     (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
   114     (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
   115   val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
   116   val update_const_gram: bool -> (string -> bool) ->
   117     mode -> (string * typ * mixfix) list -> syntax -> syntax
   118   val update_trrules: Ast.ast trrule list -> syntax -> syntax
   119   val remove_trrules: Ast.ast trrule list -> syntax -> syntax
   120   val const: string -> term
   121   val free: string -> term
   122   val var: indexname -> term
   123 end;
   124 
   125 structure Syntax: SYNTAX =
   126 struct
   127 
   128 
   129 (** inner syntax operations **)
   130 
   131 (* back-patched operations *)
   132 
   133 type operations =
   134  {parse_sort: Proof.context -> string -> sort,
   135   parse_typ: Proof.context -> string -> typ,
   136   parse_term: Proof.context -> string -> term,
   137   parse_prop: Proof.context -> string -> term,
   138   unparse_sort: Proof.context -> sort -> Pretty.T,
   139   unparse_typ: Proof.context -> typ -> Pretty.T,
   140   unparse_term: Proof.context -> term -> Pretty.T,
   141   check_typs: Proof.context -> typ list -> typ list,
   142   check_terms: Proof.context -> term list -> term list,
   143   check_props: Proof.context -> term list -> term list,
   144   uncheck_typs: Proof.context -> typ list -> typ list,
   145   uncheck_terms: Proof.context -> term list -> term list};
   146 
   147 val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations";
   148 fun install_operations ops = Single_Assignment.assign operations ops;
   149 
   150 fun operation which ctxt x =
   151   (case Single_Assignment.peek operations of
   152     NONE => raise Fail "Inner syntax operations not installed"
   153   | SOME ops => which ops ctxt x);
   154 
   155 
   156 (* configuration options *)
   157 
   158 val root = Config.string (Config.declare "syntax_root" (K (Config.String "any")));
   159 
   160 val ambiguity_enabled =
   161   Config.bool (Config.declare "syntax_ambiguity_enabled" (fn _ => Config.Bool true));
   162 
   163 val ambiguity_level_raw = Config.declare "syntax_ambiguity_level" (fn _ => Config.Int 1);
   164 val ambiguity_level = Config.int ambiguity_level_raw;
   165 
   166 val ambiguity_limit =
   167   Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
   168 
   169 val ambiguity_warnings_raw = 
   170   Config.declare "syntax_ambiguity_warnings" (fn _ => Config.Bool true);
   171 val ambiguity_warnings =
   172   Config.bool ambiguity_warnings_raw;
   173 
   174 
   175 (* outer syntax token -- with optional YXML content *)
   176 
   177 fun explode_token tree =
   178   let
   179     val text = XML.content_of [tree];
   180     val pos =
   181       (case tree of
   182         XML.Elem ((name, props), _) =>
   183           if name = Isabelle_Markup.tokenN then Position.of_properties props
   184           else Position.none
   185       | XML.Text _ => Position.none);
   186   in (Symbol_Pos.explode (text, pos), pos) end;
   187 
   188 fun read_token str = explode_token (YXML.parse str handle Fail msg => error msg);
   189 
   190 fun parse_token ctxt decode markup parse str =
   191   let
   192     fun parse_tree tree =
   193       let
   194         val (syms, pos) = explode_token tree;
   195         val _ = Context_Position.report ctxt pos markup;
   196       in parse (syms, pos) end;
   197   in
   198     (case YXML.parse_body str handle Fail msg => error msg of
   199       body as [tree as XML.Elem ((name, _), _)] =>
   200         if name = Isabelle_Markup.tokenN then parse_tree tree else decode body
   201     | [tree as XML.Text _] => parse_tree tree
   202     | body => decode body)
   203   end;
   204 
   205 
   206 (* (un)parsing *)
   207 
   208 val parse_sort = operation #parse_sort;
   209 val parse_typ = operation #parse_typ;
   210 val parse_term = operation #parse_term;
   211 val parse_prop = operation #parse_prop;
   212 val unparse_sort = operation #unparse_sort;
   213 val unparse_typ = operation #unparse_typ;
   214 val unparse_term = operation #unparse_term;
   215 
   216 
   217 (* (un)checking *)
   218 
   219 val check_typs = operation #check_typs;
   220 val check_terms = operation #check_terms;
   221 val check_props = operation #check_props;
   222 
   223 val check_typ = singleton o check_typs;
   224 val check_term = singleton o check_terms;
   225 val check_prop = singleton o check_props;
   226 
   227 val uncheck_typs = operation #uncheck_typs;
   228 val uncheck_terms = operation #uncheck_terms;
   229 
   230 
   231 (* derived operations for algebra of sorts *)
   232 
   233 local
   234 
   235 fun map_sort f S =
   236   (case f (TFree ("", S)) of
   237     TFree ("", S') => S'
   238   | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
   239 
   240 in
   241 
   242 val check_sort = map_sort o check_typ;
   243 val uncheck_sort = map_sort o singleton o uncheck_typs;
   244 
   245 end;
   246 
   247 
   248 val uncheck_classrel = map o singleton o uncheck_sort;
   249 
   250 fun unparse_classrel ctxt cs = Pretty.block (flat
   251   (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
   252 
   253 fun uncheck_arity ctxt (a, Ss, S) =
   254   let
   255     val T = Type (a, replicate (length Ss) dummyT);
   256     val a' =
   257       (case singleton (uncheck_typs ctxt) T of
   258         Type (a', _) => a'
   259       | T => raise TYPE ("uncheck_arity", [T], []));
   260     val Ss' = map (uncheck_sort ctxt) Ss;
   261     val S' = uncheck_sort ctxt S;
   262   in (a', Ss', S') end;
   263 
   264 fun unparse_arity ctxt (a, Ss, S) =
   265   let
   266     val prtT = unparse_typ ctxt (Type (a, []));
   267     val dom =
   268       if null Ss then []
   269       else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
   270   in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
   271 
   272 
   273 (* read = parse + check *)
   274 
   275 fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
   276 fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
   277 
   278 fun read_terms ctxt = Par_List.map_name "Syntax.read_terms" (parse_term ctxt) #> check_terms ctxt;
   279 fun read_props ctxt = Par_List.map_name "Syntax.read_props" (parse_prop ctxt) #> check_props ctxt;
   280 
   281 val read_term = singleton o read_terms;
   282 val read_prop = singleton o read_props;
   283 
   284 val read_sort_global = read_sort o Proof_Context.init_global;
   285 val read_typ_global = read_typ o Proof_Context.init_global;
   286 val read_term_global = read_term o Proof_Context.init_global;
   287 val read_prop_global = read_prop o Proof_Context.init_global;
   288 
   289 
   290 (* pretty = uncheck + unparse *)
   291 
   292 fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
   293 fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
   294 fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
   295 fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
   296 fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
   297 
   298 val string_of_term = Pretty.string_of oo pretty_term;
   299 val string_of_typ = Pretty.string_of oo pretty_typ;
   300 val string_of_sort = Pretty.string_of oo pretty_sort;
   301 val string_of_classrel = Pretty.string_of oo pretty_classrel;
   302 val string_of_arity = Pretty.string_of oo pretty_arity;
   303 
   304 
   305 (* global pretty printing *)
   306 
   307 val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false)));
   308 fun is_pretty_global ctxt = Config.get ctxt pretty_global;
   309 val set_pretty_global = Config.put pretty_global;
   310 val init_pretty_global = set_pretty_global true o Proof_Context.init_global;
   311 val init_pretty = Context.pretty_context init_pretty_global;
   312 
   313 val pretty_term_global = pretty_term o init_pretty_global;
   314 val pretty_typ_global = pretty_typ o init_pretty_global;
   315 val pretty_sort_global = pretty_sort o init_pretty_global;
   316 
   317 val string_of_term_global = string_of_term o init_pretty_global;
   318 val string_of_typ_global = string_of_typ o init_pretty_global;
   319 val string_of_sort_global = string_of_sort o init_pretty_global;
   320 
   321 
   322 
   323 (** tables of translation functions **)
   324 
   325 (* parse (ast) translations *)
   326 
   327 fun err_dup_trfun name c =
   328   error ("More than one " ^ name ^ " for " ^ quote c);
   329 
   330 fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
   331 
   332 fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns;
   333 
   334 fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
   335   handle Symtab.DUP c => err_dup_trfun name c;
   336 
   337 fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2)
   338   handle Symtab.DUP c => err_dup_trfun name c;
   339 
   340 
   341 (* print (ast) translations *)
   342 
   343 fun apply_tr' tab c ctxt T args =
   344   let
   345     val fns = map fst (Symtab.lookup_list tab c);
   346     fun app_first [] = raise Match
   347       | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
   348   in app_first fns end;
   349 
   350 fun apply_ast_tr' tab c ctxt args =
   351   let
   352     val fns = map fst (Symtab.lookup_list tab c);
   353     fun app_first [] = raise Match
   354       | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
   355   in app_first fns end;
   356 
   357 fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns;
   358 fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns;
   359 fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2);
   360 
   361 
   362 
   363 (** tables of translation rules **)
   364 
   365 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   366 
   367 fun dest_ruletab tab = maps snd (Symtab.dest tab);
   368 
   369 val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
   370 val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
   371 fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
   372 
   373 
   374 
   375 (** datatype syntax **)
   376 
   377 datatype syntax =
   378   Syntax of {
   379     input: Syntax_Ext.xprod list,
   380     lexicon: Scan.lexicon,
   381     gram: Parser.gram lazy,
   382     consts: string Symtab.table,
   383     prmodes: string list,
   384     parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   385     parse_ruletab: ruletab,
   386     parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
   387     print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
   388     print_ruletab: ruletab,
   389     print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   390     prtabs: Printer.prtabs} * stamp;
   391 
   392 fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
   393 
   394 fun force_syntax (Syntax ({gram, ...}, _)) = ignore (Lazy.force gram);
   395 
   396 fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
   397 fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
   398 fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
   399 fun parse (Syntax ({gram, ...}, _)) = Parser.parse (Lazy.force gram);
   400 
   401 fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
   402 fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
   403 fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
   404 fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
   405 fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
   406 fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
   407 
   408 fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
   409 
   410 type mode = string * bool;
   411 val mode_default = ("", true);
   412 val mode_input = (Print_Mode.input, true);
   413 
   414 
   415 (* empty_syntax *)
   416 
   417 val empty_syntax = Syntax
   418   ({input = [],
   419     lexicon = Scan.empty_lexicon,
   420     gram = Lazy.value Parser.empty_gram,
   421     consts = Symtab.empty,
   422     prmodes = [],
   423     parse_ast_trtab = Symtab.empty,
   424     parse_ruletab = Symtab.empty,
   425     parse_trtab = Symtab.empty,
   426     print_trtab = Symtab.empty,
   427     print_ruletab = Symtab.empty,
   428     print_ast_trtab = Symtab.empty,
   429     prtabs = Printer.empty_prtabs}, stamp ());
   430 
   431 
   432 (* update_syntax *)
   433 
   434 fun update_const (c, b) tab =
   435   if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c))
   436   then tab
   437   else Symtab.update (c, b) tab;
   438 
   439 fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   440   let
   441     val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
   442       parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
   443     val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
   444       parse_rules, parse_translation, print_translation, print_rules,
   445       print_ast_translation} = syn_ext;
   446     val new_xprods =
   447       if inout then distinct (op =) (filter_out (member (op =) input) xprods) else [];
   448     fun if_inout xs = if inout then xs else [];
   449   in
   450     Syntax
   451     ({input = new_xprods @ input,
   452       lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon,
   453       gram = Lazy.value (Parser.extend_gram new_xprods (Lazy.force gram)),
   454       consts = fold update_const consts2 consts1,
   455       prmodes = insert (op =) mode prmodes,
   456       parse_ast_trtab =
   457         update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
   458       parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
   459       parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
   460       print_trtab = update_tr'tab print_translation print_trtab,
   461       print_ruletab = update_ruletab print_rules print_ruletab,
   462       print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
   463       prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
   464   end;
   465 
   466 
   467 (* remove_syntax *)
   468 
   469 fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   470   let
   471     val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
   472       parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext;
   473     val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
   474       parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
   475     val input' = if inout then subtract (op =) xprods input else input;
   476     val changed = length input <> length input';
   477     fun if_inout xs = if inout then xs else [];
   478   in
   479     Syntax
   480     ({input = input',
   481       lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon,
   482       gram = if changed then Lazy.value (Parser.make_gram input') else gram,
   483       consts = consts,
   484       prmodes = prmodes,
   485       parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
   486       parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
   487       parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
   488       print_trtab = remove_tr'tab print_translation print_trtab,
   489       print_ruletab = remove_ruletab print_rules print_ruletab,
   490       print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   491       prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
   492   end;
   493 
   494 
   495 (* merge_syntax *)
   496 
   497 fun merge_syntax (Syntax (tabs1, _), Syntax (tabs2, _)) =
   498   let
   499     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   500       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   501       parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   502       print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1;
   503 
   504     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   505       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   506       parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   507       print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2;
   508 
   509     val (input', gram') =
   510       (case subtract (op =) input1 input2 of
   511         [] => (input1, gram1)
   512       | new_xprods2 =>
   513           if subset (op =) (input1, input2) then (input2, gram2)
   514           else
   515             let
   516               val input' = new_xprods2 @ input1;
   517               val gram' = Lazy.lazy (fn () => Parser.make_gram input');
   518             in (input', gram') end);
   519   in
   520     Syntax
   521     ({input = input',
   522       lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
   523       gram = gram',
   524       consts = Symtab.merge (K true) (consts1, consts2),
   525       prmodes = Library.merge (op =) (prmodes1, prmodes2),
   526       parse_ast_trtab =
   527         merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   528       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   529       parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   530       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   531       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   532       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   533       prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
   534   end;
   535 
   536 
   537 (* basic syntax *)
   538 
   539 val token_markers =
   540   ["_tfree", "_tvar", "_free", "_bound", "_loose", "_var", "_numeral", "_inner_string"];
   541 
   542 val basic_nonterms =
   543   (Lexicon.terminals @ ["logic", "type", "types", "sort", "classes",
   544     "args", "cargs", "pttrn", "pttrns", "idt", "idts", "aprop", "asms",
   545     "any", "prop'", "num_const", "float_const", "xnum_const", "num_position",
   546     "float_position", "xnum_position", "index", "struct", "id_position",
   547     "longid_position", "xstr_position", "type_name", "class_name"]);
   548 
   549 
   550 
   551 (** print syntax **)
   552 
   553 local
   554 
   555 fun pretty_strs_qs name strs =
   556   Pretty.strs (name :: map quote (sort_strings strs));
   557 
   558 fun pretty_gram (Syntax (tabs, _)) =
   559   let
   560     val {lexicon, prmodes, gram, ...} = tabs;
   561     val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
   562   in
   563     [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
   564       Pretty.big_list "prods:" (Parser.pretty_gram (Lazy.force gram)),
   565       pretty_strs_qs "print modes:" prmodes']
   566   end;
   567 
   568 fun pretty_trans (Syntax (tabs, _)) =
   569   let
   570     fun pretty_tab name tab =
   571       pretty_strs_qs name (sort_strings (Symtab.keys tab));
   572 
   573     fun pretty_ruletab name tab =
   574       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   575 
   576     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   577       print_ruletab, print_ast_trtab, ...} = tabs;
   578   in
   579    [pretty_tab "consts:" consts,
   580     pretty_tab "parse_ast_translation:" parse_ast_trtab,
   581     pretty_ruletab "parse_rules:" parse_ruletab,
   582     pretty_tab "parse_translation:" parse_trtab,
   583     pretty_tab "print_translation:" print_trtab,
   584     pretty_ruletab "print_rules:" print_ruletab,
   585     pretty_tab "print_ast_translation:" print_ast_trtab]
   586   end;
   587 
   588 in
   589 
   590 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
   591 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
   592 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
   593 
   594 end;
   595 
   596 
   597 (* reconstructing infixes -- educated guessing *)
   598 
   599 fun guess_infix (Syntax ({gram, ...}, _)) c =
   600   (case Parser.guess_infix_lr (Lazy.force gram) c of
   601     SOME (s, l, r, j) => SOME
   602      (if l then Mixfix.Infixl (s, j)
   603       else if r then Mixfix.Infixr (s, j)
   604       else Mixfix.Infix (s, j))
   605   | NONE => NONE);
   606 
   607 
   608 
   609 (** prepare translation rules **)
   610 
   611 (* rules *)
   612 
   613 datatype 'a trrule =
   614   Parse_Rule of 'a * 'a |
   615   Print_Rule of 'a * 'a |
   616   Parse_Print_Rule of 'a * 'a;
   617 
   618 fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
   619   | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
   620   | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y);
   621 
   622 fun parse_rule (Parse_Rule pats) = SOME pats
   623   | parse_rule (Print_Rule _) = NONE
   624   | parse_rule (Parse_Print_Rule pats) = SOME pats;
   625 
   626 fun print_rule (Parse_Rule _) = NONE
   627   | print_rule (Print_Rule pats) = SOME (swap pats)
   628   | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
   629 
   630 
   631 (* check_rules *)
   632 
   633 local
   634 
   635 fun check_rule rule =
   636   (case Ast.rule_error rule of
   637     SOME msg =>
   638       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   639         Pretty.string_of (Ast.pretty_rule rule))
   640   | NONE => rule);
   641 
   642 in
   643 
   644 fun check_rules rules =
   645  (map check_rule (map_filter parse_rule rules),
   646   map check_rule (map_filter print_rule rules));
   647 
   648 end;
   649 
   650 
   651 
   652 (** modify syntax **)
   653 
   654 fun ext_syntax f decls = update_syntax mode_default (f decls);
   655 
   656 val update_trfuns = ext_syntax Syntax_Ext.syn_ext_trfuns;
   657 val update_advanced_trfuns = ext_syntax Syntax_Ext.syn_ext_advanced_trfuns;
   658 
   659 fun update_type_gram add prmode decls =
   660   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
   661 
   662 fun update_const_gram add is_logtype prmode decls =
   663   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
   664 
   665 val update_trrules = ext_syntax Syntax_Ext.syn_ext_rules o check_rules;
   666 val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules;
   667 
   668 
   669 open Lexicon.Syntax;
   670 
   671 end;
   672