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