src/Pure/Syntax/syntax.ML
author wenzelm
Thu Feb 03 13:59:56 1994 +0100 (1994-02-03)
changeset 260 967813b8a7bf
parent 237 a7d3e712767a
child 330 2fda15dd1e0f
permissions -rw-r--r--
added simple_string_of_typ, simple_pprint_typ;
various internal changes;
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@260
     9
sig
wenzelm@260
    10
  include AST0
wenzelm@260
    11
  include SEXTENSION0
wenzelm@260
    12
  include PRINTER0
wenzelm@260
    13
end;
wenzelm@260
    14
clasohm@0
    15
signature SYNTAX =
clasohm@0
    16
sig
wenzelm@260
    17
  include AST1
clasohm@0
    18
  include LEXICON0
wenzelm@237
    19
  include SYN_EXT0
clasohm@0
    20
  include TYPE_EXT0
clasohm@0
    21
  include SEXTENSION1
clasohm@0
    22
  include PRINTER0
wenzelm@18
    23
  type syntax
wenzelm@144
    24
  val extend: syntax -> (string -> typ) -> string list * string list * sext -> syntax
wenzelm@167
    25
  val merge: string list -> syntax -> syntax -> syntax
wenzelm@260
    26
  val type_syn: syntax
wenzelm@18
    27
  val print_gram: syntax -> unit
wenzelm@18
    28
  val print_trans: syntax -> unit
wenzelm@18
    29
  val print_syntax: syntax -> unit
wenzelm@18
    30
  val test_read: syntax -> string -> string -> unit
wenzelm@18
    31
  val read: syntax -> typ -> string -> term
wenzelm@144
    32
  val read_typ: syntax -> (indexname -> sort) -> string -> typ
wenzelm@237
    33
  val simple_read_typ: string -> typ
wenzelm@18
    34
  val pretty_term: syntax -> term -> Pretty.T
wenzelm@18
    35
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@18
    36
  val string_of_term: syntax -> term -> string
wenzelm@18
    37
  val string_of_typ: syntax -> typ -> string
wenzelm@260
    38
  val simple_string_of_typ: typ -> string
wenzelm@260
    39
  val simple_pprint_typ: typ -> pprint_args -> unit
clasohm@0
    40
end;
clasohm@0
    41
clasohm@0
    42
functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
wenzelm@237
    43
  and SExtension: SEXTENSION and Printer: PRINTER
wenzelm@237
    44
  sharing SExtension.Parser.SynExt = TypeExt.SynExt = Printer.SynExt)(*: SYNTAX *) = (* FIXME *)
clasohm@0
    45
struct
clasohm@0
    46
wenzelm@237
    47
structure SynExt = TypeExt.SynExt;
wenzelm@237
    48
structure Parser = SExtension.Parser;
wenzelm@237
    49
structure Lexicon = Parser.Lexicon;
wenzelm@237
    50
open Lexicon SynExt SynExt.Ast Parser TypeExt SExtension Printer;
wenzelm@237
    51
wenzelm@237
    52
wenzelm@237
    53
(** tables of translation functions **)
wenzelm@237
    54
wenzelm@237
    55
(*the ref serves as unique id*)
wenzelm@237
    56
type 'a trtab = (('a list -> 'a) * unit ref) Symtab.table;
wenzelm@237
    57
wenzelm@237
    58
val dest_trtab = Symtab.alist_of;
wenzelm@237
    59
wenzelm@237
    60
fun lookup_trtab tab c =
wenzelm@237
    61
  apsome fst (Symtab.lookup (tab, c));
clasohm@0
    62
clasohm@0
    63
wenzelm@237
    64
(* empty, extend, merge trtabs *)
wenzelm@237
    65
wenzelm@237
    66
fun err_dup_trfun name c =
wenzelm@237
    67
  error ("More than one " ^ name ^ " for " ^ quote c);
wenzelm@237
    68
wenzelm@237
    69
val empty_trtab = Symtab.null;
wenzelm@237
    70
wenzelm@237
    71
fun extend_trtab tab trfuns name =
wenzelm@260
    72
  Symtab.extend (K false) (tab, map (fn (c, f) => (c, (f, ref ()))) trfuns)
wenzelm@237
    73
    handle Symtab.DUPLICATE c => err_dup_trfun name c;
