src/Pure/context.ML
author wenzelm
Thu Aug 30 15:04:42 2007 +0200 (2007-08-30 ago)
changeset 24484 013b98b57b86
parent 24369 0cb1f4d76452
child 24559 dae0972c0066
permissions -rw-r--r--
maintain mode in context (get/set/restore_mode);
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@22603
    39
  val thy_ord: theory * theory -> order
wenzelm@16436
    40
  val subthy: theory * theory -> bool
wenzelm@16594
    41
  val joinable: theory * theory -> bool
wenzelm@23355
    42
  val merge: theory * theory -> theory
wenzelm@23355
    43
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@16436
    44
  val copy_thy: theory -> theory
wenzelm@16436
    45
  val checkpoint_thy: theory -> theory
wenzelm@16489
    46
  val finish_thy: theory -> theory
wenzelm@16489
    47
  val pre_pure_thy: theory
wenzelm@16489
    48
  val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
wenzelm@16533
    49
  (*proof context*)
wenzelm@16533
    50
  type proof
wenzelm@16533
    51
  val theory_of_proof: proof -> theory
wenzelm@17060
    52
  val transfer_proof: theory -> proof -> proof
wenzelm@16533
    53
  val init_proof: theory -> proof
wenzelm@16533
    54
  (*generic context*)
wenzelm@18632
    55
  datatype generic = Theory of theory | Proof of proof
wenzelm@18632
    56
  val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
wenzelm@19678
    57
  val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
wenzelm@21660
    58
  val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
wenzelm@18632
    59
  val the_theory: generic -> theory
wenzelm@18632
    60
  val the_proof: generic -> proof
wenzelm@18731
    61
  val map_theory: (theory -> theory) -> generic -> generic
wenzelm@18731
    62
  val map_proof: (proof -> proof) -> generic -> generic
wenzelm@18731
    63
  val theory_map: (generic -> generic) -> theory -> theory
wenzelm@18731
    64
  val proof_map: (generic -> generic) -> proof -> proof
wenzelm@18665
    65
  val theory_of: generic -> theory   (*total*)
wenzelm@18665
    66
  val proof_of: generic -> proof     (*total*)
wenzelm@22095
    67
  (*delayed setup*)
wenzelm@22085
    68
  val add_setup: (theory -> theory) -> unit
wenzelm@22085
    69
  val setup: unit -> theory -> theory
wenzelm@6185
    70
end;
wenzelm@6185
    71
wenzelm@16436
    72
signature PRIVATE_CONTEXT =
wenzelm@16436
    73
sig
wenzelm@16436
    74
  include CONTEXT
wenzelm@16436
    75
  structure TheoryData:
wenzelm@16436
    76
  sig
wenzelm@22847
    77
    val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
