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