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