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