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