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