src/Pure/sign.ML
author wenzelm
Thu Oct 09 14:50:39 1997 +0200 (1997-10-09)
changeset 3810 350150bd3744
parent 3805 a50d0b5219dd
child 3850 305e5adfd7c8
permissions -rw-r--r--
tuned exports;
tuned add_space;
tuned print_sg;
fixed "op ==>" syntax;
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@0
     4
wenzelm@251
     5
The abstract type "sg" of signatures.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@3805
     8
(*external forms*)
wenzelm@3805
     9
type xstring = string;
wenzelm@3805
    10
type xclass = class;
wenzelm@3805
    11
type xsort = sort;
wenzelm@3805
    12
type xtyp = typ;
wenzelm@3805
    13
type xterm = term;
wenzelm@3805
    14
wenzelm@19
    15
signature SIGN =
wenzelm@3791
    16
sig
paulson@1501
    17
  type sg
wenzelm@2197
    18
  val rep_sg: sg ->
wenzelm@2197
    19
   {tsig: Type.type_sig,
wenzelm@2197
    20
    const_tab: typ Symtab.table,
wenzelm@2197
    21
    syn: Syntax.syntax,
wenzelm@3791
    22
    path: string list,
wenzelm@3791
    23
    spaces: (string * NameSpace.T) list,
wenzelm@2197
    24
    stamps: string ref list}
paulson@1501
    25
  val subsig: sg * sg -> bool
paulson@1501
    26
  val eq_sg: sg * sg -> bool
paulson@1501
    27
  val same_sg: sg * sg -> bool
paulson@1501
    28
  val is_draft: sg -> bool
paulson@1501
    29
  val const_type: sg -> string -> typ option
paulson@1501
    30
  val classes: sg -> class list
paulson@1501
    31
  val subsort: sg -> sort * sort -> bool
paulson@1501
    32
  val nodup_Vars: term -> unit
paulson@1501
    33
  val norm_sort: sg -> sort -> sort
paulson@1501
    34
  val nonempty_sort: sg -> sort list -> sort -> bool
wenzelm@3791
    35
  val long_names: bool ref
wenzelm@3810
    36
  val classK: string
wenzelm@3810
    37
  val typeK: string
wenzelm@3810
    38
  val constK: string
wenzelm@3810
    39
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    40
  val extern: sg -> string -> string -> xstring
wenzelm@3810
    41
  val full_name: sg -> xstring -> string
wenzelm@3805
    42
  val intern_class: sg -> xclass -> class
wenzelm@3805
    43
  val extern_class: sg -> class -> xclass
wenzelm@3805
    44
  val intern_sort: sg -> xsort -> sort
wenzelm@3805
    45
  val extern_sort: sg -> sort -> xsort
wenzelm@3805
    46
  val intern_typ: sg -> xtyp -> typ
wenzelm@3805
    47
  val extern_typ: sg -> typ -> xtyp
wenzelm@3805
    48
  val intern_term: sg -> xterm -> term
wenzelm@3805
    49
  val extern_term: sg -> term -> xterm
wenzelm@3805
    50
  val intern_tycons: sg -> xtyp -> typ
wenzelm@3805
    51
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    52
  val intern_const: sg -> xstring -> string
paulson@1501
    53
  val print_sg: sg -> unit
paulson@1501
    54
  val pretty_sg: sg -> Pretty.T
paulson@1501
    55
  val pprint_sg: sg -> pprint_args -> unit
paulson@1501
    56
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    57
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    58
  val pretty_sort: sg -> sort -> Pretty.T
paulson@1501
    59
  val string_of_term: sg -> term -> string
paulson@1501
    60
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    61
  val string_of_sort: sg -> sort -> string
paulson@1501
    62
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    63
  val pprint_typ: sg -> typ -> pprint_args -> unit
paulson@1501
    64
  val certify_typ: sg -> typ -> typ
paulson@1501
    65
  val certify_term: sg -> term -> term * typ * int
paulson@1501
    66
  val read_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    67
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
    68
    (indexname -> sort option) -> string list -> bool
wenzelm@3805
    69
    -> xterm list * typ -> int * term * (indexname * typ) list
wenzelm@3805
    70
  val add_classes: (xclass * xclass list) list -> sg -> sg
wenzelm@3805
    71
  val add_classes_i: (xclass * class list) list -> sg -> sg
wenzelm@3805
    72
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
    73
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@3805
    74
  val add_defsort: xsort -> sg -> sg
wenzelm@3791
    75
  val add_defsort_i: sort -> sg -> sg
wenzelm@3805
    76
  val add_types: (xstring * int * mixfix) list -> sg -> sg
