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