wenzelm@237
    74
wenzelm@237
    75
fun merge_trtabs tab1 tab2 name =
wenzelm@237
    76
  Symtab.merge eq_snd (tab1, tab2)
wenzelm@237
    77
    handle Symtab.DUPLICATE c => err_dup_trfun name c;
wenzelm@237
    78
wenzelm@237
    79
wenzelm@237
    80
wenzelm@237
    81
(** tables of translation rules **)
wenzelm@237
    82
wenzelm@237
    83
type ruletab = (ast * ast) list Symtab.table;
wenzelm@237
    84
wenzelm@237
    85
fun dest_ruletab tab = flat (map snd (Symtab.alist_of tab));
wenzelm@237
    86
wenzelm@237
    87
wenzelm@237
    88
(* lookup_ruletab *)
wenzelm@237
    89
wenzelm@237
    90
fun lookup_ruletab tab =
wenzelm@237
    91
  if Symtab.is_null tab then None
wenzelm@237
    92
  else Some (fn a => Symtab.lookup_multi (tab, a));
wenzelm@237
    93
wenzelm@237
    94
wenzelm@237
    95
(* empty, extend, merge ruletabs *)
wenzelm@237
    96
wenzelm@237
    97
val empty_ruletab = Symtab.null;
wenzelm@237
    98
wenzelm@237
    99
fun extend_ruletab tab rules =
wenzelm@237
   100
  generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
wenzelm@237
   101
    (map (fn r => (head_of_rule r, r)) (distinct rules));
wenzelm@237
   102
wenzelm@237
   103
fun merge_ruletabs tab1 tab2 =
wenzelm@237
   104
  generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
clasohm@0
   105
clasohm@0
   106
clasohm@0
   107
clasohm@0
   108
(** datatype syntax **)
clasohm@0
   109
wenzelm@237
   110
datatype syntax =
wenzelm@237
   111
  Syntax of {
wenzelm@18
   112
    lexicon: lexicon,
wenzelm@18
   113
    roots: string list,
wenzelm@18
   114
    gram: gram,
wenzelm@18
   115
    consts: string list,
wenzelm@237
   116
    parse_ast_trtab: ast trtab,
wenzelm@237
   117
    parse_ruletab: ruletab,
wenzelm@237
   118
    parse_trtab: term trtab,
wenzelm@237
   119
    print_trtab: term trtab,
wenzelm@237
   120
    print_ruletab: ruletab,
wenzelm@237
   121
    print_ast_trtab: ast trtab,
wenzelm@18
   122
    prtab: prtab};
clasohm@0
   123
wenzelm@18
   124
wenzelm@237
   125
(* empty_syntax *)
wenzelm@18
   126
wenzelm@237
   127
val empty_syntax =
wenzelm@237
   128
  Syntax {
wenzelm@167
   129
    lexicon = empty_lexicon,
wenzelm@167
   130
    roots = [],
wenzelm@171
   131
    gram = empty_gram,
wenzelm@167
   132
    consts = [],
wenzelm@237
   133
    parse_ast_trtab = empty_trtab,
wenzelm@237
   134
    parse_ruletab = empty_ruletab,
wenzelm@237
   135
    parse_trtab = empty_trtab,
wenzelm@237
   136
    print_trtab = empty_trtab,
wenzelm@237
   137
    print_ruletab = empty_ruletab,
wenzelm@237
   138
    print_ast_trtab = empty_trtab,
wenzelm@167
   139
    prtab = empty_prtab};
wenzelm@167
   140
wenzelm@167
   141
wenzelm@237
   142
(* extend_syntax *)
wenzelm@167
   143
wenzelm@237
   144
fun extend_syntax (Syntax tabs) syn_ext =
wenzelm@167
   145
  let
wenzelm@167
   146
    val {lexicon, roots = roots1, gram, consts = consts1, parse_ast_trtab,
wenzelm@237
   147
      parse_ruletab, parse_trtab, print_trtab, print_ruletab, print_ast_trtab,
wenzelm@237
   148
      prtab} = tabs;
wenzelm@237
   149
    val SynExt {roots = roots2, xprods, consts = consts2, parse_ast_translation,
wenzelm@167
   150
      parse_rules, parse_translation, print_translation, print_rules,
wenzelm@237
   151
      print_ast_translation} = syn_ext;
