src/Pure/Syntax/syntax.ML
author wenzelm
Sat Apr 23 19:51:24 2005 +0200 (2005-04-23)
changeset 15833 78109c7012ed
parent 15759 144c9f9a8ade
child 16613 76e57e08dcb5
permissions -rw-r--r--
removed token_trans.ML (some content moved to syn_ext.ML);
     1 (*  Title:      Pure/Syntax/syntax.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Root of Isabelle's syntax module.
     6 *)
     7 
     8 signature BASIC_SYNTAX =
     9 sig
    10   include AST0
    11   include SYN_TRANS0
    12   include MIXFIX0
    13   include PRINTER0
    14 end;
    15 
    16 signature SYNTAX =
    17 sig
    18   include AST1
    19   include LEXICON0
    20   include SYN_EXT0
    21   include TYPE_EXT0
    22   include SYN_TRANS1
    23   include MIXFIX1
    24   include PRINTER0
    25   val extend_trtab: string -> (string * ('a * stamp)) list ->
    26     ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
    27   val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
    28     ('a * stamp) Symtab.table
    29   val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    30     -> ('a * stamp) list Symtab.table
    31   val extend_tr'tab: (string * ('a * stamp)) list ->
    32     ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    33   datatype 'a trrule =
    34     ParseRule of 'a * 'a |
    35     PrintRule of 'a * 'a |
    36     ParsePrintRule of 'a * 'a
    37   type syntax
    38   val is_keyword: syntax -> string -> bool
    39   val default_mode: string * bool
    40   val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
    41   val extend_const_gram: (string -> bool) ->
    42     string * bool -> (string * typ * mixfix) list -> syntax -> syntax
    43   val extend_consts: string list -> syntax -> syntax
    44   val extend_trfuns:
    45     (string * ((ast list -> ast) * stamp)) list *
    46     (string * ((term list -> term) * stamp)) list *
    47     (string * ((bool -> typ -> term list -> term) * stamp)) list *
    48     (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
    49   val extend_tokentrfuns: (string * string * (string -> string * real)) list -> syntax -> syntax
    50   val extend_trrules_i: ast trrule list -> syntax -> syntax
    51   val extend_trrules: (string -> bool) -> syntax ->
    52     (string * string) trrule list -> syntax -> syntax
    53   val remove_const_gram: (string -> bool) ->
    54     string * bool -> (string * typ * mixfix) list -> syntax -> syntax
    55   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    56   val merge_syntaxes: syntax -> syntax -> syntax
    57   val type_syn: syntax
    58   val pure_syn: syntax
    59   val print_gram: syntax -> unit
    60   val print_trans: syntax -> unit
    61   val print_syntax: syntax -> unit
    62   val read: (string -> bool) -> syntax -> typ -> string -> term list
    63   val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> (sort -> sort) ->
    64     string -> typ
    65   val read_sort: syntax -> string -> sort
    66   val pretty_term: syntax -> bool -> term -> Pretty.T
    67   val pretty_typ: syntax -> typ -> Pretty.T
    68   val pretty_sort: syntax -> sort -> Pretty.T
    69   val simple_pprint_typ: typ -> pprint_args -> unit
    70   val ambiguity_level: int ref
    71   val ambiguity_is_error: bool ref
    72 end;
    73 
    74 structure Syntax: SYNTAX =
    75 struct
    76 
    77 (** tables of translation functions **)
    78 
    79 (* parse (ast) translations *)
    80 
    81 fun lookup_tr tab c = Option.map fst (Symtab.lookup (tab, c));
    82 
    83 fun err_dup_trfuns name cs =
    84   error ("More than one " ^ name ^ " for " ^ commas_quote cs);
    85 
    86 fun extend_trtab name trfuns tab = Symtab.extend (tab, trfuns)
    87   handle Symtab.DUPS cs => err_dup_trfuns name cs;
    88 
    89 fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns;
    90 
    91 fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
    92   handle Symtab.DUPS cs => err_dup_trfuns name cs;
    93 
    94 
    95 (* print (ast) translations *)
    96 
    97 fun lookup_tr' tab c = map fst (Symtab.lookup_multi (tab, c));
    98 fun extend_tr'tab trfuns = fold_rev (curry Symtab.update_multi) trfuns;
    99 fun remove_tr'tab trfuns = fold (Symtab.remove_multi SynExt.eq_trfun) trfuns;
   100 fun merge_tr'tabs tab1 tab2 = Symtab.merge_multi' SynExt.eq_trfun (tab1, tab2);
   101 
   102 
   103 
   104 (** tables of token translation functions **)
   105 
   106 fun lookup_tokentr tabs modes =
   107   let val trs = gen_distinct eq_fst (List.concat (map (assocs tabs) (modes @ [""])))
   108   in fn c => Option.map fst (assoc (trs, c)) end;
   109 
   110 fun merge_tokentrtabs tabs1 tabs2 =
   111   let
   112     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   113 
   114     fun name (s, _) = implode (tl (Symbol.explode s));
   115 
   116     fun merge mode =
   117       let
   118         val trs1 = assocs tabs1 mode;
   119         val trs2 = assocs tabs2 mode;
   120         val trs = gen_distinct eq_tr (trs1 @ trs2);
   121       in
   122         (case gen_duplicates eq_fst trs of
   123           [] => (mode, trs)
   124         | dups => error ("More than one token translation function in mode " ^
   125             quote mode ^ " for " ^ commas_quote (map name dups)))
   126       end;
   127   in
   128     map merge (distinct (map fst (tabs1 @ tabs2)))
   129   end;
   130 
   131 fun extend_tokentrtab tokentrs tabs =
   132   let
   133     fun ins_tokentr (m, c, f) ts =
   134       overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
   135   in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
   136 
   137 
   138 
   139 (** tables of translation rules **)
   140 
   141 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   142 
   143 fun dest_ruletab tab = List.concat (map snd (Symtab.dest tab));
   144 fun lookup_ruletab tab a = Symtab.lookup_multi (tab, a);
   145 
   146 
   147 (* empty, extend, merge ruletabs *)
   148 
   149 val extend_ruletab =
   150   fold_rev (fn r => fn tab => Symtab.update_multi ((Ast.head_of_rule r, r), tab));
   151 
   152 val remove_ruletab = fold (fn r => Symtab.remove_multi (op =) (Ast.head_of_rule r, r));
   153 
   154 fun merge_ruletabs tab1 tab2 = Symtab.merge_multi' (op =) (tab1, tab2);
   155 
   156 
   157 
   158 (** datatype syntax **)
   159 
   160 datatype syntax =
   161   Syntax of {
   162     input: SynExt.xprod list,
   163     lexicon: Scan.lexicon,
   164     gram: Parser.gram,
   165     consts: string list,
   166     prmodes: string list,
   167     parse_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   168     parse_ruletab: ruletab,
   169     parse_trtab: ((term list -> term) * stamp) Symtab.table,
   170     print_trtab: ((bool -> typ -> term list -> term) * stamp) list Symtab.table,
   171     print_ruletab: ruletab,
   172     print_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   173     tokentrtab: (string * (string * ((string -> string * real) * stamp)) list) list,
   174     prtabs: Printer.prtabs}
   175 
   176 fun is_keyword (Syntax {lexicon, ...}) = Scan.is_literal lexicon o Symbol.explode;
   177 
   178 val default_mode = ("", true);
   179 
   180 
   181 (* empty_syntax *)
   182 
   183 val empty_syntax =
   184   Syntax {
   185     input = [],
   186     lexicon = Scan.empty_lexicon,
   187     gram = Parser.empty_gram,
   188     consts = [],
   189     prmodes = [],
   190     parse_ast_trtab = Symtab.empty,
   191     parse_ruletab = Symtab.empty,
   192     parse_trtab = Symtab.empty,
   193     print_trtab = Symtab.empty,
   194     print_ruletab = Symtab.empty,
   195     print_ast_trtab = Symtab.empty,
   196     tokentrtab = [],
   197     prtabs = Printer.empty_prtabs};
   198 
   199 
   200 (* extend_syntax *)
   201 
   202 fun extend_syntax (mode, inout) syn_ext (Syntax tabs) =
   203   let
   204     val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
   205       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   206       print_ast_trtab, tokentrtab, prtabs} = tabs;
   207     val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
   208       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   209       print_ast_translation, token_translation} = syn_ext;
   210   in
   211     Syntax {
   212       input = if inout then xprods @ input else input,
   213       lexicon = if inout then Scan.extend_lexicon lexicon (SynExt.delims_of xprods) else lexicon,
   214       gram = if inout then Parser.extend_gram gram xprods else gram,
   215       consts = merge_lists' consts1 consts2,
   216       prmodes = mode ins_string (merge_lists' prmodes1 prmodes2),
   217       parse_ast_trtab =
   218         extend_trtab "parse ast translation" parse_ast_translation parse_ast_trtab,
   219       parse_ruletab = extend_ruletab parse_rules parse_ruletab,
   220       parse_trtab = extend_trtab "parse translation" parse_translation parse_trtab,
   221       print_trtab = extend_tr'tab print_translation print_trtab,
   222       print_ruletab = extend_ruletab print_rules print_ruletab,
   223       print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
   224       tokentrtab = extend_tokentrtab token_translation tokentrtab,
   225       prtabs = Printer.extend_prtabs mode xprods prtabs}
   226   end;
   227 
   228 
   229 (* remove_syntax *)
   230 
   231 fun remove_syntax (mode, inout) syn_ext (Syntax tabs) =
   232   let
   233     val SynExt.SynExt {xprods, consts = _, prmodes = _,
   234       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   235       print_ast_translation, token_translation = _} = syn_ext;
   236     val {input, lexicon, gram, consts, prmodes,
   237       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   238       print_ast_trtab, tokentrtab, prtabs} = tabs;
   239     val input' = if inout then fold (remove (op =)) xprods input else input;
   240   in
   241     Syntax {
   242       input = input',
   243       lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
   244       gram = if inout then Parser.make_gram input' else gram,
   245       consts = consts,
   246       prmodes = prmodes,
   247       parse_ast_trtab = remove_trtab parse_ast_translation parse_ast_trtab,
   248       parse_ruletab = remove_ruletab parse_rules parse_ruletab,
   249       parse_trtab = remove_trtab parse_translation parse_trtab,
   250       print_trtab = remove_tr'tab print_translation print_trtab,
   251       print_ruletab = remove_ruletab print_rules print_ruletab,
   252       print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   253       tokentrtab = tokentrtab,
   254       prtabs = Printer.remove_prtabs mode xprods prtabs}
   255   end;
   256 
   257 
   258 (* merge_syntaxes *)
   259 
   260 fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   261   let
   262     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   263       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
   264       parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
   265       print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   266       print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   267 
   268     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   269       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
   270       parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
   271       print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   272       print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   273   in
   274     Syntax {
   275       input = merge_lists' input1 input2,
   276       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   277       gram = Parser.merge_grams gram1 gram2,
   278       consts = unique_strings (sort_strings (consts1 @ consts2)),
   279       prmodes = merge_lists prmodes1 prmodes2,
   280       parse_ast_trtab =
   281         merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   282       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   283       parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   284       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   285       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   286       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   287       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   288       prtabs = Printer.merge_prtabs prtabs1 prtabs2}
   289   end;
   290 
   291 
   292 (* type_syn *)
   293 
   294 val type_syn = empty_syntax |> extend_syntax default_mode TypeExt.type_ext;
   295 val pure_syn = type_syn |> extend_syntax default_mode SynExt.pure_ext;
   296 
   297 
   298 
   299 (** print syntax **)
   300 
   301 local
   302 
   303 fun pretty_strs_qs name strs =
   304   Pretty.strs (name :: map Library.quote (sort_strings strs));
   305 
   306 fun pretty_gram (Syntax tabs) =
   307   let
   308     val {lexicon, prmodes, gram, prtabs, ...} = tabs;
   309     val prmodes' = sort_strings (filter_out (equal "") prmodes);
   310   in
   311     [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
   312       Pretty.big_list "prods:" (Parser.pretty_gram gram),
   313       pretty_strs_qs "print modes:" prmodes']
   314   end;
   315 
   316 fun pretty_trans (Syntax tabs) =
   317   let
   318     fun pretty_trtab name tab =
   319       pretty_strs_qs name (Symtab.keys tab);
   320 
   321     fun pretty_ruletab name tab =
   322       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   323 
   324     fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
   325 
   326     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   327       print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
   328   in
   329     [pretty_strs_qs "consts:" consts,
   330       pretty_trtab "parse_ast_translation:" parse_ast_trtab,
   331       pretty_ruletab "parse_rules:" parse_ruletab,
   332       pretty_trtab "parse_translation:" parse_trtab,
   333       pretty_trtab "print_translation:" print_trtab,
   334       pretty_ruletab "print_rules:" print_ruletab,
   335       pretty_trtab "print_ast_translation:" print_ast_trtab,
   336       Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
   337   end;
   338 
   339 in
   340 
   341 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
   342 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
   343 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
   344 
   345 end;
   346 
   347 
   348 
   349 (** read **)
   350 
   351 (* read_ast *)
   352 
   353 val ambiguity_level = ref 1;
   354 val ambiguity_is_error = ref false
   355 
   356 fun read_asts is_logtype (Syntax tabs) xids root str =
   357   let
   358     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   359     val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
   360     val chars = Symbol.explode str;
   361     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   362 
   363     fun show_pt pt =
   364       warning (Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts (K NONE) [pt]))));
   365   in
   366     if length pts > ! ambiguity_level then
   367         if ! ambiguity_is_error then
   368             error ("Ambiguous input " ^ quote str)
   369         else
   370             (warning ("Ambiguous input " ^ quote str);
   371              warning "produces the following parse trees:";
   372              List.app show_pt pts)
   373     else ();
   374     SynTrans.pts_to_asts (lookup_tr parse_ast_trtab) pts
   375   end;
   376 
   377 
   378 (* read *)
   379 
   380 fun read is_logtype (syn as Syntax tabs) ty str =
   381   let
   382     val {parse_ruletab, parse_trtab, ...} = tabs;
   383     val asts = read_asts is_logtype syn false (SynExt.typ_to_nonterm ty) str;
   384   in
   385     SynTrans.asts_to_terms (lookup_tr parse_trtab)
   386       (map (Ast.normalize_ast (lookup_ruletab parse_ruletab)) asts)
   387   end;
   388 
   389 
   390 (* read types *)
   391 
   392 fun read_typ syn get_sort map_sort str =
   393   (case read (K false) syn SynExt.typeT str of
   394     [t] => TypeExt.typ_of_term (get_sort (TypeExt.raw_term_sorts t)) map_sort t
   395   | _ => error "read_typ: ambiguous syntax");
   396 
   397 
   398 (* read sorts *)
   399 
   400 fun read_sort syn str =
   401   (case read (K false) syn TypeExt.sortT str of
   402     [t] => TypeExt.sort_of_term t
   403   | _ => error "read_sort: ambiguous syntax");
   404 
   405 
   406 
   407 (** prepare translation rules **)
   408 
   409 datatype 'a trrule =
   410   ParseRule of 'a * 'a |
   411   PrintRule of 'a * 'a |
   412   ParsePrintRule of 'a * 'a;
   413 
   414 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   415   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   416   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   417 
   418 fun parse_rule (ParseRule pats) = SOME pats
   419   | parse_rule (PrintRule _) = NONE
   420   | parse_rule (ParsePrintRule pats) = SOME pats;
   421 
   422 fun print_rule (ParseRule _) = NONE
   423   | print_rule (PrintRule pats) = SOME (swap pats)
   424   | print_rule (ParsePrintRule pats) = SOME (swap pats);
   425 
   426 
   427 fun check_rule (rule as (lhs, rhs)) =
   428   (case Ast.rule_error rule of
   429     SOME msg =>
   430       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   431         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   432   | NONE => rule);
   433 
   434 
   435 fun read_pattern is_logtype syn (root, str) =
   436   let
   437     val Syntax {consts, ...} = syn;
   438 
   439     fun constify (ast as Ast.Constant _) = ast
   440       | constify (ast as Ast.Variable x) =
   441           if x mem consts orelse NameSpace.is_qualified x then Ast.Constant x
   442           else ast
   443       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   444   in
   445     (case read_asts is_logtype syn true root str of
   446       [ast] => constify ast
   447     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   448   end handle ERROR =>
   449     error ("The error(s) above occurred in translation pattern " ^
   450       quote str);
   451 
   452 
   453 fun prep_rules rd_pat raw_rules =
   454   let val rules = map (map_trrule rd_pat) raw_rules in
   455     (map check_rule (List.mapPartial parse_rule rules),
   456       map check_rule (List.mapPartial print_rule rules))
   457   end
   458 
   459 
   460 
   461 (** pretty terms, typs, sorts **)
   462 
   463 fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
   464   let
   465     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   466     val ast = t_to_ast (lookup_tr' print_trtab) t;
   467   in
   468     prt_t curried prtabs (lookup_tr' print_ast_trtab)
   469       (lookup_tokentr tokentrtab (! print_mode))
   470       (Ast.normalize_ast (lookup_ruletab print_ruletab) ast)
   471   end;
   472 
   473 val pretty_term = pretty_t Printer.term_to_ast Printer.pretty_term_ast;
   474 fun pretty_typ syn = pretty_t Printer.typ_to_ast Printer.pretty_typ_ast syn false;
   475 fun pretty_sort syn = pretty_t Printer.sort_to_ast Printer.pretty_typ_ast syn false;
   476 
   477 val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
   478 
   479 
   480 
   481 (** extend syntax **)
   482 
   483 fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
   484 fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
   485 
   486 val extend_type_gram   = ext_syntax Mixfix.syn_ext_types;
   487 val extend_const_gram  = ext_syntax' Mixfix.syn_ext_consts;
   488 val extend_consts      = ext_syntax SynExt.syn_ext_const_names;
   489 val extend_trfuns      = ext_syntax SynExt.syn_ext_trfuns;
   490 val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns;
   491 val extend_trrules_i   = ext_syntax SynExt.syn_ext_rules o prep_rules I;
   492 
   493 fun extend_trrules is_logtype syn rules =
   494   ext_syntax' (K SynExt.syn_ext_rules) (K false) default_mode
   495     (prep_rules (read_pattern is_logtype syn) rules);
   496 
   497 fun remove_const_gram is_logtype prmode decls =
   498   remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
   499 
   500 
   501 
   502 (*export parts of internal Syntax structures*)
   503 open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   504 
   505 end;
   506 
   507 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   508 open BasicSyntax;