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