src/Pure/context.ML
author wenzelm
Tue Oct 20 21:26:45 2009 +0200 (2009-10-20)
changeset 33031 b75c35574e04
parent 32784 1a5dde5079ac
child 33033 fcc77a029bb2
permissions -rw-r--r--
backpatching of structure Proof and ProofContext -- avoid odd aliases;
renamed transfer_proof to raw_transfer;
indicate firm naming conventions for theory, Proof.context, Context.generic;
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@16436
    84
  structure TheoryData:
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@16533
    91
  structure ProofData:
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@22847
   128
fun invoke_empty k   = invoke (K o #empty) k ();
wenzelm@22847
   129
val invoke_copy      = invoke #copy;
wenzelm@22847
   130
val invoke_extend    = invoke #extend;
wenzelm@22847
   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@16436
   179
val data_of     = #2 o rep_theory;
wenzelm@16489
   180
val ancestry_of = #3 o rep_theory;
wenzelm@16489
   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@32738
   257
datatype theory_ref = TheoryRef of theory Unsynchronized.ref;
wenzelm@16436
   258
wenzelm@32738
   259
fun deref (TheoryRef (Unsynchronized.ref thy)) = thy;
wenzelm@24141
   260
wenzelm@24141
   261
fun check_thy thy =  (*thread-safe version*)
wenzelm@24141
   262
  let val thy_ref = TheoryRef (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@29093
   297
  if member eq_thy thys thy then raise THEORY ("Duplicate theory node", thy :: thys)
wenzelm@29093
   298
  else thy :: thys;
wenzelm@29093
   299
wenzelm@29093
   300
fun extend_ancestors_of thy = extend_ancestors thy (ancestors_of thy);
wenzelm@29093
   301
wenzelm@29093
   302
val merge_ancestors = merge (fn (thy1, thy2) =>
wenzelm@29093
   303
  eq_thy (thy1, thy2) orelse
wenzelm@29093
   304
    theory_name thy1 = theory_name thy2 andalso
wenzelm@29093
   305
      raise THEORY ("Inconsistent theory versions", [thy1, thy2]));
wenzelm@29093
   306
wenzelm@29093
   307
wenzelm@23355
   308
(* trivial merge *)
wenzelm@16436
   309
wenzelm@16436
   310
fun merge (thy1, thy2) =
wenzelm@16719
   311
  if eq_thy (thy1, thy2) then thy1
wenzelm@16719
   312
  else if proper_subthy (thy2, thy1) then thy1
wenzelm@16719
   313
  else if proper_subthy (thy1, thy2) then thy2
wenzelm@29093
   314
  else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
wenzelm@29093
   315
    str_of_thy thy1, str_of_thy thy2]);
wenzelm@16436
   316
wenzelm@16719
   317
fun merge_refs (ref1, ref2) =
wenzelm@16719
   318
  if ref1 = ref2 then ref1
wenzelm@24141
   319
  else check_thy (merge (deref ref1, deref ref2));
wenzelm@16436
   320
wenzelm@16436
   321
wenzelm@16489
   322
wenzelm@16489
   323
(** build theories **)
wenzelm@16489
   324
wenzelm@16489
   325
(* primitives *)
wenzelm@16489
   326
wenzelm@29093
   327
fun create_thy self draft ids data ancestry history =
wenzelm@29093
   328
  let val identity = make_identity self draft (serial ()) ids;
wenzelm@29093
   329
  in vitalize (Theory (identity, data, ancestry, history)) end;
wenzelm@16436
   330
wenzelm@29093
   331
fun change_thy draft' f thy =
wenzelm@16489
   332
  let
wenzelm@29093
   333
    val Theory ({self, draft, id, ids}, data, ancestry, history) = thy;
wenzelm@16489
   334
    val (self', data', ancestry') =
wenzelm@29093
   335
      if draft then (self, data, ancestry)    (*destructive change!*)
wenzelm@29093
   336
      else if #stage history > 0
wenzelm@22847
   337
      then (NONE, copy_data data, ancestry)
wenzelm@29093
   338
      else (NONE, extend_data data, make_ancestry [thy] (extend_ancestors_of thy));
wenzelm@29093
   339
    val ids' = insert_id draft id ids;
wenzelm@16489
   340
    val data'' = f data';
wenzelm@24369
   341
    val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@29093
   342
      (check_thy thy; create_thy self' draft' ids' data'' ancestry' history));
wenzelm@24369
   343
  in thy' end;
wenzelm@16489
   344
wenzelm@29093
   345
