src/Pure/context.ML
author wenzelm
Sat Dec 13 15:00:39 2008 +0100 (2008-12-13)
changeset 29091 b81fe045e799
parent 29069 c7ba485581ae
child 29093 1cc36c0ec9eb
permissions -rw-r--r--
Context.display_names;
     1 (*  Title:      Pure/context.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Generic theory contexts with unique identity, arbitrarily typed data,
     5 monotonic development graph and history support.  Generic proof
     6 contexts with arbitrarily typed data.
     7 *)
     8 
     9 signature BASIC_CONTEXT =
    10 sig
    11   type theory
    12   type theory_ref
    13   exception THEORY of string * theory list
    14 end;
    15 
    16 signature CONTEXT =
    17 sig
    18   include BASIC_CONTEXT
    19   (*theory context*)
    20   val parents_of: theory -> theory list
    21   val ancestors_of: theory -> theory list
    22   val theory_name: theory -> string
    23   val is_stale: theory -> bool
    24   val PureN: string
    25   val is_draft: theory -> bool
    26   val reject_draft: theory -> theory
    27   val exists_name: string -> theory -> bool
    28   val names_of: theory -> string list
    29   val pretty_thy: theory -> Pretty.T
    30   val string_of_thy: theory -> string
    31   val pprint_thy: theory -> pprint_args -> unit
    32   val pprint_thy_ref: theory_ref -> pprint_args -> unit
    33   val pretty_abbrev_thy: theory -> Pretty.T
    34   val str_of_thy: theory -> string
    35   val deref: theory_ref -> theory
    36   val check_thy: theory -> theory_ref
    37   val eq_thy: theory * theory -> bool
    38   val subthy: theory * theory -> bool
    39   val joinable: theory * theory -> bool
    40   val merge: theory * theory -> theory
    41   val merge_refs: theory_ref * theory_ref -> theory_ref
    42   val copy_thy: theory -> theory
    43   val checkpoint_thy: theory -> theory
    44   val finish_thy: theory -> theory
    45   val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    46   (*proof context*)
    47   type proof
    48   val theory_of_proof: proof -> theory
    49   val transfer_proof: theory -> proof -> proof
    50   val init_proof: theory -> proof
    51   (*generic context*)
    52   datatype generic = Theory of theory | Proof of proof
    53   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    54   val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
    55   val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
    56   val the_theory: generic -> theory
    57   val the_proof: generic -> proof
    58   val map_theory: (theory -> theory) -> generic -> generic
    59   val map_proof: (proof -> proof) -> generic -> generic
    60   val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    61   val map_proof_result: (proof -> 'a * proof) -> generic -> 'a * generic
    62   val theory_map: (generic -> generic) -> theory -> theory
    63   val proof_map: (generic -> generic) -> proof -> proof
    64   val theory_of: generic -> theory   (*total*)
    65   val proof_of: generic -> proof     (*total*)
    66   (*thread data*)
    67   val thread_data: unit -> generic option
    68   val the_thread_data: unit -> generic
    69   val set_thread_data: generic option -> unit
    70   val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
    71   val >> : (generic -> generic) -> unit
    72   val >>> : (generic -> 'a * generic) -> 'a
    73 end;
    74 
    75 signature PRIVATE_CONTEXT =
    76 sig
    77   include CONTEXT
    78   structure TheoryData:
    79   sig
    80     val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    81       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    82     val get: serial -> (Object.T -> 'a) -> theory -> 'a
    83     val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    84   end
    85   structure ProofData:
    86   sig
    87     val declare: (theory -> Object.T) -> serial
    88     val get: serial -> (Object.T -> 'a) -> proof -> 'a
    89     val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
    90   end
    91 end;
    92 
    93 structure Context: PRIVATE_CONTEXT =
    94 struct
    95 
    96 (*** theory context ***)
    97 
    98 (** theory data **)
    99 
   100 (* data kinds and access methods *)
   101 
   102 (*private copy avoids potential conflict of table exceptions*)
   103 structure Datatab = TableFun(type key = int val ord = int_ord);
   104 
   105 local
   106 
   107 type kind =
   108  {empty: Object.T,
   109   copy: Object.T -> Object.T,
   110   extend: Object.T -> Object.T,
   111   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   112 
   113 val kinds = ref (Datatab.empty: kind Datatab.table);
   114 
   115 fun invoke f k =
   116   (case Datatab.lookup (! kinds) k of
   117     SOME kind => f kind
   118   | NONE => sys_error "Invalid theory data identifier");
   119 
   120 in
   121 
   122 fun invoke_empty k   = invoke (K o #empty) k ();
   123 val invoke_copy      = invoke #copy;
   124 val invoke_extend    = invoke #extend;
   125 fun invoke_merge pp  = invoke (fn kind => #merge kind pp);
   126 
   127 fun declare_theory_data empty copy extend merge =
   128   let
   129     val k = serial ();
   130     val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
   131     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
   132   in k end;
   133 
   134 val copy_data = Datatab.map' invoke_copy;
   135 val extend_data = Datatab.map' invoke_extend;
   136 fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
   137 
   138 end;
   139 
   140 
   141 
   142 (** datatype theory **)
   143 
   144 datatype theory =
   145   Theory of
   146    (*identity*)
   147    {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
   148     id: serial * (string * int),        (*identifier/name of this theory node*)
   149     ids: (string * int) Inttab.table} * (*ancestors and checkpoints*)
   150    (*data*)
   151    Object.T Datatab.table *
   152    (*ancestry*)
   153    {parents: theory list,               (*immediate predecessors*)
   154     ancestors: theory list} *           (*all predecessors*)
   155    (*history*)
   156    {name: string,                       (*prospective name of finished theory*)
   157     version: int};                      (*checkpoint counter*)
   158 
   159 exception THEORY of string * theory list;
   160 
   161 fun rep_theory (Theory args) = args;
   162 
   163 val identity_of = #1 o rep_theory;
   164 val data_of     = #2 o rep_theory;
   165 val ancestry_of = #3 o rep_theory;
   166 val history_of  = #4 o rep_theory;
   167 
   168 fun make_identity self id ids = {self = self, id = id, ids = ids};
   169 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   170 fun make_history name version = {name = name, version = version};
   171 
   172 val the_self = the o #self o identity_of;
   173 val parents_of = #parents o ancestry_of;
   174 val ancestors_of = #ancestors o ancestry_of;
   175 val theory_name = #name o history_of;
   176 
   177 
   178 (* staleness *)
   179 
   180 fun eq_id ((i: int, _), (j, _)) = (i = j);
   181 
   182 fun is_stale
   183     (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
   184       not (eq_id (id, id'))
   185   | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   186 
   187 fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   188   | vitalize (thy as Theory ({self = NONE, id, ids}, data, ancestry, history)) =
   189       let
   190         val r = ref thy;
   191         val thy' = Theory (make_identity (SOME r) id ids, data, ancestry, history);
   192       in r := thy'; thy' end;
   193 
   194 
   195 (* names *)
   196 
   197 val PureN = "Pure";
   198 
   199 val draftN = "#";
   200 val draft_name = (draftN, ~1);
   201 
   202 fun draft_id (_, (name, _)) = (name = draftN);
   203 val is_draft = draft_id o #id o identity_of;
   204 
   205 fun reject_draft thy =
   206   if is_draft thy then raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
   207   else thy;
   208 
   209 fun exists_name name (thy as Theory ({id = (_, (a, _)), ids, ...}, _, _, _)) =
   210   name = theory_name thy orelse
   211   name = a orelse
   212   Inttab.exists (fn (_, (b, _)) => b = name) ids;
   213 
   214 fun name_of (a, ~1) = a
   215   | name_of (a, i) = a ^ ":" ^ string_of_int i;
   216 
   217 fun names_of (Theory ({id = (_, a), ids, ...}, _, _, _)) =
   218   rev (name_of a :: Inttab.fold (fn (_, (b, ~1)) => cons b | _ => I) ids []);
   219 
   220 fun pretty_thy thy =
   221   Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   222 
   223 val string_of_thy = Pretty.string_of o pretty_thy;
   224 val pprint_thy = Pretty.pprint o pretty_thy;
   225 
   226 fun pretty_abbrev_thy thy =
   227   let
   228     val names = names_of thy;
   229     val n = length names;
   230     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   231   in Pretty.str_list "{" "}" abbrev end;
   232 
   233 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   234 
   235 
   236 (* theory references *)
   237 
   238 (*theory_ref provides a safe way to store dynamic references to a
   239   theory in external data structures -- a plain theory value would
   240   become stale as the self reference moves on*)
   241 
   242 datatype theory_ref = TheoryRef of theory ref;
   243 
   244 fun deref (TheoryRef (ref thy)) = thy;
   245 
   246 fun check_thy thy =  (*thread-safe version*)
   247   let val thy_ref = TheoryRef (the_self thy) in
   248     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   249     else thy_ref
   250   end;
   251 
   252 val pprint_thy_ref = Pretty.pprint o pretty_thy o deref;
   253 
   254 
   255 (* consistency *)
   256 
   257 fun check_insert id ids =
   258   if draft_id id orelse Inttab.defined ids (#1 id) then ids
   259   else if Inttab.exists (fn (_, a) => a = #2 id) ids then
   260     error ("Different versions of theory component " ^ quote (name_of (#2 id)))
   261   else Inttab.update id ids;
   262 
   263 fun check_merge
   264     (Theory ({id = id1, ids = ids1, ...}, _, _, _))
   265     (Theory ({id = id2, ids = ids2, ...}, _, _, _)) =
   266   Inttab.fold check_insert ids2 ids1
   267   |> check_insert id1
   268   |> check_insert id2;
   269 
   270 
   271 (* equality and inclusion *)
   272 
   273 val eq_thy = eq_id o pairself (#id o identity_of);
   274 
   275 fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
   276   Inttab.defined ids (#1 id);
   277 
   278 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   279 
   280 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   281 
   282 
   283 (* trivial merge *)
   284 
   285 fun merge (thy1, thy2) =
   286   if eq_thy (thy1, thy2) then thy1
   287   else if proper_subthy (thy2, thy1) then thy1
   288   else if proper_subthy (thy1, thy2) then thy2
   289   else (check_merge thy1 thy2;
   290     error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   291       str_of_thy thy1, str_of_thy thy2]));
   292 
   293 fun merge_refs (ref1, ref2) =
   294   if ref1 = ref2 then ref1
   295   else check_thy (merge (deref ref1, deref ref2));
   296 
   297 
   298 
   299 (** build theories **)
   300 
   301 (* primitives *)
   302 
   303 fun create_thy name self id ids data ancestry history =
   304   let
   305     val {version, ...} = history;
   306     val ids' = check_insert id ids;
   307     val id' = (serial (), name);
   308     val _ = check_insert id' ids';
   309     val identity' = make_identity self id' ids';
   310   in vitalize (Theory (identity', data, ancestry, history)) end;
   311 
   312 fun change_thy name f thy =
   313   let
   314     val Theory ({self, id, ids}, data, ancestry, history) = thy;
   315     val (self', data', ancestry') =
   316       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   317       else if #version history > 0
   318       then (NONE, copy_data data, ancestry)
   319       else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   320     val data'' = f data';
   321     val thy' = NAMED_CRITICAL "theory" (fn () =>
   322       (check_thy thy; create_thy name self' id ids data'' ancestry' history));
   323   in thy' end;
   324 
   325 fun name_thy name = change_thy name I;
   326 val modify_thy = change_thy draft_name;
   327 val extend_thy = modify_thy I;
   328 
   329 fun copy_thy thy =
   330   let
   331     val Theory ({id, ids, ...}, data, ancestry, history) = thy;
   332     val data' = copy_data data;
   333     val thy' = NAMED_CRITICAL "theory" (fn () =>
   334       (check_thy thy; create_thy draft_name NONE id ids data' ancestry history));
   335   in thy' end;
   336 
   337 val pre_pure_thy = create_thy draft_name NONE (serial (), draft_name) Inttab.empty
   338   Datatab.empty (make_ancestry [] []) (make_history PureN 0);
   339 
   340 
   341 (* named theory nodes *)
   342 
   343 fun merge_thys pp (thy1, thy2) =
   344   let
   345     val ids = check_merge thy1 thy2;
   346     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   347     val ancestry = make_ancestry [] [];
   348     val history = make_history "" 0;
   349     val thy' = NAMED_CRITICAL "theory" (fn () =>
   350      (check_thy thy1; check_thy thy2;
   351       create_thy draft_name NONE (serial (), draft_name) ids data ancestry history));
   352   in thy' end;
   353 
   354 fun maximal_thys thys =
   355   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
   356 
   357 fun begin_thy pp name imports =
   358   if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   359   else
   360     let
   361       val parents = maximal_thys (distinct eq_thy imports);
   362       val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
   363       val Theory ({id, ids, ...}, data, _, _) =
   364         (case parents of
   365           [] => error "No parent theories"
   366         | [thy] => extend_thy thy
   367         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   368       val ancestry = make_ancestry parents ancestors;
   369       val history = make_history name 0;
   370       val thy' = NAMED_CRITICAL "theory" (fn () =>
   371         (map check_thy imports; create_thy draft_name NONE id ids data ancestry history));
   372     in thy' end;
   373 
   374 
   375 (* persistent checkpoints *)
   376 
   377 fun checkpoint_thy thy =
   378   if not (is_draft thy) then thy
   379   else
   380     let
   381       val {name, version} = history_of thy;
   382       val thy' as Theory (identity', data', ancestry', _) = name_thy (name, version) thy;
   383       val history' = make_history name (version + 1);
   384       val thy'' = NAMED_CRITICAL "theory" (fn () =>
   385         (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   386     in thy'' end;
   387 
   388 fun finish_thy thy = NAMED_CRITICAL "theory" (fn () =>
   389   let
   390     val name = theory_name thy;
   391     val Theory (identity', data', ancestry', _) = name_thy (name, ~1) thy;
   392     val history' = make_history name 0;
   393     val thy' = vitalize (Theory (identity', data', ancestry', history'));
   394   in thy' end);
   395 
   396 
   397 (* theory data *)
   398 
   399 structure TheoryData =
   400 struct
   401 
   402 val declare = declare_theory_data;
   403 
   404 fun get k dest thy =
   405   dest ((case Datatab.lookup (data_of thy) k of
   406     SOME x => x
   407   | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
   408 
   409 fun put k mk x = modify_thy (Datatab.update (k, mk x));
   410 
   411 end;
   412 
   413 
   414 
   415 (*** proof context ***)
   416 
   417 (* datatype proof *)
   418 
   419 datatype proof = Prf of Object.T Datatab.table * theory_ref;
   420 
   421 fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
   422 fun data_of_proof (Prf (data, _)) = data;
   423 fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
   424 
   425 
   426 (* proof data kinds *)
   427 
   428 local
   429 
   430 val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
   431 
   432 fun invoke_init k =
   433   (case Datatab.lookup (! kinds) k of
   434     SOME init => init
   435   | NONE => sys_error "Invalid proof data identifier");
   436 
   437 fun init_data thy =
   438   Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
   439 
   440 fun init_new_data data thy =
   441   Datatab.merge (K true) (data, init_data thy);
   442 
   443 in
   444 
   445 fun init_proof thy = Prf (init_data thy, check_thy thy);
   446 
   447 fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
   448   let
   449     val thy = deref thy_ref;
   450     val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
   451     val _ = check_thy thy;
   452     val data' = init_new_data data thy';
   453     val thy_ref' = check_thy thy';
   454   in Prf (data', thy_ref') end;
   455 
   456 
   457 structure ProofData =
   458 struct
   459 
   460 fun declare init =
   461   let
   462     val k = serial ();
   463     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
   464   in k end;
   465 
   466 fun get k dest prf =
   467   dest (case Datatab.lookup (data_of_proof prf) k of
   468     SOME x => x
   469   | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
   470 
   471 fun put k mk x = map_prf (Datatab.update (k, mk x));
   472 
   473 end;
   474 
   475 end;
   476 
   477 
   478 
   479 (*** generic context ***)
   480 
   481 datatype generic = Theory of theory | Proof of proof;
   482 
   483 fun cases f _ (Theory thy) = f thy
   484   | cases _ g (Proof prf) = g prf;
   485 
   486 fun mapping f g = cases (Theory o f) (Proof o g);
   487 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   488 
   489 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   490 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   491 
   492 fun map_theory f = Theory o f o the_theory;
   493 fun map_proof f = Proof o f o the_proof;
   494 
   495 fun map_theory_result f = apsnd Theory o f o the_theory;
   496 fun map_proof_result f = apsnd Proof o f o the_proof;
   497 
   498 fun theory_map f = the_theory o f o Theory;
   499 fun proof_map f = the_proof o f o Proof;
   500 
   501 val theory_of = cases I theory_of_proof;
   502 val proof_of = cases init_proof I;
   503 
   504 
   505 
   506 (** thread data **)
   507 
   508 local val tag = Universal.tag () : generic option Universal.tag in
   509 
   510 fun thread_data () =
   511   (case Thread.getLocal tag of
   512     SOME (SOME context) => SOME context
   513   | _ => NONE);
   514 
   515 fun the_thread_data () =
   516   (case thread_data () of
   517     SOME context => context
   518   | _ => error "Unknown context");
   519 
   520 fun set_thread_data context = Thread.setLocal (tag, context);
   521 fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
   522 
   523 end;
   524 
   525 fun >>> f =
   526   let
   527     val (res, context') = f (the_thread_data ());
   528     val _ = set_thread_data (SOME context');
   529   in res end;
   530 
   531 nonfix >>;
   532 fun >> f = >>> (fn context => ((), f context));
   533 
   534 val _ = set_thread_data (SOME (Theory pre_pure_thy));
   535 
   536 end;
   537 
   538 structure BasicContext: BASIC_CONTEXT = Context;
   539 open BasicContext;
   540 
   541 
   542 
   543 (*** type-safe interfaces for data declarations ***)
   544 
   545 (** theory data **)
   546 
   547 signature THEORY_DATA_ARGS =
   548 sig
   549   type T
   550   val empty: T
   551   val copy: T -> T
   552   val extend: T -> T
   553   val merge: Pretty.pp -> T * T -> T
   554 end;
   555 
   556 signature THEORY_DATA =
   557 sig
   558   type T
   559   val get: theory -> T
   560   val put: T -> theory -> theory
   561   val map: (T -> T) -> theory -> theory
   562   val init: theory -> theory
   563 end;
   564 
   565 functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
   566 struct
   567 
   568 structure TheoryData = Context.TheoryData;
   569 
   570 type T = Data.T;
   571 exception Data of T;
   572 
   573 val kind = TheoryData.declare
   574   (Data Data.empty)
   575   (fn Data x => Data (Data.copy x))
   576   (fn Data x => Data (Data.extend x))
   577   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   578 
   579 val get = TheoryData.get kind (fn Data x => x);
   580 val put = TheoryData.put kind Data;
   581 fun map f thy = put (f (get thy)) thy;
   582 
   583 fun init thy = map I thy;
   584 
   585 end;
   586 
   587 
   588 
   589 (** proof data **)
   590 
   591 signature PROOF_DATA_ARGS =
   592 sig
   593   type T
   594   val init: theory -> T
   595 end;
   596 
   597 signature PROOF_DATA =
   598 sig
   599   type T
   600   val get: Context.proof -> T
   601   val put: T -> Context.proof -> Context.proof
   602   val map: (T -> T) -> Context.proof -> Context.proof
   603 end;
   604 
   605 functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
   606 struct
   607 
   608 structure ProofData = Context.ProofData;
   609 
   610 type T = Data.T;
   611 exception Data of T;
   612 
   613 val kind = ProofData.declare (Data o Data.init);
   614 
   615 val get = ProofData.get kind (fn Data x => x);
   616 val put = ProofData.put kind Data;
   617 fun map f prf = put (f (get prf)) prf;
   618 
   619 end;
   620 
   621 
   622 
   623 (** generic data **)
   624 
   625 signature GENERIC_DATA_ARGS =
   626 sig
   627   type T
   628   val empty: T
   629   val extend: T -> T
   630   val merge: Pretty.pp -> T * T -> T
   631 end;
   632 
   633 signature GENERIC_DATA =
   634 sig
   635   type T
   636   val get: Context.generic -> T
   637   val put: T -> Context.generic -> Context.generic
   638   val map: (T -> T) -> Context.generic -> Context.generic
   639 end;
   640 
   641 functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   642 struct
   643 
   644 structure ThyData = TheoryDataFun(open Data val copy = I);
   645 structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
   646 
   647 type T = Data.T;
   648 
   649 fun get (Context.Theory thy) = ThyData.get thy
   650   | get (Context.Proof prf) = PrfData.get prf;
   651 
   652 fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
   653   | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
   654 
   655 fun map f ctxt = put (f (get ctxt)) ctxt;
   656 
   657 end;
   658 
   659 (*hide private interface*)
   660 structure Context: CONTEXT = Context;
   661 
   662 (*fake predeclarations*)
   663 structure Proof = struct type context = Context.proof end;
   664 structure ProofContext =
   665 struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;