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