src/Pure/context.ML
author wenzelm
Thu Apr 05 00:30:31 2007 +0200 (2007-04-05)
changeset 22603 76c30440c9af
parent 22599 d920d38f1f02
child 22827 7dc27b37f7f7
permissions -rw-r--r--
added thy_ord -- order of creation;
ancestors: back to traditional ad-hoc order (avoid occasional problems with get_thm);
     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 development graph and history support. Generic proof contexts with
     7 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 ProtoPureN: string
    26   val PureN: string
    27   val CPureN: string
    28   val draftN: string
    29   val exists_name: string -> theory -> bool
    30   val names_of: theory -> string list
    31   val pretty_thy: theory -> Pretty.T
    32   val string_of_thy: theory -> string
    33   val pprint_thy: theory -> pprint_args -> unit
    34   val pretty_abbrev_thy: theory -> Pretty.T
    35   val str_of_thy: theory -> string
    36   val check_thy: theory -> theory
    37   val eq_thy: theory * theory -> bool
    38   val thy_ord: theory * theory -> order
    39   val subthy: theory * theory -> bool
    40   val joinable: theory * theory -> bool
    41   val merge: theory * theory -> theory                     (*exception TERM*)
    42   val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    43   val self_ref: theory -> theory_ref
    44   val deref: theory_ref -> theory
    45   val copy_thy: theory -> theory
    46   val checkpoint_thy: theory -> theory
    47   val finish_thy: theory -> theory
    48   val theory_data_of: theory -> string list
    49   val pre_pure_thy: theory
    50   val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    51   (*proof context*)
    52   type proof
    53   val theory_of_proof: proof -> theory
    54   val transfer_proof: theory -> proof -> proof
    55   val init_proof: theory -> proof
    56   val proof_data_of: theory -> string list
    57   (*generic context*)
    58   datatype generic = Theory of theory | Proof of proof
    59   val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
    60   val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
    61   val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
    62   val the_theory: generic -> theory
    63   val the_proof: generic -> proof
    64   val map_theory: (theory -> theory) -> generic -> generic
    65   val map_proof: (proof -> proof) -> generic -> generic
    66   val theory_map: (generic -> generic) -> theory -> theory
    67   val proof_map: (generic -> generic) -> proof -> proof
    68   val theory_of: generic -> theory   (*total*)
    69   val proof_of: generic -> proof     (*total*)
    70   (*delayed setup*)
    71   val add_setup: (theory -> theory) -> unit
    72   val setup: unit -> theory -> theory
    73 end;
    74 
    75 signature PRIVATE_CONTEXT =
    76 sig
    77   include CONTEXT
    78   structure TheoryData:
    79   sig
    80     val declare: string -> Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    81       (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    82     val init: serial -> theory -> theory
    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: string -> (theory -> Object.T) -> serial
    89     val init: serial -> theory -> theory
    90     val get: serial -> (Object.T -> 'a) -> proof -> 'a
    91     val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
    92   end
    93 end;
    94 
    95 structure Context: PRIVATE_CONTEXT =
    96 struct
    97 
    98 (*** theory context ***)
    99 
   100 (** theory data **)
   101 
   102 (* data kinds and access methods *)
   103 
   104 (*private copy avoids potential conflict of table exceptions*)
   105 structure Datatab = TableFun(type key = int val ord = int_ord);
   106 
   107 local
   108 
   109 type kind =
   110  {name: string,
   111   empty: Object.T,
   112   copy: Object.T -> Object.T,
   113   extend: Object.T -> Object.T,
   114   merge: Pretty.pp -> Object.T * Object.T -> Object.T};
   115 
   116 val kinds = ref (Datatab.empty: kind Datatab.table);
   117 
   118 fun invoke meth_name meth_fn k =
   119   (case Datatab.lookup (! kinds) k of
   120     SOME kind => meth_fn kind |> transform_failure (fn exn =>
   121       EXCEPTION (exn, "Theory data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
   122   | NONE => sys_error ("Invalid theory data identifier " ^ string_of_int k));
   123 
   124 in
   125 
   126 fun invoke_name k    = invoke "name" (K o #name) k ();
   127 fun invoke_empty k   = invoke "empty" (K o #empty) k ();
   128 val invoke_copy      = invoke "copy" #copy;
   129 val invoke_extend    = invoke "extend" #extend;
   130 fun invoke_merge pp  = invoke "merge" (fn kind => #merge kind pp);
   131 
   132 fun declare_theory_data name empty copy extend merge =
   133   let
   134     val k = serial ();
   135     val kind = {name = name, empty = empty, copy = copy, extend = extend, merge = merge};
   136     val _ =
   137       if Datatab.exists (equal name o #name o #2) (! kinds) then
   138         warning ("Duplicate declaration of theory data " ^ quote name)
   139       else ();
   140     val _ = change kinds (Datatab.update (k, kind));
   141   in k end;
   142 
   143 val copy_data = Datatab.map' invoke_copy;
   144 val extend_data = Datatab.map' invoke_extend;
   145 fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
   146 
   147 end;
   148 
   149 
   150 
   151 (** datatype theory **)
   152 
   153 datatype theory =
   154   Theory of
   155    (*identity*)
   156    {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
   157     id: serial * string,                (*identifier of this theory*)
   158     ids: string Inttab.table,           (*identifiers of ancestors*)
   159     iids: string Inttab.table} *        (*identifiers of intermediate checkpoints*)
   160    (*data*)
   161    {theory: Object.T Datatab.table,     (*theory data record*)
   162     proof: unit Datatab.table} *        (*proof data kinds*)
   163    (*ancestry*)
   164    {parents: theory list,               (*immediate predecessors*)
   165     ancestors: theory list} *           (*all predecessors*)
   166    (*history*)
   167    {name: string,                       (*prospective name of finished theory*)
   168     version: int,                       (*checkpoint counter*)
   169     intermediates: theory list};        (*intermediate checkpoints*)
   170 
   171 exception THEORY of string * theory list;
   172 
   173 fun rep_theory (Theory args) = args;
   174 
   175 val identity_of = #1 o rep_theory;
   176 val data_of     = #2 o rep_theory;
   177 val ancestry_of = #3 o rep_theory;
   178 val history_of  = #4 o rep_theory;
   179 
   180 fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   181 fun make_data theory proof = {theory = theory, proof = proof};
   182 fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   183 fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   184 
   185 fun map_theory_data f {theory, proof} = make_data (f theory) proof;
   186 fun map_proof_data f {theory, proof} = make_data theory (f proof);
   187 
   188 val the_self = the o #self o identity_of;
   189 val parents_of = #parents o ancestry_of;
   190 val ancestors_of = #ancestors o ancestry_of;
   191 val theory_name = #name o history_of;
   192 
   193 
   194 (* staleness *)
   195 
   196 fun eq_id ((i: int, _), (j, _)) = (i = j);
   197 
   198 fun is_stale
   199     (Theory ({self = SOME (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, id, ids, iids}, data, ancestry, history)) =
   205       let
   206         val r = ref thy;
   207         val thy' = Theory (make_identity (SOME r) id ids iids, data, ancestry, history);
   208       in r := thy'; thy' end;
   209 
   210 
   211 (* names *)
   212 
   213 val ProtoPureN = "ProtoPure";
   214 val PureN = "Pure";
   215 val CPureN = "CPure";
   216 
   217 val draftN = "#";
   218 fun draft_id (_, name) = (name = draftN);
   219 val is_draft = draft_id o #id o identity_of;
   220 
   221 fun exists_name name (thy as Theory ({id, ids, iids, ...}, _, _, _)) =
   222   name = theory_name thy orelse
   223   name = #2 id orelse
   224   Inttab.exists (equal name o #2) ids orelse
   225   Inttab.exists (equal name o #2) iids;
   226 
   227 fun names_of (Theory ({id, ids, iids, ...}, _, _, _)) =
   228   rev (#2 id :: Inttab.fold (cons o #2) iids (Inttab.fold (cons o #2) ids []));
   229 
   230 fun pretty_thy thy =
   231   Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   232 
   233 val string_of_thy = Pretty.string_of o pretty_thy;
   234 val pprint_thy = Pretty.pprint o pretty_thy;
   235 
   236 fun pretty_abbrev_thy thy =
   237   let
   238     val names = names_of thy;
   239     val n = length names;
   240     val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
   241   in Pretty.str_list "{" "}" abbrev end;
   242 
   243 val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
   244 
   245 
   246 (* consistency *)    (*exception TERM*)
   247 
   248 fun check_thy thy =
   249   if is_stale thy then
   250     raise TERM ("Stale theory encountered:\n" ^ string_of_thy thy, [])
   251   else thy;
   252 
   253 fun check_ins id ids =
   254   if draft_id id orelse Inttab.defined ids (#1 id) then ids
   255   else if Inttab.exists (equal (#2 id) o #2) ids then
   256     raise TERM ("Different versions of theory component " ^ quote (#2 id), [])
   257   else Inttab.update id ids;
   258 
   259 fun check_insert intermediate id (ids, iids) =
   260   let val ids' = check_ins id ids and iids' = check_ins id iids
   261   in if intermediate then (ids, iids') else (ids', iids) end;
   262 
   263 fun check_merge
   264     (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
   265     (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
   266   (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
   267   |> check_insert (#version history1 > 0) id1
   268   |> check_insert (#version history2 > 0) id2;
   269 
   270 
   271 (* equality and inclusion *)
   272 
   273 val eq_thy = eq_id o pairself (#id o identity_of o check_thy);
   274 val thy_ord = int_ord o pairself (#1 o #id o identity_of);
   275 
   276 fun proper_subthy
   277     (Theory ({id = (i, _), ...}, _, _, _), Theory ({ids, iids, ...}, _, _, _)) =
   278   Inttab.defined ids i orelse Inttab.defined iids i;
   279 
   280 fun subthy thys = eq_thy thys orelse proper_subthy thys;
   281 
   282 fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
   283 
   284 
   285 (* theory references *)
   286 
   287 (*theory_ref provides a safe way to store dynamic references to a
   288   theory in external data structures -- a plain theory value would
   289   become stale as the self reference moves on*)
   290 
   291 datatype theory_ref = TheoryRef of theory ref;
   292 
   293 val self_ref = TheoryRef o the_self o check_thy;
   294 fun deref (TheoryRef (ref thy)) = thy;
   295 
   296 
   297 (* trivial merge *)    (*exception TERM*)
   298 
   299 fun merge (thy1, thy2) =
   300   if eq_thy (thy1, thy2) then thy1
   301   else if proper_subthy (thy2, thy1) then thy1
   302   else if proper_subthy (thy1, thy2) then thy2
   303   else (check_merge thy1 thy2;
   304     raise TERM (cat_lines ["Attempt to perform non-trivial merge of theories:",
   305       str_of_thy thy1, str_of_thy thy2], []));
   306 
   307 fun merge_refs (ref1, ref2) =
   308   if ref1 = ref2 then ref1
   309   else self_ref (merge (deref ref1, deref ref2));
   310 
   311 
   312 
   313 (** build theories **)
   314 
   315 (* primitives *)
   316 
   317 fun create_thy name self id ids iids data ancestry history =
   318   let
   319     val {version, name = _, intermediates = _} = history;
   320     val intermediate = version > 0;
   321     val (ids', iids') = check_insert intermediate id (ids, iids);
   322     val id' = (serial (), name);
   323     val _ = check_insert intermediate id' (ids', iids');
   324     val identity' = make_identity self id' ids' iids';
   325   in vitalize (Theory (identity', data, ancestry, history)) end;
   326 
   327 fun change_thy name f (thy as Theory ({self, id, ids, iids}, data, ancestry, history)) =
   328   let
   329     val _ = check_thy thy;
   330     val (self', data', ancestry') =
   331       if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   332       else if #version history > 0
   333       then (NONE, map_theory_data copy_data data, ancestry)
   334       else (NONE, map_theory_data extend_data data,
   335         make_ancestry [thy] (thy :: #ancestors ancestry));
   336     val data'' = f data';
   337   in create_thy name self' id ids iids data'' ancestry' history end;
   338 
   339 fun name_thy name = change_thy name I;
   340 val modify_thy = change_thy draftN;
   341 val extend_thy = modify_thy I;
   342 
   343 fun copy_thy (thy as Theory ({id, ids, iids, ...}, data, ancestry, history)) =
   344   (check_thy thy;
   345     create_thy draftN NONE id ids iids (map_theory_data copy_data data) ancestry history);
   346 
   347 val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   348   (make_data Datatab.empty Datatab.empty) (make_ancestry [] []) (make_history ProtoPureN 0 []);
   349 
   350 
   351 (* named theory nodes *)
   352 
   353 fun merge_thys pp (thy1, thy2) =
   354   if exists_name CPureN thy1 <> exists_name CPureN thy2 then
   355     error "Cannot merge Pure and CPure developments"
   356   else
   357     let
   358       val (ids, iids) = check_merge thy1 thy2;
   359       val data1 = data_of thy1 and data2 = data_of thy2;
   360       val data = make_data
   361         (merge_data (pp thy1) (#theory data1, #theory data2))
   362         (Datatab.merge (K true) (#proof data1, #proof data2));
   363       val ancestry = make_ancestry [] [];
   364       val history = make_history "" 0 [];
   365     in create_thy draftN NONE (serial (), draftN) ids iids data ancestry history end;
   366 
   367 fun maximal_thys thys =
   368   thys |> filter (fn thy => not (exists (fn thy' => proper_subthy (thy, thy')) thys));
   369 
   370 fun begin_thy pp name imports =
   371   if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   372   else
   373     let
   374       val parents =
   375         maximal_thys (distinct eq_thy (map check_thy imports));
   376       val ancestors = distinct eq_thy (parents @ maps ancestors_of parents);
   377       val Theory ({id, ids, iids, ...}, data, _, _) =
   378         (case parents of
   379           [] => error "No parent theories"
   380         | [thy] => extend_thy thy
   381         | thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
   382       val ancestry = make_ancestry parents ancestors;
   383       val history = make_history name 0 [];
   384     in create_thy draftN NONE id ids iids data ancestry history end;
   385 
   386 
   387 (* undoable checkpoints *)
   388 
   389 fun checkpoint_thy thy =
   390   if not (is_draft thy) then thy
   391   else
   392     let
   393       val {name, version, intermediates} = history_of thy;
   394       val thy' as Theory (identity', data', ancestry', _) =
   395         name_thy (name ^ ":" ^ string_of_int version) thy;
   396       val history' = make_history name (version + 1) (thy' :: intermediates);
   397     in vitalize (Theory (identity', data', ancestry', history')) end;
   398 
   399 fun finish_thy thy =
   400   let
   401     val {name, version, intermediates} = history_of thy;
   402     val rs = map (the_self o check_thy) intermediates;
   403     val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
   404     val identity' = make_identity self id ids Inttab.empty;
   405     val history' = make_history name 0 [];
   406     val thy'' = vitalize (Theory (identity', data', ancestry', history'));
   407     val _ = List.app (fn r => r := thy'') rs;
   408   in thy'' end;
   409 
   410 
   411 (* theory data *)
   412 
   413 fun dest_data name_of tab =
   414   map name_of (Datatab.keys tab)
   415   |> map (rpair ()) |> Symtab.make_list |> Symtab.dest
   416   |> map (apsnd length)
   417   |> map (fn (name, 1) => name | (name, n) => name ^ enclose "[" "]" (string_of_int n));
   418 
   419 val theory_data_of = dest_data invoke_name o #theory o data_of;
   420 
   421 structure TheoryData =
   422 struct
   423 
   424 val declare = declare_theory_data;
   425 
   426 fun get k dest thy =
   427   (case Datatab.lookup (#theory (data_of thy)) k of
   428     SOME x => (dest x handle Match =>
   429       error ("Failed to access theory data " ^ quote (invoke_name k)))
   430   | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
   431 
   432 fun put k mk x = modify_thy (map_theory_data (Datatab.update (k, mk x)));
   433 fun init k = put k I (invoke_empty k);
   434 
   435 end;
   436 
   437 
   438 
   439 (*** proof context ***)
   440 
   441 (* datatype proof *)
   442 
   443 datatype proof = Proof of theory_ref * Object.T Datatab.table;
   444 
   445 fun theory_of_proof (Proof (thy_ref, _)) = deref thy_ref;
   446 fun data_of_proof (Proof (_, data)) = data;
   447 fun map_prf f (Proof (thy_ref, data)) = Proof (thy_ref, f data);
   448 
   449 fun transfer_proof thy' (prf as Proof (thy_ref, data)) =
   450   if not (subthy (deref thy_ref, thy')) then
   451     error "transfer proof context: not a super theory"
   452   else Proof (self_ref thy', data);
   453 
   454 
   455 (* proof data kinds *)
   456 
   457 local
   458 
   459 type kind =
   460  {name: string,
   461   init: theory -> Object.T};
   462 
   463 val kinds = ref (Datatab.empty: kind Datatab.table);
   464 
   465 fun invoke meth_name meth_fn k =
   466   (case Datatab.lookup (! kinds) k of
   467     SOME kind => meth_fn kind |> transform_failure (fn exn =>
   468       EXCEPTION (exn, "Proof data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
   469   | NONE => sys_error ("Invalid proof data identifier " ^ string_of_int k));
   470 
   471 fun invoke_name k = invoke "name" (K o #name) k ();
   472 val invoke_init   = invoke "init" #init;
   473 
   474 in
   475 
   476 val proof_data_of = dest_data invoke_name o #proof o data_of;
   477 
   478 fun init_proof thy =
   479   Proof (self_ref thy, Datatab.map' (fn k => fn _ => invoke_init k thy) (#proof (data_of thy)));
   480 
   481 structure ProofData =
   482 struct
   483 
   484 fun declare name init =
   485   let
   486     val k = serial ();
   487     val kind = {name = name, init = init};
   488     val _ =
   489       if Datatab.exists (equal name o #name o #2) (! kinds) then
   490         warning ("Duplicate declaration of proof data " ^ quote name)
   491       else ();
   492     val _ = change kinds (Datatab.update (k, kind));
   493   in k end;
   494 
   495 fun init k = modify_thy (map_proof_data (Datatab.update (k, ())));
   496 
   497 fun get k dest prf =
   498   (case Datatab.lookup (data_of_proof prf) k of
   499     SOME x => (dest x handle Match =>
   500       error ("Failed to access proof data " ^ quote (invoke_name k)))
   501   | NONE => error ("Uninitialized proof data " ^ quote (invoke_name k)));
   502 
   503 fun put k mk x = map_prf (Datatab.update (k, mk x));
   504 
   505 end;
   506 
   507 end;
   508 
   509 
   510 
   511 (*** generic context ***)
   512 
   513 datatype generic = Theory of theory | Proof of proof;
   514 
   515 fun cases f _ (Theory thy) = f thy
   516   | cases _ g (Proof prf) = g prf;
   517 
   518 fun mapping f g = cases (Theory o f) (Proof o g);
   519 fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
   520 
   521 val the_theory = cases I (fn _ => raise Fail "Ill-typed context: theory expected");
   522 val the_proof = cases (fn _ => raise Fail "Ill-typed context: proof expected") I;
   523 
   524 fun map_theory f = Theory o f o the_theory;
   525 fun map_proof f = Proof o f o the_proof;
   526 
   527 fun theory_map f = the_theory o f o Theory;
   528 fun proof_map f = the_proof o f o Proof;
   529 
   530 val theory_of = cases I theory_of_proof;
   531 val proof_of = cases init_proof I;
   532 
   533 
   534 
   535 (** delayed theory setup **)
   536 
   537 local
   538   val setup_fn = ref (I: theory -> theory);
   539 in
   540   fun add_setup f = setup_fn := (! setup_fn #> f);
   541   fun setup () = let val f = ! setup_fn in setup_fn := I; f end;
   542 end;
   543 
   544 end;
   545 
   546 structure BasicContext: BASIC_CONTEXT = Context;
   547 open BasicContext;
   548 
   549 
   550 
   551 (*** type-safe interfaces for data declarations ***)
   552 
   553 (** theory data **)
   554 
   555 signature THEORY_DATA_ARGS =
   556 sig
   557   val name: string
   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   val print: theory -> T -> unit
   564 end;
   565 
   566 signature THEORY_DATA =
   567 sig
   568   type T
   569   val init: theory -> theory
   570   val print: theory -> unit
   571   val get: theory -> T
   572   val get_sg: theory -> T    (*obsolete*)
   573   val put: T -> theory -> theory
   574   val map: (T -> T) -> theory -> theory
   575 end;
   576 
   577 functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
   578 struct
   579 
   580 structure TheoryData = Context.TheoryData;
   581 
   582 type T = Data.T;
   583 exception Data of T;
   584 
   585 val kind = TheoryData.declare Data.name
   586   (Data Data.empty)
   587   (fn Data x => Data (Data.copy x))
   588   (fn Data x => Data (Data.extend x))
   589   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   590 
   591 val init = TheoryData.init kind;
   592 val get = TheoryData.get kind (fn Data x => x);
   593 val get_sg = get;
   594 fun print thy = Data.print thy (get thy);
   595 val put = TheoryData.put kind Data;
   596 fun map f thy = put (f (get thy)) thy;
   597 
   598 end;
   599 
   600 
   601 
   602 (** proof data **)
   603 
   604 signature PROOF_DATA_ARGS =
   605 sig
   606   val name: string
   607   type T
   608   val init: theory -> T
   609   val print: Context.proof -> T -> unit
   610 end;
   611 
   612 signature PROOF_DATA =
   613 sig
   614   type T
   615   val init: theory -> theory
   616   val print: Context.proof -> unit
   617   val get: Context.proof -> T
   618   val put: T -> Context.proof -> Context.proof
   619   val map: (T -> T) -> Context.proof -> Context.proof
   620 end;
   621 
   622 functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
   623 struct
   624 
   625 structure ProofData = Context.ProofData;
   626 
   627 type T = Data.T;
   628 exception Data of T;
   629 
   630 val kind = ProofData.declare Data.name (Data o Data.init);
   631 
   632 val init = ProofData.init kind;
   633 val get = ProofData.get kind (fn Data x => x);
   634 fun print prf = Data.print prf (get prf);
   635 val put = ProofData.put kind Data;
   636 fun map f prf = put (f (get prf)) prf;
   637 
   638 end;
   639 
   640 
   641 
   642 (** generic data **)
   643 
   644 signature GENERIC_DATA_ARGS =
   645 sig
   646   val name: string
   647   type T
   648   val empty: T
   649   val extend: T -> T
   650   val merge: Pretty.pp -> T * T -> T
   651   val print: Context.generic -> T -> unit
   652 end;
   653 
   654 signature GENERIC_DATA =
   655 sig
   656   type T
   657   val init: theory -> theory
   658   val get: Context.generic -> T
   659   val put: T -> Context.generic -> Context.generic
   660   val map: (T -> T) -> Context.generic -> Context.generic
   661   val print: Context.generic -> unit
   662 end;
   663 
   664 functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
   665 struct
   666 
   667 structure ThyData = TheoryDataFun(open Data val copy = I fun print _ _ = ());
   668 structure PrfData =
   669   ProofDataFun(val name = Data.name type T = Data.T val init = ThyData.get fun print _ _ = ());
   670 
   671 type T = Data.T;
   672 val init = ThyData.init #> PrfData.init;
   673 
   674 fun get (Context.Theory thy) = ThyData.get thy
   675   | get (Context.Proof prf) = PrfData.get prf;
   676 
   677 fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
   678   | put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
   679 
   680 fun map f ctxt = put (f (get ctxt)) ctxt;
   681 
   682 fun print ctxt = Data.print ctxt (get ctxt);
   683 
   684 end;
   685 
   686 (*hide private interface*)
   687 structure Context: CONTEXT = Context;
   688 
   689 (*fake predeclarations*)
   690 structure Proof = struct type context = Context.proof end;
   691 structure ProofContext =
   692 struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;