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