src/Pure/context.ML
author wenzelm
Wed May 14 14:43:37 2008 +0200 (2008-05-14)
changeset 26889 ccea41fb5c39
parent 26623 81547c8d51f8
child 26957 e3f04fdd994d
permissions -rw-r--r--
names_of: exclude intermediate ids -- less verbosity;
     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 is_draft: theory -> bool
    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 map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    61   val map_proof_result: (proof -> 'a * proof) -> generic -> 'a * generic
    62   val theory_map: (generic -> generic) -> theory -> theory
    63   val proof_map: (generic -> generic) -> proof -> proof
    64   val theory_of: generic -> theory   (*total*)
    65   val proof_of: generic -> proof     (*total*)
    66   (*thread data*)
    67   val thread_data: unit -> generic option
    68   val the_thread_data: unit -> generic
    69   val set_thread_data: generic option -> unit
    70   val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
    71   val >> : (generic -> generic) -> unit
    72   val >>> : (generic -> 'a * generic) -> 'a
    73 end;
    74 
    75 signature PRIVATE_CONTEXT =
    76 sig
    77   include CONTEXT
    78   structure TheoryData:
    79   sig
    80     val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    81       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    82     val get: serial -> (Object.T -> 'a) -> theory -> 'a
    83     val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    84   end
    85   structure ProofData:
    86   sig
    87     val declare: (theory -> Object.T) -> serial
    88     val get: serial -> (Object.T -> 'a) -> proof -> 'a
    89     val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
    90   end
    91 end;
    92 
    93 structure Context: PRIVATE_CONTEXT =
    94 struct
    95 
    96 (*** theory context ***)
    97 
    98 (** theory data **)
    99 
   100 (* data kinds and access methods *)
   101 
   102 (*private copy avoids potential conflict of table exceptions*)
   103 structure Datatab = TableFun(type key = int val ord = int_ord);
   104 
   105 local
   106 
   107 type kind =
   108  {empty: Object.T,
   109   copy: Object.T -> Object.T,
   110   extend: Object.T -> Object.T,
   111   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   112 
   113 val kinds = ref (Datatab.empty: kind Datatab.table);
   114 
   115 fun invoke f k =
   116   (case Datatab.lookup (! kinds) k of
   117     SOME kind => f kind
   118   | NONE => sys_error "Invalid theory data identifier");
   119 
   120 in
   121 
   122 fun invoke_empty k   = invoke (K o #empty) k ();
   123 val invoke_copy      = invoke #copy;
   124 val invoke_extend    = invoke #extend;
   125 fun invoke_merge pp  = invoke (fn kind => #merge kind pp);
   126 
   127 fun declare_theory_data empty copy extend merge =
   128   let
   129     val k = serial ();
   130     val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
   131     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
   132   in k end;
   133 
   134 val copy_data = Datatab.map' invoke_copy;
   135 val extend_data = Datatab.map' invoke_extend;
   136 fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
   137 
   138 end;
   139 
   140 
   141 
   142 (** datatype theory **)
   143 
   144 datatype theory =
   145   Theory of
   146    (*identity*)
   147    {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
   148     id: serial * string,                (*identifier of this theory*)
   149     ids: string Inttab.table,           (*identifiers of ancestors*)
   150     iids: string Inttab.table} *        (*identifiers of intermediate checkpoints*)
   151    (*data*)
   152    Object.T Datatab.table *
   153    (*ancestry*)
   154    {parents: theory list,               (*immediate predecessors*)
   155     ancestors: theory list} *           (*all predecessors*)
   156    (*history*)
   157    {name: string,                       (*prospective name of finished theory*)
   158     version: int,                       (*checkpoint counter*)
   159     intermediates: theory list};        (*intermediate checkpoints*)
   160 
   161 exception THEORY of string * theory list;
   162 
   163 fun rep_theory (Theory args) = args;
   164 
   165 val identity_of = #1 o rep_theory;
   166 val data_of     = #2 o rep_theory;
   167 val ancestry_of = #3 o rep_theory;
   168 val history_of  = #4 o rep_theory;
   169 
   170 fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   171 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   172 fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   173 
   174 val the_self = the o #self o identity_of;
   175 val parents_of = #parents o ancestry_of;
   176 val ancestors_of = #ancestors o ancestry_of;
   177 val theory_name = #name o history_of;
   178 
   179 
   180 (* staleness *)
   181 
   182 fun eq_id ((i: int, _), (j, _)) = (i = j);
   183 
   184 fun is_stale
   185     (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
   186       not (eq_id (id, id'))
   187   | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   188 
   189 fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   190   | vitalize (thy as Theory ({self = NONE, id, ids, iids}, data, ancestry, history)) =
   191       let
   192         val r = ref thy;
   193         val thy' = Theory (make_identity (SOME r) id ids iids, data, ancestry, history);
   194       in r := thy'; thy' end;
   195 
   196 
   197 (* names *)
   198 
   199 val PureN = "Pure";
   200 val CPureN = "CPure";
   201 
   202 val draftN = "#";
   203 fun draft_id (_, name) = (name = draftN);
   204 val is_draft = draft_id o #id o identity_of;
   205 
   206 fun exists_name name (thy as Theory ({id, ids, iids, ...}, _, _, _)) =
   207   name = theory_name thy orelse
   208   name = #2 id orelse
   209   Inttab.exists (equal name o #2) ids orelse
   210   Inttab.exists (equal name o #2) iids;
   211 
   212 fun names_of (Theory ({id, ids, ...}, _, _, _)) =
   213   rev (#2 id :: Inttab.fold (cons o #2) ids []);
   214 
   215 fun pretty_thy thy =
   216   Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   217 
   218 val string_of_thy = Pretty.string_of o pretty_thy;
   219 val pprint_thy = Pretty.pprint o pretty_thy;
   220 
   221 fun pretty_abbrev_thy thy =
   222   let
   223     val names = names_of thy;
   224     val n = length names;
   225     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   226   in Pretty.str_list "{" "}" abbrev end;
   227 
   228 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   229 
   230 
   231 (* theory references *)
   232 
   233 (*theory_ref provides a safe way to store dynamic references to a
   234   theory in external data structures -- a plain theory value would
   235   become stale as the self reference moves on*)
   236 
   237 datatype theory_ref = TheoryRef of theory ref;
   238 
   239 fun deref (TheoryRef (ref thy)) = thy;
   240 
   241 fun check_thy thy =  (*thread-safe version*)
   242   let val thy_ref = TheoryRef (the_self thy) in
   243     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   244     else thy_ref
   245   end;
   246 
   247 
   248 (* consistency *)
   249 
   250 fun check_ins id ids =
   251   if draft_id id orelse Inttab.defined ids (#1 id) then ids
   252   else if Inttab.exists (equal (#2 id) o #2) ids then
   253     error ("Different versions of theory component " ^ quote (#2 id))
   254   else Inttab.update id ids;
   255 
   256 fun check_insert intermediate id (ids, iids) =
   257   let val ids' = check_ins id ids and iids' = check_ins id iids
   258   in if intermediate then (ids, iids') else (ids', iids) end;
   259 
   260 fun check_merge
   261     (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
   262     (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
   263   (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
   264   |> check_insert (#version history1 > 0) id1
   265   |> check_insert (#version history2 > 0) id2;
   266 
   267 
   268 (* equality and inclusion *)
   269 
   270 val eq_thy = eq_id o pairself (#id o identity_of);
   271 
   272 fun proper_subthy
   273     (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
   274   Inttab.defined ids i orelse Inttab.defined iids i;
   275 
   276 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   277 
   278 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   279 
   280 
   281 (* trivial merge *)
   282 
   283 fun merge (thy1, thy2) =
   284   if eq_thy (thy1, thy2) then thy1
   285   else if proper_subthy (thy2, thy1) then thy1
   286   else if proper_subthy (thy1, thy2) then thy2
   287   else (check_merge thy1 thy2;
   288     error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   289       str_of_thy thy1, str_of_thy thy2]));
   290 
   291 fun merge_refs (ref1, ref2) =
   292   if ref1 = ref2 then ref1
   293   else check_thy (merge (deref ref1, deref ref2));
   294 
   295 
   296 
   297 (** build theories **)
   298 
   299 (* primitives *)
   300 
   301 fun create_thy name self id ids iids data ancestry history =
   302   let
   303     val {version, name = _, intermediates = _} = history;
   304     val intermediate = version > 0;
   305     val (ids', iids') = check_insert intermediate id (ids, iids);
   306     val id' = (serial (), name);
   307     val _ = check_insert intermediate id' (ids', iids');
   308     val identity' = make_identity self id' ids' iids';
   309   in vitalize (Theory (identity', data, ancestry, history)) end;
   310 
   311 fun change_thy name f thy =
   312   let
   313     val Theory ({self, id, ids, iids}, data, ancestry, history) = thy;
   314     val (self', data', ancestry') =
   315       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   316       else if #version history > 0
   317       then (NONE, copy_data data, ancestry)
   318       else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   319     val data'' = f data';
   320     val thy' = NAMED_CRITICAL "theory" (fn () =>
   321       (check_thy thy; create_thy name self' id ids iids data'' ancestry' history));
   322   in thy' end;
   323 
   324 fun name_thy name = change_thy name I;
   325 val modify_thy = change_thy draftN;
   326 val extend_thy = modify_thy I;
   327 
   328 fun copy_thy thy =
   329   let
   330     val Theory ({id, ids, iids, ...}, data, ancestry, history) = thy;
   331     val data' = copy_data data;
   332     val thy' = NAMED_CRITICAL "theory" (fn () =>
   333       (check_thy thy; create_thy draftN NONE id ids iids data' ancestry history));
   334   in thy' end;
   335 
   336 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   337   Datatab.empty (make_ancestry [] []) (make_history PureN 0 []);
   338 
   339 
   340 (* named theory nodes *)
   341 
   342 fun merge_thys pp (thy1, thy2) =
   343   if exists_name CPureN thy1 <> exists_name CPureN thy2 then
   344     error "Cannot merge Pure and CPure developments"
   345   else
   346     let
   347       val (ids, iids) = check_merge thy1 thy2;
   348       val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   349       val ancestry = make_ancestry [] [];
   350       val history = make_history "" 0 [];
   351       val thy' = NAMED_CRITICAL "theory" (fn () =>
   352        (check_thy thy1; check_thy thy2;
   353         create_thy draftN NONE (serial (), draftN) ids iids data ancestry history))
   354     in thy' end;
   355 
   356 fun maximal_thys thys =
   357   thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
   358 
   359 fun begin_thy pp name imports =
   360   if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   361   else
   362     let
   363       val parents = maximal_thys (distinct eq_thy imports);
   364       val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
   365       val Theory ({id, ids, iids, ...}, data, _, _) =
   366         (case parents of
   367           [] => error "No parent theories"
   368         | [thy] => extend_thy thy
   369         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   370       val ancestry = make_ancestry parents ancestors;
   371       val history = make_history name 0 [];
   372       val thy' = NAMED_CRITICAL "theory" (fn () =>
   373         (map check_thy imports; create_thy draftN NONE id ids iids data ancestry history));
   374     in thy' end;
   375 
   376 
   377 (* undoable checkpoints *)
   378 
   379 fun checkpoint_thy thy =
   380   if not (is_draft thy) then thy
   381   else
   382     let
   383       val {name, version, intermediates} = history_of thy;
   384       val thy' as Theory (identity', data', ancestry', _) =
   385         name_thy (name ^ ":" ^ string_of_int version) thy;
   386       val history' = make_history name (version + 1) (thy' :: intermediates);
   387       val thy'' = NAMED_CRITICAL "theory" (fn () =>
   388         (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   389     in thy'' end;
   390 
   391 fun finish_thy thy = NAMED_CRITICAL "theory" (fn () =>
   392   let
   393     val {name, version, intermediates} = history_of thy;
   394     val rs = map ((fn TheoryRef r => r) o check_thy) intermediates;
   395     val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
   396     val identity' = make_identity self id ids Inttab.empty;
   397     val history' = make_history name 0 [];
   398     val thy'' = vitalize (Theory (identity', data', ancestry', history'));
   399     val _ = List.app (fn r => r := thy'') rs;
   400   in thy'' end);
   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 Multithreading.get_data 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 = Multithreading.put_data (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;