src/Pure/Syntax/syntax.ML
author clasohm
Wed Jun 14 12:05:13 1995 +0200 (1995-06-14)
changeset 1147 57b5f55bf879
parent 922 196ca0973a6d
child 1158 96804ce95516
permissions -rw-r--r--
removed 'raw' productions from gram datatype; replaced mk_gram by add_prods;
completely changed the generation of internal grammars to reuse existing
ones in extend_gram
     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 infix |-> <-| <->;
     9 
    10 signature BASIC_SYNTAX =
    11 sig
    12   include AST0
    13   include SYN_TRANS0
    14   include MIXFIX0
    15   include PRINTER0
    16   type xrule
    17   val |-> : (string * string) * (string * string) -> xrule
    18   val <-| : (string * string) * (string * string) -> xrule
    19   val <-> : (string * string) * (string * string) -> xrule
    20 end;
    21 
    22 signature SYNTAX =
    23 sig
    24   include AST1
    25   include LEXICON0
    26   include SYN_EXT0
    27   include TYPE_EXT0
    28   include SYN_TRANS1
    29   include MIXFIX1
    30   include PRINTER0
    31   sharing type ast = Parser.SynExt.Ast.ast
    32   type xrule
    33   val |-> : (string * string) * (string * string) -> xrule
    34   val <-| : (string * string) * (string * string) -> xrule
    35   val <-> : (string * string) * (string * string) -> xrule
    36   type syntax
    37   val extend_log_types: syntax -> string list -> syntax
    38   val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax
    39   val extend_const_gram: syntax -> (string * typ * mixfix) list -> syntax
    40   val extend_consts: syntax -> string list -> syntax
    41   val extend_trfuns: syntax ->
    42     (string * (ast list -> ast)) list *
    43     (string * (term list -> term)) list *
    44     (string * (term list -> term)) list *
    45     (string * (ast list -> ast)) list -> syntax
    46   val extend_trrules: syntax -> xrule list -> syntax
    47   val merge_syntaxes: syntax -> syntax -> syntax
    48   val type_syn: syntax
    49   val pure_syn: syntax
    50   val print_gram: syntax -> unit
    51   val print_trans: syntax -> unit
    52   val print_syntax: syntax -> unit
    53   val test_read: syntax -> string -> string -> unit
    54   val read: syntax -> typ -> string -> term list
    55   val read_typ: syntax -> (indexname -> sort) -> string -> typ
    56   val simple_read_typ: string -> typ
    57   val pretty_term: bool -> syntax -> term -> Pretty.T
    58   val pretty_typ: syntax -> typ -> Pretty.T
    59   val string_of_term: bool -> syntax -> term -> string
    60   val string_of_typ: syntax -> typ -> 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 functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
    67   and SynTrans: SYN_TRANS and Mixfix: MIXFIX and Printer: PRINTER
    68   sharing Mixfix.SynExt = SynTrans.Parser.SynExt = TypeExt.SynExt = Printer.SynExt)
    69   : SYNTAX =
    70 struct
    71 
    72 structure SynExt = TypeExt.SynExt;
    73 structure Parser = SynTrans.Parser;
    74 structure Lexicon = Parser.Lexicon;
    75 open Lexicon SynExt SynExt.Ast Parser TypeExt SynTrans Mixfix Printer;
    76 
    77 
    78 (** tables of translation functions **)
    79 
    80 (*the ref serves as unique id*)
    81 type 'a trtab = (('a list -> 'a) * unit ref) Symtab.table;
    82 
    83 val dest_trtab = Symtab.dest;
    84 
    85 fun lookup_trtab tab c =
    86   apsome fst (Symtab.lookup (tab, c));
    87 
    88 
    89 (* empty, extend, merge trtabs *)
    90 
    91 fun err_dup_trfuns name cs =
    92   error ("More than one " ^ name ^ " for " ^ commas_quote cs);
    93 
    94 val empty_trtab = Symtab.null;
    95 
    96 fun extend_trtab tab trfuns name =
    97   Symtab.extend_new (tab, map (fn (c, f) => (c, (f, ref ()))) trfuns)
    98     handle Symtab.DUPS cs => err_dup_trfuns name cs;
    99 
   100 fun merge_trtabs tab1 tab2 name =
   101   Symtab.merge eq_snd (tab1, tab2)
   102     handle Symtab.DUPS cs => err_dup_trfuns name cs;
   103 
   104 
   105 
   106 (** tables of translation rules **)
   107 
   108 type ruletab = (ast * ast) list Symtab.table;
   109 
   110 fun dest_ruletab tab = flat (map snd (Symtab.dest tab));
   111 
   112 
   113 (* lookup_ruletab *)
   114 
   115 fun lookup_ruletab tab =
   116   if Symtab.is_null tab then None
   117   else Some (fn a => Symtab.lookup_multi (tab, a));
   118 
   119 
   120 (* empty, extend, merge ruletabs *)
   121 
   122 val empty_ruletab = Symtab.null;
   123 
   124 fun extend_ruletab tab rules =
   125   generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
   126     (map (fn r => (head_of_rule r, r)) (distinct rules));
   127 
   128 fun merge_ruletabs tab1 tab2 =
   129   generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
   130 
   131 
   132 
   133 (** datatype syntax **)
   134 
   135 datatype syntax =
   136   Syntax of {
   137     lexicon: lexicon,
   138     logtypes: string list,
   139     gram: gram,
   140     consts: string list,
   141     parse_ast_trtab: ast trtab,
   142     parse_ruletab: ruletab,
   143     parse_trtab: term trtab,
   144     print_trtab: term trtab,
   145     print_ruletab: ruletab,
   146     print_ast_trtab: ast trtab,
   147     prtab: prtab};
   148 
   149 
   150 (* empty_syntax *)
   151 
   152 val empty_syntax =
   153   Syntax {
   154     lexicon = empty_lexicon,
   155     logtypes = [],
   156     gram = empty_gram,
   157     consts = [],
   158     parse_ast_trtab = empty_trtab,
   159     parse_ruletab = empty_ruletab,
   160     parse_trtab = empty_trtab,
   161     print_trtab = empty_trtab,
   162     print_ruletab = empty_ruletab,
   163     print_ast_trtab = empty_trtab,
   164     prtab = empty_prtab};
   165 
   166 
   167 (* extend_syntax *)
   168 
   169 fun extend_syntax (Syntax tabs) syn_ext =
   170   let
   171     val {lexicon, logtypes = logtypes1, gram, consts = consts1, parse_ast_trtab,
   172       parse_ruletab, parse_trtab, print_trtab, print_ruletab, print_ast_trtab,
   173       prtab} = tabs;
   174     val SynExt {logtypes = logtypes2, xprods, consts = consts2, parse_ast_translation,
   175       parse_rules, parse_translation, print_translation, print_rules,
   176       print_ast_translation} = syn_ext;
   177   in
   178     Syntax {
   179       lexicon = extend_lexicon lexicon (delims_of xprods),
   180       logtypes = extend_list logtypes1 logtypes2,
   181       gram = extend_gram gram xprods,
   182       consts = consts2 union consts1,
   183       parse_ast_trtab =
   184         extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
   185       parse_ruletab = extend_ruletab parse_ruletab parse_rules,
   186       parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
   187       print_trtab = extend_trtab print_trtab print_translation "print translation",
   188       print_ruletab = extend_ruletab print_ruletab print_rules,
   189       print_ast_trtab =
   190         extend_trtab print_ast_trtab print_ast_translation "print ast translation",
   191       prtab = extend_prtab prtab xprods}
   192   end;
   193 
   194 
   195 (* merge_syntaxes *)
   196 
   197 fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   198   let
   199     val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
   200       parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   201       parse_trtab = parse_trtab1, print_trtab = print_trtab1,
   202       print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
   203       prtab = prtab1} = tabs1;
   204 
   205     val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
   206       parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   207       parse_trtab = parse_trtab2, print_trtab = print_trtab2,
   208       print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
   209       prtab = prtab2} = tabs2;
   210   in
   211     Syntax {
   212       lexicon = merge_lexicons lexicon1 lexicon2,
   213       logtypes = merge_lists logtypes1 logtypes2,
   214       gram = merge_grams gram1 gram2,
   215       consts = merge_lists consts1 consts2,
   216       parse_ast_trtab =
   217         merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
   218       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   219       parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
   220       print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
   221       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   222       print_ast_trtab =
   223         merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
   224       prtab = merge_prtabs prtab1 prtab2}
   225   end;
   226 
   227 
   228 (* type_syn *)
   229 
   230 val type_syn = extend_syntax empty_syntax type_ext;
   231 val pure_syn = extend_syntax type_syn pure_ext;
   232 
   233 
   234 (** inspect syntax **)
   235 
   236 fun pretty_strs_qs name strs =
   237   Pretty.strs (name :: map quote (sort_strings strs));
   238 
   239 
   240 (* print_gram *)
   241 
   242 fun print_gram (Syntax tabs) =
   243   let
   244     val {lexicon, logtypes, gram, ...} = tabs;
   245   in
   246     Pretty.writeln (pretty_strs_qs "lexicon:" (dest_lexicon lexicon));
   247     Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
   248     Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram))
   249   end;
   250 
   251 
   252 (* print_trans *)
   253 
   254 fun print_trans (Syntax tabs) =
   255   let
   256     fun pretty_trtab name tab =
   257       pretty_strs_qs name (map fst (dest_trtab tab));
   258 
   259     fun pretty_ruletab name tab =
   260       Pretty.big_list name (map pretty_rule (dest_ruletab tab));
   261 
   262     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   263       print_ruletab, print_ast_trtab, ...} = tabs;
   264   in
   265     Pretty.writeln (pretty_strs_qs "consts:" consts);
   266     Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
   267     Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
   268     Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
   269     Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
   270     Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
   271     Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab)
   272   end;
   273 
   274 
   275 (* print_syntax *)
   276 
   277 fun print_syntax syn = (print_gram syn; print_trans syn);
   278 
   279 
   280 
   281 (** read **)
   282 
   283 (* test_read *)
   284 
   285 fun test_read (Syntax tabs) root str =
   286   let
   287     val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
   288 
   289     val toks = tokenize lexicon false str;
   290     val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
   291 
   292     fun show_pt pt =
   293       let
   294         val raw_ast = pt_to_ast (K None) pt;
   295         val _ = writeln ("raw: " ^ str_of_ast raw_ast);
   296         val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
   297         val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
   298       in () end;
   299   in
   300     seq show_pt (parse gram root toks)
   301   end;
   302 
   303 
   304 (* read_ast *)
   305 
   306 val ambiguity_level = ref 1;
   307 
   308 fun read_asts (Syntax tabs) xids root str =
   309   let
   310     val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
   311     val root' = if root mem logtypes then logic else root;
   312     val pts = parse gram root' (tokenize lexicon xids str);
   313 
   314     fun show_pt pt = writeln (str_of_ast (pt_to_ast (K None) pt));
   315   in
   316     if length pts > ! ambiguity_level then
   317       (writeln ("Warning: Ambiguous input " ^ quote str);
   318        writeln "produces the following parse trees:";
   319        seq show_pt pts)
   320     else ();
   321     map (pt_to_ast (lookup_trtab parse_ast_trtab)) pts
   322   end;
   323 
   324 
   325 (* read *)
   326 
   327 fun read (syn as Syntax tabs) ty str =
   328   let
   329     val {parse_ruletab, parse_trtab, ...} = tabs;
   330     val asts = read_asts syn false (typ_to_nonterm ty) str;
   331   in
   332     map (ast_to_term (lookup_trtab parse_trtab))
   333       (map (normalize_ast (lookup_ruletab parse_ruletab)) asts)
   334   end;
   335 
   336 
   337 (* read types *)
   338 
   339 fun read_typ syn def_sort str =
   340   (case read syn typeT str of
   341     [t] => typ_of_term (raw_term_sorts t) def_sort t
   342   | _ => sys_error "read_typ: ambiguous type syntax");
   343 
   344 fun simple_read_typ str = read_typ type_syn (K []) str;
   345 
   346 
   347 (* read translation rules *)
   348 
   349 datatype 'a rule =
   350   op |-> of 'a * 'a |
   351   op <-| of 'a * 'a |
   352   op <-> of 'a * 'a;
   353 
   354 type xrule = (string * string) rule;
   355 
   356 
   357 fun read_pattern syn (root, str) =
   358   let
   359     val Syntax {consts, ...} = syn;
   360 
   361     fun constify (ast as Constant _) = ast
   362       | constify (ast as Variable x) =
   363           if x mem consts then Constant x else ast
   364       | constify (Appl asts) = Appl (map constify asts);
   365   in
   366     (case read_asts syn true root str of
   367       [ast] => constify ast
   368     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   369   end handle ERROR =>
   370     error ("The error(s) above occurred in translation pattern " ^
   371       quote str);
   372 
   373 
   374 fun check_rule (rule as (lhs, rhs)) =
   375   (case rule_error rule of
   376     Some msg =>
   377       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   378         str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
   379   | None => rule);
   380 
   381 
   382 fun read_xrules syn xrules =
   383   let
   384     fun map_rule f (x |-> y) = (f x |-> f y)
   385       | map_rule f (x <-| y) = (f x <-| f y)
   386       | map_rule f (x <-> y) = (f x <-> f y);
   387 
   388     fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
   389       | right_rule (xpat1 <-| xpat2) = None
   390       | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
   391 
   392     fun left_rule (xpat1 |-> xpat2) = None
   393       | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
   394       | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
   395 
   396     val rules = map (map_rule (read_pattern syn)) xrules;
   397   in
   398     (map check_rule (mapfilter right_rule rules),
   399       map check_rule (mapfilter left_rule rules))
   400   end;
   401 
   402 
   403 
   404 (** pretty terms or typs **)
   405 
   406 fun pretty_t t_to_ast pretty_t curried (syn as Syntax tabs) t =
   407   let
   408     val {print_trtab, print_ruletab, print_ast_trtab, prtab, ...} = tabs;
   409     val ast = t_to_ast (lookup_trtab print_trtab) t;
   410   in
   411     pretty_t curried prtab (lookup_trtab print_ast_trtab)
   412       (normalize_ast (lookup_ruletab print_ruletab) ast)
   413   end;
   414 
   415 val pretty_term = pretty_t term_to_ast pretty_term_ast;
   416 val pretty_typ = pretty_t typ_to_ast pretty_typ_ast false;
   417 
   418 fun string_of_term curried syn t =
   419   Pretty.string_of (pretty_term curried syn t);
   420 fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
   421 
   422 val simple_string_of_typ = string_of_typ type_syn;
   423 val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
   424 
   425 
   426 
   427 (** extend syntax (external interfaces) **)
   428 
   429 fun ext_syntax mk_syn_ext (syn as Syntax {logtypes, ...}) decls =
   430   extend_syntax syn (mk_syn_ext logtypes decls);
   431 
   432 
   433 fun extend_log_types syn logtypes =
   434   extend_syntax syn (syn_ext_logtypes logtypes);
   435 
   436 val extend_type_gram = ext_syntax syn_ext_types;
   437 
   438 val extend_const_gram = ext_syntax syn_ext_consts;
   439 
   440 val extend_consts = ext_syntax syn_ext_const_names;
   441 
   442 val extend_trfuns = ext_syntax syn_ext_trfuns;
   443 
   444 fun extend_trrules syn xrules =
   445   ext_syntax syn_ext_rules syn (read_xrules syn xrules);
   446 
   447 end;