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