wenzelm@16489
    78
      (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
wenzelm@16436
    79
    val get: serial -> (Object.T -> 'a) -> theory -> 'a
wenzelm@16436
    80
    val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
wenzelm@16489
    81
  end
wenzelm@16533
    82
  structure ProofData:
wenzelm@16533
    83
  sig
wenzelm@22847
    84
    val declare: (theory -> Object.T) -> serial
wenzelm@16533
    85
    val get: serial -> (Object.T -> 'a) -> proof -> 'a
wenzelm@16533
    86
    val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
wenzelm@16533
    87
  end
wenzelm@16436
    88
end;
wenzelm@16436
    89
wenzelm@16436
    90
structure Context: PRIVATE_CONTEXT =
wenzelm@6185
    91
struct
wenzelm@6185
    92
wenzelm@16436
    93
(*** theory context ***)
wenzelm@6185
    94
wenzelm@16489
    95
(** theory data **)
wenzelm@16489
    96
wenzelm@16489
    97
(* data kinds and access methods *)
wenzelm@16489
    98
wenzelm@19028
    99
(*private copy avoids potential conflict of table exceptions*)
wenzelm@19028
   100
structure Datatab = TableFun(type key = int val ord = int_ord);
wenzelm@19028
   101
wenzelm@16489
   102
local
wenzelm@16489
   103
wenzelm@16489
   104
type kind =
wenzelm@22847
   105
 {empty: Object.T,
wenzelm@16489
   106
  copy: Object.T -> Object.T,
wenzelm@16489
   107
  extend: Object.T -> Object.T,
wenzelm@16489
   108
  merge: Pretty.pp -> Object.T * Object.T -> Object.T};
wenzelm@16489
   109
wenzelm@19028
   110
val kinds = ref (Datatab.empty: kind Datatab.table);
wenzelm@16489
   111
wenzelm@22847
   112
fun invoke f k =
wenzelm@19028
   113
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   114
    SOME kind => f kind
wenzelm@22847
   115
  | NONE => sys_error "Invalid theory data identifier");
wenzelm@16489
   116
wenzelm@16489
   117
in
wenzelm@16489
   118
wenzelm@22847
   119
fun invoke_empty k   = invoke (K o #empty) k ();
wenzelm@22847
   120
val invoke_copy      = invoke #copy;
wenzelm@22847
   121
val invoke_extend    = invoke #extend;
wenzelm@22847
   122
fun invoke_merge pp  = invoke (fn kind => #merge kind pp);
wenzelm@16489
   123
wenzelm@22847
   124
fun declare_theory_data empty copy extend merge =
wenzelm@16489
   125
  let
wenzelm@16489
   126
    val k = serial ();
wenzelm@22847
   127
    val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
wenzelm@23944
   128
    val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
wenzelm@16489
   129
  in k end;
wenzelm@16489
   130
wenzelm@19028
   131
val copy_data = Datatab.map' invoke_copy;
wenzelm@19028
   132
val extend_data = Datatab.map' invoke_extend;
wenzelm@19028
   133
fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
wenzelm@16489
   134
wenzelm@16489
   135
end;
wenzelm@16489
   136
wenzelm@16489
   137
wenzelm@16489
   138
wenzelm@16489
   139
(** datatype theory **)
wenzelm@16489
   140
wenzelm@16436
   141
datatype theory =
wenzelm@16436
   142
  Theory of
wenzelm@16533
   143
   (*identity*)
wenzelm@16489
   144
   {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
wenzelm@16489
   145
    id: serial * string,                (*identifier of this theory*)
wenzelm@16489
   146
    ids: string Inttab.table,           (*identifiers of ancestors*)
wenzelm@16489
   147
    iids: string Inttab.table} *        (*identifiers of intermediate checkpoints*)
wenzelm@16533
   148
   (*data*)
wenzelm@22847
   149
   Object.T Datatab.table *
wenzelm@16533
   150
   (*ancestry*)
wenzelm@16489
   151
   {parents: theory list,               (*immediate predecessors*)
wenzelm@16489
   152
    ancestors: theory list} *           (*all predecessors*)
wenzelm@16533
   153
   (*history*)
wenzelm@16489
   154
   {name: string,                       (*prospective name of finished theory*)
wenzelm@16489
   155
    version: int,                       (*checkpoint counter*)
wenzelm@16489
   156
    intermediates: theory list};        (*intermediate checkpoints*)
wenzelm@16436
   157
wenzelm@16436
   158
exception THEORY of string * theory list;
wenzelm@16436
   159
wenzelm@16436
   160
fun rep_theory (Theory args) = args;
wenzelm@16436
   161
wenzelm@16436
   162
val identity_of = #1 o rep_theory;
wenzelm@16436
   163
val data_of     = #2 o rep_theory;
wenzelm@16489
   164
val ancestry_of = #3 o rep_theory;
wenzelm@16489
   165
val history_of  = #4 o rep_theory;
wenzelm@16436
   166
wenzelm@16489
   167
fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
wenzelm@16489
   168
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
wenzelm@16436
   169
fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
wenzelm@16436
   170
wenzelm@16533
   171
val the_self = the o #self o identity_of;
wenzelm@16436
   172
val parents_of = #parents o ancestry_of;
wenzelm@16436
   173
val ancestors_of = #ancestors o ancestry_of;
wenzelm@16489
   174
val theory_name = #name o history_of;
wenzelm@16436
   175
wenzelm@16436
   176
wenzelm@16436
   177
(* staleness *)
wenzelm@16436
   178
wenzelm@16533
   179
fun eq_id ((i: int, _), (j, _)) = (i = j);
wenzelm@16436
   180
wenzelm@16436
   181
fun is_stale
wenzelm@16436
   182
    (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
wenzelm@16436
   183
      not (eq_id (id, id'))
wenzelm@16436
   184
  | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
wenzelm@16436
   185
wenzelm@16436
   186
fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
wenzelm@16489
   187
  | vitalize (thy as Theory ({self = NONE, id, ids, iids}, data, ancestry, history)) =
wenzelm@16436
   188
      let
wenzelm@16436
   189
        val r = ref thy;
wenzelm@16489
   190
        val thy' = Theory (make_identity (SOME r) id ids iids, data, ancestry, history);
wenzelm@16436
   191
      in r := thy'; thy' end;
wenzelm@16436
   192
wenzelm@16436
   193
wenzelm@16436
   194
(* names *)
wenzelm@16436
   195
wenzelm@16436
   196
val ProtoPureN = "ProtoPure";
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@22603
   269
val thy_ord = int_ord o pairself (#1 o #id o identity_of);
wenzelm@16533
   270
wenzelm@16533
   271
fun proper_subthy
wenzelm@16719
   272
    (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
wenzelm@16894
   273
  Inttab.defined ids i orelse Inttab.defined iids i;
wenzelm@16533
   274
wenzelm@16533
   275
fun subthy thys = eq_thy thys orelse proper_subthy thys;
wenzelm@16533
   276
wenzelm@16594
   277
fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
wenzelm@16594
   278
wenzelm@16533
   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@24141
   292
  else check_thy (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@24369
   310
fun change_thy name f thy =
wenzelm@16489
   311
  let
wenzelm@24141
   312
    val Theory ({self, id, ids, iids}, data, ancestry, history) = 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@24369
   317
      else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
wenzelm@16489
   318
    val data'' = f data';
wenzelm@24369
   319
    val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@24369
   320
      (check_thy thy; create_thy name self' id ids iids data'' ancestry' history));
wenzelm@24369
   321
  in thy' end;
wenzelm@16489
   322
wenzelm@16489
   323
fun name_thy name = change_thy name I;
wenzelm@16489
   324
val modify_thy = change_thy draftN;
wenzelm@16489
   325
val extend_thy = modify_thy I;
wenzelm@16489
   326
wenzelm@24369
   327
fun copy_thy thy =
wenzelm@24141
   328
  let
wenzelm@24141
   329
    val Theory ({id, ids, iids, ...}, data, ancestry, history) = thy;
wenzelm@24369
   330
    val data' = copy_data data;
wenzelm@24369
   331
    val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@24369
   332
      (check_thy thy; create_thy draftN NONE id ids iids data' ancestry history));
wenzelm@24369
   333
  in thy' end;
wenzelm@16489
   334
wenzelm@16489
   335
val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
wenzelm@22847
   336
  Datatab.empty (make_ancestry [] []) (make_history ProtoPureN 0 []);
wenzelm@16489
   337
wenzelm@16489
   338
wenzelm@16489
   339
(* named theory nodes *)
wenzelm@16489
   340
wenzelm@16489
   341
fun merge_thys pp (thy1, thy2) =
wenzelm@16533
   342
  if exists_name CPureN thy1 <> exists_name CPureN thy2 then
wenzelm@16436
   343
    error "Cannot merge Pure and CPure developments"
wenzelm@24369
   344
  else
wenzelm@16436
   345
    let
wenzelm@16489
   346
      val (ids, iids) = check_merge thy1 thy2;
wenzelm@22847
   347
      val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
wenzelm@16489
   348
      val ancestry = make_ancestry [] [];
wenzelm@16436
   349
      val history = make_history "" 0 [];
wenzelm@24369
   350
      val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@24369
   351
       (check_thy thy1; check_thy thy2;
wenzelm@24369
   352
        create_thy draftN NONE (serial (), draftN) ids iids data ancestry history))
wenzelm@24369
   353
    in thy' end;
wenzelm@16489
   354
wenzelm@16533
   355
fun maximal_thys thys =
wenzelm@16533
   356
  thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
wenzelm@16533
   357
wenzelm@16489
   358
fun begin_thy pp name imports =
wenzelm@16489
   359
  if name = draftN then error ("Illegal theory name: " ^ quote draftN)
wenzelm@24369
   360
  else
wenzelm@16489
   361
    let
wenzelm@24141
   362
      val parents = maximal_thys (distinct eq_thy imports);
wenzelm@22603
   363
      val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
wenzelm@16489
   364
      val Theory ({id, ids, iids, ...}, data, _, _) =
wenzelm@16489
   365
        (case parents of
wenzelm@16489
   366
          [] => error "No parent theories"
wenzelm@16533
   367
        | [thy] => extend_thy thy
wenzelm@16533
   368
        | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
wenzelm@16489
   369
      val ancestry = make_ancestry parents ancestors;
wenzelm@16489
   370
      val history = make_history name 0 [];
wenzelm@24369
   371
      val thy' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@24369
   372
        (map check_thy imports; create_thy draftN NONE id ids iids data ancestry history));
wenzelm@24369
   373
    in thy' end;
wenzelm@16436
   374
wenzelm@16436
   375
wenzelm@16489
   376
(* undoable checkpoints *)
wenzelm@16436
   377
wenzelm@16489
   378
fun checkpoint_thy thy =
wenzelm@16489
   379
  if not (is_draft thy) then thy
wenzelm@24369
   380
  else
wenzelm@16436
   381
    let
wenzelm@16489
   382
      val {name, version, intermediates} = history_of thy;
wenzelm@16489
   383
      val thy' as Theory (identity', data', ancestry', _) =
wenzelm@16489
   384
        name_thy (name ^ ":" ^ string_of_int version) thy;
wenzelm@16489
   385
      val history' = make_history name (version + 1) (thy' :: intermediates);
wenzelm@24369
   386
      val thy'' = NAMED_CRITICAL "theory" (fn () =>
wenzelm@24369
   387
        (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
wenzelm@24369
   388
    in thy'' end;
wenzelm@16489
   389
wenzelm@24141
   390
fun finish_thy thy = NAMED_CRITICAL "theory" (fn () =>
wenzelm@16489
   391
  let
wenzelm@16489
   392
    val {name, version, intermediates} = history_of thy;
wenzelm@24141
   393
    val rs = map ((fn TheoryRef r => r) o check_thy) intermediates;
wenzelm@16489
   394
    val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
wenzelm@16489
   395
    val identity' = make_identity self id ids Inttab.empty;
wenzelm@16489
   396
    val history' = make_history name 0 [];
wenzelm@16489
   397
    val thy'' = vitalize (Theory (identity', data', ancestry', history'));
wenzelm@16533
   398
    val _ = List.app (fn r => r := thy'') rs;
wenzelm@24141
   399
  in thy'' end);
wenzelm@16489
   400
wenzelm@16489
   401
wenzelm@16489
   402
(* theory data *)
wenzelm@16436
   403
wenzelm@16489
   404
structure TheoryData =
wenzelm@16489
   405
struct
wenzelm@16489
   406
wenzelm@16489
   407
val declare = declare_theory_data;
wenzelm@16489
   408
wenzelm@16489
   409
fun get k dest thy =
wenzelm@22847
   410
  dest ((case Datatab.lookup (data_of thy) k of
wenzelm@22847
   411
    SOME x => x
wenzelm@22847
   412
  | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
wenzelm@16489
   413
wenzelm@22847
   414
fun put k mk x = modify_thy (Datatab.update (k, mk x));
wenzelm@16489
   415
wenzelm@16489
   416
end;
wenzelm@16436
   417
wenzelm@16436
   418
wenzelm@16436
   419
wenzelm@16533
   420
(*** proof context ***)
wenzelm@16533
   421
wenzelm@16533
   422
(* datatype proof *)
wenzelm@16533
   423
wenzelm@24184
   424
datatype proof = Prf of Object.T Datatab.table * theory_ref;
wenzelm@17060
   425
wenzelm@24184
   426
fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
wenzelm@24184
   427
fun data_of_proof (Prf (data, _)) = data;
wenzelm@24184
   428
fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
wenzelm@17060
   429
wenzelm@16533
   430
wenzelm@16533
   431
(* proof data kinds *)
wenzelm@16533
   432
wenzelm@16533
   433
local
wenzelm@16533
   434
wenzelm@22847
   435
val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
wenzelm@16533
   436
wenzelm@22847
   437
fun invoke_init k =
wenzelm@19028
   438
  (case Datatab.lookup (! kinds) k of
wenzelm@22847
   439
    SOME init => init
wenzelm@22847
   440
  | NONE => sys_error "Invalid proof data identifier");
wenzelm@16533
   441
wenzelm@22847
   442
fun init_data thy =
wenzelm@22847
   443
  Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
wenzelm@22847
   444
wenzelm@22847
   445
fun init_new_data data thy =
wenzelm@22847
   446
  Datatab.merge (K true) (data, init_data thy);
wenzelm@16533
   447
wenzelm@16533
   448
in
wenzelm@16533
   449
wenzelm@24184
   450
fun init_proof thy = Prf (init_data thy, check_thy thy);
wenzelm@16533
   451
wenzelm@24184
   452
fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
wenzelm@24141
   453
  let
wenzelm@24141
   454
    val thy = deref thy_ref;
wenzelm@24141
   455
    val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
wenzelm@24141
   456
    val _ = check_thy thy;
wenzelm@24184
   457
    val data' = init_new_data data thy';
wenzelm@24141
   458
    val thy_ref' = check_thy thy';
wenzelm@24184
   459
  in Prf (data', thy_ref') end;
wenzelm@22847
   460
wenzelm@16533
   461
wenzelm@16533
   462
structure ProofData =
wenzelm@16533
   463
struct
wenzelm@16533
   464
wenzelm@22847
   465
fun declare init =
wenzelm@16533
   466
  let
wenzelm@16533
   467
    val k = serial ();
wenzelm@23944
   468
    val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
wenzelm@16533
   469
  in k end;
wenzelm@16533
   470
wenzelm@16533
   471
fun get k dest prf =
wenzelm@22847
   472
  dest (case Datatab.lookup (data_of_proof prf) k of
wenzelm@22847
   473
    SOME x => x
wenzelm@22847
   474
  | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
wenzelm@16533
   475
wenzelm@19028
   476
fun put k mk x = map_prf (Datatab.update (k, mk x));
wenzelm@16533
   477
wenzelm@16533
   478
end;
wenzelm@16533
   479
wenzelm@16533
   480
end;
wenzelm@16533
   481
wenzelm@16533
   482
wenzelm@18632
   483
wenzelm@16533
   484
(*** generic context ***)
wenzelm@16533
   485
wenzelm@18632
   486
datatype generic = Theory of theory | Proof of proof;
wenzelm@18632
   487
wenzelm@18632
   488
fun cases f _ (Theory thy) = f thy
wenzelm@18632
   489
  | cases _ g (Proof prf) = g prf;
wenzelm@16533
   490
wenzelm@19678
   491
fun mapping f g = cases (Theory o f) (Proof o g);
wenzelm@21660
   492
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
wenzelm@19678
   493
wenzelm@23595
   494
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
wenzelm@23595
   495
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
wenzelm@16533
   496
wenzelm@18731
   497
fun map_theory f = Theory o f o the_theory;
wenzelm@18731
   498
fun map_proof f = Proof o f o the_proof;
wenzelm@18731
   499
wenzelm@18731
   500
fun theory_map f = the_theory o f o Theory;
wenzelm@18731
   501
fun proof_map f = the_proof o f o Proof;
wenzelm@18665
   502
wenzelm@18632
   503
val theory_of = cases I theory_of_proof;
wenzelm@18632
   504
val proof_of = cases init_proof I;
wenzelm@16533
   505
wenzelm@22085
   506
wenzelm@22085
   507
wenzelm@22095
   508
(** delayed theory setup **)
wenzelm@22085
   509
wenzelm@22085
   510
local
wenzelm@22085
   511
  val setup_fn = ref (I: theory -> theory);
wenzelm@22085
   512
in
wenzelm@23944
   513
  fun add_setup f = CRITICAL (fn () => setup_fn := (! setup_fn #> f));
wenzelm@23944
   514
  fun setup () = CRITICAL (fn () => let val f = ! setup_fn in setup_fn := I; f end);
wenzelm@22085
   515
end;
wenzelm@22085
   516
wenzelm@6185
   517
end;
wenzelm@6185
   518
wenzelm@6185
   519
structure BasicContext: BASIC_CONTEXT = Context;
wenzelm@6185
   520
open BasicContext;
wenzelm@16436
   521
wenzelm@16436
   522
wenzelm@16436
   523
wenzelm@16533
   524
(*** type-safe interfaces for data declarations ***)
wenzelm@16533
   525
wenzelm@16533
   526
(** theory data **)
wenzelm@16436
   527
wenzelm@16436
   528
signature THEORY_DATA_ARGS =
wenzelm@16436
   529
sig
wenzelm@16436
   530
  type T
wenzelm@16436
   531
  val empty: T
wenzelm@16436
   532
  val copy: T -> T
wenzelm@16436
   533
  val extend: T -> T
wenzelm@16436
   534
  val merge: Pretty.pp -> T * T -> T
wenzelm@16436
   535
end;
wenzelm@16436
   536
wenzelm@16436
   537
signature THEORY_DATA =
wenzelm@16436
   538
sig
wenzelm@16436
   539
  type T
wenzelm@16436
   540
  val get: theory -> T
wenzelm@16436
   541
  val put: T -> theory -> theory
wenzelm@16436
   542
  val map: (T -> T) -> theory -> theory
wenzelm@22847
   543
  val init: theory -> theory
wenzelm@16436
   544
end;
wenzelm@16436
   545
wenzelm@16436
   546
functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
wenzelm@16436
   547
struct
wenzelm@16436
   548
wenzelm@16436
   549
structure TheoryData = Context.TheoryData;
wenzelm@16436
   550
wenzelm@16436
   551
type T = Data.T;
wenzelm@16436
   552
exception Data of T;
wenzelm@16436
   553
wenzelm@22847
   554
val kind = TheoryData.declare
wenzelm@16436
   555
  (Data Data.empty)
wenzelm@16436
   556
  (fn Data x => Data (Data.copy x))
wenzelm@16436
   557
  (fn Data x => Data (Data.extend x))
wenzelm@16489
   558
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
wenzelm@16436
   559
wenzelm@16436
   560
val get = TheoryData.get kind (fn Data x => x);
wenzelm@16436
   561
val put = TheoryData.put kind Data;
wenzelm@16436
   562
fun map f thy = put (f (get thy)) thy;
wenzelm@16436
   563
wenzelm@22847
   564
fun init thy = map I thy;
wenzelm@22847
   565
wenzelm@16436
   566
end;
wenzelm@16436
   567
wenzelm@16533
   568
wenzelm@16533
   569
wenzelm@16533
   570
(** proof data **)
wenzelm@16533
   571
wenzelm@16533
   572
signature PROOF_DATA_ARGS =
wenzelm@16533
   573
sig
wenzelm@16533
   574
  type T
wenzelm@16533
   575
  val init: theory -> T
wenzelm@16533
   576
end;
wenzelm@16533
   577
wenzelm@16533
   578
signature PROOF_DATA =
wenzelm@16533
   579
sig
wenzelm@16533
   580
  type T
wenzelm@16533
   581
  val get: Context.proof -> T
wenzelm@16533
   582
  val put: T -> Context.proof -> Context.proof
wenzelm@16533
   583
  val map: (T -> T) -> Context.proof -> Context.proof
wenzelm@16533
   584
end;
wenzelm@16533
   585
wenzelm@16533
   586
functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
wenzelm@16533
   587
struct
wenzelm@16533
   588
wenzelm@16533
   589
structure ProofData = Context.ProofData;
wenzelm@16533
   590
wenzelm@16533
   591
type T = Data.T;
wenzelm@16533
   592
exception Data of T;
wenzelm@16533
   593
wenzelm@22847
   594
val kind = ProofData.declare (Data o Data.init);
wenzelm@16533
   595
wenzelm@16533
   596
val get = ProofData.get kind (fn Data x => x);
wenzelm@16533
   597
val put = ProofData.put kind Data;
wenzelm@16533
   598
fun map f prf = put (f (get prf)) prf;
wenzelm@16533
   599
wenzelm@16533
   600
end;
wenzelm@16533
   601
wenzelm@18632
   602
wenzelm@18632
   603
wenzelm@18632
   604
(** generic data **)
wenzelm@18632
   605
wenzelm@18632
   606
signature GENERIC_DATA_ARGS =
wenzelm@18632
   607
sig
wenzelm@18632
   608
  type T
wenzelm@18632
   609
  val empty: T
wenzelm@18632
   610
  val extend: T -> T
wenzelm@18632
   611
  val merge: Pretty.pp -> T * T -> T
wenzelm@18632
   612
end;
wenzelm@18632
   613
wenzelm@18632
   614
signature GENERIC_DATA =
wenzelm@18632
   615
sig
wenzelm@18632
   616
  type T
wenzelm@18632
   617
  val get: Context.generic -> T
wenzelm@18632
   618
  val put: T -> Context.generic -> Context.generic
wenzelm@18632
   619
  val map: (T -> T) -> Context.generic -> Context.generic
wenzelm@18632
   620
end;
wenzelm@18632
   621
wenzelm@18632
   622
functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
wenzelm@18632
   623
struct
wenzelm@18632
   624
wenzelm@22847
   625
structure ThyData = TheoryDataFun(open Data val copy = I);
wenzelm@22847
   626
structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
wenzelm@18632
   627
wenzelm@18632
   628
type T = Data.T;
wenzelm@18632
   629
wenzelm@18632
   630
fun get (Context.Theory thy) = ThyData.get thy
wenzelm@18632
   631
  | get (Context.Proof prf) = PrfData.get prf;
wenzelm@18632
   632
wenzelm@18632
   633
fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
wenzelm@18632
   634
  | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
wenzelm@18632
   635
wenzelm@18632
   636
fun map f ctxt = put (f (get ctxt)) ctxt;
wenzelm@18632
   637
wenzelm@18632
   638
end;
wenzelm@18632
   639
wenzelm@16533
   640
(*hide private interface*)
wenzelm@16436
   641
structure Context: CONTEXT = Context;
wenzelm@20297
   642
wenzelm@21518
   643
(*fake predeclarations*)
wenzelm@20297
   644
structure Proof = struct type context = Context.proof end;
wenzelm@21518
   645
structure ProofContext =
wenzelm@21518
   646
struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;