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