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