src/Pure/Syntax/syntax.ML
author wenzelm
Mon Mar 09 16:12:39 1998 +0100 (1998-03-09)
changeset 4703 a50ab39756db
parent 4618 731bed12f762
child 4887 bbc13af86c16
permissions -rw-r--r--
adapted to symbols, scan;
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@260
     8
signature BASIC_SYNTAX =
wenzelm@2383
     9
sig
wenzelm@260
    10
  include AST0
wenzelm@556
    11
  include SYN_TRANS0
wenzelm@556
    12
  include MIXFIX0
wenzelm@260
    13
  include PRINTER0
wenzelm@2383
    14
end;
wenzelm@260
    15
clasohm@0
    16
signature SYNTAX =
wenzelm@2383
    17
sig
wenzelm@260
    18
  include AST1
clasohm@0
    19
  include LEXICON0
wenzelm@237
    20
  include SYN_EXT0
clasohm@0
    21
  include TYPE_EXT0
wenzelm@556
    22
  include SYN_TRANS1
wenzelm@556
    23
  include MIXFIX1
clasohm@0
    24
  include PRINTER0
wenzelm@1158
    25
  datatype 'a trrule =
wenzelm@3526
    26
    ParseRule of 'a * 'a |
wenzelm@3526
    27
    PrintRule of 'a * 'a |
wenzelm@3526
    28
    ParsePrintRule of 'a * 'a
wenzelm@556
    29
  type syntax
wenzelm@556
    30
  val extend_log_types: syntax -> string list -> syntax
wenzelm@556
    31
  val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax
wenzelm@2366
    32
  val extend_const_gram: syntax -> string * bool -> (string * typ * mixfix) list -> syntax
wenzelm@556
    33
  val extend_consts: syntax -> string list -> syntax
wenzelm@556
    34
  val extend_trfuns: syntax ->
wenzelm@556
    35
    (string * (ast list -> ast)) list *
wenzelm@556
    36
    (string * (term list -> term)) list *
wenzelm@556
    37
    (string * (term list -> term)) list *
wenzelm@556
    38
    (string * (ast list -> ast)) list -> syntax
wenzelm@4145
    39
  val extend_trfunsT: syntax -> (string * (bool -> typ -> term list -> term)) list -> syntax
wenzelm@2700
    40
  val extend_tokentrfuns: syntax -> (string * string * (string -> string * int)) list -> syntax
wenzelm@1158
    41
  val extend_trrules: syntax -> (string * string) trrule list -> syntax
wenzelm@1158
    42
  val extend_trrules_i: syntax -> ast trrule list -> syntax
wenzelm@4618
    43
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
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@3779
    52
  val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> string -> typ
wenzelm@556
    53
  val simple_read_typ: string -> typ
wenzelm@3779
    54
  val pretty_term: syntax -> bool -> term -> Pretty.T
wenzelm@556
    55
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@3779
    56
  val pretty_sort: syntax -> sort -> Pretty.T
wenzelm@3782
    57
  val simple_str_of_sort: sort -> string
wenzelm@556
    58
  val simple_string_of_typ: typ -> string
wenzelm@556
    59
  val simple_pprint_typ: typ -> pprint_args -> unit
clasohm@882
    60
  val ambiguity_level: int ref
wenzelm@2383
    61
end;
clasohm@0
    62
paulson@1511
    63
structure Syntax : SYNTAX =
clasohm@0
    64
struct
clasohm@0
    65
paulson@1511
    66
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
wenzelm@237
    67
wenzelm@237
    68
wenzelm@237
    69
(** tables of translation functions **)
wenzelm@237
    70
wenzelm@2383
    71
(*does not subsume typed print translations*)
wenzelm@2504
    72
type 'a trtab = (('a list -> 'a) * stamp) Symtab.table;
wenzelm@237
    73
wenzelm@383
    74
val dest_trtab = Symtab.dest;
wenzelm@237
    75
wenzelm@237
    76
fun lookup_trtab tab c =
wenzelm@237
    77
  apsome fst (Symtab.lookup (tab, c));
