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