src/Pure/context.ML
author wenzelm
Fri Jul 17 21:40:47 2015 +0200 (2015-07-17)
changeset 60749 f727b99faaf7
parent 59163 857a600f0c94
child 60947 d5f7b424ba47
permissions -rw-r--r--
skeleton for interactive debugger;
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
  exception THEORY of string * theory list
wenzelm@33031
    18
  structure Proof: sig type context end
wenzelm@42360
    19
  structure Proof_Context:
wenzelm@33031
    20
  sig
wenzelm@33031
    21
    val theory_of: Proof.context -> theory
wenzelm@36610
    22
    val init_global: theory -> Proof.context
wenzelm@51686
    23
    val get_global: theory -> string -> 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@29093
    36
  val PureN: string
wenzelm@29093
    37
  val display_names: theory -> string list
wenzelm@16436
    38
  val pretty_thy: theory -> Pretty.T
wenzelm@16436
    39
  val string_of_thy: theory -> string
wenzelm@16436
    40
  val pretty_abbrev_thy: theory -> Pretty.T
wenzelm@16436
    41
  val str_of_thy: theory -> string
wenzelm@37867
    42
  val get_theory: theory -> string -> theory
wenzelm@37871
    43
  val this_theory: theory -> string -> theory
wenzelm@16436
    44
  val eq_thy: theory * theory -> bool
wenzelm@16436
    45
  val subthy: theory * theory -> bool
wenzelm@23355
    46
  val merge: theory * theory -> theory
wenzelm@16489
    47
  val finish_thy: theory -> theory
wenzelm@42383
    48
  val begin_thy: (theory -> pretty) -> string -> theory list -> theory
wenzelm@16533
    49
  (*proof context*)
wenzelm@33031
    50
  val raw_transfer: theory -> Proof.context -> Proof.context
wenzelm@16533
    51
  (*generic context*)
wenzelm@33031
    52
  datatype generic = Theory of theory | Proof of Proof.context
wenzelm@33031
    53
  val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
wenzelm@33031
    54
  val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
wenzelm@33031
    55
  val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
wenzelm@33031
    56
    generic -> 'a * generic
wenzelm@18632
    57
  val the_theory: generic -> theory
wenzelm@33031
    58
  val the_proof: generic -> Proof.context
wenzelm@18731
    59
  val map_theory: (theory -> theory) -> generic -> generic
wenzelm@33031
    60
  val map_proof: (Proof.context -> Proof.context) -> generic -> generic
wenzelm@26486
    61
  val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