clasohm@0
    78
clasohm@0
    79
wenzelm@237
    80
(* empty, extend, merge trtabs *)
wenzelm@237
    81
wenzelm@383
    82
fun err_dup_trfuns name cs =
wenzelm@383
    83
  error ("More than one " ^ name ^ " for " ^ commas_quote cs);
wenzelm@237
    84
wenzelm@4487
    85
val empty_trtab = Symtab.empty;
wenzelm@237
    86
wenzelm@237
    87
fun extend_trtab tab trfuns name =
wenzelm@4487
    88
  Symtab.extend (tab, map (fn (c, f) => (c, (f, stamp ()))) trfuns)
wenzelm@383
    89
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    90
wenzelm@237
    91
fun merge_trtabs tab1 tab2 name =
wenzelm@237
    92
  Symtab.merge eq_snd (tab1, tab2)
wenzelm@383
    93
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    94
wenzelm@237
    95
wenzelm@237
    96
wenzelm@2700
    97
(** tables of token translation functions **)
wenzelm@2700
    98
wenzelm@2700
    99
fun lookup_tokentr tabs modes =
wenzelm@4496
   100
  let val trs = gen_distinct eq_fst (flat (map (assocs tabs) (modes @ [""])))
wenzelm@2700
   101
  in fn c => apsome fst (assoc (trs, c)) end;
wenzelm@2700
   102
wenzelm@2700
   103
fun merge_tokentrtabs tabs1 tabs2 =
wenzelm@2700
   104
  let
wenzelm@2700
   105
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
wenzelm@2700
   106
wenzelm@4703
   107
    fun name (s, _) = implode (tl (Symbol.explode s));
wenzelm@2706
   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@2706
   118
            quote mode ^ " for " ^ commas_quote (map name 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@4487
   144
  if Symtab.is_empty 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@4487
   150
val empty_ruletab = Symtab.empty;
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@4703
   165
    lexicon: Scan.lexicon,
clasohm@764
   166
    logtypes: string list,
wenzelm@18
   167
    gram: gram,
wenzelm@18
   168
    consts: string list,
wenzelm@2913
   169
    prmodes: string list,
wenzelm@237
   170
    parse_ast_trtab: ast trtab,
wenzelm@237
   171
    parse_ruletab: ruletab,
wenzelm@237
   172
    parse_trtab: term trtab,
wenzelm@4145
   173
    print_trtab: ((bool -> typ -> term list -> term) * stamp) Symtab.table,
wenzelm@237
   174
    print_ruletab: ruletab,
wenzelm@237
   175
    print_ast_trtab: ast trtab,
wenzelm@2700
   176
    tokentrtab: (string * (string * ((string -> string * int) * stamp)) list) list,
wenzelm@2913
   177
    prtabs: prtabs}
clasohm@0
   178
wenzelm@18
   179
wenzelm@237
   180
(* empty_syntax *)
wenzelm@18
   181
wenzelm@237
   182
val empty_syntax =
wenzelm@237
   183
  Syntax {
wenzelm@4703
   184
    lexicon = Scan.empty_lexicon,
clasohm@764
   185
    logtypes = [],
wenzelm@171
   186
    gram = empty_gram,
wenzelm@167
   187
    consts = [],
wenzelm@2913
   188
    prmodes = [],
wenzelm@237
   189
    parse_ast_trtab = empty_trtab,
wenzelm@237
   190
    parse_ruletab = empty_ruletab,
wenzelm@237
   191
    parse_trtab = empty_trtab,
wenzelm@237
   192
    print_trtab = empty_trtab,
wenzelm@237
   193
    print_ruletab = empty_ruletab,
wenzelm@237
   194
    print_ast_trtab = empty_trtab,
wenzelm@2700
   195
    tokentrtab = [],
wenzelm@2913
   196
    prtabs = empty_prtabs}
wenzelm@167
   197
wenzelm@167
   198
wenzelm@237
   199
(* extend_syntax *)
wenzelm@167
   200
wenzelm@2366
   201
fun extend_syntax (mode, inout) (Syntax tabs) syn_ext =
wenzelm@167
   202
  let
