src/Pure/context.ML
author wenzelm
Thu Jul 05 20:01:32 2007 +0200 (2007-07-05)
changeset 23595 7ca68a2c8575
parent 23355 d2c033fd4514
child 23944 2ea068548a83
permissions -rw-r--r--
the_theory/proof: error instead of exception Fail;
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@22847
     6
development graph and history support.  Generic proof contexts with
wenzelm@22095
     7
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@16719
    36
  val check_thy: theory -> theory
wenzelm@16436
    37
  val eq_thy: theory * theory -> bool
wenzelm@22603
    38
  val thy_ord: theory * theory -> order
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 self_ref: theory -> theory_ref
wenzelm@16436
    44
  val deref: theory_ref -> theory
wenzelm@16436
    45
  val copy_thy: theory -> theory
wenzelm@16436
    46
  val checkpoint_thy: theory -> theory
wenzelm@16489
    47
  val finish_thy: theory -> theory
wenzelm@16489
    48
  val pre_pure_thy: theory
wenzelm@16489
    49
  val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
wenzelm@16533
    50
  (*proof context*)
wenzelm@16533
    51
  type proof
wenzelm@16533
    52
  val theory_of_proof: proof -> theory
wenzelm@17060
    53
  val transfer_proof: theory -> proof -> proof
wenzelm@16533
    54
  val init_proof: theory -> proof
wenzelm@16533
    55
  (*generic context*)
wenzelm@18632
    56
  datatype generic = Theory of theory | Proof of proof
wenzelm@18632
    57
  val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
wenzelm@19678
    58
  val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
wenzelm@21660
    59
  val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
wenzelm@18632
    60
  val the_theory: generic -> theory
wenzelm@18632
    61
  val the_proof: generic -> proof
wenzelm@18731
    62
  val map_theory: (theory -> theory) -> generic -> generic
wenzelm@18731
    63
  val map_proof: (proof -> proof) -> generic -> generic
wenzelm@18731
    64
  val theory_map: (generic -> generic) -> theory -> theory
wenzelm@18731
    65
  val proof_map: (generic -> generic) -> proof -> proof
wenzelm@18665
    66
  val theory_of: generic -> theory   (*total*)
wenzelm@18665
    67
  val proof_of: generic -> proof     (*total*)
wenzelm@22095
    68
  (*delayed setup*)
wenzelm@22085
    69
  val add_setup: (theory -> theory) -> unit
wenzelm@22085
    70
  val setup: unit -> theory -> theory
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@19028
   129
    val _ = 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 ProtoPureN = "ProtoPure";
wenzelm@16436
   198
val PureN = "Pure";
wenzelm@16436
   199
val CPureN = "CPure";
wenzelm@16436
   200
wenzelm@16436
   201
val draftN = "#";
wenzelm@16436
   202
fun draft_id (_, name) = (name = draftN);
wenzelm@16436
   203
val is_draft = draft_id o #id o identity_of;
wenzelm@16436
   204
wenzelm@20821
   205
fun exists_name name (thy as Theory ({id, ids, iids, ...}, _, _, _)) =
wenzelm@20821
   206
  name = theory_name thy orelse
wenzelm@16489
   207
  name = #2 id orelse
