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