wenzelm@2913
   203
    val {lexicon, logtypes = logtypes1, gram, consts = consts1, prmodes = prmodes1,
wenzelm@2366
   204
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@2700
   205
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@2913
   206
    val SynExt {logtypes = logtypes2, xprods, consts = consts2, prmodes = prmodes2,
wenzelm@2913
   207
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@2700
   208
      print_ast_translation, token_translation} = syn_ext;
wenzelm@167
   209
  in
wenzelm@237
   210
    Syntax {
wenzelm@4703
   211
      lexicon = if inout then Scan.extend_lexicon lexicon (delims_of xprods) else lexicon,
clasohm@764
   212
      logtypes = extend_list logtypes1 logtypes2,
wenzelm@2366
   213
      gram = if inout then extend_gram gram xprods else gram,
wenzelm@167
   214
      consts = consts2 union consts1,
wenzelm@2913
   215
      prmodes = (mode ins prmodes2) union prmodes1,
wenzelm@167
   216
      parse_ast_trtab =
wenzelm@167
   217
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
wenzelm@167
   218
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
wenzelm@167
   219
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
wenzelm@167
   220
      print_trtab = extend_trtab print_trtab print_translation "print translation",
wenzelm@167
   221
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@237
   222
      print_ast_trtab =
wenzelm@237
   223
        extend_trtab print_ast_trtab print_ast_translation "print ast translation",
wenzelm@2700
   224
      tokentrtab = extend_tokentrtab tokentrtab token_translation,
wenzelm@2383
   225
      prtabs = extend_prtabs prtabs mode xprods}
wenzelm@18
   226
  end;
wenzelm@18
   227
wenzelm@18
   228
wenzelm@237
   229
(* merge_syntaxes *)
clasohm@0
   230
wenzelm@237
   231
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
clasohm@0
   232
  let
wenzelm@2913
   233
    val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
wenzelm@2913
   234
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
wenzelm@237
   235
      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
wenzelm@237
   236
      print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
wenzelm@2700
   237
      tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   238
wenzelm@2913
   239
    val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
wenzelm@2913
   240
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
wenzelm@237
   241
      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
wenzelm@237
   242
      print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
wenzelm@2700
   243
      tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   244
  in
wenzelm@237
   245
    Syntax {
wenzelm@4703
   246
      lexicon = Scan.merge_lexicons lexicon1 lexicon2,
clasohm@764
   247
      logtypes = merge_lists logtypes1 logtypes2,
wenzelm@237
   248
      gram = merge_grams gram1 gram2,
wenzelm@237
   249
      consts = merge_lists consts1 consts2,
wenzelm@2913
   250
      prmodes = merge_lists prmodes1 prmodes2,
wenzelm@237
   251
      parse_ast_trtab =
wenzelm@237
   252
        merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
wenzelm@237
   253
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@237
   254
      parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
wenzelm@237
   255
      print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
wenzelm@237
   256
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@237
   257
      print_ast_trtab =
wenzelm@237
   258
        merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
wenzelm@2700
   259
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
wenzelm@2383
   260
      prtabs = merge_prtabs prtabs1 prtabs2}
clasohm@0
   261
  end;
clasohm@0
   262
clasohm@0
   263
wenzelm@260
   264
(* type_syn *)
wenzelm@260
   265
wenzelm@2700
   266
val type_syn =
wenzelm@2700
   267
  extend_syntax ("", true) empty_syntax type_ext;
wenzelm@2700
   268
wenzelm@2366
   269
val pure_syn = extend_syntax ("", true) type_syn pure_ext;
wenzelm@260
   270
clasohm@0
   271
wenzelm@237
   272
(** inspect syntax **)
clasohm@0
   273
wenzelm@260
   274
fun pretty_strs_qs name strs =
wenzelm@260
   275
  Pretty.strs (name :: map quote (sort_strings strs));
clasohm@0
   276
wenzelm@18
   277
wenzelm@18
   278
(* print_gram *)
clasohm@0
   279
wenzelm@237
   280
fun print_gram (Syntax tabs) =
clasohm@0
   281
  let
wenzelm@2913
   282
    val {lexicon, logtypes, prmodes, gram, prtabs, ...} = tabs;
wenzelm@2913
   283
    val prmodes' = sort_strings (filter_out (equal "") prmodes);
clasohm@0
   284
  in
wenzelm@4703
   285
    Pretty.writeln (pretty_strs_qs "lexicon:" (map implode (Scan.dest_lexicon lexicon)));
clasohm@764
   286
    Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
wenzelm@2202
   287
    Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram));