wenzelm@167
   152
  in
wenzelm@237
   153
    Syntax {
wenzelm@237
   154
      lexicon = extend_lexicon lexicon (delims_of xprods),
wenzelm@237
   155
      roots = extend_list roots1 roots2,
wenzelm@237
   156
      gram = extend_gram gram roots2 xprods,
wenzelm@167
   157
      consts = consts2 union consts1,
wenzelm@167
   158
      parse_ast_trtab =
wenzelm@167
   159
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
wenzelm@167
   160
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
wenzelm@167
   161
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
wenzelm@167
   162
      print_trtab = extend_trtab print_trtab print_translation "print translation",
wenzelm@167
   163
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@237
   164
      print_ast_trtab =
wenzelm@237
   165
        extend_trtab print_ast_trtab print_ast_translation "print ast translation",
wenzelm@237
   166
      prtab = extend_prtab prtab xprods}
wenzelm@18
   167
  end;
wenzelm@18
   168
wenzelm@18
   169
wenzelm@237
   170
(* merge_syntaxes *)
clasohm@0
   171
wenzelm@237
   172
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
clasohm@0
   173
  let
wenzelm@237
   174
    val {lexicon = lexicon1, roots = roots1, gram = gram1, consts = consts1,
wenzelm@237
   175
      parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
wenzelm@237
   176
      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
wenzelm@237
   177
      print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
wenzelm@237
   178
      prtab = prtab1} = tabs1;
wenzelm@237
   179
wenzelm@237
   180
    val {lexicon = lexicon2, roots = roots2, gram = gram2, consts = consts2,
wenzelm@237
   181
      parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
wenzelm@237
   182
      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
wenzelm@237
   183
      print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
wenzelm@237
   184
      prtab = prtab2} = tabs2;
clasohm@0
   185
  in
wenzelm@237
   186
    Syntax {
wenzelm@237
   187
      lexicon = merge_lexicons lexicon1 lexicon2,
wenzelm@237
   188
      roots = merge_lists roots1 roots2,
wenzelm@237
   189
      gram = merge_grams gram1 gram2,
wenzelm@237
   190
      consts = merge_lists consts1 consts2,
wenzelm@237
   191
      parse_ast_trtab =
wenzelm@237
   192
        merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
wenzelm@237
   193
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@237
   194
      parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
wenzelm@237
   195
      print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
wenzelm@237
   196
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@237
   197
      print_ast_trtab =
wenzelm@237
   198
        merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
wenzelm@237
   199
      prtab = merge_prtabs prtab1 prtab2}
clasohm@0
   200
  end;
clasohm@0
   201
clasohm@0
   202
wenzelm@260
   203
(* type_syn *)
wenzelm@260
   204
wenzelm@260
   205
val type_syn = extend_syntax empty_syntax type_ext;
wenzelm@260
   206
wenzelm@260
   207
clasohm@0
   208
wenzelm@237
   209
(** inspect syntax **)
clasohm@0
   210
wenzelm@260
   211
fun pretty_strs_qs name strs =
wenzelm@260
   212
  Pretty.strs (name :: map quote (sort_strings strs));
clasohm@0
   213
wenzelm@18
   214
wenzelm@18
   215
(* print_gram *)
clasohm@0
   216
wenzelm@237
   217
fun print_gram (Syntax tabs) =
clasohm@0
   218
  let
wenzelm@237
   219
    val {lexicon, roots, gram, ...} = tabs;
clasohm@0
   220
  in
wenzelm@260
   221
    Pretty.writeln (pretty_strs_qs "lexicon:" (dest_lexicon lexicon));
wenzelm@260
   222
    Pretty.writeln (Pretty.strs ("roots:" :: roots));
wenzelm@260
   223
    Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram))
clasohm@0
   224
  end;
clasohm@0
   225
clasohm@0
   226
wenzelm@18
   227
(* print_trans *)
clasohm@0
   228
wenzelm@237
   229
fun print_trans (Syntax tabs) =
clasohm@0
   230
  let
wenzelm@260
   231
    fun pretty_trtab name tab =
wenzelm@260
   232
      pretty_strs_qs name (map fst (dest_trtab tab));
