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