src/Pure/sign.ML
author wenzelm
Sat Jun 11 22:15:53 2005 +0200 (2005-06-11)
changeset 16368 a06868ebeb0f
parent 16354 6f0ca9628840
child 16442 1171ecf7fb7e
permissions -rw-r--r--
discontinued named name spaces (classK, typeK, constK);
name space of classes and types maintained in tsig;
read_tyname/read_const now raise ERROR instead of TYPE;
tuned;
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@19
     8
signature SIGN =
wenzelm@3791
     9
sig
paulson@1501
    10
  type sg
wenzelm@3967
    11
  type sg_ref
wenzelm@4256
    12
  type data
wenzelm@2197
    13
  val rep_sg: sg ->
wenzelm@3975
    14
   {self: sg_ref,
wenzelm@14784
    15
    tsig: Type.tsig,
wenzelm@16368
    16
    consts: (typ * stamp) NameSpace.table,
wenzelm@16133
    17
    naming: NameSpace.naming,
wenzelm@4256
    18
    data: data}
wenzelm@3975
    19
  val stamp_names_of: sg -> string list
wenzelm@10932
    20
  val exists_stamp: string -> sg -> bool
wenzelm@16337
    21
  val pretty_sg: sg -> Pretty.T
wenzelm@16337
    22
  val pprint_sg: sg -> pprint_args -> unit
wenzelm@16337
    23
  val pretty_abbrev_sg: sg -> Pretty.T
wenzelm@16337
    24
  val str_of_sg: sg -> string
wenzelm@16337
    25
  val is_stale: sg -> bool
wenzelm@3967
    26
  val self_ref: sg -> sg_ref
wenzelm@16337
    27
  val deref: sg_ref -> sg
wenzelm@16337
    28
  val name_of: sg -> string
wenzelm@16337
    29
  val is_draft: sg -> bool
wenzelm@16337
    30
  val eq_sg: sg * sg -> bool
paulson@1501
    31
  val subsig: sg * sg -> bool
paulson@1501
    32
  val same_sg: sg * sg -> bool
wenzelm@16337
    33
  val joinable: sg * sg -> bool
wenzelm@16337
    34
  val prep_ext: sg -> sg
wenzelm@16337
    35
  val copy: sg -> sg
wenzelm@16337
    36
  val add_name: string -> sg -> sg
wenzelm@16337
    37
  val data_kinds: data -> string list
wenzelm@16337
    38
  val print_all_data: sg -> unit
wenzelm@16337
    39
  val pre_pure: sg
wenzelm@16337
    40
  val PureN: string
wenzelm@16337
    41
  val CPureN: string
wenzelm@16288
    42
  val naming_of: sg -> NameSpace.naming
wenzelm@16337
    43
  val base_name: string -> bstring
wenzelm@16337
    44
  val full_name: sg -> bstring -> string
wenzelm@16337
    45
  val full_name_path: sg -> string -> bstring -> string
wenzelm@16337
    46
  val declare_name: sg -> string -> NameSpace.T -> NameSpace.T
wenzelm@16337
    47
  val syn_of: sg -> Syntax.syntax
wenzelm@16337
    48
  val tsig_of: sg -> Type.tsig
paulson@1501
    49
  val classes: sg -> class list
wenzelm@4844
    50
  val defaultS: sg -> sort
paulson@1501
    51
  val subsort: sg -> sort * sort -> bool
wenzelm@4568
    52
  val of_sort: sg -> typ * sort -> bool
wenzelm@7640
    53
  val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
wenzelm@14784
    54
  val universal_witness: sg -> (typ * sort) option
wenzelm@10443
    55
  val typ_instance: sg -> typ * typ -> bool
wenzelm@16337
    56
  val is_logtype: sg -> string -> bool
wenzelm@16337
    57
  val const_type: sg -> string -> typ option
wenzelm@16337
    58
  val the_const_type: sg -> string -> typ
wenzelm@16337
    59
  val declared_tyname: sg -> string -> bool
wenzelm@16337
    60
  val declared_const: sg -> string -> bool
wenzelm@16368
    61
  val class_space: sg -> NameSpace.T
wenzelm@16368
    62
  val type_space: sg -> NameSpace.T
wenzelm@16368
    63
  val const_space: sg -> NameSpace.T
wenzelm@16337
    64
  val intern_class: sg -> xstring -> string
wenzelm@16337
    65
  val extern_class: sg -> string -> xstring
wenzelm@16368
    66
  val intern_type: sg -> xstring -> string
wenzelm@16368
    67
  val extern_type: sg -> string -> xstring
wenzelm@3805
    68
  val intern_const: sg -> xstring -> string
wenzelm@16337
    69
  val extern_const: sg -> string -> xstring
wenzelm@16337
    70
  val intern_sort: sg -> sort -> sort
wenzelm@16337
    71
  val extern_sort: sg -> sort -> sort
wenzelm@16337
    72
  val intern_typ: sg -> typ -> typ
wenzelm@16337
    73
  val extern_typ: sg -> typ -> typ
wenzelm@16337
    74
  val intern_term: sg -> term -> term
wenzelm@16337
    75
  val extern_term: sg -> term -> term
wenzelm@16337
    76
  val intern_tycons: sg -> typ -> typ
wenzelm@16337
    77
  val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
paulson@1501
    78
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    79
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    80
  val pretty_sort: sg -> sort -> Pretty.T
wenzelm@14828
    81
  val pretty_classrel: sg -> class list -> Pretty.T
wenzelm@14828
    82
  val pretty_arity: sg -> arity -> Pretty.T
paulson@1501
    83
  val string_of_term: sg -> term -> string
paulson@1501
    84
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    85
  val string_of_sort: sg -> sort -> string
wenzelm@14828
    86
  val string_of_classrel: sg -> class list -> string
wenzelm@14828
    87
  val string_of_arity: sg -> arity -> string
paulson@1501
    88
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    89
  val pprint_typ: sg -> typ -> pprint_args -> unit
wenzelm@14828
    90
  val pp: sg -> Pretty.pp
wenzelm@8898
    91
  val certify_class: sg -> class -> class
wenzelm@8898
    92
  val certify_sort: sg -> sort -> sort
paulson@1501
    93
  val certify_typ: sg -> typ -> typ
wenzelm@16337
    94
  val certify_typ_syntax: sg -> typ -> typ
wenzelm@16337
    95
  val certify_typ_abbrev: sg -> typ -> typ
wenzelm@14828
    96
  val certify_term: Pretty.pp -> sg -> term -> term * typ * int
wenzelm@16337
    97
  val read_sort': Syntax.syntax -> sg -> string -> sort
wenzelm@8898
    98
  val read_sort: sg -> string -> sort
wenzelm@16337
    99
  val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   100
  val read_typ_syntax': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   101
  val read_typ_abbrev': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
paulson@1501
   102
  val read_typ: sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   103
  val read_typ_syntax: sg * (indexname -> sort option) -> string -> typ
wenzelm@16337
   104
  val read_typ_abbrev: sg * (indexname -> sort option) -> string -> typ
wenzelm@16368
   105
  val read_tyname: sg -> string -> typ
wenzelm@16368
   106
  val read_const: sg -> string -> term
wenzelm@16337
   107
  val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
wenzelm@16337
   108
    (indexname -> sort option) -> string list -> bool
wenzelm@16337
   109
    -> (term list * typ) list -> term list * (indexname * typ) list
wenzelm@14828
   110
  val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
paulson@1501
   111
    (indexname -> sort option) -> string list -> bool
wenzelm@16337
   112
    -> term list * typ -> term * (indexname * typ) list
wenzelm@14905
   113
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
wenzelm@14828
   114
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@12068
   115
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8607
   116
  val read_def_terms:
wenzelm@8607
   117
    sg * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@8607
   118
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@8802
   119
  val simple_read_term: sg -> typ -> string -> term
wenzelm@8898
   120
  val add_defsort: string -> sg -> sg
wenzelm@3791
   121
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
   122
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@4844
   123
  val add_nonterminals: bstring list -> sg -> sg
