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