src/Pure/Syntax/syntax.ML
author wenzelm
Tue Nov 30 11:04:07 1993 +0100 (1993-11-30)
changeset 171 ab0f93a291b5
parent 168 1bf4e2cab673
child 174 319ff2d6760b
permissions -rw-r--r--
*** empty log message ***
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.
wenzelm@18
     6
wenzelm@18
     7
TODO:
wenzelm@167
     8
  fix empty_tables, extend_tables, mk_tables (requires empty_gram, extend_gram)
wenzelm@167
     9
  fix extend (requires extend_tables)
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
signature SYNTAX =
clasohm@0
    13
sig
wenzelm@18
    14
  include AST0
clasohm@0
    15
  include LEXICON0
clasohm@0
    16
  include EXTENSION0
clasohm@0
    17
  include TYPE_EXT0
clasohm@0
    18
  include SEXTENSION1
clasohm@0
    19
  include PRINTER0
wenzelm@18
    20
  type syntax
wenzelm@18
    21
  val type_syn: syntax
wenzelm@144
    22
  val extend: syntax -> (string -> typ) -> string list * string list * sext -> syntax
wenzelm@167
    23
  val merge: string list -> syntax -> syntax -> syntax
wenzelm@18
    24
  val print_gram: syntax -> unit
wenzelm@18
    25
  val print_trans: syntax -> unit
wenzelm@18
    26
  val print_syntax: syntax -> unit
wenzelm@18
    27
  val test_read: syntax -> string -> string -> unit
wenzelm@18
    28
  val read: syntax -> typ -> string -> term
wenzelm@144
    29
  val read_typ: syntax -> (indexname -> sort) -> string -> typ
wenzelm@18
    30
  val pretty_term: syntax -> term -> Pretty.T
wenzelm@18
    31
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@18
    32
  val string_of_term: syntax -> term -> string
wenzelm@18
    33
  val string_of_typ: syntax -> typ -> string
clasohm@0
    34
end;
clasohm@0
    35
clasohm@0
    36
functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
clasohm@0
    37
  and Parser: PARSER and SExtension: SEXTENSION and Printer: PRINTER
wenzelm@18
    38
  sharing TypeExt.Extension = SExtension.Extension
wenzelm@18
    39
  and Parser.XGram = TypeExt.Extension.XGram = Printer.XGram
wenzelm@47
    40
  and Parser.XGram.Ast = Parser.ParseTree.Ast): SYNTAX =
clasohm@0
    41
struct
clasohm@0
    42
clasohm@0
    43
structure Extension = TypeExt.Extension;
clasohm@0
    44
structure XGram = Extension.XGram;
clasohm@0
    45
structure Lexicon = Parser.ParseTree.Lexicon;
wenzelm@18
    46
open Lexicon Parser Parser.ParseTree Extension TypeExt SExtension Printer
wenzelm@18
    47
  XGram XGram.Ast;
clasohm@0
    48
clasohm@0
    49
clasohm@0
    50
fun lookup tab a = Symtab.lookup (tab, a);
clasohm@0
    51
clasohm@0
    52
clasohm@0
    53
clasohm@0
    54
(** datatype syntax **)
clasohm@0
    55
clasohm@0
    56
datatype tables =
wenzelm@18
    57
  Tabs of {
wenzelm@18
    58
    lexicon: lexicon,
wenzelm@18
    59
    roots: string list,
wenzelm@18
    60
    gram: gram,
wenzelm@18
    61
    consts: string list,
clasohm@0
    62
    parse_ast_trtab: (ast list -> ast) Symtab.table,
clasohm@0
    63
    parse_ruletab: (ast * ast) list Symtab.table,
clasohm@0
    64
    parse_trtab: (term list -> term) Symtab.table,
clasohm@0
    65
    print_trtab: (term list -> term) Symtab.table,
clasohm@0
    66
    print_ruletab: (ast * ast) list Symtab.table,
wenzelm@18
    67
    prtab: prtab};
