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