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