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