wenzelm@3956
   124
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
   125
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@8898
   126
  val add_arities: (xstring * string list * string) list -> sg -> sg
wenzelm@3791
   127
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
   128
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   129
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
   130
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
   131
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
wenzelm@15746
   132
  val del_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@15746
   133
  val del_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
wenzelm@16337
   134
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@16337
   135
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@16337
   136
  val const_of_class: class -> string
wenzelm@16337
   137
  val class_of_const: string -> class
wenzelm@16337
   138
  val add_classes: (bstring * xstring list) list -> sg -> sg
wenzelm@16337
   139
  val add_classes_i: (bstring * class list) list -> sg -> sg
wenzelm@16337
   140
  val add_classrel: (xstring * xstring) list -> sg -> sg
wenzelm@16337
   141
  val add_classrel_i: (class * class) list -> sg -> sg
paulson@1501
   142
  val add_trfuns:
wenzelm@4344
   143
    (string * (ast list -> ast)) list *
wenzelm@4344
   144
    (string * (term list -> term)) list *
wenzelm@4344
   145
    (string * (term list -> term)) list *
wenzelm@4344
   146
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
   147
  val add_trfunsT:
wenzelm@4344
   148
    (string * (bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@14645
   149
  val add_advanced_trfuns:
wenzelm@14645
   150
    (string * (sg -> ast list -> ast)) list *
wenzelm@14645
   151
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   152
    (string * (sg -> term list -> term)) list *
wenzelm@14645
   153
    (string * (sg -> ast list -> ast)) list -> sg -> sg
wenzelm@14645
   154
  val add_advanced_trfunsT:
wenzelm@14645
   155
    (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
   156
  val add_tokentrfuns:
wenzelm@6311
   157
    (string * string * (string -> string * real)) list -> sg -> sg
wenzelm@4619
   158
  val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
paulson@1810
   159
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   160
  val add_path: string -> sg -> sg
wenzelm@16133
   161
  val qualified_names: sg -> sg
wenzelm@16133
   162
  val no_base_names: sg -> sg
wenzelm@16133
   163
  val custom_accesses: (string list -> string list list) -> sg -> sg
wenzelm@16133
   164
  val set_policy: (string -> bstring -> string) * (string list -> string list list) -> sg -> sg
wenzelm@16189
   165
  val restore_naming: sg -> sg -> sg
wenzelm@16368
   166
  val hide_classes: bool -> xstring list -> sg -> sg
wenzelm@16368
   167
  val hide_classes_i: bool -> string list -> sg -> sg
wenzelm@16368
   168
  val hide_types: bool -> xstring list -> sg -> sg
wenzelm@16368
   169
  val hide_types_i: bool -> string list -> sg -> sg
wenzelm@16368
   170
  val hide_consts: bool -> xstring list -> sg -> sg
wenzelm@16368
   171
  val hide_consts_i: bool -> string list -> sg -> sg
wenzelm@3967
   172
  val merge_refs: sg_ref * sg_ref -> sg_ref
wenzelm@4627
   173
  val merge: sg * sg -> sg
wenzelm@15703
   174
  val prep_ext_merge: sg list -> sg
wenzelm@3791
   175
end;
clasohm@0
   176
wenzelm@6191
   177
signature PRIVATE_SIGN =
wenzelm@5642
   178
sig
wenzelm@5642
   179
  include SIGN
wenzelm@6546
   180
  val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
wenzelm@12310
   181
    (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
wenzelm@5642
   182
  val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
wenzelm@5642
   183
  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
wenzelm@5642
   184
  val print_data: Object.kind -> sg -> unit
wenzelm@5642
   185
end;
wenzelm@5642
   186
wenzelm@6191
   187
structure Sign: PRIVATE_SIGN =
wenzelm@143
   188
struct
clasohm@0
   189
wenzelm@251
   190
(** datatype sg **)
wenzelm@251
   191
wenzelm@14645
   192
(* types sg, data, syn, sg_ref *)
wenzelm@4256
   193
wenzelm@19
   194
datatype sg =
wenzelm@3975
   195
  Sg of
wenzelm@16337
   196
   {id: string ref,
wenzelm@16368
   197
    stamps: string ref list,                  (*unique theory indentifier*)
wenzelm@16368
   198
    syn: syn} *                               (*syntax for parsing and printing*)
wenzelm@16368
   199
   {self: sg_ref,                             (*mutable self reference*)
wenzelm@16368
   200
    tsig: Type.tsig,                          (*order-sorted signature of types*)
wenzelm@16368
   201
    consts: (typ * stamp) NameSpace.table,    (*type schemes of constants*)
wenzelm@16337
   202
    naming: NameSpace.naming,
wenzelm@16337
   203
    data: data}
wenzelm@4256
   204
and data =
wenzelm@4256
   205
  Data of
wenzelm@16133
   206
    (Object.kind *                                (*kind (for authorization)*)
wenzelm@16133
   207
      (Object.T *                                 (*value*)
wenzelm@16133
   208
        ((Object.T -> Object.T) *                 (*copy method*)
wenzelm@16133
   209
          (Object.T -> Object.T) *                (*prepare extend method*)
wenzelm@16133
   210
          (Object.T * Object.T -> Object.T) *     (*merge and prepare extend method*)
wenzelm@16133
   211
          (sg -> Object.T -> unit))))             (*print method*)
wenzelm@4256
   212
    Symtab.table
wenzelm@14645
   213
and syn =
wenzelm@14645
   214
  Syn of
wenzelm@14645
   215
    Syntax.syntax *
wenzelm@14645
   216
     (((sg -> ast list -> ast) * stamp) Symtab.table *
wenzelm@14645
   217
      ((sg -> term list -> term) * stamp) Symtab.table *
wenzelm@14645
   218
      ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
wenzelm@14645
   219
      ((sg -> ast list -> ast) * stamp) list Symtab.table)
wenzelm@3967
   220
and sg_ref =
wenzelm@4998
   221
  SgRef of sg ref option;
clasohm@0
   222
wenzelm@16337
   223
(* FIXME assign!??? *)
wenzelm@16368
   224
fun make_sg (id, self, stamps) naming data (syn, tsig, consts) =
wenzelm@14645
   225
  Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
wenzelm@16368
   226
    consts = consts, naming = naming, data = data});
wenzelm@3975
   227
wenzelm@16337
   228
fun rep_sg (Sg (_, args)) = args;
wenzelm@3975
   229
wenzelm@3975
   230
wenzelm@16337
   231
(* stamps *)
wenzelm@3967
   232
wenzelm@15801
   233
fun stamps_of (Sg ({stamps, ...}, _)) = stamps;
wenzelm@15801
   234
val stamp_names_of = rev o map ! o stamps_of;
wenzelm@15801
   235
fun exists_stamp name = exists (equal name o !) o stamps_of;
wenzelm@15801
   236
wenzelm@16354
   237
fun is_stale (Sg ({id, ...}, {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
wenzelm@16354
   238
      id <> id'
wenzelm@16354
   239
  | is_stale _ = false;
wenzelm@16354
   240
wenzelm@16354
   241
fun pretty_sg sg =
wenzelm@16354
   242
  Pretty.str_list "{" "}" (stamp_names_of sg @ (if is_stale sg then ["!"] else []));
wenzelm@16354
   243
wenzelm@3975
   244
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@3975
   245
wenzelm@16337
   246
fun pretty_abbrev_sg sg =
wenzelm@16337
   247
  let
wenzelm@16337
   248
    val stamps = map ! (stamps_of sg);
wenzelm@16337
   249
    val abbrev = if length stamps > 5 then "..." :: rev (List.take (stamps, 5)) else rev stamps;
wenzelm@16337
   250
  in Pretty.str_list "{" "}" abbrev end;
wenzelm@16288
   251
wenzelm@16337
   252
val str_of_sg = Pretty.str_of o pretty_abbrev_sg;
wenzelm@15703
   253
wenzelm@3967
   254
wenzelm@3995
   255
(* id and self *)
wenzelm@3967
   256
wenzelm@16354
   257
fun check_stale pos sg =
wenzelm@16354
   258
  if is_stale sg then raise TERM ("Stale signature (in " ^ pos ^ "): " ^ str_of_sg sg, [])
wenzelm@16354
   259
  else sg;
wenzelm@4951
   260
wenzelm@16337
   261
fun self_ref (sg as Sg (_, {self, ...})) = (check_stale "Sign.self_ref" sg; self);
wenzelm@3995
   262
skalberg@15531
   263
fun deref (SgRef (SOME (ref sg))) = sg
skalberg@15531
   264
  | deref (SgRef NONE) = sys_error "Sign.deref";
wenzelm@3995
   265
wenzelm@16337
   266
fun assign (SgRef (SOME r)) sg = r := sg
wenzelm@16337
   267
  | assign (SgRef NONE) _ = sys_error "Sign.assign";
wenzelm@16337
   268
wenzelm@3975
   269
fun name_of (sg as Sg ({id = ref name, ...}, _)) =
wenzelm@4844
   270
  if name = "" orelse ord name = ord "#" then
wenzelm@3975
   271
    raise TERM ("Nameless signature " ^ str_of_sg sg, [])
wenzelm@3975
   272
  else name;
wenzelm@3975
   273
wenzelm@16337
   274
fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
wenzelm@16337
   275
      name = "" orelse ord name = ord "#"
wenzelm@16337
   276
  | is_draft _ = sys_error "Sign.is_draft";
wenzelm@16337
   277
nipkow@206
   278
wenzelm@2979
   279
(* inclusion and equality *)
paulson@2180
   280
wenzelm@2185
   281
local
wenzelm@2185
   282
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@16337
   283
  fun mem_stamp (_: string ref, []) = false
wenzelm@2185
   284
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   285
wenzelm@2185
   286
  fun subset_stamp ([], ys) = true
wenzelm@2185
   287
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   288
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
wenzelm@2185
   289
in
wenzelm@3975
   290
  fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
wenzelm@16337
   291
    (check_stale "Sign.eq_sg" sg1; check_stale "Sign.eq_sg" sg2; id1 = id2);
wenzelm@2185
   292
wenzelm@15801
   293
  fun subsig (sg1, sg2) =
wenzelm@15801
   294
    eq_sg (sg1, sg2) orelse mem_stamp (hd (stamps_of sg1), stamps_of sg2);
paulson@2180
   295
wenzelm@15801
   296
  fun subsig_internal (sg1, sg2) =
wenzelm@15801
   297
    eq_sg (sg1, sg2) orelse subset_stamp (stamps_of sg1, stamps_of sg2);
wenzelm@2185
   298
end;
wenzelm@2185
   299
wenzelm@402
   300
wenzelm@9031
   301
fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
wenzelm@9031
   302
wenzelm@2185
   303
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   304
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   305
  same version of it*)
wenzelm@15801
   306
fun same_sg (sg1, sg2) =
wenzelm@15801
   307
  eq_sg (sg1, sg2) orelse eq_set_string (pairself (map ! o stamps_of) (sg1, sg2));
wenzelm@2185
   308
wenzelm@16337
   309
wenzelm@16337
   310
(* data operations *)
wenzelm@16337
   311
wenzelm@16337
   312
fun err_method name kind e =    (* FIXME wrap exn msg!? *)
wenzelm@16337
   313
  (warning ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
wenzelm@16337
   314
wenzelm@16337
   315
fun err_inconsistent kinds =
wenzelm@16337
   316
  error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
wenzelm@16337
   317
wenzelm@16337
   318
val empty_data = Data Symtab.empty;
wenzelm@16337
   319
wenzelm@16337
   320
fun merge_data (Data tab1, Data tab2) =
wenzelm@16337
   321
  let
wenzelm@16337
   322
    val data1 = map snd (Symtab.dest tab1);
wenzelm@16337
   323
    val data2 = map snd (Symtab.dest tab2);
wenzelm@16337
   324
    val all_data = data1 @ data2;
wenzelm@16337
   325
    val kinds = gen_distinct Object.eq_kind (map fst all_data);
wenzelm@16337
   326
wenzelm@16337
   327
   fun entry data kind =
wenzelm@16337
   328
     (case gen_assoc Object.eq_kind (data, kind) of
wenzelm@16337
   329
       NONE => []
wenzelm@16337
   330
     | SOME x => [(kind, x)]);
wenzelm@16337
   331
wenzelm@16337
   332
    fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
wenzelm@16337
   333
          (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
wenzelm@16337
   334
      | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
wenzelm@16337
   335
          (kind, (mrg (e1, e2)
wenzelm@16337
   336
            handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
wenzelm@16337
   337
      | merge_entries _ = sys_error "merge_entries";
wenzelm@16337
   338
wenzelm@16337
   339
    val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
wenzelm@16337
   340
    val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
wenzelm@16337
   341
  in
wenzelm@16337
   342
    Data (Symtab.make data_idx)
wenzelm@16337
   343
      handle Symtab.DUPS dups => err_inconsistent dups
wenzelm@16337
   344
  end;
wenzelm@16337
   345
wenzelm@16337
   346
fun prep_ext_data data = merge_data (data, empty_data);
wenzelm@16337
   347
wenzelm@16337
   348
fun copy_data (Data tab) =
wenzelm@16337
   349
  let
wenzelm@16337
   350
    fun cp_data (k, (e, mths as (cp, _, _, _))) =
wenzelm@16337
   351
      (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
wenzelm@16337
   352
  in Data (Symtab.map cp_data tab) end;
wenzelm@16337
   353
wenzelm@16337
   354
wenzelm@16337
   355
wenzelm@16337
   356
(** build signatures **)
wenzelm@16337
   357
wenzelm@16337
   358
fun create_sg name self stamps naming data sign =
wenzelm@16337
   359
  let
wenzelm@16337
   360
    val id = ref name;
wenzelm@16337
   361
    val stamps' = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@16337
   362
    val _ = conditional (exists (equal name o !) stamps')
wenzelm@16337
   363
      (fn () => error ("Theory already contains a " ^ quote name ^ " component"));
wenzelm@16337
   364
    val sg = make_sg (id, self, id :: stamps') naming data sign;
wenzelm@16337
   365
  in assign self sg; sg end;
wenzelm@16337
   366
wenzelm@16368
   367
fun new_sg f (sg as Sg ({stamps, syn, ...}, {self = _, tsig, consts, naming, data})) =
wenzelm@16337
   368
  let
wenzelm@16337
   369
    val _ = check_stale "Sign.new_sg" sg;
wenzelm@16337
   370
    val self' = SgRef (SOME (ref sg));
wenzelm@16337
   371
    val data' = f data;
wenzelm@16368
   372
  in create_sg "#" self' stamps naming data' (syn, tsig, consts) end;
wenzelm@16337
   373
wenzelm@16337
   374
val prep_ext = new_sg prep_ext_data;
wenzelm@16337
   375
val copy = new_sg copy_data;
wenzelm@16337
   376
wenzelm@16368
   377
fun add_name name (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
wenzelm@16354
   378
  let
wenzelm@16354
   379
    val _ = check_stale "Sign.add_name" sg;
wenzelm@16354
   380
    val (self', data') =
wenzelm@16354
   381
      if is_draft sg then (self, data)
wenzelm@16354
   382
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@16368
   383
  in create_sg name self' stamps naming data' (syn, tsig, consts) end;
wenzelm@16337
   384
wenzelm@16368
   385
fun map_sg keep f (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
wenzelm@16337
   386
  let
wenzelm@16337
   387
    val _ = check_stale "Sign.map_sg" sg;
wenzelm@16354
   388
    val (self', data') =
wenzelm@16354
   389
      if is_draft sg andalso keep then (self, data)
wenzelm@16354
   390
      else (SgRef (SOME (ref sg)), prep_ext_data data);
wenzelm@16368
   391
    val (naming', data', sign') = f (naming, data', (syn, tsig, consts));
wenzelm@16354
   392
  in create_sg "#" self' stamps naming' data' sign' end;
wenzelm@16337
   393
wenzelm@16354
   394
fun map_naming f = map_sg true (fn (naming, data, sign) => (f naming, data, sign));
wenzelm@16354
   395
fun map_data f = map_sg true (fn (naming, data, sign) => (naming, f data, sign));
wenzelm@16354
   396
fun map_sign f = map_sg true (fn (naming, data, sign) => (naming, data, f sign));
wenzelm@16368
   397
fun map_syn f = map_sign (fn (syn, tsig, consts) => (f syn, tsig, consts));
wenzelm@16368
   398
fun map_tsig f = map_sign (fn (syn, tsig, consts) => (syn, f tsig, consts));
wenzelm@16368
   399
fun map_consts f = map_sign (fn (syn, tsig, consts) => (syn, tsig, f consts));
wenzelm@16337
   400
wenzelm@16337
   401
wenzelm@16337
   402
wenzelm@16337
   403
(** signature data **)
wenzelm@16337
   404
wenzelm@16337
   405
(* errors *)
wenzelm@16337
   406
wenzelm@16337
   407
fun of_theory sg = "\nof theory " ^ str_of_sg sg;
wenzelm@16337
   408
wenzelm@16337
   409
fun err_dup_init sg kind =
wenzelm@16337
   410
  error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   411
wenzelm@16337
   412
fun err_uninit sg kind =
wenzelm@16337
   413
  error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   414
wenzelm@16337
   415
fun err_access sg kind =
wenzelm@16337
   416
  error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
wenzelm@16337
   417
wenzelm@16337
   418
wenzelm@16337
   419
(* access data *)
wenzelm@16337
   420
wenzelm@16337
   421
fun data_kinds (Data tab) = Symtab.keys tab;
wenzelm@16337
   422
wenzelm@16337
   423
fun lookup_data sg tab kind =
wenzelm@16337
   424
  let val name = Object.name_of_kind kind in
wenzelm@16337
   425
    (case Symtab.lookup (tab, name) of
wenzelm@16337
   426
      SOME (k, x) =>
wenzelm@16337
   427
        if Object.eq_kind (kind, k) then x
wenzelm@16337
   428
        else err_access sg name
wenzelm@16337
   429
    | NONE => err_uninit sg name)
wenzelm@16337
   430
  end;
wenzelm@16337
   431
wenzelm@16337
   432
fun init_data (kind, (e, cp, ext, mrg, prt)) sg = sg |> map_data (fn Data tab =>
wenzelm@16337
   433
  Data (Symtab.update_new ((Object.name_of_kind kind, (kind, (e, (cp, ext, mrg, prt)))), tab))
wenzelm@16337
   434
    handle Symtab.DUP name => err_dup_init sg name);
wenzelm@16337
   435
wenzelm@16337
   436
fun get_data kind dest (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   437
  let val x = fst (lookup_data sg tab kind)
wenzelm@16337
   438
  in dest x handle Match => Object.kind_error kind end;
wenzelm@16337
   439
wenzelm@16337
   440
fun put_data kind mk x sg = sg |> map_data (fn Data tab =>
wenzelm@16337
   441
  Data (Symtab.update ((Object.name_of_kind kind,
wenzelm@16337
   442
    (kind, (mk x, snd (lookup_data sg tab kind)))), tab)));
wenzelm@16337
   443
wenzelm@16337
   444
fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   445
  let val (e, (_, _, _, prt)) = lookup_data sg tab kind in
wenzelm@16337
   446
    prt sg e handle exn =>
wenzelm@16337
   447
      err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn
wenzelm@16337
   448
  end;
wenzelm@16337
   449
wenzelm@16337
   450
fun print_all_data (sg as Sg (_, {data = Data tab, ...})) =
wenzelm@16337
   451
  List.app (fn kind => print_data kind sg) (map (#1 o #2) (Symtab.dest tab));
wenzelm@16337
   452
wenzelm@16337
   453
wenzelm@16337
   454
wenzelm@16337
   455
(** primitive signatures **)
wenzelm@16337
   456
wenzelm@16337
   457
val pure_syn =
wenzelm@16337
   458
  Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
wenzelm@16337
   459
wenzelm@16337
   460
val dummy_sg = make_sg (ref "", SgRef NONE, []) NameSpace.default_naming empty_data
wenzelm@16368
   461
  (pure_syn, Type.empty_tsig, NameSpace.empty_table);
wenzelm@16337
   462
wenzelm@16337
   463
val pre_pure =
wenzelm@16337
   464
  create_sg "#" (SgRef (SOME (ref dummy_sg))) [] NameSpace.default_naming empty_data
wenzelm@16368
   465
    (pure_syn, Type.empty_tsig, NameSpace.empty_table);
wenzelm@16337
   466
wenzelm@16337
   467
val PureN = "Pure";
wenzelm@16337
   468
val CPureN = "CPure";
wenzelm@16337
   469
wenzelm@16337
   470
wenzelm@16337
   471
wenzelm@16337
   472
(** signature content **)
wenzelm@16337
   473
wenzelm@16337
   474
(* naming *)
wenzelm@16337
   475
wenzelm@16337
   476
fun naming_of (Sg (_, {naming, ...})) = naming;
wenzelm@16337
   477
wenzelm@16337
   478
val base_name = NameSpace.base;
wenzelm@16337
   479
val full_name = NameSpace.full o naming_of;
wenzelm@16337
   480
fun full_name_path sg elems = NameSpace.full (NameSpace.add_path elems (naming_of sg));
wenzelm@16337
   481
val declare_name = NameSpace.declare o naming_of;
wenzelm@14645
   482
wenzelm@14645
   483
wenzelm@14645
   484
(* syntax *)
wenzelm@14645
   485
wenzelm@14645
   486
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
wenzelm@14645
   487
wenzelm@14645
   488
fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
wenzelm@14645
   489
  let
wenzelm@15746
   490
    fun apply (c, (f, s)) = (c, (f sg, s));
wenzelm@14645
   491
    fun prep tab = map apply (Symtab.dest tab);
wenzelm@14645
   492
    fun prep' tab = map apply (Symtab.dest_multi tab);
wenzelm@14645
   493
  in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
wenzelm@14645
   494
wenzelm@14645
   495
fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
wenzelm@14645
   496
wenzelm@14645
   497
wenzelm@14645
   498
(* advanced translation functions *)
wenzelm@14645
   499
wenzelm@14645
   500
fun extend_trfuns (atrs, trs, tr's, atr's)
wenzelm@14645
   501
    (Syn (syn, (parse_ast_trtab, parse_trtab, print_trtab, print_ast_trtab))) =
wenzelm@15746
   502
  Syn (syn, (Syntax.extend_trtab "parse ast translation" atrs parse_ast_trtab,
wenzelm@15746
   503
    Syntax.extend_trtab "parse translation" trs parse_trtab,
wenzelm@15746
   504
    Syntax.extend_tr'tab tr's print_trtab,
wenzelm@15746
   505
    Syntax.extend_tr'tab atr's print_ast_trtab));
wenzelm@14645
   506
wenzelm@14645
   507
fun merge_trfuns
wenzelm@14645
   508
    (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
wenzelm@14645
   509
    (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
wenzelm@15746
   510
  (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@15746
   511
    Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@14645
   512
    Syntax.merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@14645
   513
    Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
wenzelm@386
   514
clasohm@0
   515
wenzelm@16337
   516
(* type signature *)
wenzelm@16337
   517
wenzelm@16337
   518
val tsig_of = #tsig o rep_sg;
wenzelm@4568
   519
wenzelm@7640
   520
val classes = Type.classes o tsig_of;
wenzelm@4844
   521
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   522
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   523
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   524
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@14784
   525
val universal_witness = Type.universal_witness o tsig_of;
wenzelm@14784
   526
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@16337
   527
fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
wenzelm@4256
   528
wenzelm@4256
   529
wenzelm@16337
   530
(* consts signature *)
wenzelm@4256
   531
wenzelm@16368
   532
fun const_type (Sg (_, {consts, ...})) c = Option.map #1 (Symtab.lookup (#2 consts, c));
wenzelm@4256
   533
wenzelm@16337
   534
fun the_const_type sg c =
wenzelm@16337
   535
  (case const_type sg c of SOME T => T
wenzelm@16337
   536
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
wenzelm@4256
   537
wenzelm@16368
   538
fun declared_tyname sg c =
wenzelm@16368
   539
  is_some (Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c));
wenzelm@3791
   540
wenzelm@16368
   541
fun declared_const sg c = is_some (const_type sg c);
wenzelm@620
   542
wenzelm@402
   543
clasohm@0
   544
wenzelm@16337
   545
(** intern / extern names **)
wenzelm@16337
   546
wenzelm@16368
   547
val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
wenzelm@16368
   548
val type_space = #1 o #types o Type.rep_tsig o tsig_of;
wenzelm@16368
   549
val const_space = #1 o #consts o rep_sg
wenzelm@14645
   550
wenzelm@16368
   551
val intern_class = NameSpace.intern o class_space;
wenzelm@16368
   552
val extern_class = NameSpace.extern o class_space;
wenzelm@16368
   553
val intern_type = NameSpace.intern o type_space;
wenzelm@16368
   554
val extern_type = NameSpace.extern o type_space;
wenzelm@16368
   555
val intern_const = NameSpace.intern o const_space;
wenzelm@16368
   556
val extern_const = NameSpace.extern o const_space;
wenzelm@16337
   557
wenzelm@16337
   558
val intern_sort = map o intern_class;
wenzelm@16337
   559
val extern_sort = map o extern_class;
wenzelm@16337
   560
wenzelm@16337
   561
local
wenzelm@14645
   562
wenzelm@16337
   563
fun mapping add_names f t =
wenzelm@16337
   564
  let
wenzelm@16337
   565
    fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
wenzelm@16337
   566
    val tab = List.mapPartial f' (add_names (t, []));
wenzelm@16337
   567
    fun get x = if_none (assoc_string (tab, x)) x;
wenzelm@16337
   568
  in get end;
wenzelm@16337
   569
wenzelm@16337
   570
fun typ_mapping f g sg T =
wenzelm@16337
   571
  T |> Term.map_typ
wenzelm@16337
   572
    (mapping add_typ_classes (f sg) T)
wenzelm@16337
   573
    (mapping add_typ_tycons (g sg) T);
wenzelm@14645
   574
wenzelm@16337
   575
fun term_mapping f g h sg t =
wenzelm@16337
   576
  t |> Term.map_term
wenzelm@16337
   577
    (mapping add_term_classes (f sg) t)
wenzelm@16337
   578
    (mapping add_term_tycons (g sg) t)
wenzelm@16337
   579
    (mapping add_term_consts (h sg) t);
wenzelm@16337
   580
wenzelm@16337
   581
in
wenzelm@14645
   582
wenzelm@16368
   583
val intern_typ = typ_mapping intern_class intern_type;
wenzelm@16368
   584
val extern_typ = typ_mapping extern_class extern_type;
wenzelm@16368
   585
val intern_term = term_mapping intern_class intern_type intern_const;
wenzelm@16368
   586
val extern_term = term_mapping extern_class extern_type extern_const;
wenzelm@16368
   587
val intern_tycons = typ_mapping (K I) intern_type;
wenzelm@16337
   588
wenzelm@16337
   589
end;
wenzelm@14645
   590
wenzelm@14645
   591
wenzelm@14645
   592
wenzelm@4249
   593
(** pretty printing of terms, types etc. **)
wenzelm@3937
   594
wenzelm@16337
   595
fun pretty_term' syn sg t = Syntax.pretty_term syn (exists_stamp CPureN sg) (extern_term sg t);
wenzelm@14974
   596
fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
wenzelm@16337
   597
fun pretty_typ sg T = Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
wenzelm@16337
   598
fun pretty_sort sg S = Syntax.pretty_sort (syn_of sg) (extern_sort sg S);
wenzelm@3937
   599
skalberg@15570
   600
fun pretty_classrel sg cs = Pretty.block (List.concat
wenzelm@14828
   601
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
wenzelm@3937
   602
wenzelm@16337
   603
fun pretty_arity sg (a, Ss, S) =
wenzelm@3937
   604
  let
wenzelm@16368
   605
    val a' = extern_type sg a;
wenzelm@3937
   606
    val dom =
wenzelm@3937
   607
      if null Ss then []
wenzelm@3937
   608
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@16337
   609
  in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) end;
wenzelm@3937
   610
wenzelm@14828
   611
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   612
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@14828
   613
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@14828
   614
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@14828
   615
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@3937
   616
wenzelm@16337
   617
val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
wenzelm@16337
   618
val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
wenzelm@3937
   619
wenzelm@14974
   620
fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
wenzelm@14974
   621
  pretty_classrel sg, pretty_arity sg);
wenzelm@14828
   622
wenzelm@3937
   623
wenzelm@3937
   624
wenzelm@16337
   625
(** certify entities **)    (*exception TYPE*)
wenzelm@8898
   626
wenzelm@16337
   627
(* certify wrt. type signature *)
wenzelm@8898
   628
wenzelm@16337
   629
fun certify cert = cert o tsig_of o check_stale "Sign.certify";
wenzelm@562
   630
wenzelm@16337
   631
val certify_class      = certify Type.cert_class;
wenzelm@16337
   632
val certify_sort       = certify Type.cert_sort;
wenzelm@16337
   633
val certify_typ        = certify Type.cert_typ;
wenzelm@16337
   634
val certify_typ_syntax = certify Type.cert_typ_syntax;
wenzelm@16337
   635
val certify_typ_abbrev = certify Type.cert_typ_abbrev;
wenzelm@10443
   636
wenzelm@4961
   637
wenzelm@4961
   638
(* certify_term *)
wenzelm@4961
   639
wenzelm@14987
   640
local
nipkow@1494
   641
wenzelm@16337
   642
(*determine and check the type of a term*)
wenzelm@16337
   643
fun type_check pp tm =
wenzelm@4961
   644
  let
wenzelm@4961
   645
    fun err_appl why bs t T u U =
wenzelm@4961
   646
      let
wenzelm@10404
   647
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   648
        val t' = subst_bounds (xs, t);
wenzelm@4961
   649
        val u' = subst_bounds (xs, u);
wenzelm@16337
   650
        val msg = cat_lines
wenzelm@14828
   651
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@16337
   652
      in raise TYPE (msg, [T, U], [t', u']) end;
wenzelm@4961
   653
wenzelm@4961
   654
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   655
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   656
      | typ_of (_, Var (_, T)) = T
skalberg@15570
   657
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
wenzelm@4961
   658
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   659
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   660
      | typ_of (bs, t $ u) =
wenzelm@4961
   661
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   662
            (case T of
wenzelm@4961
   663
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   664
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   665
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   666
          end;
wenzelm@4961
   667
wenzelm@4961
   668
  in typ_of ([], tm) end;
clasohm@0
   669
wenzelm@14987
   670
in
wenzelm@14987
   671
wenzelm@14828
   672
fun certify_term pp sg tm =
wenzelm@251
   673
  let
wenzelm@16337
   674
    val _ = check_stale "Sign.certify_term" sg;
wenzelm@14784
   675
wenzelm@14993
   676
    val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
wenzelm@14784
   677
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
wenzelm@14784
   678
wenzelm@14784
   679
    fun err msg = raise TYPE (msg, [], [tm']);
wenzelm@3969
   680
wenzelm@14828
   681
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
wenzelm@169
   682
wenzelm@14784
   683
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
wenzelm@14784
   684
      | check_atoms (Abs (_, _, t)) = check_atoms t
wenzelm@14784
   685
      | check_atoms (Const (a, T)) =
wenzelm@14784
   686
          (case const_type sg a of
skalberg@15531
   687
            NONE => err ("Undeclared constant " ^ show_const a T)
skalberg@15531
   688
          | SOME U =>
wenzelm@14784
   689
              if typ_instance sg (T, U) then ()
wenzelm@14784
   690
              else err ("Illegal type for constant " ^ show_const a T))
wenzelm@14784
   691
      | check_atoms (Var ((x, i), _)) =
wenzelm@14784
   692
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
wenzelm@14784
   693
      | check_atoms _ = ();
wenzelm@14784
   694
  in
wenzelm@14784
   695
    check_atoms tm';
wenzelm@16337
   696
    (tm', type_check pp tm', maxidx_of_term tm')
wenzelm@14784
   697
  end;
wenzelm@251
   698
wenzelm@14987
   699
end;
wenzelm@14987
   700
wenzelm@251
   701
wenzelm@16337
   702
wenzelm@16368
   703
(** read and certify entities **)    (*exception ERROR/TYPE*)
wenzelm@16337
   704
wenzelm@16337
   705
(* sorts *)
wenzelm@16337
   706
wenzelm@16337
   707
fun read_sort' syn sg str =
wenzelm@16337
   708
  let
wenzelm@16337
   709
    val _ = check_stale "Sign.read_sort'" sg;
wenzelm@16337
   710
    val S = intern_sort sg (Syntax.read_sort syn str);
wenzelm@16337
   711
  in Type.cert_sort (tsig_of sg) S handle TYPE (msg, _, _) => error msg end;
wenzelm@16337
   712
wenzelm@16337
   713
fun read_sort sg str = read_sort' (syn_of sg) sg str;
wenzelm@16337
   714
wenzelm@16337
   715
wenzelm@16337
   716
(* types *)
wenzelm@16337
   717
wenzelm@16337
   718
local
wenzelm@16337
   719
wenzelm@16337
   720
fun gen_read_typ' cert syn (sg, def_sort) str =
wenzelm@16337
   721
  let
wenzelm@16337
   722
    val _ = check_stale "Sign.gen_read_typ'" sg;
wenzelm@16337
   723
    val get_sort = TypeInfer.get_sort (tsig_of sg) def_sort (intern_sort sg);
wenzelm@16337
   724
    val T = intern_tycons sg (Syntax.read_typ syn get_sort (intern_sort sg) str);
wenzelm@16337
   725
  in cert (tsig_of sg) T handle TYPE (msg, _, _) => error msg end
wenzelm@16337
   726
  handle ERROR => error ("The error(s) above occurred in type " ^ quote str);
wenzelm@16337
   727
wenzelm@16337
   728
fun gen_read_typ cert (sg, def_sort) str = gen_read_typ' cert (syn_of sg) (sg, def_sort) str;
wenzelm@16337
   729
wenzelm@16337
   730
in
wenzelm@16337
   731
wenzelm@16337
   732
fun no_def_sort sg = (sg, K NONE);
wenzelm@16337
   733
wenzelm@16337
   734
val read_typ'        = gen_read_typ' Type.cert_typ;
wenzelm@16337
   735
val read_typ_syntax' = gen_read_typ' Type.cert_typ_syntax;
wenzelm@16337
   736
val read_typ_abbrev' = gen_read_typ' Type.cert_typ_abbrev;
wenzelm@16337
   737
val read_typ         = gen_read_typ Type.cert_typ;
wenzelm@16337
   738
val read_typ_syntax  = gen_read_typ Type.cert_typ_syntax;
wenzelm@16337
   739
val read_typ_abbrev  = gen_read_typ Type.cert_typ_abbrev;
wenzelm@16337
   740
wenzelm@16337
   741
end;
wenzelm@16337
   742
wenzelm@16337
   743
wenzelm@16368
   744
(* type and constant names *)
wenzelm@15703
   745
wenzelm@15703
   746
fun read_tyname sg raw_c =
wenzelm@16368
   747
  let val c = intern_type sg raw_c in
wenzelm@16368
   748
    (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c) of
wenzelm@15703
   749
      SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
wenzelm@16368
   750
    | _ => error ("Undeclared type constructor: " ^ quote c))
wenzelm@15703
   751
  end;
wenzelm@15703
   752
wenzelm@15703
   753
fun read_const sg raw_c =
wenzelm@16368
   754
  let
wenzelm@16368
   755
    val c = intern_const sg raw_c;
wenzelm@16368
   756
    val _ = the_const_type sg c handle TYPE (msg, _, _) => error msg;
wenzelm@16368
   757
  in Const (c, dummyT) end;
wenzelm@15703
   758
wenzelm@15703
   759
wenzelm@251
   760
wenzelm@583
   761
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   762
wenzelm@2979
   763
(*
wenzelm@16337
   764
  def_type: partial map from indexnames to types (constrains Frees and Vars)
wenzelm@16337
   765
  def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
wenzelm@2979
   766
  used: list of already used type variables
wenzelm@2979
   767
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   768
wenzelm@4249
   769
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   770
  typs: expected types
wenzelm@2979
   771
*)
wenzelm@2979
   772
wenzelm@14828
   773
fun infer_types_simult pp sg def_type def_sort used freeze args =
wenzelm@251
   774
  let
wenzelm@3975
   775
    val tsig = tsig_of sg;
wenzelm@4249
   776
skalberg@15574
   777
    val termss = foldr multiply [[]] (map fst args);
wenzelm@4249
   778
    val typs =
wenzelm@4249
   779
      map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   780
wenzelm@14828
   781
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
wenzelm@14828
   782
        (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
wenzelm@14828
   783
        (intern_sort sg) used freeze typs ts)
wenzelm@4249
   784
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   785
wenzelm@4249
   786
    val err_results = map infer termss;
skalberg@15570
   787
    val errs = List.mapPartial get_error err_results;
skalberg@15570
   788
    val results = List.mapPartial get_ok err_results;
wenzelm@4249
   789
wenzelm@14645
   790
    val ambiguity = length termss;
wenzelm@14645
   791
wenzelm@4249
   792
    fun ambig_msg () =
wenzelm@4249
   793
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   794
      then
wenzelm@4249
   795
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   796
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   797
      else ();
wenzelm@4249
   798
  in
wenzelm@4249
   799
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   800
    else if length results = 1 then
wenzelm@4249
   801
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   802
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   803
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   804
      else (); hd results)
wenzelm@4249
   805
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
skalberg@15570
   806
      cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
wenzelm@4249
   807
  end;
clasohm@623
   808
wenzelm@4249
   809
wenzelm@14828
   810
fun infer_types pp sg def_type def_sort used freeze tsT =
wenzelm@14828
   811
  apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
wenzelm@251
   812
wenzelm@251
   813
wenzelm@8607
   814
wenzelm@16337
   815
(** read_def_terms -- read terms and infer types **)
wenzelm@16337
   816
wenzelm@16337
   817
fun read_def_terms' pp is_logtype syn (sg, types, sorts) used freeze sTs =
wenzelm@8607
   818
  let
wenzelm@8607
   819
    fun read (s, T) =
wenzelm@16337
   820
      let val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg
wenzelm@14905
   821
      in (Syntax.read is_logtype syn T' s, T') end;
wenzelm@16337
   822
  in infer_types_simult pp sg types sorts used freeze (map read sTs) end;
wenzelm@8607
   823
wenzelm@16337
   824
fun read_def_terms (sg, types, sorts) =
wenzelm@16337
   825
  read_def_terms' (pp sg) (is_logtype sg) (syn_of sg) (sg, types, sorts);
wenzelm@12068
   826
wenzelm@16337
   827
fun simple_read_term sg T s =
wenzelm@16337
   828
  let val ([t], _) = read_def_terms (sg, K NONE, K NONE) [] true [(s, T)]
wenzelm@16337
   829
  in t end
wenzelm@16337
   830
  handle ERROR => error ("The error(s) above occurred for term " ^ s);
wenzelm@8802
   831
wenzelm@8607
   832
wenzelm@2979
   833
wenzelm@16337
   834
(** signature extension functions **)  (*exception ERROR/TYPE*)
wenzelm@386
   835
wenzelm@386
   836
(* add default sort *)
wenzelm@386
   837
wenzelm@16368
   838
fun gen_add_defsort prep_sort s sg =
wenzelm@16368
   839
  sg |> map_tsig (Type.set_defsort (prep_sort sg s));
wenzelm@8898
   840
wenzelm@16337
   841
val add_defsort = gen_add_defsort read_sort;
wenzelm@16337
   842
val add_defsort_i = gen_add_defsort certify_sort;
wenzelm@386
   843
wenzelm@386
   844
wenzelm@386
   845
(* add type constructors *)
wenzelm@386
   846
wenzelm@16368
   847
fun add_types types sg = sg |> map_sign (fn (syn, tsig, consts) =>
wenzelm@14856
   848
  let
wenzelm@14905
   849
    val syn' = map_syntax (Syntax.extend_type_gram types) syn;
wenzelm@16368
   850
    val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
wenzelm@16368
   851
    val tsig' = Type.add_types (naming_of sg) decls tsig;
wenzelm@16368
   852
  in (syn', tsig', consts) end);
wenzelm@16337
   853
wenzelm@16337
   854
wenzelm@16337
   855
(* add nonterminals *)
wenzelm@16337
   856
wenzelm@16368
   857
fun add_nonterminals bnames sg = sg |> map_sign (fn (syn, tsig, consts) =>
wenzelm@16337
   858
  let
wenzelm@16337
   859
    val syn' = map_syntax (Syntax.extend_consts bnames) syn;
wenzelm@16368
   860
    val tsig' = Type.add_nonterminals (naming_of sg) bnames tsig;
wenzelm@16368
   861
  in (syn', tsig', consts) end);
wenzelm@386
   862
wenzelm@386
   863
wenzelm@386
   864
(* add type abbreviations *)
wenzelm@386
   865
wenzelm@16337
   866
fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) sg =
wenzelm@16368
   867
  sg |> map_sign (fn (syn, tsig, consts) =>
wenzelm@16337
   868
    let
wenzelm@16337
   869
      val syn' = map_syntax (Syntax.extend_type_gram [(a, length vs, mx)]) syn;
wenzelm@16368
   870
      val a' = Syntax.type_name a mx;
wenzelm@16337
   871
      val abbr = (a', vs, prep_typ sg rhs) handle ERROR =>
wenzelm@16368
   872
        error ("in type abbreviation " ^ quote a');
wenzelm@16368
   873
      val tsig' = Type.add_abbrevs (naming_of sg) [abbr] tsig;
wenzelm@16368
   874
    in (syn', tsig', consts) end);
wenzelm@386
   875
wenzelm@16337
   876
val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
wenzelm@16337
   877
val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
wenzelm@14784
   878
wenzelm@14784
   879
wenzelm@386
   880
(* add type arities *)
wenzelm@386
   881
wenzelm@16368
   882
fun gen_add_arities int_type prep_sort arities sg = sg |> map_tsig (fn tsig =>
wenzelm@16368
   883
  let
wenzelm@16368
   884
    fun prep_arity (a, Ss, S) = (int_type sg a, map (prep_sort sg) Ss, prep_sort sg S)
wenzelm@16368
   885
      handle ERROR => error ("in arity for type " ^ quote a);
wenzelm@16368
   886
    val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
wenzelm@16368
   887
  in tsig' end);
wenzelm@386
   888
wenzelm@16368
   889
val add_arities = gen_add_arities intern_type read_sort;
wenzelm@16337
   890
val add_arities_i = gen_add_arities (K I) certify_sort;
wenzelm@8898
   891
wenzelm@386
   892
wenzelm@16337
   893
(* modify syntax *)
wenzelm@16337
   894
wenzelm@16337
   895
fun gen_syntax change_gram prep_typ (prmode, args) sg =
wenzelm@16337
   896
  let
wenzelm@16337
   897
    fun prep (c, T, mx) = (c, prep_typ sg T, mx) handle ERROR =>
wenzelm@16337
   898
      error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
wenzelm@16337
   899
  in sg |> map_syn (map_syntax (change_gram (is_logtype sg) prmode (map prep args))) end;
wenzelm@16337
   900
wenzelm@16337
   901
fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
wenzelm@386
   902
wenzelm@16337
   903
val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
wenzelm@16337
   904
val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
wenzelm@16337
   905
val add_syntax = curry add_modesyntax Syntax.default_mode;
wenzelm@16337
   906
val add_syntax_i = curry add_modesyntax_i Syntax.default_mode;
wenzelm@16337
   907
val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
wenzelm@16337
   908
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
wenzelm@3805
   909
wenzelm@16337
   910
wenzelm@16337
   911
(* add constants *)
wenzelm@386
   912
wenzelm@386
   913
fun err_dup_consts cs =
wenzelm@386
   914
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   915
wenzelm@16337
   916
fun gen_add_consts prep_typ raw_args sg =
wenzelm@386
   917
  let
wenzelm@16337
   918
    val prepT = compress_type o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ sg;
wenzelm@16337
   919
    fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
wenzelm@16337
   920
      handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
wenzelm@16337
   921
    val args = map prep raw_args;
wenzelm@16368
   922
    val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, (T, stamp ())));
wenzelm@16368
   923
    fun extend_consts consts = NameSpace.extend_table (naming_of sg) (consts, decls)
wenzelm@16368
   924
      handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@16368
   925
  in sg |> map_consts extend_consts |> add_syntax_i args end;
wenzelm@386
   926
wenzelm@16337
   927
val add_consts = gen_add_consts (read_typ o no_def_sort);
wenzelm@16337
   928
val add_consts_i = gen_add_consts certify_typ;
wenzelm@386
   929
wenzelm@386
   930
wenzelm@386
   931
(* add type classes *)
wenzelm@386
   932
wenzelm@16337
   933
val classN = "_class";
wenzelm@386
   934
wenzelm@16337
   935
val const_of_class = suffix classN;
wenzelm@16337
   936
fun class_of_const c = unsuffix classN c
wenzelm@16337
   937
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@386
   938
wenzelm@16337
   939
fun gen_add_class int_class (bclass, raw_classes) sg =
wenzelm@16368
   940
  sg |> map_sign (fn (syn, tsig, consts) =>
wenzelm@16337
   941
    let
wenzelm@16337
   942
      val classes = map (int_class sg) raw_classes;
wenzelm@16337
   943
      val syn' = map_syntax (Syntax.extend_consts [bclass]) syn;
wenzelm@16368
   944
      val tsig' = Type.add_classes (pp sg) (naming_of sg) [(bclass, classes)] tsig;
wenzelm@16368
   945
    in (syn', tsig', consts) end)
wenzelm@16337
   946
  |> add_consts_i [(const_of_class bclass, a_itselfT --> propT, Syntax.NoSyn)];
wenzelm@3791
   947
wenzelm@16337
   948
val add_classes = fold (gen_add_class intern_class);
wenzelm@16337
   949
val add_classes_i = fold (gen_add_class (K I));
wenzelm@386
   950
wenzelm@386
   951
wenzelm@2963
   952
(* add to classrel *)
wenzelm@421
   953
wenzelm@16368
   954
fun gen_add_classrel int_class raw_pairs sg = sg |> map_tsig (fn tsig =>
wenzelm@16368
   955
  let
wenzelm@16368
   956
    val pairs = map (pairself (int_class sg)) raw_pairs;
wenzelm@16368
   957
    val tsig' = Type.add_classrel (pp sg) pairs tsig;
wenzelm@16368
   958
  in tsig' end);
wenzelm@16337
   959
wenzelm@16337
   960
val add_classrel = gen_add_classrel intern_class;
wenzelm@16337
   961
val add_classrel_i = gen_add_classrel (K I);
wenzelm@421
   962
wenzelm@421
   963
wenzelm@14645
   964
(* add translation functions *)
wenzelm@14645
   965
wenzelm@15746
   966
local
wenzelm@15746
   967
wenzelm@15746
   968
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15746
   969
wenzelm@16337
   970
fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) sg = sg |> map_syn (fn syn =>
wenzelm@15746
   971
  let val syn' = syn |> ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's)
wenzelm@16337
   972
  in make_syntax sg syn'; syn' end);
wenzelm@14645
   973
wenzelm@16337
   974
fun gen_add_trfunsT ext tr's sg = sg |> map_syn (fn syn =>
wenzelm@15746
   975
  let val syn' = syn |> ext ([], [], mk tr's, [])
wenzelm@16337
   976
  in make_syntax sg syn'; syn' end);
wenzelm@14645
   977
wenzelm@15746
   978
in
wenzelm@15746
   979
wenzelm@16337
   980
val add_trfuns = gen_add_trfuns (map_syntax o Syntax.extend_trfuns) Syntax.non_typed_tr';
wenzelm@16337
   981
val add_trfunsT = gen_add_trfunsT (map_syntax o Syntax.extend_trfuns);
wenzelm@16337
   982
val add_advanced_trfuns = gen_add_trfuns extend_trfuns Syntax.non_typed_tr'';
wenzelm@16337
   983
val add_advanced_trfunsT = gen_add_trfunsT extend_trfuns;
wenzelm@14645
   984
wenzelm@15746
   985
end;
wenzelm@15746
   986
wenzelm@16337
   987
val add_tokentrfuns = map_syn o map_syntax o Syntax.extend_tokentrfuns;
wenzelm@14645
   988
wenzelm@14645
   989
wenzelm@4619
   990
(* add translation rules *)
wenzelm@4619
   991
wenzelm@16337
   992
fun add_trrules args sg = sg |> map_syn (fn syn =>
wenzelm@16368
   993
  let val rules = map (Syntax.map_trrule (apfst (intern_type sg))) args
wenzelm@16337
   994
  in map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn) rules) syn end);
wenzelm@8725
   995
wenzelm@16337
   996
val add_trrules_i = map_syn o map_syntax o Syntax.extend_trrules_i;
wenzelm@386
   997
wenzelm@386
   998
wenzelm@16337
   999
(* modify naming *)
wenzelm@6546
  1000
wenzelm@16337
  1001
val add_path        = map_naming o NameSpace.add_path;
wenzelm@16337
  1002
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@16337
  1003
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16337
  1004
val custom_accesses = map_naming o NameSpace.custom_accesses;
wenzelm@16337
  1005
val set_policy      = map_naming o NameSpace.set_policy;
wenzelm@16337
  1006
val restore_naming  = map_naming o K o naming_of;
wenzelm@6546
  1007
wenzelm@6546
  1008
wenzelm@16337
  1009
(* hide names *)
wenzelm@386
  1010
wenzelm@16368
  1011
fun hide_classes b xs sg = sg |> map_tsig (Type.hide_classes b (map (intern_class sg) xs));
wenzelm@16368
  1012
val hide_classes_i = map_tsig oo Type.hide_classes;
wenzelm@16368
  1013
fun hide_types b xs sg = sg |> map_tsig (Type.hide_types b (map (intern_type sg) xs));
wenzelm@16368
  1014
val hide_types_i = map_tsig oo Type.hide_types;
wenzelm@16368
  1015
fun hide_consts b xs sg =
wenzelm@16368
  1016
  sg |> map_consts (apfst (fold (NameSpace.hide b o intern_const sg) xs));
wenzelm@16368
  1017
val hide_consts_i = (map_consts o apfst) oo (fold o NameSpace.hide);
wenzelm@386
  1018
wenzelm@386
  1019
wenzelm@3867
  1020
wenzelm@16337
  1021
(** merge signatures **)
wenzelm@4228
  1022
wenzelm@4228
  1023
fun merge_stamps stamps1 stamps2 =
wenzelm@12296
  1024
  let val stamps = merge_lists' stamps1 stamps2 in
wenzelm@4228
  1025
    (case duplicates (map ! stamps) of
wenzelm@4228
  1026
      [] => stamps
wenzelm@15826
  1027
    | dups => error ("Attempt to merge different versions of theories " ^ commas_quote dups))
wenzelm@4228
  1028
  end;
wenzelm@4228
  1029
wenzelm@4228
  1030
wenzelm@16337
  1031
(* trivial merge *)
wenzelm@3877
  1032
skalberg@15531
  1033
fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
skalberg@15531
  1034
        sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
wenzelm@15703
  1035
      if subsig (sg2, sg1) then sgr1
wenzelm@3967
  1036
      else if subsig (sg1, sg2) then sgr2
wenzelm@16337
  1037
      else (merge_stamps s1 s2;  (*check for different versions*)
wenzelm@16337
  1038
        error ("Attempt to do non-trivial merge of signature\n" ^
wenzelm@16337
  1039
          str_of_sg sg1 ^ " and " ^ str_of_sg sg2))
wenzelm@3967
  1040
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
  1041
wenzelm@4627
  1042
val merge = deref o merge_refs o pairself self_ref;
wenzelm@4627
  1043
wenzelm@3867
  1044
wenzelm@16337
  1045
(* non-trivial merge *)              (*exception TERM/ERROR*)
wenzelm@15703
  1046
wenzelm@15703
  1047
local
wenzelm@10932
  1048
wenzelm@6961
  1049
fun nontriv_merge (sg1, sg2) =
wenzelm@15703
  1050
  if subsig_internal (sg2, sg1) then sg1
wenzelm@15703
  1051
  else if subsig_internal (sg1, sg2) then sg2
wenzelm@15703
  1052
  else
wenzelm@15801
  1053
    if exists_stamp CPureN sg1 <> exists_stamp CPureN sg2
wenzelm@15801
  1054
    then error "Cannot merge Pure and CPure developments"
wenzelm@251
  1055
  else
wenzelm@251
  1056
    let
wenzelm@14645
  1057
      val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
wenzelm@16368
  1058
          {self = _, tsig = tsig1, consts = consts1, naming = _, data = data1}) = sg1;
wenzelm@14645
  1059
      val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
wenzelm@16368
  1060
          {self = _, tsig = tsig2, consts = consts2, naming = _, data = data2}) = sg2;
wenzelm@386
  1061
wenzelm@4228
  1062
      val stamps = merge_stamps stamps1 stamps2;
wenzelm@14645
  1063
      val syntax = Syntax.merge_syntaxes syntax1 syntax2;
wenzelm@14645
  1064
      val trfuns = merge_trfuns trfuns1 trfuns2;
wenzelm@16337
  1065
      val syn = Syn (syntax, trfuns);
wenzelm@16368
  1066
      val consts = NameSpace.merge_tables eq_snd (consts1, consts2)
wenzelm@14784
  1067
        handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@16133
  1068
      val naming = NameSpace.default_naming;
wenzelm@4256
  1069
      val data = merge_data (data1, data2);
wenzelm@3967
  1070
wenzelm@16337
  1071
      val pre_sg = make_sg (ref "", SgRef (SOME (ref dummy_sg)), ref "#" :: stamps1)
wenzelm@16368
  1072
        naming data (syn, tsig1, consts);
wenzelm@16337
  1073
      val tsig = Type.merge_tsigs (pp pre_sg) (tsig1, tsig2);
wenzelm@14905
  1074
wenzelm@16337
  1075
      val self = SgRef (SOME (ref dummy_sg));
wenzelm@16368
  1076
      val sg = make_sg (ref "", self, stamps) naming data (syn, tsig, consts);
wenzelm@16337
  1077
    in assign self sg; syn_of sg; sg end;
wenzelm@3791
  1078
wenzelm@15703
  1079
in
wenzelm@15703
  1080
wenzelm@15703
  1081
fun prep_ext_merge sgs =
wenzelm@15703
  1082
  if null sgs then
wenzelm@15703
  1083
    error "Merge: no parent theories"
wenzelm@15703
  1084
  else if exists is_draft sgs then
wenzelm@15703
  1085
    error "Attempt to merge draft theories"
wenzelm@15703
  1086
  else
wenzelm@15703
  1087
    Library.foldl nontriv_merge (hd sgs, tl sgs)
wenzelm@15703
  1088
    |> prep_ext
wenzelm@15703
  1089
    |> add_path "/";
wenzelm@15703
  1090
clasohm@0
  1091
end;
wenzelm@15703
  1092
wenzelm@15703
  1093
end;