src/Pure/context.ML
author wenzelm
Wed Nov 11 00:11:26 2009 +0100 (2009-11-11)
changeset 33606 2b27020ffcb2
parent 33517 d064fa48f305
child 34245 25bd3ed2ac9f
permissions -rw-r--r--
local mutex for theory content/identity operations;
     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 Firm naming conventions:
     9    thy, thy', thy1, thy2: theory
    10    ctxt, ctxt', ctxt1, ctxt2: Proof.context
    11    context: Context.generic
    12 *)
    13 
    14 signature BASIC_CONTEXT =
    15 sig
    16   type theory
    17   type theory_ref
    18   exception THEORY of string * theory list
    19   structure Proof: sig type context end
    20   structure ProofContext:
    21   sig
    22     val theory_of: Proof.context -> theory
    23     val init: theory -> Proof.context
    24   end
    25 end;
    26 
    27 signature CONTEXT =
    28 sig
    29   include BASIC_CONTEXT
    30   (*theory context*)
    31   val parents_of: theory -> theory list
    32   val ancestors_of: theory -> theory list
    33   val theory_name: theory -> string
    34   val is_stale: theory -> bool
    35   val is_draft: theory -> bool
    36   val reject_draft: theory -> theory
    37   val PureN: string
    38   val display_names: theory -> string list
    39   val pretty_thy: theory -> Pretty.T
    40   val string_of_thy: theory -> string
    41   val pretty_abbrev_thy: theory -> Pretty.T
    42   val str_of_thy: theory -> string
    43   val deref: theory_ref -> theory
    44   val check_thy: theory -> theory_ref
    45   val eq_thy: theory * theory -> bool
    46   val subthy: theory * theory -> bool
    47   val joinable: theory * theory -> bool
    48   val merge: theory * theory -> theory
    49   val merge_refs: theory_ref * theory_ref -> theory_ref
    50   val copy_thy: theory -> theory
    51   val checkpoint_thy: theory -> theory
    52   val finish_thy: theory -> theory
    53   val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    54   (*proof context*)
    55   val raw_transfer: theory -> Proof.context -> Proof.context
    56   (*generic context*)
    57   datatype generic = Theory of theory | Proof of Proof.context
    58   val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a
    59   val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic
    60   val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) ->
    61     generic -> 'a * generic
    62   val the_theory: generic -> theory
    63   val the_proof: generic -> Proof.context
    64   val map_theory: (theory -> theory) -> generic -> generic
    65   val map_proof: (Proof.context -> Proof.context) -> generic -> generic
    66   val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
    67   val map_proof_result: (Proof.context -> 'a * Proof.context) -> generic -> 'a * generic
    68   val theory_map: (generic -> generic) -> theory -> theory
    69   val proof_map: (generic -> generic) -> Proof.context -> Proof.context
    70   val theory_of: generic -> theory  (*total*)
    71   val proof_of: generic -> Proof.context  (*total*)
    72   (*thread data*)
    73   val thread_data: unit -> generic option
    74   val the_thread_data: unit -> generic
    75   val set_thread_data: generic option -> unit
    76   val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
    77   val >> : (generic -> generic) -> unit
    78   val >>> : (generic -> 'a * generic) -> 'a
    79 end;
    80 
    81 signature PRIVATE_CONTEXT =
    82 sig
    83   include CONTEXT
    84   structure Theory_Data:
    85   sig
    86     val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    87       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    88     val get: serial -> (Object.T -> 'a) -> theory -> 'a
    89     val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    90   end
    91   structure Proof_Data:
    92   sig
    93     val declare: (theory -> Object.T) -> serial
    94     val get: serial -> (Object.T -> 'a) -> Proof.context -> 'a
    95     val put: serial -> ('a -> Object.T) -> 'a -> Proof.context -> Proof.context
    96   end
    97 end;
    98 
    99 structure Context: PRIVATE_CONTEXT =
   100 struct
   101 
   102 (*** theory context ***)
   103 
   104 (** theory data **)
   105 
   106 (* data kinds and access methods *)
   107 
   108 (*private copy avoids potential conflict of table exceptions*)
   109 structure Datatab = Table(type key = int val ord = int_ord);
   110 
   111 local
   112 
   113 type kind =
   114  {empty: Object.T,
   115   copy: Object.T -> Object.T,
   116   extend: Object.T -> Object.T,
   117   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   118 
   119 val kinds = Unsynchronized.ref (Datatab.empty: kind Datatab.table);
   120 
   121 fun invoke f k =
   122   (case Datatab.lookup (! kinds) k of
   123     SOME kind => f kind
   124   | NONE => sys_error "Invalid theory data identifier");
   125 
   126 in
   127 
   128 fun invoke_empty k = invoke (K o #empty) k ();
   129 val invoke_copy = invoke #copy;
   130 val invoke_extend = invoke #extend;
   131 fun invoke_merge pp = invoke (fn kind => #merge kind pp);
   132 
   133 fun declare_theory_data empty copy extend merge =
   134   let
   135     val k = serial ();
   136     val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
   137     val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, kind)));
   138   in k end;
   139 
   140 val copy_data = Datatab.map' invoke_copy;
   141 val extend_data = Datatab.map' invoke_extend;
   142 
   143 fun merge_data pp (data1, data2) =
   144   Datatab.keys (Datatab.merge (K true) (data1, data2))
   145   |> Par_List.map (fn k =>
   146     (case (Datatab.lookup data1 k, Datatab.lookup data2 k) of
   147       (SOME x, NONE) => (k, invoke_extend k x)
   148     | (NONE, SOME y) => (k, invoke_extend k y)
   149     | (SOME x, SOME y) => (k, invoke_merge pp k (invoke_extend k x, invoke_extend k y))))
   150   |> Datatab.make;
   151 
   152 end;
   153 
   154 
   155 
   156 (** datatype theory **)
   157 
   158 datatype theory =
   159   Theory of
   160    (*identity*)
   161    {self: theory Unsynchronized.ref option,  (*dynamic self reference -- follows theory changes*)
   162     draft: bool,                  (*draft mode -- linear destructive changes*)
   163     id: serial,                   (*identifier*)
   164     ids: unit Inttab.table} *     (*cumulative identifiers of non-drafts -- symbolic body content*)
   165    (*data*)
   166    Object.T Datatab.table *       (*body content*)
   167    (*ancestry*)
   168    {parents: theory list,         (*immediate predecessors*)
   169     ancestors: theory list} *     (*all predecessors -- canonical reverse order*)
   170    (*history*)
   171    {name: string,                 (*official theory name*)
   172     stage: int};                  (*checkpoint counter*)
   173 
   174 exception THEORY of string * theory list;
   175 
   176 fun rep_theory (Theory args) = args;
   177 
   178 val identity_of = #1 o rep_theory;
   179 val data_of = #2 o rep_theory;
   180 val ancestry_of = #3 o rep_theory;
   181 val history_of = #4 o rep_theory;
   182 
   183 fun make_identity self draft id ids = {self = self, draft = draft, id = id, ids = ids};
   184 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   185 fun make_history name stage = {name = name, stage = stage};
   186 
   187 val the_self = the o #self o identity_of;
   188 val parents_of = #parents o ancestry_of;
   189 val ancestors_of = #ancestors o ancestry_of;
   190 val theory_name = #name o history_of;
   191 
   192 
   193 (* staleness *)
   194 
   195 fun eq_id (i: int, j) = i = j;
   196 
   197 fun is_stale
   198     (Theory ({self =
   199         SOME (Unsynchronized.ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
   200       not (eq_id (id, id'))
   201   | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   202 
   203 fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   204   | vitalize (thy as Theory ({self = NONE, draft, id, ids}, data, ancestry, history)) =
   205       let
   206         val r = Unsynchronized.ref thy;
   207         val thy' = Theory (make_identity (SOME r) draft id ids, data, ancestry, history);
   208       in r := thy'; thy' end;
   209 
   210 
   211 (* draft mode *)
   212 
   213 val is_draft = #draft o identity_of;
   214 
   215 fun reject_draft thy =
   216   if is_draft thy then
   217     raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
   218   else thy;
   219 
   220 
   221 (* names *)
   222 
   223 val PureN = "Pure";
   224 val draftN = "#";
   225 val finished = ~1;
   226 
   227 fun display_names thy =
   228   let
   229     val draft = if is_draft thy then [draftN] else [];
   230     val {stage, ...} = history_of thy;
   231     val name =
   232       if stage = finished then theory_name thy
   233       else theory_name thy ^ ":" ^ string_of_int stage;
   234     val ancestor_names = map theory_name (ancestors_of thy);
   235     val stale = if is_stale thy then ["!"] else [];
   236   in rev (stale @ draft @ [name] @ ancestor_names) end;
   237 
   238 val pretty_thy = Pretty.str_list "{" "}" o display_names;
   239 val string_of_thy = Pretty.string_of o pretty_thy;
   240 
   241 fun pretty_abbrev_thy thy =
   242   let
   243     val names = display_names thy;
   244     val n = length names;
   245     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   246   in Pretty.str_list "{" "}" abbrev end;
   247 
   248 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   249 
   250 
   251 (* theory references *)
   252 
   253 (*theory_ref provides a safe way to store dynamic references to a
   254   theory in external data structures -- a plain theory value would
   255   become stale as the self reference moves on*)
   256 
   257 datatype theory_ref = Theory_Ref of theory Unsynchronized.ref;
   258 
   259 fun deref (Theory_Ref (Unsynchronized.ref thy)) = thy;
   260 
   261 fun check_thy thy =  (*thread-safe version*)
   262   let val thy_ref = Theory_Ref (the_self thy) in
   263     if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
   264     else thy_ref
   265   end;
   266 
   267 
   268 (* build ids *)
   269 
   270 fun insert_id draft id ids =
   271   if draft then ids
   272   else Inttab.update (id, ()) ids;
   273 
   274 fun merge_ids
   275     (Theory ({draft = draft1, id = id1, ids = ids1, ...}, _, _, _))
   276     (Theory ({draft = draft2, id = id2, ids = ids2, ...}, _, _, _)) =
   277   Inttab.merge (K true) (ids1, ids2)
   278   |> insert_id draft1 id1
   279   |> insert_id draft2 id2;
   280 
   281 
   282 (* equality and inclusion *)
   283 
   284 val eq_thy = eq_id o pairself (#id o identity_of);
   285 
   286 fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
   287   Inttab.defined ids id;
   288 
   289 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   290 
   291 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   292 
   293 
   294 (* consistent ancestors *)
   295 
   296 fun extend_ancestors thy thys =
   297   if member eq_thy thys thy then
   298     raise THEORY ("Duplicate theory node", thy :: thys)
   299   else thy :: thys;
   300 
   301 fun extend_ancestors_of thy = extend_ancestors thy (ancestors_of thy);
   302 
   303 val merge_ancestors = merge (fn (thy1, thy2) =>
   304   eq_thy (thy1, thy2) orelse
   305     theory_name thy1 = theory_name thy2 andalso
   306       raise THEORY ("Inconsistent theory versions", [thy1, thy2]));
   307 
   308 
   309 (* trivial merge *)
   310 
   311 fun merge (thy1, thy2) =
   312   if eq_thy (thy1, thy2) then thy1
   313   else if proper_subthy (thy2, thy1) then thy1
   314   else if proper_subthy (thy1, thy2) then thy2
   315   else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
   316     str_of_thy thy1, str_of_thy thy2]);
   317 
   318 fun merge_refs (ref1, ref2) =
   319   if ref1 = ref2 then ref1
   320   else check_thy (merge (deref ref1, deref ref2));
   321 
   322 
   323 
   324 (** build theories **)
   325 
   326 (* primitives *)
   327 
   328 local
   329   val lock = Mutex.mutex ();
   330 in
   331   fun SYNCHRONIZED e = SimpleThread.synchronized "theory" lock e;
   332 end;
   333 
   334 fun create_thy self draft ids data ancestry history =
   335   let val identity = make_identity self draft (serial ()) ids;
   336   in vitalize (Theory (identity, data, ancestry, history)) end;
   337 
   338 fun change_thy draft' f thy =
   339   let
   340     val Theory ({self, draft, id, ids}, data, ancestry, history) = thy;
   341     val (self', data', ancestry') =
   342       if draft then (self, data, ancestry)    (*destructive change!*)
   343       else if #stage history > 0
   344       then (NONE, copy_data data, ancestry)
   345       else (NONE, extend_data data, make_ancestry [thy] (extend_ancestors_of thy));
   346     val ids' = insert_id draft id ids;
   347     val data'' = f data';
   348     val thy' = SYNCHRONIZED (fn () =>
   349       (check_thy thy; create_thy self' draft' ids' data'' ancestry' history));
   350   in thy' end;
   351 
   352 val name_thy = change_thy false I;
   353 val extend_thy = change_thy true I;
   354 val modify_thy = change_thy true;
   355 
   356 fun copy_thy thy =
   357   let
   358     val Theory ({draft, id, ids, ...}, data, ancestry, history) = thy;
   359     val ids' = insert_id draft id ids;
   360     val data' = copy_data data;
   361     val thy' = SYNCHRONIZED (fn () =>
   362       (check_thy thy; create_thy NONE true ids' data' ancestry history));
   363   in thy' end;
   364 
   365 val pre_pure_thy = create_thy NONE true Inttab.empty
   366   Datatab.empty (make_ancestry [] []) (make_history PureN 0);
   367 
   368 
   369 (* named theory nodes *)
   370 
   371 fun merge_thys pp (thy1, thy2) =
   372   let
   373     val ids = merge_ids thy1 thy2;
   374     val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   375     val ancestry = make_ancestry [] [];
   376     val history = make_history "" 0;
   377     val thy' = SYNCHRONIZED (fn () =>
   378      (check_thy thy1; check_thy thy2; create_thy NONE true ids data ancestry history));
   379   in thy' end;
   380 
   381 fun maximal_thys thys =
   382   thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
   383 
   384 fun begin_thy pp name imports =
   385   if name = "" orelse name = draftN then error ("Bad theory name: " ^ quote name)
   386   else
   387     let
   388       val parents = maximal_thys (distinct eq_thy imports);
   389       val ancestors =
   390         Library.foldl merge_ancestors ([], map ancestors_of parents)
   391         |> fold extend_ancestors parents;
   392 
   393       val Theory ({ids, ...}, data, _, _) =
   394         (case parents of
   395           [] => error "No parent theories"
   396         | [thy] => extend_thy thy
   397         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   398 
   399       val ancestry = make_ancestry parents ancestors;
   400       val history = make_history name 0;
   401       val thy' = SYNCHRONIZED (fn () =>
   402         (map check_thy imports; create_thy NONE true ids data ancestry history));
   403     in thy' end;
   404 
   405 
   406 (* history stages *)
   407 
   408 fun history_stage f thy =
   409   let
   410     val {name, stage} = history_of thy;
   411     val _ = stage = finished andalso raise THEORY ("Theory already finished", [thy]);
   412     val history' = make_history name (f stage);
   413     val thy' as Theory (identity', data', ancestry', _) = name_thy thy;
   414     val thy'' = SYNCHRONIZED (fn () =>
   415       (check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
   416   in thy'' end;
   417 
   418 fun checkpoint_thy thy =
   419   if is_draft thy then history_stage (fn stage => stage + 1) thy
   420   else thy;
   421 
   422 val finish_thy = history_stage (fn _ => finished);
   423 
   424 
   425 (* theory data *)
   426 
   427 structure Theory_Data =
   428 struct
   429 
   430 val declare = declare_theory_data;
   431 
   432 fun get k dest thy =
   433   dest ((case Datatab.lookup (data_of thy) k of
   434     SOME x => x
   435   | NONE => invoke_copy k (invoke_empty k)));   (*adhoc value*)
   436 
   437 fun put k mk x = modify_thy (Datatab.update (k, mk x));
   438 
   439 end;
   440 
   441 
   442 
   443 (*** proof context ***)
   444 
   445 (* datatype Proof.context *)
   446 
   447 structure Proof =
   448 struct
   449   datatype context = Context of Object.T Datatab.table * theory_ref;
   450 end;
   451 
   452 fun theory_of_proof (Proof.Context (_, thy_ref)) = deref thy_ref;
   453 fun data_of_proof (Proof.Context (data, _)) = data;
   454 fun map_prf f (Proof.Context (data, thy_ref)) = Proof.Context (f data, thy_ref);
   455 
   456 
   457 (* proof data kinds *)
   458 
   459 local
   460 
   461 val kinds = Unsynchronized.ref (Datatab.empty: (theory -> Object.T) Datatab.table);
   462 
   463 fun invoke_init k =
   464   (case Datatab.lookup (! kinds) k of
   465     SOME init => init
   466   | NONE => sys_error "Invalid proof data identifier");
   467 
   468 fun init_data thy =
   469   Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
   470 
   471 fun init_new_data data thy =
   472   Datatab.merge (K true) (data, init_data thy);
   473 
   474 in
   475 
   476 fun raw_transfer thy' (Proof.Context (data, thy_ref)) =
   477   let
   478     val thy = deref thy_ref;
   479     val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
   480     val _ = check_thy thy;
   481     val data' = init_new_data data thy';
   482     val thy_ref' = check_thy thy';
   483   in Proof.Context (data', thy_ref') end;
   484 
   485 structure ProofContext =
   486 struct
   487   val theory_of = theory_of_proof;
   488   fun init thy = Proof.Context (init_data thy, check_thy thy);
   489 end;
   490 
   491 structure Proof_Data =
   492 struct
   493 
   494 fun declare init =
   495   let
   496     val k = serial ();
   497     val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, init)));
   498   in k end;
   499 
   500 fun get k dest prf =
   501   dest (case Datatab.lookup (data_of_proof prf) k of
   502     SOME x => x
   503   | NONE => invoke_init k (ProofContext.theory_of prf));   (*adhoc value*)
   504 
   505 fun put k mk x = map_prf (Datatab.update (k, mk x));
   506 
   507 end;
   508 
   509 end;
   510 
   511 
   512 
   513 (*** generic context ***)
   514 
   515 datatype generic = Theory of theory | Proof of Proof.context;
   516 
   517 fun cases f _ (Theory thy) = f thy
   518   | cases _ g (Proof prf) = g prf;
   519 
   520 fun mapping f g = cases (Theory o f) (Proof o g);
   521 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   522 
   523 val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
   524 val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
   525 
   526 fun map_theory f = Theory o f o the_theory;
   527 fun map_proof f = Proof o f o the_proof;
   528 
   529 fun map_theory_result f = apsnd Theory o f o the_theory;
   530 fun map_proof_result f = apsnd Proof o f o the_proof;
   531 
   532 fun theory_map f = the_theory o f o Theory;
   533 fun proof_map f = the_proof o f o Proof;
   534 
   535 val theory_of = cases I ProofContext.theory_of;
   536 val proof_of = cases ProofContext.init I;
   537 
   538 
   539 
   540 (** thread data **)
   541 
   542 local val tag = Universal.tag () : generic option Universal.tag in
   543 
   544 fun thread_data () =
   545   (case Thread.getLocal tag of
   546     SOME (SOME context) => SOME context
   547   | _ => NONE);
   548 
   549 fun the_thread_data () =
   550   (case thread_data () of
   551     SOME context => context
   552   | _ => error "Unknown context");
   553 
   554 fun set_thread_data context = Thread.setLocal (tag, context);
   555 fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
   556 
   557 end;
   558 
   559 fun >>> f =
   560   let
   561     val (res, context') = f (the_thread_data ());
   562     val _ = set_thread_data (SOME context');
   563   in res end;
   564 
   565 nonfix >>;
   566 fun >> f = >>> (fn context => ((), f context));
   567 
   568 val _ = set_thread_data (SOME (Theory pre_pure_thy));
   569 
   570 end;
   571 
   572 structure Basic_Context: BASIC_CONTEXT = Context;
   573 open Basic_Context;
   574 
   575 
   576 
   577 (*** type-safe interfaces for data declarations ***)
   578 
   579 (** theory data **)
   580 
   581 signature OLD_THEORY_DATA_ARGS =
   582 sig
   583   type T
   584   val empty: T
   585   val copy: T -> T
   586   val extend: T -> T
   587   val merge: Pretty.pp -> T * T -> T
   588 end;
   589 
   590 signature OLD_THEORY_DATA =
   591 sig
   592   type T
   593   val get: theory -> T
   594   val put: T -> theory -> theory
   595   val map: (T -> T) -> theory -> theory
   596   val init: theory -> theory
   597 end;
   598 
   599 functor TheoryDataFun(Data: OLD_THEORY_DATA_ARGS): OLD_THEORY_DATA =
   600 struct
   601 
   602 type T = Data.T;
   603 exception Data of T;
   604 
   605 val kind = Context.Theory_Data.declare
   606   (Data Data.empty)
   607   (fn Data x => Data (Data.copy x))
   608   (fn Data x => Data (Data.extend x))
   609   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   610 
   611 val get = Context.Theory_Data.get kind (fn Data x => x);
   612 val put = Context.Theory_Data.put kind Data;
   613 fun map f thy = put (f (get thy)) thy;
   614 
   615 fun init thy = map I thy;
   616 
   617 end;
   618 
   619 signature THEORY_DATA_ARGS =
   620 sig
   621   type T
   622   val empty: T
   623   val extend: T -> T
   624   val merge: T * T -> T
   625 end;
   626 
   627 signature THEORY_DATA =
   628 sig
   629   type T
   630   val get: theory -> T
   631   val put: T -> theory -> theory
   632   val map: (T -> T) -> theory -> theory
   633 end;
   634 
   635 functor Theory_Data(Data: THEORY_DATA_ARGS): THEORY_DATA =
   636 struct
   637 
   638 structure Result = TheoryDataFun
   639 (
   640   type T = Data.T;
   641   val empty = Data.empty;
   642   val copy = I;
   643   val extend = Data.extend;
   644   fun merge _ = Data.merge;
   645 );
   646 
   647 open Result;
   648 
   649 end;
   650 
   651 
   652 
   653 (** proof data **)
   654 
   655 signature PROOF_DATA_ARGS =
   656 sig
   657   type T
   658   val init: theory -> T
   659 end;
   660 
   661 signature PROOF_DATA =
   662 sig
   663   type T
   664   val get: Proof.context -> T
   665   val put: T -> Proof.context -> Proof.context
   666   val map: (T -> T) -> Proof.context -> Proof.context
   667 end;
   668 
   669 functor Proof_Data(Data: PROOF_DATA_ARGS): PROOF_DATA =
   670 struct
   671 
   672 type T = Data.T;
   673 exception Data of T;
   674 
   675 val kind = Context.Proof_Data.declare (Data o Data.init);
   676 
   677 val get = Context.Proof_Data.get kind (fn Data x => x);
   678 val put = Context.Proof_Data.put kind Data;
   679 fun map f prf = put (f (get prf)) prf;
   680 
   681 end;
   682 
   683 
   684 
   685 (** generic data **)
   686 
   687 signature GENERIC_DATA_ARGS =
   688 sig
   689   type T
   690   val empty: T
   691   val extend: T -> T
   692   val merge: T * T -> T
   693 end;
   694 
   695 signature GENERIC_DATA =
   696 sig
   697   type T
   698   val get: Context.generic -> T
   699   val put: T -> Context.generic -> Context.generic
   700   val map: (T -> T) -> Context.generic -> Context.generic
   701 end;
   702 
   703 functor Generic_Data(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   704 struct
   705 
   706 structure Thy_Data = Theory_Data(Data);
   707 structure Prf_Data = Proof_Data(type T = Data.T val init = Thy_Data.get);
   708 
   709 type T = Data.T;
   710 
   711 fun get (Context.Theory thy) = Thy_Data.get thy
   712   | get (Context.Proof prf) = Prf_Data.get prf;
   713 
   714 fun put x (Context.Theory thy) = Context.Theory (Thy_Data.put x thy)
   715   | put x (Context.Proof prf) = Context.Proof (Prf_Data.put x prf);
   716 
   717 fun map f ctxt = put (f (get ctxt)) ctxt;
   718 
   719 end;
   720 
   721 (*hide private interface*)
   722 structure Context: CONTEXT = Context;
   723