wenzelm@2913
   288
    Pretty.writeln (pretty_strs_qs "print modes:" prmodes')
clasohm@0
   289
  end;
clasohm@0
   290
clasohm@0
   291
wenzelm@18
   292
(* print_trans *)
clasohm@0
   293
wenzelm@237
   294
fun print_trans (Syntax tabs) =
clasohm@0
   295
  let
wenzelm@260
   296
    fun pretty_trtab name tab =
wenzelm@260
   297
      pretty_strs_qs name (map fst (dest_trtab tab));
clasohm@0
   298
wenzelm@260
   299
    fun pretty_ruletab name tab =
wenzelm@260
   300
      Pretty.big_list name (map pretty_rule (dest_ruletab tab));
clasohm@0
   301
wenzelm@4703
   302
    fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs);
wenzelm@4703
   303
wenzelm@237
   304
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@4703
   305
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
clasohm@0
   306
  in
wenzelm@260
   307
    Pretty.writeln (pretty_strs_qs "consts:" consts);
wenzelm@260
   308
    Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
wenzelm@260
   309
    Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
wenzelm@260
   310
    Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
wenzelm@260
   311
    Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
wenzelm@260
   312
    Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
wenzelm@4703
   313
    Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab);
wenzelm@4703
   314
    Pretty.writeln (Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab))
clasohm@0
   315
  end;
clasohm@0
   316
clasohm@0
   317
clasohm@0
   318
(* print_syntax *)
clasohm@0
   319
wenzelm@237
   320
fun print_syntax syn = (print_gram syn; print_trans syn);
clasohm@0
   321
clasohm@0
   322
clasohm@0
   323
wenzelm@237
   324
(** read **)
wenzelm@18
   325
wenzelm@237
   326
(* test_read *)
wenzelm@18
   327
wenzelm@237
   328
fun test_read (Syntax tabs) root str =
wenzelm@18
   329
  let
wenzelm@237
   330
    val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   331
wenzelm@4703
   332
    val chars = Symbol.explode str;
wenzelm@2366
   333
    val toks = tokenize lexicon false chars;
clasohm@624
   334
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   335
wenzelm@383
   336
    fun show_pt pt =
wenzelm@383
   337
      let
wenzelm@383
   338
        val raw_ast = pt_to_ast (K None) pt;
wenzelm@383
   339
        val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@383
   340
        val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
wenzelm@383
   341
        val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
wenzelm@383
   342
      in () end;
wenzelm@383
   343
  in
wenzelm@383
   344
    seq show_pt (parse gram root toks)
wenzelm@383
   345
  end;
wenzelm@18
   346
wenzelm@18
   347
wenzelm@260
   348
(* read_ast *)
wenzelm@260
   349
clasohm@882
   350
val ambiguity_level = ref 1;
clasohm@882
   351
clasohm@865
   352
fun read_asts (Syntax tabs) xids root str =
wenzelm@260
   353
  let
clasohm@764
   354
    val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
clasohm@764
   355
    val root' = if root mem logtypes then logic else root;
wenzelm@4703
   356
    val chars = Symbol.explode str;
wenzelm@2366
   357
    val pts = parse gram root' (tokenize lexicon xids chars);
clasohm@330
   358
wenzelm@3739
   359
    fun show_pt pt =
wenzelm@3739
   360
      warning (Pretty.string_of (pretty_ast (pt_to_ast (K None) pt)));
wenzelm@260
   361
  in
wenzelm@888
   362
    if length pts > ! ambiguity_level then
