src/Pure/Syntax/syntax.ML
author wenzelm
Thu Apr 22 10:59:41 2004 +0200 (2004-04-22)
changeset 14648 0e67b385a6df
parent 14598 7009f59711e3
child 14687 e089757b952a
permissions -rw-r--r--
tuned interfaces to accomodate advanced translation functions;
     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 extend_log_types: string list -> syntax -> syntax
    41   val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
    42   val extend_const_gram: 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: (string * string * (string -> string * real)) list -> syntax -> syntax
    50   val extend_trrules: syntax -> (string * string) trrule list -> syntax -> syntax
    51   val extend_trrules_i: ast trrule list -> syntax -> syntax
    52   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    53   val merge_syntaxes: syntax -> syntax -> syntax
    54   val type_syn: syntax
    55   val pure_syn: syntax
    56   val print_gram: syntax -> unit
    57   val print_trans: syntax -> unit
    58   val print_syntax: syntax -> unit
    59   val test_read: syntax -> string -> string -> unit
    60   val read: syntax -> typ -> string -> term list
    61   val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> (sort -> sort) ->
    62     string -> typ
    63   val read_sort: syntax -> string -> sort
    64   val pretty_term: syntax -> bool -> term -> Pretty.T
    65   val pretty_typ: syntax -> typ -> Pretty.T
    66   val pretty_sort: syntax -> sort -> Pretty.T
    67   val simple_str_of_sort: sort -> string
    68   val simple_string_of_typ: typ -> string
    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     logtypes: string list,
   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 
   182 (* empty_syntax *)
   183 
   184 val empty_syntax =
   185   Syntax {
   186     lexicon = Scan.empty_lexicon,
   187     logtypes = [],
   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) (Syntax tabs) syn_ext =
   204   let
   205     val {lexicon, logtypes = logtypes1, 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 {logtypes = logtypes2, 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       logtypes = merge_lists logtypes1 logtypes2,
   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 
   230 (* merge_syntaxes *)
   231 
   232 fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   233   let
   234     val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
   235       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   236       parse_trtab = parse_trtab1, print_trtab = print_trtab1,
   237       print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
   238       tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   239 
   240     val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
   241       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   242       parse_trtab = parse_trtab2, print_trtab = print_trtab2,
   243       print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
   244       tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   245   in
   246     Syntax {
   247       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   248       logtypes = merge_lists logtypes1 logtypes2,
   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 = extend_syntax ("", true) empty_syntax TypeExt.type_ext;
   267 val pure_syn = extend_syntax ("", true) type_syn 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, logtypes, 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.strs ("logtypes:" :: logtypes),
   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 (* test_read *)
   328 
   329 fun test_read (Syntax tabs) root str =
   330   let
   331     val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
   332 
   333     val chars = Symbol.explode str;
   334     val toks = Lexicon.tokenize lexicon false chars;
   335     val _ = writeln ("tokens: " ^ space_implode " " (map Lexicon.display_token toks));
   336 
   337     fun show_pt pt =
   338       let
   339         val raw_ast = SynTrans.pt_to_ast (K None) pt;
   340         val _ = writeln ("raw: " ^ Ast.str_of_ast raw_ast);
   341         val pre_ast = SynTrans.pt_to_ast (lookup_tr parse_ast_trtab) pt;
   342         val _ = Ast.normalize true true (lookup_ruletab parse_ruletab) pre_ast;
   343       in () end;
   344   in seq show_pt (Parser.parse gram root toks) end;
   345 
   346 
   347 (* read_ast *)
   348 
   349 val ambiguity_level = ref 1;
   350 
   351 fun read_asts (Syntax tabs) xids root str =
   352   let
   353     val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
   354     val root' = if root mem logtypes then SynExt.logic else root;
   355     val chars = Symbol.explode str;
   356     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   357 
   358     fun show_pt pt =
   359       warning (Pretty.string_of (Ast.pretty_ast (SynTrans.pt_to_ast (K None) pt)));
   360   in
   361     if length pts > ! ambiguity_level then
   362 	if ! ambiguity_is_error then
   363 	    error ("Ambiguous input " ^ quote str)
   364 	else
   365 	    (warning ("Ambiguous input " ^ quote str);
   366 	     warning "produces the following parse trees:";
   367 	     seq show_pt pts)
   368     else ();
   369     map (SynTrans.pt_to_ast (lookup_tr parse_ast_trtab)) pts
   370   end;
   371 
   372 
   373 (* read *)
   374 
   375 fun read (syn as Syntax tabs) ty str =
   376   let
   377     val {parse_ruletab, parse_trtab, ...} = tabs;
   378     val asts = read_asts syn false (SynExt.typ_to_nonterm ty) str;
   379   in
   380     map (SynTrans.ast_to_term (lookup_tr parse_trtab))
   381       (map (Ast.normalize_ast (lookup_ruletab parse_ruletab)) asts)
   382   end;
   383 
   384 
   385 (* read types *)
   386 
   387 fun read_typ syn get_sort map_sort str =
   388   (case read syn SynExt.typeT str of
   389     [t] => TypeExt.typ_of_term (get_sort (TypeExt.raw_term_sorts t)) map_sort t
   390   | _ => error "read_typ: ambiguous syntax");
   391 
   392 
   393 (* read sorts *)
   394 
   395 fun read_sort syn str =
   396   (case read syn TypeExt.sortT str of
   397     [t] => TypeExt.sort_of_term t
   398   | _ => error "read_sort: ambiguous syntax");
   399 
   400 
   401 
   402 (** prepare translation rules **)
   403 
   404 datatype 'a trrule =
   405   ParseRule of 'a * 'a |
   406   PrintRule of 'a * 'a |
   407   ParsePrintRule of 'a * 'a;
   408 
   409 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   410   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   411   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   412 
   413 fun parse_rule (ParseRule pats) = Some pats
   414   | parse_rule (PrintRule _) = None
   415   | parse_rule (ParsePrintRule pats) = Some pats;
   416 
   417 fun print_rule (ParseRule _) = None
   418   | print_rule (PrintRule pats) = Some (swap pats)
   419   | print_rule (ParsePrintRule pats) = Some (swap pats);
   420 
   421 
   422 fun check_rule (rule as (lhs, rhs)) =
   423   (case Ast.rule_error rule of
   424     Some msg =>
   425       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   426         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   427   | None => rule);
   428 
   429 
   430 fun read_pattern syn (root, str) =
   431   let
   432     val Syntax {consts, ...} = syn;
   433 
   434     fun constify (ast as Ast.Constant _) = ast
   435       | constify (ast as Ast.Variable x) =
   436           if x mem consts orelse NameSpace.is_qualified x then Ast.Constant x
   437           else ast
   438       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   439   in
   440     (case read_asts syn true root str of
   441       [ast] => constify ast
   442     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   443   end handle ERROR =>
   444     error ("The error(s) above occurred in translation pattern " ^
   445       quote str);
   446 
   447 
   448 fun prep_rules rd_pat raw_rules =
   449   let val rules = map (map_trrule rd_pat) raw_rules in
   450     (map check_rule (mapfilter parse_rule rules),
   451       map check_rule (mapfilter print_rule rules))
   452   end
   453 
   454 
   455 
   456 (** pretty terms, typs, sorts **)
   457 
   458 fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
   459   let
   460     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   461     val ast = t_to_ast (lookup_tr' print_trtab) t;
   462   in
   463     prt_t curried prtabs (lookup_tr' print_ast_trtab)
   464       (lookup_tokentr tokentrtab (! print_mode))
   465       (Ast.normalize_ast (lookup_ruletab print_ruletab) ast)
   466   end;
   467 
   468 val pretty_term = pretty_t Printer.term_to_ast Printer.pretty_term_ast;
   469 fun pretty_typ syn = pretty_t Printer.typ_to_ast Printer.pretty_typ_ast syn false;
   470 fun pretty_sort syn = pretty_t Printer.sort_to_ast Printer.pretty_typ_ast syn false;
   471 
   472 val simple_str_of_sort = Pretty.str_of o pretty_sort type_syn;
   473 val simple_string_of_typ = Pretty.string_of o (pretty_typ type_syn);
   474 val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
   475 
   476 
   477 
   478 (** extend syntax (external interfaces) **)
   479 
   480 fun ext_syntax mk_syn_ext prmode decls (syn as Syntax {logtypes, ...}) =
   481   extend_syntax prmode syn (mk_syn_ext logtypes decls);
   482 
   483 
   484 fun extend_log_types logtypes syn =
   485   extend_syntax ("", true) syn (SynExt.syn_ext_logtypes logtypes);
   486 
   487 val extend_type_gram = ext_syntax Mixfix.syn_ext_types ("", true);
   488 
   489 fun extend_const_gram prmode = ext_syntax Mixfix.syn_ext_consts prmode;
   490 
   491 val extend_consts = ext_syntax SynExt.syn_ext_const_names ("", true);
   492 
   493 val extend_trfuns = ext_syntax SynExt.syn_ext_trfuns ("", true);
   494 
   495 val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns ("", true);
   496 
   497 fun extend_trrules syn rules =
   498   ext_syntax SynExt.syn_ext_rules ("", true) (prep_rules (read_pattern syn) rules);
   499 
   500 fun extend_trrules_i rules = ext_syntax SynExt.syn_ext_rules ("", true) (prep_rules I rules);
   501 
   502 
   503 
   504 (** export parts of internal Syntax structures **)
   505 
   506 open TokenTrans Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   507 
   508 
   509 end;
   510 
   511 
   512 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   513 open BasicSyntax;