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