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