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