berghofe@1580
   363
      (warning ("Ambiguous input " ^ quote str);
wenzelm@3526
   364
       warning "produces the following parse trees:";
clasohm@882
   365
       seq show_pt pts)
clasohm@624
   366
    else ();
clasohm@624
   367
    map (pt_to_ast (lookup_trtab parse_ast_trtab)) pts
wenzelm@260
   368
  end;
wenzelm@260
   369
wenzelm@260
   370
wenzelm@237
   371
(* read *)
clasohm@0
   372
wenzelm@237
   373
fun read (syn as Syntax tabs) ty str =
clasohm@0
   374
  let
wenzelm@237
   375
    val {parse_ruletab, parse_trtab, ...} = tabs;
clasohm@865
   376
    val asts = read_asts syn false (typ_to_nonterm ty) str;
clasohm@0
   377
  in
clasohm@624
   378
    map (ast_to_term (lookup_trtab parse_trtab))
clasohm@624
   379
      (map (normalize_ast (lookup_ruletab parse_ruletab)) asts)
clasohm@0
   380
  end;
clasohm@0
   381
clasohm@0
   382
wenzelm@237
   383
(* read types *)
clasohm@0
   384
wenzelm@3779
   385
fun read_typ syn get_sort str =
wenzelm@888
   386
  (case read syn typeT str of
wenzelm@3779
   387
    [t] => typ_of_term (get_sort (raw_term_sorts t)) t
wenzelm@2585
   388
  | _ => error "read_typ: ambiguous type syntax");
wenzelm@144
   389
wenzelm@2585
   390
fun simple_read_typ str =
wenzelm@2585
   391
  let fun get_sort env xi = if_none (assoc (env, xi)) [] in
wenzelm@3779
   392
    read_typ type_syn get_sort str
wenzelm@2585
   393
  end;
wenzelm@144
   394
wenzelm@144
   395
wenzelm@18
   396
wenzelm@1158
   397
(** prepare translation rules **)
wenzelm@1158
   398
wenzelm@1158
   399
datatype 'a trrule =
wenzelm@3526
   400
  ParseRule of 'a * 'a |
wenzelm@3526
   401
  PrintRule of 'a * 'a |
wenzelm@3526
   402
  ParsePrintRule of 'a * 'a;
wenzelm@888
   403
wenzelm@4618
   404
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
wenzelm@4618
   405
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
wenzelm@4618
   406
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
wenzelm@1158
   407
wenzelm@3526
   408
fun parse_rule (ParseRule pats) = Some pats
wenzelm@3526
   409
  | parse_rule (PrintRule _) = None
wenzelm@3526
   410
  | parse_rule (ParsePrintRule pats) = Some pats;
wenzelm@1158
   411
wenzelm@3526
   412
fun print_rule (ParseRule _) = None
wenzelm@3526
   413
  | print_rule (PrintRule pats) = Some (swap pats)
wenzelm@3526
   414
  | print_rule (ParsePrintRule pats) = Some (swap pats);
wenzelm@1158
   415
wenzelm@1158
   416
wenzelm@1158
   417
fun check_rule (rule as (lhs, rhs)) =
wenzelm@1158
   418
  (case rule_error rule of
wenzelm@1158
   419
    Some msg =>
wenzelm@1158
   420
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@1158
   421
        str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@1158
   422
  | None => rule);
wenzelm@888
   423
wenzelm@888
   424
wenzelm@888
   425
fun read_pattern syn (root, str) =
wenzelm@18
   426
  let
wenzelm@237
   427
    val Syntax {consts, ...} = syn;
wenzelm@18
   428
wenzelm@888
   429
    fun constify (ast as Constant _) = ast
wenzelm@888
   430
      | constify (ast as Variable x) =
wenzelm@3830
   431
          if x mem consts orelse NameSpace.qualified x then Constant x
wenzelm@3830
   432
          else ast
wenzelm@888
   433
      | constify (Appl asts) = Appl (map constify asts);
wenzelm@18
   434
  in
