src/Pure/sign.ML
author wenzelm
Thu Oct 11 19:22:00 2001 +0200 (2001-10-11)
changeset 11720 5341e38309e8
parent 11501 3b6415035d1a
child 12068 469f372d63db
permissions -rw-r--r--
added certify_tyname;
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@3956
     8
(*base names*)
wenzelm@3956
     9
type bstring = string;
wenzelm@3956
    10
type bclass = class;
wenzelm@3956
    11
(*external forms -- partially qualified names*)
wenzelm@3805
    12
type xstring = string;
wenzelm@3805
    13
type xclass = class;
wenzelm@3805
    14
type xsort = sort;
wenzelm@3805
    15
type xtyp = typ;
wenzelm@3805
    16
type xterm = term;
wenzelm@3805
    17
wenzelm@19
    18
signature SIGN =
wenzelm@3791
    19
sig
paulson@1501
    20
  type sg
wenzelm@3967
    21
  type sg_ref
wenzelm@4256
    22
  type data
wenzelm@2197
    23
  val rep_sg: sg ->
wenzelm@3975
    24
   {self: sg_ref,
wenzelm@3967
    25
    tsig: Type.type_sig,
wenzelm@2197
    26
    const_tab: typ Symtab.table,
wenzelm@2197
    27
    syn: Syntax.syntax,
wenzelm@11501
    28
    path: string list option,
wenzelm@3791
    29
    spaces: (string * NameSpace.T) list,
wenzelm@4256
    30
    data: data}
wenzelm@3975
    31
  val name_of: sg -> string
wenzelm@3975
    32
  val stamp_names_of: sg -> string list
wenzelm@10932
    33
  val exists_stamp: string -> sg -> bool
wenzelm@3967
    34
  val tsig_of: sg -> Type.type_sig
wenzelm@3967
    35
  val deref: sg_ref -> sg
wenzelm@3967
    36
  val self_ref: sg -> sg_ref
paulson@1501
    37
  val subsig: sg * sg -> bool
wenzelm@9031
    38
  val joinable: sg * sg -> bool
paulson@1501
    39
  val eq_sg: sg * sg -> bool
paulson@1501
    40
  val same_sg: sg * sg -> bool
paulson@1501
    41
  val is_draft: sg -> bool
wenzelm@4951
    42
  val is_stale: sg -> bool
paulson@1501
    43
  val const_type: sg -> string -> typ option
paulson@1501
    44
  val classes: sg -> class list
wenzelm@4844
    45
  val defaultS: sg -> sort
paulson@1501
    46
  val subsort: sg -> sort * sort -> bool
wenzelm@8290
    47
  val nodup_vars: term -> term
paulson@1501
    48
  val norm_sort: sg -> sort -> sort
wenzelm@4568
    49
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    50
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@7640
    51
  val univ_witness: sg -> (typ * sort) option
wenzelm@10443
    52
  val typ_instance: sg -> typ * typ -> bool
wenzelm@3810
    53
  val classK: string
wenzelm@3810
    54
  val typeK: string
wenzelm@3810
    55
  val constK: string
wenzelm@3956
    56
  val full_name: sg -> bstring -> string
wenzelm@4844
    57
  val full_name_path: sg -> string -> bstring -> string
wenzelm@3956
    58
  val base_name: string -> bstring
wenzelm@3810
    59
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    60
  val extern: sg -> string -> string -> xstring
wenzelm@3937
    61
  val cond_extern: sg -> string -> string -> xstring
wenzelm@6845
    62
  val cond_extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list
wenzelm@3805
    63
  val intern_class: sg -> xclass -> class
wenzelm@3805
    64
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    65
  val intern_const: sg -> xstring -> string
wenzelm@3937
    66
  val intern_sort: sg -> xsort -> sort
wenzelm@3937
    67
  val intern_typ: sg -> xtyp -> typ
wenzelm@3937
    68
  val intern_term: sg -> xterm -> term
wenzelm@3937
    69
  val intern_tycons: sg -> xtyp -> typ
paulson@1501
    70
  val pretty_sg: sg -> Pretty.T
wenzelm@4051
    71
  val str_of_sg: sg -> string
paulson@1501
    72
  val pprint_sg: sg -> pprint_args -> unit
paulson@1501
    73
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    74
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    75
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@4249
    76
  val pretty_classrel: sg -> class * class -> Pretty.T
wenzelm@4249
    77
  val pretty_arity: sg -> string * sort list * sort -> Pretty.T
paulson@1501
    78
  val string_of_term: sg -> term -> string
paulson@1501
    79
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    80
  val string_of_sort: sg -> sort -> string
wenzelm@3855
    81
  val str_of_sort: sg -> sort -> string
wenzelm@3855
    82
  val str_of_classrel: sg -> class * class -> string
wenzelm@3855
    83
  val str_of_arity: sg -> string * sort list * sort -> string
paulson@1501
    84
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    85
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@8898
    86
  val certify_class: sg -> class -> class
wenzelm@8898
    87
  val certify_sort: sg -> sort -> sort
paulson@1501
    88
  val certify_typ: sg -> typ -> typ
wenzelm@9504
    89
  val certify_typ_no_norm: sg -> typ -> typ
wenzelm@10443
    90
  val certify_tycon: sg -> string -> string
wenzelm@10443
    91
  val certify_tyabbr: sg -> string -> string
wenzelm@11720
    92
  val certify_tyname: sg -> string -> string
wenzelm@10443
    93
  val certify_const: sg -> string -> string
paulson@1501
    94
  val certify_term: sg -> term -> term * typ * int
wenzelm@8898
    95
  val read_sort: sg -> string -> sort
wenzelm@4227
    96
  val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    97
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@9504
    98
  val read_typ_no_norm: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    99
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
   100
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   101
    -> xterm list * typ -> term * (indexname * typ) list
wenzelm@4249
   102
  val infer_types_simult: sg -> (indexname -> typ option) ->
wenzelm@4249
   103
    (indexname -> sort option) -> string list -> bool
