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