wenzelm@888
   435
    (case read_asts syn true root str of
wenzelm@888
   436
      [ast] => constify ast
wenzelm@888
   437
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
wenzelm@888
   438
  end handle ERROR =>
wenzelm@888
   439
    error ("The error(s) above occurred in translation pattern " ^
wenzelm@888
   440
      quote str);
clasohm@0
   441
wenzelm@556
   442
wenzelm@1158
   443
fun prep_rules rd_pat raw_rules =
wenzelm@4618
   444
  let val rules = map (map_trrule rd_pat) raw_rules in
wenzelm@3526
   445
    (map check_rule (mapfilter parse_rule rules),
wenzelm@3526
   446
      map check_rule (mapfilter print_rule rules))
wenzelm@1158
   447
  end
wenzelm@18
   448
wenzelm@18
   449
wenzelm@18
   450
wenzelm@3779
   451
(** pretty terms, typs, sorts **)
wenzelm@18
   452
wenzelm@3779
   453
fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
clasohm@0
   454
  let
wenzelm@2700
   455
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
wenzelm@237
   456
    val ast = t_to_ast (lookup_trtab print_trtab) t;
clasohm@0
   457
  in
wenzelm@2202
   458
    prt_t curried prtabs (lookup_trtab print_ast_trtab)
wenzelm@2700
   459
      (lookup_tokentr tokentrtab (! print_mode))
wenzelm@237
   460
      (normalize_ast (lookup_ruletab print_ruletab) ast)
clasohm@0
   461
  end;
clasohm@0
   462
clasohm@0
   463
val pretty_term = pretty_t term_to_ast pretty_term_ast;
wenzelm@3779
   464
fun pretty_typ syn = pretty_t typ_to_ast pretty_typ_ast syn false;
wenzelm@3779
   465
fun pretty_sort syn = pretty_t sort_to_ast pretty_typ_ast syn false;
clasohm@0
   466
wenzelm@3782
   467
val simple_str_of_sort = Pretty.str_of o pretty_sort type_syn;
wenzelm@3779
   468
val simple_string_of_typ = Pretty.string_of o (pretty_typ type_syn);
wenzelm@260
   469
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
clasohm@0
   470
clasohm@0
   471
clasohm@0
   472
wenzelm@383
   473
(** extend syntax (external interfaces) **)
wenzelm@383
   474
wenzelm@2202
   475
fun ext_syntax mk_syn_ext prmode (syn as Syntax {logtypes, ...}) decls =
wenzelm@2202
   476
  extend_syntax prmode syn (mk_syn_ext logtypes decls);
wenzelm@383
   477
wenzelm@167
   478
clasohm@764
   479
fun extend_log_types syn logtypes =
wenzelm@2366
   480
  extend_syntax ("", true) syn (syn_ext_logtypes logtypes);
wenzelm@383
   481
wenzelm@2366
   482
val extend_type_gram = ext_syntax syn_ext_types ("", true);
wenzelm@383
   483
wenzelm@2202
   484
fun extend_const_gram syn prmode = ext_syntax syn_ext_consts prmode syn;
wenzelm@383
   485
wenzelm@2366
   486
val extend_consts = ext_syntax syn_ext_const_names ("", true);
wenzelm@383
   487
wenzelm@2366
   488
val extend_trfuns = ext_syntax syn_ext_trfuns ("", true);
wenzelm@383
   489
wenzelm@2383
   490
val extend_trfunsT = ext_syntax syn_ext_trfunsT ("", true);
wenzelm@2383
   491
wenzelm@2700
   492
val extend_tokentrfuns = ext_syntax syn_ext_tokentrfuns ("", true);
wenzelm@2700
   493
wenzelm@1158
   494
fun extend_trrules syn rules =
wenzelm@2366
   495
  ext_syntax syn_ext_rules ("", true) syn (prep_rules (read_pattern syn) rules);
wenzelm@1158
   496
wenzelm@1158
   497
fun extend_trrules_i syn rules =
wenzelm@2366
   498
  ext_syntax syn_ext_rules ("", true) syn (prep_rules I rules);
wenzelm@2366
   499
wenzelm@2366
   500
clasohm@0
   501
end;