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