wenzelm@16489
   208
  Inttab.exists (equal name o #2) ids orelse
wenzelm@16489
   209
  Inttab.exists (equal name o #2) iids;
wenzelm@16436
   210
wenzelm@16489
   211
fun names_of (Theory ({id, ids, iids, ...}, _, _, _)) =
wenzelm@16489
   212
  rev (#2 id :: Inttab.fold (cons o #2) iids (Inttab.fold (cons o #2) ids []));
wenzelm@16436
   213
wenzelm@16436
   214
fun pretty_thy thy =
wenzelm@16436
   215
  Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
wenzelm@16436
   216
wenzelm@16436
   217
val string_of_thy = Pretty.string_of o pretty_thy;
wenzelm@16436
   218
val pprint_thy = Pretty.pprint o pretty_thy;
wenzelm@16436
   219
wenzelm@16436
   220
fun pretty_abbrev_thy thy =
wenzelm@16436
   221
  let
wenzelm@16436
   222
    val names = names_of thy;
wenzelm@16436
   223
    val n = length names;
wenzelm@16436
   224
    val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
wenzelm@16436
   225
  in Pretty.str_list "{" "}" abbrev end;
wenzelm@16436
   226
wenzelm@16436
   227
val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
wenzelm@16436
   228
wenzelm@16436
   229
wenzelm@23355
   230
(* consistency *)
wenzelm@16436
   231
wenzelm@16719
   232
fun check_thy thy =
wenzelm@23355
   233
  if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
wenzelm@16436
   234
  else thy;
wenzelm@16436
   235
wenzelm@16489
   236
fun check_ins id ids =
wenzelm@16894
   237
  if draft_id id orelse Inttab.defined ids (#1 id) then ids
wenzelm@16436
   238
  else if Inttab.exists (equal (#2 id) o #2) ids then
wenzelm@23355
   239
    error ("Different versions of theory component " ^ quote (#2 id))
wenzelm@17412
   240
  else Inttab.update id ids;
wenzelm@16436
   241
wenzelm@16489
   242
fun check_insert intermediate id (ids, iids) =
wenzelm@16489
   243
  let val ids' = check_ins id ids and iids' = check_ins id iids
wenzelm@16489
   244
  in if intermediate then (ids, iids') else (ids', iids) end;
wenzelm@16489
   245
wenzelm@16489
   246
fun check_merge
wenzelm@16489
   247
    (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
wenzelm@16489
   248
    (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
wenzelm@16489
   249
  (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
wenzelm@16489
   250
  |> check_insert (#version history1 > 0) id1
wenzelm@16489
   251
  |> check_insert (#version history2 > 0) id2;
wenzelm@16436
   252
wenzelm@16436
   253
wenzelm@16533
   254
(* equality and inclusion *)
wenzelm@16533
   255
wenzelm@16719
   256
val eq_thy = eq_id o pairself (#id o identity_of o check_thy);
wenzelm@22603
   257
val thy_ord = int_ord o pairself (#1 o #id o identity_of);
wenzelm@16533
   258
wenzelm@16533
   259
fun proper_subthy
wenzelm@16719
   260
    (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
wenzelm@16894
   261
  Inttab.defined ids i orelse Inttab.defined iids i;
wenzelm@16533
   262
wenzelm@16533
   263
fun subthy thys = eq_thy thys orelse proper_subthy thys;
wenzelm@16533
   264
wenzelm@16594
   265
fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
wenzelm@16594
   266
wenzelm@16533
   267
wenzelm@16489
   268
(* theory references *)
wenzelm@16489
   269
wenzelm@16489
   270
(*theory_ref provides a safe way to store dynamic references to a
wenzelm@16533
   271
  theory in external data structures -- a plain theory value would
wenzelm@16533
   272
  become stale as the self reference moves on*)
wenzelm@16489
   273
wenzelm@16489
   274
datatype theory_ref = TheoryRef of theory ref;
wenzelm@16489
   275
wenzelm@16719
   276
val self_ref = TheoryRef o the_self o check_thy;
wenzelm@16489
   277
fun deref (TheoryRef (ref thy)) = thy;
wenzelm@16489
   278
wenzelm@16489
   279
wenzelm@23355
   280
(* trivial merge *)
wenzelm@16436
   281
wenzelm@16436
   282
fun merge (thy1, thy2) =
wenzelm@16719
   283
  if eq_thy (thy1, thy2) then thy1
wenzelm@16719
   284
  else if proper_subthy (thy2, thy1) then thy1
wenzelm@16719
   285
  else if proper_subthy (thy1, thy2) then thy2
wenzelm@16436
   286
  else (check_merge thy1 thy2;
wenzelm@23355
   287
    error (cat_lines ["Attempt to perform non-trivial merge of theories:",
wenzelm@23355
   288
      str_of_thy thy1, str_of_thy thy2]));
wenzelm@16436
   289
wenzelm@16719
   290
fun merge_refs (ref1, ref2) =
wenzelm@16719
   291
  if ref1 = ref2 then ref1
wenzelm@16719
   292
  else self_ref (merge (deref ref1, deref ref2));
wenzelm@16436
   293
wenzelm@16436
   294
wenzelm@16489
   295
wenzelm@16489
   296
(** build theories **)
wenzelm@16489
   297
wenzelm@16489
   298
(* primitives *)
wenzelm@16489
   299
wenzelm@16489
   300
fun create_thy name self id ids iids data ancestry history =
wenzelm@16489
   301
  let
wenzelm@17756
   302
    val {version, name = _, intermediates = _} = history;
wenzelm@17756
   303
    val intermediate = version > 0;
wenzelm@16489
   304
    val (ids', iids') = check_insert intermediate id (ids, iids);
wenzelm@16489
   305
    val id' = (serial (), name);
wenzelm@16489
   306
    val _ = check_insert intermediate id' (ids', iids');
wenzelm@16489
   307
    val identity' = make_identity self id' ids' iids';
wenzelm@16489
   308
  in vitalize (Theory (identity', data, ancestry, history)) end;
wenzelm@16436
   309
wenzelm@16489
   310
fun change_thy name f (thy as Theory ({self, id, ids, iids}, data, ancestry, history)) =
wenzelm@16489
   311
  let
wenzelm@16719
   312
    val _ = check_thy thy;
wenzelm@16489
   313
    val (self', data', ancestry') =
wenzelm@16489
   314
      if is_draft thy then (self, data, ancestry)    (*destructive change!*)
wenzelm@16489
   315
      else if #version history > 0
wenzelm@22847
   316
      then (NONE, copy_data data, ancestry)
wenzelm@22847
   317
      else (NONE, extend_data data,
wenzelm@18731
   318
        make_ancestry [thy] (thy :: #ancestors ancestry));
wenzelm@16489
   319
    val data'' = f data';
wenzelm@16489
   320
  in create_thy name self' id ids iids data'' ancestry' history 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@16489
   326
fun copy_thy (thy as Theory ({id, ids, iids, ...}, data, ancestry, history)) =
wenzelm@22847
   327
  (check_thy thy; create_thy draftN NONE id ids iids (copy_data data) ancestry history);
wenzelm@16489
   328
wenzelm@16489
   329
val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
wenzelm@22847
   330
  Datatab.empty (make_ancestry [] []) (make_history ProtoPureN 0 []);
wenzelm@16489
   331
wenzelm@16489
   332
wenzelm@16489
   333
(* named theory nodes *)
wenzelm@16489
   334
wenzelm@16489
   335
fun merge_thys pp (thy1, thy2) =
wenzelm@16533
   336
  if exists_name CPureN thy1 <> exists_name CPureN thy2 then
wenzelm@16436
   337
    error "Cannot merge Pure and CPure developments"
wenzelm@16436
   338
  else
wenzelm@16436
   339
    let
wenzelm@16489
   340
      val (ids, iids) = check_merge thy1 thy2;
wenzelm@22847
   341
      val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
wenzelm@16489
   342
      val ancestry = make_ancestry [] [];
wenzelm@16436
   343
      val history = make_history "" 0 [];
wenzelm@16489
   344
    in create_thy draftN NONE (serial (), draftN) ids iids data ancestry history end;
wenzelm@16489
   345
wenzelm@16533
   346
fun maximal_thys thys =
wenzelm@16533
   347
  thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
wenzelm@16533
   348
wenzelm@16489
   349
fun begin_thy pp name imports =
wenzelm@16489
   350
  if name = draftN then error ("Illegal theory name: " ^ quote draftN)
wenzelm@16489
   351
  else
wenzelm@16489
   352
    let
wenzelm@16533
   353
      val parents =
wenzelm@19046
   354
        maximal_thys (distinct eq_thy (map check_thy imports));
wenzelm@22603
   355
      val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
wenzelm@16489
   356
      val Theory ({id, ids, iids, ...}, data, _, _) =
wenzelm@16489
   357
        (case parents of
wenzelm@16489
   358
          [] => error "No parent theories"
wenzelm@16533
   359
        | [thy] => extend_thy thy
wenzelm@16533
   360
        | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
wenzelm@16489
   361
      val ancestry = make_ancestry parents ancestors;
wenzelm@16489
   362
      val history = make_history name 0 [];
wenzelm@16489
   363
    in create_thy draftN NONE id ids iids data ancestry history end;
wenzelm@16436
   364
wenzelm@16436
   365
wenzelm@16489
   366
(* undoable checkpoints *)
wenzelm@16436
   367
wenzelm@16489
   368
fun checkpoint_thy thy =
wenzelm@16489
   369
  if not (is_draft thy) then thy
wenzelm@16436
   370
  else
wenzelm@16436
   371
    let
wenzelm@16489
   372
      val {name, version, intermediates} = history_of thy;
wenzelm@16489
   373
      val thy' as Theory (identity', data', ancestry', _) =
wenzelm@16489
   374
        name_thy (name ^ ":" ^ string_of_int version) thy;
wenzelm@16489
   375
      val history' = make_history name (version + 1) (thy' :: intermediates);
wenzelm@16489
   376
    in vitalize (Theory (identity', data', ancestry', history')) end;
wenzelm@16489
   377
wenzelm@16489
   378
fun finish_thy thy =
wenzelm@16489
   379
  let
wenzelm@16489
   380
    val {name, version, intermediates} = history_of thy;
wenzelm@16719
   381
    val rs = map (the_self o check_thy) intermediates;
wenzelm@16489
   382
    val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
wenzelm@16489
   383
    val identity' = make_identity self id ids Inttab.empty;
wenzelm@16489
   384
    val history' = make_history name 0 [];
wenzelm@16489
   385
    val thy'' = vitalize (Theory (identity', data', ancestry', history'));
wenzelm@16533
   386
    val _ = List.app (fn r => r := thy'') rs;
wenzelm@16533
   387
  in thy'' end;
wenzelm@16489
   388
wenzelm@16489
   389
wenzelm@16489
   390
(* theory data *)
wenzelm@16436
   391
wenzelm@16489
   392
structure TheoryData =
wenzelm@16489
   393
struct
wenzelm@16489
   394
wenzelm@16489
   395
val declare = declare_theory_data;
wenzelm@16489
   396
wenzelm@16489
   397
fun get k dest thy =
wenzelm@22847
   398
  dest ((case Datatab.lookup (data_of thy) k of
wenzelm@22847
   399
    SOME x => x
wenzelm@22847
   400
  | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
wenzelm@16489
   401
wenzelm@22847
   402
fun put k mk x = modify_thy (Datatab.update (k, mk x));
wenzelm@16489
   403
wenzelm@16489
   404
end;
wenzelm@16436
   405
wenzelm@16436
   406
wenzelm@16436
   407
wenzelm@16533
   408
(*** proof context ***)
wenzelm@16533
   409
wenzelm@16533
   410
(* datatype proof *)
wenzelm@16533
   411
wenzelm@19028
   412
datatype proof = Proof of theory_ref * Object.T Datatab.table;
wenzelm@17060
   413
wenzelm@17060
   414
fun theory_of_proof (Proof (thy_ref, _)) = deref thy_ref;
wenzelm@16533
   415
fun data_of_proof (Proof (_, data)) = data;
wenzelm@17060
   416
fun map_prf f (Proof (thy_ref, data)) = Proof (thy_ref, f data);
wenzelm@17060
   417
wenzelm@16533
   418
wenzelm@16533
   419
(* proof data kinds *)
wenzelm@16533
   420
wenzelm@16533
   421
local
wenzelm@16533
   422
wenzelm@22847
   423
val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
wenzelm@16533
   424
wenzelm@22847
   425
fun invoke_init k =
wenzelm@19028
   426
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   427
    SOME init => init
wenzelm@22847
   428
  | NONE => sys_error "Invalid proof data identifier");
wenzelm@16533
   429
wenzelm@22847
   430
fun init_data thy =
wenzelm@22847
   431
  Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
wenzelm@22847
   432
wenzelm@22847
   433
fun init_new_data data thy =
wenzelm@22847
   434
  Datatab.merge (K true) (data, init_data thy);
wenzelm@16533
   435
wenzelm@16533
   436
in
wenzelm@16533
   437
wenzelm@22847
   438
fun init_proof thy = Proof (self_ref thy, init_data thy);
wenzelm@16533
   439
wenzelm@22847
   440
fun transfer_proof thy' (prf as Proof (thy_ref, data)) =
wenzelm@22847
   441
  if not (subthy (deref thy_ref, thy')) then
wenzelm@22847
   442
    error "transfer proof context: not a super theory"
wenzelm@22847
   443
  else Proof (self_ref thy', init_new_data data thy');
wenzelm@22847
   444
wenzelm@16533
   445
wenzelm@16533
   446
structure ProofData =
wenzelm@16533
   447
struct
wenzelm@16533
   448
wenzelm@22847
   449
fun declare init =
wenzelm@16533
   450
  let
wenzelm@16533
   451
    val k = serial ();
wenzelm@22847
   452
    val _ = change kinds (Datatab.update (k, init));
wenzelm@16533
   453
  in k end;
wenzelm@16533
   454
wenzelm@16533
   455
fun get k dest prf =
wenzelm@22847
   456
  dest (case Datatab.lookup (data_of_proof prf) k of
wenzelm@22847
   457
    SOME x => x
wenzelm@22847
   458
  | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
wenzelm@16533
   459
wenzelm@19028
   460
fun put k mk x = map_prf (Datatab.update (k, mk x));
wenzelm@16533
   461
wenzelm@16533
   462
end;
wenzelm@16533
   463
wenzelm@16533
   464
end;
wenzelm@16533
   465
wenzelm@16533
   466
wenzelm@18632
   467
wenzelm@16533
   468
(*** generic context ***)
wenzelm@16533
   469
wenzelm@18632
   470
datatype generic = Theory of theory | Proof of proof;
wenzelm@18632
   471
wenzelm@18632
   472
fun cases f _ (Theory thy) = f thy
wenzelm@18632
   473
  | cases _ g (Proof prf) = g prf;
wenzelm@16533
   474
wenzelm@19678
   475
fun mapping f g = cases (Theory o f) (Proof o g);
wenzelm@21660
   476
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
wenzelm@19678
   477
wenzelm@23595
   478
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
wenzelm@23595
   479
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
wenzelm@16533
   480
wenzelm@18731
   481
fun map_theory f = Theory o f o the_theory;
wenzelm@18731
   482
fun map_proof f = Proof o f o the_proof;
wenzelm@18731
   483
wenzelm@18731
   484
fun theory_map f = the_theory o f o Theory;
wenzelm@18731
   485
fun proof_map f = the_proof o f o Proof;
wenzelm@18665
   486
wenzelm@18632
   487
val theory_of = cases I theory_of_proof;
wenzelm@18632
   488
val proof_of = cases init_proof I;
wenzelm@16533
   489
wenzelm@22085
   490
wenzelm@22085
   491
wenzelm@22095
   492
(** delayed theory setup **)
wenzelm@22085
   493
wenzelm@22085
   494
local
wenzelm@22085
   495
  val setup_fn = ref (I: theory -> theory);
wenzelm@22085
   496
in
wenzelm@22085
   497
  fun add_setup f = setup_fn := (! setup_fn #> f);
wenzelm@22085
   498
  fun setup () = let val f = ! setup_fn in setup_fn := I; f end;
wenzelm@22085
   499
end;
wenzelm@22085
   500
wenzelm@6185
   501
end;
wenzelm@6185
   502
wenzelm@6185
   503
structure BasicContext: BASIC_CONTEXT = Context;
wenzelm@6185
   504
open BasicContext;
wenzelm@16436
   505
wenzelm@16436
   506
wenzelm@16436
   507
wenzelm@16533
   508
(*** type-safe interfaces for data declarations ***)
wenzelm@16533
   509
wenzelm@16533
   510
(** theory data **)
wenzelm@16436
   511
wenzelm@16436
   512
signature THEORY_DATA_ARGS =
wenzelm@16436
   513
sig
wenzelm@16436
   514
  type T
wenzelm@16436
   515
  val empty: T
wenzelm@16436
   516
  val copy: T -> T
wenzelm@16436
   517
  val extend: T -> T
wenzelm@16436
   518
  val merge: Pretty.pp -> T * T -> T
wenzelm@16436
   519
end;
wenzelm@16436
   520
wenzelm@16436
   521
signature THEORY_DATA =
wenzelm@16436
   522
sig
wenzelm@16436
   523
  type T
wenzelm@16436
   524
  val get: theory -> T
wenzelm@16436
   525
  val put: T -> theory -> theory
wenzelm@16436
   526
  val map: (T -> T) -> theory -> theory
wenzelm@22847
   527
  val init: theory -> theory
wenzelm@16436
   528
end;
wenzelm@16436
   529
wenzelm@16436
   530
functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
wenzelm@16436
   531
struct
wenzelm@16436
   532
wenzelm@16436
   533
structure TheoryData = Context.TheoryData;
wenzelm@16436
   534
wenzelm@16436
   535
type T = Data.T;
wenzelm@16436
   536
exception Data of T;
wenzelm@16436
   537
wenzelm@22847
   538
val kind = TheoryData.declare
wenzelm@16436
   539
  (Data Data.empty)
wenzelm@16436
   540
  (fn Data x => Data (Data.copy x))
wenzelm@16436
   541
  (fn Data x => Data (Data.extend x))
wenzelm@16489
   542
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
wenzelm@16436
   543
wenzelm@16436
   544
val get = TheoryData.get kind (fn Data x => x);
wenzelm@16436
   545
val put = TheoryData.put kind Data;
wenzelm@16436
   546
fun map f thy = put (f (get thy)) thy;
wenzelm@16436
   547
wenzelm@22847
   548
fun init thy = map I thy;
wenzelm@22847
   549
wenzelm@16436
   550
end;
wenzelm@16436
   551
wenzelm@16533
   552
wenzelm@16533
   553
wenzelm@16533
   554
(** proof data **)
wenzelm@16533
   555
wenzelm@16533
   556
signature PROOF_DATA_ARGS =
wenzelm@16533
   557
sig
wenzelm@16533
   558
  type T
wenzelm@16533
   559
  val init: theory -> T
wenzelm@16533
   560
end;
wenzelm@16533
   561
wenzelm@16533
   562
signature PROOF_DATA =
wenzelm@16533
   563
sig
wenzelm@16533
   564
  type T
wenzelm@16533
   565
  val get: Context.proof -> T
wenzelm@16533
   566
  val put: T -> Context.proof -> Context.proof
wenzelm@16533
   567
  val map: (T -> T) -> Context.proof -> Context.proof
wenzelm@16533
   568
end;
wenzelm@16533
   569
wenzelm@16533
   570
functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
wenzelm@16533
   571
struct
wenzelm@16533
   572
wenzelm@16533
   573
structure ProofData = Context.ProofData;
wenzelm@16533
   574
wenzelm@16533
   575
type T = Data.T;
wenzelm@16533
   576
exception Data of T;
wenzelm@16533
   577
wenzelm@22847
   578
val kind = ProofData.declare (Data o Data.init);
wenzelm@16533
   579
wenzelm@16533
   580
val get = ProofData.get kind (fn Data x => x);
wenzelm@16533
   581
val put = ProofData.put kind Data;
wenzelm@16533
   582
fun map f prf = put (f (get prf)) prf;
wenzelm@16533
   583
wenzelm@16533
   584
end;
wenzelm@16533
   585
wenzelm@18632
   586
wenzelm@18632
   587
wenzelm@18632
   588
(** generic data **)
wenzelm@18632
   589
wenzelm@18632
   590
signature GENERIC_DATA_ARGS =
wenzelm@18632
   591
sig
wenzelm@18632
   592
  type T
wenzelm@18632
   593
  val empty: T
wenzelm@18632
   594
  val extend: T -> T
wenzelm@18632
   595
  val merge: Pretty.pp -> T * T -> T
wenzelm@18632
   596
end;
wenzelm@18632
   597
wenzelm@18632
   598
signature GENERIC_DATA =
wenzelm@18632
   599
sig
wenzelm@18632
   600
  type T
wenzelm@18632
   601
  val get: Context.generic -> T
wenzelm@18632
   602
  val put: T -> Context.generic -> Context.generic
wenzelm@18632
   603
  val map: (T -> T) -> Context.generic -> Context.generic
wenzelm@18632
   604
end;
wenzelm@18632
   605
wenzelm@18632
   606
functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
wenzelm@18632
   607
struct
wenzelm@18632
   608
wenzelm@22847
   609
structure ThyData = TheoryDataFun(open Data val copy = I);
wenzelm@22847
   610
structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
wenzelm@18632
   611
wenzelm@18632
   612
type T = Data.T;
wenzelm@18632
   613
wenzelm@18632
   614
fun get (Context.Theory thy) = ThyData.get thy
wenzelm@18632
   615
  | get (Context.Proof prf) = PrfData.get prf;
wenzelm@18632
   616
wenzelm@18632
   617
fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
wenzelm@18632
   618
  | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
wenzelm@18632
   619
wenzelm@18632
   620
fun map f ctxt = put (f (get ctxt)) ctxt;
wenzelm@18632
   621
wenzelm@18632
   622
end;
wenzelm@18632
   623
wenzelm@16533
   624
(*hide private interface*)
wenzelm@16436
   625
structure Context: CONTEXT = Context;
wenzelm@20297
   626
wenzelm@21518
   627
(*fake predeclarations*)
wenzelm@20297
   628
structure Proof = struct type context = Context.proof end;
wenzelm@21518
   629
structure ProofContext =
wenzelm@21518
   630
struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;