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