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