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