src/Pure/context.ML
author wenzelm
Mon Sep 22 15:26:11 2008 +0200 (2008-09-22)
changeset 28317 83c4fc383409
parent 28122 3d099ce624e7
child 28375 c879d88d038a
permissions -rw-r--r--
added reject_draft;
     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 (equal name o #2) ids orelse
   214   Inttab.exists (equal name o #2) 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 (equal (#2 id) o #2) 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
   279     (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
   280   Inttab.defined ids i orelse Inttab.defined iids i;
   281 
   282 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   283 
   284 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   285 
   286 
   287 (* trivial merge *)
   288 
   289 fun merge (thy1, thy2) =
   290   if eq_thy (thy1, thy2) then thy1
   291   else if proper_subthy (thy2, thy1) then thy1
   292   else if proper_subthy (thy1, thy2) then thy2
   293   else (check_merge thy1 thy2;
   294     error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   295       str_of_thy thy1, str_of_thy thy2]));
   296 
   297 fun merge_refs (ref1, ref2) =
   298   if ref1 = ref2 then ref1
   299   else check_thy (merge (deref ref1, deref ref2));
   300 
   301 
   302 
   303 (** build theories **)
   304 
   305 (* primitives *)
   306 
   307 fun create_thy name self id ids iids data ancestry history =
   308   let
   309     val {version, name = _, intermediates = _} = history;
   310     val intermediate = version > 0;
   311     val (ids', iids') = check_insert intermediate id (ids, iids);
   312     val id' = (serial (), name);
   313     val _ = check_insert intermediate id' (ids', iids');
   314     val identity' = make_identity self id' ids' iids';
   315   in vitalize (Theory (identity', data, ancestry, history)) end;
   316 
   317 fun change_thy name f thy =
   318   let
   319     val Theory ({self, id, ids, iids}, data, ancestry, history) = thy;
   320     val (self', data', ancestry') =
   321       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   322       else if #version history > 0
   323       then (NONE, copy_data data, ancestry)
   324       else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   325     val data'' = f data';
   326     val thy' = NAMED_CRITICAL "theory" (fn () =>
   327       (check_thy thy; create_thy name self' id ids iids data'' ancestry' history));
   328   in thy' end;
   329 
   330 fun name_thy name = change_thy name I;
   331 val modify_thy = change_thy draftN;
   332 val extend_thy = modify_thy I;
   333 
   334 fun copy_thy thy =
   335   let
   336     val Theory ({id, ids, iids, ...}, data, ancestry, history) = thy;
   337     val data' = copy_data data;
   338     val thy' = NAMED_CRITICAL "theory" (fn () =>
   339       (check_thy thy; create_thy draftN NONE id ids iids data' ancestry history));
   340   in thy' end;
   341 
   342 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   343   Datatab.empty (make_ancestry [] []) (make_history PureN 0 []);
   344 
   345 
   346 (* named theory nodes *)
   347 
   348 fun merge_thys pp (thy1, thy2) =
   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 map_theory_result f = apsnd Theory o f o the_theory;
   505 fun map_proof_result f = apsnd Proof o f o the_proof;
   506 
   507 fun theory_map f = the_theory o f o Theory;
   508 fun proof_map f = the_proof o f o Proof;
   509 
   510 val theory_of = cases I theory_of_proof;
   511 val proof_of = cases init_proof I;
   512 
   513 
   514 
   515 (** thread data **)
   516 
   517 local val tag = Universal.tag () : generic option Universal.tag in
   518 
   519 fun thread_data () =
   520   (case Thread.getLocal tag of
   521     SOME (SOME context) => SOME context
   522   | _ => NONE);
   523 
   524 fun the_thread_data () =
   525   (case thread_data () of
   526     SOME context => context
   527   | _ => error "Unknown context");
   528 
   529 fun set_thread_data context = Thread.setLocal (tag, context);
   530 fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
   531 
   532 end;
   533 
   534 fun >>> f =
   535   let
   536     val (res, context') = f (the_thread_data ());
   537     val _ = set_thread_data (SOME context');
   538   in res end;
   539 
   540 nonfix >>;
   541 fun >> f = >>> (fn context => ((), f context));
   542 
   543 val _ = set_thread_data (SOME (Theory pre_pure_thy));
   544 
   545 end;
   546 
   547 structure BasicContext: BASIC_CONTEXT = Context;
   548 open BasicContext;
   549 
   550 
   551 
   552 (*** type-safe interfaces for data declarations ***)
   553 
   554 (** theory data **)
   555 
   556 signature THEORY_DATA_ARGS =
   557 sig
   558   type T
   559   val empty: T
   560   val copy: T -> T
   561   val extend: T -> T
   562   val merge: Pretty.pp -> T * T -> T
   563 end;
   564 
   565 signature THEORY_DATA =
   566 sig
   567   type T
   568   val get: theory -> T
   569   val put: T -> theory -> theory
   570   val map: (T -> T) -> theory -> theory
   571   val init: theory -> theory
   572 end;
   573 
   574 functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
   575 struct
   576 
   577 structure TheoryData = Context.TheoryData;
   578 
   579 type T = Data.T;
   580 exception Data of T;
   581 
   582 val kind = TheoryData.declare
   583   (Data Data.empty)
   584   (fn Data x => Data (Data.copy x))
   585   (fn Data x => Data (Data.extend x))
   586   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   587 
   588 val get = TheoryData.get kind (fn Data x => x);
   589 val put = TheoryData.put kind Data;
   590 fun map f thy = put (f (get thy)) thy;
   591 
   592 fun init thy = map I thy;
   593 
   594 end;
   595 
   596 
   597 
   598 (** proof data **)
   599 
   600 signature PROOF_DATA_ARGS =
   601 sig
   602   type T
   603   val init: theory -> T
   604 end;
   605 
   606 signature PROOF_DATA =
   607 sig
   608   type T
   609   val get: Context.proof -> T
   610   val put: T -> Context.proof -> Context.proof
   611   val map: (T -> T) -> Context.proof -> Context.proof
   612 end;
   613 
   614 functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
   615 struct
   616 
   617 structure ProofData = Context.ProofData;
   618 
   619 type T = Data.T;
   620 exception Data of T;
   621 
   622 val kind = ProofData.declare (Data o Data.init);
   623 
   624 val get = ProofData.get kind (fn Data x => x);
   625 val put = ProofData.put kind Data;
   626 fun map f prf = put (f (get prf)) prf;
   627 
   628 end;
   629 
   630 
   631 
   632 (** generic data **)
   633 
   634 signature GENERIC_DATA_ARGS =
   635 sig
   636   type T
   637   val empty: T
   638   val extend: T -> T
   639   val merge: Pretty.pp -> T * T -> T
   640 end;
   641 
   642 signature GENERIC_DATA =
   643 sig
   644   type T
   645   val get: Context.generic -> T
   646   val put: T -> Context.generic -> Context.generic
   647   val map: (T -> T) -> Context.generic -> Context.generic
   648 end;
   649 
   650 functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   651 struct
   652 
   653 structure ThyData = TheoryDataFun(open Data val copy = I);
   654 structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
   655 
   656 type T = Data.T;
   657 
   658 fun get (Context.Theory thy) = ThyData.get thy
   659   | get (Context.Proof prf) = PrfData.get prf;
   660 
   661 fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
   662   | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
   663 
   664 fun map f ctxt = put (f (get ctxt)) ctxt;
   665 
   666 end;
   667 
   668 (*hide private interface*)
   669 structure Context: CONTEXT = Context;
   670 
   671 (*fake predeclarations*)
   672 structure Proof = struct type context = Context.proof end;
   673 structure ProofContext =
   674 struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;