clasohm@0
    68
clasohm@0
    69
datatype gramgraph =
clasohm@0
    70
  EmptyGG |
wenzelm@167
    71
  ExtGG of gramgraph ref * ext |
clasohm@0
    72
  MergeGG of gramgraph ref * gramgraph ref;
clasohm@0
    73
clasohm@0
    74
datatype syntax = Syntax of gramgraph ref * tables;
clasohm@0
    75
clasohm@0
    76
clasohm@0
    77
clasohm@0
    78
(*** compile syntax ***)
clasohm@0
    79
wenzelm@18
    80
(* translation funs *)
wenzelm@18
    81
wenzelm@18
    82
fun extend_trtab tab trfuns name =
wenzelm@18
    83
  Symtab.balance (Symtab.st_of_alist (trfuns, tab)) handle Symtab.DUPLICATE s
wenzelm@18
    84
    => error ("More than one " ^ name ^ " for " ^ quote s);
wenzelm@18
    85
wenzelm@18
    86
val mk_trtab = extend_trtab Symtab.null;
wenzelm@18
    87
wenzelm@18
    88
wenzelm@18
    89
(* translation rules *)
wenzelm@18
    90
wenzelm@18
    91
fun mk_ruletab rules =
wenzelm@18
    92
  let
wenzelm@18
    93
    fun add_rule (r, tab) =
wenzelm@18
    94
      let val a = head_of_rule r;
wenzelm@18
    95
      in
wenzelm@18
    96
        (case lookup tab a of
wenzelm@18
    97
          None => Symtab.update ((a, [r]), tab)
wenzelm@18
    98
        | Some rs => Symtab.update ((a, r :: rs), tab))
wenzelm@18
    99
      end;
wenzelm@18
   100
  in
wenzelm@18
   101
    Symtab.balance (foldr add_rule (rules, Symtab.null))
wenzelm@18
   102
  end;
wenzelm@18
   103
wenzelm@18
   104