wenzelm@4249
   104
    -> (xterm list * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   105
  val read_def_terms:
wenzelm@8607
   106
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   107
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   108
  val simple_read_term: sg -> typ -> string -> term
wenzelm@3956
   109
  val add_classes: (bclass * xclass list) list -> sg -> sg
wenzelm@3956
   110
  val add_classes_i: (bclass * class list) list -> sg -> sg
wenzelm@3805
   111
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
   112
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@8898
   113
  val add_defsort: string -> sg -> sg
wenzelm@3791
   114
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
   115
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@4844
   116
  val add_nonterminals: bstring list -> sg -> sg
wenzelm@3956
   117
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
   118
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@8898
   119
  val add_arities: (xstring * string list * string) list -> sg -> sg
wenzelm@3791
   120
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
   121
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   122
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   123
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   124
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   125
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   126
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
paulson@1501
   127
  val add_trfuns:
wenzelm@4344
   128
    (string * (ast list -> ast)) list *
wenzelm@4344
   129
    (string * (term list -> term)) list *
wenzelm@4344
   130
    (string * (term list -> term)) list *
wenzelm@4344
   131
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   132
  val add_trfunsT:
wenzelm@4344
   133
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   134
  val add_tokentrfuns:
wenzelm@6311
   135
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   136
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   137
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   138
  val add_path: string -> sg -> sg
wenzelm@3810
   139
  val add_space: string * string list -> sg -> sg
wenzelm@8725
   140
  val hide_space: string * string list -> sg -> sg
wenzelm@8725
   141
  val hide_space_i: string * string list -> sg -> sg
paulson@1501
   142
  val add_name: string -> sg -> sg
wenzelm@4256
   143
  val data_kinds: data -> string list
wenzelm@3967
   144
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   145
  val merge: sg * sg -> sg
wenzelm@3975
   146
  val prep_ext: sg -> sg
wenzelm@6546
   147
  val copy: sg -> sg
wenzelm@10932
   148
  val PureN: string
wenzelm@10932
   149
  val CPureN: string
wenzelm@4627
   150
  val nontriv_merge: sg * sg -> sg
wenzelm@3995
   151
  val pre_pure: sg
paulson@1501
   152
  val const_of_class: class -> string
paulson@1501
   153
  val class_of_const: string -> class
wenzelm@3791
   154
end;
clasohm@0
   155
wenzelm@6191
   156
signature PRIVATE_SIGN =
wenzelm@5642
   157
sig
wenzelm@5642
   158
  include SIGN
wenzelm@6546
   159
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@5642
   160
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   161
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   162
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   163
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   164
end;
wenzelm@5642
   165
wenzelm@6191
   166
structure Sign: PRIVATE_SIGN =
wenzelm@143
   167
struct
clasohm@0
   168
wenzelm@3967
   169
wenzelm@251
   170
(** datatype sg **)
wenzelm@251
   171
wenzelm@4256
   172
(* types sg, data, sg_ref *)
wenzelm@4256
   173
wenzelm@19
   174
datatype sg =
wenzelm@3975
   175
  Sg of
wenzelm@3975
   176
   {id: string ref,                             (*id*)
wenzelm@3975
   177
    stamps: string ref list} *                  (*unique theory indentifier*)
wenzelm@3975
   178
   {self: sg_ref,                               (*mutable self reference*)
wenzelm@3791
   179
    tsig: Type.type_sig,                        (*order-sorted signature of types*)
wenzelm@3805
   180
    const_tab: typ Symtab.table,                (*type schemes of constants*)
wenzelm@3791
   181
    syn: Syntax.syntax,                         (*syntax for parsing and printing*)
wenzelm@11501
   182
    path: string list option,                   (*current name space entry prefix*)
wenzelm@3975
   183
    spaces: (string * NameSpace.T) list,        (*name spaces for consts, types etc.*)
wenzelm@4256
   184
    data: data}                                 (*anytype data*)
wenzelm@4256
   185
and data =
wenzelm@4256
   186
  Data of
wenzelm@10404
   187
    (Object.kind *                              (*kind (for authorization)*)
wenzelm@10404
   188
      (Object.T *                               (*value*)
wenzelm@4998
   189
        ((Object.T -> Object.T) *               (*prepare extend method*)
wenzelm@6546
   190
          (Object.T -> Object.T) *              (*copy method*)
wenzelm@4998
   191
          (Object.T * Object.T -> Object.T) *   (*merge and prepare extend method*)
wenzelm@4998
   192
          (sg -> Object.T -> unit))))           (*print method*)
wenzelm@4256
   193
    Symtab.table
wenzelm@3967
   194
and sg_ref =
wenzelm@4998
   195
  SgRef of sg ref option;
clasohm@0
   196
wenzelm@3967
   197
(*make signature*)
wenzelm@3967
   198
fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) =
wenzelm@3975
   199
  Sg ({id = id, stamps = stamps}, {self = self, tsig = tsig, const_tab = const_tab,
wenzelm@3975
   200
    syn = syn, path = path, spaces = spaces, data = data});
wenzelm@3975
   201
wenzelm@3975
   202
wenzelm@4256
   203
(* basic operations *)
wenzelm@3975
   204
wenzelm@3975
   205
fun rep_sg (Sg (_, args)) = args;
wenzelm@3967
   206
wenzelm@3975
   207
(*show stamps*)
wenzelm@3975
   208
fun stamp_names_of (Sg ({stamps, ...}, _)) = rev (map ! stamps);
wenzelm@10932
   209
fun exists_stamp name (Sg ({stamps, ...}, _)) = exists (equal name o !) stamps;
wenzelm@3975
   210
fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
wenzelm@3975
   211
val str_of_sg = Pretty.str_of o pretty_sg;
wenzelm@3975
   212
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   213
wenzelm@402
   214
val tsig_of = #tsig o rep_sg;
wenzelm@3967
   215
wenzelm@3975
   216
fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, c);
wenzelm@3967
   217
wenzelm@3967
   218
wenzelm@3995
   219
(* id and self *)
wenzelm@3967
   220
wenzelm@3975
   221
