src/Pure/Syntax/syntax.ML
author wenzelm
Mon Dec 29 14:29:34 1997 +0100 (1997-12-29)
changeset 4496 16187138463d
parent 4487 9b4c1db5aca1
child 4618 731bed12f762
permissions -rw-r--r--
removed distinct_fst_string;
     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 merge_syntaxes: syntax -> syntax -> syntax
    44   val type_syn: syntax
    45   val pure_syn: syntax
    46   val print_gram: syntax -> unit
    47   val print_trans: syntax -> unit
    48   val print_syntax: syntax -> unit
    49   val test_read: syntax -> string -> string -> unit
    50   val read: syntax -> typ -> string -> term list
    51   val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> string -> typ
    52   val simple_read_typ: string -> typ
    53   val pretty_term: syntax -> bool -> term -> Pretty.T
    54   val pretty_typ: syntax -> typ -> Pretty.T
    55   val pretty_sort: syntax -> sort -> Pretty.T
    56   val simple_str_of_sort: sort -> string
    57   val simple_string_of_typ: typ -> string
    58   val simple_pprint_typ: typ -> pprint_args -> unit
    59   val ambiguity_level: int ref
    60 end;
    61 
    62 structure Syntax : SYNTAX =
    63 struct
    64 
    65 open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
    66 
    67 
    68 (** tables of translation functions **)
    69 
    70 (*does not subsume typed print translations*)
    71 type 'a trtab = (('a list -> 'a) * stamp) Symtab.table;
    72 
    73 val dest_trtab = Symtab.dest;
    74 
    75 fun lookup_trtab tab c =
    76   apsome fst (Symtab.lookup (tab, c));
    77 
    78 
    79 (* empty, extend, merge trtabs *)
    80 
    81 fun err_dup_trfuns name cs =
    82   error ("More than one " ^ name ^ " for " ^ commas_quote cs);
    83 
    84 val empty_trtab = Symtab.empty;
    85 
    86 fun extend_trtab tab trfuns name =
    87   Symtab.extend (tab, map (fn (c, f) => (c, (f, stamp ()))) trfuns)
    88     handle Symtab.DUPS cs => err_dup_trfuns name cs;
    89 
    90 fun merge_trtabs tab1 tab2 name =
    91   Symtab.merge eq_snd (tab1, tab2)
    92     handle Symtab.DUPS cs => err_dup_trfuns name cs;
    93 
    94 
    95 
    96 (** tables of token translation functions **)
    97 
    98 fun lookup_tokentr tabs modes =
    99   let val trs = gen_distinct eq_fst (flat (map (assocs tabs) (modes @ [""])))
   100   in fn c => apsome fst (assoc (trs, c)) end;
   101 
   102 fun merge_tokentrtabs tabs1 tabs2 =
   103   let
   104     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   105 
   106     fun name (s, _) = implode (tl (explode s));
   107 
   108     fun merge mode =
   109       let
   110         val trs1 = assocs tabs1 mode;
   111         val trs2 = assocs tabs2 mode;
   112         val trs = gen_distinct eq_tr (trs1 @ trs2);
   113       in
   114         (case gen_duplicates eq_fst trs of
   115           [] => (mode, trs)
   116         | dups => error ("More than one token translation function in mode " ^
   117             quote mode ^ " for " ^ commas_quote (map name dups)))
   118       end;
   119   in
   120     map merge (distinct (map fst (tabs1 @ tabs2)))
   121   end;
   122 
   123 fun extend_tokentrtab tabs tokentrs =
   124   let
   125     fun ins_tokentr (ts, (m, c, f)) =
   126       overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
   127   in
   128     merge_tokentrtabs tabs (foldl ins_tokentr ([], tokentrs))
   129   end;
   130 
   131 
   132 
   133 (** tables of translation rules **)
   134 
   135 type ruletab = (ast * ast) list Symtab.table;
   136 
   137 fun dest_ruletab tab = flat (map snd (Symtab.dest tab));
   138 
   139 
   140 (* lookup_ruletab *)
   141 
   142 fun lookup_ruletab tab =
   143   if Symtab.is_empty tab then None
   144   else Some (fn a => Symtab.lookup_multi (tab, a));
   145 
   146 
   147 (* empty, extend, merge ruletabs *)
   148 
   149 val empty_ruletab = Symtab.empty;
   150 
   151 fun extend_ruletab tab rules =
   152   generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
   153     (map (fn r => (head_of_rule r, r)) (distinct rules));
   154 
   155 fun merge_ruletabs tab1 tab2 =
   156   generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
   157 
   158 
   159 
   160 (** datatype syntax **)
   161 
   162 datatype syntax =
   163   Syntax of {
   164     lexicon: lexicon,
   165     logtypes: string list,
   166     gram: gram,
   167     consts: string list,
   168     prmodes: string list,
   169     parse_ast_trtab: ast trtab,
   170     parse_ruletab: ruletab,
   171     parse_trtab: term trtab,
   172     print_trtab: ((bool -> typ -> term list -> term) * stamp) Symtab.table,
   173     print_ruletab: ruletab,
   174     print_ast_trtab: ast trtab,
   175     tokentrtab: (string * (string * ((string -> string * int) * stamp)) list) list,
   176     prtabs: prtabs}
   177 
   178 
   179 (* empty_syntax *)
   180 
   181 val empty_syntax =
   182   Syntax {
   183     lexicon = empty_lexicon,
   184     logtypes = [],
   185     gram = empty_gram,
   186     consts = [],
   187     prmodes = [],
   188     parse_ast_trtab = empty_trtab,
   189     parse_ruletab = empty_ruletab,
   190     parse_trtab = empty_trtab,
   191     print_trtab = empty_trtab,
   192     print_ruletab = empty_ruletab,
   193     print_ast_trtab = empty_trtab,
   194     tokentrtab = [],
   195     prtabs = empty_prtabs}
   196 
   197 
   198 (* extend_syntax *)
   199 
   200 fun extend_syntax (mode, inout) (Syntax tabs) syn_ext =
   201   let
   202     val {lexicon, logtypes = logtypes1, gram, consts = consts1, prmodes = prmodes1,
   203       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   204       print_ast_trtab, tokentrtab, prtabs} = tabs;
   205     val SynExt {logtypes = logtypes2, xprods, consts = consts2, prmodes = prmodes2,
   206       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   207       print_ast_translation, token_translation} = syn_ext;
   208   in
   209     Syntax {
   210       lexicon = if inout then extend_lexicon lexicon (delims_of xprods) else lexicon,
   211       logtypes = extend_list logtypes1 logtypes2,
   212       gram = if inout then extend_gram gram xprods else gram,
   213       consts = consts2 union consts1,
   214       prmodes = (mode ins prmodes2) union prmodes1,
   215       parse_ast_trtab =
   216         extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
   217       parse_ruletab = extend_ruletab parse_ruletab parse_rules,
   218       parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
   219       print_trtab = extend_trtab print_trtab print_translation "print translation",
   220       print_ruletab = extend_ruletab print_ruletab print_rules,
   221       print_ast_trtab =
   222         extend_trtab print_ast_trtab print_ast_translation "print ast translation",
   223       tokentrtab = extend_tokentrtab tokentrtab token_translation,
   224       prtabs = extend_prtabs prtabs mode xprods}
   225   end;
   226 
   227 
   228 (* merge_syntaxes *)
   229 
   230 fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   231   let
   232     val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
   233       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   234       parse_trtab = parse_trtab1, print_trtab = print_trtab1,
   235       print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
   236       tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   237 
   238     val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
   239       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   240       parse_trtab = parse_trtab2, print_trtab = print_trtab2,
   241       print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
   242       tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   243   in
   244     Syntax {
   245       lexicon = merge_lexicons lexicon1 lexicon2,
   246       logtypes = merge_lists logtypes1 logtypes2,
   247       gram = merge_grams gram1 gram2,
   248       consts = merge_lists consts1 consts2,
   249       prmodes = merge_lists prmodes1 prmodes2,
   250       parse_ast_trtab =
   251         merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
   252       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   253       parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
   254       print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
   255       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   256       print_ast_trtab =
   257         merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
   258       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   259       prtabs = merge_prtabs prtabs1 prtabs2}
   260   end;
   261 
   262 
   263 (* type_syn *)
   264 
   265 val type_syn =
   266   extend_syntax ("", true) empty_syntax type_ext;
   267 
   268 val pure_syn = extend_syntax ("", true) type_syn pure_ext;
   269 
   270 
   271 (** inspect syntax **)
   272 
   273 fun pretty_strs_qs name strs =
   274   Pretty.strs (name :: map 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.writeln (pretty_strs_qs "lexicon:" (dest_lexicon lexicon));
   285     Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
   286     Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram));
   287     Pretty.writeln (pretty_strs_qs "print modes:" prmodes')
   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 (map fst (dest_trtab tab));
   297 
   298     fun pretty_ruletab name tab =
   299       Pretty.big_list name (map pretty_rule (dest_ruletab tab));
   300 
   301     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   302       print_ruletab, print_ast_trtab, ...} = tabs;
   303   in
   304     Pretty.writeln (pretty_strs_qs "consts:" consts);
   305     Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
   306     Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
   307     Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
   308     Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
   309     Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
   310     Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab)
   311   end;
   312 
   313 
   314 (* print_syntax *)
   315 
   316 fun print_syntax syn = (print_gram syn; print_trans syn);
   317 
   318 
   319 
   320 (** read **)
   321 
   322 (* test_read *)
   323 
   324 fun test_read (Syntax tabs) root str =
   325   let
   326     val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
   327 
   328     val chars = SymbolFont.read_charnames (explode str);
   329     val toks = tokenize lexicon false chars;
   330     val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
   331 
   332     fun show_pt pt =
   333       let
   334         val raw_ast = pt_to_ast (K None) pt;
   335         val _ = writeln ("raw: " ^ str_of_ast raw_ast);
   336         val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
   337         val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
   338       in () end;
   339   in
   340     seq show_pt (parse gram root toks)
   341   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 logic else root;
   352     val chars = SymbolFont.read_charnames (explode str);
   353     val pts = parse gram root' (tokenize lexicon xids chars);
   354 
   355     fun show_pt pt =
   356       warning (Pretty.string_of (pretty_ast (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 (pt_to_ast (lookup_trtab 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 (typ_to_nonterm ty) str;
   373   in
   374     map (ast_to_term (lookup_trtab parse_trtab))
   375       (map (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 typeT str of
   383     [t] => typ_of_term (get_sort (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_rule f (ParseRule (x, y)) = ParseRule (f x, f y)
   401   | map_rule f (PrintRule (x, y)) = PrintRule (f x, f y)
   402   | map_rule 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 rule_error rule of
   415     Some msg =>
   416       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   417         str_of_ast lhs ^ "  ->  " ^ 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 Constant _) = ast
   426       | constify (ast as Variable x) =
   427           if x mem consts orelse NameSpace.qualified x then Constant x
   428           else ast
   429       | constify (Appl asts) = 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_rule 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_trtab print_trtab) t;
   453   in
   454     prt_t curried prtabs (lookup_trtab print_ast_trtab)
   455       (lookup_tokentr tokentrtab (! print_mode))
   456       (normalize_ast (lookup_ruletab print_ruletab) ast)
   457   end;
   458 
   459 val pretty_term = pretty_t term_to_ast pretty_term_ast;
   460 fun pretty_typ syn = pretty_t typ_to_ast pretty_typ_ast syn false;
   461 fun pretty_sort syn = pretty_t sort_to_ast 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 (syn_ext_logtypes logtypes);
   477 
   478 val extend_type_gram = ext_syntax syn_ext_types ("", true);
   479 
   480 fun extend_const_gram syn prmode = ext_syntax syn_ext_consts prmode syn;
   481 
   482 val extend_consts = ext_syntax syn_ext_const_names ("", true);
   483 
   484 val extend_trfuns = ext_syntax syn_ext_trfuns ("", true);
   485 
   486 val extend_trfunsT = ext_syntax syn_ext_trfunsT ("", true);
   487 
   488 val extend_tokentrfuns = ext_syntax syn_ext_tokentrfuns ("", true);
   489 
   490 fun extend_trrules syn rules =
   491   ext_syntax syn_ext_rules ("", true) syn (prep_rules (read_pattern syn) rules);
   492 
   493 fun extend_trrules_i syn rules =
   494   ext_syntax syn_ext_rules ("", true) syn (prep_rules I rules);
   495 
   496 
   497 end;