fun extend_ruletab tab rules =
wenzelm@18
   105
  mk_ruletab (flat (map #2 (Symtab.alist_of tab)) @ rules);
wenzelm@18
   106
wenzelm@18
   107
wenzelm@167
   108
wenzelm@167
   109
(** tables **)
wenzelm@167
   110
wenzelm@167
   111
(* empty_tables *)
wenzelm@167
   112
wenzelm@171
   113
(*(* FIXME *)
wenzelm@167
   114
val empty_tables =
wenzelm@167
   115
  Tabs {
wenzelm@167
   116
    lexicon = empty_lexicon,
wenzelm@167
   117
    roots = [],
wenzelm@171
   118
    gram = empty_gram,
wenzelm@167
   119
    consts = [],
wenzelm@167
   120
    parse_ast_trtab = Symtab.null,
wenzelm@167
   121
    parse_ruletab = Symtab.null,
wenzelm@167
   122
    parse_trtab = Symtab.null,
wenzelm@167
   123
    print_trtab = Symtab.null,
wenzelm@167
   124
    print_ruletab = Symtab.null,
wenzelm@167
   125
    prtab = empty_prtab};
wenzelm@171
   126
*)
wenzelm@167
   127
wenzelm@167
   128
wenzelm@167
   129
(* extend_tables *)
wenzelm@167
   130
wenzelm@167
   131
fun extend_tables (Tabs tabs) (XGram xgram) =
wenzelm@167
   132
  let
wenzelm@167
   133
    val {lexicon, roots = roots1, gram, consts = consts1, parse_ast_trtab,
wenzelm@167
   134
      parse_ruletab, parse_trtab, print_trtab, print_ruletab, prtab} = tabs;
wenzelm@167
   135
    val {roots = roots2, prods, consts = consts2, parse_ast_translation,
wenzelm@167
   136
      parse_rules, parse_translation, print_translation, print_rules,
wenzelm@167
   137
      print_ast_translation} = xgram;
wenzelm@167
   138
  in
wenzelm@167
   139
    (* FIXME *)
wenzelm@167
   140
    if not (null prods) then
wenzelm@167
   141
      error "extend_tables: called with non-empty prods"
wenzelm@167
   142
    else
wenzelm@167
   143
wenzelm@167
   144
    Tabs {
wenzelm@167
   145
      lexicon = extend_lexicon lexicon (literals_of prods),
wenzelm@167
   146
      roots = roots2 union roots1,
wenzelm@167
   147
      (* gram = extend_gram gram roots2 prods, *)  (* FIXME *)
wenzelm@167
   148
      gram = gram,                                 (* FIXME *)
wenzelm@167
   149
      consts = consts2 union consts1,
wenzelm@167
   150
      parse_ast_trtab =
wenzelm@167
   151
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
wenzelm@167
   152
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
wenzelm@167
   153
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
wenzelm@167
   154
      print_trtab = extend_trtab print_trtab print_translation "print translation",
wenzelm@167
   155
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@167
   156
      prtab = extend_prtab prtab prods print_ast_translation}
wenzelm@167
   157
  end;
wenzelm@167
   158
wenzelm@167
   159
wenzelm@18
   160
(* mk_tables *)
wenzelm@18
   161
wenzelm@171
   162
(* FIXME *)
wenzelm@168
   163
(* val mk_tables = extend_tables empty_tables; *)
wenzelm@167
   164
wenzelm@167
   165
(* FIXME *)
wenzelm@18
   166
fun mk_tables (XGram xgram) =
wenzelm@18
   167
  let
wenzelm@18
   168
    val {roots, prods, consts, parse_ast_translation, parse_rules,
wenzelm@18
   169
      parse_translation, print_translation, print_rules,
wenzelm@18
   170
      print_ast_translation} = xgram;
wenzelm@18
   171
  in
wenzelm@18
   172
    Tabs {
wenzelm@18
   173
      lexicon = mk_lexicon (literals_of prods),
wenzelm@18
   174
      roots = roots,
wenzelm@18
   175
      gram = mk_gram roots prods,
wenzelm@18
   176
      consts = consts,
wenzelm@18
   177
      parse_ast_trtab = mk_trtab parse_ast_translation "parse ast translation",
wenzelm@18
   178
      parse_ruletab = mk_ruletab parse_rules,
wenzelm@18
   179
      parse_trtab = mk_trtab parse_translation "parse translation",
wenzelm@18
   180
      print_trtab = mk_trtab print_translation "print translation",
wenzelm@18
   181
      print_ruletab = mk_ruletab print_rules,
wenzelm@18
   182
      prtab = mk_prtab prods print_ast_translation}
wenzelm@18
   183
  end;
wenzelm@18
   184
wenzelm@18
   185
clasohm@0
   186
(* ggr_to_xgram *)
clasohm@0
   187
clasohm@0
   188
fun ggr_to_xgram ggr =
clasohm@0
   189
  let
clasohm@0
   190
    fun flatGG ggr (xg, v) =
clasohm@0
   191
      if ggr mem v then (xg, v) else flatGG' ggr (xg, ggr :: v)
clasohm@0
   192
    and flatGG' (ref EmptyGG) xgv = xgv
clasohm@0
   193
      | flatGG' (ref (ExtGG (ggr, ext))) xgv =
clasohm@0
   194
          let
wenzelm@18
   195
            val (xg', v') = flatGG ggr xgv;
clasohm@0
   196
          in
wenzelm@18
   197
            (extend_xgram xg' ext, v')
clasohm@0
   198
          end
clasohm@0
   199
      | flatGG' (ref (MergeGG (ggr1, ggr2))) xgv =
clasohm@0
   200
          flatGG ggr1 (flatGG ggr2 xgv);
clasohm@0
   201
  in
wenzelm@18
   202
    #1 (flatGG ggr (empty_xgram, []))
clasohm@0
   203
  end;
clasohm@0
   204
clasohm@0
   205
wenzelm@167
   206
(* mk_syntax *)
clasohm@0
   207
wenzelm@167
   208
fun mk_syntax ggr = Syntax (ggr, mk_tables (ggr_to_xgram ggr));
clasohm@0
   209
clasohm@0
   210
clasohm@0
   211
clasohm@0
   212
(*** inspect syntax ***)
clasohm@0
   213
clasohm@0
   214
fun xgram_of (Syntax (ggr, _)) = ggr_to_xgram ggr;
clasohm@0
   215
clasohm@0
   216
fun string_of_big_list name prts =
clasohm@0
   217
  Pretty.string_of (Pretty.blk (2,
clasohm@0
   218
    separate Pretty.fbrk (Pretty.str name :: prts)));
clasohm@0
   219
wenzelm@18
   220
fun string_of_strings name strs =
wenzelm@18
   221
  Pretty.string_of (Pretty.blk (2,
wenzelm@18
   222
    separate (Pretty.brk 1)
wenzelm@18
   223
      (map Pretty.str (name :: map quote (sort_strings strs)))));
clasohm@0
   224
wenzelm@18
   225
wenzelm@18
   226
(* print_gram *)
clasohm@0
   227
clasohm@0
   228
fun prt_gram (XGram {roots, prods, ...}) =
clasohm@0
   229
  let
clasohm@0
   230
    fun pretty_name name = [Pretty.str (name ^ " ="), Pretty.brk 1];
clasohm@0
   231
clasohm@0
   232
    fun pretty_xsymbs (Terminal s :: xs) =
clasohm@0
   233
          Pretty.str (quote s) :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   234
      | pretty_xsymbs (Nonterminal (s, p) :: xs) =
clasohm@0
   235
          (if is_terminal s then Pretty.str s
clasohm@0
   236
          else Pretty.str (s ^ "[" ^ string_of_int p ^ "]"))
clasohm@0
   237
            :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   238
      | pretty_xsymbs (_ :: xs) = pretty_xsymbs xs
clasohm@0
   239
      | pretty_xsymbs [] = [];
clasohm@0
   240
clasohm@0
   241
    fun pretty_const "" = [Pretty.brk 1]
clasohm@0
   242
      | pretty_const c = [Pretty.str (" => " ^ quote c), Pretty.brk 1];
clasohm@0
   243
clasohm@0
   244
    fun pretty_pri p = [Pretty.str ("(" ^ string_of_int p ^ ")")];
clasohm@0
   245
clasohm@0
   246
    fun pretty_prod (Prod (name, xsymbs, const, pri)) =
clasohm@0
   247
      Pretty.blk (2, pretty_name name @ pretty_xsymbs xsymbs @
clasohm@0
   248
        pretty_const const @ pretty_pri pri);
clasohm@0
   249
  in
wenzelm@18
   250
    writeln (string_of_strings "lexicon:" (literals_of prods));
clasohm@0
   251
    writeln (Pretty.string_of (Pretty.blk (2,
clasohm@0
   252
      separate (Pretty.brk 1) (map Pretty.str ("roots:" :: roots)))));
clasohm@0
   253
    writeln (string_of_big_list "prods:" (map pretty_prod prods))
clasohm@0
   254
  end;
clasohm@0
   255
clasohm@0
   256
val print_gram = prt_gram o xgram_of;
clasohm@0
   257
clasohm@0
   258
wenzelm@18
   259
(* print_trans *)
clasohm@0
   260
clasohm@0
   261
fun prt_trans (XGram xgram) =
clasohm@0
   262
  let
clasohm@0
   263
    fun string_of_trs name trs = string_of_strings name (map fst trs);
clasohm@0
   264
clasohm@0
   265
    fun string_of_rules name rules =
clasohm@0
   266
      string_of_big_list name (map pretty_rule rules);
clasohm@0
   267
wenzelm@18
   268
    val {consts, parse_ast_translation, parse_rules, parse_translation,
wenzelm@18
   269
      print_translation, print_rules, print_ast_translation, ...} = xgram;
clasohm@0
   270
  in
clasohm@0
   271
    writeln (string_of_strings "consts:" consts);
clasohm@0
   272
    writeln (string_of_trs "parse_ast_translation:" parse_ast_translation);
clasohm@0
   273
    writeln (string_of_rules "parse_rules:" parse_rules);
clasohm@0
   274
    writeln (string_of_trs "parse_translation:" parse_translation);
clasohm@0
   275
    writeln (string_of_trs "print_translation:" print_translation);
clasohm@0
   276
    writeln (string_of_rules "print_rules:" print_rules);
clasohm@0
   277
    writeln (string_of_trs "print_ast_translation:" print_ast_translation)
clasohm@0
   278
  end;
clasohm@0
   279
clasohm@0
   280
val print_trans = prt_trans o xgram_of;
clasohm@0
   281
clasohm@0
   282
clasohm@0
   283
(* print_syntax *)
clasohm@0
   284
clasohm@0
   285
fun print_syntax syn =
clasohm@0
   286
  let
clasohm@0
   287
    val xgram = xgram_of syn;
clasohm@0
   288
  in
clasohm@0
   289
    prt_gram xgram; prt_trans xgram
clasohm@0
   290
  end;
clasohm@0
   291
clasohm@0
   292
clasohm@0
   293
clasohm@0
   294
(*** parsing and printing ***)
clasohm@0
   295
wenzelm@18
   296
(* mk_get_rules *)
clasohm@0
   297
wenzelm@18
   298
fun mk_get_rules ruletab =
clasohm@0
   299
  let
clasohm@0
   300
    fun get_rules a =
clasohm@0
   301
      (case lookup ruletab a of
clasohm@0
   302
        Some rules => rules
clasohm@0
   303
      | None => []);
clasohm@0
   304
  in
wenzelm@18
   305
    if Symtab.is_null ruletab then None else Some get_rules
wenzelm@18
   306
  end;
wenzelm@18
   307
wenzelm@18
   308
wenzelm@18
   309
(* read_ast *)
wenzelm@18
   310
wenzelm@18
   311
fun read_ast (Syntax (_, tabs)) xids root str =
wenzelm@18
   312
  let
wenzelm@18
   313
    val Tabs {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@18
   314
  in
wenzelm@18
   315
    pt_to_ast (lookup parse_ast_trtab)
wenzelm@18
   316
      (parse gram root (tokenize lexicon xids str))
clasohm@0
   317
  end;
clasohm@0
   318
clasohm@0
   319
clasohm@0
   320
wenzelm@18
   321
(** test_read **)
wenzelm@18
   322
wenzelm@18
   323
fun test_read (Syntax (_, tabs)) root str =
wenzelm@18
   324
  let
wenzelm@18
   325
    val Tabs {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   326
wenzelm@18
   327
    val toks = tokenize lexicon false str;
wenzelm@18
   328
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   329
wenzelm@18
   330
    val pt = parse gram root toks;
wenzelm@18
   331
    val raw_ast = pt_to_ast (K None) pt;
wenzelm@18
   332
    val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@18
   333
wenzelm@18
   334
    val pre_ast = pt_to_ast (lookup parse_ast_trtab) pt;
wenzelm@18
   335
    val _ = normalize true true (mk_get_rules parse_ruletab) pre_ast;
wenzelm@18
   336
  in () end;
wenzelm@18
   337
wenzelm@18
   338
wenzelm@18
   339
clasohm@0
   340
(** read **)
clasohm@0
   341
wenzelm@18
   342
fun read (syn as Syntax (_, tabs)) ty str =
clasohm@0
   343
  let
wenzelm@18
   344
    val Tabs {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@18
   345
    val ast = read_ast syn false (typ_to_nonterm ty) str;
clasohm@0
   346
  in
clasohm@0
   347
    ast_to_term (lookup parse_trtab)
wenzelm@18
   348
      (normalize_ast (mk_get_rules parse_ruletab) ast)
clasohm@0
   349
  end;
clasohm@0
   350
clasohm@0
   351
clasohm@0
   352
wenzelm@144
   353
(** read_typ **)
wenzelm@144
   354
wenzelm@144
   355
fun read_typ syn def_sort str = typ_of_term def_sort (read syn typeT str);
wenzelm@144
   356
wenzelm@144
   357
wenzelm@144
   358
wenzelm@18
   359
(** read_rule **)
wenzelm@18
   360
wenzelm@18
   361
fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) =
wenzelm@18
   362
  let
wenzelm@18
   363
    val Syntax (_, Tabs {consts, ...}) = syn;
wenzelm@18
   364
wenzelm@18
   365
    fun constantify (ast as Constant _) = ast
wenzelm@18
   366
      | constantify (ast as Variable x) =
wenzelm@18
   367
          if x mem consts then Constant x else ast
wenzelm@18
   368
      | constantify (Appl asts) = Appl (map constantify asts);
clasohm@0
   369
wenzelm@18
   370
    fun read_pat (root, str) =
wenzelm@18
   371
      constantify (read_ast syn true root str)
wenzelm@18
   372
        handle ERROR => error ("The error above occurred in " ^ quote str);
wenzelm@18
   373
wenzelm@18
   374
    val rule as (lhs, rhs) = (pairself read_pat) xrule;
wenzelm@18
   375
  in
wenzelm@18
   376
    (case rule_error rule of
wenzelm@18
   377
      Some msg =>
wenzelm@18
   378
        error ("Error in syntax translation rule: " ^ msg ^
wenzelm@18
   379
          "\nexternal: " ^ quote lhs_src ^ "  ->  " ^ quote rhs_src ^
wenzelm@18
   380
          "\ninternal: " ^ str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@18
   381
    | None => rule)
wenzelm@18
   382
  end;
clasohm@0
   383
clasohm@0
   384
clasohm@0
   385
wenzelm@18
   386
(** read_xrules **)
wenzelm@18
   387
wenzelm@18
   388
fun read_xrules syn xrules =
wenzelm@18
   389
  let
wenzelm@18
   390
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
wenzelm@18
   391
      | right_rule (xpat1 <-| xpat2) = None
wenzelm@18
   392
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
clasohm@0
   393
wenzelm@18
   394
    fun left_rule (xpat1 |-> xpat2) = None
wenzelm@18
   395
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
wenzelm@18
   396
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
wenzelm@18
   397
  in
wenzelm@18
   398
    (map (read_rule syn) (mapfilter right_rule xrules),
wenzelm@18
   399
     map (read_rule syn) (mapfilter left_rule xrules))
wenzelm@18
   400
  end;
wenzelm@18
   401
wenzelm@18
   402
wenzelm@18
   403
wenzelm@18
   404
(** pretty terms or typs **)
wenzelm@18
   405
wenzelm@18
   406
fun pretty_t t_to_ast pretty_t (syn as Syntax (_, tabs)) t =
clasohm@0
   407
  let
wenzelm@18
   408
    val Tabs {print_trtab, print_ruletab, prtab, ...} = tabs;
clasohm@0
   409
    val ast = t_to_ast (lookup print_trtab) t;
clasohm@0
   410
  in
wenzelm@18
   411
    pretty_t prtab (normalize_ast (mk_get_rules print_ruletab) ast)
clasohm@0
   412
  end;
clasohm@0
   413
clasohm@0
   414
val pretty_term = pretty_t term_to_ast pretty_term_ast;
clasohm@0
   415
clasohm@0
   416
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
clasohm@0
   417
clasohm@0
   418
fun string_of_term syn t = Pretty.string_of (pretty_term syn t);
clasohm@0
   419
clasohm@0
   420
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
clasohm@0
   421
clasohm@0
   422
clasohm@0
   423
clasohm@0
   424
(*** build syntax ***)
clasohm@0
   425
clasohm@0
   426
(* type_syn *)
clasohm@0
   427
wenzelm@167
   428
val type_syn = mk_syntax (ref (ExtGG (ref EmptyGG, type_ext)));
wenzelm@167
   429
wenzelm@167
   430
wenzelm@171
   431
(* extend *)  (* FIXME *)
wenzelm@167
   432
wenzelm@167
   433
fun old_extend syn read_ty (roots, xconsts, sext) =
wenzelm@167
   434
  let
wenzelm@167
   435
    val Syntax (ggr0, Tabs {roots = roots0, ...}) = syn;
wenzelm@167
   436
wenzelm@167
   437
    val ext1 = ext_of_sext ((distinct roots) \\ roots0) xconsts read_ty sext;
wenzelm@167
   438
    val (syn1 as Syntax (ggr1, tabs1)) = mk_syntax (ref (ExtGG (ggr0, ext1)));
wenzelm@167
   439
wenzelm@167
   440
    val (parse_rules, print_rules) = read_xrules syn1 (xrules_of sext);
wenzelm@167
   441
    val ext2 = ExtRules {parse_rules = parse_rules, print_rules = print_rules};
wenzelm@167
   442
  in
wenzelm@167
   443
    Syntax (ref (ExtGG (ggr1, ext2)), extend_tables tabs1 (mk_xgram ext2))
wenzelm@167
   444
  end;
clasohm@0
   445
clasohm@0
   446
wenzelm@167
   447
fun new_extend syn read_ty (roots, xconsts, sext) =
clasohm@0
   448
  let
wenzelm@167
   449
    val Syntax (ggr0, tabs0 as Tabs {roots = roots0, ...}) = syn;
clasohm@0
   450
wenzelm@167
   451
    val ext1 = ext_of_sext ((distinct roots) \\ roots0) xconsts read_ty sext;
wenzelm@167
   452
    val (syn1 as Syntax (ggr1, tabs1)) =
wenzelm@167
   453
      Syntax (ref (ExtGG (ggr0, ext1)), extend_tables tabs0 (mk_xgram ext1));
clasohm@0
   454
wenzelm@167
   455
    val (parse_rules, print_rules) = read_xrules syn1 (xrules_of sext);
wenzelm@167
   456
    val ext2 = ExtRules {parse_rules = parse_rules, print_rules = print_rules};
clasohm@0
   457
  in
wenzelm@167
   458
    Syntax (ref (ExtGG (ggr1, ext2)), extend_tables tabs1 (mk_xgram ext2))
clasohm@0
   459
  end;
clasohm@0
   460
clasohm@0
   461
wenzelm@168
   462
fun extend syn read_ty (ex as (roots, _, sext)) =
wenzelm@168
   463
  (case (roots, sext) of
wenzelm@168
   464
    ([], Sext {mixfix = [], ...}) => new_extend
wenzelm@168
   465
  | ([], NewSext {mixfix = [], ...}) => new_extend
wenzelm@167
   466
  | _ => old_extend) syn read_ty ex;
wenzelm@167
   467
wenzelm@167
   468
clasohm@0
   469
(* merge *)
clasohm@0
   470
wenzelm@167
   471
fun merge roots syn1 syn2 =
wenzelm@167
   472
  let
wenzelm@167
   473
    val Syntax (ggr1, Tabs {roots = roots1, ...}) = syn1;
wenzelm@167
   474
    val Syntax (ggr2, Tabs {roots = roots2, ...}) = syn2;
wenzelm@167
   475
    val mggr = ref (MergeGG (ggr1, ggr2));
wenzelm@167
   476
  in
wenzelm@167
   477
    (case ((distinct roots) \\ roots1) \\ roots2 of
wenzelm@167
   478
      [] => mk_syntax mggr
wenzelm@167
   479
    | new_roots => mk_syntax (ref (ExtGG (mggr, ExtRoots new_roots))))
wenzelm@167
   480
  end;
clasohm@0
   481
clasohm@0
   482
clasohm@0
   483
end;
clasohm@0
   484