val name_thy = change_thy false I;
wenzelm@29093
   346
val extend_thy = change_thy true I;
wenzelm@29093
   347
val modify_thy = change_thy true;
wenzelm@16489
   348
wenzelm@24369
   349
fun copy_thy thy =
wenzelm@24141
   350
  let
wenzelm@29093
   351
    val Theory ({draft, id, ids, ...}, data, ancestry, history) = thy;
wenzelm@29093
   352
    val ids' = insert_id draft id ids;
wenzelm@24369
   353
    val data' = copy_data data;
wenzelm@24369
   354
    val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@29093
   355
      (check_thy thy; create_thy NONE true ids' data' ancestry history));
wenzelm@24369
   356
  in thy' end;
wenzelm@16489
   357
wenzelm@29093
   358
val pre_pure_thy = create_thy NONE true Inttab.empty
wenzelm@29069
   359
  Datatab.empty (make_ancestry [] []) (make_history PureN 0);
wenzelm@16489
   360
wenzelm@16489
   361
wenzelm@16489
   362
(* named theory nodes *)
wenzelm@16489
   363
wenzelm@16489
   364
fun merge_thys pp (thy1, thy2) =
wenzelm@26957
   365
  let
wenzelm@29093
   366
    val ids = merge_ids thy1 thy2;
wenzelm@26957
   367
    val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
wenzelm@26957
   368
    val ancestry = make_ancestry [] [];
wenzelm@29069
   369
    val history = make_history "" 0;
wenzelm@26957
   370
    val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@29093
   371
     (check_thy thy1; check_thy thy2; create_thy NONE true ids data ancestry history));
wenzelm@26957
   372
  in thy' end;
wenzelm@16489
   373
wenzelm@16533
   374
fun maximal_thys thys =
wenzelm@28617
   375
  thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
wenzelm@16533
   376
wenzelm@16489
   377
fun begin_thy pp name imports =
wenzelm@29093
   378
  if name = "" orelse name = draftN then error ("Bad theory name: " ^ quote name)
wenzelm@24369
   379
  else
wenzelm@16489
   380
    let
wenzelm@24141
   381
      val parents = maximal_thys (distinct eq_thy imports);
wenzelm@29093
   382
      val ancestors =
wenzelm@29093
   383
        Library.foldl merge_ancestors ([], map ancestors_of parents)
wenzelm@29093
   384
        |> fold extend_ancestors parents;
wenzelm@29093
   385
wenzelm@29093
   386
      val Theory ({ids, ...}, data, _, _) =
wenzelm@16489
   387
        (case parents of
wenzelm@16489
   388
          [] => error "No parent theories"
wenzelm@16533
   389
        | [thy] => extend_thy thy
wenzelm@16533
   390
        | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
wenzelm@29093
   391
wenzelm@16489
   392
      val ancestry = make_ancestry parents ancestors;
wenzelm@29069
   393
      val history = make_history name 0;
wenzelm@24369
   394
      val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@29093
   395
        (map check_thy imports; create_thy NONE true ids data ancestry history));
wenzelm@24369
   396
    in thy' end;
wenzelm@16436
   397
wenzelm@16436
   398
wenzelm@29093
   399
(* history stages *)
wenzelm@29093
   400
wenzelm@29093
   401
fun history_stage f thy =
wenzelm@29093
   402
  let
wenzelm@29093
   403
    val {name, stage} = history_of thy;
wenzelm@29095
   404
    val _ = stage = finished andalso raise THEORY ("Theory already finished", [thy]);
wenzelm@29093
   405
    val history' = make_history name (f stage);
wenzelm@29093
   406
    val thy' as Theory (identity', data', ancestry', _) = name_thy thy;
