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