src/Pure/context.ML
author wenzelm
Wed Nov 11 00:11:26 2009 +0100 (2009-11-11 ago)
changeset 33606 2b27020ffcb2
parent 33517 d064fa48f305
child 34245 25bd3ed2ac9f
permissions -rw-r--r--
local mutex for theory content/identity operations;
wenzelm@6185
     1
(*  Title:      Pure/context.ML
wenzelm@6185
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6185
     3
wenzelm@16436
     4
Generic theory contexts with unique identity, arbitrarily typed data,
wenzelm@24141
     5
monotonic development graph and history support.  Generic proof
wenzelm@24141
     6
contexts with arbitrarily typed data.
wenzelm@33031
     7
wenzelm@33031
     8
Firm naming conventions:
wenzelm@33031
     9
   thy, thy', thy1, thy2: theory
wenzelm@33031
    10
   ctxt, ctxt', ctxt1, ctxt2: Proof.context
wenzelm@33031
    11
   context: Context.generic
wenzelm@6185
    12
*)
wenzelm@6185
    13
wenzelm@6185
    14
signature BASIC_CONTEXT =
wenzelm@6185
    15
sig
wenzelm@16436
    16
  type theory
wenzelm@16436
    17
  type theory_ref
wenzelm@16436
    18
  exception THEORY of string * theory list
wenzelm@33031
    19
  structure Proof: sig type context end
wenzelm@33031
    20
  structure ProofContext:
wenzelm@33031
    21
  sig
wenzelm@33031
    22
    val theory_of: Proof.context -> theory
wenzelm@33031
    23
    val init: theory -> Proof.context
wenzelm@33031
    24
  end
wenzelm@6185
    25
end;
wenzelm@6185
    26
wenzelm@6185
    27
signature CONTEXT =
wenzelm@6185
    28
sig
wenzelm@6185
    29
  include BASIC_CONTEXT
wenzelm@16436
    30
  (*theory context*)
wenzelm@16436
    31
  val parents_of: theory -> theory list
wenzelm@16436
    32
  val ancestors_of: theory -> theory list
wenzelm@29069
    33
  val theory_name: theory -> string
wenzelm@16436
    34
  val is_stale: theory -> bool
wenzelm@26623
    35
  val is_draft: theory -> bool
wenzelm@28317
    36
  val reject_draft: theory -> theory
wenzelm@29093
    37
  val PureN: string
wenzelm@29093
    38
  val display_names: theory -> string list
wenzelm@16436
    39
  val pretty_thy: theory -> Pretty.T
wenzelm@16436
    40
  val string_of_thy: theory -> string
wenzelm@16436
    41
  val pretty_abbrev_thy: theory -> Pretty.T
wenzelm@16436
    42
  val str_of_thy: theory -> string
wenzelm@24141
    43
  val deref: theory_ref -> theory
wenzelm@24141
    44
  val check_thy: theory -> theory_ref
wenzelm@16436
    45
  val eq_thy: theory * theory -> bool
wenzelm@16436
    46
  val subthy: theory * theory -> bool
wenzelm@16594
    47
  val joinable: theory * theory -> bool
wenzelm@23355
    48
  val merge: theory * theory -> theory
wenzelm@23355
    49
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@16436
    50
  val copy_thy: theory -> theory
wenzelm@16436
    51
  val checkpoint_thy: theory -> theory
wenzelm@16489
    52
  val finish_thy: theory -> theory
wenzelm@16489
    53
  val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
wenzelm@16533
    54
  (*proof context*)
wenzelm@33031
    55
  val raw_transfer: theory -> Proof.context -> Proof.context
wenzelm@16533
    56
  (*generic context*)
wenzelm@33031
    57
  datatype generic = Theory of theory | Proof of Proof.context
wenzelm@33031
    58
  val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
wenzelm@33031
    59
  val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
wenzelm@33031
    60
  val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
wenzelm@33031
    61
    generic -> 'a * generic
wenzelm@18632
    62
  val the_theory: generic -> theory
wenzelm@33031
    63
  val the_proof: generic -> Proof.context
wenzelm@18731
    64
  val map_theory: (theory -> theory) -> generic -> generic
wenzelm@33031
    65
  val map_proof: (Proof.context -> Proof.context) -> generic -> generic
wenzelm@26486
    66
  val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