clasohm@0
   233
wenzelm@260
   234
    fun pretty_ruletab name tab =
wenzelm@260
   235
      Pretty.big_list name (map pretty_rule (dest_ruletab tab));
clasohm@0
   236
wenzelm@237
   237
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@237
   238
      print_ruletab, print_ast_trtab, ...} = tabs;
clasohm@0
   239
  in
wenzelm@260
   240
    Pretty.writeln (pretty_strs_qs "consts:" consts);
wenzelm@260
   241
    Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
wenzelm@260
   242
    Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
wenzelm@260
   243
    Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
wenzelm@260
   244
    Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
wenzelm@260
   245
    Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
wenzelm@260
   246
    Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab)
clasohm@0
   247
  end;
clasohm@0
   248
clasohm@0
   249
clasohm@0
   250
(* print_syntax *)
clasohm@0
   251
wenzelm@237
   252
fun print_syntax syn = (print_gram syn; print_trans syn);
clasohm@0
   253
clasohm@0
   254
clasohm@0
   255
wenzelm@237
   256
(** read **)
wenzelm@18
   257
wenzelm@237
   258
(* test_read *)
wenzelm@18
   259
wenzelm@237
   260
fun test_read (Syntax tabs) root str =
wenzelm@18
   261
  let
wenzelm@237
   262
    val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   263
wenzelm@18
   264
    val toks = tokenize lexicon false str;
wenzelm@18
   265
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   266
wenzelm@18
   267
    val pt = parse gram root toks;
wenzelm@18
   268
    val raw_ast = pt_to_ast (K None) pt;
wenzelm@18
   269
    val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@18
   270
wenzelm@237
   271
    val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
wenzelm@237
   272
    val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
wenzelm@18
   273
  in () end;
wenzelm@18
   274
wenzelm@18
   275
wenzelm@260
   276
(* read_ast *)
wenzelm@260
   277
wenzelm@260
   278
fun read_ast (Syntax tabs) xids root str =
wenzelm@260
   279
  let
wenzelm@260
   280
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@260
   281
  in
wenzelm@260
   282
    pt_to_ast (lookup_trtab parse_ast_trtab)
wenzelm@260
   283
      (parse gram root (tokenize lexicon xids str))
wenzelm@260
   284
  end;
wenzelm@260
   285
wenzelm@260
   286
wenzelm@237
   287
(* read *)
clasohm@0
   288
wenzelm@237
   289
fun read (syn as Syntax tabs) ty str =
clasohm@0
   290
  let
wenzelm@237
   291
    val {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@18
   292
    val ast = read_ast syn false (typ_to_nonterm ty) str;
clasohm@0
   293
  in
wenzelm@237
   294
    ast_to_term (lookup_trtab parse_trtab)
wenzelm@237
   295
      (normalize_ast (lookup_ruletab parse_ruletab) ast)
clasohm@0
   296
  end;
clasohm@0
   297
clasohm@0
   298
wenzelm@237
   299
(* read types *)
clasohm@0
   300
wenzelm@237
   301
fun read_typ syn def_sort str =
wenzelm@237
   302
  typ_of_term def_sort (read syn typeT str);
wenzelm@144
   303
wenzelm@237
   304
fun simple_read_typ str = read_typ type_syn (K []) str;
wenzelm@144
   305
wenzelm@144
   306
wenzelm@237
   307
(* read rules *)
wenzelm@18
   308
wenzelm@18
   309
fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) =
wenzelm@18
   310
  let
wenzelm@237
   311
    val Syntax {consts, ...} = syn;
wenzelm@18
   312
wenzelm@18
   313
    fun constantify (ast as Constant _) = ast
wenzelm@18
   314
      | constantify (ast as Variable x) =
wenzelm@18
   315
          if x mem consts then Constant x else ast
wenzelm@18
   316
      | constantify (Appl asts) = Appl (map constantify asts);
clasohm@0
   317
wenzelm@18
   318
    fun read_pat (root, str) =
wenzelm@18
   319
      constantify (read_ast syn true root str)
wenzelm@18
   320
        handle ERROR => error ("The error above occurred in " ^ quote str);
wenzelm@18
   321
wenzelm@18
   322
    val rule as (lhs, rhs) = (pairself read_pat) xrule;