wenzelm@29093
   407
    val thy'' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@29093
   408
      (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
wenzelm@29093
   409
  in thy'' end;
wenzelm@16436
   410
wenzelm@16489
   411
fun checkpoint_thy thy =
wenzelm@29093
   412
  if is_draft thy then history_stage (fn stage => stage + 1) thy
wenzelm@29093
   413
  else thy;
wenzelm@16489
   414
wenzelm@29095
   415
val finish_thy = history_stage (fn _ => finished);
wenzelm@16489
   416
wenzelm@16489
   417
wenzelm@16489
   418
(* theory data *)
wenzelm@16436
   419
wenzelm@16489
   420
structure TheoryData =
wenzelm@16489
   421
struct
wenzelm@16489
   422
wenzelm@16489
   423
val declare = declare_theory_data;
wenzelm@16489
   424
wenzelm@16489
   425
fun get k dest thy =
wenzelm@22847
   426
  dest ((case Datatab.lookup (data_of thy) k of
wenzelm@22847
   427
    SOME x => x
wenzelm@22847
   428
  | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
wenzelm@16489
   429
wenzelm@22847
   430
fun put k mk x = modify_thy (Datatab.update (k, mk x));
wenzelm@16489
   431
wenzelm@16489
   432
end;
wenzelm@16436
   433
wenzelm@16436
   434
wenzelm@16436
   435
wenzelm@16533
   436
(*** proof context ***)
wenzelm@16533
   437
wenzelm@33031
   438
(* datatype Proof.context *)
wenzelm@17060
   439
wenzelm@33031
   440
structure Proof =
wenzelm@33031
   441
struct
wenzelm@33031
   442
  datatype context = Context of Object.T Datatab.table * theory_ref;
wenzelm@33031
   443
end;
wenzelm@33031
   444
wenzelm@33031
   445
fun theory_of_proof (Proof.Context (_, thy_ref)) = deref thy_ref;
wenzelm@33031
   446
fun data_of_proof (Proof.Context (data, _)) = data;
wenzelm@33031
   447
fun map_prf f (Proof.Context (data, thy_ref)) = Proof.Context (f data, thy_ref);
wenzelm@17060
   448
wenzelm@16533
   449
wenzelm@16533
   450
(* proof data kinds *)
wenzelm@16533
   451
wenzelm@16533
   452
local
wenzelm@16533
   453
wenzelm@32738
   454
val kinds = Unsynchronized.ref (Datatab.empty: (theory -> Object.T) Datatab.table);
wenzelm@16533
   455
wenzelm@22847
   456
fun invoke_init k =
wenzelm@19028
   457
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   458
    SOME init => init
wenzelm@22847
   459
  | NONE => sys_error "Invalid proof data identifier");
wenzelm@16533
   460
wenzelm@22847
   461
fun init_data thy =
wenzelm@22847
   462
  Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
wenzelm@22847
   463
wenzelm@22847
   464
fun init_new_data data thy =
wenzelm@22847
   465
  Datatab.merge (K true) (data, init_data thy);
wenzelm@16533
   466
wenzelm@16533
   467
in
wenzelm@16533
   468
wenzelm@33031
   469
fun raw_transfer thy' (Proof.Context (data, thy_ref)) =
wenzelm@24141
   470
  let
wenzelm@24141
   471
    val thy = deref thy_ref;
wenzelm@24141
   472
    val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
wenzelm@24141
   473
    val _ = check_thy thy;
wenzelm@24184
   474
    val data' = init_new_data data thy';
wenzelm@24141
   475
    val thy_ref' = check_thy thy';
wenzelm@33031
   476
  in Proof.Context (data', thy_ref') end;
wenzelm@22847
   477
wenzelm@33031
   478
structure ProofContext =
wenzelm@33031
   479
struct
wenzelm@33031
   480
  val theory_of = theory_of_proof;
wenzelm@33031
   481
  fun init thy = Proof.Context (init_data thy, check_thy thy);
wenzelm@33031
   482
end;
wenzelm@16533
   483
wenzelm@16533
   484
structure ProofData =
wenzelm@16533
   485
struct
wenzelm@16533
   486
wenzelm@22847
   487
fun declare init =
wenzelm@16533
   488
  let
wenzelm@16533
   489
    val k = serial ();
wenzelm@32738
   490
    val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, init)));
wenzelm@16533
   491
  in k end;
wenzelm@16533
   492
wenzelm@16533
   493
fun get k dest prf =
wenzelm@22847
   494
  dest (case Datatab.lookup (data_of_proof prf) k of
wenzelm@22847
   495
    SOME x => x
wenzelm@33031
   496
  | NONE => invoke_init k (ProofContext.theory_of prf));   (*adhoc value*)
wenzelm@16533
   497
wenzelm@19028
   498
fun put k mk x = map_prf (Datatab.update (k, mk x));
wenzelm@16533
   499
wenzelm@16533
   500
end;
wenzelm@16533
   501
wenzelm@16533
   502
end;
wenzelm@16533
   503
wenzelm@16533
   504
wenzelm@18632
   505
wenzelm@16533
   506
(*** generic context ***)
wenzelm@16533
   507
wenzelm@33031
   508
datatype generic = Theory of theory | Proof of Proof.context;
wenzelm@18632
   509
wenzelm@18632
   510
