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