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