fun check_stale (sg as Sg ({id, ...},
wenzelm@3975
   222
        {self = SgRef (Some (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@3967
   223
      if id = id' then sg
wenzelm@3975
   224
      else raise TERM ("Stale signature: " ^ str_of_sg sg, [])
wenzelm@3967
   225
  | check_stale _ = sys_error "Sign.check_stale";
clasohm@0
   226
wenzelm@4951
   227
fun is_stale sg = (check_stale sg; false) handle TERM _ => true;
wenzelm@4951
   228
wenzelm@3995
   229
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self);
wenzelm@3995
   230
wenzelm@3995
   231
fun deref (SgRef (Some (ref sg))) = sg
wenzelm@3995
   232
  | deref (SgRef None) = sys_error "Sign.deref";
wenzelm@3995
   233
wenzelm@3975
   234
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   235
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   236
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   237
  else name;
wenzelm@3975
   238
nipkow@206
   239
wenzelm@2979
   240
(* inclusion and equality *)
paulson@2180
   241
wenzelm@2185
   242
local
wenzelm@2185
   243
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   244
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   245
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   246
wenzelm@2185
   247
  fun subset_stamp ([], ys) = true
wenzelm@2185
   248
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   249
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   250
wenzelm@2185
   251
  (*fast partial test*)
wenzelm@2185
   252
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   253
    | fast_sub (_, []) = false
wenzelm@2185
   254
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   255
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   256
        else fast_sub (x :: xs, ys);
wenzelm@2185
   257
in
wenzelm@3975
   258
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@3967
   259
    (check_stale sg1; check_stale sg2; id1 = id2);
wenzelm@2185
   260
wenzelm@3975
   261
  fun subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   262
    eq_sg (sg1, sg2) orelse subset_stamp (s1, s2);
paulson@2180
   263
wenzelm@3975
   264
  fun fast_subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   265
    eq_sg (sg1, sg2) orelse fast_sub (s1, s2);
wenzelm@2185
   266
end;
wenzelm@2185
   267
wenzelm@402
   268
wenzelm@9031
   269
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   270
wenzelm@2185
   271
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   272
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   273
  same version of it*)
wenzelm@3975
   274
fun same_sg (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
wenzelm@3967
   275
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   276
wenzelm@2185
   277
(*test for drafts*)
wenzelm@4844
   278
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) = name = "" orelse ord name = ord "#";
wenzelm@386
   279
clasohm@0
   280
wenzelm@4568
   281
(* classes and sorts *)
wenzelm@4568
   282
wenzelm@7640
   283
val classes = Type.classes o tsig_of;
wenzelm@4844
   284
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   285
val subsort = Type.subsort o tsig_of;
wenzelm@4568
   286
val norm_sort = Type.norm_sort o tsig_of;
wenzelm@7640
   287
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   288
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@7640
   289
val univ_witness = Type.univ_witness o tsig_of;
wenzelm@10443
   290
fun typ_instance sg (T, U) = Type.typ_instance (tsig_of sg, T, U);
wenzelm@4568
   291
wenzelm@4256
   292
wenzelm@4256
   293
(** signature data **)
wenzelm@4256
   294
wenzelm@4256
   295
(* errors *)
wenzelm@4256
   296
wenzelm@4261
   297
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@4256
   298
wenzelm@4998
   299
fun err_inconsistent kinds =
wenzelm@4998
   300
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@4998
   301
wenzelm@6961
   302
fun err_method name kind e =
wenzelm@6961
   303
  (writeln ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@4256
   304
wenzelm@4256
   305
fun err_dup_init sg kind =
wenzelm@4256
   306
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4256
   307
wenzelm@4256
   308
fun err_uninit sg kind =
wenzelm@10404
   309
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^
wenzelm@10404
   310
         of_theory sg);
wenzelm@4256
   311
paulson@6040
   312
(*Trying to access theory data using get / put operations from a different
paulson@6040
   313
  instance of the TheoryDataFun result.  Typical cure: re-load all files*)
wenzelm@4998
   314
fun err_access sg kind =
wenzelm@4998
   315
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@4998
   316
wenzelm@4256
   317
wenzelm@4256
   318
(* prepare data *)
wenzelm@4256
   319
wenzelm@4489
   320
val empty_data = Data Symtab.empty;
wenzelm@4256
   321
wenzelm@4256
   322
fun merge_data (Data tab1, Data tab2) =
wenzelm@4256
   323
  let
wenzelm@4998
   324
    val data1 = map snd (Symtab.dest tab1);
wenzelm@4998
   325
    val data2 = map snd (Symtab.dest tab2);
wenzelm@4256
   326
    val all_data = data1 @ data2;
wenzelm@4998
   327
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@4256
   328
wenzelm@4256
   329
   fun entry data kind =
wenzelm@4998
   330
     (case gen_assoc Object.eq_kind (data, kind) of
wenzelm@4256
   331
       None => []
wenzelm@4256
   332
     | Some x => [(kind, x)]);
wenzelm@4256
   333
wenzelm@6546
   334
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@6961
   335
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@6546
   336
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@6961
   337
          (kind, (mrg (e1, e2)
wenzelm@6961
   338
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@4256
   339
      | merge_entries _ = sys_error "merge_entries";
wenzelm@4256
   340
wenzelm@4256
   341
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@4998
   342
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@4998
   343
  in
wenzelm@4998
   344
    Data (Symtab.make data_idx)
wenzelm@4998
   345
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@4998
   346
  end;
wenzelm@4256
   347
wenzelm@4256
   348
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@4256
   349
wenzelm@6546
   350
fun init_data_sg sg (Data tab) kind e cp ext mrg prt =
wenzelm@4998
   351
  let val name = Object.name_of_kind kind in
wenzelm@6546
   352
    Data (Symtab.update_new ((name, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@4998
   353
      handle Symtab.DUP _ => err_dup_init sg name
wenzelm@4998
   354
  end;
wenzelm@4256
   355
wenzelm@4256
   356
wenzelm@4256
   357
(* access data *)
wenzelm@4256
   358
wenzelm@4256
   359
fun data_kinds (Data tab) = map fst (Symtab.dest tab);
wenzelm@4256
   360
wenzelm@4256
   361
fun lookup_data sg tab kind =
wenzelm@4998
   362
  let val name = Object.name_of_kind kind in
wenzelm@4998
   363
    (case Symtab.lookup (tab, name) of
wenzelm@4998
   364
      Some (k, x) =>
wenzelm@4998
   365
        if Object.eq_kind (kind, k) then x
wenzelm@4998
   366
        else err_access sg name
wenzelm@4998
   367
    | None => err_uninit sg name)
wenzelm@4998
   368
  end;
wenzelm@4256
   369
wenzelm@4998
   370
fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@4998
   371
  let val x = fst (lookup_data sg tab kind)
wenzelm@4998
   372
  in f x handle Match => Object.kind_error kind end;
wenzelm@4998
   373
wenzelm@4998
   374
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@6546
   375
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind
wenzelm@6961
   376
  in prt sg e handle exn => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn end;
wenzelm@4256
   377
wenzelm@4998
   378
fun put_data_sg sg (Data tab) kind f x =
wenzelm@4998
   379
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@4998
   380
    (kind, (f x, snd (lookup_data sg tab kind)))), tab));
wenzelm@4256
   381
wenzelm@4256
   382
wenzelm@4256
   383
wenzelm@4256
   384
(** build signatures **)
wenzelm@3967
   385
wenzelm@3967
   386
fun ext_stamps stamps (id as ref name) =
wenzelm@3967
   387
  let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in
wenzelm@3967
   388
    if exists (equal name o !) stmps then
wenzelm@3967
   389
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@3967
   390
    else id :: stmps
wenzelm@3967
   391
  end;
wenzelm@3967
   392
wenzelm@3967
   393
fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) =
wenzelm@3967
   394
  let
wenzelm@3967
   395
    val id = ref name;
wenzelm@3967
   396
    val sign =
wenzelm@3967
   397
      make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id);
wenzelm@3967
   398
  in
wenzelm@3967
   399
    (case self of
wenzelm@3967
   400
      SgRef (Some r) => r := sign
wenzelm@3967
   401
    | _ => sys_error "Sign.create_sign");
wenzelm@3967
   402
    sign
wenzelm@3967
   403
  end;
wenzelm@3967
   404
wenzelm@3975
   405
fun extend_sign keep extfun name decls
wenzelm@3975
   406
    (sg as Sg ({id = _, stamps}, {self, tsig, const_tab, syn, path, spaces, data})) =
wenzelm@3967
   407
  let
wenzelm@3967
   408
    val _ = check_stale sg;
wenzelm@3967
   409
    val (self', data') =
wenzelm@3975
   410
      if is_draft sg andalso keep then (self, data)
wenzelm@4256
   411
      else (SgRef (Some (ref sg)), prep_ext_data data);
wenzelm@3967
   412
  in
wenzelm@3967
   413
    create_sign self' stamps name
wenzelm@3967
   414
      (extfun (syn, tsig, const_tab, (path, spaces), data') decls)
wenzelm@3967
   415
  end;
wenzelm@3967
   416
wenzelm@3967
   417
wenzelm@3791
   418
wenzelm@3791
   419
(** name spaces **)
wenzelm@3791
   420
wenzelm@3791
   421
(* kinds *)
wenzelm@3791
   422
wenzelm@3791
   423
val classK = "class";
wenzelm@3791
   424
val typeK = "type";
wenzelm@3791
   425
val constK = "const";
wenzelm@3791
   426
wenzelm@3791
   427
wenzelm@8725
   428
(* declare and retrieve names *)
wenzelm@3791
   429
wenzelm@3791
   430
fun space_of spaces kind =
wenzelm@3791
   431
  if_none (assoc (spaces, kind)) NameSpace.empty;
wenzelm@3791
   432
wenzelm@3791
   433
(*input and output of qualified names*)
wenzelm@3995
   434
fun intrn spaces kind = NameSpace.intern (space_of spaces kind);
wenzelm@3995
   435
fun extrn spaces kind = NameSpace.extern (space_of spaces kind);
wenzelm@5175
   436
fun cond_extrn spaces kind = NameSpace.cond_extern (space_of spaces kind);
wenzelm@6845
   437
fun cond_extrn_table spaces kind tab = NameSpace.cond_extern_table (space_of spaces kind) tab;
wenzelm@3791
   438
wenzelm@8725
   439
(*add / hide names*)
wenzelm@8725
   440
fun change_space f spaces kind x = overwrite (spaces, (kind, f (space_of spaces kind, x)));
wenzelm@8730
   441
fun add_names x = change_space NameSpace.extend x;
wenzelm@8730
   442
fun hide_names x = change_space NameSpace.hide x;
wenzelm@3791
   443
wenzelm@3810
   444
(*make full names*)
wenzelm@11501
   445
fun full _ "" = error "Attempt to declare empty name \"\""
wenzelm@11501
   446
  | full None name = name
wenzelm@11501
   447
  | full (Some path) name =
wenzelm@11501
   448
      if NameSpace.is_qualified name then
wenzelm@11501
   449
        error ("Attempt to declare qualified name " ^ quote name)
wenzelm@11501
   450
      else NameSpace.pack (path @ [name]);
wenzelm@3937
   451
wenzelm@3937
   452
(*base name*)
wenzelm@3937
   453
val base_name = NameSpace.base;
wenzelm@3791
   454
wenzelm@3791
   455
wenzelm@3791
   456
(* intern / extern names *)
wenzelm@3791
   457
wenzelm@3791
   458
local
wenzelm@3791
   459
  (*prepare mapping of names*)
wenzelm@3791
   460
  fun mapping f add_xs t =
wenzelm@3791
   461
    let
wenzelm@3791
   462
      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
wenzelm@3791
   463
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   464
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   465
    in lookup end;
wenzelm@3791
   466
wenzelm@3791
   467
  (*intern / extern typ*)
wenzelm@3791
   468
  fun trn_typ trn T =
wenzelm@3791
   469
    T |> map_typ
wenzelm@3791
   470
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   471
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   472
wenzelm@3791
   473
  (*intern / extern term*)
wenzelm@3791
   474
  fun trn_term trn t =
wenzelm@3791
   475
    t |> map_term
wenzelm@3791
   476
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   477
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   478
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   479
wenzelm@3975
   480
  val spaces_of = #spaces o rep_sg;
wenzelm@3791
   481
in
wenzelm@3791
   482
  fun intrn_class spaces = intrn spaces classK;
wenzelm@3791
   483
  fun extrn_class spaces = extrn spaces classK;
wenzelm@3937
   484
wenzelm@3791
   485
  val intrn_sort = map o intrn_class;
wenzelm@3791
   486
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   487
  val intrn_term = trn_term o intrn;
wenzelm@3937
   488
wenzelm@3937
   489
  val extrn_sort = map o extrn_class;
wenzelm@3791
   490
  val extrn_typ = trn_typ o extrn;
wenzelm@3791
   491
  val extrn_term = trn_term o extrn;
wenzelm@3791
   492
wenzelm@3805
   493
  fun intrn_tycons spaces T =
wenzelm@3805
   494
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   495
wenzelm@3810
   496
  val intern = intrn o spaces_of;
wenzelm@3855
   497
  val extern = extrn o spaces_of;
wenzelm@5175
   498
  val cond_extern = cond_extrn o spaces_of;
wenzelm@6845
   499
  fun cond_extern_table sg = cond_extrn_table (spaces_of sg);
wenzelm@3937
   500
wenzelm@3791
   501
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   502
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   503
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   504
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   505
wenzelm@3791
   506
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   507
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   508
wenzelm@3791
   509
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   510
wenzelm@3975
   511
  val full_name = full o #path o rep_sg;
wenzelm@11501
   512
wenzelm@11501
   513
  fun full_name_path sg elems =
wenzelm@11501
   514
    full (Some (if_none (#path (rep_sg sg)) [] @ NameSpace.unpack elems));
wenzelm@11501
   515
wenzelm@3791
   516
end;
wenzelm@620
   517
wenzelm@402
   518
clasohm@0
   519
wenzelm@4249
   520
(** pretty printing of terms, types etc. **)
wenzelm@3937
   521
wenzelm@4249
   522
fun pretty_term (sg as Sg ({stamps, ...}, {syn, spaces, ...})) t =
wenzelm@3937
   523
  Syntax.pretty_term syn
wenzelm@4249
   524
    (exists (equal "CPure" o !) stamps)
wenzelm@5175
   525
    (if ! NameSpace.long_names then t else extrn_term spaces t);
wenzelm@3937
   526
wenzelm@3975
   527
fun pretty_typ (Sg (_, {syn, spaces, ...})) T =
wenzelm@3937
   528
  Syntax.pretty_typ syn
wenzelm@5175
   529
    (if ! NameSpace.long_names then T else extrn_typ spaces T);
wenzelm@3937
   530
wenzelm@3975
   531
fun pretty_sort (Sg (_, {syn, spaces, ...})) S =
wenzelm@3937
   532
  Syntax.pretty_sort syn
wenzelm@5175
   533
    (if ! NameSpace.long_names then S else extrn_sort spaces S);
wenzelm@3937
   534
wenzelm@3937
   535
fun pretty_classrel sg (c1, c2) = Pretty.block
wenzelm@3937
   536
  [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]];
wenzelm@3937
   537
wenzelm@3937
   538
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   539
  let
wenzelm@3937
   540
    val t' = cond_extern sg typeK t;
wenzelm@3937
   541
    val dom =
wenzelm@3937
   542
      if null Ss then []
wenzelm@3937
   543
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   544
  in
wenzelm@3937
   545
    Pretty.block
wenzelm@3937
   546
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   547
  end;
wenzelm@3937
   548
wenzelm@3937
   549
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3937
   550
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3937
   551
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@3937
   552
wenzelm@3937
   553
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S);
wenzelm@3937
   554
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2);
wenzelm@3937
   555
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar);
wenzelm@3937
   556
wenzelm@3937
   557
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   558
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   559
wenzelm@3937
   560
wenzelm@3937
   561
wenzelm@8898
   562
(** read sorts **)  (*exception ERROR*)
wenzelm@8898
   563
wenzelm@8898
   564
fun err_in_sort s =
wenzelm@8898
   565
  error ("The error(s) above occurred in sort " ^ quote s);
wenzelm@8898
   566
wenzelm@8898
   567
fun rd_sort syn tsig spaces str =
wenzelm@8898
   568
  let val S = intrn_sort spaces (Syntax.read_sort syn str handle ERROR => err_in_sort str)
wenzelm@8898
   569
  in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end;
wenzelm@8898
   570
wenzelm@8898
   571
(*read and certify sort wrt a signature*)
wenzelm@8898
   572
fun read_sort (sg as Sg (_, {tsig, syn, spaces, ...})) str =
wenzelm@8898
   573
  (check_stale sg; rd_sort syn tsig spaces str);
wenzelm@8898
   574
wenzelm@8898
   575
fun cert_sort _ tsig _ = Type.cert_sort tsig;
wenzelm@8898
   576
wenzelm@8898
   577
wenzelm@8898
   578
wenzelm@562
   579
(** read types **)  (*exception ERROR*)
wenzelm@562
   580
wenzelm@562
   581
fun err_in_type s =
wenzelm@562
   582
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   583
wenzelm@4227
   584
fun rd_raw_typ syn tsig spaces def_sort str =
wenzelm@3791
   585
  intrn_tycons spaces
wenzelm@3791
   586
    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str
wenzelm@3791
   587
      handle ERROR => err_in_type str);
wenzelm@4227
   588
wenzelm@4227
   589
fun read_raw_typ (sg as Sg (_, {tsig, syn, spaces, ...}), def_sort) str =
wenzelm@4227
   590
  (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
wenzelm@4227
   591
wenzelm@562
   592
(*read and certify typ wrt a signature*)
wenzelm@9504
   593
local
wenzelm@9504
   594
  fun read_typ_aux cert (sg, def_sort) str =
wenzelm@9504
   595
    (cert (tsig_of sg) (read_raw_typ (sg, def_sort) str)
wenzelm@9504
   596
        handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
wenzelm@9504
   597
in
wenzelm@9504
   598
  val read_typ = read_typ_aux Type.cert_typ;
wenzelm@9504
   599
  val read_typ_no_norm = read_typ_aux Type.cert_typ_no_norm;
wenzelm@9504
   600
end;
wenzelm@562
   601
wenzelm@562
   602
wenzelm@562
   603
wenzelm@8898
   604
(** certify classes, sorts, types and terms **)   (*exception TYPE*)
wenzelm@251
   605
wenzelm@8898
   606
val certify_class = Type.cert_class o tsig_of;
wenzelm@8898
   607
val certify_sort = Type.cert_sort o tsig_of;
wenzelm@3975
   608
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@9504
   609
val certify_typ_no_norm = Type.cert_typ_no_norm o tsig_of;
wenzelm@10443
   610
wenzelm@10443
   611
fun certify_tycon sg c =
wenzelm@10443
   612
  if is_some (Symtab.lookup (#tycons (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   613
  else raise TYPE ("Undeclared type constructor " ^ quote c, [], []);
wenzelm@10443
   614
wenzelm@10443
   615
fun certify_tyabbr sg c =
wenzelm@10443
   616
  if is_some (Symtab.lookup (#abbrs (Type.rep_tsig (tsig_of sg)), c)) then c
wenzelm@10443
   617
  else raise TYPE ("Unknown type abbreviation " ^ quote c, [], []);
wenzelm@10443
   618
wenzelm@11720
   619
fun certify_tyname sg c =
wenzelm@11720
   620
  certify_tycon sg c handle TYPE _ => certify_tyabbr sg c handle TYPE _ =>
wenzelm@11720
   621
    raise TYPE ("Unknown type name " ^ quote c, [], []);
wenzelm@11720
   622
wenzelm@10443
   623
fun certify_const sg c =
wenzelm@10443
   624
  if is_some (const_type sg c) then c else raise TYPE ("Undeclared constant " ^ quote c, [], []);
wenzelm@251
   625
wenzelm@4961
   626
wenzelm@4961
   627
(* certify_term *)
wenzelm@4961
   628
wenzelm@8290
   629
(*check for duplicate occurrences of TFree/TVar with distinct sorts*)
wenzelm@8290
   630
fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
wenzelm@8290
   631
  | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
wenzelm@8290
   632
      (case assoc_string (tfrees, a) of
wenzelm@8290
   633
        Some S' =>
wenzelm@8290
   634
          if S = S' then env
wenzelm@8290
   635
          else raise TYPE ("Type variable " ^ quote a ^
wenzelm@8290
   636
            " has two distinct sorts", [TFree (a, S'), T], [])
wenzelm@8290
   637
      | None => (v :: tfrees, tvars))
wenzelm@8290
   638
  | nodup_tvars (env as (tfrees, tvars), T as TVar (v as (a, S))) =
wenzelm@2979
   639
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   640
        Some S' =>
wenzelm@8290
   641
          if S = S' then env
wenzelm@8290
   642
          else raise TYPE ("Type variable " ^ quote (Syntax.string_of_vname a) ^
wenzelm@3791
   643
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@8290
   644
      | None => (tfrees, v :: tvars))
wenzelm@8290
   645
(*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
wenzelm@8290
   646
and nodup_tvars_list (env, []) = env
wenzelm@8290
   647
  | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
nipkow@1494
   648
wenzelm@8290
   649
(*check for duplicate occurrences of Free/Var with distinct types*)
wenzelm@8290
   650
fun nodup_vars tm =
wenzelm@2979
   651
  let
wenzelm@8290
   652
    fun nodups (envs as (env as (frees, vars), envT)) tm =
wenzelm@2979
   653
      (case tm of
wenzelm@8290
   654
        Const (c, T) => (env, nodup_tvars (envT, T))
wenzelm@8290
   655
      | Free (v as (a, T)) =>
wenzelm@8290
   656
          (case assoc_string (frees, a) of
wenzelm@8290
   657
            Some T' =>
wenzelm@8290
   658
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   659
              else raise TYPE ("Variable " ^ quote a ^
wenzelm@8290
   660
                " has two distinct types", [T', T], [])
wenzelm@8290
   661
          | None => ((v :: frees, vars), nodup_tvars (envT, T)))
wenzelm@2979
   662
      | Var (v as (ixn, T)) =>
wenzelm@2979
   663
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   664
            Some T' =>
wenzelm@8290
   665
              if T = T' then (env, nodup_tvars (envT, T))
wenzelm@8290
   666
              else raise TYPE ("Variable " ^ quote (Syntax.string_of_vname ixn) ^
wenzelm@3791
   667
                " has two distinct types", [T', T], [])
wenzelm@8290
   668
          | None => ((frees, v :: vars), nodup_tvars (envT, T)))
wenzelm@8290
   669
      | Bound _ => envs
wenzelm@8290
   670
      | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
wenzelm@8290
   671
      | s $ t => nodups (nodups envs s) t)
wenzelm@8290
   672
  in nodups (([], []), ([], [])) tm; tm end;
wenzelm@2979
   673
wenzelm@4961
   674
(*compute and check type of the term*)
wenzelm@4961
   675
fun type_check sg tm =
wenzelm@4961
   676
  let
wenzelm@8725
   677
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   678
    val prT = pretty_typ sg;
wenzelm@251
   679
wenzelm@4961
   680
    fun err_appl why bs t T u U =
wenzelm@4961
   681
      let
wenzelm@10404
   682
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   683
        val t' = subst_bounds (xs, t);
wenzelm@4961
   684
        val u' = subst_bounds (xs, u);
wenzelm@10404
   685
        val text = cat_lines (TypeInfer.appl_error prt prT why t' T u' U);
wenzelm@4961
   686
      in raise TYPE (text, [T, U], [t', u']) end;
wenzelm@4961
   687
wenzelm@4961
   688
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   689
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   690
      | typ_of (_, Var (_, T)) = T
wenzelm@4961
   691
      | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
wenzelm@4961
   692
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   693
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   694
      | typ_of (bs, t $ u) =
wenzelm@4961
   695
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   696
            (case T of
wenzelm@4961
   697
              Type ("fun", [T1, T2]) =>
wenzelm@4961
   698
                if T1 = U then T2 else err_appl "Incompatible operand type." bs t T u U
wenzelm@4961
   699
            | _ => err_appl "Operator not of function type." bs t T u U)
wenzelm@4961
   700
          end;
wenzelm@4961
   701
wenzelm@4961
   702
  in typ_of ([], tm) end;
clasohm@0
   703
wenzelm@2979
   704
wenzelm@3975
   705
fun certify_term sg tm =
wenzelm@251
   706
  let
wenzelm@3969
   707
    val _ = check_stale sg;
wenzelm@3975
   708
    val tsig = tsig_of sg;
wenzelm@3969
   709
wenzelm@3975
   710
    fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
wenzelm@169
   711
wenzelm@4961
   712
    fun atom_err (errs, Const (a, T)) =
wenzelm@3975
   713
        (case const_type sg a of
wenzelm@4961
   714
          None => ("Undeclared constant " ^ show_const a T) :: errs
wenzelm@3975
   715
        | Some U =>
wenzelm@10404
   716
            if typ_instance sg (T, U) then errs
wenzelm@4961
   717
            else ("Illegal type for constant " ^ show_const a T) :: errs)
wenzelm@4961
   718
      | atom_err (errs, Var ((x, i), _)) =
wenzelm@4961
   719
          if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs
wenzelm@4961
   720
      | atom_err (errs, _) = errs;
wenzelm@251
   721
wenzelm@251
   722
    val norm_tm =
wenzelm@2185
   723
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@7068
   724
        [] => Type.norm_term tsig tm
wenzelm@3791
   725
      | errs => raise TYPE (cat_lines errs, [], [tm]));
wenzelm@8290
   726
    val _ = nodup_vars norm_tm;
wenzelm@251
   727
  in
wenzelm@4961
   728
    (case foldl_aterms atom_err ([], norm_tm) of
wenzelm@4961
   729
      [] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   730
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   731
  end;
wenzelm@251
   732
wenzelm@251
   733
wenzelm@583
   734
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   735
wenzelm@2979
   736
(*
wenzelm@2979
   737
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   738
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   739
  used: list of already used type variables
wenzelm@2979
   740
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   741
wenzelm@4249
   742
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   743
  typs: expected types
wenzelm@2979
   744
*)
wenzelm@2979
   745
wenzelm@4249
   746
fun infer_types_simult sg def_type def_sort used freeze args =
wenzelm@251
   747
  let
wenzelm@3975
   748
    val tsig = tsig_of sg;
wenzelm@8725
   749
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@8725
   750
    val prT = pretty_typ sg;
wenzelm@4249
   751
wenzelm@4249
   752
    val termss = foldr multiply (map fst args, [[]]);
wenzelm@4249
   753
    val typs =
wenzelm@4249
   754
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   755
wenzelm@4249
   756
    fun infer ts = OK
wenzelm@4249
   757
      (Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@4249
   758
        (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts)
wenzelm@4249
   759
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   760
wenzelm@4249
   761
    val err_results = map infer termss;
wenzelm@4249
   762
    val errs = mapfilter get_error err_results;
wenzelm@4249
   763
    val results = mapfilter get_ok err_results;
wenzelm@4249
   764
wenzelm@10404
   765
    val ambiguity = length termss;      (* FIXME !? *)
wenzelm@4249
   766
    (* FIXME to syntax.ML!? *)
wenzelm@4249
   767
    fun ambig_msg () =
wenzelm@4249
   768
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   769
      then
wenzelm@4249
   770
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   771
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   772
      else ();
wenzelm@4249
   773
  in
wenzelm@4249
   774
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   775
    else if length results = 1 then
wenzelm@4249
   776
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   777
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   778
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   779
      else (); hd results)
wenzelm@4249
   780
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
wenzelm@4249
   781
      (cat_lines (map (Pretty.string_of o prt) (flat (map fst results))))))
wenzelm@4249
   782
  end;
clasohm@623
   783
wenzelm@4249
   784
wenzelm@4249
   785
fun infer_types sg def_type def_sort used freeze tsT =
wenzelm@4249
   786
  apfst hd (infer_types_simult sg def_type def_sort used freeze [tsT]);
wenzelm@251
   787
wenzelm@251
   788
wenzelm@8607
   789
(** read_def_terms **)
wenzelm@8607
   790
wenzelm@8607
   791
(*read terms, infer types*)
wenzelm@8607
   792
fun read_def_terms (sign, types, sorts) used freeze sTs =
wenzelm@8607
   793
  let
wenzelm@8607
   794
    val syn = #syn (rep_sg sign);
wenzelm@8607
   795
    fun read (s, T) =
wenzelm@8607
   796
      let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg
wenzelm@8607
   797
      in (Syntax.read syn T' s, T') end;
wenzelm@8607
   798
    val tsTs = map read sTs;
wenzelm@8607
   799
  in infer_types_simult sign types sorts used freeze tsTs end;
wenzelm@8607
   800
wenzelm@8802
   801
fun simple_read_term sign T s =
wenzelm@8802
   802
  (read_def_terms (sign, K None, K None) [] true [(s, T)]
wenzelm@8802
   803
    handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
wenzelm@8802
   804
wenzelm@8607
   805
wenzelm@2979
   806
clasohm@623
   807
(** extend signature **)    (*exception ERROR*)
clasohm@623
   808
wenzelm@620
   809
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   810
wenzelm@3791
   811
fun decls_of path name_of mfixs =
wenzelm@3810
   812
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   813
wenzelm@3791
   814
fun no_read _ _ _ decl = decl;
wenzelm@386
   815
wenzelm@386
   816
wenzelm@386
   817
(* add default sort *)
wenzelm@386
   818
wenzelm@8898
   819
fun ext_defS prep_sort (syn, tsig, ctab, (path, spaces), data) S =
wenzelm@8898
   820
  (syn, Type.ext_tsig_defsort tsig (prep_sort syn tsig spaces S), ctab, (path, spaces), data);
wenzelm@8898
   821
paulson@8927
   822
fun ext_defsort arg   = ext_defS rd_sort arg;
paulson@8927
   823
fun ext_defsort_i arg = ext_defS cert_sort arg;
wenzelm@386
   824
wenzelm@386
   825
wenzelm@386
   826
(* add type constructors *)
wenzelm@386
   827
wenzelm@3967
   828
fun ext_types (syn, tsig, ctab, (path, spaces), data) types =
wenzelm@3791
   829
  let val decls = decls_of path Syntax.type_name types in
wenzelm@3791
   830
    (Syntax.extend_type_gram syn types,
wenzelm@3791
   831
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3967
   832
      (path, add_names spaces typeK (map fst decls)), data)
wenzelm@3791
   833
  end;
wenzelm@386
   834
wenzelm@4844
   835
fun ext_nonterminals sg nonterms =
wenzelm@4844
   836
  ext_types sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms);
wenzelm@4844
   837
wenzelm@386
   838
wenzelm@386
   839
(* add type abbreviations *)
wenzelm@386
   840
wenzelm@3791
   841
fun read_abbr syn tsig spaces (t, vs, rhs_src) =
wenzelm@4227
   842
  (t, vs, rd_raw_typ syn tsig spaces (K None) rhs_src)
wenzelm@386
   843
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   844
wenzelm@3967
   845
fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces), data) abbrs =
wenzelm@386
   846
  let
wenzelm@386
   847
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@3791
   848
    val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   849
wenzelm@3791
   850
    val abbrs' =
wenzelm@3791
   851
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   852
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   853
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@3805
   854
    val decls = map (rd_abbr syn' tsig spaces') abbrs';
wenzelm@386
   855
  in
wenzelm@3967
   856
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
wenzelm@386
   857
  end;
wenzelm@386
   858
wenzelm@3791
   859
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   860
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   861
wenzelm@386
   862
wenzelm@386
   863
(* add type arities *)
wenzelm@386
   864
wenzelm@8898
   865
fun ext_ars int prep_sort (syn, tsig, ctab, (path, spaces), data) arities =
wenzelm@386
   866
  let
wenzelm@8898
   867
    val prepS = prep_sort syn tsig spaces;
wenzelm@8898
   868
    fun prep_arity (c, Ss, S) =
wenzelm@8898
   869
      (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
wenzelm@8898
   870
    val tsig' = Type.ext_tsig_arities tsig (map prep_arity arities);
wenzelm@3791
   871
    val log_types = Type.logical_types tsig';
wenzelm@386
   872
  in
wenzelm@3967
   873
    (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces), data)
wenzelm@386
   874
  end;
wenzelm@386
   875
paulson@8927
   876
fun ext_arities arg   = ext_ars true rd_sort arg;
paulson@8927
   877
fun ext_arities_i arg = ext_ars false cert_sort arg;
wenzelm@8898
   878
wenzelm@386
   879
wenzelm@386
   880
(* add term constants and syntax *)
wenzelm@386
   881
wenzelm@3805
   882
fun const_name path c mx =
wenzelm@3810
   883
  full path (Syntax.const_name c mx);
wenzelm@3805
   884
wenzelm@386
   885
fun err_in_const c =
wenzelm@386
   886
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   887
wenzelm@386
   888
fun err_dup_consts cs =
wenzelm@386
   889
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   890
wenzelm@251
   891
wenzelm@3805
   892
fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@4227
   893
  (c, rd_raw_typ syn tsig spaces (K None) ty_src, mx)
wenzelm@3805
   894
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   895
wenzelm@3967
   896
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces), data) raw_consts =
wenzelm@386
   897
  let
wenzelm@2979
   898
    fun prep_const (c, ty, mx) =
wenzelm@3791
   899
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   900
        handle TYPE (msg, _, _) =>
wenzelm@3805
   901
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   902
wenzelm@3805
   903
    val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts;
wenzelm@386
   904
    val decls =
wenzelm@386
   905
      if syn_only then []
wenzelm@3791
   906
      else decls_of path Syntax.const_name consts;
wenzelm@386
   907
  in
wenzelm@2197
   908
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@4489
   909
      Symtab.extend (ctab, decls)
wenzelm@3791
   910
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3967
   911
      (path, add_names spaces constK (map fst decls)), data)
wenzelm@386
   912
  end;
wenzelm@386
   913
wenzelm@4007
   914
fun ext_consts_i sg = ext_cnsts no_read false ("", true) sg;
wenzelm@4007
   915
fun ext_consts sg = ext_cnsts read_const false ("", true) sg;
wenzelm@4007
   916
fun ext_syntax_i sg = ext_cnsts no_read true ("", true) sg;
wenzelm@4007
   917
fun ext_syntax sg = ext_cnsts read_const true ("", true) sg;
wenzelm@3791
   918
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts;
wenzelm@2197
   919
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   920
wenzelm@386
   921
wenzelm@386
   922
(* add type classes *)
wenzelm@386
   923
wenzelm@386
   924
fun const_of_class c = c ^ "_class";
wenzelm@386
   925
wenzelm@386
   926
fun class_of_const c_class =
wenzelm@386
   927
  let
wenzelm@3791
   928
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
   929
  in
wenzelm@386
   930
    if const_of_class c = c_class then c
wenzelm@3791
   931
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
   932
  end;
wenzelm@386
   933
wenzelm@386
   934
wenzelm@3967
   935
fun ext_classes int (syn, tsig, ctab, (path, spaces), data) classes =
wenzelm@386
   936
  let
wenzelm@562
   937
    val names = map fst classes;
wenzelm@386
   938
    val consts =
wenzelm@386
   939
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
   940
wenzelm@3810
   941
    val full_names = map (full path) names;
wenzelm@3791
   942
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
   943
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
   944
    val classes' =
wenzelm@3791
   945
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
   946
  in
wenzelm@386
   947
    ext_consts_i
wenzelm@3791
   948
      (Syntax.extend_consts syn names,
wenzelm@3967
   949
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
wenzelm@3791
   950
    consts
wenzelm@386
   951
  end;
wenzelm@386
   952
wenzelm@386
   953
wenzelm@2963
   954
(* add to classrel *)
wenzelm@421
   955
wenzelm@3967
   956
fun ext_classrel int (syn, tsig, ctab, (path, spaces), data) pairs =
wenzelm@3791
   957
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3967
   958
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
wenzelm@3791
   959
  end;
wenzelm@421
   960
wenzelm@421
   961
wenzelm@4619
   962
(* add translation rules *)
wenzelm@4619
   963
wenzelm@4619
   964
fun ext_trrules (syn, tsig, ctab, (path, spaces), data) args =
wenzelm@4619
   965
  (Syntax.extend_trrules syn
wenzelm@4619
   966
    (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args),
wenzelm@4619
   967
      tsig, ctab, (path, spaces), data);
wenzelm@4619
   968
wenzelm@4619
   969
wenzelm@1159
   970
(* add to syntax *)
wenzelm@386
   971
wenzelm@3967
   972
fun ext_syn extfun (syn, tsig, ctab, names, data) args =
wenzelm@3967
   973
  (extfun syn args, tsig, ctab, names, data);
wenzelm@3791
   974
wenzelm@3791
   975
wenzelm@3791
   976
(* add to path *)
wenzelm@3791
   977
wenzelm@4844
   978
fun ext_path (syn, tsig, ctab, (path, spaces), data) elems =
wenzelm@3791
   979
  let
wenzelm@3791
   980
    val path' =
wenzelm@11501
   981
      if elems = "//" then None
wenzelm@11501
   982
      else if elems = "/" then Some []
wenzelm@11501
   983
      else if elems = ".." andalso is_some path andalso path <> Some [] then
wenzelm@11501
   984
        Some (fst (split_last (the path)))
wenzelm@11501
   985
      else Some (if_none path [] @ NameSpace.unpack elems);
wenzelm@3791
   986
  in
wenzelm@3967
   987
    (syn, tsig, ctab, (path', spaces), data)
wenzelm@10404
   988
  end;
wenzelm@3791
   989
wenzelm@3791
   990
wenzelm@8725
   991
(* change name space *)
wenzelm@8725
   992
wenzelm@8725
   993
fun ext_add_space (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@8725
   994
  (syn, tsig, ctab, (path, add_names spaces kind names), data);
wenzelm@3791
   995
wenzelm@8725
   996
fun ext_hide_space (syn, tsig, ctab, (path, spaces), data) (kind, xnames) =
wenzelm@8725
   997
  (syn, tsig, ctab, (path, hide_names spaces kind (map (intrn spaces kind) xnames)), data);
wenzelm@8725
   998
wenzelm@8725
   999
fun ext_hide_space_i (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@8725
  1000
  (syn, tsig, ctab, (path, hide_names spaces kind names), data);
wenzelm@386
  1001
wenzelm@386
  1002
wenzelm@3967
  1003
(* signature data *)
wenzelm@386
  1004
wenzelm@6546
  1005
fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) =
wenzelm@6546
  1006
  (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt);
wenzelm@386
  1007
wenzelm@4998
  1008
fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) =
wenzelm@4998
  1009
  (syn, tsig, ctab, names, put_data_sg sg data kind f x);
wenzelm@386
  1010
wenzelm@386
  1011
wenzelm@6546
  1012
fun copy_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@6961
  1013
  (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@6546
  1014
wenzelm@6546
  1015
fun copy (sg as Sg ({id = _, stamps}, {self, tsig, const_tab, syn, path, spaces, data})) =
wenzelm@6546
  1016
  let
wenzelm@6546
  1017
    val _ = check_stale sg;
wenzelm@6546
  1018
    val self' = SgRef (Some (ref sg));
wenzelm@6546
  1019
    val Data tab = data;
wenzelm@6546
  1020
    val data' = Data (Symtab.map copy_data tab);
wenzelm@6546
  1021
  in create_sign self' stamps "#" (syn, tsig, const_tab, (path, spaces), data') end;
wenzelm@6546
  1022
wenzelm@6546
  1023
wenzelm@386
  1024
(* the external interfaces *)
wenzelm@386
  1025
wenzelm@4998
  1026
val add_classes       = extend_sign true (ext_classes true) "#";
wenzelm@4998
  1027
val add_classes_i     = extend_sign true (ext_classes false) "#";
wenzelm@4998
  1028
val add_classrel      = extend_sign true (ext_classrel true) "#";
wenzelm@4998
  1029
val add_classrel_i    = extend_sign true (ext_classrel false) "#";
wenzelm@8898
  1030
val add_defsort       = extend_sign true ext_defsort "#";
wenzelm@8898
  1031
val add_defsort_i     = extend_sign true ext_defsort_i "#";
wenzelm@4998
  1032
val add_types         = extend_sign true ext_types "#";
wenzelm@4998
  1033
val add_nonterminals  = extend_sign true ext_nonterminals "#";
wenzelm@4998
  1034
val add_tyabbrs       = extend_sign true ext_tyabbrs "#";
wenzelm@4998
  1035
val add_tyabbrs_i     = extend_sign true ext_tyabbrs_i "#";
wenzelm@8898
  1036
val add_arities       = extend_sign true ext_arities "#";
wenzelm@8898
  1037
val add_arities_i     = extend_sign true ext_arities_i "#";
wenzelm@4998
  1038
val add_consts        = extend_sign true ext_consts "#";
wenzelm@4998
  1039
val add_consts_i      = extend_sign true ext_consts_i "#";
wenzelm@4998
  1040
val add_syntax        = extend_sign true ext_syntax "#";
wenzelm@4998
  1041
val add_syntax_i      = extend_sign true ext_syntax_i "#";
wenzelm@4998
  1042
val add_modesyntax    = extend_sign true ext_modesyntax "#";
wenzelm@4998
  1043
val add_modesyntax_i  = extend_sign true ext_modesyntax_i "#";
wenzelm@4998
  1044
val add_trfuns        = extend_sign true (ext_syn Syntax.extend_trfuns) "#";
wenzelm@4998
  1045
val add_trfunsT       = extend_sign true (ext_syn Syntax.extend_trfunsT) "#";
wenzelm@4998
  1046
val add_tokentrfuns   = extend_sign true (ext_syn Syntax.extend_tokentrfuns) "#";
wenzelm@4998
  1047
val add_trrules       = extend_sign true ext_trrules "#";
wenzelm@4998
  1048
val add_trrules_i     = extend_sign true (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@4998
  1049
val add_path          = extend_sign true ext_path "#";
wenzelm@8725
  1050
val add_space         = extend_sign true ext_add_space "#";
wenzelm@8725
  1051
val hide_space        = extend_sign true ext_hide_space "#";
wenzelm@8725
  1052
val hide_space_i      = extend_sign true ext_hide_space_i "#";
wenzelm@4998
  1053
fun init_data arg sg  = extend_sign true (ext_init_data sg) "#" arg sg;
wenzelm@4998
  1054
fun put_data k f x sg = extend_sign true (ext_put_data sg) "#" (k, f, x) sg;
wenzelm@4998
  1055
fun add_name name sg  = extend_sign true K name () sg;
wenzelm@4998
  1056
fun prep_ext sg       = extend_sign false K "#" () sg;
wenzelm@386
  1057
wenzelm@386
  1058
wenzelm@3867
  1059
wenzelm@10404
  1060
(** merge signatures **)        (*exception TERM*)
wenzelm@3867
  1061
wenzelm@4228
  1062
(* merge_stamps *)
wenzelm@4228
  1063
wenzelm@4228
  1064
fun merge_stamps stamps1 stamps2 =
wenzelm@4228
  1065
  let val stamps = merge_rev_lists stamps1 stamps2 in
wenzelm@4228
  1066
    (case duplicates (map ! stamps) of
wenzelm@4228
  1067
      [] => stamps
wenzelm@4228
  1068
    | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@4228
  1069
        ^ commas_quote dups, []))
wenzelm@4228
  1070
  end;
wenzelm@4228
  1071
wenzelm@4228
  1072
wenzelm@4627
  1073
(* implicit merge -- trivial only *)
wenzelm@3877
  1074
wenzelm@4228
  1075
fun merge_refs (sgr1 as SgRef (Some (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
wenzelm@4228
  1076
        sgr2 as SgRef (Some (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@3967
  1077
      if fast_subsig (sg2, sg1) then sgr1
wenzelm@3967
  1078
      else if fast_subsig (sg1, sg2) then sgr2
wenzelm@3967
  1079
      else if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1080
      else if subsig (sg1, sg2) then sgr2
wenzelm@4228
  1081
      else (merge_stamps s1 s2; (*check for different versions*)
wenzelm@4228
  1082
        raise TERM ("Attempt to do non-trivial merge of signatures", []))
wenzelm@3967
  1083
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1084
wenzelm@4627
  1085
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1086
wenzelm@3867
  1087
wenzelm@10404
  1088
(* proper merge *)              (*exception TERM/ERROR*)
wenzelm@143
  1089
wenzelm@10932
  1090
val PureN = "Pure";
wenzelm@10932
  1091
val CPureN = "CPure";
wenzelm@10932
  1092
wenzelm@6961
  1093
fun nontriv_merge (sg1, sg2) =
wenzelm@3967
  1094
  if subsig (sg2, sg1) then sg1
wenzelm@251
  1095
  else if subsig (sg1, sg2) then sg2
wenzelm@386
  1096
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3967
  1097
    raise TERM ("Attempt to merge draft signatures", [])
wenzelm@10932
  1098
  else if exists_stamp PureN sg1 andalso exists_stamp CPureN sg2 orelse
wenzelm@10932
  1099
      exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
wenzelm@10932
  1100
    raise TERM ("Cannot merge Pure and CPure signatures", [])
wenzelm@251
  1101
  else
wenzelm@251
  1102
    (*neither is union already; must form union*)
wenzelm@251
  1103
    let
wenzelm@3975
  1104
      val Sg ({id = _, stamps = stamps1}, {self = _, tsig = tsig1, const_tab = const_tab1,
wenzelm@3975
  1105
        syn = syn1, path = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@3975
  1106
      val Sg ({id = _, stamps = stamps2}, {self = _, tsig = tsig2, const_tab = const_tab2,
wenzelm@3975
  1107
        syn = syn2, path = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
  1108
wenzelm@3967
  1109
      val id = ref "";
wenzelm@3975
  1110
      val self_ref = ref sg1;                   (*dummy value*)
wenzelm@3967
  1111
      val self = SgRef (Some self_ref);
wenzelm@402
  1112
wenzelm@4228
  1113
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@2185
  1114
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
  1115
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
  1116
        handle Symtab.DUPS cs =>
wenzelm@3791
  1117
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@386
  1118
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@3791
  1119
wenzelm@11501
  1120
      val path = Some [];
wenzelm@3791
  1121
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
  1122
      val spaces =
wenzelm@3791
  1123
        kinds ~~
wenzelm@3791
  1124
          ListPair.map NameSpace.merge
wenzelm@3791
  1125
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
  1126
wenzelm@4256
  1127
      val data = merge_data (data1, data2);
wenzelm@3967
  1128
wenzelm@3967
  1129
      val sign = make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps);
wenzelm@251
  1130
    in
wenzelm@3967
  1131
      self_ref := sign; sign
wenzelm@251
  1132
    end;
wenzelm@143
  1133
clasohm@0
  1134
clasohm@0
  1135
wenzelm@3995
  1136
(** partial Pure signature **)
clasohm@0
  1137
wenzelm@3967
  1138
val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
wenzelm@11501
  1139
  Symtab.empty, Syntax.pure_syn, Some [], [], empty_data, []);
wenzelm@3967
  1140
wenzelm@3995
  1141
val pre_pure =
wenzelm@3967
  1142
  create_sign (SgRef (Some (ref dummy_sg))) [] "#"
wenzelm@11501
  1143
    (Syntax.pure_syn, Type.tsig0, Symtab.empty, (Some [], []), empty_data);
clasohm@0
  1144
wenzelm@3791
  1145
clasohm@0
  1146
end;