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