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