wenzelm@18
   323
  in
wenzelm@18
   324
    (case rule_error rule of
wenzelm@18
   325
      Some msg =>
wenzelm@18
   326
        error ("Error in syntax translation rule: " ^ msg ^
wenzelm@18
   327
          "\nexternal: " ^ quote lhs_src ^ "  ->  " ^ quote rhs_src ^
wenzelm@18
   328
          "\ninternal: " ^ str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@18
   329
    | None => rule)
wenzelm@18
   330
  end;
clasohm@0
   331
wenzelm@18
   332
fun read_xrules syn xrules =
wenzelm@18
   333
  let
wenzelm@18
   334
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
wenzelm@18
   335
      | right_rule (xpat1 <-| xpat2) = None
wenzelm@18
   336
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
clasohm@0
   337
wenzelm@18
   338
    fun left_rule (xpat1 |-> xpat2) = None
wenzelm@18
   339
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
wenzelm@18
   340
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
wenzelm@18
   341
  in
wenzelm@18
   342
    (map (read_rule syn) (mapfilter right_rule xrules),
wenzelm@18
   343
     map (read_rule syn) (mapfilter left_rule xrules))
wenzelm@18
   344
  end;
wenzelm@18
   345
wenzelm@18
   346
wenzelm@18
   347
wenzelm@18
   348
(** pretty terms or typs **)
wenzelm@18
   349
wenzelm@237
   350
fun pretty_t t_to_ast pretty_t (syn as Syntax tabs) t =
clasohm@0
   351
  let
wenzelm@237
   352
    val {print_trtab, print_ruletab, print_ast_trtab, prtab, ...} = tabs;
wenzelm@237
   353
    val ast = t_to_ast (lookup_trtab print_trtab) t;
clasohm@0
   354
  in
wenzelm@237
   355
    pretty_t prtab (lookup_trtab print_ast_trtab)
wenzelm@237
   356
      (normalize_ast (lookup_ruletab print_ruletab) ast)
clasohm@0
   357
  end;
clasohm@0
   358
clasohm@0
   359
val pretty_term = pretty_t term_to_ast pretty_term_ast;
clasohm@0
   360
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
clasohm@0
   361
clasohm@0
   362
fun string_of_term syn t = Pretty.string_of (pretty_term syn t);
wenzelm@260
   363
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
clasohm@0
   364
wenzelm@260
   365
val simple_string_of_typ = string_of_typ type_syn;
wenzelm@260
   366
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
clasohm@0
   367
clasohm@0
   368
clasohm@0
   369
wenzelm@237
   370
(** build syntax **)
wenzelm@167
   371
wenzelm@237
   372
(* extend *)
wenzelm@167
   373
wenzelm@237
   374
fun extend syn0 read_ty (all_roots, xconsts, sext) =
wenzelm@237
   375
  let
wenzelm@237
   376
    val Syntax {roots, ...} = syn0;
wenzelm@174
   377
wenzelm@237
   378
    val syn1 = extend_syntax syn0
wenzelm@237
   379
      (syn_ext_of_sext (all_roots \\ roots) xconsts read_ty sext);
wenzelm@167
   380
wenzelm@237
   381
    val syn2 = extend_syntax syn1
wenzelm@237
   382
      (syn_ext_rules (read_xrules syn1 (xrules_of sext)));
wenzelm@237
   383
  in syn2 end;
clasohm@0
   384
clasohm@0
   385
clasohm@0
   386
(* merge *)
clasohm@0
   387
wenzelm@237
   388
fun merge all_roots syn1 syn2 =
wenzelm@167
   389
  let
wenzelm@237
   390
    val syn as (Syntax {roots, ...}) = merge_syntaxes syn1 syn2;
wenzelm@167
   391
  in
wenzelm@237
   392
    (case all_roots \\ roots of
wenzelm@237
   393
      [] => syn
wenzelm@260
   394
    | new_roots => (writeln ("DEBUG new roots:" ^ commas new_roots); (* FIXME debug *)
wenzelm@237
   395
        extend_syntax syn (syn_ext_roots new_roots)))
wenzelm@167
   396
  end;
clasohm@0
   397
clasohm@0
   398
clasohm@0
   399
end;
clasohm@0
   400