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