src/Pure/Syntax/syntax.ML
author wenzelm
Mon Aug 20 17:46:32 2007 +0200 (2007-08-20)
changeset 24341 7b8da2396c49
parent 24278 cf82c471f9ee
child 24372 743575ccfec8
permissions -rw-r--r--
type_check: tuned singleton funs case;
     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:
    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 list
    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 global_parse_sort: theory -> string -> sort
    99   val global_parse_typ: theory -> string -> typ
   100   val global_parse_term: theory -> string -> term
   101   val global_parse_prop: theory -> string -> term
   102   val add_type_check: (term list -> Proof.context -> Term.term list * Proof.context) ->
   103     Context.generic -> Context.generic
   104   val type_check: term list -> Proof.context -> term list * Proof.context
   105   val check_terms: Proof.context -> term list -> term list
   106   val check_props: Proof.context -> term list -> term list
   107   val check_typs: Proof.context -> typ list -> typ list
   108   val read_sort: Proof.context -> string -> sort
   109   val read_typ: Proof.context -> string -> typ
   110   val read_terms: Proof.context -> string list -> term list
   111   val read_props: Proof.context -> string list -> term list
   112   val read_term: Proof.context -> string -> term
   113   val read_prop: Proof.context -> string -> term
   114   val global_read_sort: theory -> string -> sort
   115   val global_read_typ: theory -> string -> typ
   116   val global_read_term: theory -> string -> term
   117   val global_read_prop: theory -> string -> term
   118 end;
   119 
   120 structure Syntax: SYNTAX =
   121 struct
   122 
   123 (** tables of translation functions **)
   124 
   125 (* parse (ast) translations *)
   126 
   127 fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
   128 
   129 fun err_dup_trfun name c =
   130   error ("More than one " ^ name ^ " for " ^ quote c);
   131 
   132 fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
   133 
   134 fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns)
   135   handle Symtab.DUP c => err_dup_trfun name c;
   136 
   137 fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
   138   handle Symtab.DUP c => err_dup_trfun name c;
   139 
   140 
   141 (* print (ast) translations *)
   142 
   143 fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c);
   144 fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns;
   145 fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns;
   146 fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2);
   147 
   148 
   149 
   150 (** tables of token translation functions **)
   151 
   152 fun lookup_tokentr tabs modes =
   153   let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
   154   in fn c => Option.map fst (AList.lookup (op =) trs c) end;
   155 
   156 fun merge_tokentrtabs tabs1 tabs2 =
   157   let
   158     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   159 
   160     fun name (s, _) = implode (tl (Symbol.explode s));
   161 
   162     fun merge mode =
   163       let
   164         val trs1 = these (AList.lookup (op =) tabs1 mode);
   165         val trs2 = these (AList.lookup (op =) tabs2 mode);
   166         val trs = distinct eq_tr (trs1 @ trs2);
   167       in
   168         (case duplicates (eq_fst (op =)) trs of
   169           [] => (mode, trs)
   170         | dups => error ("More than one token translation function in mode " ^
   171             quote mode ^ " for " ^ commas_quote (map name dups)))
   172       end;
   173   in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
   174 
   175 fun extend_tokentrtab tokentrs tabs =
   176   let
   177     fun ins_tokentr (m, c, f) =
   178       AList.default (op =) (m, [])
   179       #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
   180   in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
   181 
   182 
   183 
   184 (** tables of translation rules **)
   185 
   186 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   187 
   188 fun dest_ruletab tab = maps snd (Symtab.dest tab);
   189 
   190 
   191 (* empty, extend, merge ruletabs *)
   192 
   193 val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r));
   194 val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
   195 fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
   196 
   197 
   198 
   199 (** datatype syntax **)
   200 
   201 datatype syntax =
   202   Syntax of {
   203     input: SynExt.xprod list,
   204     lexicon: Scan.lexicon,
   205     gram: Parser.gram,
   206     consts: string list,
   207     prmodes: string list,
   208     parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   209     parse_ruletab: ruletab,
   210     parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
   211     print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table,
   212     print_ruletab: ruletab,
   213     print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   214     tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list,
   215     prtabs: Printer.prtabs} * stamp;
   216 
   217 fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
   218 
   219 fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
   220 
   221 type mode = string * bool;
   222 val default_mode = ("", true);
   223 val input_mode = ("input", true);
   224 val internalM = "internal";
   225 
   226 
   227 (* empty_syntax *)
   228 
   229 val empty_syntax = Syntax
   230   ({input = [],
   231     lexicon = Scan.empty_lexicon,
   232     gram = Parser.empty_gram,
   233     consts = [],
   234     prmodes = [],
   235     parse_ast_trtab = Symtab.empty,
   236     parse_ruletab = Symtab.empty,
   237     parse_trtab = Symtab.empty,
   238     print_trtab = Symtab.empty,
   239     print_ruletab = Symtab.empty,
   240     print_ast_trtab = Symtab.empty,
   241     tokentrtab = [],
   242     prtabs = Printer.empty_prtabs}, stamp ());
   243 
   244 
   245 (* extend_syntax *)
   246 
   247 fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   248   let
   249     val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
   250       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   251       print_ast_trtab, tokentrtab, prtabs} = tabs;
   252     val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
   253       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   254       print_ast_translation, token_translation} = syn_ext;
   255     fun if_inout xs = if inout then xs else [];
   256   in
   257     Syntax
   258     ({input = if_inout xprods @ input,
   259       lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon,
   260       gram = Parser.extend_gram gram (if_inout xprods),
   261       consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2),
   262       prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
   263       parse_ast_trtab =
   264         extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
   265       parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab,
   266       parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab,
   267       print_trtab = extend_tr'tab print_translation print_trtab,
   268       print_ruletab = extend_ruletab print_rules print_ruletab,
   269       print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
   270       tokentrtab = extend_tokentrtab token_translation tokentrtab,
   271       prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ())
   272   end;
   273 
   274 
   275 (* remove_syntax *)
   276 
   277 fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
   278   let
   279     val SynExt.SynExt {xprods, consts = _, prmodes = _,
   280       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   281       print_ast_translation, token_translation = _} = syn_ext;
   282     val {input, lexicon, gram, consts, prmodes,
   283       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   284       print_ast_trtab, tokentrtab, prtabs} = tabs;
   285     val input' = if inout then subtract (op =) xprods input else input;
   286     fun if_inout xs = if inout then xs else [];
   287   in
   288     Syntax
   289     ({input = input',
   290       lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
   291       gram = if inout then Parser.make_gram input' else gram,
   292       consts = consts,
   293       prmodes = prmodes,
   294       parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
   295       parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
   296       parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
   297       print_trtab = remove_tr'tab print_translation print_trtab,
   298       print_ruletab = remove_ruletab print_rules print_ruletab,
   299       print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   300       tokentrtab = tokentrtab,
   301       prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
   302   end;
   303 
   304 
   305 (* merge_syntaxes *)
   306 
   307 fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
   308   let
   309     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   310       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
   311       parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
   312       print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   313       print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   314 
   315     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   316       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
   317       parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
   318       print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   319       print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   320   in
   321     Syntax
   322     ({input = Library.merge (op =) (input1, input2),
   323       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   324       gram = Parser.merge_grams gram1 gram2,
   325       consts = sort_distinct string_ord (consts1 @ consts2),
   326       prmodes = Library.merge (op =) (prmodes1, prmodes2),
   327       parse_ast_trtab =
   328         merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   329       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   330       parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   331       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   332       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   333       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   334       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   335       prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
   336   end;
   337 
   338 
   339 (* basic syntax *)
   340 
   341 val basic_syn =
   342   empty_syntax
   343   |> extend_syntax default_mode TypeExt.type_ext
   344   |> extend_syntax default_mode SynExt.pure_ext;
   345 
   346 val basic_nonterms =
   347   (Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes",
   348     SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
   349     "asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]);
   350 
   351 
   352 
   353 (** print syntax **)
   354 
   355 local
   356 
   357 fun pretty_strs_qs name strs =
   358   Pretty.strs (name :: map Library.quote (sort_strings strs));
   359 
   360 fun pretty_gram (Syntax (tabs, _)) =
   361   let
   362     val {lexicon, prmodes, gram, prtabs, ...} = tabs;
   363     val prmodes' = sort_strings (filter_out (equal "") prmodes);
   364   in
   365     [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
   366       Pretty.big_list "prods:" (Parser.pretty_gram gram),
   367       pretty_strs_qs "print modes:" prmodes']
   368   end;
   369 
   370 fun pretty_trans (Syntax (tabs, _)) =
   371   let
   372     fun pretty_trtab name tab =
   373       pretty_strs_qs name (Symtab.keys tab);
   374 
   375     fun pretty_ruletab name tab =
   376       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   377 
   378     fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
   379 
   380     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   381       print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
   382   in
   383     [pretty_strs_qs "consts:" consts,
   384       pretty_trtab "parse_ast_translation:" parse_ast_trtab,
   385       pretty_ruletab "parse_rules:" parse_ruletab,
   386       pretty_trtab "parse_translation:" parse_trtab,
   387       pretty_trtab "print_translation:" print_trtab,
   388       pretty_ruletab "print_rules:" print_ruletab,
   389       pretty_trtab "print_ast_translation:" print_ast_trtab,
   390       Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
   391   end;
   392 
   393 in
   394 
   395 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
   396 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
   397 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
   398 
   399 end;
   400 
   401 
   402 
   403 (** read **)
   404 
   405 (* read_ast *)
   406 
   407 val ambiguity_level = ref 1;
   408 val ambiguity_is_error = ref false;
   409 
   410 fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str =
   411   let
   412     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   413     val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
   414     val chars = Symbol.explode str;
   415     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   416 
   417     fun show_pt pt =
   418       Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt])));
   419   in
   420     if length pts > ! ambiguity_level then
   421       if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str)
   422       else (warning ("Ambiguous input " ^ quote str ^ "\n" ^
   423           "produces " ^ string_of_int (length pts) ^ " parse trees.");
   424          List.app (warning o show_pt) pts)
   425     else ();
   426     SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts
   427   end;
   428 
   429 
   430 (* read *)
   431 
   432 fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str =
   433   let
   434     val {parse_ruletab, parse_trtab, ...} = tabs;
   435     val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str;
   436   in
   437     SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab)
   438       (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
   439   end;
   440 
   441 
   442 (* read terms *)
   443 
   444 fun standard_parse_term get_sort map_const map_free map_type map_sort ctxt is_logtype syn ty str =
   445   map (TypeExt.decode_term get_sort map_const map_free map_type map_sort)
   446     (read ctxt is_logtype syn ty str);
   447 
   448 
   449 (* read types *)
   450 
   451 fun standard_parse_typ ctxt syn get_sort map_sort str =
   452   (case read ctxt (K false) syn SynExt.typeT str of
   453     [t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t
   454   | _ => error "read_typ: ambiguous syntax");
   455 
   456 
   457 (* read sorts *)
   458 
   459 fun standard_parse_sort ctxt syn map_sort str =
   460   (case read ctxt (K false) syn TypeExt.sortT str of
   461     [t] => TypeExt.sort_of_term map_sort t
   462   | _ => error "read_sort: ambiguous syntax");
   463 
   464 
   465 
   466 (** prepare translation rules **)
   467 
   468 datatype 'a trrule =
   469   ParseRule of 'a * 'a |
   470   PrintRule of 'a * 'a |
   471   ParsePrintRule of 'a * 'a;
   472 
   473 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   474   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   475   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   476 
   477 fun parse_rule (ParseRule pats) = SOME pats
   478   | parse_rule (PrintRule _) = NONE
   479   | parse_rule (ParsePrintRule pats) = SOME pats;
   480 
   481 fun print_rule (ParseRule _) = NONE
   482   | print_rule (PrintRule pats) = SOME (swap pats)
   483   | print_rule (ParsePrintRule pats) = SOME (swap pats);
   484 
   485 
   486 local
   487 
   488 fun check_rule (rule as (lhs, rhs)) =
   489   (case Ast.rule_error rule of
   490     SOME msg =>
   491       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   492         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   493   | NONE => rule);
   494 
   495 
   496 fun read_pattern ctxt is_logtype syn (root, str) =
   497   let
   498     val Syntax ({consts, ...}, _) = syn;
   499 
   500     fun constify (ast as Ast.Constant _) = ast
   501       | constify (ast as Ast.Variable x) =
   502           if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x
   503           else ast
   504       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   505   in
   506     (case read_asts ctxt is_logtype syn true root str of
   507       [ast] => constify ast
   508     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   509   end handle ERROR msg =>
   510     cat_error msg ("The error(s) above occurred in translation pattern " ^
   511       quote str);
   512 
   513 fun prep_rules rd_pat raw_rules =
   514   let val rules = map (map_trrule rd_pat) raw_rules in
   515     (map check_rule (map_filter parse_rule rules),
   516       map check_rule (map_filter print_rule rules))
   517   end
   518 
   519 in
   520 
   521 val cert_rules = prep_rules I;
   522 
   523 fun read_rules ctxt is_logtype syn =
   524   prep_rules (read_pattern ctxt is_logtype syn);
   525 
   526 end;
   527 
   528 
   529 
   530 (** pretty terms, typs, sorts **)
   531 
   532 fun pretty_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t =
   533   let
   534     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   535     val ast = t_to_ast ctxt (lookup_tr' print_trtab) t;
   536   in
   537     Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab)
   538       (lookup_tokentr tokentrtab (! print_mode))
   539       (Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast))
   540   end;
   541 
   542 fun pretty_term extern =
   543   pretty_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term;
   544 fun pretty_typ ctxt syn =
   545   pretty_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false;
   546 fun pretty_sort ctxt syn =
   547   pretty_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false;
   548 
   549 
   550 
   551 (** modify syntax **)
   552 
   553 fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
   554 fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
   555 
   556 val extend_type_gram       = ext_syntax Mixfix.syn_ext_types;
   557 val extend_const_gram      = ext_syntax' Mixfix.syn_ext_consts;
   558 val extend_consts          = ext_syntax SynExt.syn_ext_const_names;
   559 val extend_trfuns          = ext_syntax SynExt.syn_ext_trfuns;
   560 val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns;
   561 val extend_tokentrfuns     = ext_syntax SynExt.syn_ext_tokentrfuns;
   562 
   563 fun remove_const_gram is_logtype prmode decls =
   564   remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
   565 
   566 fun extend_trrules ctxt is_logtype syn =
   567   ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   568 
   569 fun remove_trrules ctxt is_logtype syn =
   570   remove_syntax default_mode o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn;
   571 
   572 val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules;
   573 val remove_trrules_i = remove_syntax default_mode o SynExt.syn_ext_rules o cert_rules;
   574 
   575 
   576 
   577 (** generic parsing and type-checking **)
   578 
   579 (* parsing *)
   580 
   581 type parsers =
   582  {sort: Proof.context -> string -> sort,
   583   typ: Proof.context -> string -> typ,
   584   term: Proof.context -> string -> term,
   585   prop: Proof.context -> string -> term};
   586 
   587 local
   588   val parsers = ref (NONE: parsers option);
   589 in
   590   fun install_parsers ps = CRITICAL (fn () =>
   591     if is_some (! parsers) then error "Inner syntax parsers already installed"
   592     else parsers := SOME ps);
   593 
   594   fun parse which ctxt s =
   595     (case ! parsers of
   596       NONE => error "Inner syntax parsers not yet installed"
   597     | SOME ps => which ps ctxt s);
   598 end;
   599 
   600 val parse_sort = parse #sort;
   601 val parse_typ = parse #typ;
   602 val parse_term = parse #term;
   603 val parse_prop = parse #prop;
   604 
   605 val global_parse_sort = parse_sort o ProofContext.init;
   606 val global_parse_typ = parse_typ o ProofContext.init;
   607 val global_parse_term = parse_term o ProofContext.init;
   608 val global_parse_prop = parse_prop o ProofContext.init;
   609 
   610 
   611 (* type-checking *)
   612 
   613 structure TypeCheck = GenericDataFun
   614 (
   615   type T = ((term list -> Proof.context -> term list * Proof.context) * stamp) list;
   616   val empty = [];
   617   val extend = I;
   618   fun merge _ type_checks : T = Library.merge (eq_snd op =) type_checks;
   619 );
   620 
   621 fun add_type_check f = TypeCheck.map (cons (f, stamp ()));
   622 
   623 fun type_check ts0 ctxt0 =
   624   let
   625     val funs = map #1 (rev (TypeCheck.get (Context.Proof ctxt0)));
   626     fun check [] ts ctxt = (ts, ctxt)
   627       | check (f :: fs) ts ctxt = f ts ctxt |-> check fs;
   628 
   629     fun check_fixpoint ts ctxt =
   630       let val (ts', ctxt') = check funs ts ctxt in
   631         if eq_list (op aconv) (ts, ts') then (ts, ctxt)
   632         else check_fixpoint ts' ctxt'
   633       end;
   634   in (case funs of [f] => f ts0 ctxt0 | _ => check_fixpoint ts0 ctxt0) end;
   635 
   636 fun check_terms ctxt ts = #1 (type_check ts ctxt);
   637 fun check_props ctxt = map (fn t => TypeInfer.constrain t propT) #> check_terms ctxt;
   638 fun check_typs ctxt = map Logic.mk_type #> check_terms ctxt #> map Logic.dest_type;
   639 
   640 fun check_sort ctxt S =
   641   (case singleton (check_typs ctxt) (TFree ("", S)) of   (* FIXME TypeInfer.invent_type (!?) *)
   642     TFree ("", S') => S'
   643   | T => raise TYPE ("check_sort", [T], []));
   644 
   645 
   646 (* combined operations *)
   647 
   648 fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
   649 fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
   650 
   651 fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
   652 fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
   653 
   654 val read_term = singleton o read_terms;
   655 val read_prop = singleton o read_props;
   656 
   657 val global_read_sort = read_sort o ProofContext.init;
   658 val global_read_typ = read_typ o ProofContext.init;
   659 val global_read_term = read_term o ProofContext.init;
   660 val global_read_prop = read_prop o ProofContext.init;
   661 
   662 
   663 (*export parts of internal Syntax structures*)
   664 open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   665 
   666 end;
   667 
   668 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   669 open BasicSyntax;
   670 
   671 structure Hidden = struct end;
   672 structure Ast = Hidden;
   673 structure SynExt = Hidden;
   674 structure Parser = Hidden;
   675 structure TypeExt = Hidden;
   676 structure SynTrans = Hidden;
   677 structure Mixfix = Hidden;
   678 structure Printer = Hidden;