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