wenzelm@6185
|
1 |
(* Title: Pure/context.ML
|
wenzelm@6185
|
2 |
Author: Markus Wenzel, TU Muenchen
|
wenzelm@6185
|
3 |
|
wenzelm@16436
|
4 |
Generic theory contexts with unique identity, arbitrarily typed data,
|
wenzelm@24141
|
5 |
monotonic development graph and history support. Generic proof
|
wenzelm@24141
|
6 |
contexts with arbitrarily typed data.
|
wenzelm@6185
|
7 |
*)
|
wenzelm@6185
|
8 |
|
wenzelm@6185
|
9 |
signature BASIC_CONTEXT =
|
wenzelm@6185
|
10 |
sig
|
wenzelm@16436
|
11 |
type theory
|
wenzelm@16436
|
12 |
type theory_ref
|
wenzelm@16436
|
13 |
exception THEORY of string * theory list
|
wenzelm@6185
|
14 |
end;
|
wenzelm@6185
|
15 |
|
wenzelm@6185
|
16 |
signature CONTEXT =
|
wenzelm@6185
|
17 |
sig
|
wenzelm@6185
|
18 |
include BASIC_CONTEXT
|
wenzelm@16436
|
19 |
(*theory context*)
|
wenzelm@16436
|
20 |
val parents_of: theory -> theory list
|
wenzelm@16436
|
21 |
val ancestors_of: theory -> theory list
|
wenzelm@29069
|
22 |
val theory_name: theory -> string
|
wenzelm@16436
|
23 |
val is_stale: theory -> bool
|
wenzelm@26623
|
24 |
val is_draft: theory -> bool
|
wenzelm@28317
|
25 |
val reject_draft: theory -> theory
|
wenzelm@29093
|
26 |
val PureN: string
|
wenzelm@29093
|
27 |
val display_names: theory -> string list
|
wenzelm@16436
|
28 |
val pretty_thy: theory -> Pretty.T
|
wenzelm@16436
|
29 |
val string_of_thy: theory -> string
|
wenzelm@16436
|
30 |
val pprint_thy: theory -> pprint_args -> unit
|
wenzelm@27341
|
31 |
val pprint_thy_ref: theory_ref -> pprint_args -> unit
|
wenzelm@16436
|
32 |
val pretty_abbrev_thy: theory -> Pretty.T
|
wenzelm@16436
|
33 |
val str_of_thy: theory -> string
|
wenzelm@24141
|
34 |
val deref: theory_ref -> theory
|
wenzelm@24141
|
35 |
val check_thy: theory -> theory_ref
|
wenzelm@16436
|
36 |
val eq_thy: theory * theory -> bool
|
wenzelm@16436
|
37 |
val subthy: theory * theory -> bool
|
wenzelm@16594
|
38 |
val joinable: theory * theory -> bool
|
wenzelm@23355
|
39 |
val merge: theory * theory -> theory
|
wenzelm@23355
|
40 |
val merge_refs: theory_ref * theory_ref -> theory_ref
|
wenzelm@16436
|
41 |
val copy_thy: theory -> theory
|
wenzelm@16436
|
42 |
val checkpoint_thy: theory -> theory
|
wenzelm@16489
|
43 |
val finish_thy: theory -> theory
|
wenzelm@16489
|
44 |
val begin_thy: (theory -> Pretty.pp) -> string -> theory list -> theory
|
wenzelm@16533
|
45 |
(*proof context*)
|
wenzelm@16533
|
46 |
type proof
|
wenzelm@16533
|
47 |
val theory_of_proof: proof -> theory
|
wenzelm@17060
|
48 |
val transfer_proof: theory -> proof -> proof
|
wenzelm@16533
|
49 |
val init_proof: theory -> proof
|
wenzelm@16533
|
50 |
(*generic context*)
|
wenzelm@18632
|
51 |
datatype generic = Theory of theory | Proof of proof
|
wenzelm@18632
|
52 |
val cases: (theory -> 'a) -> (proof -> 'a) -> generic -> 'a
|
wenzelm@19678
|
53 |
val mapping: (theory -> theory) -> (proof -> proof) -> generic -> generic
|
wenzelm@21660
|
54 |
val mapping_result: (theory -> 'a * theory) -> (proof -> 'a * proof) -> generic -> 'a * generic
|
wenzelm@18632
|
55 |
val the_theory: generic -> theory
|
wenzelm@18632
|
56 |
val the_proof: generic -> proof
|
wenzelm@18731
|
57 |
val map_theory: (theory -> theory) -> generic -> generic
|
wenzelm@18731
|
58 |
val map_proof: (proof -> proof) -> generic -> generic
|
wenzelm@26486
|
59 |
val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic
|
wenzelm@26486
|
60 |
val map_proof_result: (proof -> 'a * proof) -> generic -> 'a * generic
|
wenzelm@18731
|
61 |
val theory_map: (generic -> generic) -> theory -> theory
|
wenzelm@18731
|
62 |
val proof_map: (generic -> generic) -> proof -> proof
|
wenzelm@18665
|
63 |
val theory_of: generic -> theory (*total*)
|
wenzelm@18665
|
64 |
val proof_of: generic -> proof (*total*)
|
wenzelm@26413
|
65 |
(*thread data*)
|
wenzelm@26413
|
66 |
val thread_data: unit -> generic option
|
wenzelm@26413
|
67 |
val the_thread_data: unit -> generic
|
wenzelm@26413
|
68 |
val set_thread_data: generic option -> unit
|
wenzelm@26413
|
69 |
val setmp_thread_data: generic option -> ('a -> 'b) -> 'a -> 'b
|
wenzelm@26463
|
70 |
val >> : (generic -> generic) -> unit
|
wenzelm@26463
|
71 |
val >>> : (generic -> 'a * generic) -> 'a
|
wenzelm@6185
|
72 |
end;
|
wenzelm@6185
|
73 |
|
wenzelm@16436
|
74 |
signature PRIVATE_CONTEXT =
|
wenzelm@16436
|
75 |
sig
|
wenzelm@16436
|
76 |
include CONTEXT
|
wenzelm@16436
|
77 |
structure TheoryData:
|
wenzelm@16436
|
78 |
sig
|
wenzelm@22847
|
79 |
val declare: Object.T -> (Object.T -> Object.T) -> (Object.T -> Object.T) ->
|
wenzelm@16489
|
80 |
(Pretty.pp -> Object.T * Object.T -> Object.T) -> serial
|
wenzelm@16436
|
81 |
val get: serial -> (Object.T -> 'a) -> theory -> 'a
|
wenzelm@16436
|
82 |
val put: serial -> ('a -> Object.T) -> 'a -> theory -> theory
|
wenzelm@16489
|
83 |
end
|
wenzelm@16533
|
84 |
structure ProofData:
|
wenzelm@16533
|
85 |
sig
|
wenzelm@22847
|
86 |
val declare: (theory -> Object.T) -> serial
|
wenzelm@16533
|
87 |
val get: serial -> (Object.T -> 'a) -> proof -> 'a
|
wenzelm@16533
|
88 |
val put: serial -> ('a -> Object.T) -> 'a -> proof -> proof
|
wenzelm@16533
|
89 |
end
|
wenzelm@16436
|
90 |
end;
|
wenzelm@16436
|
91 |
|
wenzelm@16436
|
92 |
structure Context: PRIVATE_CONTEXT =
|
wenzelm@6185
|
93 |
struct
|
wenzelm@6185
|
94 |
|
wenzelm@16436
|
95 |
(*** theory context ***)
|
wenzelm@6185
|
96 |
|
wenzelm@16489
|
97 |
(** theory data **)
|
wenzelm@16489
|
98 |
|
wenzelm@16489
|
99 |
(* data kinds and access methods *)
|
wenzelm@16489
|
100 |
|
wenzelm@19028
|
101 |
(*private copy avoids potential conflict of table exceptions*)
|
wenzelm@19028
|
102 |
structure Datatab = TableFun(type key = int val ord = int_ord);
|
wenzelm@19028
|
103 |
|
wenzelm@16489
|
104 |
local
|
wenzelm@16489
|
105 |
|
wenzelm@16489
|
106 |
type kind =
|
wenzelm@22847
|
107 |
{empty: Object.T,
|
wenzelm@16489
|
108 |
copy: Object.T -> Object.T,
|
wenzelm@16489
|
109 |
extend: Object.T -> Object.T,
|
wenzelm@16489
|
110 |
merge: Pretty.pp -> Object.T * Object.T -> Object.T};
|
wenzelm@16489
|
111 |
|
wenzelm@19028
|
112 |
val kinds = ref (Datatab.empty: kind Datatab.table);
|
wenzelm@16489
|
113 |
|
wenzelm@22847
|
114 |
fun invoke f k =
|
wenzelm@19028
|
115 |
(case Datatab.lookup (! kinds) k of
|
wenzelm@22847
|
116 |
SOME kind => f kind
|
wenzelm@22847
|
117 |
| NONE => sys_error "Invalid theory data identifier");
|
wenzelm@16489
|
118 |
|
wenzelm@16489
|
119 |
in
|
wenzelm@16489
|
120 |
|
wenzelm@22847
|
121 |
fun invoke_empty k = invoke (K o #empty) k ();
|
wenzelm@22847
|
122 |
val invoke_copy = invoke #copy;
|
wenzelm@22847
|
123 |
val invoke_extend = invoke #extend;
|
wenzelm@22847
|
124 |
fun invoke_merge pp = invoke (fn kind => #merge kind pp);
|
wenzelm@16489
|
125 |
|
wenzelm@22847
|
126 |
fun declare_theory_data empty copy extend merge =
|
wenzelm@16489
|
127 |
let
|
wenzelm@16489
|
128 |
val k = serial ();
|
wenzelm@22847
|
129 |
val kind = {empty = empty, copy = copy, extend = extend, merge = merge};
|
wenzelm@23944
|
130 |
val _ = CRITICAL (fn () => change kinds (Datatab.update (k, kind)));
|
wenzelm@16489
|
131 |
in k end;
|
wenzelm@16489
|
132 |
|
wenzelm@19028
|
133 |
val copy_data = Datatab.map' invoke_copy;
|
wenzelm@19028
|
134 |
val extend_data = Datatab.map' invoke_extend;
|
wenzelm@19028
|
135 |
fun merge_data pp = Datatab.join (invoke_merge pp) o pairself extend_data;
|
wenzelm@16489
|
136 |
|
wenzelm@16489
|
137 |
end;
|
wenzelm@16489
|
138 |
|
wenzelm@16489
|
139 |
|
wenzelm@16489
|
140 |
|
wenzelm@16489
|
141 |
(** datatype theory **)
|
wenzelm@16489
|
142 |
|
wenzelm@16436
|
143 |
datatype theory =
|
wenzelm@16436
|
144 |
Theory of
|
wenzelm@16533
|
145 |
(*identity*)
|
wenzelm@29093
|
146 |
{self: theory ref option, (*dynamic self reference -- follows theory changes*)
|
wenzelm@29095
|
147 |
draft: bool, (*draft mode -- linear destructive changes*)
|
wenzelm@29093
|
148 |
id: serial, (*identifier*)
|
wenzelm@29093
|
149 |
ids: unit Inttab.table} * (*cumulative identifiers of non-drafts -- symbolic body content*)
|
wenzelm@29095
|
150 |
(*data*)
|
wenzelm@29095
|
151 |
Object.T Datatab.table * (*body content*)
|
wenzelm@29095
|
152 |
(*ancestry*)
|
wenzelm@29093
|
153 |
{parents: theory list, (*immediate predecessors*)
|
wenzelm@29093
|
154 |
ancestors: theory list} * (*all predecessors -- canonical reverse order*)
|
wenzelm@29095
|
155 |
(*history*)
|
wenzelm@29093
|
156 |
{name: string, (*official theory name*)
|
wenzelm@29093
|
157 |
stage: int}; (*checkpoint counter*)
|
wenzelm@16436
|
158 |
|
wenzelm@16436
|
159 |
exception THEORY of string * theory list;
|
wenzelm@16436
|
160 |
|
wenzelm@16436
|
161 |
fun rep_theory (Theory args) = args;
|
wenzelm@16436
|
162 |
|
wenzelm@16436
|
163 |
val identity_of = #1 o rep_theory;
|
wenzelm@16436
|
164 |
val data_of = #2 o rep_theory;
|
wenzelm@16489
|
165 |
val ancestry_of = #3 o rep_theory;
|
wenzelm@16489
|
166 |
val history_of = #4 o rep_theory;
|
wenzelm@16436
|
167 |
|
wenzelm@29093
|
168 |
fun make_identity self draft id ids = {self = self, draft = draft, id = id, ids = ids};
|
wenzelm@16489
|
169 |
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors};
|
wenzelm@29093
|
170 |
fun make_history name stage = {name = name, stage = stage};
|
wenzelm@16436
|
171 |
|
wenzelm@16533
|
172 |
val the_self = the o #self o identity_of;
|
wenzelm@16436
|
173 |
val parents_of = #parents o ancestry_of;
|
wenzelm@16436
|
174 |
val ancestors_of = #ancestors o ancestry_of;
|
wenzelm@16489
|
175 |
val theory_name = #name o history_of;
|
wenzelm@16436
|
176 |
|
wenzelm@16436
|
177 |
|
wenzelm@16436
|
178 |
(* staleness *)
|
wenzelm@16436
|
179 |
|
wenzelm@29093
|
180 |
fun eq_id (i: int, j) = i = j;
|
wenzelm@16436
|
181 |
|
wenzelm@16436
|
182 |
fun is_stale
|
wenzelm@16436
|
183 |
(Theory ({self = SOME (ref (Theory ({id = id', ...}, _, _, _))), id, ...}, _, _, _)) =
|
wenzelm@16436
|
184 |
not (eq_id (id, id'))
|
wenzelm@16436
|
185 |
| is_stale (Theory ({self = NONE, ...}, _, _, _)) = true;
|
wenzelm@16436
|
186 |
|
wenzelm@16436
|
187 |
fun vitalize (thy as Theory ({self = SOME r, ...}, _, _, _)) = (r := thy; thy)
|
wenzelm@29093
|
188 |
| vitalize (thy as Theory ({self = NONE, draft, id, ids}, data, ancestry, history)) =
|
wenzelm@16436
|
189 |
let
|
wenzelm@16436
|
190 |
val r = ref thy;
|
wenzelm@29093
|
191 |
val thy' = Theory (make_identity (SOME r) draft id ids, data, ancestry, history);
|
wenzelm@16436
|
192 |
in r := thy'; thy' end;
|
wenzelm@16436
|
193 |
|
wenzelm@16436
|
194 |
|
wenzelm@29093
|
195 |
(* draft mode *)
|
wenzelm@16436
|
196 |
|
wenzelm@29093
|
197 |
val is_draft = #draft o identity_of;
|
wenzelm@16436
|
198 |
|
wenzelm@28317
|
199 |
fun reject_draft thy =
|
wenzelm@28317
|
200 |
if is_draft thy then raise THEORY ("Illegal draft theory -- stable checkpoint required", [thy])
|
wenzelm@28317
|
201 |
else thy;
|
wenzelm@28317
|
202 |
|
wenzelm@16436
|
203 |
|
wenzelm@29093
|
204 |
(* names *)
|
wenzelm@29069
|
205 |
|
wenzelm@29093
|
206 |
val PureN = "Pure";
|
wenzelm@29093
|
207 |
val draftN = "#";
|
wenzelm@29095
|
208 |
val finished = ~1;
|
wenzelm@16436
|
209 |
|
wenzelm@29093
|
210 |
fun display_names thy =
|
wenzelm@29093
|
211 |
let
|
wenzelm@29093
|
212 |
val draft = if is_draft thy then [draftN] else [];
|
wenzelm@29095
|
213 |
val {stage, ...} = history_of thy;
|
wenzelm@29093
|
214 |
val name =
|
wenzelm@29095
|
215 |
if stage = finished then theory_name thy
|
wenzelm@29095
|
216 |
else theory_name thy ^ ":" ^ string_of_int stage;
|
wenzelm@29093
|
217 |
val ancestor_names = map theory_name (ancestors_of thy);
|
wenzelm@29093
|
218 |
val stale = if is_stale thy then ["!"] else [];
|
wenzelm@29093
|
219 |
in rev (stale @ draft @ [name] @ ancestor_names) end;
|
wenzelm@16436
|
220 |
|
wenzelm@29093
|
221 |
val pretty_thy = Pretty.str_list "{" "}" o display_names;
|
wenzelm@16436
|
222 |
val string_of_thy = Pretty.string_of o pretty_thy;
|
wenzelm@16436
|
223 |
val pprint_thy = Pretty.pprint o pretty_thy;
|
wenzelm@16436
|
224 |
|
wenzelm@16436
|
225 |
fun pretty_abbrev_thy thy =
|
wenzelm@16436
|
226 |
let
|
wenzelm@29093
|
227 |
val names = display_names thy;
|
wenzelm@16436
|
228 |
val n = length names;
|
wenzelm@16436
|
229 |
val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names;
|
wenzelm@16436
|
230 |
in Pretty.str_list "{" "}" abbrev end;
|
wenzelm@16436
|
231 |
|
wenzelm@16436
|
232 |
val str_of_thy = Pretty.str_of o pretty_abbrev_thy;
|
wenzelm@16436
|
233 |
|
wenzelm@16436
|
234 |
|
wenzelm@24141
|
235 |
(* theory references *)
|
wenzelm@24141
|
236 |
|
wenzelm@24141
|
237 |
(*theory_ref provides a safe way to store dynamic references to a
|
wenzelm@24141
|
238 |
theory in external data structures -- a plain theory value would
|
wenzelm@24141
|
239 |
become stale as the self reference moves on*)
|
wenzelm@24141
|
240 |
|
wenzelm@24141
|
241 |
datatype theory_ref = TheoryRef of theory ref;
|
wenzelm@24141
|
242 |
|
wenzelm@24141
|
243 |
fun deref (TheoryRef (ref thy)) = thy;
|
wenzelm@24141
|
244 |
|
wenzelm@24141
|
245 |
fun check_thy thy = (*thread-safe version*)
|
wenzelm@24141
|
246 |
let val thy_ref = TheoryRef (the_self thy) in
|
wenzelm@24141
|
247 |
if is_stale thy then error ("Stale theory encountered:\n" ^ string_of_thy thy)
|
wenzelm@24141
|
248 |
else thy_ref
|
wenzelm@24141
|
249 |
end;
|
wenzelm@24141
|
250 |
|
wenzelm@27341
|
251 |
val pprint_thy_ref = Pretty.pprint o pretty_thy o deref;
|
wenzelm@27341
|
252 |
|
wenzelm@24141
|
253 |
|
wenzelm@29093
|
254 |
(* build ids *)
|
wenzelm@16436
|
255 |
|
wenzelm@29093
|
256 |
fun insert_id draft id ids =
|
wenzelm@29093
|
257 |
if draft then ids
|
wenzelm@29093
|
258 |
else Inttab.update (id, ()) ids;
|
wenzelm@16436
|
259 |
|
wenzelm@29093
|
260 |
fun merge_ids
|
wenzelm@29093
|
261 |
(Theory ({draft = draft1, id = id1, ids = ids1, ...}, _, _, _))
|
wenzelm@29093
|
262 |
(Theory ({draft = draft2, id = id2, ids = ids2, ...}, _, _, _)) =
|
wenzelm@29093
|
263 |
Inttab.merge (K true) (ids1, ids2)
|
wenzelm@29093
|
264 |
|> insert_id draft1 id1
|
wenzelm@29093
|
265 |
|> insert_id draft2 id2;
|
wenzelm@16436
|
266 |
|
wenzelm@16489
|
267 |
|
wenzelm@16533
|
268 |
(* equality and inclusion *)
|
wenzelm@16533
|
269 |
|
wenzelm@24141
|
270 |
val eq_thy = eq_id o pairself (#id o identity_of);
|
wenzelm@16533
|
271 |
|
wenzelm@29069
|
272 |
fun proper_subthy (Theory ({id, ...}, _, _, _), Theory ({ids, ...}, _, _, _)) =
|
wenzelm@29093
|
273 |
Inttab.defined ids id;
|
wenzelm@16533
|
274 |
|
wenzelm@16533
|
275 |
fun subthy thys = eq_thy thys orelse proper_subthy thys;
|
wenzelm@16533
|
276 |
|
wenzelm@16594
|
277 |
fun joinable (thy1, thy2) = subthy (thy1, thy2) orelse subthy (thy2, thy1);
|
wenzelm@16594
|
278 |
|
wenzelm@16533
|
279 |
|
wenzelm@29093
|
280 |
(* consistent ancestors *)
|
wenzelm@29093
|
281 |
|
wenzelm@29093
|
282 |
fun extend_ancestors thy thys =
|
wenzelm@29093
|
283 |
if member eq_thy thys thy then raise THEORY ("Duplicate theory node", thy :: thys)
|
wenzelm@29093
|
284 |
else thy :: thys;
|
wenzelm@29093
|
285 |
|
wenzelm@29093
|
286 |
fun extend_ancestors_of thy = extend_ancestors thy (ancestors_of thy);
|
wenzelm@29093
|
287 |
|
wenzelm@29093
|
288 |
val merge_ancestors = merge (fn (thy1, thy2) =>
|
wenzelm@29093
|
289 |
eq_thy (thy1, thy2) orelse
|
wenzelm@29093
|
290 |
theory_name thy1 = theory_name thy2 andalso
|
wenzelm@29093
|
291 |
raise THEORY ("Inconsistent theory versions", [thy1, thy2]));
|
wenzelm@29093
|
292 |
|
wenzelm@29093
|
293 |
|
wenzelm@23355
|
294 |
(* trivial merge *)
|
wenzelm@16436
|
295 |
|
wenzelm@16436
|
296 |
fun merge (thy1, thy2) =
|
wenzelm@16719
|
297 |
if eq_thy (thy1, thy2) then thy1
|
wenzelm@16719
|
298 |
else if proper_subthy (thy2, thy1) then thy1
|
wenzelm@16719
|
299 |
else if proper_subthy (thy1, thy2) then thy2
|
wenzelm@29093
|
300 |
else error (cat_lines ["Attempt to perform non-trivial merge of theories:",
|
wenzelm@29093
|
301 |
str_of_thy thy1, str_of_thy thy2]);
|
wenzelm@16436
|
302 |
|
wenzelm@16719
|
303 |
fun merge_refs (ref1, ref2) =
|
wenzelm@16719
|
304 |
if ref1 = ref2 then ref1
|
wenzelm@24141
|
305 |
else check_thy (merge (deref ref1, deref ref2));
|
wenzelm@16436
|
306 |
|
wenzelm@16436
|
307 |
|
wenzelm@16436
|
308 |
|
wenzelm@16489
|
309 |
(** build theories **)
|
wenzelm@16489
|
310 |
|
wenzelm@16489
|
311 |
(* primitives *)
|
wenzelm@16489
|
312 |
|
wenzelm@29093
|
313 |
fun create_thy self draft ids data ancestry history =
|
wenzelm@29093
|
314 |
let val identity = make_identity self draft (serial ()) ids;
|
wenzelm@29093
|
315 |
in vitalize (Theory (identity, data, ancestry, history)) end;
|
wenzelm@29093
|
316 |
|
wenzelm@29093
|
317 |
fun change_thy draft' f thy =
|
wenzelm@16489
|
318 |
let
|
wenzelm@29093
|
319 |
val Theory ({self, draft, id, ids}, data, ancestry, history) = thy;
|
wenzelm@16489
|
320 |
val (self', data', ancestry') =
|
wenzelm@29093
|
321 |
if draft then (self, data, ancestry) (*destructive change!*)
|
wenzelm@29093
|
322 |
else if #stage history > 0
|
wenzelm@22847
|
323 |
then (NONE, copy_data data, ancestry)
|
wenzelm@29093
|
324 |
else (NONE, extend_data data, make_ancestry [thy] (extend_ancestors_of thy));
|
wenzelm@29093
|
325 |
val ids' = insert_id draft id ids;
|
wenzelm@16489
|
326 |
val data'' = f data';
|
wenzelm@24369
|
327 |
val thy' = NAMED_CRITICAL "theory" (fn () =>
|
wenzelm@29093
|
328 |
(check_thy thy; create_thy self' draft' ids' data'' ancestry' history));
|
wenzelm@24369
|
329 |
in thy' end;
|
wenzelm@16489
|
330 |
|
wenzelm@29093
|
331 |
val name_thy = change_thy false I;
|
wenzelm@29093
|
332 |
val extend_thy = change_thy true I;
|
wenzelm@29093
|
333 |
val modify_thy = change_thy true;
|
wenzelm@16489
|
334 |
|
wenzelm@24369
|
335 |
fun copy_thy thy =
|
wenzelm@24141
|
336 |
let
|
wenzelm@29093
|
337 |
val Theory ({draft, id, ids, ...}, data, ancestry, history) = thy;
|
wenzelm@29093
|
338 |
val ids' = insert_id draft id ids;
|
wenzelm@24369
|
339 |
val data' = copy_data data;
|
wenzelm@24369
|
340 |
val thy' = NAMED_CRITICAL "theory" (fn () =>
|
wenzelm@29093
|
341 |
(check_thy thy; create_thy NONE true ids' data' ancestry history));
|
wenzelm@24369
|
342 |
in thy' end;
|
wenzelm@16489
|
343 |
|
wenzelm@29093
|
344 |
val pre_pure_thy = create_thy NONE true Inttab.empty
|
wenzelm@29069
|
345 |
Datatab.empty (make_ancestry [] []) (make_history PureN 0);
|
wenzelm@16489
|
346 |
|
wenzelm@16489
|
347 |
|
wenzelm@16489
|
348 |
(* named theory nodes *)
|
wenzelm@16489
|
349 |
|
wenzelm@16489
|
350 |
fun merge_thys pp (thy1, thy2) =
|
wenzelm@26957
|
351 |
let
|
wenzelm@29093
|
352 |
val ids = merge_ids thy1 thy2;
|
wenzelm@26957
|
353 |
val data = merge_data (pp thy1) (data_of thy1, data_of thy2);
|
wenzelm@26957
|
354 |
val ancestry = make_ancestry [] [];
|
wenzelm@29069
|
355 |
val history = make_history "" 0;
|
wenzelm@26957
|
356 |
val thy' = NAMED_CRITICAL "theory" (fn () =>
|
wenzelm@29093
|
357 |
(check_thy thy1; check_thy thy2; create_thy NONE true ids data ancestry history));
|
wenzelm@26957
|
358 |
in thy' end;
|
wenzelm@16436
|
359 |
|
wenzelm@16533
|
360 |
fun maximal_thys thys =
|
wenzelm@28617
|
361 |
thys |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys);
|
wenzelm@16533
|
362 |
|
wenzelm@16489
|
363 |
fun begin_thy pp name imports =
|
wenzelm@29093
|
364 |
if name = "" orelse name = draftN then error ("Bad theory name: " ^ quote name)
|
wenzelm@24369
|
365 |
else
|
wenzelm@16436
|
366 |
let
|
wenzelm@24141
|
367 |
val parents = maximal_thys (distinct eq_thy imports);
|
wenzelm@29093
|
368 |
val ancestors =
|
wenzelm@29093
|
369 |
Library.foldl merge_ancestors ([], map ancestors_of parents)
|
wenzelm@29093
|
370 |
|> fold extend_ancestors parents;
|
wenzelm@29093
|
371 |
|
wenzelm@29093
|
372 |
val Theory ({ids, ...}, data, _, _) =
|
wenzelm@16436
|
373 |
(case parents of
|
wenzelm@16436
|
374 |
[] => error "No parent theories"
|
wenzelm@16533
|
375 |
| [thy] => extend_thy thy
|
wenzelm@16533
|
376 |
| thy :: thys => Library.foldl (merge_thys pp) (thy, thys));
|
wenzelm@29093
|
377 |
|
wenzelm@16489
|
378 |
val ancestry = make_ancestry parents ancestors;
|
wenzelm@29069
|
379 |
val history = make_history name 0;
|
wenzelm@24369
|
380 |
val thy' = NAMED_CRITICAL "theory" (fn () =>
|
wenzelm@29093
|
381 |
(map check_thy imports; create_thy NONE true ids data ancestry history));
|
wenzelm@24369
|
382 |
in thy' end;
|
wenzelm@16436
|
383 |
|
wenzelm@16436
|
384 |
|
wenzelm@29093
|
385 |
(* history stages *)
|
wenzelm@29093
|
386 |
|
wenzelm@29093
|
387 |
fun history_stage f thy =
|
wenzelm@29093
|
388 |
let
|
wenzelm@29093
|
389 |
val {name, stage} = history_of thy;
|
wenzelm@29095
|
390 |
val _ = stage = finished andalso raise THEORY ("Theory already finished", [thy]);
|
wenzelm@29093
|
391 |
val history' = make_history name (f stage);
|
wenzelm@29093
|
392 |
val thy' as Theory (identity', data', ancestry', _) = name_thy thy;
|
wenzelm@29093
|
393 |
val thy'' = NAMED_CRITICAL "theory" (fn () =>
|
wenzelm@29093
|
394 |
(check_thy thy'; vitalize (Theory (identity', data', ancestry', history'))));
|
wenzelm@29093
|
395 |
in thy'' end;
|
wenzelm@16436
|
396 |
|
wenzelm@16489
|
397 |
fun checkpoint_thy thy =
|
wenzelm@29093
|
398 |
if is_draft thy then history_stage (fn stage => stage + 1) thy
|
wenzelm@29093
|
399 |
else thy;
|
wenzelm@16436
|
400 |
|
wenzelm@29095
|
401 |
val finish_thy = history_stage (fn _ => finished);
|
wenzelm@16436
|
402 |
|
wenzelm@16489
|
403 |
|
wenzelm@16489
|
404 |
(* theory data *)
|
wenzelm@16489
|
405 |
|
wenzelm@16489
|
406 |
structure TheoryData =
|
wenzelm@16489
|
407 |
struct
|
wenzelm@16489
|
408 |
|
wenzelm@16489
|
409 |
val declare = declare_theory_data;
|
wenzelm@16489
|
410 |
|
wenzelm@16489
|
411 |
fun get k dest thy =
|
wenzelm@22847
|
412 |
dest ((case Datatab.lookup (data_of thy) k of
|
wenzelm@22847
|
413 |
SOME x => x
|
wenzelm@22847
|
414 |
| NONE => invoke_copy k (invoke_empty k))); (*adhoc value*)
|
wenzelm@16489
|
415 |
|
wenzelm@22847
|
416 |
fun put k mk x = modify_thy (Datatab.update (k, mk x));
|
wenzelm@16489
|
417 |
|
wenzelm@16489
|
418 |
end;
|
wenzelm@16489
|
419 |
|
wenzelm@16489
|
420 |
|
wenzelm@16489
|
421 |
|
wenzelm@16533
|
422 |
(*** proof context ***)
|
wenzelm@16533
|
423 |
|
wenzelm@16533
|
424 |
(* datatype proof *)
|
wenzelm@16533
|
425 |
|
wenzelm@24184
|
426 |
datatype proof = Prf of Object.T Datatab.table * theory_ref;
|
wenzelm@16533
|
427 |
|
wenzelm@24184
|
428 |
fun theory_of_proof (Prf (_, thy_ref)) = deref thy_ref;
|
wenzelm@24184
|
429 |
fun data_of_proof (Prf (data, _)) = data;
|
wenzelm@24184
|
430 |
fun map_prf f (Prf (data, thy_ref)) = Prf (f data, thy_ref);
|
wenzelm@17060
|
431 |
|
wenzelm@16533
|
432 |
|
wenzelm@16533
|
433 |
(* proof data kinds *)
|
wenzelm@16533
|
434 |
|
wenzelm@16533
|
435 |
local
|
wenzelm@16533
|
436 |
|
wenzelm@22847
|
437 |
val kinds = ref (Datatab.empty: (theory -> Object.T) Datatab.table);
|
wenzelm@16533
|
438 |
|
wenzelm@22847
|
439 |
fun invoke_init k =
|
wenzelm@22847
|
440 |
(case Datatab.lookup (! kinds) k of
|
wenzelm@22847
|
441 |
SOME init => init
|
wenzelm@22847
|
442 |
| NONE => sys_error "Invalid proof data identifier");
|
wenzelm@16533
|
443 |
|
wenzelm@22847
|
444 |
fun init_data thy =
|
wenzelm@22847
|
445 |
Datatab.map' (fn k => fn _ => invoke_init k thy) (! kinds);
|
wenzelm@16533
|
446 |
|
wenzelm@22847
|
447 |
fun init_new_data data thy =
|
wenzelm@22847
|
448 |
Datatab.merge (K true) (data, init_data thy);
|
wenzelm@16533
|
449 |
|
wenzelm@16533
|
450 |
in
|
wenzelm@16533
|
451 |
|
wenzelm@24184
|
452 |
fun init_proof thy = Prf (init_data thy, check_thy thy);
|
wenzelm@16533
|
453 |
|
wenzelm@24184
|
454 |
fun transfer_proof thy' (prf as Prf (data, thy_ref)) =
|
wenzelm@24141
|
455 |
let
|
wenzelm@24141
|
456 |
val thy = deref thy_ref;
|
wenzelm@24141
|
457 |
val _ = subthy (thy, thy') orelse error "transfer proof context: not a super theory";
|
wenzelm@24141
|
458 |
val _ = check_thy thy;
|
wenzelm@24184
|
459 |
val data' = init_new_data data thy';
|
wenzelm@24141
|
460 |
val thy_ref' = check_thy thy';
|
wenzelm@24184
|
461 |
in Prf (data', thy_ref') end;
|
wenzelm@22847
|
462 |
|
wenzelm@16533
|
463 |
|
wenzelm@16533
|
464 |
structure ProofData =
|
wenzelm@16533
|
465 |
struct
|
wenzelm@16533
|
466 |
|
wenzelm@22847
|
467 |
fun declare init =
|
wenzelm@16533
|
468 |
let
|
wenzelm@16533
|
469 |
val k = serial ();
|
wenzelm@23944
|
470 |
val _ = CRITICAL (fn () => change kinds (Datatab.update (k, init)));
|
wenzelm@16533
|
471 |
in k end;
|
wenzelm@16533
|
472 |
|
wenzelm@16533
|
473 |
fun get k dest prf =
|
wenzelm@22847
|
474 |
dest (case Datatab.lookup (data_of_proof prf) k of
|
wenzelm@22847
|
475 |
SOME x => x
|
wenzelm@22847
|
476 |
| NONE => invoke_init k (theory_of_proof prf)); (*adhoc value*)
|
wenzelm@16533
|
477 |
|
wenzelm@19028
|
478 |
fun put k mk x = map_prf (Datatab.update (k, mk x));
|
wenzelm@16533
|
479 |
|
wenzelm@16533
|
480 |
end;
|
wenzelm@16533
|
481 |
|
wenzelm@16533
|
482 |
end;
|
wenzelm@16533
|
483 |
|
wenzelm@16533
|
484 |
|
wenzelm@18632
|
485 |
|
wenzelm@16533
|
486 |
(*** generic context ***)
|
wenzelm@16533
|
487 |
|
wenzelm@18632
|
488 |
datatype generic = Theory of theory | Proof of proof;
|
wenzelm@16533
|
489 |
|
wenzelm@18632
|
490 |
fun cases f _ (Theory thy) = f thy
|
wenzelm@18632
|
491 |
| cases _ g (Proof prf) = g prf;
|
wenzelm@16533
|
492 |
|
wenzelm@19678
|
493 |
fun mapping f g = cases (Theory o f) (Proof o g);
|
wenzelm@21660
|
494 |
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g);
|
wenzelm@19678
|
495 |
|
wenzelm@23595
|
496 |
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected");
|
wenzelm@23595
|
497 |
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I;
|
wenzelm@18632
|
498 |
|
wenzelm@18731
|
499 |
fun map_theory f = Theory o f o the_theory;
|
wenzelm@18731
|
500 |
fun map_proof f = Proof o f o the_proof;
|
wenzelm@18731
|
501 |
|
wenzelm@26486
|
502 |
fun map_theory_result f = apsnd Theory o f o the_theory;
|
wenzelm@26486
|
503 |
fun map_proof_result f = apsnd Proof o f o the_proof;
|
wenzelm@26486
|
504 |
|
wenzelm@18731
|
505 |
fun theory_map f = the_theory o f o Theory;
|
wenzelm@18731
|
506 |
fun proof_map f = the_proof o f o Proof;
|
wenzelm@18665
|
507 |
|
wenzelm@18632
|
508 |
val theory_of = cases I theory_of_proof;
|
wenzelm@18632
|
509 |
val proof_of = cases init_proof I;
|
wenzelm@16533
|
510 |
|
wenzelm@22085
|
511 |
|
wenzelm@22085
|
512 |
|
wenzelm@26413
|
513 |
(** thread data **)
|
wenzelm@26413
|
514 |
|
wenzelm@26413
|
515 |
local val tag = Universal.tag () : generic option Universal.tag in
|
wenzelm@26413
|
516 |
|
wenzelm@26413
|
517 |
fun thread_data () =
|
wenzelm@28122
|
518 |
(case Thread.getLocal tag of
|
wenzelm@26413
|
519 |
SOME (SOME context) => SOME context
|
wenzelm@26413
|
520 |
| _ => NONE);
|
wenzelm@26413
|
521 |
|
wenzelm@26413
|
522 |
fun the_thread_data () =
|
wenzelm@26413
|
523 |
(case thread_data () of
|
wenzelm@26413
|
524 |
SOME context => context
|
wenzelm@26413
|
525 |
| _ => error "Unknown context");
|
wenzelm@26413
|
526 |
|
wenzelm@28122
|
527 |
fun set_thread_data context = Thread.setLocal (tag, context);
|
wenzelm@26413
|
528 |
fun setmp_thread_data context = Library.setmp_thread_data tag (thread_data ()) context;
|
wenzelm@26413
|
529 |
|
wenzelm@26428
|
530 |
end;
|
wenzelm@26428
|
531 |
|
wenzelm@26428
|
532 |
fun >>> f =
|
wenzelm@26428
|
533 |
let
|
wenzelm@26463
|
534 |
val (res, context') = f (the_thread_data ());
|
wenzelm@26463
|
535 |
val _ = set_thread_data (SOME context');
|
wenzelm@26428
|
536 |
in res end;
|
wenzelm@26428
|
537 |
|
wenzelm@26421
|
538 |
nonfix >>;
|
wenzelm@26463
|
539 |
fun >> f = >>> (fn context => ((), f context));
|
wenzelm@26413
|
540 |
|
wenzelm@26428
|
541 |
val _ = set_thread_data (SOME (Theory pre_pure_thy));
|
wenzelm@26413
|
542 |
|
wenzelm@6185
|
543 |
end;
|
wenzelm@6185
|
544 |
|
wenzelm@6185
|
545 |
structure BasicContext: BASIC_CONTEXT = Context;
|
wenzelm@6185
|
546 |
open BasicContext;
|
wenzelm@16436
|
547 |
|
wenzelm@16436
|
548 |
|
wenzelm@16436
|
549 |
|
wenzelm@16533
|
550 |
(*** type-safe interfaces for data declarations ***)
|
wenzelm@16533
|
551 |
|
wenzelm@16533
|
552 |
(** theory data **)
|
wenzelm@16436
|
553 |
|
wenzelm@16436
|
554 |
signature THEORY_DATA_ARGS =
|
wenzelm@16436
|
555 |
sig
|
wenzelm@16436
|
556 |
type T
|
wenzelm@16436
|
557 |
val empty: T
|
wenzelm@16436
|
558 |
val copy: T -> T
|
wenzelm@16436
|
559 |
val extend: T -> T
|
wenzelm@16436
|
560 |
val merge: Pretty.pp -> T * T -> T
|
wenzelm@16436
|
561 |
end;
|
wenzelm@16436
|
562 |
|
wenzelm@16436
|
563 |
signature THEORY_DATA =
|
wenzelm@16436
|
564 |
sig
|
wenzelm@16436
|
565 |
type T
|
wenzelm@16436
|
566 |
val get: theory -> T
|
wenzelm@16436
|
567 |
val put: T -> theory -> theory
|
wenzelm@16436
|
568 |
val map: (T -> T) -> theory -> theory
|
wenzelm@22847
|
569 |
val init: theory -> theory
|
wenzelm@16436
|
570 |
end;
|
wenzelm@16436
|
571 |
|
wenzelm@16436
|
572 |
functor TheoryDataFun(Data: THEORY_DATA_ARGS): THEORY_DATA =
|
wenzelm@16436
|
573 |
struct
|
wenzelm@16436
|
574 |
|
wenzelm@16436
|
575 |
structure TheoryData = Context.TheoryData;
|
wenzelm@16436
|
576 |
|
wenzelm@16436
|
577 |
type T = Data.T;
|
wenzelm@16436
|
578 |
exception Data of T;
|
wenzelm@16436
|
579 |
|
wenzelm@22847
|
580 |
val kind = TheoryData.declare
|
wenzelm@16436
|
581 |
(Data Data.empty)
|
wenzelm@16436
|
582 |
(fn Data x => Data (Data.copy x))
|
wenzelm@16436
|
583 |
(fn Data x => Data (Data.extend x))
|
wenzelm@16489
|
584 |
(fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)));
|
wenzelm@16436
|
585 |
|
wenzelm@16436
|
586 |
val get = TheoryData.get kind (fn Data x => x);
|
wenzelm@16436
|
587 |
val put = TheoryData.put kind Data;
|
wenzelm@16436
|
588 |
fun map f thy = put (f (get thy)) thy;
|
wenzelm@16436
|
589 |
|
wenzelm@22847
|
590 |
fun init thy = map I thy;
|
wenzelm@22847
|
591 |
|
wenzelm@16436
|
592 |
end;
|
wenzelm@16436
|
593 |
|
wenzelm@16533
|
594 |
|
wenzelm@16533
|
595 |
|
wenzelm@16533
|
596 |
(** proof data **)
|
wenzelm@16533
|
597 |
|
wenzelm@16533
|
598 |
signature PROOF_DATA_ARGS =
|
wenzelm@16533
|
599 |
sig
|
wenzelm@16533
|
600 |
type T
|
wenzelm@16533
|
601 |
val init: theory -> T
|
wenzelm@16533
|
602 |
end;
|
wenzelm@16533
|
603 |
|
wenzelm@16533
|
604 |
signature PROOF_DATA =
|
wenzelm@16533
|
605 |
sig
|
wenzelm@16533
|
606 |
type T
|
wenzelm@16533
|
607 |
val get: Context.proof -> T
|
wenzelm@16533
|
608 |
val put: T -> Context.proof -> Context.proof
|
wenzelm@16533
|
609 |
val map: (T -> T) -> Context.proof -> Context.proof
|
wenzelm@16533
|
610 |
end;
|
wenzelm@16533
|
611 |
|
wenzelm@16533
|
612 |
functor ProofDataFun(Data: PROOF_DATA_ARGS): PROOF_DATA =
|
wenzelm@16533
|
613 |
struct
|
wenzelm@16533
|
614 |
|
wenzelm@16533
|
615 |
structure ProofData = Context.ProofData;
|
wenzelm@16533
|
616 |
|
wenzelm@16533
|
617 |
type T = Data.T;
|
wenzelm@16533
|
618 |
exception Data of T;
|
wenzelm@16533
|
619 |
|
wenzelm@22847
|
620 |
val kind = ProofData.declare (Data o Data.init);
|
wenzelm@16533
|
621 |
|
wenzelm@16533
|
622 |
val get = ProofData.get kind (fn Data x => x);
|
wenzelm@16533
|
623 |
val put = ProofData.put kind Data;
|
wenzelm@16533
|
624 |
fun map f prf = put (f (get prf)) prf;
|
wenzelm@16533
|
625 |
|
wenzelm@16533
|
626 |
end;
|
wenzelm@16533
|
627 |
|
wenzelm@18632
|
628 |
|
wenzelm@18632
|
629 |
|
wenzelm@18632
|
630 |
(** generic data **)
|
wenzelm@18632
|
631 |
|
wenzelm@18632
|
632 |
signature GENERIC_DATA_ARGS =
|
wenzelm@18632
|
633 |
sig
|
wenzelm@18632
|
634 |
type T
|
wenzelm@18632
|
635 |
val empty: T
|
wenzelm@18632
|
636 |
val extend: T -> T
|
wenzelm@18632
|
637 |
val merge: Pretty.pp -> T * T -> T
|
wenzelm@18632
|
638 |
end;
|
wenzelm@18632
|
639 |
|
wenzelm@18632
|
640 |
signature GENERIC_DATA =
|
wenzelm@18632
|
641 |
sig
|
wenzelm@18632
|
642 |
type T
|
wenzelm@18632
|
643 |
val get: Context.generic -> T
|
wenzelm@18632
|
644 |
val put: T -> Context.generic -> Context.generic
|
wenzelm@18632
|
645 |
val map: (T -> T) -> Context.generic -> Context.generic
|
wenzelm@18632
|
646 |
end;
|
wenzelm@18632
|
647 |
|
wenzelm@18632
|
648 |
functor GenericDataFun(Data: GENERIC_DATA_ARGS): GENERIC_DATA =
|
wenzelm@18632
|
649 |
struct
|
wenzelm@18632
|
650 |
|
wenzelm@22847
|
651 |
structure ThyData = TheoryDataFun(open Data val copy = I);
|
wenzelm@22847
|
652 |
structure PrfData = ProofDataFun(type T = Data.T val init = ThyData.get);
|
wenzelm@18632
|
653 |
|
wenzelm@18632
|
654 |
type T = Data.T;
|
wenzelm@18632
|
655 |
|
wenzelm@18632
|
656 |
fun get (Context.Theory thy) = ThyData.get thy
|
wenzelm@18632
|
657 |
| get (Context.Proof prf) = PrfData.get prf;
|
wenzelm@18632
|
658 |
|
wenzelm@18632
|
659 |
fun put x (Context.Theory thy) = Context.Theory (ThyData.put x thy)
|
wenzelm@18632
|
660 |
| put x (Context.Proof prf) = Context.Proof (PrfData.put x prf);
|
wenzelm@18632
|
661 |
|
wenzelm@18632
|
662 |
fun map f ctxt = put (f (get ctxt)) ctxt;
|
wenzelm@18632
|
663 |
|
wenzelm@18632
|
664 |
end;
|
wenzelm@18632
|
665 |
|
wenzelm@16533
|
666 |
(*hide private interface*)
|
wenzelm@16436
|
667 |
structure Context: CONTEXT = Context;
|
wenzelm@20297
|
668 |
|
wenzelm@21518
|
669 |
(*fake predeclarations*)
|
wenzelm@20297
|
670 |
structure Proof = struct type context = Context.proof end;
|
wenzelm@21518
|
671 |
structure ProofContext =
|
wenzelm@21518
|
672 |
struct val theory_of = Context.theory_of_proof val init = Context.init_proof end;
|