src/Pure/context.ML
author wenzelm
Fri Sep 26 19:07:56 2008 +0200 (2008-09-26)
changeset 28375 c879d88d038a
parent 28317 83c4fc383409
child 28617 c9c1c8b28a62
permissions -rw-r--r--
eliminated polymorphic equality;
     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 is_draft: theory -> bool
    27   val reject_draft: theory -> theory
    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 pprint_thy_ref: theory_ref -> 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 begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    47   (*proof context*)
    48   type proof
    49   val theory_of_proof: proof -> theory
    50   val transfer_proof: theory -> proof -> proof
    51   val init_proof: theory -> proof
    52   (*generic context*)
    53   datatype generic = Theory of theory | Proof of proof
    54   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    55   val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
    56   val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
    57   val the_theory: generic -> theory
    58   val the_proof: generic -> proof
    59   val map_theory: (theory -> theory) -> generic -> generic
    60   val map_proof: (proof -> proof) -> generic -> generic
    61   val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    62   val map_proof_result: (proof -> 'a * proof) -> generic -> 'a * generic
    63   val theory_map: (generic -> generic) -> theory -> theory
    64   val proof_map: (generic -> generic) -> proof -> proof
    65   val theory_of: generic -> theory   (*total*)
    66   val proof_of: generic -> proof     (*total*)
    67   (*thread data*)
    68   val thread_data: unit -> generic option
    69   val the_thread_data: unit -> generic
    70   val set_thread_data: generic option -> unit
    71   val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
    72   val >> : (generic -> generic) -> unit
    73   val >>> : (generic -> 'a * generic) -> 'a
    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 
   202 val draftN = "#";
   203 fun draft_id (_, name) = (name = draftN);
   204 val is_draft = draft_id o #id o identity_of;
   205 
   206 fun reject_draft thy =
   207   if is_draft thy then raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
   208   else thy;
   209 
   210 fun exists_name name (thy as Theory ({id, ids, iids, ...}, _, _, _)) =
   211   name = theory_name thy orelse
   212   name = #2 id orelse
   213   Inttab.exists (fn (_, a) => a = name) ids orelse
   214   Inttab.exists (fn (_, a) => a = name) iids;
   215 
   216 fun names_of (Theory ({id, ids, ...}, _, _, _)) =
   217   rev (#2 id :: Inttab.fold (cons o #2) ids []);
   218 
   219 fun pretty_thy thy =
   220   Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   221 
   222 val string_of_thy = Pretty.string_of o pretty_thy;
   223 val pprint_thy = Pretty.pprint o pretty_thy;
   224 
   225 fun pretty_abbrev_thy thy =
   226   let
   227     val names = names_of thy;
   228     val n = length names;
   229     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   230   in Pretty.str_list "{" "}" abbrev end;
   231 
   232 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   233 
   234 
   235 (* theory references *)
   236 
   237 (*theory_ref provides a safe way to store dynamic references to a
   238   theory in external data structures -- a plain theory value would
   239   become stale as the self reference moves on*)
   240 
   241 datatype theory_ref = TheoryRef of theory ref;
   242 
   243 fun deref (TheoryRef (ref thy)) = thy;
   244 
   245 fun check_thy thy =  (*thread-safe version*)
   246   let val thy_ref = TheoryRef (the_self thy) in
   247     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   248     else thy_ref
   249   end;
   250 
   251 val pprint_thy_ref = Pretty.pprint o pretty_thy o deref;
   252 
   253 
   254 (* consistency *)
   255 
   256 fun check_ins id ids =
   257   if draft_id id orelse Inttab.defined ids (#1 id) then ids
   258   else if Inttab.exists (fn (_, a) => a = #2 id) ids then
   259     error ("Different versions of theory component " ^ quote (#2 id))
   260   else Inttab.update id ids;
   261 
   262 fun check_insert intermediate id (ids, iids) =
   263   let val ids' = check_ins id ids and iids' = check_ins id iids
   264   in if intermediate then (ids, iids') else (ids', iids) end;
   265 
   266 fun check_merge
   267     (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
   268     (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
   269   (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
   270   |> check_insert (#version history1 > 0) id1
   271   |> check_insert (#version history2 > 0) id2;
   272 
   273 
   274 (* equality and inclusion *)
   275 
   276 val eq_thy = eq_id o pairself (#id o identity_of);
   277 
   278 fun proper_subthy (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
   279   Inttab.defined ids i orelse Inttab.defined iids i;
   280 
   281 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   282 
   283 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   284 
   285 
   286 (* trivial merge *)
   287 
   288 fun merge (thy1, thy2) =
   289   if eq_thy (thy1, thy2) then thy1
   290   else if proper_subthy (thy2, thy1) then thy1
   291   else if proper_subthy (thy1, thy2) then thy2
   292   else (check_merge thy1 thy2;
   293     error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   294       str_of_thy thy1, str_of_thy thy2]));
   295 
   296 fun merge_refs (ref1, ref2) =
   297   if ref1 = ref2 then ref1
   298   else check_thy (merge (deref ref1, deref ref2));
   299 
   300 
   301 
   302 (** build theories **)
   303 
   304 (* primitives *)
   305 
   306 fun create_thy name self id ids iids data ancestry history =
   307   let
   308     val {version, name = _, intermediates = _} = history;
   309     val intermediate = version > 0;
   310     val (ids', iids') = check_insert intermediate id (ids, iids);
   311     val id' = (serial (), name);
   312     val _ = check_insert intermediate id' (ids', iids');
   313     val identity' = make_identity self id' ids' iids';
   314   in vitalize (Theory (identity', data, ancestry, history)) end;
   315 
   316 fun change_thy name f thy =
   317   let
   318     val Theory ({self, id, ids, iids}, data, ancestry, history) = thy;
   319     val (self', data', ancestry') =
   320       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   321       else if #version history > 0
   322       then (NONE, copy_data data, ancestry)
   323       else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   324     val data'' = f data';
   325     val thy' = NAMED_CRITICAL "theory" (fn () =>
   326       (check_thy thy; create_thy name self' id ids iids data'' ancestry' history));
   327   in thy' end;
   328 
   329 fun name_thy name = change_thy name I;
   330 val modify_thy = change_thy draftN;
   331 val extend_thy = modify_thy I;
   332 
   333 fun copy_thy thy =
   334   let
   335     val Theory ({id, ids, iids, ...}, data, ancestry, history) = thy;
   336     val data' = copy_data data;
   337     val thy' = NAMED_CRITICAL "theory" (fn () =>
   338       (check_thy thy; create_thy draftN NONE id ids iids data' ancestry history));
   339   in thy' end;
   340 
   341 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   342   Datatab.empty (make_ancestry [] []) (make_history PureN 0 []);
   343 
   344 
   345 (* named theory nodes *)
   346 
   347 fun merge_thys pp (thy1, thy2) =
   348   let
   349     val (ids, iids) = check_merge thy1 thy2;
   350     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   351     val ancestry = make_ancestry [] [];
   352     val history = make_history "" 0 [];
   353     val thy' = NAMED_CRITICAL "theory" (fn () =>
   354      (check_thy thy1; check_thy thy2;
   355       create_thy draftN NONE (serial (), draftN) ids iids data ancestry history))
   356   in thy' end;
   357 
   358 fun maximal_thys thys =
   359   thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
   360 
   361 fun begin_thy pp name imports =
   362   if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   363   else
   364     let
   365       val parents = maximal_thys (distinct eq_thy imports);
   366       val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
   367       val Theory ({id, ids, iids, ...}, data, _, _) =
   368         (case parents of
   369           [] => error "No parent theories"
   370         | [thy] => extend_thy thy
   371         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   372       val ancestry = make_ancestry parents ancestors;
   373       val history = make_history name 0 [];
   374       val thy' = NAMED_CRITICAL "theory" (fn () =>
   375         (map check_thy imports; create_thy draftN NONE id ids iids data ancestry history));
   376     in thy' end;
   377 
   378 
   379 (* undoable checkpoints *)
   380 
   381 fun checkpoint_thy thy =
   382   if not (is_draft thy) then thy
   383   else
   384     let
   385       val {name, version, intermediates} = history_of thy;
   386       val thy' as Theory (identity', data', ancestry', _) =
   387         name_thy (name ^ ":" ^ string_of_int version) thy;
   388       val history' = make_history name (version + 1) (thy' :: intermediates);
   389       val thy'' = NAMED_CRITICAL "theory" (fn () =>
   390         (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   391     in thy'' end;
   392 
   393 fun finish_thy thy = NAMED_CRITICAL "theory" (fn () =>
   394   let
   395     val {name, version, intermediates} = history_of thy;
   396     val rs = map ((fn TheoryRef r => r) o check_thy) intermediates;
   397     val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
   398     val identity' = make_identity self id ids Inttab.empty;
   399     val history' = make_history name 0 [];
   400     val thy'' = vitalize (Theory (identity', data', ancestry', history'));
   401     val _ = List.app (fn r => r := thy'') rs;
   402   in thy'' end);
   403 
   404 
   405 (* theory data *)
   406 
   407 structure TheoryData =
   408 struct
   409 
   410 val declare = declare_theory_data;
   411 
   412 fun get k dest thy =
   413   dest ((case Datatab.lookup (data_of thy) k of
   414     SOME x => x
   415   | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
   416 
   417 fun put k mk x = modify_thy (Datatab.update (k, mk x));
   418 
   419 end;
   420 
   421 
   422 
   423 (*** proof context ***)
   424 
   425 (* datatype proof *)
   426 
   427 datatype proof = Prf of Object.T Datatab.table * theory_ref;
   428 
   429 fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
   430 fun data_of_proof (Prf (data, _)) = data;
   431 fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
   432 
   433 
   434 (* proof data kinds *)
   435 
   436 local
   437 
   438 val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
   439 
   440 fun invoke_init k =
   441   (case Datatab.lookup (! kinds) k of
   442     SOME init => init
   443   | NONE => sys_error "Invalid proof data identifier");
   444 
   445 fun init_data thy =
   446   Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
   447 
   448 fun init_new_data data thy =
   449   Datatab.merge (K true) (data, init_data thy);
   450 
   451 in
   452 
   453 fun init_proof thy = Prf (init_data thy, check_thy thy);
   454 
   455 fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
   456   let
   457     val thy = deref thy_ref;
   458     val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
   459     val _ = check_thy thy;
   460     val data' = init_new_data data thy';
   461     val thy_ref' = check_thy thy';
   462   in Prf (data', thy_ref') end;
   463 
   464 
   465 structure ProofData =
   466 struct
   467 
   468 fun declare init =
   469   let
   470     val k = serial ();
   471     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
   472   in k end;
   473 
   474 fun get k dest prf =
   475   dest (case Datatab.lookup (data_of_proof prf) k of
   476     SOME x => x
   477   | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
   478 
   479 fun put k mk x = map_prf (Datatab.update (k, mk x));
   480 
   481 end;
   482 
   483 end;
   484 
   485 
   486 
   487 (*** generic context ***)
   488 
   489 datatype generic = Theory of theory | Proof of proof;
   490 
   491 fun cases f _ (Theory thy) = f thy
   492   | cases _ g (Proof prf) = g prf;
   493 
   494 fun mapping f g = cases (Theory o f) (Proof o g);
   495 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   496 
   497 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   498 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   499 
   500 fun map_theory f = Theory o f o the_theory;
   501 fun map_proof f = Proof o f o the_proof;
   502 
   503 fun map_theory_result f = apsnd Theory o f o the_theory;
   504 fun map_proof_result f = apsnd Proof o f o the_proof;
   505 
   506 fun theory_map f = the_theory o f o Theory;
   507 fun proof_map f = the_proof o f o Proof;
   508 
   509 val theory_of = cases I theory_of_proof;
   510 val proof_of = cases init_proof I;
   511 
   512 
   513 
   514 (** thread data **)
   515 
   516 local val tag = Universal.tag () : generic option Universal.tag in
   517 
   518 fun thread_data () =
   519   (case Thread.getLocal tag of
   520     SOME (SOME context) => SOME context
   521   | _ => NONE);
   522 
   523 fun the_thread_data () =
   524   (case thread_data () of
   525     SOME context => context
   526   | _ => error "Unknown context");
   527 
   528 fun set_thread_data context = Thread.setLocal (tag, context);
   529 fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
   530 
   531 end;
   532 
   533 fun >>> f =
   534   let
   535     val (res, context') = f (the_thread_data ());
   536     val _ = set_thread_data (SOME context');
   537   in res end;
   538 
   539 nonfix >>;
   540 fun >> f = >>> (fn context => ((), f context));
   541 
   542 val _ = set_thread_data (SOME (Theory pre_pure_thy));
   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;