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