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