fun cases f _ (Theory thy) = f thy
wenzelm@18632
   511
  | cases _ g (Proof prf) = g prf;
wenzelm@16533
   512
wenzelm@19678
   513
fun mapping f g = cases (Theory o f) (Proof o g);
wenzelm@21660
   514
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
wenzelm@19678
   515
wenzelm@23595
   516
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
wenzelm@23595
   517
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
wenzelm@16533
   518
wenzelm@18731
   519
fun map_theory f = Theory o f o the_theory;
wenzelm@18731
   520
fun map_proof f = Proof o f o the_proof;
wenzelm@18731
   521
wenzelm@26486
   522
fun map_theory_result f = apsnd Theory o f o the_theory;
wenzelm@26486
   523
fun map_proof_result f = apsnd Proof o f o the_proof;
wenzelm@26486
   524
wenzelm@18731
   525
fun theory_map f = the_theory o f o Theory;
wenzelm@18731
   526
fun proof_map f = the_proof o f o Proof;
wenzelm@18665
   527
wenzelm@33031
   528
val theory_of = cases I ProofContext.theory_of;
wenzelm@33031
   529
val proof_of = cases ProofContext.init I;
wenzelm@16533
   530
wenzelm@22085
   531
wenzelm@22085
   532
wenzelm@26413
   533
(** thread data **)
wenzelm@26413
   534
wenzelm@26413
   535
local val tag = Universal.tag () : generic option Universal.tag in
wenzelm@26413
   536
wenzelm@26413
   537
fun thread_data () =
wenzelm@28122
   538
  (case Thread.getLocal tag of
wenzelm@26413
   539
    SOME (SOME context) => SOME context
wenzelm@26413
   540
  | _ => NONE);
wenzelm@26413
   541
wenzelm@26413
   542
fun the_thread_data () =
wenzelm@26413
   543
  (case thread_data () of
wenzelm@26413
   544
    SOME context => context
wenzelm@26413
   545
  | _ => error "Unknown context");
wenzelm@26413
   546
wenzelm@28122
   547
fun set_thread_data context = Thread.setLocal (tag, context);
wenzelm@26413
   548
fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
wenzelm@26413
   549
wenzelm@26428
   550
end;
wenzelm@26428
   551
wenzelm@26428
   552
fun >>> f =
wenzelm@26428
   553
  let
