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