improved treatment of intermediate checkpoints: actual copy
authorwenzelm
Mon Jun 20 22:14:03 2005 +0200 (2005-06-20)
changeset 16489f66ab8a4e98f
parent 16488 38bc902946b2
child 16490 e10b0d5fa33a
improved treatment of intermediate checkpoints: actual copy
instead of extend, purge after end;
tuned;
src/Pure/context.ML
     1.1 --- a/src/Pure/context.ML	Mon Jun 20 22:14:02 2005 +0200
     1.2 +++ b/src/Pure/context.ML	Mon Jun 20 22:14:03 2005 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4      Author:     Markus Wenzel, TU Muenchen
     1.5  
     1.6  Generic theory contexts with unique identity, arbitrarily typed data,
     1.7 -linear and graph development.  Implicit ML theory context.
     1.8 +graph-based development.  Implicit theory context in ML.
     1.9  *)
    1.10  
    1.11  signature BASIC_CONTEXT =
    1.12 @@ -18,7 +18,9 @@
    1.13  signature CONTEXT =
    1.14  sig
    1.15    include BASIC_CONTEXT
    1.16 +  exception DATA_FAIL of exn * string
    1.17    (*theory context*)
    1.18 +  val theory_name: theory -> string
    1.19    val parents_of: theory -> theory list
    1.20    val ancestors_of: theory -> theory list
    1.21    val is_stale: theory -> bool
    1.22 @@ -26,7 +28,6 @@
    1.23    val PureN: string
    1.24    val CPureN: string
    1.25    val draftN: string
    1.26 -  val is_draft: theory -> bool
    1.27    val exists_name: string -> theory -> bool
    1.28    val names_of: theory -> string list
    1.29    val pretty_thy: theory -> Pretty.T
    1.30 @@ -37,21 +38,17 @@
    1.31    val check_thy: string -> theory -> theory
    1.32    val eq_thy: theory * theory -> bool
    1.33    val subthy: theory * theory -> bool
    1.34 +  val merge: theory * theory -> theory                     (*exception TERM*)
    1.35 +  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    1.36    val self_ref: theory -> theory_ref
    1.37    val deref: theory_ref -> theory
    1.38 -  exception DATA_FAIL of exn * string
    1.39 -  val theory_data: theory -> string list
    1.40 -  val print_all_data: theory -> unit
    1.41    val copy_thy: theory -> theory
    1.42    val checkpoint_thy: theory -> theory
    1.43 -  val pre_pure: theory
    1.44 -  val merge: theory * theory -> theory                     (*exception TERM*)
    1.45 -  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
    1.46 -  val theory_name: theory -> string
    1.47 -  val begin_theory: (theory -> Pretty.pp) -> string -> theory list -> theory
    1.48 -  val end_theory: theory -> theory
    1.49 -
    1.50 -  (*ML context*)
    1.51 +  val finish_thy: theory -> theory
    1.52 +  val theory_data: theory -> string list
    1.53 +  val pre_pure_thy: theory
    1.54 +  val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
    1.55 +  (*ML theory context*)
    1.56    val get_context: unit -> theory option
    1.57    val set_context: theory option -> unit
    1.58    val reset_context: unit -> unit
    1.59 @@ -73,14 +70,12 @@
    1.60    include CONTEXT
    1.61    structure TheoryData:
    1.62    sig
    1.63 -    val declare: string -> Object.T -> (Object.T -> Object.T) ->
    1.64 -      (Object.T -> Object.T) -> (Pretty.pp -> Object.T * Object.T -> Object.T) ->
    1.65 -      (theory -> Object.T -> unit) -> serial
    1.66 +    val declare: string -> Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
    1.67 +      (Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
    1.68      val init: serial -> theory -> theory
    1.69 -    val print: serial -> theory -> unit
    1.70      val get: serial -> (Object.T -> 'a) -> theory -> 'a
    1.71      val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
    1.72 -  end;
    1.73 +  end
    1.74  end;
    1.75  
    1.76  structure Context: PRIVATE_CONTEXT =
    1.77 @@ -88,21 +83,72 @@
    1.78  
    1.79  (*** theory context ***)
    1.80  
    1.81 +(** theory data **)
    1.82 +
    1.83 +(* data kinds and access methods *)
    1.84 +
    1.85 +exception DATA_FAIL of exn * string;
    1.86 +
    1.87 +local
    1.88 +
    1.89 +type kind =
    1.90 + {name: string,
    1.91 +  empty: Object.T,
    1.92 +  copy: Object.T -> Object.T,
    1.93 +  extend: Object.T -> Object.T,
    1.94 +  merge: Pretty.pp -> Object.T * Object.T -> Object.T};
    1.95 +
    1.96 +val kinds = ref (Inttab.empty: kind Inttab.table);
    1.97 +
    1.98 +fun invoke meth_name meth_fn k =
    1.99 +  (case Inttab.lookup (! kinds, k) of
   1.100 +    SOME kind => meth_fn kind |> transform_failure (fn exn =>
   1.101 +      DATA_FAIL (exn, "Theory data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
   1.102 +  | NONE => sys_error ("Invalid theory data identifier " ^ string_of_int k));
   1.103 +
   1.104 +in
   1.105 +
   1.106 +fun invoke_name k    = invoke "name" (K o #name) k ();
   1.107 +fun invoke_empty k   = invoke "empty" (K o #empty) k ();
   1.108 +val invoke_copy      = invoke "copy" #copy;
   1.109 +val invoke_extend    = invoke "extend" #extend;
   1.110 +fun invoke_merge pp  = invoke "merge" (fn kind => #merge kind pp);
   1.111 +
   1.112 +fun declare_theory_data name e cp ext mrg =
   1.113 +  let
   1.114 +    val k = serial ();
   1.115 +    val kind = {name = name, empty = e, copy = cp, extend = ext, merge = mrg};
   1.116 +    val _ = conditional (Inttab.exists (equal name o #name o #2) (! kinds)) (fn () =>
   1.117 +      warning ("Duplicate declaration of theory data " ^ quote name));
   1.118 +    val _ = kinds := Inttab.update ((k, kind), ! kinds);
   1.119 +  in k end;
   1.120 +
   1.121 +val copy_data = Inttab.map' invoke_copy;
   1.122 +val extend_data = Inttab.map' invoke_extend;
   1.123 +fun merge_data pp = Inttab.join (SOME oo invoke_merge pp) o pairself extend_data;
   1.124 +
   1.125 +end;
   1.126 +
   1.127 +
   1.128 +
   1.129 +(** datatype theory **)
   1.130 +
   1.131  datatype theory =
   1.132    Theory of
   1.133    (*identity*)
   1.134 -   {self: theory ref option,
   1.135 -    id: serial * string,
   1.136 -    ids: string Inttab.table} *
   1.137 +   {self: theory ref option,            (*dynamic self reference -- follows theory changes*)
   1.138 +    id: serial * string,                (*identifier of this theory*)
   1.139 +    ids: string Inttab.table,           (*identifiers of ancestors*)
   1.140 +    iids: string Inttab.table} *        (*identifiers of intermediate checkpoints*)
   1.141    (*data*)
   1.142 -  Object.T Inttab.table *
   1.143 +  Object.T Inttab.table *               (*record of arbitrarily typed data*)
   1.144 +  (*ancestry of graph development*)
   1.145 +   {parents: theory list,               (*immediate predecessors*)
   1.146 +    ancestors: theory list} *           (*all predecessors*)
   1.147    (*history of linear development*)
   1.148 -   {name: string,
   1.149 -    version: int,
   1.150 -    intermediates: theory list} *
   1.151 -  (*ancestry of graph development*)
   1.152 -   {parents: theory list,
   1.153 -    ancestors: theory list};
   1.154 +   {name: string,                       (*prospective name of finished theory*)
   1.155 +    version: int,                       (*checkpoint counter*)
   1.156 +    intermediates: theory list};        (*intermediate checkpoints*)
   1.157  
   1.158  exception THEORY of string * theory list;
   1.159  
   1.160 @@ -110,20 +156,18 @@
   1.161  
   1.162  val identity_of = #1 o rep_theory;
   1.163  val data_of     = #2 o rep_theory;
   1.164 -val history_of  = #3 o rep_theory;
   1.165 -val ancestry_of = #4 o rep_theory;
   1.166 +val ancestry_of = #3 o rep_theory;
   1.167 +val history_of  = #4 o rep_theory;
   1.168  
   1.169 -fun make_identity self id ids = {self = self, id = id, ids = ids};
   1.170 +fun make_identity self id ids iids = {self = self, id = id, ids = ids, iids = iids};
   1.171 +fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   1.172  fun make_history name vers ints = {name = name, version = vers, intermediates = ints};
   1.173 -fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
   1.174  
   1.175  val parents_of = #parents o ancestry_of;
   1.176  val ancestors_of = #ancestors o ancestry_of;
   1.177 -
   1.178 +val theory_name = #name o history_of;
   1.179  
   1.180  
   1.181 -(** theory identity **)
   1.182 -
   1.183  (* staleness *)
   1.184  
   1.185  fun eq_id ((i: int, _), (j, _)) = i = j;
   1.186 @@ -134,10 +178,10 @@
   1.187    | is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
   1.188  
   1.189  fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
   1.190 -  | vitalize (thy as Theory ({self = NONE, id, ids}, data, history, ancestry)) =
   1.191 +  | vitalize (thy as Theory ({self = NONE, id, ids, iids}, data, ancestry, history)) =
   1.192        let
   1.193          val r = ref thy;
   1.194 -        val thy' = Theory (make_identity (SOME r) id ids, data, history, ancestry);
   1.195 +        val thy' = Theory (make_identity (SOME r) id ids iids, data, ancestry, history);
   1.196        in r := thy'; thy' end;
   1.197  
   1.198  
   1.199 @@ -151,11 +195,13 @@
   1.200  fun draft_id (_, name) = (name = draftN);
   1.201  val is_draft = draft_id o #id o identity_of;
   1.202  
   1.203 -fun exists_name name (Theory ({id, ids, ...}, _, _, _)) =
   1.204 -  name = #2 id orelse Inttab.exists (equal name o #2) ids;
   1.205 +fun exists_name name (Theory ({id, ids, iids, ...}, _, _, _)) =
   1.206 +  name = #2 id orelse
   1.207 +  Inttab.exists (equal name o #2) ids orelse
   1.208 +  Inttab.exists (equal name o #2) iids;
   1.209  
   1.210 -fun names_of (Theory ({id, ids, ...}, _, _, _)) =
   1.211 -  map #2 (Inttab.dest ids @ [id]);
   1.212 +fun names_of (Theory ({id, ids, iids, ...}, _, _, _)) =
   1.213 +  rev (#2 id :: Inttab.fold (cons o #2) iids (Inttab.fold (cons o #2) ids []));
   1.214  
   1.215  fun pretty_thy thy =
   1.216    Pretty.str_list "{" "}" (names_of thy @ (if is_stale thy then ["!"] else []));
   1.217 @@ -177,33 +223,54 @@
   1.218  
   1.219  fun check_thy pos thy =
   1.220    if is_stale thy then
   1.221 -    raise TERM ("Stale theory encountered (see " ^ pos ^ "):\n" ^ string_of_thy thy, [])
   1.222 +    raise TERM ("Stale theory encountered (in " ^ pos ^ "):\n" ^ string_of_thy thy, [])
   1.223    else thy;
   1.224  
   1.225 -fun check_insert id ids =
   1.226 +fun check_ins id ids =
   1.227    if draft_id id orelse is_some (Inttab.lookup (ids, #1 id)) then ids
   1.228    else if Inttab.exists (equal (#2 id) o #2) ids then
   1.229      raise TERM ("Different versions of theory component " ^ quote (#2 id), [])
   1.230    else Inttab.update (id, ids);
   1.231  
   1.232 -fun check_merge thy1 thy2 =
   1.233 -  let
   1.234 -    val {id = id1, ids = ids1, ...} = identity_of thy1;
   1.235 -    val {id = id2, ids = ids2, ...} = identity_of thy2;
   1.236 -  in check_insert id2 (Inttab.fold check_insert ids2 (check_insert id1 ids1)) end;
   1.237 +fun check_insert intermediate id (ids, iids) =
   1.238 +  let val ids' = check_ins id ids and iids' = check_ins id iids
   1.239 +  in if intermediate then (ids, iids') else (ids', iids) end;
   1.240 +
   1.241 +fun check_merge
   1.242 +    (Theory ({id = id1, ids = ids1, iids = iids1, ...}, _, _, history1))
   1.243 +    (Theory ({id = id2, ids = ids2, iids = iids2, ...}, _, _, history2)) =
   1.244 +  (Inttab.fold check_ins ids2 ids1, Inttab.fold check_ins iids2 iids1)
   1.245 +  |> check_insert (#version history1 > 0) id1
   1.246 +  |> check_insert (#version history2 > 0) id2;
   1.247  
   1.248  
   1.249 -(* equality and inclusion *)
   1.250 +(* theory references *)
   1.251 +
   1.252 +(*theory_ref provides a safe way to store dynamic references to a
   1.253 +  theory -- a plain theory value would become stale as the self
   1.254 +  reference moves on*)
   1.255 +
   1.256 +datatype theory_ref = TheoryRef of theory ref;
   1.257 +
   1.258 +val self_ref = TheoryRef o the o #self o identity_of o check_thy "Context.self_ref";
   1.259 +fun deref (TheoryRef (ref thy)) = thy;
   1.260 +
   1.261 +
   1.262 +(* equality and inclusion *)  (* FIXME proper_subthy; no subthy_internal *)
   1.263  
   1.264  local
   1.265  
   1.266 -fun exists_ids (Theory ({id, ids, ...}, _, _, _)) (i, _) =
   1.267 -  i = #1 id orelse is_some (Inttab.lookup (ids, i));
   1.268 +fun exists_ids (Theory ({id, ids, iids, ...}, _, _, _)) (i, _) =
   1.269 +  i = #1 id orelse
   1.270 +  is_some (Inttab.lookup (ids, i)) orelse
   1.271 +  is_some (Inttab.lookup (iids, i));
   1.272  
   1.273  fun member_ids (Theory ({id, ...}, _, _, _), thy) = exists_ids thy id;
   1.274  
   1.275 -fun subset_ids (Theory ({id, ids, ...}, _, _, _), thy) =
   1.276 -  exists_ids thy id andalso Inttab.forall (exists_ids thy) ids;
   1.277 +fun subset_ids (Theory ({id, ids, iids, ...}, _, _, _), thy) =
   1.278 +  exists_ids thy id andalso
   1.279 +  Inttab.forall (exists_ids thy) ids andalso
   1.280 +  Inttab.forall (exists_ids thy) iids;
   1.281  
   1.282  in
   1.283  
   1.284 @@ -214,139 +281,6 @@
   1.285  end;
   1.286  
   1.287  
   1.288 -(* external references *)
   1.289 -
   1.290 -datatype theory_ref = TheoryRef of theory ref;
   1.291 -
   1.292 -val self_ref = TheoryRef o the o #self o identity_of o check_thy "Context.self_ref";
   1.293 -fun deref (TheoryRef (ref thy)) = thy;
   1.294 -
   1.295 -
   1.296 -
   1.297 -(** theory data **)
   1.298 -
   1.299 -(* data kinds and access methods *)
   1.300 -
   1.301 -exception DATA_FAIL of exn * string;
   1.302 -
   1.303 -local
   1.304 -
   1.305 -type kind =
   1.306 - {name: string,
   1.307 -  empty: Object.T,
   1.308 -  copy: Object.T -> Object.T,
   1.309 -  extend: Object.T -> Object.T,
   1.310 -  merge: Pretty.pp -> Object.T * Object.T -> Object.T,
   1.311 -  print: theory -> Object.T -> unit};
   1.312 -
   1.313 -val kinds = ref (Inttab.empty: kind Inttab.table);
   1.314 -
   1.315 -fun invoke meth_name meth_fn k =
   1.316 -  (case Inttab.lookup (! kinds, k) of
   1.317 -    SOME kind => meth_fn kind |> transform_failure (fn exn =>
   1.318 -      DATA_FAIL (exn, "Theory data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
   1.319 -  | NONE => sys_error ("Invalid theory data identifier " ^ string_of_int k));
   1.320 -
   1.321 -in
   1.322 -
   1.323 -fun invoke_name k    = invoke "name" (K o #name) k ();
   1.324 -fun invoke_empty k   = invoke "empty" (K o #empty) k ();
   1.325 -val invoke_copy      = invoke "copy" #copy;
   1.326 -val invoke_extend    = invoke "extend" #extend;
   1.327 -fun invoke_merge pp  = invoke "merge" (fn kind => #merge kind pp);
   1.328 -fun invoke_print thy = invoke "print" (fn kind => #print kind thy);
   1.329 -
   1.330 -fun declare name e cp ext mrg prt =
   1.331 -  let
   1.332 -    val k = serial ();
   1.333 -    val kind = {name = name, empty = e, copy = cp, extend = ext, merge = mrg, print = prt};
   1.334 -    val _ = conditional (Inttab.exists (equal name o #name o #2) (! kinds)) (fn () =>
   1.335 -      warning ("Duplicate declaration of theory data " ^ quote name));
   1.336 -    val _ = kinds := Inttab.update ((k, kind), ! kinds);
   1.337 -  in k end;
   1.338 -
   1.339 -val copy_data = Inttab.map' invoke_copy;
   1.340 -val extend_data = Inttab.map' invoke_extend;
   1.341 -fun merge_data pp = Inttab.join (SOME oo invoke_merge pp) o pairself extend_data;
   1.342 -
   1.343 -fun theory_data thy =
   1.344 -  map invoke_name (Inttab.keys (data_of thy))
   1.345 -  |> map (rpair ()) |> Symtab.make_multi |> Symtab.dest
   1.346 -  |> map (apsnd length)
   1.347 -  |> map (fn (name, 1) => name | (name, n) => name ^ enclose "[" "]" (string_of_int n));
   1.348 -
   1.349 -fun print_all_data thy =
   1.350 -  List.app (uncurry (invoke_print thy)) (Inttab.dest (data_of thy));
   1.351 -
   1.352 -end;
   1.353 -
   1.354 -
   1.355 -
   1.356 -(** build theories **)
   1.357 -
   1.358 -(* primitives *)
   1.359 -
   1.360 -fun create_thy name self id ids data history ancestry =
   1.361 -  let
   1.362 -    val ids' = check_insert id ids;
   1.363 -    val id' = (serial (), name);
   1.364 -    val _ = check_insert id' ids';
   1.365 -    val identity' = make_identity self id' ids';
   1.366 -  in vitalize (Theory (identity', data, history, ancestry)) end;
   1.367 -
   1.368 -fun copy_thy (thy as Theory ({id, ids, ...}, data, history, ancestry)) =
   1.369 -  let val _ = check_thy "Context.copy_thy" thy;
   1.370 -  in create_thy draftN NONE id ids (copy_data data) history ancestry end;
   1.371 -
   1.372 -fun change_thy name f (thy as Theory ({self, id, ids}, data, history, ancestry)) =
   1.373 -  let
   1.374 -    val _ = check_thy "Context.change_thy" thy;
   1.375 -    val (self', data', ancestry') =
   1.376 -      if is_draft thy then (self, data, ancestry)
   1.377 -      else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   1.378 -  in create_thy name self' id ids (f data') history ancestry' end;
   1.379 -
   1.380 -fun name_thy name = change_thy name I;
   1.381 -val map_thy = change_thy draftN;
   1.382 -val extend_thy = map_thy I;
   1.383 -
   1.384 -fun checkpoint_thy thy =
   1.385 -  if not (is_draft thy) then thy
   1.386 -  else
   1.387 -    let
   1.388 -      val {name, version, intermediates} = history_of thy;
   1.389 -      val thy' as Theory (identity', data', _, ancestry') =
   1.390 -        name_thy (name ^ ":" ^ string_of_int version) thy;
   1.391 -      val history' = make_history name (version + 1) (thy' :: intermediates);
   1.392 -    in vitalize (Theory (identity', data', history', ancestry')) end;
   1.393 -
   1.394 -
   1.395 -(* theory data *)
   1.396 -
   1.397 -structure TheoryData =
   1.398 -struct
   1.399 -
   1.400 -val declare = declare;
   1.401 -
   1.402 -fun get k dest thy =
   1.403 -  (case Inttab.lookup (data_of thy, k) of
   1.404 -    SOME x => (dest x handle Match =>
   1.405 -      error ("Failed to access theory data " ^ quote (invoke_name k)))
   1.406 -  | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
   1.407 -
   1.408 -fun print k thy = invoke_print thy k (get k I thy);
   1.409 -fun put k mk x = map_thy (curry Inttab.update (k, mk x));
   1.410 -fun init k = put k I (invoke_empty k);
   1.411 -
   1.412 -end;
   1.413 -
   1.414 -
   1.415 -(* pre_pure *)
   1.416 -
   1.417 -val pre_pure = create_thy draftN NONE (serial (), draftN) Inttab.empty
   1.418 -  Inttab.empty (make_history ProtoPureN 0 []) (make_ancestry [] []);
   1.419 -
   1.420 -
   1.421  (* trivial merge *)
   1.422  
   1.423  fun merge (thy1, thy2) =
   1.424 @@ -359,52 +293,123 @@
   1.425  fun merge_refs (ref1, ref2) = self_ref (merge (deref ref1, deref ref2));
   1.426  
   1.427  
   1.428 -(* non-trivial merge *)
   1.429 +
   1.430 +(** build theories **)
   1.431 +
   1.432 +(* primitives *)
   1.433 +
   1.434 +fun create_thy name self id ids iids data ancestry history =
   1.435 +  let
   1.436 +    val intermediate = #version history > 0;
   1.437 +    val (ids', iids') = check_insert intermediate id (ids, iids);
   1.438 +    val id' = (serial (), name);
   1.439 +    val _ = check_insert intermediate id' (ids', iids');
   1.440 +    val identity' = make_identity self id' ids' iids';
   1.441 +  in vitalize (Theory (identity', data, ancestry, history)) end;
   1.442  
   1.443 -fun merge_internal pp (thy1, thy2) =
   1.444 +fun change_thy name f (thy as Theory ({self, id, ids, iids}, data, ancestry, history)) =
   1.445 +  let
   1.446 +    val _ = check_thy "Context.change_thy" thy;
   1.447 +    val (self', data', ancestry') =
   1.448 +      if is_draft thy then (self, data, ancestry)    (*destructive change!*)
   1.449 +      else if #version history > 0
   1.450 +      then (NONE, copy_data data, ancestry)
   1.451 +      else (NONE, extend_data data, make_ancestry [thy] (thy :: #ancestors ancestry));
   1.452 +    val data'' = f data';
   1.453 +  in create_thy name self' id ids iids data'' ancestry' history end;
   1.454 +
   1.455 +fun name_thy name = change_thy name I;
   1.456 +val modify_thy = change_thy draftN;
   1.457 +val extend_thy = modify_thy I;
   1.458 +
   1.459 +fun copy_thy (thy as Theory ({id, ids, iids, ...}, data, ancestry, history)) =
   1.460 +  (check_thy "Context.copy_thy" thy;
   1.461 +    create_thy draftN NONE id ids iids (copy_data data) ancestry history);
   1.462 +
   1.463 +val pre_pure_thy = create_thy draftN NONE (serial (), draftN) Inttab.empty Inttab.empty
   1.464 +  Inttab.empty (make_ancestry [] []) (make_history ProtoPureN 0 []);
   1.465 +
   1.466 +
   1.467 +(* named theory nodes *)
   1.468 +
   1.469 +fun merge_thys pp (thy1, thy2) =
   1.470    if subthy_internal (thy2, thy1) then thy1
   1.471    else if subthy_internal (thy1, thy2) then thy2
   1.472    else if exists_name CPureN thy1 <> exists_name CPureN thy2 then
   1.473      error "Cannot merge Pure and CPure developments"
   1.474    else
   1.475      let
   1.476 -      val ids = check_merge thy1 thy2;
   1.477 +      val (ids, iids) = check_merge thy1 thy2;
   1.478        val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
   1.479 +      val ancestry = make_ancestry [] [];
   1.480        val history = make_history "" 0 [];
   1.481 -      val ancestry = make_ancestry [] [];
   1.482 -    in create_thy draftN NONE (serial (), draftN) ids data history ancestry end;
   1.483 +    in create_thy draftN NONE (serial (), draftN) ids iids data ancestry history end;
   1.484 +
   1.485 +fun begin_thy pp name imports =
   1.486 +  if name = draftN then error ("Illegal theory name: " ^ quote draftN)
   1.487 +  else
   1.488 +    let
   1.489 +      val parents = gen_distinct eq_thy imports;  (* FIXME maximals *)
   1.490 +      val ancestors = gen_distinct eq_thy (parents @ List.concat (map ancestors_of parents));
   1.491 +      val Theory ({id, ids, iids, ...}, data, _, _) =
   1.492 +        (case parents of
   1.493 +          [] => error "No parent theories"
   1.494 +        | thy :: thys => extend_thy (Library.foldl (merge_thys pp) (thy, thys)));
   1.495 +      val ancestry = make_ancestry parents ancestors;
   1.496 +      val history = make_history name 0 [];
   1.497 +    in create_thy draftN NONE id ids iids data ancestry history end;
   1.498  
   1.499  
   1.500 -(* named theory nodes *)
   1.501 -
   1.502 -val theory_name = #name o history_of;
   1.503 +(* undoable checkpoints *)
   1.504  
   1.505 -fun begin_theory pp name imports =
   1.506 -  if name = draftN then
   1.507 -    error ("Illegal theory name: " ^ quote draftN)
   1.508 -  else if exists is_draft imports then
   1.509 -    error "Attempt to import draft theories"
   1.510 +fun checkpoint_thy thy =
   1.511 +  if not (is_draft thy) then thy
   1.512    else
   1.513      let
   1.514 -      val parents = gen_distinct eq_thy imports;
   1.515 -      val ancestors = gen_distinct eq_thy (parents @ List.concat (map ancestors_of parents));
   1.516 -      val Theory ({id, ids, ...}, data, _, _) =
   1.517 -        (case parents of
   1.518 -          [] => error "No parent theories"
   1.519 -        | thy :: thys => extend_thy (Library.foldl (merge_internal pp) (thy, thys)));
   1.520 -      val history = make_history name 0 [];
   1.521 -      val ancestry = make_ancestry parents ancestors;
   1.522 -    in create_thy draftN NONE id ids data history ancestry end;
   1.523 +      val {name, version, intermediates} = history_of thy;
   1.524 +      val thy' as Theory (identity', data', ancestry', _) =
   1.525 +        name_thy (name ^ ":" ^ string_of_int version) thy;
   1.526 +      val history' = make_history name (version + 1) (thy' :: intermediates);
   1.527 +    in vitalize (Theory (identity', data', ancestry', history')) end;
   1.528 +
   1.529 +fun finish_thy thy =
   1.530 +  let
   1.531 +    val {name, version, intermediates} = history_of thy;
   1.532 +    val rs = map (the o #self o identity_of o check_thy "Context.finish_thy") intermediates;
   1.533 +    val thy' as Theory ({self, id, ids, ...}, data', ancestry', _) = name_thy name thy;
   1.534 +    val identity' = make_identity self id ids Inttab.empty;
   1.535 +    val history' = make_history name 0 [];
   1.536 +    val thy'' = vitalize (Theory (identity', data', ancestry', history'));
   1.537 +  in List.app (fn r => r := thy'') rs; thy'' end;
   1.538 +
   1.539 +
   1.540 +(* theory data *)
   1.541  
   1.542 -fun end_theory thy =
   1.543 -  thy
   1.544 -(*|> purge_thy  FIXME *)
   1.545 -  |> name_thy (theory_name thy);
   1.546 +fun theory_data thy =
   1.547 +  map invoke_name (Inttab.keys (data_of thy))
   1.548 +  |> map (rpair ()) |> Symtab.make_multi |> Symtab.dest
   1.549 +  |> map (apsnd length)
   1.550 +  |> map (fn (name, 1) => name | (name, n) => name ^ enclose "[" "]" (string_of_int n));
   1.551 +
   1.552 +structure TheoryData =
   1.553 +struct
   1.554 +
   1.555 +val declare = declare_theory_data;
   1.556 +
   1.557 +fun get k dest thy =
   1.558 +  (case Inttab.lookup (data_of thy, k) of
   1.559 +    SOME x => (dest x handle Match =>
   1.560 +      error ("Failed to access theory data " ^ quote (invoke_name k)))
   1.561 +  | NONE => error ("Uninitialized theory data " ^ quote (invoke_name k)));
   1.562 +
   1.563 +fun put k mk x = modify_thy (curry Inttab.update (k, mk x));
   1.564 +fun init k = put k I (invoke_empty k);
   1.565 +
   1.566 +end;
   1.567  
   1.568  
   1.569  
   1.570 -
   1.571 -(*** ML theory context ***)
   1.572 +(** ML theory context **)
   1.573  
   1.574  local
   1.575    val current_theory = ref (NONE: theory option);
   1.576 @@ -489,6 +494,7 @@
   1.577    val init: theory -> theory
   1.578    val print: theory -> unit
   1.579    val get: theory -> T
   1.580 +  val get_sg: theory -> T    (*obsolete*)
   1.581    val put: T -> theory -> theory
   1.582    val map: (T -> T) -> theory -> theory
   1.583  end;
   1.584 @@ -505,13 +511,12 @@
   1.585    (Data Data.empty)
   1.586    (fn Data x => Data (Data.copy x))
   1.587    (fn Data x => Data (Data.extend x))
   1.588 -  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
   1.589 -  (fn thy => fn Data x => Data.print thy x);
   1.590 +  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
   1.591  
   1.592  val init = TheoryData.init kind;
   1.593 -val print = TheoryData.print kind;
   1.594  val get = TheoryData.get kind (fn Data x => x);
   1.595 -val get_sg = get;    (*obsolete*)
   1.596 +val get_sg = get;
   1.597 +fun print thy = Data.print thy (get thy);
   1.598  val put = TheoryData.put kind Data;
   1.599  fun map f thy = put (f (get thy)) thy;
   1.600