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