wenzelm@26463
   554
    val (res, context') = f (the_thread_data ());
wenzelm@26463
   555
    val _ = set_thread_data (SOME context');
wenzelm@26428
   556
  in res end;
wenzelm@26428
   557
wenzelm@26421
   558
nonfix >>;
wenzelm@26463
   559
fun >> f = >>> (fn context => ((), f context));
wenzelm@26413
   560
wenzelm@26428
   561
val _ = set_thread_data (SOME (Theory pre_pure_thy));
wenzelm@26413
   562
wenzelm@6185
   563
end;
wenzelm@6185
   564
wenzelm@33031
   565
structure Basic_Context: BASIC_CONTEXT = Context;
wenzelm@33031
   566
open Basic_Context;
wenzelm@16436
   567
wenzelm@16436
   568
wenzelm@16436
   569
wenzelm@16533
   570
(*** type-safe interfaces for data declarations ***)
wenzelm@16533
   571
wenzelm@16533
   572
(** theory data **)
wenzelm@16436
   573
wenzelm@16436
   574
signature THEORY_DATA_ARGS =
wenzelm@16436
   575
sig
wenzelm@16436
   576
  type T
wenzelm@16436
   577
  val empty: T
wenzelm@16436
   578
  val copy: T -> T
wenzelm@16436
   579
  val extend: T -> T
wenzelm@16436
   580
  val merge: Pretty.pp -> T * T -> T
wenzelm@16436
   581
end;
wenzelm@16436
   582
wenzelm@16436
   583
signature THEORY_DATA =
wenzelm@16436
   584
sig
wenzelm@16436
   585
  type T
wenzelm@16436
   586
  val get: theory -> T
wenzelm@16436
   587
  val put: T -> theory -> theory
wenzelm@16436
   588
  val map: (T -> T) -> theory -> theory
wenzelm@22847
   589
  val init: theory -> theory
wenzelm@16436
   590
end;
wenzelm@16436
   591
wenzelm@16436
   592
functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
wenzelm@16436
   593
struct
wenzelm@16436
   594
wenzelm@16436
   595
structure TheoryData = Context.TheoryData;
wenzelm@16436
   596
wenzelm@16436
   597
type T = Data.T;
wenzelm@16436
   598
exception Data of T;
wenzelm@16436
   599
wenzelm@22847
   600
val kind = TheoryData.declare
wenzelm@16436
   601
  (Data Data.empty)
wenzelm@16436
   602
  (fn Data x => Data (Data.copy x))
wenzelm@16436
   603
  (fn Data x => Data (Data.extend x))
wenzelm@16489
   604
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
wenzelm@16436
   605
wenzelm@16436
   606
val get = TheoryData.get kind (fn Data x => x);
wenzelm@16436
   607
val put = TheoryData.put kind Data;
wenzelm@16436
   608
fun map f thy = put (f (get thy)) thy;
wenzelm@16436
   609
wenzelm@22847
   610
fun init thy = map I thy;
wenzelm@22847
   611
wenzelm@16436
   612
end;
wenzelm@16436
   613
wenzelm@16533
   614
wenzelm@16533
   615
wenzelm@16533
   616
(** proof data **)
wenzelm@16533
   617
wenzelm@16533
   618
signature PROOF_DATA_ARGS =
wenzelm@16533
   619
sig
wenzelm@16533
   620
  type T
wenzelm@16533
   621
  val init: theory -> T
wenzelm@16533
   622
end;
wenzelm@16533
   623
wenzelm@16533
   624
signature PROOF_DATA =
wenzelm@16533
   625
sig
wenzelm@16533
   626
  type T
wenzelm@33031
   627
  val get: Proof.context -> T
wenzelm@33031
   628
  val put: T -> Proof.context -> Proof.context
wenzelm@33031
   629
  val map: (T -> T) -> Proof.context -> Proof.context
wenzelm@16533
   630
end;
wenzelm@16533
   631
wenzelm@16533
   632
functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
wenzelm@16533
   633
struct
wenzelm@16533
   634
wenzelm@16533
   635
structure ProofData = Context.ProofData;
wenzelm@16533
   636
wenzelm@16533
   637
type T = Data.T;
wenzelm@16533
   638
exception Data of T;
wenzelm@16533
   639
wenzelm@22847
   640
val kind = ProofData.declare (Data o Data.init);
wenzelm@16533
   641
wenzelm@16533
   642
val get = ProofData.get kind (fn Data x => x);
wenzelm@16533
   643
val put = ProofData.put kind Data;
wenzelm@16533
   644
fun map f prf = put (f (get prf)) prf;
wenzelm@16533
   645
wenzelm@16533
   646
end;
wenzelm@16533
   647
wenzelm@18632
   648
wenzelm@18632
   649
wenzelm@18632
   650
(** generic data **)
wenzelm@18632
   651
wenzelm@18632
   652
signature GENERIC_DATA_ARGS =
wenzelm@18632
   653
sig
wenzelm@18632
   654
  type T
wenzelm@18632
   655
  val empty: T
wenzelm@18632
   656
  val extend: T -> T
wenzelm@18632
   657
  val merge: Pretty.pp -> T * T -> T
wenzelm@18632
   658
end;
wenzelm@18632
   659
wenzelm@18632
   660
signature GENERIC_DATA =
wenzelm@18632
   661
sig
wenzelm@18632
   662
  type T
wenzelm@18632
   663
  val get: Context.generic -> T
wenzelm@18632
   664
  val put: T -> Context.generic -> Context.generic
wenzelm@18632
   665
  val map: (T -> T) -> Context.generic -> Context.generic
wenzelm@18632
   666
end;
wenzelm@18632
   667
wenzelm@18632
   668
functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
wenzelm@18632
   669
struct
wenzelm@18632
   670
wenzelm@22847
   671
structure ThyData = TheoryDataFun(open Data val copy = I);
wenzelm@22847
   672
structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
wenzelm@18632
   673
wenzelm@18632
   674
type T = Data.T;
wenzelm@18632
   675
wenzelm@18632
   676
fun get (Context.Theory thy) = ThyData.get thy
wenzelm@18632
   677
  | get (Context.Proof prf) = PrfData.get prf;
wenzelm@18632
   678
wenzelm@18632
   679
fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
wenzelm@18632
   680
  | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
wenzelm@18632
   681
wenzelm@18632
   682
fun map f ctxt = put (f (get ctxt)) ctxt;
wenzelm@18632
   683
wenzelm@18632
   684
end;
wenzelm@18632
   685
wenzelm@16533
   686
(*hide private interface*)
wenzelm@16436
   687
structure Context: CONTEXT = Context;
wenzelm@20297
   688