wenzelm@3805
    77
  val add_tyabbrs: (xstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3805
    78
  val add_tyabbrs_i: (xstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@3805
    79
  val add_arities: (xstring * xsort list * xsort) list -> sg -> sg
wenzelm@3791
    80
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3805
    81
  val add_consts: (xstring * string * mixfix) list -> sg -> sg
wenzelm@3805
    82
  val add_consts_i: (xstring * typ * mixfix) list -> sg -> sg
wenzelm@3805
    83
  val add_syntax: (xstring * string * mixfix) list -> sg -> sg
wenzelm@3805
    84
  val add_syntax_i: (xstring * typ * mixfix) list -> sg -> sg
wenzelm@3805
    85
  val add_modesyntax: (string * bool) * (xstring * string * mixfix) list -> sg -> sg
wenzelm@3805
    86
  val add_modesyntax_i: (string * bool) * (xstring * typ * mixfix) list -> sg -> sg
paulson@1501
    87
  val add_trfuns:
paulson@1501
    88
    (string * (ast list -> ast)) list *
paulson@1501
    89
    (string * (term list -> term)) list *
paulson@1501
    90
    (string * (term list -> term)) list *
paulson@1501
    91
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
    92
  val add_trfunsT:
wenzelm@2385
    93
    (string * (typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
    94
  val add_tokentrfuns:
wenzelm@2693
    95
    (string * string * (string -> string * int)) list -> sg -> sg
paulson@1810
    96
  val add_trrules: (string * string) Syntax.trrule list -> sg -> sg
paulson@1810
    97
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
    98
  val add_path: string -> sg -> sg
wenzelm@3810
    99
  val add_space: string * string list -> sg -> sg
paulson@1501
   100
  val add_name: string -> sg -> sg
paulson@1501
   101
  val make_draft: sg -> sg
paulson@1501
   102
  val merge: sg * sg -> sg
paulson@1501
   103
  val proto_pure: sg
paulson@1501
   104
  val pure: sg
paulson@1501
   105
  val cpure: sg
paulson@1501
   106
  val const_of_class: class -> string
paulson@1501
   107
  val class_of_const: string -> class
wenzelm@3791
   108
end;
clasohm@0
   109
paulson@1501
   110
structure Sign : SIGN =
wenzelm@143
   111
struct
clasohm@0
   112
wenzelm@251
   113
(** datatype sg **)
wenzelm@251
   114
wenzelm@19
   115
datatype sg =
wenzelm@143
   116
  Sg of {
wenzelm@3791
   117
    tsig: Type.type_sig,                        (*order-sorted signature of types*)
wenzelm@3805
   118
    const_tab: typ Symtab.table,                (*type schemes of constants*)
wenzelm@3791
   119
    syn: Syntax.syntax,                         (*syntax for parsing and printing*)
wenzelm@3805
   120
    path: string list,                     	(*current name space entry prefix*)
wenzelm@3791
   121
    spaces: (string * NameSpace.T) list,   	(*name spaces for consts, types etc.*)
wenzelm@3791
   122
    stamps: string ref list};                   (*unique theory indentifier*)
wenzelm@3791
   123
wenzelm@3791
   124
    (*the "ref" in stamps ensures that no two signatures are identical
wenzelm@3791
   125
      -- it is impossible to forge a signature*)
clasohm@0
   126
clasohm@0
   127
fun rep_sg (Sg args) = args;
wenzelm@402
   128
val tsig_of = #tsig o rep_sg;
clasohm@0
   129
nipkow@206
   130
wenzelm@2979
   131
(* inclusion and equality *)
paulson@2180
   132
wenzelm@2185
   133
local
wenzelm@2185
   134
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   135
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   136
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   137
wenzelm@2185
   138
  fun subset_stamp ([], ys) = true
wenzelm@2185
   139
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   140
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   141
wenzelm@2185
   142
  (*fast partial test*)
wenzelm@2185
   143
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   144
    | fast_sub (_, []) = false
wenzelm@2185
   145
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   146
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   147
        else fast_sub (x :: xs, ys);
wenzelm@2185
   148
in
wenzelm@2185
   149
  fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   150
    s1 = s2 orelse subset_stamp (s1, s2);
wenzelm@2185
   151
wenzelm@2185
   152
  fun fast_subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   153
    s1 = s2 orelse fast_sub (s1, s2);
paulson@2180
   154
wenzelm@2185
   155
  fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   156
    s1 = s2 orelse (subset_stamp (s1, s2) andalso subset_stamp (s2, s1));
wenzelm@2185
   157
end;
wenzelm@2185
   158
wenzelm@402
   159
wenzelm@2185
   160
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   161
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   162
  same version of it*)
wenzelm@2185
   163
fun same_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   164
  eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   165
wenzelm@2185
   166
(*test for drafts*)
wenzelm@386
   167
fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
wenzelm@386
   168
  | is_draft _ = false;
wenzelm@386
   169
clasohm@0
   170
wenzelm@402
   171
(* consts *)
wenzelm@402
   172
wenzelm@2979
   173
fun const_type (Sg {const_tab, ...}) c = Symtab.lookup (const_tab, c);
wenzelm@386
   174
wenzelm@386
   175
wenzelm@620
   176
(* classes and sorts *)
wenzelm@402
   177
wenzelm@402
   178
val classes = #classes o Type.rep_tsig o tsig_of;
wenzelm@402
   179
wenzelm@402
   180
val subsort = Type.subsort o tsig_of;
wenzelm@402
   181
val norm_sort = Type.norm_sort o tsig_of;
wenzelm@1216
   182
val nonempty_sort = Type.nonempty_sort o tsig_of;
wenzelm@402
   183
wenzelm@3791
   184
wenzelm@3791
   185
wenzelm@3791
   186
(** name spaces **)
wenzelm@3791
   187
wenzelm@3805
   188
(*prune names on output by default*)
wenzelm@3791
   189
val long_names = ref false;
wenzelm@3791
   190
wenzelm@3791
   191
wenzelm@3791
   192
(* kinds *)
wenzelm@3791
   193
wenzelm@3791
   194
val classK = "class";
wenzelm@3791
   195
val typeK = "type";
wenzelm@3791
   196
val constK = "const";
wenzelm@3791
   197
wenzelm@3791
   198
wenzelm@3791
   199
(* add and retrieve names *)
wenzelm@3791
   200
wenzelm@3791
   201
fun space_of spaces kind =
wenzelm@3791
   202
  if_none (assoc (spaces, kind)) NameSpace.empty;
wenzelm@3791
   203
wenzelm@3791
   204
(*input and output of qualified names*)
wenzelm@3791
   205
fun intrn spaces kind = NameSpace.lookup (space_of spaces kind);
wenzelm@3791
   206
fun extrn spaces kind = NameSpace.prune (space_of spaces kind);
wenzelm@3791
   207
wenzelm@3791
   208
(*add names*)
wenzelm@3791
   209
fun add_names spaces kind names =
wenzelm@3791
   210
  let val space' = NameSpace.extend (names, space_of spaces kind) in
wenzelm@3791
   211
    overwrite (spaces, (kind, space'))
wenzelm@3791
   212
  end;
wenzelm@3791
   213
wenzelm@3810
   214
(*make full names*)
wenzelm@3810
   215
fun full path name = NameSpace.pack (path @ NameSpace.unpack name);
wenzelm@3791
   216
wenzelm@3791
   217
wenzelm@3791
   218
(* intern / extern names *)
wenzelm@3791
   219
wenzelm@3791
   220
local
wenzelm@3791
   221
wenzelm@3791
   222
  fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs)
wenzelm@3791
   223
    | add_typ_classes (TFree (_, S), cs) = S union cs
wenzelm@3791
   224
    | add_typ_classes (TVar (_, S), cs) = S union cs;
wenzelm@3791
   225
wenzelm@3791
   226
  fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs)
wenzelm@3791
   227
    | add_typ_tycons (_, cs) = cs;
wenzelm@3791
   228
wenzelm@3791
   229
  val add_term_classes = it_term_types add_typ_classes;
wenzelm@3791
   230
  val add_term_tycons = it_term_types add_typ_tycons;
wenzelm@3791
   231
wenzelm@3791
   232
  fun add_term_consts (Const (c, _), cs) = c ins cs
wenzelm@3791
   233
    | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@3791
   234
    | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@3791
   235
    | add_term_consts (_, cs) = cs;
wenzelm@3791
   236
wenzelm@3791
   237
wenzelm@3791
   238
  (*map classes, tycons*)
wenzelm@3791
   239
  fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@3791
   240
    | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@3791
   241
    | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@3791
   242
wenzelm@3791
   243
  (*map classes, tycons, consts*)
wenzelm@3791
   244
  fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@3791
   245
    | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@3791
   246
    | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@3791
   247
    | map_term _ _ _ (t as Bound _) = t
wenzelm@3791
   248
    | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@3791
   249
    | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@3791
   250
wenzelm@3791
   251
  (*prepare mapping of names*)
wenzelm@3791
   252
  fun mapping f add_xs t =
wenzelm@3791
   253
    let
wenzelm@3791
   254
      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
wenzelm@3791
   255
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   256
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   257
    in lookup end;
wenzelm@3791
   258
wenzelm@3791
   259
  (*intern / extern typ*)
wenzelm@3791
   260
  fun trn_typ trn T =
wenzelm@3791
   261
    T |> map_typ
wenzelm@3791
   262
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   263
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   264
wenzelm@3791
   265
  (*intern / extern term*)
wenzelm@3791
   266
  fun trn_term trn t =
wenzelm@3791
   267
    t |> map_term
wenzelm@3791
   268
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   269
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   270
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   271
wenzelm@3791
   272
wenzelm@3791
   273
  fun spaces_of (Sg {spaces, ...}) = spaces;
wenzelm@3791
   274
wenzelm@3791
   275
in
wenzelm@3791
   276
wenzelm@3791
   277
  fun intrn_class spaces = intrn spaces classK;
wenzelm@3791
   278
  fun extrn_class spaces = extrn spaces classK;
wenzelm@3791
   279
  val intrn_sort = map o intrn_class;
wenzelm@3791
   280
  val extrn_sort = map o extrn_class;
wenzelm@3791
   281
  val intrn_typ = trn_typ o intrn;
wenzelm@3791
   282
  val extrn_typ = trn_typ o extrn;
wenzelm@3791
   283
  val intrn_term = trn_term o intrn;
wenzelm@3791
   284
  val extrn_term = trn_term o extrn;
wenzelm@3791
   285
wenzelm@3805
   286
  fun intrn_tycons spaces T =
wenzelm@3805
   287
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   288
wenzelm@3810
   289
  val intern = intrn o spaces_of;
wenzelm@3810
   290
  val extern = intrn o spaces_of;
wenzelm@3791
   291
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   292
  val extern_class = extrn_class o spaces_of;
wenzelm@3791
   293
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   294
  val extern_sort = extrn_sort o spaces_of;
wenzelm@3791
   295
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   296
  val extern_typ = extrn_typ o spaces_of;
wenzelm@3791
   297
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   298
  val extern_term = extrn_term o spaces_of;
wenzelm@3791
   299
wenzelm@3791
   300
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   301
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3791
   302
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   303
wenzelm@3810
   304
  fun full_name (Sg {path, ...}) = full path;
wenzelm@3791
   305
end;
wenzelm@620
   306
wenzelm@402
   307
clasohm@0
   308
wenzelm@251
   309
(** print signature **)
wenzelm@251
   310
paulson@2228
   311
fun stamp_names stamps = rev (map ! stamps);
wenzelm@386
   312
wenzelm@251
   313
fun print_sg sg =
wenzelm@251
   314
  let
wenzelm@3791
   315
    val Sg {syn, ...} = sg;
wenzelm@3791
   316
wenzelm@3791
   317
    fun prt_typ ty = Pretty.quote (Syntax.pretty_typ syn ty);
wenzelm@3791
   318
    fun prt_cls c = Syntax.pretty_sort syn [c];
wenzelm@3791
   319
wenzelm@3791
   320
    fun pretty_space (kind, space) = Pretty.block (Pretty.breaks
wenzelm@3791
   321
      (map Pretty.str (kind ^ ":" :: map quote (NameSpace.dest space))));
wenzelm@251
   322
wenzelm@3791
   323
    fun pretty_classes cs = Pretty.block
wenzelm@3791
   324
      (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
wenzelm@251
   325
wenzelm@3791
   326
    fun pretty_classrel (c, cs) = Pretty.block
wenzelm@3791
   327
      (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@3791
   328
        Pretty.commas (map prt_cls cs));
wenzelm@3791
   329
wenzelm@3791
   330
    fun pretty_default S = Pretty.block
wenzelm@3791
   331
      [Pretty.str "default:", Pretty.brk 1, Syntax.pretty_sort syn S];
wenzelm@251
   332
nipkow@963
   333
    fun pretty_ty (ty, n) = Pretty.str (ty ^ " " ^ string_of_int n);
wenzelm@251
   334
wenzelm@3791
   335
    fun pretty_abbr (ty, (vs, rhs)) = Pretty.block
wenzelm@3791
   336
      [prt_typ (Type (ty, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@3791
   337
        Pretty.str " =", Pretty.brk 1, prt_typ rhs];
wenzelm@251
   338
wenzelm@3791
   339
    fun pretty_arity ty (c, []) = Pretty.block
wenzelm@3791
   340
          [Pretty.str (ty ^ " ::"), Pretty.brk 1, prt_cls c]
wenzelm@3791
   341
      | pretty_arity ty (c, Ss) = Pretty.block
wenzelm@251
   342
          [Pretty.str (ty ^ " ::"), Pretty.brk 1,
wenzelm@3791
   343
            Pretty.list "(" ")" (map (Syntax.pretty_sort syn) Ss),
wenzelm@3791
   344
            Pretty.brk 1, prt_cls c];
wenzelm@251
   345
nipkow@963
   346
    fun pretty_arities (ty, ars) = map (pretty_arity ty) ars;
wenzelm@251
   347
wenzelm@3791
   348
    fun pretty_const (c, ty) = Pretty.block
wenzelm@3791
   349
      [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ ty];
wenzelm@251
   350
wenzelm@3805
   351
    val Sg {tsig, const_tab, syn = _, path, spaces, stamps} = sg;
wenzelm@3810
   352
    val spaces' = sort (fn ((k1, _), (k2, _)) => k1 < k2) spaces;
wenzelm@2963
   353
    val {classes, classrel, default, tycons, abbrs, arities} =
wenzelm@2185
   354
      Type.rep_tsig tsig;
wenzelm@251
   355
  in
wenzelm@386
   356
    Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
wenzelm@3791
   357
    Pretty.writeln (Pretty.strs ["name entry path:", NameSpace.pack path]);
wenzelm@3810
   358
    Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_space spaces'));
wenzelm@3791
   359
    Pretty.writeln (pretty_classes classes);
wenzelm@3791
   360
    Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
wenzelm@251
   361
    Pretty.writeln (pretty_default default);
wenzelm@3791
   362
    Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
wenzelm@3791
   363
    Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
wenzelm@3791
   364
    Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
wenzelm@3791
   365
    Pretty.writeln (Pretty.big_list "consts:" (map pretty_const (Symtab.dest const_tab)))
wenzelm@251
   366
  end;
wenzelm@251
   367
wenzelm@251
   368
wenzelm@562
   369
fun pretty_sg (Sg {stamps, ...}) =
wenzelm@562
   370
  Pretty.str_list "{" "}" (stamp_names stamps);
wenzelm@562
   371
wenzelm@562
   372
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@251
   373
wenzelm@251
   374
wenzelm@251
   375
wenzelm@251
   376
(** pretty printing of terms and types **)
wenzelm@251
   377
wenzelm@3791
   378
fun pretty_term (Sg {syn, spaces, stamps, ...}) t =
wenzelm@3791
   379
  Syntax.pretty_term syn
wenzelm@3791
   380
    ("CPure" mem_string (map ! stamps))
wenzelm@3791
   381
    (if ! long_names then t else extrn_term spaces t);
clasohm@922
   382
wenzelm@3791
   383
fun pretty_typ (Sg {syn, spaces, ...}) T =
wenzelm@3791
   384
  Syntax.pretty_typ syn
wenzelm@3791
   385
    (if ! long_names then T else extrn_typ spaces T);
wenzelm@251
   386
wenzelm@3791
   387
fun pretty_sort (Sg {syn, spaces, ...}) S =
wenzelm@3791
   388
  Syntax.pretty_sort syn
wenzelm@3791
   389
    (if ! long_names then S else extrn_sort spaces S);
clasohm@922
   390
wenzelm@3791
   391
wenzelm@3791
   392
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3791
   393
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3791
   394
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@251
   395
wenzelm@251
   396
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@251
   397
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
clasohm@0
   398
clasohm@0
   399
wenzelm@169
   400
wenzelm@562
   401
(** read types **)  (*exception ERROR*)
wenzelm@562
   402
wenzelm@562
   403
fun err_in_type s =
wenzelm@562
   404
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   405
wenzelm@3791
   406
fun read_raw_typ syn tsig spaces def_sort str =
wenzelm@3791
   407
  intrn_tycons spaces
wenzelm@3791
   408
    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str
wenzelm@3791
   409
      handle ERROR => err_in_type str);
wenzelm@3791
   410
  
wenzelm@562
   411
(*read and certify typ wrt a signature*)
wenzelm@3791
   412
fun read_typ (Sg {tsig, syn, spaces, ...}, def_sort) str =
wenzelm@3791
   413
  Type.cert_typ tsig (read_raw_typ syn tsig spaces def_sort str)
wenzelm@3552
   414
    handle TYPE (msg, _, _) => (error_msg msg; err_in_type str);
wenzelm@562
   415
wenzelm@562
   416
wenzelm@562
   417
wenzelm@386
   418
(** certify types and terms **)   (*exception TYPE*)
wenzelm@251
   419
wenzelm@2185
   420
fun certify_typ (Sg {tsig, ...}) ty = Type.cert_typ tsig ty;
wenzelm@251
   421
wenzelm@2979
   422
(*check for duplicate TVars with distinct sorts*)
wenzelm@2979
   423
fun nodup_TVars (tvars, T) =
wenzelm@2979
   424
  (case T of
wenzelm@2979
   425
    Type (_, Ts) => nodup_TVars_list (tvars, Ts)
wenzelm@2979
   426
  | TFree _ => tvars
wenzelm@2979
   427
  | TVar (v as (a, S)) =>
wenzelm@2979
   428
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   429
        Some S' =>
wenzelm@2979
   430
          if S = S' then tvars
wenzelm@3791
   431
          else raise TYPE ("Type variable " ^ Syntax.string_of_vname a ^
wenzelm@3791
   432
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@2979
   433
      | None => v :: tvars))
wenzelm@2979
   434
(*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
wenzelm@2979
   435
and nodup_TVars_list (tvars, []) = tvars
wenzelm@2979
   436
  | nodup_TVars_list (tvars, T :: Ts) =
wenzelm@2979
   437
      nodup_TVars_list (nodup_TVars (tvars, T), Ts);
nipkow@1494
   438
wenzelm@2979
   439
(*check for duplicate Vars with distinct types*)
nipkow@1494
   440
fun nodup_Vars tm =
wenzelm@2979
   441
  let
wenzelm@2979
   442
    fun nodups vars tvars tm =
wenzelm@2979
   443
      (case tm of
wenzelm@2979
   444
        Const (c, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   445
      | Free (a, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   446
      | Var (v as (ixn, T)) =>
wenzelm@2979
   447
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   448
            Some T' =>
wenzelm@2979
   449
              if T = T' then (vars, nodup_TVars (tvars, T))
wenzelm@3791
   450
              else raise TYPE ("Variable " ^ Syntax.string_of_vname ixn ^
wenzelm@3791
   451
                " has two distinct types", [T', T], [])
wenzelm@2979
   452
          | None => (v :: vars, tvars))
wenzelm@2979
   453
      | Bound _ => (vars, tvars)
wenzelm@3791
   454
      | Abs (_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
wenzelm@2979
   455
      | s $ t =>
wenzelm@2979
   456
          let val (vars',tvars') = nodups vars tvars s in
wenzelm@2979
   457
            nodups vars' tvars' t
wenzelm@2979
   458
          end);
wenzelm@2979
   459
  in nodups [] [] tm; () end;
wenzelm@2979
   460
wenzelm@251
   461
wenzelm@386
   462
fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
wenzelm@386
   463
  | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
wenzelm@386
   464
  | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
clasohm@0
   465
wenzelm@2979
   466
wenzelm@386
   467
fun certify_term (sg as Sg {tsig, ...}) tm =
wenzelm@251
   468
  let
wenzelm@251
   469
    fun valid_const a T =
wenzelm@386
   470
      (case const_type sg a of
wenzelm@2185
   471
        Some U => Type.typ_instance (tsig, T, U)
wenzelm@251
   472
      | _ => false);
wenzelm@169
   473
wenzelm@251
   474
    fun atom_err (Const (a, T)) =
wenzelm@251
   475
          if valid_const a T then None
wenzelm@251
   476
          else Some ("Illegal type for constant " ^ quote a ^ " :: " ^
wenzelm@251
   477
            quote (string_of_typ sg T))
wenzelm@251
   478
      | atom_err (Var ((x, i), _)) =
wenzelm@251
   479
          if i < 0 then Some ("Negative index for Var " ^ quote x) else None
wenzelm@251
   480
      | atom_err _ = None;
wenzelm@251
   481
wenzelm@251
   482
    val norm_tm =
wenzelm@2185
   483
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@2185
   484
        [] => map_term_types (Type.norm_typ tsig) tm
wenzelm@3791
   485
      | errs => raise TYPE (cat_lines errs, [], [tm]));
nipkow@1494
   486
    val _ = nodup_Vars norm_tm;
wenzelm@251
   487
  in
wenzelm@386
   488
    (case mapfilt_atoms atom_err norm_tm of
wenzelm@251
   489
      [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   490
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   491
  end;
wenzelm@251
   492
wenzelm@251
   493
wenzelm@251
   494
wenzelm@583
   495
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   496
wenzelm@2979
   497
(*
wenzelm@2979
   498
  ts: list of alternative parses (hopefully only one is type-correct)
wenzelm@2979
   499
  T: expected type
wenzelm@2979
   500
wenzelm@2979
   501
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   502
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   503
  used: list of already used type variables
wenzelm@2979
   504
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@2979
   505
*)
wenzelm@2979
   506
wenzelm@2979
   507
fun infer_types sg def_type def_sort used freeze (ts, T) =
wenzelm@251
   508
  let
wenzelm@562
   509
    val Sg {tsig, ...} = sg;
wenzelm@3810
   510
    val prt =
wenzelm@3810
   511
      setmp Syntax.show_brackets true
wenzelm@3810
   512
        (setmp long_names true (pretty_term sg));
wenzelm@3810
   513
    val prT = setmp long_names true (pretty_typ sg);
wenzelm@3791
   514
    val infer = Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@3791
   515
      (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze;
wenzelm@169
   516
wenzelm@2979
   517
    val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg;
clasohm@623
   518
wenzelm@2979
   519
    fun warn () =
wenzelm@2979
   520
      if length ts > 1 andalso length ts <= ! Syntax.ambiguity_level
nipkow@952
   521
      then (*no warning shown yet*)
wenzelm@3805
   522
        warning "Got more than one parse tree.\n\
wenzelm@3805
   523
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   524
      else ();
clasohm@623
   525
wenzelm@2979
   526
    datatype result =
wenzelm@2979
   527
      One of int * term * (indexname * typ) list |
wenzelm@2979
   528
      Errs of string list |
wenzelm@2979
   529
      Ambigs of term list;
clasohm@623
   530
wenzelm@2979
   531
    fun process_term (res, (t, i)) =
wenzelm@2979
   532
      let val ([u], tye) = infer [T'] [t] in
wenzelm@2979
   533
        (case res of
wenzelm@2979
   534
          One (_, t0, _) => Ambigs ([u, t0])
wenzelm@2979
   535
        | Errs _ => One (i, u, tye)
wenzelm@2979
   536
        | Ambigs us => Ambigs (u :: us))
wenzelm@2979
   537
      end handle TYPE (msg, _, _) =>
wenzelm@2979
   538
        (case res of
wenzelm@2979
   539
          Errs errs => Errs (msg :: errs)
wenzelm@2979
   540
        | _ => res);
wenzelm@2979
   541
  in
wenzelm@2979
   542
    (case foldl process_term (Errs [], ts ~~ (0 upto (length ts - 1))) of
wenzelm@2979
   543
      One res =>
wenzelm@2979
   544
       (if length ts > ! Syntax.ambiguity_level then
wenzelm@3552
   545
          warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@3805
   546
            \You may still want to disambiguate your grammar or your input."
wenzelm@2979
   547
        else (); res)
wenzelm@2979
   548
    | Errs errs => (warn (); error (cat_lines errs))
wenzelm@2979
   549
    | Ambigs us =>
wenzelm@3791
   550
        (warn (); error ("More than one term is type correct:\n" ^
wenzelm@2979
   551
          (cat_lines (map (Pretty.string_of o prt) us)))))
clasohm@623
   552
  end;
wenzelm@251
   553
wenzelm@251
   554
wenzelm@2979
   555
clasohm@623
   556
(** extend signature **)    (*exception ERROR*)
clasohm@623
   557
wenzelm@620
   558
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   559
wenzelm@3791
   560
fun decls_of path name_of mfixs =
wenzelm@3810
   561
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   562
wenzelm@3791
   563
fun no_read _ _ _ decl = decl;
wenzelm@386
   564
wenzelm@386
   565
wenzelm@386
   566
(* add default sort *)
wenzelm@386
   567
wenzelm@3791
   568
fun ext_defsort int (syn, tsig, ctab, (path, spaces)) S =
wenzelm@3791
   569
  (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
wenzelm@3791
   570
    ctab, (path, spaces));
wenzelm@386
   571
wenzelm@386
   572
wenzelm@386
   573
(* add type constructors *)
wenzelm@386
   574
wenzelm@3791
   575
fun ext_types (syn, tsig, ctab, (path, spaces)) types =
wenzelm@3791
   576
  let val decls = decls_of path Syntax.type_name types in
wenzelm@3791
   577
    (Syntax.extend_type_gram syn types,
wenzelm@3791
   578
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3791
   579
      (path, add_names spaces typeK (map fst decls)))
wenzelm@3791
   580
  end;
wenzelm@386
   581
wenzelm@386
   582
wenzelm@386
   583
(* add type abbreviations *)
wenzelm@386
   584
wenzelm@3791
   585
fun read_abbr syn tsig spaces (t, vs, rhs_src) =
wenzelm@3791
   586
  (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
wenzelm@386
   587
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   588
wenzelm@3791
   589
fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces)) abbrs =
wenzelm@386
   590
  let
wenzelm@386
   591
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@3791
   592
    val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   593
wenzelm@3791
   594
    val abbrs' =
wenzelm@3791
   595
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   596
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   597
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@3805
   598
    val decls = map (rd_abbr syn' tsig spaces') abbrs';
wenzelm@386
   599
  in
wenzelm@3805
   600
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'))
wenzelm@386
   601
  end;
wenzelm@386
   602
wenzelm@3791
   603
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   604
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   605
wenzelm@386
   606
wenzelm@386
   607
(* add type arities *)
wenzelm@386
   608
wenzelm@3791
   609
fun ext_arities int (syn, tsig, ctab, (path, spaces)) arities =
wenzelm@386
   610
  let
wenzelm@3791
   611
    fun intrn_arity (c, Ss, S) =
wenzelm@3791
   612
      (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
wenzelm@3791
   613
    val intrn = if int then map intrn_arity else I;
wenzelm@3791
   614
    val tsig' = Type.ext_tsig_arities tsig (intrn arities);
wenzelm@3791
   615
    val log_types = Type.logical_types tsig';
wenzelm@386
   616
  in
wenzelm@3791
   617
    (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces))
wenzelm@386
   618
  end;
wenzelm@386
   619
wenzelm@386
   620
wenzelm@386
   621
(* add term constants and syntax *)
wenzelm@386
   622
wenzelm@3805
   623
fun const_name path c mx =
wenzelm@3810
   624
  full path (Syntax.const_name c mx);
wenzelm@3805
   625
wenzelm@386
   626
fun err_in_const c =
wenzelm@386
   627
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   628
wenzelm@386
   629
fun err_dup_consts cs =
wenzelm@386
   630
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   631
wenzelm@251
   632
wenzelm@3805
   633
fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@3791
   634
  (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
wenzelm@3805
   635
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   636
wenzelm@3791
   637
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces)) raw_consts =
wenzelm@386
   638
  let
wenzelm@2979
   639
    fun prep_const (c, ty, mx) =
wenzelm@3791
   640
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   641
        handle TYPE (msg, _, _) =>
wenzelm@3805
   642
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   643
wenzelm@3805
   644
    val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts;
wenzelm@386
   645
    val decls =
wenzelm@386
   646
      if syn_only then []
wenzelm@3791
   647
      else decls_of path Syntax.const_name consts;
wenzelm@386
   648
  in
wenzelm@2197
   649
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@386
   650
      Symtab.extend_new (ctab, decls)
wenzelm@3791
   651
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3791
   652
      (path, add_names spaces constK (map fst decls)))
wenzelm@386
   653
  end;
wenzelm@386
   654
wenzelm@3791
   655
val ext_consts_i = ext_cnsts no_read false ("", true);
wenzelm@2359
   656
val ext_consts = ext_cnsts read_const false ("", true);
wenzelm@3791
   657
val ext_syntax_i = ext_cnsts no_read true ("", true);
wenzelm@2359
   658
val ext_syntax = ext_cnsts read_const true ("", true);
wenzelm@3791
   659
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts;
wenzelm@2197
   660
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   661
wenzelm@386
   662
wenzelm@386
   663
(* add type classes *)
wenzelm@386
   664
wenzelm@386
   665
fun const_of_class c = c ^ "_class";
wenzelm@386
   666
wenzelm@386
   667
fun class_of_const c_class =
wenzelm@386
   668
  let
wenzelm@3791
   669
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
   670
  in
wenzelm@386
   671
    if const_of_class c = c_class then c
wenzelm@3791
   672
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
   673
  end;
wenzelm@386
   674
wenzelm@386
   675
wenzelm@3791
   676
fun ext_classes int (syn, tsig, ctab, (path, spaces)) classes =
wenzelm@386
   677
  let
wenzelm@562
   678
    val names = map fst classes;
wenzelm@386
   679
    val consts =
wenzelm@386
   680
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
   681
wenzelm@3810
   682
    val full_names = map (full path) names;
wenzelm@3791
   683
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
   684
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
   685
    val classes' =
wenzelm@3791
   686
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
   687
  in
wenzelm@386
   688
    ext_consts_i
wenzelm@3791
   689
      (Syntax.extend_consts syn names,
wenzelm@3791
   690
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'))
wenzelm@3791
   691
    consts
wenzelm@386
   692
  end;
wenzelm@386
   693
wenzelm@386
   694
wenzelm@2963
   695
(* add to classrel *)
wenzelm@421
   696
wenzelm@3791
   697
fun ext_classrel int (syn, tsig, ctab, (path, spaces)) pairs =
wenzelm@3791
   698
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3791
   699
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces))
wenzelm@3791
   700
  end;
wenzelm@421
   701
wenzelm@421
   702
wenzelm@1159
   703
(* add to syntax *)
wenzelm@386
   704
wenzelm@3791
   705
fun ext_syn extfun (syn, tsig, ctab, names) args =
wenzelm@3791
   706
  (extfun syn args, tsig, ctab, names);
wenzelm@3791
   707
wenzelm@3791
   708
wenzelm@3791
   709
(* add to path *)
wenzelm@3791
   710
wenzelm@3791
   711
fun ext_path (syn, tsig, ctab, (path, spaces)) elem =
wenzelm@3791
   712
  let
wenzelm@3791
   713
    val path' =
wenzelm@3791
   714
      if elem = ".." andalso not (null path) then fst (split_last path)
wenzelm@3791
   715
      else if elem = "/" then []
wenzelm@3791
   716
      else path @ NameSpace.unpack elem;
wenzelm@3791
   717
  in
wenzelm@3791
   718
    (syn, tsig, ctab, (path', spaces))
wenzelm@3791
   719
  end;      
wenzelm@3791
   720
wenzelm@3791
   721
wenzelm@3791
   722
(* add to name space *)
wenzelm@3791
   723
wenzelm@3791
   724
fun ext_space (syn, tsig, ctab, (path, spaces)) (kind, names) =
wenzelm@3791
   725
  (syn, tsig, ctab, (path, add_names spaces kind names));
wenzelm@386
   726
wenzelm@386
   727
wenzelm@386
   728
(* build signature *)
wenzelm@386
   729
wenzelm@386
   730
fun ext_stamps stamps name =
wenzelm@386
   731
  let
wenzelm@386
   732
    val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@386
   733
  in
wenzelm@386
   734
    if exists (equal name o !) stmps then
wenzelm@386
   735
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@402
   736
    else ref name :: stmps
wenzelm@386
   737
  end;
wenzelm@386
   738
wenzelm@3791
   739
fun make_sign (syn, tsig, ctab, (path, spaces)) stamps name =
wenzelm@3791
   740
  Sg {tsig = tsig, const_tab = ctab, syn = syn, path = path, spaces = spaces,
wenzelm@386
   741
    stamps = ext_stamps stamps name};
wenzelm@386
   742
wenzelm@3791
   743
fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, path, spaces, stamps}) =
wenzelm@3791
   744
  make_sign (extfun (syn, tsig, const_tab, (path, spaces)) decls) stamps name;
wenzelm@386
   745
wenzelm@386
   746
wenzelm@386
   747
(* the external interfaces *)
wenzelm@386
   748
wenzelm@3791
   749
val add_classes      = extend_sign (ext_classes true) "#";
wenzelm@3791
   750
val add_classes_i    = extend_sign (ext_classes false) "#";
wenzelm@3791
   751
val add_classrel     = extend_sign (ext_classrel true) "#";
wenzelm@3791
   752
val add_classrel_i   = extend_sign (ext_classrel false) "#";
wenzelm@3791
   753
val add_defsort      = extend_sign (ext_defsort true) "#";
wenzelm@3791
   754
val add_defsort_i    = extend_sign (ext_defsort false) "#";
wenzelm@2197
   755
val add_types        = extend_sign ext_types "#";
wenzelm@2197
   756
val add_tyabbrs      = extend_sign ext_tyabbrs "#";
wenzelm@2197
   757
val add_tyabbrs_i    = extend_sign ext_tyabbrs_i "#";
wenzelm@3791
   758
val add_arities      = extend_sign (ext_arities true) "#";
wenzelm@3791
   759
val add_arities_i    = extend_sign (ext_arities false) "#";
wenzelm@2197
   760
val add_consts       = extend_sign ext_consts "#";
wenzelm@2197
   761
val add_consts_i     = extend_sign ext_consts_i "#";
wenzelm@2197
   762
val add_syntax       = extend_sign ext_syntax "#";
wenzelm@2197
   763
val add_syntax_i     = extend_sign ext_syntax_i "#";
wenzelm@2197
   764
val add_modesyntax   = extend_sign ext_modesyntax "#";
wenzelm@2197
   765
val add_modesyntax_i = extend_sign ext_modesyntax_i "#";
wenzelm@2197
   766
val add_trfuns       = extend_sign (ext_syn Syntax.extend_trfuns) "#";
wenzelm@2385
   767
val add_trfunsT      = extend_sign (ext_syn Syntax.extend_trfunsT) "#";
wenzelm@2693
   768
val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
wenzelm@2197
   769
val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
wenzelm@2197
   770
val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@3791
   771
val add_path         = extend_sign ext_path "#";
wenzelm@3791
   772
val add_space        = extend_sign ext_space "#";
wenzelm@3791
   773
fun add_name name sg = extend_sign K name () sg;
wenzelm@386
   774
wenzelm@386
   775
val make_draft = add_name "#";
wenzelm@386
   776
wenzelm@386
   777
wenzelm@386
   778
wenzelm@3805
   779
(** merge signatures **)    (*exception TERM*)
wenzelm@143
   780
wenzelm@3805
   781
fun merge_aux (sg1, sg2) =
wenzelm@2185
   782
  if fast_subsig (sg2, sg1) then sg1
wenzelm@2185
   783
  else if fast_subsig (sg1, sg2) then sg2
wenzelm@2185
   784
  else if subsig (sg2, sg1) then sg1
wenzelm@251
   785
  else if subsig (sg1, sg2) then sg2
wenzelm@386
   786
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3791
   787
    raise TERM ("Illegal merge of draft signatures", [])
wenzelm@251
   788
  else
wenzelm@251
   789
    (*neither is union already; must form union*)
wenzelm@251
   790
    let
wenzelm@251
   791
      val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
wenzelm@3791
   792
        path, spaces = spaces1, stamps = stamps1} = sg1;
wenzelm@251
   793
      val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
wenzelm@3791
   794
        path = _, spaces = spaces2, stamps = stamps2} = sg2;
wenzelm@251
   795
wenzelm@386
   796
wenzelm@402
   797
      val stamps = merge_rev_lists stamps1 stamps2;
wenzelm@402
   798
      val _ =
wenzelm@402
   799
        (case duplicates (stamp_names stamps) of
wenzelm@402
   800
          [] => ()
wenzelm@3791
   801
        | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@3791
   802
            ^ commas_quote dups, []));
wenzelm@402
   803
wenzelm@2185
   804
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
   805
wenzelm@251
   806
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
   807
        handle Symtab.DUPS cs =>
wenzelm@3791
   808
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@386
   809
wenzelm@386
   810
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@3791
   811
wenzelm@3791
   812
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
   813
      val spaces =
wenzelm@3791
   814
        kinds ~~
wenzelm@3791
   815
          ListPair.map NameSpace.merge
wenzelm@3791
   816
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@251
   817
    in
wenzelm@3791
   818
      Sg {tsig = tsig, const_tab = const_tab, syn = syn,
wenzelm@3791
   819
        path = path, spaces = spaces, stamps = stamps}
wenzelm@251
   820
    end;
wenzelm@143
   821
wenzelm@3805
   822
fun merge sgs =
wenzelm@3805
   823
  (case handle_error merge_aux sgs of
wenzelm@3805
   824
    OK sg => sg
wenzelm@3805
   825
  | Error msg => raise TERM (msg, []));
wenzelm@3805
   826
clasohm@0
   827
clasohm@0
   828
wenzelm@251
   829
(** the Pure signature **)
clasohm@0
   830
clasohm@922
   831
val proto_pure =
wenzelm@3791
   832
  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], [])) [] "#"
wenzelm@410
   833
  |> add_types
wenzelm@386
   834
   (("fun", 2, NoSyn) ::
wenzelm@386
   835
    ("prop", 0, NoSyn) ::
wenzelm@386
   836
    ("itself", 1, NoSyn) ::
wenzelm@562
   837
    Syntax.pure_types)
wenzelm@3805
   838
  |> add_classes_i [(NameSpace.base logicC, [])]
wenzelm@3791
   839
  |> add_defsort_i logicS
wenzelm@3791
   840
  |> add_arities_i
wenzelm@386
   841
   [("fun", [logicS, logicS], logicS),
wenzelm@386
   842
    ("prop", [], logicS),
wenzelm@386
   843
    ("itself", [logicS], logicS)]
wenzelm@562
   844
  |> add_syntax Syntax.pure_syntax
wenzelm@2359
   845
  |> add_modesyntax (("symbols", true), Syntax.pure_sym_syntax)
wenzelm@410
   846
  |> add_trfuns Syntax.pure_trfuns
wenzelm@2693
   847
  |> add_trfunsT Syntax.pure_trfunsT
wenzelm@3810
   848
  |> add_syntax
wenzelm@3810
   849
   [("==>", "[prop, prop] => prop", Delimfix "op ==>")]
wenzelm@410
   850
  |> add_consts
wenzelm@2197
   851
   [("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)),
wenzelm@2197
   852
    ("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)),
wenzelm@2211
   853
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
clasohm@865
   854
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@573
   855
    ("TYPE", "'a itself", NoSyn)]
clasohm@922
   856
  |> add_name "ProtoPure";
clasohm@922
   857
clasohm@922
   858
val pure = proto_pure
clasohm@1180
   859
  |> add_syntax Syntax.pure_appl_syntax
wenzelm@410
   860
  |> add_name "Pure";
clasohm@0
   861
clasohm@922
   862
val cpure = proto_pure
clasohm@1180
   863
  |> add_syntax Syntax.pure_applC_syntax
clasohm@922
   864
  |> add_name "CPure";
clasohm@0
   865
wenzelm@3791
   866
clasohm@0
   867
end;
wenzelm@3791
   868
wenzelm@3791
   869
wenzelm@3791
   870
val long_names = Sign.long_names;