src/Pure/context.ML
author wenzelm
Wed Jan 21 22:26:49 2009 +0100 (2009-01-21)
changeset 29605 f2924219125e
parent 29368 503ce3f8f092
child 30628 4078276bcace
permissions -rw-r--r--
eliminated obsolete var morphism;
     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 
     9 signature BASIC_CONTEXT =
    10 sig
    11   type theory
    12   type theory_ref
    13   exception THEORY of string * theory list
    14 end;
    15 
    16 signature CONTEXT =
    17 sig
    18   include BASIC_CONTEXT
    19   (*theory context*)
    20   val parents_of: theory -> theory list
    21   val ancestors_of: theory -> theory list
    22   val theory_name: theory -> string
    23   val is_stale: theory -> bool
    24   val is_draft: theory -> bool
    25   val reject_draft: theory -> theory
    26   val PureN: string
    27   val display_names: theory -> string list
    28   val pretty_thy: theory -> Pretty.T
    29   val string_of_thy: theory -> string
    30   val pprint_thy: theory -> pprint_args -> unit
    31   val pprint_thy_ref: theory_ref -> pprint_args -> unit
    32   val pretty_abbrev_thy: theory -> Pretty.T
    33   val str_of_thy: theory -> string
    34   val deref: theory_ref -> theory
    35   val check_thy: theory -> theory_ref
    36   val eq_thy: theory * theory -> bool
    37   val subthy: theory * theory -> bool
    38   val joinable: theory * theory -> bool
    39   val merge: theory * theory -> theory
    40   val merge_refs: theory_ref * theory_ref -> theory_ref
    41   val copy_thy: theory -> theory
    42   val checkpoint_thy: theory -> theory
    43   val finish_thy: theory -> theory
    44   val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    45   (*proof context*)
    46   type proof
    47   val theory_of_proof: proof -> theory
    48   val transfer_proof: theory -> proof -> proof
    49   val init_proof: theory -> proof
    50   (*generic context*)
    51   datatype generic = Theory of theory | Proof of proof
    52   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    53   val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
    54   val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
    55   val the_theory: generic -> theory
    56   val the_proof: generic -> proof
    57   val map_theory: (theory -> theory) -> generic -> generic
    58   val map_proof: (proof -> proof) -> generic -> generic
    59   val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    60   val map_proof_result: (proof -> 'a * proof) -> generic -> 'a * generic
    61   val theory_map: (generic -> generic) -> theory -> theory
    62   val proof_map: (generic -> generic) -> proof -> proof
    63   val theory_of: generic -> theory   (*total*)
    64   val proof_of: generic -> proof     (*total*)
    65   (*thread data*)
    66   val thread_data: unit -> generic option
    67   val the_thread_data: unit -> generic
    68   val set_thread_data: generic option -> unit
    69   val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
    70   val >> : (generic -> generic) -> unit
    71   val >>> : (generic -> 'a * generic) -> 'a
    72 end;
    73 
    74 signature PRIVATE_CONTEXT =
    75 sig
    76   include CONTEXT
    77   structure TheoryData:
    78   sig
    79     val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    80       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    81     val get: serial -> (Object.T -> 'a) -> theory -> 'a
    82     val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    83   end
    84   structure ProofData:
    85   sig
    86     val declare: (theory -> Object.T) -> serial
    87     val get: serial -> (Object.T -> 'a) -> proof -> 'a
    88     val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
    89   end
    90 end;
    91 
    92 structure Context: PRIVATE_CONTEXT =
    93 struct
    94 
    95 (*** theory context ***)
    96 
    97 (** theory data **)
    98 
    99 (* data kinds and access methods *)
   100 
   101 (*private copy avoids potential conflict of table exceptions*)
   102 structure Datatab = TableFun(type key = int val ord = int_ord);
   103 
   104 local
   105 
   106 type kind =
   107  {empty: Object.T,
   108   copy: Object.T -> Object.T,
   109   extend: Object.T -> Object.T,
   110   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   111 
   112 val kinds = ref (Datatab.empty: kind Datatab.table);
   113 
   114 fun invoke f k =
   115   (case Datatab.lookup (! kinds) k of
   116     SOME kind => f kind
   117   | NONE => sys_error "Invalid theory data identifier");
   118 
   119 in
   120 
   121 fun invoke_empty k   = invoke (K o #empty) k ();
   122 val invoke_copy      = invoke #copy;
   123 val invoke_extend    = invoke #extend;
   124 fun invoke_merge pp  = invoke (fn kind => #merge kind pp);
   125 
   126 fun declare_theory_data empty copy extend merge =
   127   let
   128     val k = serial ();
   129     val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
   130     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
   131   in k end;
   132 
   133 val copy_data = Datatab.map' invoke_copy;
   134 val extend_data = Datatab.map' invoke_extend;
   135 
   136 fun merge_data pp (data1, data2) =
   137   Datatab.keys (Datatab.merge (K true) (data1, data2))
   138   |> Par_List.map (fn k =>
   139     (case (Datatab.lookup data1 k, Datatab.lookup data2 k) of
   140       (SOME x, NONE) => (k, invoke_extend k x)
   141     | (NONE, SOME y) => (k, invoke_extend k y)
   142     | (SOME x, SOME y) => (k, invoke_merge pp k (invoke_extend k x, invoke_extend k y))))
   143   |> Datatab.make;
   144 
   145 end;
   146 
   147 
   148 
   149 (** datatype theory **)
   150 
   151 datatype theory =
   152   Theory of
   153    (*identity*)
   154    {self: theory ref option,      (*dynamic self reference -- follows theory changes*)
   155     draft: bool,                  (*draft mode -- linear destructive changes*)
   156     id: serial,                   (*identifier*)
   157     ids: unit Inttab.table} *     (*cumulative identifiers of non-drafts -- symbolic body content*)
   158    (*data*)
   159    Object.T Datatab.table *       (*body content*)
   160    (*ancestry*)
   161    {parents: theory list,         (*immediate predecessors*)
   162     ancestors: theory list} *     (*all predecessors -- canonical reverse order*)
   163    (*history*)
   164    {name: string,                 (*official theory name*)
   165     stage: int};                  (*checkpoint counter*)
   166 
   167 exception THEORY of string * theory list;
   168 
   169 fun rep_theory (Theory args) = args;
   170 
   171 val identity_of = #1 o rep_theory;
   172 val data_of     = #2 o rep_theory;
   173 val ancestry_of = #3 o rep_theory;
   174 val history_of  = #4 o rep_theory;
   175 
   176 fun make_identity self draft id ids = {self = self, draft = draft, id = id, ids = ids};
   177 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   178 fun make_history name stage = {name = name, stage = stage};
   179 
   180 val the_self = the o #self o identity_of;
   181 val parents_of = #parents o ancestry_of;
   182 val ancestors_of = #ancestors o ancestry_of;
   183 val theory_name = #name o history_of;
   184 
   185 
   186 (* staleness *)
   187 
   188 fun eq_id (i: int, j) = i = j;
   189 
   190 fun is_stale
   191     (Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
   192       not (eq_id (id, id'))
   193   | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   194 
   195 fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   196   | vitalize (thy as Theory ({self = NONE, draft, id, ids}, data, ancestry, history)) =
   197       let
   198         val r = ref thy;
   199         val thy' = Theory (make_identity (SOME r) draft id ids, data, ancestry, history);
   200       in r := thy'; thy' end;
   201 
   202 
   203 (* draft mode *)
   204 
   205 val is_draft = #draft o identity_of;
   206 
   207 fun reject_draft thy =
   208   if is_draft thy then raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
   209   else thy;
   210 
   211 
   212 (* names *)
   213 
   214 val PureN = "Pure";
   215 val draftN = "#";
   216 val finished = ~1;
   217 
   218 fun display_names thy =
   219   let
   220     val draft = if is_draft thy then [draftN] else [];
   221     val {stage, ...} = history_of thy;
   222     val name =
   223       if stage = finished then theory_name thy
   224       else theory_name thy ^ ":" ^ string_of_int stage;
   225     val ancestor_names = map theory_name (ancestors_of thy);
   226     val stale = if is_stale thy then ["!"] else [];
   227   in rev (stale @ draft @ [name] @ ancestor_names) end;
   228 
   229 val pretty_thy = Pretty.str_list "{" "}" o display_names;
   230 val string_of_thy = Pretty.string_of o pretty_thy;
   231 val pprint_thy = Pretty.pprint o pretty_thy;
   232 
   233 fun pretty_abbrev_thy thy =
   234   let
   235     val names = display_names thy;
   236     val n = length names;
   237     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   238   in Pretty.str_list "{" "}" abbrev end;
   239 
   240 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   241 
   242 
   243 (* theory references *)
   244 
   245 (*theory_ref provides a safe way to store dynamic references to a
   246   theory in external data structures -- a plain theory value would
   247   become stale as the self reference moves on*)
   248 
   249 datatype theory_ref = TheoryRef of theory ref;
   250 
   251 fun deref (TheoryRef (ref thy)) = thy;
   252 
   253 fun check_thy thy =  (*thread-safe version*)
   254   let val thy_ref = TheoryRef (the_self thy) in
   255     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   256     else thy_ref
   257   end;
   258 
   259 val pprint_thy_ref = Pretty.pprint o pretty_thy o deref;
   260 
   261 
   262 (* build ids *)
   263 
   264 fun insert_id draft id ids =
   265   if draft then ids
   266   else Inttab.update (id, ()) ids;
   267 
   268 fun merge_ids
   269     (Theory ({draft = draft1, id = id1, ids = ids1, ...}, _, _, _))
   270     (Theory ({draft = draft2, id = id2, ids = ids2, ...}, _, _, _)) =
   271   Inttab.merge (K true) (ids1, ids2)
   272   |> insert_id draft1 id1
   273   |> insert_id draft2 id2;
   274 
   275 
   276 (* equality and inclusion *)
   277 
   278 val eq_thy = eq_id o pairself (#id o identity_of);
   279 
   280 fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
   281   Inttab.defined ids id;
   282 
   283 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   284 
   285 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   286 
   287 
   288 (* consistent ancestors *)
   289 
   290 fun extend_ancestors thy thys =
   291   if member eq_thy thys thy then raise THEORY ("Duplicate theory node", thy :: thys)
   292   else thy :: thys;
   293 
   294 fun extend_ancestors_of thy = extend_ancestors thy (ancestors_of thy);
   295 
   296 val merge_ancestors = merge (fn (thy1, thy2) =>
   297   eq_thy (thy1, thy2) orelse
   298     theory_name thy1 = theory_name thy2 andalso
   299       raise THEORY ("Inconsistent theory versions", [thy1, thy2]));
   300 
   301 
   302 (* trivial merge *)
   303 
   304 fun merge (thy1, thy2) =
   305   if eq_thy (thy1, thy2) then thy1
   306   else if proper_subthy (thy2, thy1) then thy1
   307   else if proper_subthy (thy1, thy2) then thy2
   308   else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   309     str_of_thy thy1, str_of_thy thy2]);
   310 
   311 fun merge_refs (ref1, ref2) =
   312   if ref1 = ref2 then ref1
   313   else check_thy (merge (deref ref1, deref ref2));
   314 
   315 
   316 
   317 (** build theories **)
   318 
   319 (* primitives *)
   320 
   321 fun create_thy self draft ids data ancestry history =
   322   let val identity = make_identity self draft (serial ()) ids;
   323   in vitalize (Theory (identity, data, ancestry, history)) end;
   324 
   325 fun change_thy draft' f thy =
   326   let
   327     val Theory ({self, draft, id, ids}, data, ancestry, history) = thy;
   328     val (self', data', ancestry') =
   329       if draft then (self, data, ancestry)    (*destructive change!*)
   330       else if #stage history > 0
   331       then (NONE, copy_data data, ancestry)
   332       else (NONE, extend_data data, make_ancestry [thy] (extend_ancestors_of thy));
   333     val ids' = insert_id draft id ids;
   334     val data'' = f data';
   335     val thy' = NAMED_CRITICAL "theory" (fn () =>
   336       (check_thy thy; create_thy self' draft' ids' data'' ancestry' history));
   337   in thy' end;
   338 
   339 val name_thy = change_thy false I;
   340 val extend_thy = change_thy true I;
   341 val modify_thy = change_thy true;
   342 
   343 fun copy_thy thy =
   344   let
   345     val Theory ({draft, id, ids, ...}, data, ancestry, history) = thy;
   346     val ids' = insert_id draft id ids;
   347     val data' = copy_data data;
   348     val thy' = NAMED_CRITICAL "theory" (fn () =>
   349       (check_thy thy; create_thy NONE true ids' data' ancestry history));
   350   in thy' end;
   351 
   352 val pre_pure_thy = create_thy NONE true Inttab.empty
   353   Datatab.empty (make_ancestry [] []) (make_history PureN 0);
   354 
   355 
   356 (* named theory nodes *)
   357 
   358 fun merge_thys pp (thy1, thy2) =
   359   let
   360     val ids = merge_ids thy1 thy2;
   361     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   362     val ancestry = make_ancestry [] [];
   363     val history = make_history "" 0;
   364     val thy' = NAMED_CRITICAL "theory" (fn () =>
   365      (check_thy thy1; check_thy thy2; create_thy NONE true ids data ancestry history));
   366   in thy' end;
   367 
   368 fun maximal_thys thys =
   369   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
   370 
   371 fun begin_thy pp name imports =
   372   if name = "" orelse name = draftN then error ("Bad theory name: " ^ quote name)
   373   else
   374     let
   375       val parents = maximal_thys (distinct eq_thy imports);
   376       val ancestors =
   377         Library.foldl merge_ancestors ([], map ancestors_of parents)
   378         |> fold extend_ancestors parents;
   379 
   380       val Theory ({ids, ...}, data, _, _) =
   381         (case parents of
   382           [] => error "No parent theories"
   383         | [thy] => extend_thy thy
   384         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   385 
   386       val ancestry = make_ancestry parents ancestors;
   387       val history = make_history name 0;
   388       val thy' = NAMED_CRITICAL "theory" (fn () =>
   389         (map check_thy imports; create_thy NONE true ids data ancestry history));
   390     in thy' end;
   391 
   392 
   393 (* history stages *)
   394 
   395 fun history_stage f thy =
   396   let
   397     val {name, stage} = history_of thy;
   398     val _ = stage = finished andalso raise THEORY ("Theory already finished", [thy]);
   399     val history' = make_history name (f stage);
   400     val thy' as Theory (identity', data', ancestry', _) = name_thy thy;
   401     val thy'' = NAMED_CRITICAL "theory" (fn () =>
   402       (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   403   in thy'' end;
   404 
   405 fun checkpoint_thy thy =
   406   if is_draft thy then history_stage (fn stage => stage + 1) thy
   407   else thy;
   408 
   409 val finish_thy = history_stage (fn _ => finished);
   410 
   411 
   412 (* theory data *)
   413 
   414 structure TheoryData =
   415 struct
   416 
   417 val declare = declare_theory_data;
   418 
   419 fun get k dest thy =
   420   dest ((case Datatab.lookup (data_of thy) k of
   421     SOME x => x
   422   | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
   423 
   424 fun put k mk x = modify_thy (Datatab.update (k, mk x));
   425 
   426 end;
   427 
   428 
   429 
   430 (*** proof context ***)
   431 
   432 (* datatype proof *)
   433 
   434 datatype proof = Prf of Object.T Datatab.table * theory_ref;
   435 
   436 fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
   437 fun data_of_proof (Prf (data, _)) = data;
   438 fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
   439 
   440 
   441 (* proof data kinds *)
   442 
   443 local
   444 
   445 val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
   446 
   447 fun invoke_init k =
   448   (case Datatab.lookup (! kinds) k of
   449     SOME init => init
   450   | NONE => sys_error "Invalid proof data identifier");
   451 
   452 fun init_data thy =
   453   Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
   454 
   455 fun init_new_data data thy =
   456   Datatab.merge (K true) (data, init_data thy);
   457 
   458 in
   459 
   460 fun init_proof thy = Prf (init_data thy, check_thy thy);
   461 
   462 fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
   463   let
   464     val thy = deref thy_ref;
   465     val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
   466     val _ = check_thy thy;
   467     val data' = init_new_data data thy';
   468     val thy_ref' = check_thy thy';
   469   in Prf (data', thy_ref') end;
   470 
   471 
   472 structure ProofData =
   473 struct
   474 
   475 fun declare init =
   476   let
   477     val k = serial ();
   478     val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
   479   in k end;
   480 
   481 fun get k dest prf =
   482   dest (case Datatab.lookup (data_of_proof prf) k of
   483     SOME x => x
   484   | NONE => invoke_init k (theory_of_proof prf));   (*adhoc value*)
   485 
   486 fun put k mk x = map_prf (Datatab.update (k, mk x));
   487 
   488 end;
   489 
   490 end;
   491 
   492 
   493 
   494 (*** generic context ***)
   495 
   496 datatype generic = Theory of theory | Proof of proof;
   497 
   498 fun cases f _ (Theory thy) = f thy
   499   | cases _ g (Proof prf) = g prf;
   500 
   501 fun mapping f g = cases (Theory o f) (Proof o g);
   502 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   503 
   504 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   505 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   506 
   507 fun map_theory f = Theory o f o the_theory;
   508 fun map_proof f = Proof o f o the_proof;
   509 
   510 fun map_theory_result f = apsnd Theory o f o the_theory;
   511 fun map_proof_result f = apsnd Proof o f o the_proof;
   512 
   513 fun theory_map f = the_theory o f o Theory;
   514 fun proof_map f = the_proof o f o Proof;
   515 
   516 val theory_of = cases I theory_of_proof;
   517 val proof_of = cases init_proof I;
   518 
   519 
   520 
   521 (** thread data **)
   522 
   523 local val tag = Universal.tag () : generic option Universal.tag in
   524 
   525 fun thread_data () =
   526   (case Thread.getLocal tag of
   527     SOME (SOME context) => SOME context
   528   | _ => NONE);
   529 
   530 fun the_thread_data () =
   531   (case thread_data () of
   532     SOME context => context
   533   | _ => error "Unknown context");
   534 
   535 fun set_thread_data context = Thread.setLocal (tag, context);
   536 fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
   537 
   538 end;
   539 
   540 fun >>> f =
   541   let
   542     val (res, context') = f (the_thread_data ());
   543     val _ = set_thread_data (SOME context');
   544   in res end;
   545 
   546 nonfix >>;
   547 fun >> f = >>> (fn context => ((), f context));
   548 
   549 val _ = set_thread_data (SOME (Theory pre_pure_thy));
   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;