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