src/Pure/Syntax/syntax.ML
author wenzelm
Tue Sep 25 13:28:42 2007 +0200 (2007-09-25)
changeset 24709 ecfb9dcb6c4c
parent 24680 0d355aa59e67
child 24768 123e219b66c2
permissions -rw-r--r--
removed redundant global_parse operations;
renamed global_XXX to XXX_global;
     1 (*  Title:      Pure/Syntax/syntax.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Standard Isabelle syntax, based on arbitrary context-free grammars
     6 (specified by mixfix declarations).
     7 *)
     8 
     9 signature BASIC_SYNTAX =
    10 sig
    11   include AST0
    12   include SYN_TRANS0
    13   include MIXFIX0
    14   include PRINTER0
    15 end;
    16 
    17 signature SYNTAX =
    18 sig
    19   include AST1
    20   include LEXICON0
    21   include SYN_EXT0
    22   include TYPE_EXT0
    23   include SYN_TRANS1
    24   include MIXFIX1
    25   include PRINTER0
    26   val extend_trtab: string -> (string * ('a * stamp)) list ->
    27     ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
    28   val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
    29     ('a * stamp) Symtab.table
    30   val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    31     -> ('a * stamp) list Symtab.table
    32   val extend_tr'tab: (string * ('a * stamp)) list ->
    33     ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    34   datatype 'a trrule =
    35     ParseRule of 'a * 'a |
    36     PrintRule of 'a * 'a |
    37     ParsePrintRule of 'a * 'a
    38   type syntax
    39   val eq_syntax: syntax * syntax -> bool
    40   val is_keyword: syntax -> string -> bool
    41   type mode
    42   val default_mode: mode
    43   val input_mode: mode
    44   val internalM: string
    45   val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
    46   val extend_const_gram: (string -> bool) ->
    47     mode -> (string * typ * mixfix) list -> syntax -> syntax
    48   val extend_consts: string list -> syntax -> syntax
    49   val extend_trfuns:
    50     (string * ((ast list -> ast) * stamp)) list *
    51     (string * ((term list -> term) * stamp)) list *
    52     (string * ((bool -> typ -> term list -> term) * stamp)) list *
    53     (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
    54   val extend_advanced_trfuns:
    55     (string * ((Proof.context -> ast list -> ast) * stamp)) list *
    56     (string * ((Proof.context -> term list -> term) * stamp)) list *
    57     (string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list *
    58     (string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax
    59   val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax
    60   val remove_const_gram: (string -> bool) ->
    61     mode -> (string * typ * mixfix) list -> syntax -> syntax
    62   val extend_trrules: Proof.context -> (string -> bool) -> syntax ->
    63     (string * string) trrule list -> syntax -> syntax
    64   val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
    65     (string * string) trrule list -> syntax -> syntax
    66   val extend_trrules_i: ast trrule list -> syntax -> syntax
    67   val remove_trrules_i: ast trrule list -> syntax -> syntax
    68   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    69   val merge_syntaxes: syntax -> syntax -> syntax
    70   val basic_syn: syntax
    71   val basic_nonterms: string list
    72   val print_gram: syntax -> unit
    73   val print_trans: syntax -> unit
    74   val print_syntax: syntax -> unit
    75   val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list
    76   val standard_parse_term: Pretty.pp -> (term -> term Exn.result) ->
    77     (((string * int) * sort) list -> string * int -> Term.sort) ->
    78     (string -> string option) -> (string -> string option) ->
    79     (typ -> typ) -> (sort -> sort) -> Proof.context ->
    80     (string -> bool) -> syntax -> typ -> string -> term
    81   val standard_parse_typ: Proof.context -> syntax -> ((indexname * sort) list -> indexname -> sort) ->
    82     (sort -> sort) -> string -> typ
    83   val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort
    84   val pretty_term: (string -> xstring) -> Proof.context -> syntax -> bool -> term -> Pretty.T
    85   val pretty_typ: Proof.context -> syntax -> typ -> Pretty.T
    86   val pretty_sort: Proof.context -> syntax -> sort -> Pretty.T
    87   val ambiguity_level: int ref
    88   val ambiguity_is_error: bool ref
    89   val install_parsers:
    90    {sort: Proof.context -> string -> sort,
    91     typ: Proof.context -> string -> typ,
    92     term: Proof.context -> string -> term,
    93     prop: Proof.context -> string -> term} -> unit
    94   val parse_sort: Proof.context -> string -> sort
    95   val parse_typ: Proof.context -> string -> typ
    96   val parse_term: Proof.context -> string -> term
    97   val parse_prop: Proof.context -> string -> term
    98   val check_sort: Proof.context -> sort -> sort
    99   val check_typ: Proof.context -> typ -> typ
   100   val check_term: Proof.context -> term -> term
   101   val check_prop: Proof.context -> term -> term
   102   val check_typs: Proof.context -> typ list -> typ list
   103   val check_terms: Proof.context -> term list -> term list
   104   val check_props: Proof.context -> term list -> term list
   105   val add_typ_check: (typ list -> Proof.context -> typ list * Proof.context) ->
   106     Context.generic -> Context.generic
   107   val add_term_check: (term list -> Proof.context -> term list * Proof.context) ->
   108     Context.generic -> Context.generic
   109   val read_sort: Proof.context -> string -> sort
   110   val read_typ: Proof.context -> string -> typ
   111   val read_term: Proof.context -> string -> term
   112   val read_prop: Proof.context -> string -> term
   113   val read_terms: Proof.context -> string list -> term list
   114   val read_props: Proof.context -> string list -> term list
   115   val read_sort_global: theory -> string -> sort
   116   val read_typ_global: theory -> string -> typ
   117   val read_term_global: theory -> string -> term
   118   val read_prop_global: theory -> string -> term
   119 end;
   120 
   121 structure Syntax: SYNTAX =
   122 struct
   123 
   124 (** tables of translation functions **)
   125 
   126 (* parse (ast) translations *)
   127 
   128 fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
   129 
   130 fun err_dup_trfun name c =
   131   error ("More than one " ^ name ^ " for " ^ quote c);
   132 
   133 fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
   134 
   135 fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns)
   136   handle Symtab.DUP c => err_dup_trfun name c;
   137 
   138 fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
   139   handle Symtab.DUP c => err_dup_trfun name c;
   140 
   141 
   142 (* print (ast) translations *)
   143 
   144 fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
   145 fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns;
   146 fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
   147 fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
   148 
   149 
   150 
   151 (** tables of token translation functions **)
   152 
   153 fun lookup_tokentr tabs modes =
   154   let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
   155   in fn c => Option.map fst (AList.lookup (op =) trs c) end;
   156 
   157 fun merge_tokentrtabs tabs1 tabs2 =
   158   let
   159     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   160 
   161     fun name (s, _) = implode (tl (Symbol.explode s));
   162 
   163     fun merge mode =
   164       let
   165         val trs1 = these (AList.lookup (op =) tabs1 mode);
   166         val trs2 = these (AList.lookup (op =) tabs2 mode);
   167         val trs = distinct eq_tr (trs1 @ trs2);
   168       in
   169         (case duplicates (eq_fst (op =)) trs of
   170           [] => (mode, trs)
   171         | dups => error ("More than one token translation function in mode " ^
   172             quote mode ^ " for " ^ commas_quote (map name dups)))
   173       end;
   174   in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
   175 
   176 fun extend_tokentrtab tokentrs tabs =
   177   let
   178     fun ins_tokentr (m, c, f) =
   179       AList.default (op =) (m, [])
   180       #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
   181   in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
   182 
   183 
   184 
   185 (** tables of translation rules **)
   186 
   187 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   188 
   189 fun dest_ruletab tab = maps snd (Symtab.dest tab);
   190 
   191 
   192 (* empty, extend, merge ruletabs *)
   193 
   194 val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r));
   195 val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
   196 fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
   197 
   198 
   199 
   200 (** datatype syntax **)
   201 
   202 datatype syntax =
   203   Syntax of {
   204     input: SynExt.xprod list,
   205     lexicon: Scan.lexicon,
   206     gram: Parser.gram,
   207     consts: string list,
   208     prmodes: string list,
   209     parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   210     parse_ruletab: ruletab,
   211     parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
   212     print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
   213     print_ruletab: ruletab,
   214     print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   215     tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list,
   216     prtabs: Printer.prtabs} * stamp;
   217 
   218 fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
   219 
   220 fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
   221 
   222 type mode = string * bool;
   223 val default_mode = ("", true);
   224 val input_mode = ("input", true);
   225 val internalM = "internal";
   226 
   227 
   228 (* empty_syntax *)
   229 
   230 val empty_syntax = Syntax
   231   ({input = [],
   232     lexicon = Scan.empty_lexicon,
   233     gram = Parser.empty_gram,
   234     consts = [],
   235     prmodes = [],
   236     parse_ast_trtab = Symtab.empty,
   237     parse_ruletab = Symtab.empty,
   238     parse_trtab = Symtab.empty,
   239     print_trtab = Symtab.empty,
   240     print_ruletab = Symtab.empty,
   241     print_ast_trtab = Symtab.empty,
   242     tokentrtab = [],
   243     prtabs = Printer.empty_prtabs}, stamp ());
   244 
   245 
   246 (* extend_syntax *)
   247 
   248 fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   249   let
   250     val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
   251       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   252       print_ast_trtab, tokentrtab, prtabs} = tabs;
   253     val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
   254       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   255       print_ast_translation, token_translation} = syn_ext;
   256     fun if_inout xs = if inout then xs else [];
   257   in
   258     Syntax
   259     ({input = if_inout xprods @ input,
   260       lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon,
   261       gram = Parser.extend_gram gram (if_inout xprods),
   262       consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2),
   263       prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
   264       parse_ast_trtab =
   265         extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
   266       parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab,
   267       parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab,
   268       print_trtab = extend_tr'tab print_translation print_trtab,
   269       print_ruletab = extend_ruletab print_rules print_ruletab,
   270       print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
   271       tokentrtab = extend_tokentrtab token_translation tokentrtab,
   272       prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ())
   273   end;
   274 
   275 
   276 (* remove_syntax *)
   277 
   278 fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   279   let
   280     val SynExt.SynExt {xprods, consts = _, prmodes = _,
   281       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   282       print_ast_translation, token_translation = _} = syn_ext;
   283     val {input, lexicon, gram, consts, prmodes,
   284       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   285       print_ast_trtab, tokentrtab, prtabs} = tabs;
   286     val input' = if inout then subtract (op =) xprods input else input;
   287     fun if_inout xs = if inout then xs else [];
   288   in
   289     Syntax
   290     ({input = input',
   291       lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
   292       gram = if inout then Parser.make_gram input' else gram,
   293       consts = consts,
   294       prmodes = prmodes,
   295       parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
   296       parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
   297       parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
   298       print_trtab = remove_tr'tab print_translation print_trtab,
   299       print_ruletab = remove_ruletab print_rules print_ruletab,
   300       print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   301       tokentrtab = tokentrtab,
   302       prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
   303   end;
   304 
   305 
   306 (* merge_syntaxes *)
   307 
   308 fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
   309   let
   310     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   311       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
   312       parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
   313       print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   314       print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   315 
   316     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   317       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
   318       parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
   319       print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   320       print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   321   in
   322     Syntax
   323     ({input = Library.merge (op =) (input1, input2),
   324       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   325       gram = Parser.merge_grams gram1 gram2,
   326       consts = sort_distinct string_ord (consts1 @ consts2),
   327       prmodes = Library.merge (op =) (prmodes1, prmodes2),
   328       parse_ast_trtab =
   329         merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   330       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   331       parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   332       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   333       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   334       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   335       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   336       prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
   337   end;
   338 
   339 
   340 (* basic syntax *)
   341 
   342 val basic_syn =
   343   empty_syntax
   344   |> extend_syntax default_mode TypeExt.type_ext
   345   |> extend_syntax default_mode SynExt.pure_ext;
   346 
   347 val basic_nonterms =
   348   (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
   349     SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
   350     "asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]);
   351 
   352 
   353 
   354 (** print syntax **)
   355 
   356 local
   357 
   358 fun pretty_strs_qs name strs =
   359   Pretty.strs (name :: map Library.quote (sort_strings strs));
   360 
   361 fun pretty_gram (Syntax (tabs, _)) =
   362   let
   363     val {lexicon, prmodes, gram, prtabs, ...} = tabs;
   364     val prmodes' = sort_strings (filter_out (equal "") prmodes);
   365   in
   366     [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
   367       Pretty.big_list "prods:" (Parser.pretty_gram gram),
   368       pretty_strs_qs "print modes:" prmodes']
   369   end;
   370 
   371 fun pretty_trans (Syntax (tabs, _)) =
   372   let
   373     fun pretty_trtab name tab =
   374       pretty_strs_qs name (Symtab.keys tab);
   375 
   376     fun pretty_ruletab name tab =
   377       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   378 
   379     fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
   380 
   381     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   382       print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
   383   in
   384     [pretty_strs_qs "consts:" consts,
   385       pretty_trtab "parse_ast_translation:" parse_ast_trtab,
   386       pretty_ruletab "parse_rules:" parse_ruletab,
   387       pretty_trtab "parse_translation:" parse_trtab,
   388       pretty_trtab "print_translation:" print_trtab,
   389       pretty_ruletab "print_rules:" print_ruletab,
   390       pretty_trtab "print_ast_translation:" print_ast_trtab,
   391       Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
   392   end;
   393 
   394 in
   395 
   396 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
   397 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
   398 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
   399 
   400 end;
   401 
   402 
   403 
   404 (** read **)
   405 
   406 (* read_ast *)
   407 
   408 val ambiguity_level = ref 1;
   409 val ambiguity_is_error = ref false;
   410 
   411 fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str =
   412   let
   413     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   414     val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
   415     val chars = Symbol.explode str;
   416     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   417 
   418     fun show_pt pt =
   419       Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
   420   in
   421     if length pts > ! ambiguity_level then
   422       if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str)
   423       else (warning ("Ambiguous input " ^ quote str ^ "\n" ^
   424           "produces " ^ string_of_int (length pts) ^ " parse trees.");
   425          List.app (warning o show_pt) pts)
   426     else ();
   427     SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
   428   end;
   429 
   430 
   431 (* read *)
   432 
   433 fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str =
   434   let
   435     val {parse_ruletab, parse_trtab, ...} = tabs;
   436     val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str;
   437   in
   438     SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
   439       (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
   440   end;
   441 
   442 
   443 (* read terms *)
   444 
   445 (*brute-force disambiguation via type-inference*)
   446 fun disambig _ _ [t] = t
   447   | disambig pp check ts =
   448       let
   449         val err_results = map check ts;
   450         val errs = map_filter (fn Exn.Exn (ERROR msg) => SOME msg | _ => NONE) err_results;
   451         val results = map_filter Exn.get_result err_results;
   452 
   453         val ambiguity = length ts;
   454         fun ambig_msg () =
   455           if ambiguity > 1 andalso ambiguity <= ! ambiguity_level then
   456             "Got more than one parse tree.\n\
   457             \Retry with smaller ambiguity_level for more information."
   458           else "";
   459       in
   460         if null results then cat_error (ambig_msg ()) (cat_lines errs)
   461         else if length results = 1 then
   462           (if ambiguity > ! ambiguity_level then
   463             warning "Fortunately, only one parse tree is type correct.\n\
   464               \You may still want to disambiguate your grammar or your input."
   465           else (); hd results)
   466         else cat_error (ambig_msg ()) ("More than one term is type correct:\n" ^
   467           cat_lines (map (Pretty.string_of_term pp) ts))
   468       end;
   469 
   470 fun standard_parse_term pp check get_sort map_const map_free map_type map_sort
   471     ctxt is_logtype syn ty str =
   472   read ctxt is_logtype syn ty str
   473   |> map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
   474   |> disambig (Printer.pp_show_brackets pp) check;
   475 
   476 
   477 (* read types *)
   478 
   479 fun standard_parse_typ ctxt syn get_sort map_sort str =
   480   (case read ctxt (K false) syn SynExt.typeT str of
   481     [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
   482   | _ => error "read_typ: ambiguous syntax");
   483 
   484 
   485 (* read sorts *)
   486 
   487 fun standard_parse_sort ctxt syn map_sort str =
   488   (case read ctxt (K false) syn TypeExt.sortT str of
   489     [t] => TypeExt.sort_of_term map_sort t
   490   | _ => error "read_sort: ambiguous syntax");
   491 
   492 
   493 
   494 (** prepare translation rules **)
   495 
   496 datatype 'a trrule =
   497   ParseRule of 'a * 'a |
   498   PrintRule of 'a * 'a |
   499   ParsePrintRule of 'a * 'a;
   500 
   501 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   502   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   503   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   504 
   505 fun parse_rule (ParseRule pats) = SOME pats
   506   | parse_rule (PrintRule _) = NONE
   507   | parse_rule (ParsePrintRule pats) = SOME pats;
   508 
   509 fun print_rule (ParseRule _) = NONE
   510   | print_rule (PrintRule pats) = SOME (swap pats)
   511   | print_rule (ParsePrintRule pats) = SOME (swap pats);
   512 
   513 
   514 local
   515 
   516 fun check_rule (rule as (lhs, rhs)) =
   517   (case Ast.rule_error rule of
   518     SOME msg =>
   519       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   520         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   521   | NONE => rule);
   522 
   523 
   524 fun read_pattern ctxt is_logtype syn (root, str) =
   525   let
   526     val Syntax ({consts, ...}, _) = syn;
   527 
   528     fun constify (ast as Ast.Constant _) = ast
   529       | constify (ast as Ast.Variable x) =
   530           if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x
   531           else ast
   532       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   533   in
   534     (case read_asts ctxt is_logtype syn true root str of
   535       [ast] => constify ast
   536     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   537   end handle ERROR msg =>
   538     cat_error msg ("The error(s) above occurred in translation pattern " ^
   539       quote str);
   540 
   541 fun prep_rules rd_pat raw_rules =
   542   let val rules = map (map_trrule rd_pat) raw_rules in
   543     (map check_rule (map_filter parse_rule rules),
   544       map check_rule (map_filter print_rule rules))
   545   end
   546 
   547 in
   548 
   549 val cert_rules = prep_rules I;
   550 
   551 fun read_rules ctxt is_logtype syn =
   552   prep_rules (read_pattern ctxt is_logtype syn);
   553 
   554 end;
   555 
   556 
   557 
   558 (** pretty terms, typs, sorts **)
   559 
   560 fun pretty_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
   561   let
   562     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   563     val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
   564   in
   565     Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
   566       (lookup_tokentr tokentrtab (print_mode_value ()))
   567       (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
   568   end;
   569 
   570 fun pretty_term extern =
   571   pretty_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
   572 fun pretty_typ ctxt syn =
   573   pretty_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
   574 fun pretty_sort ctxt syn =
   575   pretty_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
   576 
   577 
   578 
   579 (** modify syntax **)
   580 
   581 fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
   582 fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
   583 
   584 val extend_type_gram       = ext_syntax Mixfix.syn_ext_types;
   585 val extend_const_gram      = ext_syntax' Mixfix.syn_ext_consts;
   586 val extend_consts          = ext_syntax SynExt.syn_ext_const_names;
   587 val extend_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
   588 val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
   589 val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
   590 
   591 fun remove_const_gram is_logtype prmode decls =
   592   remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
   593 
   594 fun extend_trrules ctxt is_logtype syn =
   595   ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   596 
   597 fun remove_trrules ctxt is_logtype syn =
   598   remove_syntax default_mode o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   599 
   600 val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
   601 val remove_trrules_i = remove_syntax default_mode o SynExt.syn_ext_rules o cert_rules;
   602 
   603 
   604 
   605 (** generic parsing and type-checking **)
   606 
   607 (* parsing *)
   608 
   609 type parsers =
   610  {sort: Proof.context -> string -> sort,
   611   typ: Proof.context -> string -> typ,
   612   term: Proof.context -> string -> term,
   613   prop: Proof.context -> string -> term};
   614 
   615 local
   616   val parsers = ref (NONE: parsers option);
   617 in
   618   fun install_parsers ps = CRITICAL (fn () =>
   619     if is_some (! parsers) then error "Inner syntax parsers already installed"
   620     else parsers := SOME ps);
   621 
   622   fun parse which ctxt s =
   623     (case ! parsers of
   624       NONE => error "Inner syntax parsers not yet installed"
   625     | SOME ps => which ps ctxt s);
   626 end;
   627 
   628 val parse_sort = parse #sort;
   629 val parse_typ = parse #typ;
   630 val parse_term = parse #term;
   631 val parse_prop = parse #prop;
   632 
   633 
   634 (* checking types/terms *)
   635 
   636 local
   637 
   638 structure Checks = GenericDataFun
   639 (
   640   type T =
   641     ((typ list -> Proof.context -> typ list * Proof.context) * stamp) list *
   642     ((term list -> Proof.context -> term list * Proof.context) * stamp) list;
   643   val empty = ([], []);
   644   val extend = I;
   645   fun merge _ ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
   646     (Library.merge (eq_snd op =) (typ_checks1, typ_checks2),
   647      Library.merge (eq_snd op =) (term_checks1, term_checks2));
   648 );
   649 
   650 fun gen_check which eq ctxt0 xs0 =
   651   let
   652     val funs = map fst (rev (which (Checks.get (Context.Proof ctxt0))));
   653 
   654     fun check [] xs ctxt = (xs, ctxt)
   655       | check (f :: fs) xs ctxt = f xs ctxt |-> check fs;
   656 
   657     fun check_fixpoint xs ctxt =
   658       let val (xs', ctxt') = check funs xs ctxt in
   659         if eq_list eq (xs, xs') then (xs, ctxt)
   660         else check_fixpoint xs' ctxt'
   661       end;
   662   in #1 (case funs of [f] => f xs0 ctxt0 | _ => check_fixpoint xs0 ctxt0) end;
   663 
   664 in
   665 
   666 val check_typs = gen_check fst (op =);
   667 val check_terms = gen_check snd (op aconv);
   668 fun check_props ctxt = map (TypeInfer.constrain propT) #> check_terms ctxt;
   669 
   670 val check_typ = singleton o check_typs;
   671 val check_term = singleton o check_terms;
   672 val check_prop = singleton o check_props;
   673 
   674 fun check_sort ctxt S =
   675   (case singleton (check_typs ctxt) (TFree ("", S)) of   (* FIXME TypeInfer.invent_type (!?) *)
   676     TFree ("", S') => S'
   677   | T => raise TYPE ("check_sort", [T], []));
   678 
   679 fun add_typ_check f = Checks.map (apfst (cons (f, stamp ())));
   680 fun add_term_check f = Checks.map (apsnd (cons (f, stamp ())));
   681 
   682 end;
   683 
   684 
   685 (* combined operations *)
   686 
   687 fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
   688 fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
   689 
   690 fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
   691 fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
   692 
   693 val read_term = singleton o read_terms;
   694 val read_prop = singleton o read_props;
   695 
   696 val read_sort_global = read_sort o ProofContext.init;
   697 val read_typ_global = read_typ o ProofContext.init;
   698 val read_term_global = read_term o ProofContext.init;
   699 val read_prop_global = read_prop o ProofContext.init;
   700 
   701 
   702 (*export parts of internal Syntax structures*)
   703 open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   704 
   705 end;
   706 
   707 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   708 open BasicSyntax;
   709 
   710 structure Hidden = struct end;
   711 structure Ast = Hidden;
   712 structure SynExt = Hidden;
   713 structure Parser = Hidden;
   714 structure TypeExt = Hidden;
   715 structure SynTrans = Hidden;
   716 structure Mixfix = Hidden;
   717 structure Printer = Hidden;