src/Pure/Syntax/syntax.ML
author wenzelm
Mon Nov 29 12:32:42 1993 +0100 (1993-11-29)
changeset 167 128e122acc89
parent 144 0a0da273a6c5
child 168 1bf4e2cab673
permissions -rw-r--r--
added (partial) extend_tables;
improved extend;
fixed roots handling of extend and merge;
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@167
   113
val empty_tables =
wenzelm@167
   114
  Tabs {
wenzelm@167
   115
    lexicon = empty_lexicon,
wenzelm@167
   116
    roots = [],
wenzelm@167
   117
    (* gram = empty_gram, *)    (* FIXME *)
wenzelm@167
   118
    gram = mk_gram [] [],       (* FIXME *)
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@167
   126
wenzelm@167
   127
wenzelm@167
   128
(* extend_tables *)
wenzelm@167
   129
wenzelm@167
   130
fun extend_tables (Tabs tabs) (XGram xgram) =
wenzelm@167
   131
  let
wenzelm@167
   132
    val {lexicon, roots = roots1, gram, consts = consts1, parse_ast_trtab,
wenzelm@167
   133
      parse_ruletab, parse_trtab, print_trtab, print_ruletab, prtab} = tabs;
wenzelm@167
   134
    val {roots = roots2, prods, consts = consts2, parse_ast_translation,
wenzelm@167
   135
      parse_rules, parse_translation, print_translation, print_rules,
wenzelm@167
   136
      print_ast_translation} = xgram;
wenzelm@167
   137
  in
wenzelm@167
   138
    (* FIXME *)
wenzelm@167
   139
    if not (null prods) then
wenzelm@167
   140
      error "extend_tables: called with non-empty prods"
wenzelm@167
   141
    else
wenzelm@167
   142
wenzelm@167
   143
    Tabs {
wenzelm@167
   144
      lexicon = extend_lexicon lexicon (literals_of prods),
wenzelm@167
   145
      roots = roots2 union roots1,
wenzelm@167
   146
      (* gram = extend_gram gram roots2 prods, *)  (* FIXME *)
wenzelm@167
   147
      gram = gram,                                 (* FIXME *)
wenzelm@167
   148
      consts = consts2 union consts1,
wenzelm@167
   149
      parse_ast_trtab =
wenzelm@167
   150
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
wenzelm@167
   151
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
wenzelm@167
   152
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
wenzelm@167
   153
      print_trtab = extend_trtab print_trtab print_translation "print translation",
wenzelm@167
   154
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@167
   155
      prtab = extend_prtab prtab prods print_ast_translation}
wenzelm@167
   156
  end;
wenzelm@167
   157
wenzelm@167
   158
wenzelm@18
   159
(* mk_tables *)
wenzelm@18
   160
wenzelm@167
   161
val mk_tables = extend_tables empty_tables;
wenzelm@167
   162
wenzelm@167
   163
(* FIXME *)
wenzelm@18
   164
fun mk_tables (XGram xgram) =
wenzelm@18
   165
  let
wenzelm@18
   166
    val {roots, prods, consts, parse_ast_translation, parse_rules,
wenzelm@18
   167
      parse_translation, print_translation, print_rules,
wenzelm@18
   168
      print_ast_translation} = xgram;
wenzelm@18
   169
  in
wenzelm@18
   170
    Tabs {
wenzelm@18
   171
      lexicon = mk_lexicon (literals_of prods),
wenzelm@18
   172
      roots = roots,
wenzelm@18
   173
      gram = mk_gram roots prods,
wenzelm@18
   174
      consts = consts,
wenzelm@18
   175
      parse_ast_trtab = mk_trtab parse_ast_translation "parse ast translation",
wenzelm@18
   176
      parse_ruletab = mk_ruletab parse_rules,
wenzelm@18
   177
      parse_trtab = mk_trtab parse_translation "parse translation",
wenzelm@18
   178
      print_trtab = mk_trtab print_translation "print translation",
wenzelm@18
   179
      print_ruletab = mk_ruletab print_rules,
wenzelm@18
   180
      prtab = mk_prtab prods print_ast_translation}
wenzelm@18
   181
  end;
wenzelm@18
   182
wenzelm@18
   183
clasohm@0
   184
(* ggr_to_xgram *)
clasohm@0
   185
clasohm@0
   186
fun ggr_to_xgram ggr =
clasohm@0
   187
  let
clasohm@0
   188
    fun flatGG ggr (xg, v) =
clasohm@0
   189
      if ggr mem v then (xg, v) else flatGG' ggr (xg, ggr :: v)
clasohm@0
   190
    and flatGG' (ref EmptyGG) xgv = xgv
clasohm@0
   191
      | flatGG' (ref (ExtGG (ggr, ext))) xgv =
clasohm@0
   192
          let
wenzelm@18
   193
            val (xg', v') = flatGG ggr xgv;
clasohm@0
   194
          in
wenzelm@18
   195
            (extend_xgram xg' ext, v')
clasohm@0
   196
          end
clasohm@0
   197
      | flatGG' (ref (MergeGG (ggr1, ggr2))) xgv =
clasohm@0
   198
          flatGG ggr1 (flatGG ggr2 xgv);
clasohm@0
   199
  in
wenzelm@18
   200
    #1 (flatGG ggr (empty_xgram, []))
clasohm@0
   201
  end;
clasohm@0
   202
clasohm@0
   203
wenzelm@167
   204
(* mk_syntax *)
clasohm@0
   205
wenzelm@167
   206
fun mk_syntax ggr = Syntax (ggr, mk_tables (ggr_to_xgram ggr));
clasohm@0
   207
clasohm@0
   208
clasohm@0
   209
clasohm@0
   210
(*** inspect syntax ***)
clasohm@0
   211
clasohm@0
   212
fun xgram_of (Syntax (ggr, _)) = ggr_to_xgram ggr;
clasohm@0
   213
clasohm@0
   214
fun string_of_big_list name prts =
clasohm@0
   215
  Pretty.string_of (Pretty.blk (2,
clasohm@0
   216
    separate Pretty.fbrk (Pretty.str name :: prts)));
clasohm@0
   217
wenzelm@18
   218
fun string_of_strings name strs =
wenzelm@18
   219
  Pretty.string_of (Pretty.blk (2,
wenzelm@18
   220
    separate (Pretty.brk 1)
wenzelm@18
   221
      (map Pretty.str (name :: map quote (sort_strings strs)))));
clasohm@0
   222
wenzelm@18
   223
wenzelm@18
   224
(* print_gram *)
clasohm@0
   225
clasohm@0
   226
fun prt_gram (XGram {roots, prods, ...}) =
clasohm@0
   227
  let
clasohm@0
   228
    fun pretty_name name = [Pretty.str (name ^ " ="), Pretty.brk 1];
clasohm@0
   229
clasohm@0
   230
    fun pretty_xsymbs (Terminal s :: xs) =
clasohm@0
   231
          Pretty.str (quote s) :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   232
      | pretty_xsymbs (Nonterminal (s, p) :: xs) =
clasohm@0
   233
          (if is_terminal s then Pretty.str s
clasohm@0
   234
          else Pretty.str (s ^ "[" ^ string_of_int p ^ "]"))
clasohm@0
   235
            :: Pretty.brk 1 :: pretty_xsymbs xs
clasohm@0
   236
      | pretty_xsymbs (_ :: xs) = pretty_xsymbs xs
clasohm@0
   237
      | pretty_xsymbs [] = [];
clasohm@0
   238
clasohm@0
   239
    fun pretty_const "" = [Pretty.brk 1]
clasohm@0
   240
      | pretty_const c = [Pretty.str (" => " ^ quote c), Pretty.brk 1];
clasohm@0
   241
clasohm@0
   242
    fun pretty_pri p = [Pretty.str ("(" ^ string_of_int p ^ ")")];
clasohm@0
   243
clasohm@0
   244
    fun pretty_prod (Prod (name, xsymbs, const, pri)) =
clasohm@0
   245
      Pretty.blk (2, pretty_name name @ pretty_xsymbs xsymbs @
clasohm@0
   246
        pretty_const const @ pretty_pri pri);
clasohm@0
   247
  in
wenzelm@18
   248
    writeln (string_of_strings "lexicon:" (literals_of prods));
clasohm@0
   249
    writeln (Pretty.string_of (Pretty.blk (2,
clasohm@0
   250
      separate (Pretty.brk 1) (map Pretty.str ("roots:" :: roots)))));
clasohm@0
   251
    writeln (string_of_big_list "prods:" (map pretty_prod prods))
clasohm@0
   252
  end;
clasohm@0
   253
clasohm@0
   254
val print_gram = prt_gram o xgram_of;
clasohm@0
   255
clasohm@0
   256
wenzelm@18
   257
(* print_trans *)
clasohm@0
   258
clasohm@0
   259
fun prt_trans (XGram xgram) =
clasohm@0
   260
  let
clasohm@0
   261
    fun string_of_trs name trs = string_of_strings name (map fst trs);
clasohm@0
   262
clasohm@0
   263
    fun string_of_rules name rules =
clasohm@0
   264
      string_of_big_list name (map pretty_rule rules);
clasohm@0
   265
wenzelm@18
   266
    val {consts, parse_ast_translation, parse_rules, parse_translation,
wenzelm@18
   267
      print_translation, print_rules, print_ast_translation, ...} = xgram;
clasohm@0
   268
  in
clasohm@0
   269
    writeln (string_of_strings "consts:" consts);
clasohm@0
   270
    writeln (string_of_trs "parse_ast_translation:" parse_ast_translation);
clasohm@0
   271
    writeln (string_of_rules "parse_rules:" parse_rules);
clasohm@0
   272
    writeln (string_of_trs "parse_translation:" parse_translation);
clasohm@0
   273
    writeln (string_of_trs "print_translation:" print_translation);
clasohm@0
   274
    writeln (string_of_rules "print_rules:" print_rules);
clasohm@0
   275
    writeln (string_of_trs "print_ast_translation:" print_ast_translation)
clasohm@0
   276
  end;
clasohm@0
   277
clasohm@0
   278
val print_trans = prt_trans o xgram_of;
clasohm@0
   279
clasohm@0
   280
clasohm@0
   281
(* print_syntax *)
clasohm@0
   282
clasohm@0
   283
fun print_syntax syn =
clasohm@0
   284
  let
clasohm@0
   285
    val xgram = xgram_of syn;
clasohm@0
   286
  in
clasohm@0
   287
    prt_gram xgram; prt_trans xgram
clasohm@0
   288
  end;
clasohm@0
   289
clasohm@0
   290
clasohm@0
   291
clasohm@0
   292
(*** parsing and printing ***)
clasohm@0
   293
wenzelm@18
   294
(* mk_get_rules *)
clasohm@0
   295
wenzelm@18
   296
fun mk_get_rules ruletab =
clasohm@0
   297
  let
clasohm@0
   298
    fun get_rules a =
clasohm@0
   299
      (case lookup ruletab a of
clasohm@0
   300
        Some rules => rules
clasohm@0
   301
      | None => []);
clasohm@0
   302
  in
wenzelm@18
   303
    if Symtab.is_null ruletab then None else Some get_rules
wenzelm@18
   304
  end;
wenzelm@18
   305
wenzelm@18
   306
wenzelm@18
   307
(* read_ast *)
wenzelm@18
   308
wenzelm@18
   309
fun read_ast (Syntax (_, tabs)) xids root str =
wenzelm@18
   310
  let
wenzelm@18
   311
    val Tabs {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@18
   312
  in
wenzelm@18
   313
    pt_to_ast (lookup parse_ast_trtab)
wenzelm@18
   314
      (parse gram root (tokenize lexicon xids str))
clasohm@0
   315
  end;
clasohm@0
   316
clasohm@0
   317
clasohm@0
   318
wenzelm@18
   319
(** test_read **)
wenzelm@18
   320
wenzelm@18
   321
fun test_read (Syntax (_, tabs)) root str =
wenzelm@18
   322
  let
wenzelm@18
   323
    val Tabs {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   324
wenzelm@18
   325
    val toks = tokenize lexicon false str;
wenzelm@18
   326
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   327
wenzelm@18
   328
    val pt = parse gram root toks;
wenzelm@18
   329
    val raw_ast = pt_to_ast (K None) pt;
wenzelm@18
   330
    val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@18
   331
wenzelm@18
   332
    val pre_ast = pt_to_ast (lookup parse_ast_trtab) pt;
wenzelm@18
   333
    val _ = normalize true true (mk_get_rules parse_ruletab) pre_ast;
wenzelm@18
   334
  in () end;
wenzelm@18
   335
wenzelm@18
   336
wenzelm@18
   337
clasohm@0
   338
(** read **)
clasohm@0
   339
wenzelm@18
   340
fun read (syn as Syntax (_, tabs)) ty str =
clasohm@0
   341
  let
wenzelm@18
   342
    val Tabs {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@18
   343
    val ast = read_ast syn false (typ_to_nonterm ty) str;
clasohm@0
   344
  in
clasohm@0
   345
    ast_to_term (lookup parse_trtab)
wenzelm@18
   346
      (normalize_ast (mk_get_rules parse_ruletab) ast)
clasohm@0
   347
  end;
clasohm@0
   348
clasohm@0
   349
clasohm@0
   350
wenzelm@144
   351
(** read_typ **)
wenzelm@144
   352
wenzelm@144
   353
fun read_typ syn def_sort str = typ_of_term def_sort (read syn typeT str);
wenzelm@144
   354
wenzelm@144
   355
wenzelm@144
   356
wenzelm@18
   357
(** read_rule **)
wenzelm@18
   358
wenzelm@18
   359
fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) =
wenzelm@18
   360
  let
wenzelm@18
   361
    val Syntax (_, Tabs {consts, ...}) = syn;
wenzelm@18
   362
wenzelm@18
   363
    fun constantify (ast as Constant _) = ast
wenzelm@18
   364
      | constantify (ast as Variable x) =
wenzelm@18
   365
          if x mem consts then Constant x else ast
wenzelm@18
   366
      | constantify (Appl asts) = Appl (map constantify asts);
clasohm@0
   367
wenzelm@18
   368
    fun read_pat (root, str) =
wenzelm@18
   369
      constantify (read_ast syn true root str)
wenzelm@18
   370
        handle ERROR => error ("The error above occurred in " ^ quote str);
wenzelm@18
   371
wenzelm@18
   372
    val rule as (lhs, rhs) = (pairself read_pat) xrule;
wenzelm@18
   373
  in
wenzelm@18
   374
    (case rule_error rule of
wenzelm@18
   375
      Some msg =>
wenzelm@18
   376
        error ("Error in syntax translation rule: " ^ msg ^
wenzelm@18
   377
          "\nexternal: " ^ quote lhs_src ^ "  ->  " ^ quote rhs_src ^
wenzelm@18
   378
          "\ninternal: " ^ str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@18
   379
    | None => rule)
wenzelm@18
   380
  end;
clasohm@0
   381
clasohm@0
   382
clasohm@0
   383
wenzelm@18
   384
(** read_xrules **)
wenzelm@18
   385
wenzelm@18
   386
fun read_xrules syn xrules =
wenzelm@18
   387
  let
wenzelm@18
   388
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
wenzelm@18
   389
      | right_rule (xpat1 <-| xpat2) = None
wenzelm@18
   390
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
clasohm@0
   391
wenzelm@18
   392
    fun left_rule (xpat1 |-> xpat2) = None
wenzelm@18
   393
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
wenzelm@18
   394
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
wenzelm@18
   395
  in
wenzelm@18
   396
    (map (read_rule syn) (mapfilter right_rule xrules),
wenzelm@18
   397
     map (read_rule syn) (mapfilter left_rule xrules))
wenzelm@18
   398
  end;
wenzelm@18
   399
wenzelm@18
   400
wenzelm@18
   401
wenzelm@18
   402
(** pretty terms or typs **)
wenzelm@18
   403
wenzelm@18
   404
fun pretty_t t_to_ast pretty_t (syn as Syntax (_, tabs)) t =
clasohm@0
   405
  let
wenzelm@18
   406
    val Tabs {print_trtab, print_ruletab, prtab, ...} = tabs;
clasohm@0
   407
    val ast = t_to_ast (lookup print_trtab) t;
clasohm@0
   408
  in
wenzelm@18
   409
    pretty_t prtab (normalize_ast (mk_get_rules print_ruletab) ast)
clasohm@0
   410
  end;
clasohm@0
   411
clasohm@0
   412
val pretty_term = pretty_t term_to_ast pretty_term_ast;
clasohm@0
   413
clasohm@0
   414
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
clasohm@0
   415
clasohm@0
   416
fun string_of_term syn t = Pretty.string_of (pretty_term syn t);
clasohm@0
   417
clasohm@0
   418
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
clasohm@0
   419
clasohm@0
   420
clasohm@0
   421
clasohm@0
   422
(*** build syntax ***)
clasohm@0
   423
clasohm@0
   424
(* type_syn *)
clasohm@0
   425
wenzelm@167
   426
val type_syn = mk_syntax (ref (ExtGG (ref EmptyGG, type_ext)));
wenzelm@167
   427
wenzelm@167
   428
wenzelm@167
   429
(* extend *)  (* FIXME *)
wenzelm@167
   430
wenzelm@167
   431
fun old_extend syn read_ty (roots, xconsts, sext) =
wenzelm@167
   432
  let
wenzelm@167
   433
    val Syntax (ggr0, Tabs {roots = roots0, ...}) = syn;
wenzelm@167
   434
wenzelm@167
   435
    val ext1 = ext_of_sext ((distinct roots) \\ roots0) xconsts read_ty sext;
wenzelm@167
   436
    val (syn1 as Syntax (ggr1, tabs1)) = mk_syntax (ref (ExtGG (ggr0, ext1)));
wenzelm@167
   437
wenzelm@167
   438
    val (parse_rules, print_rules) = read_xrules syn1 (xrules_of sext);
wenzelm@167
   439
    val ext2 = ExtRules {parse_rules = parse_rules, print_rules = print_rules};
wenzelm@167
   440
  in
wenzelm@167
   441
    Syntax (ref (ExtGG (ggr1, ext2)), extend_tables tabs1 (mk_xgram ext2))
wenzelm@167
   442
  end;
clasohm@0
   443
clasohm@0
   444
wenzelm@167
   445
fun new_extend syn read_ty (roots, xconsts, sext) =
clasohm@0
   446
  let
wenzelm@167
   447
    val Syntax (ggr0, tabs0 as Tabs {roots = roots0, ...}) = syn;
clasohm@0
   448
wenzelm@167
   449
    val ext1 = ext_of_sext ((distinct roots) \\ roots0) xconsts read_ty sext;
wenzelm@167
   450
    val (syn1 as Syntax (ggr1, tabs1)) =
wenzelm@167
   451
      Syntax (ref (ExtGG (ggr0, ext1)), extend_tables tabs0 (mk_xgram ext1));
clasohm@0
   452
wenzelm@167
   453
    val (parse_rules, print_rules) = read_xrules syn1 (xrules_of sext);
wenzelm@167
   454
    val ext2 = ExtRules {parse_rules = parse_rules, print_rules = print_rules};
clasohm@0
   455
  in
wenzelm@167
   456
    Syntax (ref (ExtGG (ggr1, ext2)), extend_tables tabs1 (mk_xgram ext2))
clasohm@0
   457
  end;
clasohm@0
   458
clasohm@0
   459
wenzelm@167
   460
fun extend syn read_ty (ex as (_, _, sext)) =
wenzelm@167
   461
  (case sext of
wenzelm@167
   462
    Sext {mixfix = [], ...} => new_extend
wenzelm@167
   463
  | NewSext {mixfix = [], ...} => new_extend
wenzelm@167
   464
  | _ => old_extend) syn read_ty ex;
wenzelm@167
   465
wenzelm@167
   466
clasohm@0
   467
(* merge *)
clasohm@0
   468
wenzelm@167
   469
fun merge roots syn1 syn2 =
wenzelm@167
   470
  let
wenzelm@167
   471
    val Syntax (ggr1, Tabs {roots = roots1, ...}) = syn1;
wenzelm@167
   472
    val Syntax (ggr2, Tabs {roots = roots2, ...}) = syn2;
wenzelm@167
   473
    val mggr = ref (MergeGG (ggr1, ggr2));
wenzelm@167
   474
  in
wenzelm@167
   475
    (case ((distinct roots) \\ roots1) \\ roots2 of
wenzelm@167
   476
      [] => mk_syntax mggr
wenzelm@167
   477
    | new_roots => mk_syntax (ref (ExtGG (mggr, ExtRoots new_roots))))
wenzelm@167
   478
  end;
clasohm@0
   479
clasohm@0
   480
clasohm@0
   481
end;
clasohm@0
   482