src/Pure/context.ML
author wenzelm
Wed Mar 26 22:40:03 2008 +0100 (2008-03-26)
changeset 26413 003dd6155870
parent 24559 dae0972c0066
child 26421 3dfb36923a56
permissions -rw-r--r--
added thread data (formerly global ref in ML/ml_context.ML);
renamed ML_Context.>> to Context.>> (again);
     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 ProtoPureN: string
    26   val PureN: string
    27   val CPureN: string
    28   val draftN: string
    29   val exists_name: string -> theory -> bool
    30   val names_of: theory -> string list
    31   val pretty_thy: theory -> Pretty.T
    32   val string_of_thy: theory -> string
    33   val pprint_thy: theory -> pprint_args -> unit
    34   val pretty_abbrev_thy: theory -> Pretty.T
    35   val str_of_thy: theory -> string
    36   val deref: theory_ref -> theory
    37   val check_thy: theory -> theory_ref
    38   val eq_thy: theory * theory -> bool
    39   val subthy: theory * theory -> bool
    40   val joinable: theory * theory -> bool
    41   val merge: theory * theory -> theory
    42   val merge_refs: theory_ref * theory_ref -> theory_ref
    43   val copy_thy: theory -> theory
    44   val checkpoint_thy: theory -> theory
    45   val finish_thy: theory -> theory
    46   val pre_pure_thy: theory
    47   val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    48   (*proof context*)
    49   type proof
    50   val theory_of_proof: proof -> theory
    51   val transfer_proof: theory -> proof -> proof
    52   val init_proof: theory -> proof
    53   (*generic context*)
    54   datatype generic = Theory of theory | Proof of proof
    55   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    56   val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
    57   val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
    58   val the_theory: generic -> theory
    59   val the_proof: generic -> proof
    60   val map_theory: (theory -> theory) -> generic -> generic
    61   val map_proof: (proof -> proof) -> generic -> 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 >> : (theory -> theory) -> unit
    72   (*delayed setup*)
    73   val add_setup: (theory -> theory) -> unit
    74   val setup: unit -> theory -> theory
    75 end;
    76 
    77 signature PRIVATE_CONTEXT =
    78 sig
    79   include CONTEXT
    80   structure TheoryData:
    81   sig
    82     val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    83       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    84     val get: serial -> (Object.T -> 'a) -> theory -> 'a
    85     val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    86   end
    87   structure ProofData:
    88   sig
    89     val declare: (theory -> Object.T) -> serial
    90     val get: serial -> (Object.T -> 'a) -> proof -> 'a
    91     val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
    92   end
    93 end;
    94 
    95 structure Context: PRIVATE_CONTEXT =
    96 struct
    97 
    98 (*** theory context ***)
    99 
   100 (** theory data **)
   101 
   102 (* data kinds and access methods *)
   103 
   104 (*private copy avoids potential conflict of table exceptions*)
   105 structure Datatab = TableFun(type key = int val ord = int_ord);
   106 
   107 local
   108 
   109 type kind =
   110  {empty: Object.T,
   111   copy: Object.T -> Object.T,
   112   extend: Object.T -> Object.T,
   113   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   114 
   115 val kinds = ref (Datatab.empty: kind Datatab.table);
   116 
   117 fun invoke f k =
   118   (case Datatab.lookup (! kinds) k of
   119     SOME kind => f kind
   120   | NONE => sys_error "Invalid theory data identifier");
   121 
   122 in
   123 
   124 fun invoke_empty k   = invoke (K o #empty) k ();
   125 val invoke_copy      = invoke #copy;
   126 val invoke_extend    = invoke #extend;
   127 fun invoke_merge pp  = invoke (fn kind => #merge kind pp);
   128 
   129 fun declare_theory_data empty copy extend merge =
   130   let
   131     val k = serial ();
   132     val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
   133     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
   134   in k end;
   135 
   136 val copy_data = Datatab.map' invoke_copy;
   137 val extend_data = Datatab.map' invoke_extend;
   138 fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
   139 
   140 end;
   141 
   142 
   143 
   144 (** datatype theory **)
   145 
   146 datatype theory =
   147   Theory of
   148    (*identity*)
   149    {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
   150     id: serial * string,                (*identifier of this theory*)
   151     ids: string Inttab.table,           (*identifiers of ancestors*)
   152     iids: string Inttab.table} *        (*identifiers of intermediate checkpoints*)
   153    (*data*)
   154    Object.T Datatab.table *
   155    (*ancestry*)
   156    {parents: theory list,               (*immediate predecessors*)
   157     ancestors: theory list} *           (*all predecessors*)
   158    (*history*)
   159    {name: string,                       (*prospective name of finished theory*)
   160     version: int,                       (*checkpoint counter*)
   161     intermediates: theory list};        (*intermediate checkpoints*)
   162 
   163 exception THEORY of string * theory list;
   164 
   165 fun rep_theory (Theory args) = args;
   166 
   167 val identity_of = #1 o rep_theory;
   168 val data_of     = #2 o rep_theory;
   169 val ancestry_of = #3 o rep_theory;
   170 val history_of  = #4 o rep_theory;
   171 
   172 fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   173 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   174 fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   175 
   176 val the_self = the o #self o identity_of;
   177 val parents_of = #parents o ancestry_of;
   178 val ancestors_of = #ancestors o ancestry_of;
   179 val theory_name = #name o history_of;
   180 
   181 
   182 (* staleness *)
   183 
   184 fun eq_id ((i: int, _), (j, _)) = (i = j);
   185 
   186 fun is_stale
   187     (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
   188       not (eq_id (id, id'))
   189   | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   190 
   191 fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   192   | vitalize (thy as Theory ({self = NONE, id, ids, iids}, data, ancestry, history)) =
   193       let
   194         val r = ref thy;
   195         val thy' = Theory (make_identity (SOME r) id ids iids, data, ancestry, history);
   196       in r := thy'; thy' end;
   197 
   198 
   199 (* names *)
   200 
   201 val ProtoPureN = "ProtoPure";
   202 val PureN = "Pure";
   203 val CPureN = "CPure";
   204 
   205 val draftN = "#";
   206 fun draft_id (_, name) = (name = draftN);
   207 val is_draft = draft_id o #id o identity_of;
   208 
   209 fun exists_name name (thy as Theory ({id, ids, iids, ...}, _, _, _)) =
   210   name = theory_name thy orelse
   211   name = #2 id orelse
   212   Inttab.exists (equal name o #2) ids orelse
   213   Inttab.exists (equal name o #2) iids;
   214 
   215 fun names_of (Theory ({id, ids, iids, ...}, _, _, _)) =
   216   rev (#2 id :: Inttab.fold (cons o #2) iids (Inttab.fold (cons o #2) ids []));
   217 
   218 fun pretty_thy thy =
   219   Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   220 
   221 val string_of_thy = Pretty.string_of o pretty_thy;
   222 val pprint_thy = Pretty.pprint o pretty_thy;
   223 
   224 fun pretty_abbrev_thy thy =
   225   let
   226     val names = names_of thy;
   227     val n = length names;
   228     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   229   in Pretty.str_list "{" "}" abbrev end;
   230 
   231 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   232 
   233 
   234 (* theory references *)
   235 
   236 (*theory_ref provides a safe way to store dynamic references to a
   237   theory in external data structures -- a plain theory value would
   238   become stale as the self reference moves on*)
   239 
   240 datatype theory_ref = TheoryRef of theory ref;
   241 
   242 fun deref (TheoryRef (ref thy)) = thy;
   243 
   244 fun check_thy thy =  (*thread-safe version*)
   245   let val thy_ref = TheoryRef (the_self thy) in
   246     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   247     else thy_ref
   248   end;
   249 
   250 
   251 (* consistency *)
   252 
   253 fun check_ins id ids =
   254   if draft_id id orelse Inttab.defined ids (#1 id) then ids
   255   else if Inttab.exists (equal (#2 id) o #2) ids then
   256     error ("Different versions of theory component " ^ quote (#2 id))
   257   else Inttab.update id ids;
   258 
   259 fun check_insert intermediate id (ids, iids) =
   260   let val ids' = check_ins id ids and iids' = check_ins id iids
   261   in if intermediate then (ids, iids') else (ids', iids) end;
   262 
   263 fun check_merge
   264     (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
   265     (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
   266   (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
   267   |> check_insert (#version history1 > 0) id1
   268   |> check_insert (#version history2 > 0) id2;
   269 
   270 
   271 (* equality and inclusion *)
   272 
   273 val eq_thy = eq_id o pairself (#id o identity_of);
   274 
   275 fun proper_subthy
   276     (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
   277   Inttab.defined ids i orelse Inttab.defined iids i;
   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 (* trivial merge *)
   285 
   286 fun merge (thy1, thy2) =
   287   if eq_thy (thy1, thy2) then thy1
   288   else if proper_subthy (thy2, thy1) then thy1
   289   else if proper_subthy (thy1, thy2) then thy2
   290   else (check_merge thy1 thy2;
   291     error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   292       str_of_thy thy1, str_of_thy thy2]));
   293 
   294 fun merge_refs (ref1, ref2) =
   295   if ref1 = ref2 then ref1
   296   else check_thy (merge (deref ref1, deref ref2));
   297 
   298 
   299 
   300 (** build theories **)
   301 
   302 (* primitives *)
   303 
   304 fun create_thy name self id ids iids data ancestry history =
   305   let
   306     val {version, name = _, intermediates = _} = history;
   307     val intermediate = version > 0;
   308     val (ids', iids') = check_insert intermediate id (ids, iids);
   309     val id' = (serial (), name);
   310     val _ = check_insert intermediate id' (ids', iids');
   311     val identity' = make_identity self id' ids' iids';
   312   in vitalize (Theory (identity', data, ancestry, history)) end;
   313 
   314 fun change_thy name f thy =
   315   let
   316     val Theory ({self, id, ids, iids}, data, ancestry, history) = thy;
   317     val (self', data', ancestry') =
   318       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   319       else if #version history > 0
   320       then (NONE, copy_data data, ancestry)
   321       else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   322     val data'' = f data';
   323     val thy' = NAMED_CRITICAL "theory" (fn () =>
   324       (check_thy thy; create_thy name self' id ids iids data'' ancestry' history));
   325   in thy' end;
   326 
   327 fun name_thy name = change_thy name I;
   328 val modify_thy = change_thy draftN;
   329 val extend_thy = modify_thy I;
   330 
   331 fun copy_thy thy =
   332   let
   333     val Theory ({id, ids, iids, ...}, data, ancestry, history) = thy;
   334     val data' = copy_data data;
   335     val thy' = NAMED_CRITICAL "theory" (fn () =>
   336       (check_thy thy; create_thy draftN NONE id ids iids data' ancestry history));
   337   in thy' end;
   338 
   339 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   340   Datatab.empty (make_ancestry [] []) (make_history ProtoPureN 0 []);
   341 
   342 
   343 (* named theory nodes *)
   344 
   345 fun merge_thys pp (thy1, thy2) =
   346   if exists_name CPureN thy1 <> exists_name CPureN thy2 then
   347     error "Cannot merge Pure and CPure developments"
   348   else
   349     let
   350       val (ids, iids) = check_merge thy1 thy2;
   351       val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   352       val ancestry = make_ancestry [] [];
   353       val history = make_history "" 0 [];
   354       val thy' = NAMED_CRITICAL "theory" (fn () =>
   355        (check_thy thy1; check_thy thy2;
   356         create_thy draftN NONE (serial (), draftN) ids iids data ancestry history))
   357     in thy' end;
   358 
   359 fun maximal_thys thys =
   360   thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
   361 
   362 fun begin_thy pp name imports =
   363   if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   364   else
   365     let
   366       val parents = maximal_thys (distinct eq_thy imports);
   367       val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
   368       val Theory ({id, ids, iids, ...}, data, _, _) =
   369         (case parents of
   370           [] => error "No parent theories"
   371         | [thy] => extend_thy thy
   372         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   373       val ancestry = make_ancestry parents ancestors;
   374       val history = make_history name 0 [];
   375       val thy' = NAMED_CRITICAL "theory" (fn () =>
   376         (map check_thy imports; create_thy draftN NONE id ids iids data ancestry history));
   377     in thy' end;
   378 
   379 
   380 (* undoable checkpoints *)
   381 
   382 fun checkpoint_thy thy =
   383   if not (is_draft thy) then thy
   384   else
   385     let
   386       val {name, version, intermediates} = history_of thy;
   387       val thy' as Theory (identity', data', ancestry', _) =
   388         name_thy (name ^ ":" ^ string_of_int version) thy;
   389       val history' = make_history name (version + 1) (thy' :: intermediates);
   390       val thy'' = NAMED_CRITICAL "theory" (fn () =>
   391         (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   392     in thy'' end;
   393 
   394 fun finish_thy thy = NAMED_CRITICAL "theory" (fn () =>
   395   let
   396     val {name, version, intermediates} = history_of thy;
   397     val rs = map ((fn TheoryRef r => r) o check_thy) intermediates;
   398     val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
   399     val identity' = make_identity self id ids Inttab.empty;
   400     val history' = make_history name 0 [];
   401     val thy'' = vitalize (Theory (identity', data', ancestry', history'));
   402     val _ = List.app (fn r => r := thy'') rs;
   403   in thy'' end);
   404 
   405 
   406 (* theory data *)
   407 
   408 structure TheoryData =
   409 struct
   410 
   411 val declare = declare_theory_data;
   412 
   413 fun get k dest thy =
   414   dest ((case Datatab.lookup (data_of thy) k of
   415     SOME x => x
   416   | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
   417 
   418 fun put k mk x = modify_thy (Datatab.update (k, mk x));
   419 
   420 end;
   421 
   422 
   423 
   424 (*** proof context ***)
   425 
   426 (* datatype proof *)
   427 
   428 datatype proof = Prf of Object.T Datatab.table * theory_ref;
   429 
   430 fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
   431 fun data_of_proof (Prf (data, _)) = data;
   432 fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
   433 
   434 
   435 (* proof data kinds *)
   436 
   437 local
   438 
   439 val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
   440 
   441 fun invoke_init k =
   442   (case Datatab.lookup (! kinds) k of
   443     SOME init => init
   444   | NONE => sys_error "Invalid proof data identifier");
   445 
   446 fun init_data thy =
   447   Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
   448 
   449 fun init_new_data data thy =
   450   Datatab.merge (K true) (data, init_data thy);
   451 
   452 in
   453 
   454 fun init_proof thy = Prf (init_data thy, check_thy thy);
   455 
   456 fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
   457   let
   458     val thy = deref thy_ref;
   459     val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
   460     val _ = check_thy thy;
   461     val data' = init_new_data data thy';
   462     val thy_ref' = check_thy thy';
   463   in Prf (data', thy_ref') end;
   464 
   465 
   466 structure ProofData =
   467 struct
   468 
   469 fun declare init =
   470   let
   471     val k = serial ();
   472     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
   473   in k end;
   474 
   475 fun get k dest prf =
   476   dest (case Datatab.lookup (data_of_proof prf) k of
   477     SOME x => x
   478   | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
   479 
   480 fun put k mk x = map_prf (Datatab.update (k, mk x));
   481 
   482 end;
   483 
   484 end;
   485 
   486 
   487 
   488 (*** generic context ***)
   489 
   490 datatype generic = Theory of theory | Proof of proof;
   491 
   492 fun cases f _ (Theory thy) = f thy
   493   | cases _ g (Proof prf) = g prf;
   494 
   495 fun mapping f g = cases (Theory o f) (Proof o g);
   496 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   497 
   498 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   499 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   500 
   501 fun map_theory f = Theory o f o the_theory;
   502 fun map_proof f = 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 fun >> f = set_thread_data (SOME (map_theory f (the_thread_data ())));
   530 
   531 end;
   532 
   533 
   534 
   535 (** delayed theory setup **)
   536 
   537 local
   538   val setup_fn = ref (I: theory -> theory);
   539 in
   540   fun add_setup f = CRITICAL (fn () => setup_fn := (! setup_fn #> f));
   541   fun setup () = CRITICAL (fn () => let val f = ! setup_fn in setup_fn := I; f end);
   542 end;
   543 
   544 end;
   545 
   546 structure BasicContext: BASIC_CONTEXT = Context;
   547 open BasicContext;
   548 
   549 
   550 
   551 (*** type-safe interfaces for data declarations ***)
   552 
   553 (** theory data **)
   554 
   555 signature THEORY_DATA_ARGS =
   556 sig
   557   type T
   558   val empty: T
   559   val copy: T -> T
   560   val extend: T -> T
   561   val merge: Pretty.pp -> T * T -> T
   562 end;
   563 
   564 signature THEORY_DATA =
   565 sig
   566   type T
   567   val get: theory -> T
   568   val put: T -> theory -> theory
   569   val map: (T -> T) -> theory -> theory
   570   val init: theory -> theory
   571 end;
   572 
   573 functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
   574 struct
   575 
   576 structure TheoryData = Context.TheoryData;
   577 
   578 type T = Data.T;
   579 exception Data of T;
   580 
   581 val kind = TheoryData.declare
   582   (Data Data.empty)
   583   (fn Data x => Data (Data.copy x))
   584   (fn Data x => Data (Data.extend x))
   585   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   586 
   587 val get = TheoryData.get kind (fn Data x => x);
   588 val put = TheoryData.put kind Data;
   589 fun map f thy = put (f (get thy)) thy;
   590 
   591 fun init thy = map I thy;
   592 
   593 end;
   594 
   595 
   596 
   597 (** proof data **)
   598 
   599 signature PROOF_DATA_ARGS =
   600 sig
   601   type T
   602   val init: theory -> T
   603 end;
   604 
   605 signature PROOF_DATA =
   606 sig
   607   type T
   608   val get: Context.proof -> T
   609   val put: T -> Context.proof -> Context.proof
   610   val map: (T -> T) -> Context.proof -> Context.proof
   611 end;
   612 
   613 functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
   614 struct
   615 
   616 structure ProofData = Context.ProofData;
   617 
   618 type T = Data.T;
   619 exception Data of T;
   620 
   621 val kind = ProofData.declare (Data o Data.init);
   622 
   623 val get = ProofData.get kind (fn Data x => x);
   624 val put = ProofData.put kind Data;
   625 fun map f prf = put (f (get prf)) prf;
   626 
   627 end;
   628 
   629 
   630 
   631 (** generic data **)
   632 
   633 signature GENERIC_DATA_ARGS =
   634 sig
   635   type T
   636   val empty: T
   637   val extend: T -> T
   638   val merge: Pretty.pp -> T * T -> T
   639 end;
   640 
   641 signature GENERIC_DATA =
   642 sig
   643   type T
   644   val get: Context.generic -> T
   645   val put: T -> Context.generic -> Context.generic
   646   val map: (T -> T) -> Context.generic -> Context.generic
   647 end;
   648 
   649 functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   650 struct
   651 
   652 structure ThyData = TheoryDataFun(open Data val copy = I);
   653 structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
   654 
   655 type T = Data.T;
   656 
   657 fun get (Context.Theory thy) = ThyData.get thy
   658   | get (Context.Proof prf) = PrfData.get prf;
   659 
   660 fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
   661   | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
   662 
   663 fun map f ctxt = put (f (get ctxt)) ctxt;
   664 
   665 end;
   666 
   667 (*hide private interface*)
   668 structure Context: CONTEXT = Context;
   669 
   670 (*fake predeclarations*)
   671 structure Proof = struct type context = Context.proof end;
   672 structure ProofContext =
   673 struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;