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