wenzelm@33031
    62
  val map_proof_result: (Proof.context -> 'a * Proof.context) -> generic -> 'a * generic
wenzelm@18731
    63
  val theory_map: (generic -> generic) -> theory -> theory
wenzelm@33031
    64
  val proof_map: (generic -> generic) -> Proof.context -> Proof.context
wenzelm@33031
    65
  val theory_of: generic -> theory  (*total*)
wenzelm@33031
    66
  val proof_of: generic -> Proof.context  (*total*)
wenzelm@42383
    67
  (*pretty printing context*)
wenzelm@42383
    68
  val pretty: Proof.context -> pretty
wenzelm@42383
    69
  val pretty_global: theory -> pretty
wenzelm@47005
    70
  val pretty_generic: generic -> pretty
wenzelm@42383
    71
  val pretty_context: (theory -> Proof.context) -> pretty -> Proof.context
wenzelm@26413
    72
  (*thread data*)
wenzelm@26413
    73
  val thread_data: unit -> generic option
wenzelm@26413
    74
  val the_thread_data: unit -> generic
wenzelm@26413
    75
  val set_thread_data: generic option -> unit
wenzelm@26413
    76
  val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
wenzelm@26463
    77
  val >> : (generic -> generic) -> unit
wenzelm@26463
    78
  val >>> : (generic -> 'a * generic) -> 'a
wenzelm@6185
    79
end;
wenzelm@6185
    80
wenzelm@16436
    81
signature PRIVATE_CONTEXT =
wenzelm@16436
    82
sig
wenzelm@16436
    83
  include CONTEXT
wenzelm@33033
    84
  structure Theory_Data:
wenzelm@16436
    85
  sig
wenzelm@51368
    86
    val declare: Position.T -> Any.T -> (Any.T -> Any.T) ->
wenzelm@51368
    87
      (pretty -> Any.T * Any.T -> Any.T) -> serial
wenzelm@51368
    88
    val get: serial -> (Any.T -> 'a) -> theory -> 'a
wenzelm@51368
    89
    val put: serial -> ('a -> Any.T) -> 'a -> theory -> theory
wenzelm@16489
    90
  end
wenzelm@33033
    91
  structure Proof_Data:
wenzelm@16533
    92
  sig
wenzelm@51368
    93
    val declare: (theory -> Any.T) -> serial
wenzelm@51368
    94
    val get: serial -> (Any.T -> 'a) -> Proof.context -> 'a
wenzelm@51368
    95
    val put: serial -> ('a -> Any.T) -> 'a -> Proof.context -> Proof.context
wenzelm@16533
    96
  end
wenzelm@16436
    97
end;
wenzelm@16436
    98
wenzelm@16436
    99
structure Context: PRIVATE_CONTEXT =
wenzelm@6185
   100
struct
wenzelm@6185
   101
wenzelm@16436
   102
(*** theory context ***)
wenzelm@6185
   103
wenzelm@16489
   104
(** theory data **)
wenzelm@16489
   105
wenzelm@16489
   106
(* data kinds and access methods *)
wenzelm@16489
   107
wenzelm@42818
   108
val timing = Unsynchronized.ref false;
wenzelm@42818
   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@51368
   113
datatype pretty = Pretty of Any.T;
wenzelm@42383
   114
wenzelm@16489
   115
local
wenzelm@16489
   116
wenzelm@16489
   117
type kind =
wenzelm@42818
   118
 {pos: Position.T,
wenzelm@51368
   119
  empty: Any.T,
wenzelm@51368
   120
  extend: Any.T -> Any.T,
wenzelm@51368
   121
  merge: pretty -> Any.T * Any.T -> Any.T};
wenzelm@16489
   122
wenzelm@43684
   123
val kinds = Synchronized.var "Theory_Data" (Datatab.empty: kind Datatab.table);
wenzelm@16489
   124
wenzelm@42818
   125
fun invoke name f k x =
wenzelm@43684
   126
  (case Datatab.lookup (Synchronized.value kinds) k of
wenzelm@42818
   127
    SOME kind =>
wenzelm@42818
   128
      if ! timing andalso name <> "" then
wenzelm@48992
   129
        Timing.cond_timeit true ("Theory_Data." ^ name ^ Position.here (#pos kind))
wenzelm@42818
   130
          (fn () => f kind x)
wenzelm@42818
   131
      else f kind x
wenzelm@37852
   132
  | NONE => raise Fail "Invalid theory data identifier");
wenzelm@16489
   133
wenzelm@16489
   134
in
wenzelm@16489
   135
wenzelm@42818
   136
fun invoke_empty k = invoke "" (K o #empty) k ();
wenzelm@42818
   137
val invoke_extend = invoke "extend" #extend;
wenzelm@42818
   138
fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp);
wenzelm@16489
   139
wenzelm@42818
   140
fun declare_theory_data pos empty extend merge =
wenzelm@16489
   141
  let
wenzelm@16489
   142
    val k = serial ();
wenzelm@42818
   143
    val kind = {pos = pos, empty = empty, extend = extend, merge = merge};
wenzelm@43684
   144
    val _ = Synchronized.change kinds (Datatab.update (k, kind));
wenzelm@16489
   145
  in k end;
wenzelm@16489
   146
haftmann@39020
   147
val extend_data = Datatab.map invoke_extend;
wenzelm@59058
   148
fun merge_data pp = Datatab.join (invoke_merge pp) o apply2 extend_data;
wenzelm@16489
   149
wenzelm@16489
   150
end;
wenzelm@16489
   151
wenzelm@16489
   152
wenzelm@16489
   153
wenzelm@16489
   154
(** datatype theory **)
wenzelm@16489
   155
wenzelm@16436
   156
datatype theory =
wenzelm@16436
   157
  Theory of
wenzelm@16533
   158
   (*identity*)
wenzelm@52696
   159
   {id: serial,                   (*identifier*)
wenzelm@52696
   160
    ids: Inttab.set} *            (*cumulative identifiers -- symbolic body content*)
wenzelm@29095
   161
   (*data*)
wenzelm@52696
   162
   Any.T Datatab.table *          (*body content*)
wenzelm@29095
   163
   (*ancestry*)
wenzelm@29093
   164
   {parents: theory list,         (*immediate predecessors*)
wenzelm@29093
   165
    ancestors: theory list} *     (*all predecessors -- canonical reverse order*)
wenzelm@29095
   166
   (*history*)
wenzelm@29093
   167
   {name: string,                 (*official theory name*)
wenzelm@52696
   168
    stage: int};                  (*counter for anonymous updates*)
wenzelm@16436
   169
wenzelm@16436
   170
exception THEORY of string * theory list;
wenzelm@16436
   171
wenzelm@16436
   172
fun rep_theory (Theory args) = args;
wenzelm@16436
   173
wenzelm@16436
   174
val identity_of = #1 o rep_theory;
wenzelm@33033
   175
val data_of = #2 o rep_theory;
wenzelm@16489
   176
val ancestry_of = #3 o rep_theory;
wenzelm@33033
   177
val history_of = #4 o rep_theory;
wenzelm@16436
   178
wenzelm@52696
   179
fun make_identity id ids = {id = id, ids = ids};
wenzelm@16489
   180
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
wenzelm@29093
   181
fun make_history name stage = {name = name, stage = stage};
wenzelm@16436
   182
wenzelm@16436
   183
val parents_of = #parents o ancestry_of;
wenzelm@16436
   184
val ancestors_of = #ancestors o ancestry_of;
wenzelm@16489
   185
val theory_name = #name o history_of;
wenzelm@16436
   186
wenzelm@16436
   187
wenzelm@29093
   188
(* names *)
wenzelm@29093
   189
wenzelm@29093
   190
val PureN = "Pure";
wenzelm@29095
   191
val finished = ~1;
wenzelm@16436
   192
wenzelm@29093
   193
fun display_names thy =
wenzelm@29093
   194
  let
wenzelm@52696
   195
    val {name, stage} = history_of thy;
wenzelm@52696
   196
    val name' =
wenzelm@52696
   197
      if stage = finished then name
wenzelm@52696
   198
      else name ^ ":" ^ string_of_int stage;
wenzelm@29093
   199
    val ancestor_names = map theory_name (ancestors_of thy);
wenzelm@52696
   200
  in rev (name' :: ancestor_names) end;
wenzelm@29069
   201
wenzelm@29093
   202
val pretty_thy = Pretty.str_list "{" "}" o display_names;
wenzelm@16436
   203
val string_of_thy = Pretty.string_of o pretty_thy;
wenzelm@16436
   204
wenzelm@16436
   205
fun pretty_abbrev_thy thy =
wenzelm@16436
   206
  let
wenzelm@29093
   207
    val names = display_names thy;
wenzelm@16436
   208
    val n = length names;
wenzelm@16436
   209
    val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
wenzelm@16436
   210
  in Pretty.str_list "{" "}" abbrev end;
wenzelm@16436
   211
wenzelm@16436
   212
val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
wenzelm@16436
   213
wenzelm@37867
   214
fun get_theory thy name =
wenzelm@37867
   215
  if theory_name thy <> name then
wenzelm@37867
   216
    (case find_first (fn thy' => theory_name thy' = name) (ancestors_of thy) of
wenzelm@37867
   217
      SOME thy' => thy'
wenzelm@37867
   218
    | NONE => error ("Unknown ancestor theory " ^ quote name))
wenzelm@37867
   219
  else if #stage (history_of thy) = finished then thy
wenzelm@37867
   220
  else error ("Unfinished theory " ^ quote name);
wenzelm@37867
   221
wenzelm@37871
   222
fun this_theory thy name =
wenzelm@37871
   223
  if theory_name thy = name then thy
wenzelm@37871
   224
  else get_theory thy name;
wenzelm@37871
   225
wenzelm@16436
   226
wenzelm@29093
   227
(* build ids *)
wenzelm@29093
   228
wenzelm@52696
   229
fun insert_id id ids = Inttab.update (id, ()) ids;
wenzelm@16436
   230
wenzelm@29093
   231
fun merge_ids
wenzelm@52696
   232
    (Theory ({id = id1, ids = ids1, ...}, _, _, _))
wenzelm@52696
   233
    (Theory ({id = id2, ids = ids2, ...}, _, _, _)) =
wenzelm@29093
   234
  Inttab.merge (K true) (ids1, ids2)
wenzelm@52696
   235
  |> insert_id id1
wenzelm@52696
   236
  |> insert_id id2;
wenzelm@16436
   237
wenzelm@16436
   238
wenzelm@16533
   239
(* equality and inclusion *)
wenzelm@16533
   240
wenzelm@59058
   241
val eq_thy = op = o apply2 (#id o identity_of);
wenzelm@16533
   242
wenzelm@29069
   243
fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
wenzelm@29093
   244
  Inttab.defined ids id;
wenzelm@16533
   245
wenzelm@16533
   246
fun subthy thys = eq_thy thys orelse proper_subthy thys;
wenzelm@16533
   247
wenzelm@16533
   248
wenzelm@29093
   249
(* consistent ancestors *)
wenzelm@29093
   250
wenzelm@50431
   251
fun eq_thy_consistent (thy1, thy2) =
wenzelm@50431
   252
  eq_thy (thy1, thy2) orelse
wenzelm@50431
   253
    (theory_name thy1 = theory_name thy2 andalso
wenzelm@50431
   254
      raise THEORY ("Duplicate theory name", [thy1, thy2]));
wenzelm@50431
   255
wenzelm@29093
   256
fun extend_ancestors thy thys =
wenzelm@50431
   257
  if member eq_thy_consistent thys thy then
wenzelm@33033
   258
    raise THEORY ("Duplicate theory node", thy :: thys)
wenzelm@29093
   259
  else thy :: thys;
wenzelm@29093
   260
wenzelm@50431
   261
val merge_ancestors = merge eq_thy_consistent;
wenzelm@29093
   262
wenzelm@29093
   263
wenzelm@23355
   264
(* trivial merge *)
wenzelm@16436
   265
wenzelm@16436
   266
fun merge (thy1, thy2) =
wenzelm@16719
   267
  if eq_thy (thy1, thy2) then thy1
wenzelm@16719
   268
  else if proper_subthy (thy2, thy1) then thy1
wenzelm@16719
   269
  else if proper_subthy (thy1, thy2) then thy2
wenzelm@29093
   270
  else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
wenzelm@29093
   271
    str_of_thy thy1, str_of_thy thy2]);
wenzelm@16436
   272
wenzelm@16436
   273
wenzelm@16489
   274
wenzelm@16489
   275
(** build theories **)
wenzelm@16489
   276
wenzelm@16489
   277
(* primitives *)
wenzelm@16489
   278
wenzelm@52696
   279
fun create_thy ids data ancestry history =
wenzelm@52696
   280
  Theory (make_identity (serial ()) ids, data, ancestry, history);
wenzelm@33606
   281
wenzelm@52696
   282
val pre_pure_thy =
wenzelm@52696
   283
  create_thy Inttab.empty Datatab.empty (make_ancestry [] []) (make_history PureN 0);
wenzelm@16436
   284
wenzelm@52696
   285
local
wenzelm@52696
   286
wenzelm@52696
   287
fun change_thy finish f thy =
wenzelm@16489
   288
  let
wenzelm@52696
   289
    val Theory ({id, ids}, data, ancestry, {name, stage}) = thy;
wenzelm@52696
   290
    val (data', ancestry') =
wenzelm@52696
   291
      if stage = finished then
wenzelm@52696
   292
        (extend_data data, make_ancestry [thy] (extend_ancestors thy (ancestors_of thy)))
wenzelm@52696
   293
      else (data, ancestry);
wenzelm@52696
   294
    val history' = {name = name, stage = if finish then finished else stage + 1};
wenzelm@52696
   295
    val ids' = insert_id id ids;
wenzelm@16489
   296
    val data'' = f data';
wenzelm@52696
   297
  in create_thy ids' data'' ancestry' history' end;
wenzelm@16489
   298
wenzelm@52696
   299
in
wenzelm@16489
   300
wenzelm@52696
   301
val update_thy = change_thy false;
wenzelm@52696
   302
val extend_thy = update_thy I;
wenzelm@52696
   303
val finish_thy = change_thy true I;
wenzelm@16489
   304
wenzelm@52696
   305
end;
wenzelm@16489
   306
wenzelm@16489
   307
wenzelm@16489
   308
(* named theory nodes *)
wenzelm@16489
   309
wenzelm@16489
   310
fun merge_thys pp (thy1, thy2) =
wenzelm@26957
   311
  let
wenzelm@29093
   312
    val ids = merge_ids thy1 thy2;
wenzelm@26957
   313
    val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
wenzelm@26957
   314
    val ancestry = make_ancestry [] [];
wenzelm@29069
   315
    val history = make_history "" 0;
wenzelm@52696
   316
  in create_thy ids data ancestry history end;
wenzelm@16489
   317
wenzelm@16533
   318
fun maximal_thys thys =
wenzelm@28617
   319
  thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
wenzelm@16533
   320
wenzelm@16489
   321
fun begin_thy pp name imports =
wenzelm@52696
   322
  if name = "" then error ("Bad theory name: " ^ quote name)
wenzelm@24369
   323
  else
wenzelm@16489
   324
    let
wenzelm@24141
   325
      val parents = maximal_thys (distinct eq_thy imports);
wenzelm@29093
   326
      val ancestors =
wenzelm@29093
   327
        Library.foldl merge_ancestors ([], map ancestors_of parents)
wenzelm@29093
   328
        |> fold extend_ancestors parents;
wenzelm@29093
   329
wenzelm@29093
   330
      val Theory ({ids, ...}, data, _, _) =
wenzelm@16489
   331
        (case parents of
wenzelm@48638
   332
          [] => error "Missing theory imports"
wenzelm@16533
   333
        | [thy] => extend_thy thy
wenzelm@16533
   334
        | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
wenzelm@29093
   335
wenzelm@16489
   336
      val ancestry = make_ancestry parents ancestors;
wenzelm@29069
   337
      val history = make_history name 0;
wenzelm@52696
   338
    in create_thy ids data ancestry history end;
wenzelm@16489
   339
wenzelm@16489
   340
wenzelm@16489
   341
(* theory data *)
wenzelm@16436
   342
wenzelm@33033
   343
structure Theory_Data =
wenzelm@16489
   344
struct
wenzelm@16489
   345
wenzelm@16489
   346
val declare = declare_theory_data;
wenzelm@16489
   347
wenzelm@16489
   348
fun get k dest thy =
wenzelm@34253
   349
  (case Datatab.lookup (data_of thy) k of
wenzelm@22847
   350
    SOME x => x
wenzelm@34253
   351
  | NONE => invoke_empty k) |> dest;
wenzelm@16489
   352
wenzelm@52696
   353
fun put k mk x = update_thy (Datatab.update (k, mk x));
wenzelm@16489
   354
wenzelm@16489
   355
end;
wenzelm@16436
   356
wenzelm@16436
   357
wenzelm@16436
   358
wenzelm@16533
   359
(*** proof context ***)
wenzelm@16533
   360
wenzelm@33031
   361
(* datatype Proof.context *)
wenzelm@17060
   362
wenzelm@33031
   363
structure Proof =
wenzelm@33031
   364
struct
wenzelm@52788
   365
  datatype context = Context of Any.T Datatab.table * theory;
wenzelm@33031
   366
end;
wenzelm@33031
   367
wenzelm@16533
   368
wenzelm@16533
   369
(* proof data kinds *)
wenzelm@16533
   370
wenzelm@16533
   371
local
wenzelm@16533
   372
wenzelm@51368
   373
val kinds = Synchronized.var "Proof_Data" (Datatab.empty: (theory -> Any.T) Datatab.table);
wenzelm@16533
   374
wenzelm@59163
   375
fun init_data thy =
wenzelm@59163
   376
  Synchronized.value kinds |> Datatab.map (fn _ => fn init => init thy);
wenzelm@16533
   377
wenzelm@59163
   378
fun init_new_data thy =
wenzelm@59163
   379
  Synchronized.value kinds |> Datatab.fold (fn (k, init) => fn data =>
wenzelm@59163
   380
    if Datatab.defined data k then data
wenzelm@59163
   381
    else Datatab.update (k, init thy) data);
wenzelm@22847
   382
wenzelm@59163
   383
fun init_fallback k thy =
wenzelm@59163
   384
  (case Datatab.lookup (Synchronized.value kinds) k of
wenzelm@59163
   385
    SOME init => init thy
wenzelm@59163
   386
  | NONE => raise Fail "Invalid proof data identifier");
wenzelm@16533
   387
wenzelm@16533
   388
in
wenzelm@16533
   389
wenzelm@52788
   390
fun raw_transfer thy' (Proof.Context (data, thy)) =
wenzelm@24141
   391
  let
wenzelm@52682
   392
    val _ = subthy (thy, thy') orelse error "Cannot transfer proof context: not a super theory";
wenzelm@59163
   393
    val data' = init_new_data thy' data;
wenzelm@52788
   394
  in Proof.Context (data', thy') end;
wenzelm@22847
   395
wenzelm@42360
   396
structure Proof_Context =
wenzelm@33031
   397
struct
wenzelm@59163
   398
  fun theory_of (Proof.Context (_, thy)) = thy;
wenzelm@52788
   399
  fun init_global thy = Proof.Context (init_data thy, thy);
wenzelm@51686
   400
  fun get_global thy name = init_global (get_theory thy name);
wenzelm@33031
   401
end;
wenzelm@16533
   402
wenzelm@33033
   403
structure Proof_Data =
wenzelm@16533
   404
struct
wenzelm@16533
   405
wenzelm@22847
   406
fun declare init =
wenzelm@16533
   407
  let
wenzelm@16533
   408
    val k = serial ();
wenzelm@43684
   409
    val _ = Synchronized.change kinds (Datatab.update (k, init));
wenzelm@16533
   410
  in k end;
wenzelm@16533
   411
wenzelm@59163
   412
fun get k dest (Proof.Context (data, thy)) =
wenzelm@59163
   413
  (case Datatab.lookup data k of
wenzelm@22847
   414
    SOME x => x
wenzelm@59163
   415
  | NONE => init_fallback k thy) |> dest;
wenzelm@16533
   416
wenzelm@59163
   417
fun put k mk x (Proof.Context (data, thy)) =
wenzelm@59163
   418
  Proof.Context (Datatab.update (k, mk x) data, thy);
wenzelm@16533
   419
wenzelm@16533
   420
end;
wenzelm@16533
   421
wenzelm@16533
   422
end;
wenzelm@16533
   423
wenzelm@16533
   424
wenzelm@18632
   425
wenzelm@16533
   426
(*** generic context ***)
wenzelm@16533
   427
wenzelm@33031
   428
datatype generic = Theory of theory | Proof of Proof.context;
wenzelm@18632
   429
wenzelm@18632
   430
fun cases f _ (Theory thy) = f thy
wenzelm@18632
   431
  | cases _ g (Proof prf) = g prf;
wenzelm@16533
   432
wenzelm@19678
   433
fun mapping f g = cases (Theory o f) (Proof o g);
wenzelm@21660
   434
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
wenzelm@19678
   435
wenzelm@23595
   436
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
wenzelm@23595
   437
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
wenzelm@16533
   438
wenzelm@18731
   439
fun map_theory f = Theory o f o the_theory;
wenzelm@18731
   440
fun map_proof f = Proof o f o the_proof;
wenzelm@18731
   441
wenzelm@26486
   442
fun map_theory_result f = apsnd Theory o f o the_theory;
wenzelm@26486
   443
fun map_proof_result f = apsnd Proof o f o the_proof;
wenzelm@26486
   444
wenzelm@18731
   445
fun theory_map f = the_theory o f o Theory;
wenzelm@18731
   446
fun proof_map f = the_proof o f o Proof;
wenzelm@18665
   447
wenzelm@42360
   448
val theory_of = cases I Proof_Context.theory_of;
wenzelm@42360
   449
val proof_of = cases Proof_Context.init_global I;
wenzelm@16533
   450
wenzelm@22085
   451
wenzelm@42383
   452
(* pretty printing context *)
wenzelm@42383
   453
wenzelm@42383
   454
exception PRETTY of generic;
wenzelm@42383
   455
wenzelm@47005
   456
val pretty_generic = Pretty o PRETTY;
wenzelm@47005
   457
val pretty = pretty_generic o Proof;
wenzelm@47005
   458
val pretty_global = pretty_generic o Theory;
wenzelm@42383
   459
wenzelm@42383
   460
fun pretty_context init (Pretty (PRETTY context)) = cases init I context;
wenzelm@42383
   461
wenzelm@42383
   462
wenzelm@22085
   463
wenzelm@26413
   464
(** thread data **)
wenzelm@26413
   465
wenzelm@26413
   466
local val tag = Universal.tag () : generic option Universal.tag in
wenzelm@26413
   467
wenzelm@26413
   468
fun thread_data () =
wenzelm@28122
   469
  (case Thread.getLocal tag of
wenzelm@26413
   470
    SOME (SOME context) => SOME context
wenzelm@26413
   471
  | _ => NONE);
wenzelm@26413
   472
wenzelm@26413
   473
fun the_thread_data () =
wenzelm@26413
   474
  (case thread_data () of
wenzelm@26413
   475
    SOME context => context
wenzelm@26413
   476
  | _ => error "Unknown context");
wenzelm@26413
   477
wenzelm@28122
   478
fun set_thread_data context = Thread.setLocal (tag, context);
wenzelm@26413
   479
fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
wenzelm@26413
   480
wenzelm@26428
   481
end;
wenzelm@26428
   482
wenzelm@26428
   483
fun >>> f =
wenzelm@26428
   484
  let
wenzelm@26463
   485
    val (res, context') = f (the_thread_data ());
wenzelm@26463
   486
    val _ = set_thread_data (SOME context');
wenzelm@26428
   487
  in res end;
wenzelm@26428
   488
wenzelm@26421
   489
nonfix >>;
wenzelm@26463
   490
fun >> f = >>> (fn context => ((), f context));
wenzelm@26413
   491
wenzelm@26428
   492
val _ = set_thread_data (SOME (Theory pre_pure_thy));
wenzelm@26413
   493
wenzelm@6185
   494
end;
wenzelm@6185
   495
wenzelm@33031
   496
structure Basic_Context: BASIC_CONTEXT = Context;
wenzelm@33031
   497
open Basic_Context;
wenzelm@16436
   498
wenzelm@16436
   499
wenzelm@16436
   500
wenzelm@16533
   501
(*** type-safe interfaces for data declarations ***)
wenzelm@16533
   502
wenzelm@16533
   503
(** theory data **)
wenzelm@16436
   504
wenzelm@34259
   505
signature THEORY_DATA_PP_ARGS =
wenzelm@16436
   506
sig
wenzelm@16436
   507
  type T
wenzelm@16436
   508
  val empty: T
wenzelm@16436
   509
  val extend: T -> T
wenzelm@42383
   510
  val merge: Context.pretty -> T * T -> T
wenzelm@16436
   511
end;
wenzelm@16436
   512
wenzelm@34259
   513
signature THEORY_DATA_ARGS =
wenzelm@34259
   514
sig
wenzelm@34259
   515
  type T
wenzelm@34259
   516
  val empty: T
wenzelm@34259
   517
  val extend: T -> T
wenzelm@34259
   518
  val merge: T * T -> T
wenzelm@34259
   519
end;
wenzelm@34259
   520
wenzelm@34253
   521
signature THEORY_DATA =
wenzelm@16436
   522
sig
wenzelm@16436
   523
  type T
wenzelm@16436
   524
  val get: theory -> T
wenzelm@16436
   525
  val put: T -> theory -> theory
wenzelm@16436
   526
  val map: (T -> T) -> theory -> theory
wenzelm@16436
   527
end;
wenzelm@16436
   528
wenzelm@34259
   529
functor Theory_Data_PP(Data: THEORY_DATA_PP_ARGS): THEORY_DATA =
wenzelm@16436
   530
struct
wenzelm@16436
   531
wenzelm@16436
   532
type T = Data.T;
wenzelm@16436
   533
exception Data of T;
wenzelm@16436
   534
wenzelm@42818
   535
val kind =
wenzelm@42818
   536
  Context.Theory_Data.declare
wenzelm@42818
   537
    (Position.thread_data ())
wenzelm@42818
   538
    (Data Data.empty)
wenzelm@42818
   539
    (fn Data x => Data (Data.extend x))
wenzelm@42818
   540
    (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
wenzelm@16436
   541
wenzelm@33033
   542
val get = Context.Theory_Data.get kind (fn Data x => x);
wenzelm@33033
   543
val put = Context.Theory_Data.put kind Data;
wenzelm@16436
   544
fun map f thy = put (f (get thy)) thy;
wenzelm@16436
   545
wenzelm@16436
   546
end;
wenzelm@16436
   547
wenzelm@33517
   548
functor Theory_Data(Data: THEORY_DATA_ARGS): THEORY_DATA =
wenzelm@34259
   549
  Theory_Data_PP
wenzelm@34259
   550
  (
wenzelm@34259
   551
    type T = Data.T;
wenzelm@34259
   552
    val empty = Data.empty;
wenzelm@34259
   553
    val extend = Data.extend;
wenzelm@34259
   554
    fun merge _ = Data.merge;
wenzelm@34259
   555
  );
wenzelm@33517
   556
wenzelm@16533
   557
wenzelm@16533
   558
wenzelm@16533
   559
(** proof data **)
wenzelm@16533
   560
wenzelm@16533
   561
signature PROOF_DATA_ARGS =
wenzelm@16533
   562
sig
wenzelm@16533
   563
  type T
wenzelm@16533
   564
  val init: theory -> T
wenzelm@16533
   565
end;
wenzelm@16533
   566
wenzelm@16533
   567
signature PROOF_DATA =
wenzelm@16533
   568
sig
wenzelm@16533
   569
  type T
wenzelm@33031
   570
  val get: Proof.context -> T
wenzelm@33031
   571
  val put: T -> Proof.context -> Proof.context
wenzelm@33031
   572
  val map: (T -> T) -> Proof.context -> Proof.context
wenzelm@16533
   573
end;
wenzelm@16533
   574
wenzelm@33517
   575
functor Proof_Data(Data: PROOF_DATA_ARGS): PROOF_DATA =
wenzelm@16533
   576
struct
wenzelm@16533
   577
wenzelm@16533
   578
type T = Data.T;
wenzelm@16533
   579
exception Data of T;
wenzelm@16533
   580
wenzelm@33033
   581
val kind = Context.Proof_Data.declare (Data o Data.init);
wenzelm@16533
   582
wenzelm@33033
   583
val get = Context.Proof_Data.get kind (fn Data x => x);
wenzelm@33033
   584
val put = Context.Proof_Data.put kind Data;
wenzelm@16533
   585
fun map f prf = put (f (get prf)) prf;
wenzelm@16533
   586
wenzelm@16533
   587
end;
wenzelm@16533
   588
wenzelm@18632
   589
wenzelm@18632
   590
wenzelm@18632
   591
(** generic data **)
wenzelm@18632
   592
wenzelm@18632
   593
signature GENERIC_DATA_ARGS =
wenzelm@18632
   594
sig
wenzelm@18632
   595
  type T
wenzelm@18632
   596
  val empty: T
wenzelm@18632
   597
  val extend: T -> T
wenzelm@33517
   598
  val merge: T * T -> T
wenzelm@18632
   599
end;
wenzelm@18632
   600
wenzelm@18632
   601
signature GENERIC_DATA =
wenzelm@18632
   602
sig
wenzelm@18632
   603
  type T
wenzelm@18632
   604
  val get: Context.generic -> T
wenzelm@18632
   605
  val put: T -> Context.generic -> Context.generic
wenzelm@18632
   606
  val map: (T -> T) -> Context.generic -> Context.generic
wenzelm@18632
   607
end;
wenzelm@18632
   608
wenzelm@33517
   609
functor Generic_Data(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
wenzelm@18632
   610
struct
wenzelm@18632
   611
wenzelm@33517
   612
structure Thy_Data = Theory_Data(Data);
wenzelm@33517
   613
structure Prf_Data = Proof_Data(type T = Data.T val init = Thy_Data.get);
wenzelm@18632
   614
wenzelm@18632
   615
type T = Data.T;
wenzelm@18632
   616
wenzelm@33033
   617
fun get (Context.Theory thy) = Thy_Data.get thy
wenzelm@33033
   618
  | get (Context.Proof prf) = Prf_Data.get prf;
wenzelm@18632
   619
wenzelm@33033
   620
fun put x (Context.Theory thy) = Context.Theory (Thy_Data.put x thy)
wenzelm@33033
   621
  | put x (Context.Proof prf) = Context.Proof (Prf_Data.put x prf);
wenzelm@18632
   622
wenzelm@18632
   623
fun map f ctxt = put (f (get ctxt)) ctxt;
wenzelm@18632
   624
wenzelm@18632
   625
end;
wenzelm@18632
   626
wenzelm@16533
   627
(*hide private interface*)
wenzelm@16436
   628
structure Context: CONTEXT = Context;
wenzelm@20297
   629