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