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