wenzelm@33031
    67
  val map_proof_result: (Proof.context -> 'a * Proof.context) -> generic -> 'a * generic
wenzelm@18731
    68
  val theory_map: (generic -> generic) -> theory -> theory
wenzelm@33031
    69
  val proof_map: (generic -> generic) -> Proof.context -> Proof.context
wenzelm@33031
    70
  val theory_of: generic -> theory  (*total*)
wenzelm@33031
    71
  val proof_of: generic -> Proof.context  (*total*)
wenzelm@26413
    72
  (*thread data*)
wenzelm@26413
    73
  val thread_data: unit -> generic option
wenzelm@26413
    74
  val the_thread_data: unit -> generic
wenzelm@26413
    75
  val set_thread_data: generic option -> unit
wenzelm@26413
    76
  val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
wenzelm@26463
    77
  val >> : (generic -> generic) -> unit
wenzelm@26463
    78
  val >>> : (generic -> 'a * generic) -> 'a
wenzelm@6185
    79
end;
wenzelm@6185
    80
wenzelm@16436
    81
signature PRIVATE_CONTEXT =
wenzelm@16436
    82
sig
wenzelm@16436
    83
  include CONTEXT
wenzelm@33033
    84
  structure Theory_Data:
wenzelm@16436
    85
  sig
wenzelm@22847
    86
    val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
wenzelm@16489
    87
      (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
wenzelm@16436
    88
    val get: serial -> (Object.T -> 'a) -> theory -> 'a
wenzelm@16436
    89
    val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
wenzelm@16489
    90
  end
wenzelm@33033
    91
  structure Proof_Data:
wenzelm@16533
    92
  sig
wenzelm@22847
    93
    val declare: (theory -> Object.T) -> serial
wenzelm@33031
    94
    val get: serial -> (Object.T -> 'a) -> Proof.context -> 'a
wenzelm@33031
    95
    val put: serial -> ('a -> Object.T) -> 'a -> Proof.context -> Proof.context
wenzelm@16533
    96
  end
wenzelm@16436
    97
end;
wenzelm@16436
    98
wenzelm@16436
    99
structure Context: PRIVATE_CONTEXT =
wenzelm@6185
   100
struct
wenzelm@6185
   101
wenzelm@16436
   102
(*** theory context ***)
wenzelm@6185
   103
wenzelm@16489
   104
(** theory data **)
wenzelm@16489
   105
wenzelm@16489
   106
(* data kinds and access methods *)
wenzelm@16489
   107
wenzelm@19028
   108
(*private copy avoids potential conflict of table exceptions*)
wenzelm@31971
   109
structure Datatab = Table(type key = int val ord = int_ord);
wenzelm@19028
   110
wenzelm@16489
   111
local
wenzelm@16489
   112
wenzelm@16489
   113
type kind =
wenzelm@22847
   114
 {empty: Object.T,
wenzelm@16489
   115
  copy: Object.T -> Object.T,
wenzelm@16489
   116
  extend: Object.T -> Object.T,
wenzelm@16489
   117
  merge: Pretty.pp -> Object.T * Object.T -> Object.T};
wenzelm@16489
   118
wenzelm@32738
   119
val kinds = Unsynchronized.ref (Datatab.empty: kind Datatab.table);
wenzelm@16489
   120
wenzelm@22847
   121
fun invoke f k =
wenzelm@19028
   122
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   123
    SOME kind => f kind
wenzelm@22847
   124
  | NONE => sys_error "Invalid theory data identifier");
wenzelm@16489
   125
wenzelm@16489
   126
in
wenzelm@16489
   127
wenzelm@33033
   128
fun invoke_empty k = invoke (K o #empty) k ();
wenzelm@33033
   129
val invoke_copy = invoke #copy;
wenzelm@33033
   130
val invoke_extend = invoke #extend;
wenzelm@33033
   131
fun invoke_merge pp = invoke (fn kind => #merge kind pp);
wenzelm@16489
   132
wenzelm@22847
   133
fun declare_theory_data empty copy extend merge =
wenzelm@16489
   134
  let
wenzelm@16489
   135
    val k = serial ();
wenzelm@22847
   136
    val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
wenzelm@32738
   137
    val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, kind)));
wenzelm@16489
   138
  in k end;
wenzelm@16489
   139
wenzelm@19028
   140
val copy_data = Datatab.map' invoke_copy;
wenzelm@19028
   141
val extend_data = Datatab.map' invoke_extend;
wenzelm@29367
   142
wenzelm@29367
   143
fun merge_data pp (data1, data2) =
wenzelm@29367
   144
  Datatab.keys (Datatab.merge (K true) (data1, data2))
wenzelm@29368
   145
  |> Par_List.map (fn k =>
wenzelm@29367
   146
    (case (Datatab.lookup data1 k, Datatab.lookup data2 k) of
wenzelm@29367
   147
      (SOME x, NONE) => (k, invoke_extend k x)
wenzelm@29367
   148
    | (NONE, SOME y) => (k, invoke_extend k y)
wenzelm@29367
   149
    | (SOME x, SOME y) => (k, invoke_merge pp k (invoke_extend k x, invoke_extend k y))))
wenzelm@29367
   150
  |> Datatab.make;
wenzelm@16489
   151
wenzelm@16489
   152
end;
wenzelm@16489
   153
wenzelm@16489
   154
wenzelm@16489
   155
wenzelm@16489
   156
(** datatype theory **)
wenzelm@16489
   157
wenzelm@16436
   158
datatype theory =
wenzelm@16436
   159
  Theory of
wenzelm@16533
   160
   (*identity*)
wenzelm@32738
   161
   {self: theory Unsynchronized.ref option,  (*dynamic self reference -- follows theory changes*)
wenzelm@29095
   162
    draft: bool,                  (*draft mode -- linear destructive changes*)
wenzelm@29093
   163
    id: serial,                   (*identifier*)
wenzelm@29093
   164
    ids: unit Inttab.table} *     (*cumulative identifiers of non-drafts -- symbolic body content*)
wenzelm@29095
   165
   (*data*)
wenzelm@29095
   166
   Object.T Datatab.table *       (*body content*)
wenzelm@29095
   167
   (*ancestry*)
wenzelm@29093
   168
   {parents: theory list,         (*immediate predecessors*)
wenzelm@29093
   169
    ancestors: theory list} *     (*all predecessors -- canonical reverse order*)
wenzelm@29095
   170
   (*history*)
wenzelm@29093
   171
   {name: string,                 (*official theory name*)
wenzelm@29093
   172
    stage: int};                  (*checkpoint counter*)
wenzelm@16436
   173
wenzelm@16436
   174
exception THEORY of string * theory list;
wenzelm@16436
   175
wenzelm@16436
   176
fun rep_theory (Theory args) = args;
wenzelm@16436
   177
wenzelm@16436
   178
val identity_of = #1 o rep_theory;
wenzelm@33033
   179
val data_of = #2 o rep_theory;
wenzelm@16489
   180
val ancestry_of = #3 o rep_theory;
wenzelm@33033
   181
val history_of = #4 o rep_theory;
wenzelm@16436
   182
wenzelm@29093
   183
fun make_identity self draft id ids = {self = self, draft = draft, id = id, ids = ids};
wenzelm@16489
   184
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
wenzelm@29093
   185
fun make_history name stage = {name = name, stage = stage};
wenzelm@16436
   186
wenzelm@16533
   187
val the_self = the o #self o identity_of;
wenzelm@16436
   188
val parents_of = #parents o ancestry_of;
wenzelm@16436
   189
val ancestors_of = #ancestors o ancestry_of;
wenzelm@16489
   190
val theory_name = #name o history_of;
wenzelm@16436
   191
wenzelm@16436
   192
wenzelm@16436
   193
(* staleness *)
wenzelm@16436
   194
wenzelm@29093
   195
fun eq_id (i: int, j) = i = j;
wenzelm@16436
   196
wenzelm@16436
   197
fun is_stale
wenzelm@32738
   198
    (Theory ({self =
wenzelm@32738
   199
        SOME (Unsynchronized.ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
wenzelm@16436
   200
      not (eq_id (id, id'))
wenzelm@16436
   201
  | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
wenzelm@16436
   202
wenzelm@16436
   203
fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
wenzelm@29093
   204
  | vitalize (thy as Theory ({self = NONE, draft, id, ids}, data, ancestry, history)) =
wenzelm@16436
   205
      let
wenzelm@32738
   206
        val r = Unsynchronized.ref thy;
wenzelm@29093
   207
        val thy' = Theory (make_identity (SOME r) draft id ids, data, ancestry, history);
wenzelm@16436
   208
      in r := thy'; thy' end;
wenzelm@16436
   209
wenzelm@16436
   210
wenzelm@29093
   211
(* draft mode *)
wenzelm@16436
   212
wenzelm@29093
   213
val is_draft = #draft o identity_of;
wenzelm@16436
   214
wenzelm@28317
   215
fun reject_draft thy =
wenzelm@33031
   216
  if is_draft thy then
wenzelm@33031
   217
    raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
wenzelm@28317
   218
  else thy;
wenzelm@28317
   219
wenzelm@29093
   220
wenzelm@29093
   221
(* names *)
wenzelm@29093
   222
wenzelm@29093
   223
val PureN = "Pure";
wenzelm@29093
   224
val draftN = "#";
wenzelm@29095
   225
val finished = ~1;
wenzelm@16436
   226
wenzelm@29093
   227
fun display_names thy =
wenzelm@29093
   228
  let
wenzelm@29093
   229
    val draft = if is_draft thy then [draftN] else [];
wenzelm@29095
   230
    val {stage, ...} = history_of thy;
wenzelm@29093
   231
    val name =
wenzelm@29095
   232
      if stage = finished then theory_name thy
wenzelm@29095
   233
      else theory_name thy ^ ":" ^ string_of_int stage;
wenzelm@29093
   234
    val ancestor_names = map theory_name (ancestors_of thy);
wenzelm@29093
   235
    val stale = if is_stale thy then ["!"] else [];
wenzelm@29093
   236
  in rev (stale @ draft @ [name] @ ancestor_names) end;
wenzelm@29069
   237
wenzelm@29093
   238
val pretty_thy = Pretty.str_list "{" "}" o display_names;
wenzelm@16436
   239
val string_of_thy = Pretty.string_of o pretty_thy;
wenzelm@16436
   240
wenzelm@16436
   241
fun pretty_abbrev_thy thy =
wenzelm@16436
   242
  let
wenzelm@29093
   243
    val names = display_names thy;
wenzelm@16436
   244
    val n = length names;
wenzelm@16436
   245
    val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
wenzelm@16436
   246
  in Pretty.str_list "{" "}" abbrev end;
wenzelm@16436
   247
wenzelm@16436
   248
val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
wenzelm@16436
   249
wenzelm@16436
   250
wenzelm@24141
   251
(* theory references *)
wenzelm@24141
   252
wenzelm@24141
   253
(*theory_ref provides a safe way to store dynamic references to a
wenzelm@24141
   254
  theory in external data structures -- a plain theory value would
wenzelm@24141
   255
  become stale as the self reference moves on*)
wenzelm@24141
   256
wenzelm@33033
   257
datatype theory_ref = Theory_Ref of theory Unsynchronized.ref;
wenzelm@16436
   258
wenzelm@33033
   259
fun deref (Theory_Ref (Unsynchronized.ref thy)) = thy;
wenzelm@24141
   260
wenzelm@24141
   261
fun check_thy thy =  (*thread-safe version*)
wenzelm@33033
   262
  let val thy_ref = Theory_Ref (the_self thy) in
wenzelm@24141
   263
    if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
wenzelm@24141
   264
    else thy_ref
wenzelm@24141
   265
  end;
wenzelm@24141
   266
wenzelm@24141
   267
wenzelm@29093
   268
(* build ids *)
wenzelm@29093
   269
wenzelm@29093
   270
fun insert_id draft id ids =
wenzelm@29093
   271
  if draft then ids
wenzelm@29093
   272
  else Inttab.update (id, ()) ids;
wenzelm@16436
   273
wenzelm@29093
   274
fun merge_ids
wenzelm@29093
   275
    (Theory ({draft = draft1, id = id1, ids = ids1, ...}, _, _, _))
wenzelm@29093
   276
    (Theory ({draft = draft2, id = id2, ids = ids2, ...}, _, _, _)) =
wenzelm@29093
   277
  Inttab.merge (K true) (ids1, ids2)
wenzelm@29093
   278
  |> insert_id draft1 id1
wenzelm@29093
   279
  |> insert_id draft2 id2;
wenzelm@16436
   280
wenzelm@16436
   281
wenzelm@16533
   282
(* equality and inclusion *)
wenzelm@16533
   283
wenzelm@24141
   284
val eq_thy = eq_id o pairself (#id o identity_of);
wenzelm@16533
   285
wenzelm@29069
   286
fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
wenzelm@29093
   287
  Inttab.defined ids id;
wenzelm@16533
   288
wenzelm@16533
   289
fun subthy thys = eq_thy thys orelse proper_subthy thys;
wenzelm@16533
   290
wenzelm@16594
   291
fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
wenzelm@16594
   292
wenzelm@16533
   293
wenzelm@29093
   294
(* consistent ancestors *)
wenzelm@29093
   295
wenzelm@29093
   296
fun extend_ancestors thy thys =
wenzelm@33033
   297
  if member eq_thy thys thy then
wenzelm@33033
   298
    raise THEORY ("Duplicate theory node", thy :: thys)
wenzelm@29093
   299
  else thy :: thys;
wenzelm@29093
   300
wenzelm@29093
   301
fun extend_ancestors_of thy = extend_ancestors thy (ancestors_of thy);
wenzelm@29093
   302
wenzelm@29093
   303
val merge_ancestors = merge (fn (thy1, thy2) =>
wenzelm@29093
   304
  eq_thy (thy1, thy2) orelse
wenzelm@29093
   305
    theory_name thy1 = theory_name thy2 andalso
wenzelm@29093
   306
      raise THEORY ("Inconsistent theory versions", [thy1, thy2]));
wenzelm@29093
   307
wenzelm@29093
   308
wenzelm@23355
   309
(* trivial merge *)
wenzelm@16436
   310
wenzelm@16436
   311
fun merge (thy1, thy2) =
wenzelm@16719
   312
  if eq_thy (thy1, thy2) then thy1
wenzelm@16719
   313
  else if proper_subthy (thy2, thy1) then thy1
wenzelm@16719
   314
  else if proper_subthy (thy1, thy2) then thy2
wenzelm@29093
   315
  else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
wenzelm@29093
   316
    str_of_thy thy1, str_of_thy thy2]);
wenzelm@16436
   317
wenzelm@16719
   318
fun merge_refs (ref1, ref2) =
wenzelm@16719
   319
  if ref1 = ref2 then ref1
wenzelm@24141
   320
  else check_thy (merge (deref ref1, deref ref2));
wenzelm@16436
   321
wenzelm@16436
   322
wenzelm@16489
   323
wenzelm@16489
   324
(** build theories **)
wenzelm@16489
   325
wenzelm@16489
   326
(* primitives *)
wenzelm@16489
   327
wenzelm@33606
   328
local
wenzelm@33606
   329
  val lock = Mutex.mutex ();
wenzelm@33606
   330
in
wenzelm@33606
   331
  fun SYNCHRONIZED e = SimpleThread.synchronized "theory" lock e;
wenzelm@33606
   332
end;
wenzelm@33606
   333
wenzelm@29093
   334
fun create_thy self draft ids data ancestry history =
wenzelm@29093
   335
  let val identity = make_identity self draft (serial ()) ids;
wenzelm@29093
   336
  in vitalize (Theory (identity, data, ancestry, history)) end;
wenzelm@16436
   337
wenzelm@29093
   338
fun change_thy draft' f thy =
wenzelm@16489
   339
  let
wenzelm@29093
   340
    val Theory ({self, draft, id, ids}, data, ancestry, history) = thy;
wenzelm@16489
   341
    val (self', data', ancestry') =
wenzelm@29093
   342
      if draft then (self, data, ancestry)    (*destructive change!*)
wenzelm@29093
   343
      else if #stage history > 0
wenzelm@22847
   344
      then (NONE, copy_data data, ancestry)
wenzelm@29093
   345
      else (NONE, extend_data data, make_ancestry [thy] (extend_ancestors_of thy));
wenzelm@29093
   346
    val ids' = insert_id draft id ids;
wenzelm@16489
   347
    val data'' = f data';
wenzelm@33606
   348
    val thy' = SYNCHRONIZED (fn () =>
wenzelm@29093
   349
      (check_thy thy; create_thy self' draft' ids' data'' ancestry' history));
wenzelm@24369
   350
  in thy' end;
wenzelm@16489
   351
wenzelm@29093
   352
val name_thy = change_thy false I;
wenzelm@29093
   353
val extend_thy = change_thy true I;
wenzelm@29093
   354
val modify_thy = change_thy true;
wenzelm@16489
   355
wenzelm@24369
   356
fun copy_thy thy =
wenzelm@24141
   357
  let
wenzelm@29093
   358
    val Theory ({draft, id, ids, ...}, data, ancestry, history) = thy;
wenzelm@29093
   359
    val ids' = insert_id draft id ids;
wenzelm@24369
   360
    val data' = copy_data data;
wenzelm@33606
   361
    val thy' = SYNCHRONIZED (fn () =>
wenzelm@29093
   362
      (check_thy thy; create_thy NONE true ids' data' ancestry history));
wenzelm@24369
   363
  in thy' end;
wenzelm@16489
   364
wenzelm@29093
   365
val pre_pure_thy = create_thy NONE true Inttab.empty
wenzelm@29069
   366
  Datatab.empty (make_ancestry [] []) (make_history PureN 0);
wenzelm@16489
   367
wenzelm@16489
   368
wenzelm@16489
   369
(* named theory nodes *)
wenzelm@16489
   370
wenzelm@16489
   371
fun merge_thys pp (thy1, thy2) =
wenzelm@26957
   372
  let
wenzelm@29093
   373
    val ids = merge_ids thy1 thy2;
wenzelm@26957
   374
    val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
wenzelm@26957
   375
    val ancestry = make_ancestry [] [];
wenzelm@29069
   376
    val history = make_history "" 0;
wenzelm@33606
   377
    val thy' = SYNCHRONIZED (fn () =>
wenzelm@29093
   378
     (check_thy thy1; check_thy thy2; create_thy NONE true ids data ancestry history));
wenzelm@26957
   379
  in thy' end;
wenzelm@16489
   380
wenzelm@16533
   381
fun maximal_thys thys =
wenzelm@28617
   382
  thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
wenzelm@16533
   383
wenzelm@16489
   384
fun begin_thy pp name imports =
wenzelm@29093
   385
  if name = "" orelse name = draftN then error ("Bad theory name: " ^ quote name)
wenzelm@24369
   386
  else
wenzelm@16489
   387
    let
wenzelm@24141
   388
      val parents = maximal_thys (distinct eq_thy imports);
wenzelm@29093
   389
      val ancestors =
wenzelm@29093
   390
        Library.foldl merge_ancestors ([], map ancestors_of parents)
wenzelm@29093
   391
        |> fold extend_ancestors parents;
wenzelm@29093
   392
wenzelm@29093
   393
      val Theory ({ids, ...}, data, _, _) =
wenzelm@16489
   394
        (case parents of
wenzelm@16489
   395
          [] => error "No parent theories"
wenzelm@16533
   396
        | [thy] => extend_thy thy
wenzelm@16533
   397
        | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
wenzelm@29093
   398
wenzelm@16489
   399
      val ancestry = make_ancestry parents ancestors;
wenzelm@29069
   400
      val history = make_history name 0;
wenzelm@33606
   401
      val thy' = SYNCHRONIZED (fn () =>
wenzelm@29093
   402
        (map check_thy imports; create_thy NONE true ids data ancestry history));
wenzelm@24369
   403
    in thy' end;
wenzelm@16436
   404
wenzelm@16436
   405
wenzelm@29093
   406
(* history stages *)
wenzelm@29093
   407
wenzelm@29093
   408
fun history_stage f thy =
wenzelm@29093
   409
  let
wenzelm@29093
   410
    val {name, stage} = history_of thy;
wenzelm@29095
   411
    val _ = stage = finished andalso raise THEORY ("Theory already finished", [thy]);
wenzelm@29093
   412
    val history' = make_history name (f stage);
wenzelm@29093
   413
    val thy' as Theory (identity', data', ancestry', _) = name_thy thy;
wenzelm@33606
   414
    val thy'' = SYNCHRONIZED (fn () =>
wenzelm@29093
   415
      (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
wenzelm@29093
   416
  in thy'' end;
wenzelm@16436
   417
wenzelm@16489
   418
fun checkpoint_thy thy =
wenzelm@29093
   419
  if is_draft thy then history_stage (fn stage => stage + 1) thy
wenzelm@29093
   420
  else thy;
wenzelm@16489
   421
wenzelm@29095
   422
val finish_thy = history_stage (fn _ => finished);
wenzelm@16489
   423
wenzelm@16489
   424
wenzelm@16489
   425
(* theory data *)
wenzelm@16436
   426
wenzelm@33033
   427
structure Theory_Data =
wenzelm@16489
   428
struct
wenzelm@16489
   429
wenzelm@16489
   430
val declare = declare_theory_data;
wenzelm@16489
   431
wenzelm@16489
   432
fun get k dest thy =
wenzelm@22847
   433
  dest ((case Datatab.lookup (data_of thy) k of
wenzelm@22847
   434
    SOME x => x
wenzelm@22847
   435
  | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
wenzelm@16489
   436
wenzelm@22847
   437
fun put k mk x = modify_thy (Datatab.update (k, mk x));
wenzelm@16489
   438
wenzelm@16489
   439
end;
wenzelm@16436
   440
wenzelm@16436
   441
wenzelm@16436
   442
wenzelm@16533
   443
(*** proof context ***)
wenzelm@16533
   444
wenzelm@33031
   445
(* datatype Proof.context *)
wenzelm@17060
   446
wenzelm@33031
   447
structure Proof =
wenzelm@33031
   448
struct
wenzelm@33031
   449
  datatype context = Context of Object.T Datatab.table * theory_ref;
wenzelm@33031
   450
end;
wenzelm@33031
   451
wenzelm@33031
   452
fun theory_of_proof (Proof.Context (_, thy_ref)) = deref thy_ref;
wenzelm@33031
   453
fun data_of_proof (Proof.Context (data, _)) = data;
wenzelm@33031
   454
fun map_prf f (Proof.Context (data, thy_ref)) = Proof.Context (f data, thy_ref);
wenzelm@17060
   455
wenzelm@16533
   456
wenzelm@16533
   457
(* proof data kinds *)
wenzelm@16533
   458
wenzelm@16533
   459
local
wenzelm@16533
   460
wenzelm@32738
   461
val kinds = Unsynchronized.ref (Datatab.empty: (theory -> Object.T) Datatab.table);
wenzelm@16533
   462
wenzelm@22847
   463
fun invoke_init k =
wenzelm@19028
   464
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   465
    SOME init => init
wenzelm@22847
   466
  | NONE => sys_error "Invalid proof data identifier");
wenzelm@16533
   467
wenzelm@22847
   468
fun init_data thy =
wenzelm@22847
   469
  Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
wenzelm@22847
   470
wenzelm@22847
   471
fun init_new_data data thy =
wenzelm@22847
   472
  Datatab.merge (K true) (data, init_data thy);
wenzelm@16533
   473
wenzelm@16533
   474
in
wenzelm@16533
   475
wenzelm@33031
   476
fun raw_transfer thy' (Proof.Context (data, thy_ref)) =
wenzelm@24141
   477
  let
wenzelm@24141
   478
    val thy = deref thy_ref;
wenzelm@24141
   479
    val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
wenzelm@24141
   480
    val _ = check_thy thy;
wenzelm@24184
   481
    val data' = init_new_data data thy';
wenzelm@24141
   482
    val thy_ref' = check_thy thy';
wenzelm@33031
   483
  in Proof.Context (data', thy_ref') end;
wenzelm@22847
   484
wenzelm@33031
   485
structure ProofContext =
wenzelm@33031
   486
struct
wenzelm@33031
   487
  val theory_of = theory_of_proof;
wenzelm@33031
   488
  fun init thy = Proof.Context (init_data thy, check_thy thy);
wenzelm@33031
   489
end;
wenzelm@16533
   490
wenzelm@33033
   491
structure Proof_Data =
wenzelm@16533
   492
struct
wenzelm@16533
   493
wenzelm@22847
   494
fun declare init =
wenzelm@16533
   495
  let
wenzelm@16533
   496
    val k = serial ();
wenzelm@32738
   497
    val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, init)));
wenzelm@16533
   498
  in k end;
wenzelm@16533
   499
wenzelm@16533
   500
fun get k dest prf =
wenzelm@22847
   501
  dest (case Datatab.lookup (data_of_proof prf) k of
wenzelm@22847
   502
    SOME x => x
wenzelm@33031
   503
  | NONE => invoke_init k (ProofContext.theory_of prf));   (*adhoc value*)
wenzelm@16533
   504
wenzelm@19028
   505
fun put k mk x = map_prf (Datatab.update (k, mk x));
wenzelm@16533
   506
wenzelm@16533
   507
end;
wenzelm@16533
   508
wenzelm@16533
   509
end;
wenzelm@16533
   510
wenzelm@16533
   511
wenzelm@18632
   512
wenzelm@16533
   513
(*** generic context ***)
wenzelm@16533
   514
wenzelm@33031
   515
datatype generic = Theory of theory | Proof of Proof.context;
wenzelm@18632
   516
wenzelm@18632
   517
fun cases f _ (Theory thy) = f thy
wenzelm@18632
   518
  | cases _ g (Proof prf) = g prf;
wenzelm@16533
   519
wenzelm@19678
   520
fun mapping f g = cases (Theory o f) (Proof o g);
wenzelm@21660
   521
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
wenzelm@19678
   522
wenzelm@23595
   523
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
wenzelm@23595
   524
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
wenzelm@16533
   525
wenzelm@18731
   526
fun map_theory f = Theory o f o the_theory;
wenzelm@18731
   527
fun map_proof f = Proof o f o the_proof;
wenzelm@18731
   528
wenzelm@26486
   529
fun map_theory_result f = apsnd Theory o f o the_theory;
wenzelm@26486
   530
fun map_proof_result f = apsnd Proof o f o the_proof;
wenzelm@26486
   531
wenzelm@18731
   532
fun theory_map f = the_theory o f o Theory;
wenzelm@18731
   533
fun proof_map f = the_proof o f o Proof;
wenzelm@18665
   534
wenzelm@33031
   535
val theory_of = cases I ProofContext.theory_of;
wenzelm@33031
   536
val proof_of = cases ProofContext.init I;
wenzelm@16533
   537
wenzelm@22085
   538
wenzelm@22085
   539
wenzelm@26413
   540
(** thread data **)
wenzelm@26413
   541
wenzelm@26413
   542
local val tag = Universal.tag () : generic option Universal.tag in
wenzelm@26413
   543
wenzelm@26413
   544
fun thread_data () =
wenzelm@28122
   545
  (case Thread.getLocal tag of
wenzelm@26413
   546
    SOME (SOME context) => SOME context
wenzelm@26413
   547
  | _ => NONE);
wenzelm@26413
   548
wenzelm@26413
   549
fun the_thread_data () =
wenzelm@26413
   550
  (case thread_data () of
wenzelm@26413
   551
    SOME context => context
wenzelm@26413
   552
  | _ => error "Unknown context");
wenzelm@26413
   553
wenzelm@28122
   554
fun set_thread_data context = Thread.setLocal (tag, context);
wenzelm@26413
   555
fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
wenzelm@26413
   556
wenzelm@26428
   557
end;
wenzelm@26428
   558
wenzelm@26428
   559
fun >>> f =
wenzelm@26428
   560
  let
wenzelm@26463
   561
    val (res, context') = f (the_thread_data ());
wenzelm@26463
   562
    val _ = set_thread_data (SOME context');
wenzelm@26428
   563
  in res end;
wenzelm@26428
   564
wenzelm@26421
   565
nonfix >>;
wenzelm@26463
   566
fun >> f = >>> (fn context => ((), f context));
wenzelm@26413
   567
wenzelm@26428
   568
val _ = set_thread_data (SOME (Theory pre_pure_thy));
wenzelm@26413
   569
wenzelm@6185
   570
end;
wenzelm@6185
   571
wenzelm@33031
   572
structure Basic_Context: BASIC_CONTEXT = Context;
wenzelm@33031
   573
open Basic_Context;
wenzelm@16436
   574
wenzelm@16436
   575
wenzelm@16436
   576
wenzelm@16533
   577
(*** type-safe interfaces for data declarations ***)
wenzelm@16533
   578
wenzelm@16533
   579
(** theory data **)
wenzelm@16436
   580
wenzelm@33517
   581
signature OLD_THEORY_DATA_ARGS =
wenzelm@16436
   582
sig
wenzelm@16436
   583
  type T
wenzelm@16436
   584
  val empty: T
wenzelm@16436
   585
  val copy: T -> T
wenzelm@16436
   586
  val extend: T -> T
wenzelm@16436
   587
  val merge: Pretty.pp -> T * T -> T
wenzelm@16436
   588
end;
wenzelm@16436
   589
wenzelm@33517
   590
signature OLD_THEORY_DATA =
wenzelm@16436
   591
sig
wenzelm@16436
   592
  type T
wenzelm@16436
   593
  val get: theory -> T
wenzelm@16436
   594
  val put: T -> theory -> theory
wenzelm@16436
   595
  val map: (T -> T) -> theory -> theory
wenzelm@22847
   596
  val init: theory -> theory
wenzelm@16436
   597
end;
wenzelm@16436
   598
wenzelm@33517
   599
functor TheoryDataFun(Data: OLD_THEORY_DATA_ARGS): OLD_THEORY_DATA =
wenzelm@16436
   600
struct
wenzelm@16436
   601
wenzelm@16436
   602
type T = Data.T;
wenzelm@16436
   603
exception Data of T;
wenzelm@16436
   604
wenzelm@33033
   605
val kind = Context.Theory_Data.declare
wenzelm@16436
   606
  (Data Data.empty)
wenzelm@16436
   607
  (fn Data x => Data (Data.copy x))
wenzelm@16436
   608
  (fn Data x => Data (Data.extend x))
wenzelm@16489
   609
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
wenzelm@16436
   610
wenzelm@33033
   611
val get = Context.Theory_Data.get kind (fn Data x => x);
wenzelm@33033
   612
val put = Context.Theory_Data.put kind Data;
wenzelm@16436
   613
fun map f thy = put (f (get thy)) thy;
wenzelm@16436
   614
wenzelm@22847
   615
fun init thy = map I thy;
wenzelm@22847
   616
wenzelm@16436
   617
end;
wenzelm@16436
   618
wenzelm@33517
   619
signature THEORY_DATA_ARGS =
wenzelm@33517
   620
sig
wenzelm@33517
   621
  type T
wenzelm@33517
   622
  val empty: T
wenzelm@33517
   623
  val extend: T -> T
wenzelm@33517
   624
  val merge: T * T -> T
wenzelm@33517
   625
end;
wenzelm@33517
   626
wenzelm@33517
   627
signature THEORY_DATA =
wenzelm@33517
   628
sig
wenzelm@33517
   629
  type T
wenzelm@33517
   630
  val get: theory -> T
wenzelm@33517
   631
  val put: T -> theory -> theory
wenzelm@33517
   632
  val map: (T -> T) -> theory -> theory
wenzelm@33517
   633
end;
wenzelm@33517
   634
wenzelm@33517
   635
functor Theory_Data(Data: THEORY_DATA_ARGS): THEORY_DATA =
wenzelm@33517
   636
struct
wenzelm@33517
   637
wenzelm@33517
   638
structure Result = TheoryDataFun
wenzelm@33517
   639
(
wenzelm@33517
   640
  type T = Data.T;
wenzelm@33517
   641
  val empty = Data.empty;
wenzelm@33517
   642
  val copy = I;
wenzelm@33517
   643
  val extend = Data.extend;
wenzelm@33517
   644
  fun merge _ = Data.merge;
wenzelm@33517
   645
);
wenzelm@33517
   646
wenzelm@33517
   647
open Result;
wenzelm@33517
   648
wenzelm@33517
   649
end;
wenzelm@33517
   650
wenzelm@16533
   651
wenzelm@16533
   652
wenzelm@16533
   653
(** proof data **)
wenzelm@16533
   654
wenzelm@16533
   655
signature PROOF_DATA_ARGS =
wenzelm@16533
   656
sig
wenzelm@16533
   657
  type T
wenzelm@16533
   658
  val init: theory -> T
wenzelm@16533
   659
end;
wenzelm@16533
   660
wenzelm@16533
   661
signature PROOF_DATA =
wenzelm@16533
   662
sig
wenzelm@16533
   663
  type T
wenzelm@33031
   664
  val get: Proof.context -> T
wenzelm@33031
   665
  val put: T -> Proof.context -> Proof.context
wenzelm@33031
   666
  val map: (T -> T) -> Proof.context -> Proof.context
wenzelm@16533
   667
end;
wenzelm@16533
   668
wenzelm@33517
   669
functor Proof_Data(Data: PROOF_DATA_ARGS): PROOF_DATA =
wenzelm@16533
   670
struct
wenzelm@16533
   671
wenzelm@16533
   672
type T = Data.T;
wenzelm@16533
   673
exception Data of T;
wenzelm@16533
   674
wenzelm@33033
   675
val kind = Context.Proof_Data.declare (Data o Data.init);
wenzelm@16533
   676
wenzelm@33033
   677
val get = Context.Proof_Data.get kind (fn Data x => x);
wenzelm@33033
   678
val put = Context.Proof_Data.put kind Data;
wenzelm@16533
   679
fun map f prf = put (f (get prf)) prf;
wenzelm@16533
   680
wenzelm@16533
   681
end;
wenzelm@16533
   682
wenzelm@18632
   683
wenzelm@18632
   684
wenzelm@18632
   685
(** generic data **)
wenzelm@18632
   686
wenzelm@18632
   687
signature GENERIC_DATA_ARGS =
wenzelm@18632
   688
sig
wenzelm@18632
   689
  type T
wenzelm@18632
   690
  val empty: T
wenzelm@18632
   691
  val extend: T -> T
wenzelm@33517
   692
  val merge: T * T -> T
wenzelm@18632
   693
end;
wenzelm@18632
   694
wenzelm@18632
   695
signature GENERIC_DATA =
wenzelm@18632
   696
sig
wenzelm@18632
   697
  type T
wenzelm@18632
   698
  val get: Context.generic -> T
wenzelm@18632
   699
  val put: T -> Context.generic -> Context.generic
wenzelm@18632
   700
  val map: (T -> T) -> Context.generic -> Context.generic
wenzelm@18632
   701
end;
wenzelm@18632
   702
wenzelm@33517
   703
functor Generic_Data(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
wenzelm@18632
   704
struct
wenzelm@18632
   705
wenzelm@33517
   706
structure Thy_Data = Theory_Data(Data);
wenzelm@33517
   707
structure Prf_Data = Proof_Data(type T = Data.T val init = Thy_Data.get);
wenzelm@18632
   708
wenzelm@18632
   709
type T = Data.T;
wenzelm@18632
   710
wenzelm@33033
   711
fun get (Context.Theory thy) = Thy_Data.get thy
wenzelm@33033
   712
  | get (Context.Proof prf) = Prf_Data.get prf;
wenzelm@18632
   713
wenzelm@33033
   714
fun put x (Context.Theory thy) = Context.Theory (Thy_Data.put x thy)
wenzelm@33033
   715
  | put x (Context.Proof prf) = Context.Proof (Prf_Data.put x prf);
wenzelm@18632
   716
wenzelm@18632
   717
fun map f ctxt = put (f (get ctxt)) ctxt;
wenzelm@18632
   718
wenzelm@18632
   719
end;
wenzelm@18632
   720
wenzelm@16533
   721
(*hide private interface*)
wenzelm@16436
   722
structure Context: CONTEXT = Context;
wenzelm@20297
   723