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