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