author | wenzelm |
Sat, 20 May 2023 17:18:44 +0200 | |
changeset 78085 | dd7bb7f99ad5 |
parent 78062 | edb195122938 |
child 78464 | 12af46f2cd94 |
permissions | -rw-r--r-- |
6185 | 1 |
(* Title: Pure/context.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
4 |
Generic theory contexts with unique identity, arbitrarily typed data, |
24141
73baca986087
improved check_thy: produce a checked theory_ref (thread-safe version);
wenzelm
parents:
23944
diff
changeset
|
5 |
monotonic development graph and history support. Generic proof |
73baca986087
improved check_thy: produce a checked theory_ref (thread-safe version);
wenzelm
parents:
23944
diff
changeset
|
6 |
contexts with arbitrarily typed data. |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
7 |
|
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
8 |
Firm naming conventions: |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
9 |
thy, thy', thy1, thy2: theory |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
10 |
ctxt, ctxt', ctxt1, ctxt2: Proof.context |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
11 |
context: Context.generic |
6185 | 12 |
*) |
13 |
||
14 |
signature BASIC_CONTEXT = |
|
15 |
sig |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
16 |
type theory |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
17 |
exception THEORY of string * theory list |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
18 |
structure Proof: sig type context end |
42360 | 19 |
structure Proof_Context: |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
20 |
sig |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
21 |
val theory_of: Proof.context -> theory |
36610
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
34259
diff
changeset
|
22 |
val init_global: theory -> Proof.context |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
23 |
val get_global: {long: bool} -> theory -> string -> Proof.context |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
24 |
end |
6185 | 25 |
end; |
26 |
||
27 |
signature CONTEXT = |
|
28 |
sig |
|
29 |
include BASIC_CONTEXT |
|
77897 | 30 |
(*theory data*) |
31 |
type data_kind = int |
|
32 |
val data_kinds: unit -> (data_kind * Position.T) list |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
33 |
(*theory context*) |
77897 | 34 |
type id = int |
60947 | 35 |
type theory_id |
36 |
val theory_id: theory -> theory_id |
|
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
37 |
val data_timing: bool Unsynchronized.ref |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
38 |
val parents_of: theory -> theory list |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
39 |
val ancestors_of: theory -> theory list |
70586 | 40 |
val theory_id_ord: theory_id ord |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
41 |
val theory_id_name: {long: bool} -> theory_id -> string |
65459
da59e8e0a663
clarified theory_long_name (for qualified access to Thy_Info) vs. short theory_name (which is unique within any given theory context);
wenzelm
parents:
62889
diff
changeset
|
42 |
val theory_long_name: theory -> string |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
43 |
val theory_base_name: theory -> string |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
44 |
val theory_name: {long: bool} -> theory -> string |
77897 | 45 |
val theory_identifier: theory -> id |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
46 |
val PureN: string |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
47 |
val pretty_thy: theory -> Pretty.T |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
48 |
val pretty_abbrev_thy: theory -> Pretty.T |
68482 | 49 |
val get_theory: {long: bool} -> theory -> string -> theory |
60947 | 50 |
val eq_thy_id: theory_id * theory_id -> bool |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
51 |
val eq_thy: theory * theory -> bool |
60947 | 52 |
val proper_subthy_id: theory_id * theory_id -> bool |
53 |
val proper_subthy: theory * theory -> bool |
|
54 |
val subthy_id: theory_id * theory_id -> bool |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
55 |
val subthy: theory * theory -> bool |
77895 | 56 |
val join_thys: theory list -> theory |
67623 | 57 |
val begin_thy: string -> theory list -> theory |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
58 |
val finish_thy: theory -> theory |
77766 | 59 |
val theory_data_sizeof1: theory -> (Position.T * int) list |
16533 | 60 |
(*proof context*) |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
61 |
val raw_transfer: theory -> Proof.context -> Proof.context |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
62 |
(*certificate*) |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
63 |
datatype certificate = Certificate of theory | Certificate_Id of theory_id |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
64 |
val certificate_theory: certificate -> theory |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
65 |
val certificate_theory_id: certificate -> theory_id |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
66 |
val eq_certificate: certificate * certificate -> bool |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
67 |
val join_certificate: certificate * certificate -> certificate |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
68 |
val join_certificate_theory: theory * theory -> theory |
16533 | 69 |
(*generic context*) |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
70 |
datatype generic = Theory of theory | Proof of Proof.context |
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
71 |
val theory_tracing: bool Unsynchronized.ref |
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
72 |
val proof_tracing: bool Unsynchronized.ref |
78035 | 73 |
val enabled_tracing: unit -> bool |
78020 | 74 |
val finish_tracing: unit -> |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
75 |
{contexts: (generic * Position.T) list, |
77996 | 76 |
active_contexts: int, |
77 |
active_theories: int, |
|
78 |
active_proofs: int, |
|
79 |
total_contexts: int, |
|
80 |
total_theories: int, |
|
81 |
total_proofs: int} |
|
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
82 |
val cases: (theory -> 'a) -> (Proof.context -> 'a) -> generic -> 'a |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
83 |
val mapping: (theory -> theory) -> (Proof.context -> Proof.context) -> generic -> generic |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
84 |
val mapping_result: (theory -> 'a * theory) -> (Proof.context -> 'a * Proof.context) -> |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
85 |
generic -> 'a * generic |
18632 | 86 |
val the_theory: generic -> theory |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
87 |
val the_proof: generic -> Proof.context |
18731 | 88 |
val map_theory: (theory -> theory) -> generic -> generic |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
89 |
val map_proof: (Proof.context -> Proof.context) -> generic -> generic |
26486 | 90 |
val map_theory_result: (theory -> 'a * theory) -> generic -> 'a * generic |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
91 |
val map_proof_result: (Proof.context -> 'a * Proof.context) -> generic -> 'a * generic |
18731 | 92 |
val theory_map: (generic -> generic) -> theory -> theory |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
93 |
val proof_map: (generic -> generic) -> Proof.context -> Proof.context |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
94 |
val theory_of: generic -> theory (*total*) |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
95 |
val proof_of: generic -> Proof.context (*total*) |
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
96 |
(*thread data*) |
62889 | 97 |
val get_generic_context: unit -> generic option |
98 |
val put_generic_context: generic option -> unit |
|
99 |
val setmp_generic_context: generic option -> ('a -> 'b) -> 'a -> 'b |
|
62876 | 100 |
val the_generic_context: unit -> generic |
101 |
val the_global_context: unit -> theory |
|
102 |
val the_local_context: unit -> Proof.context |
|
26463 | 103 |
val >> : (generic -> generic) -> unit |
104 |
val >>> : (generic -> 'a * generic) -> 'a |
|
6185 | 105 |
end; |
106 |
||
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
107 |
signature PRIVATE_CONTEXT = |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
108 |
sig |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
109 |
include CONTEXT |
33033 | 110 |
structure Theory_Data: |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
111 |
sig |
77897 | 112 |
val declare: Position.T -> Any.T -> ((theory * Any.T) list -> Any.T) -> data_kind |
113 |
val get: data_kind -> (Any.T -> 'a) -> theory -> 'a |
|
114 |
val put: data_kind -> ('a -> Any.T) -> 'a -> theory -> theory |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
115 |
end |
33033 | 116 |
structure Proof_Data: |
16533 | 117 |
sig |
77897 | 118 |
val declare: (theory -> Any.T) -> data_kind |
119 |
val get: data_kind -> (Any.T -> 'a) -> Proof.context -> 'a |
|
120 |
val put: data_kind -> ('a -> Any.T) -> 'a -> Proof.context -> Proof.context |
|
16533 | 121 |
end |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
122 |
end; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
123 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
124 |
structure Context: PRIVATE_CONTEXT = |
6185 | 125 |
struct |
126 |
||
77994 | 127 |
(*** type definitions ***) |
6185 | 128 |
|
77994 | 129 |
(* context data *) |
77897 | 130 |
|
19028
6c238953f66c
structure Datatab: private copy avoids potential conflict of table exceptions;
wenzelm
parents:
18931
diff
changeset
|
131 |
(*private copy avoids potential conflict of table exceptions*) |
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
30628
diff
changeset
|
132 |
structure Datatab = Table(type key = int val ord = int_ord); |
19028
6c238953f66c
structure Datatab: private copy avoids potential conflict of table exceptions;
wenzelm
parents:
18931
diff
changeset
|
133 |
|
77897 | 134 |
type data_kind = int; |
135 |
val data_kind = Counter.make (); |
|
136 |
||
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
137 |
|
77994 | 138 |
(* theory identity *) |
139 |
||
140 |
type id = int; |
|
141 |
val new_id = Counter.make (); |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
142 |
|
77994 | 143 |
abstype theory_id = |
144 |
Thy_Id of |
|
145 |
{id: id, (*identifier*) |
|
146 |
ids: Intset.T, (*cumulative identifiers -- symbolic body content*) |
|
147 |
name: string, (*official theory name*) |
|
148 |
stage: int} (*index for anonymous updates*) |
|
149 |
with |
|
150 |
fun rep_theory_id (Thy_Id args) = args; |
|
151 |
val make_theory_id = Thy_Id; |
|
152 |
end; |
|
153 |
||
154 |
||
155 |
(* theory allocation state *) |
|
77895 | 156 |
|
157 |
type state = {stage: int} Synchronized.var; |
|
158 |
||
159 |
fun make_state () : state = |
|
160 |
Synchronized.var "Context.state" {stage = 0}; |
|
161 |
||
162 |
fun next_stage (state: state) = |
|
163 |
Synchronized.change_result state (fn {stage} => (stage + 1, {stage = stage + 1})); |
|
164 |
||
165 |
||
77994 | 166 |
(* theory and proof context *) |
77895 | 167 |
|
60947 | 168 |
datatype theory = |
77996 | 169 |
Thy_Undef |
170 |
| Thy of |
|
171 |
(*allocation state*) |
|
172 |
state * |
|
173 |
(*identity*) |
|
174 |
{theory_id: theory_id, |
|
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
175 |
theory_token: theory Unsynchronized.ref, |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
176 |
theory_token_pos: Position.T} * |
77996 | 177 |
(*ancestry*) |
178 |
{parents: theory list, (*immediate predecessors*) |
|
179 |
ancestors: theory list} * (*all predecessors -- canonical reverse order*) |
|
180 |
(*data*) |
|
181 |
Any.T Datatab.table; (*body content*) |
|
60947 | 182 |
|
77996 | 183 |
datatype proof = |
184 |
Prf_Undef |
|
185 |
| Prf of |
|
186 |
(*identity*) |
|
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
187 |
proof Unsynchronized.ref * (*token*) |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
188 |
Position.T * (*token_pos*) |
77996 | 189 |
theory * |
190 |
(*data*) |
|
191 |
Any.T Datatab.table; |
|
192 |
||
77994 | 193 |
structure Proof = struct type context = proof end; |
194 |
||
195 |
datatype generic = Theory of theory | Proof of Proof.context; |
|
196 |
||
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
197 |
|
77996 | 198 |
(* heap allocations *) |
199 |
||
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
200 |
val theory_tracing = Unsynchronized.ref false; |
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
201 |
val proof_tracing = Unsynchronized.ref false; |
77996 | 202 |
|
78035 | 203 |
fun enabled_tracing () = ! theory_tracing orelse ! proof_tracing; |
204 |
||
77996 | 205 |
local |
206 |
||
78020 | 207 |
fun cons_tokens var token = |
208 |
Synchronized.change var (fn (n, tokens) => (n + 1, Weak.weak (SOME token) :: tokens)); |
|
209 |
||
210 |
fun finish_tokens var = |
|
211 |
Synchronized.change_result var (fn (n, tokens) => |
|
212 |
let |
|
213 |
val tokens' = filter Unsynchronized.weak_active tokens; |
|
214 |
val results = map_filter Unsynchronized.weak_peek tokens'; |
|
215 |
in ((n, results), (n, tokens')) end); |
|
216 |
||
77996 | 217 |
fun make_token guard var token0 = |
218 |
if ! guard then |
|
219 |
let |
|
220 |
val token = Unsynchronized.ref (! token0); |
|
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
221 |
val pos = Position.thread_data (); |
78020 | 222 |
fun assign res = (token := res; cons_tokens var token; res); |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
223 |
in (token, pos, assign) end |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
224 |
else (token0, Position.none, I); |
77996 | 225 |
|
78020 | 226 |
val theory_tokens = Synchronized.var "theory_tokens" (0, []: theory Unsynchronized.weak_ref list); |
227 |
val proof_tokens = Synchronized.var "proof_tokens" (0, []: proof Unsynchronized.weak_ref list); |
|
77996 | 228 |
|
229 |
val theory_token0 = Unsynchronized.ref Thy_Undef; |
|
230 |
val proof_token0 = Unsynchronized.ref Prf_Undef; |
|
231 |
||
232 |
in |
|
233 |
||
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
234 |
fun theory_token () = make_token theory_tracing theory_tokens theory_token0; |
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
235 |
fun proof_token () = make_token proof_tracing proof_tokens proof_token0; |
77996 | 236 |
|
78020 | 237 |
fun finish_tracing () = |
77996 | 238 |
let |
239 |
val _ = ML_Heap.full_gc (); |
|
78020 | 240 |
val (total_theories, token_theories) = finish_tokens theory_tokens; |
241 |
val (total_proofs, token_proofs) = finish_tokens proof_tokens; |
|
77996 | 242 |
|
78020 | 243 |
fun cons1 (thy as Thy (_, {theory_token_pos, ...}, _, _)) = cons (Theory thy, theory_token_pos) |
244 |
| cons1 _ = I; |
|
245 |
fun cons2 (ctxt as Prf (_, proof_token_pos, _, _)) = cons (Proof ctxt, proof_token_pos) |
|
246 |
| cons2 _ = I; |
|
77996 | 247 |
|
78020 | 248 |
val contexts = build (fold cons1 token_theories #> fold cons2 token_proofs); |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
249 |
val active_theories = fold (fn (Theory _, _) => Integer.add 1 | _ => I) contexts 0; |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
250 |
val active_proofs = fold (fn (Proof _, _) => Integer.add 1 | _ => I) contexts 0; |
77996 | 251 |
in |
252 |
{contexts = contexts, |
|
253 |
active_contexts = active_theories + active_proofs, |
|
254 |
active_theories = active_theories, |
|
255 |
active_proofs = active_proofs, |
|
256 |
total_contexts = total_theories + total_proofs, |
|
257 |
total_theories = total_theories, |
|
258 |
total_proofs = total_proofs} |
|
259 |
end; |
|
260 |
||
261 |
end; |
|
262 |
||
263 |
||
77994 | 264 |
|
265 |
(*** theory operations ***) |
|
266 |
||
267 |
fun rep_theory (Thy args) = args; |
|
268 |
||
269 |
exception THEORY of string * theory list; |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
270 |
|
77895 | 271 |
val state_of = #1 o rep_theory; |
272 |
val theory_identity = #2 o rep_theory; |
|
67623 | 273 |
val theory_id = #theory_id o theory_identity; |
77886 | 274 |
val identity_of = rep_theory_id o theory_id; |
275 |
val ancestry_of = #3 o rep_theory; |
|
276 |
val data_of = #4 o rep_theory; |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
277 |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
278 |
fun make_ancestry parents ancestors = {parents = parents, ancestors = ancestors}; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
279 |
|
77886 | 280 |
fun stage_final stage = stage = 0; |
281 |
||
282 |
val theory_id_stage = #stage o rep_theory_id; |
|
283 |
val theory_id_final = stage_final o theory_id_stage; |
|
284 |
val theory_id_ord = int_ord o apply2 (#id o rep_theory_id); |
|
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
285 |
fun theory_id_name {long} thy_id = |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
286 |
let val name = #name (rep_theory_id thy_id) |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
287 |
in if long then name else Long_Name.base_name name end; |
77886 | 288 |
|
289 |
val theory_long_name = #name o identity_of; |
|
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
290 |
val theory_base_name = Long_Name.base_name o theory_long_name; |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
291 |
fun theory_name {long} = if long then theory_long_name else theory_base_name; |
77886 | 292 |
val theory_identifier = #id o identity_of; |
65459
da59e8e0a663
clarified theory_long_name (for qualified access to Thy_Info) vs. short theory_name (which is unique within any given theory context);
wenzelm
parents:
62889
diff
changeset
|
293 |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
294 |
val parents_of = #parents o ancestry_of; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
295 |
val ancestors_of = #ancestors o ancestry_of; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
296 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
297 |
|
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
298 |
(* names *) |
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
299 |
|
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
300 |
val PureN = "Pure"; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
301 |
|
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
302 |
fun display_name thy_id = |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
303 |
let |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
304 |
val name = theory_id_name {long = false} thy_id; |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
305 |
val final = theory_id_final thy_id; |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
306 |
in if final then name else name ^ ":" ^ string_of_int (theory_id_stage thy_id) end; |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
307 |
|
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
308 |
fun display_names thy = |
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
309 |
let |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
310 |
val name = display_name (theory_id thy); |
68192 | 311 |
val ancestor_names = map theory_long_name (ancestors_of thy); |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
312 |
in rev (name :: ancestor_names) end; |
29069
c7ba485581ae
unified ids for ancestors and checkpoints, removed obsolete history of checkpoints;
wenzelm
parents:
29001
diff
changeset
|
313 |
|
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
314 |
val pretty_thy = Pretty.str_list "{" "}" o display_names; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
315 |
|
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
316 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_thy); |
62663 | 317 |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
318 |
fun pretty_abbrev_thy thy = |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
319 |
let |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
320 |
val names = display_names thy; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
321 |
val n = length names; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
322 |
val abbrev = if n > 5 then "..." :: List.drop (names, n - 5) else names; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
323 |
in Pretty.str_list "{" "}" abbrev end; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
324 |
|
68482 | 325 |
fun get_theory long thy name = |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
326 |
if theory_name long thy <> name then |
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
327 |
(case find_first (fn thy' => theory_name long thy' = name) (ancestors_of thy) of |
37867
b9783e9e96e1
added Context.get_theory -- avoid referring to accidental theory loader state (cf. Thy_Info.get_theory);
wenzelm
parents:
37852
diff
changeset
|
328 |
SOME thy' => thy' |
b9783e9e96e1
added Context.get_theory -- avoid referring to accidental theory loader state (cf. Thy_Info.get_theory);
wenzelm
parents:
37852
diff
changeset
|
329 |
| NONE => error ("Unknown ancestor theory " ^ quote name)) |
77886 | 330 |
else if theory_id_final (theory_id thy) then thy |
37867
b9783e9e96e1
added Context.get_theory -- avoid referring to accidental theory loader state (cf. Thy_Info.get_theory);
wenzelm
parents:
37852
diff
changeset
|
331 |
else error ("Unfinished theory " ^ quote name); |
b9783e9e96e1
added Context.get_theory -- avoid referring to accidental theory loader state (cf. Thy_Info.get_theory);
wenzelm
parents:
37852
diff
changeset
|
332 |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
333 |
|
77895 | 334 |
(* identity *) |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
335 |
|
77895 | 336 |
fun merge_ids thys = |
337 |
fold (identity_of #> (fn {id, ids, ...} => fn acc => Intset.merge (acc, ids) |> Intset.insert id)) |
|
338 |
thys Intset.empty; |
|
16533 | 339 |
|
77886 | 340 |
val eq_thy_id = op = o apply2 (#id o rep_theory_id); |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
55547
diff
changeset
|
341 |
val eq_thy = op = o apply2 (#id o identity_of); |
16533 | 342 |
|
77886 | 343 |
val proper_subthy_id = apply2 rep_theory_id #> (fn ({id, ...}, {ids, ...}) => Intset.member ids id); |
60947 | 344 |
val proper_subthy = proper_subthy_id o apply2 theory_id; |
16533 | 345 |
|
60947 | 346 |
fun subthy_id p = eq_thy_id p orelse proper_subthy_id p; |
347 |
val subthy = subthy_id o apply2 theory_id; |
|
16533 | 348 |
|
349 |
||
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
350 |
(* consistent ancestors *) |
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
351 |
|
50431
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
352 |
fun eq_thy_consistent (thy1, thy2) = |
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
353 |
eq_thy (thy1, thy2) orelse |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
354 |
(theory_base_name thy1 = theory_base_name thy2 andalso |
50431
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
355 |
raise THEORY ("Duplicate theory name", [thy1, thy2])); |
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
356 |
|
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
357 |
fun extend_ancestors thy thys = |
50431
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
358 |
if member eq_thy_consistent thys thy then |
33033 | 359 |
raise THEORY ("Duplicate theory node", thy :: thys) |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
360 |
else thy :: thys; |
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
361 |
|
50431
955c4aa44f60
check consistent theory names for direct imports as well -- as claimed in the comments (see also 1cc36c0ec9eb);
wenzelm
parents:
48992
diff
changeset
|
362 |
val merge_ancestors = merge eq_thy_consistent; |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
363 |
|
77895 | 364 |
val eq_ancestry = |
365 |
apply2 ancestry_of #> |
|
366 |
(fn ({parents, ancestors}, {parents = parents', ancestors = ancestors'}) => |
|
367 |
eq_list eq_thy (parents, parents') andalso eq_list eq_thy (ancestors, ancestors')); |
|
368 |
||
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
369 |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
370 |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
371 |
(** theory data **) |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
372 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
373 |
(* data kinds and access methods *) |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
374 |
|
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
375 |
val data_timing = Unsynchronized.ref false; |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
376 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
377 |
local |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
378 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
379 |
type kind = |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
380 |
{pos: Position.T, |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
381 |
empty: Any.T, |
77895 | 382 |
merge: (theory * Any.T) list -> Any.T}; |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
383 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
384 |
val kinds = Synchronized.var "Theory_Data" (Datatab.empty: kind Datatab.table); |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
385 |
|
77895 | 386 |
fun the_kind k = |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
387 |
(case Datatab.lookup (Synchronized.value kinds) k of |
77895 | 388 |
SOME kind => kind |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
389 |
| NONE => raise Fail "Invalid theory data identifier"); |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
390 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
391 |
in |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
392 |
|
77897 | 393 |
fun data_kinds () = |
394 |
Datatab.fold_rev (fn (k, {pos, ...}) => cons (k, pos)) (Synchronized.value kinds) []; |
|
395 |
||
77895 | 396 |
val invoke_pos = #pos o the_kind; |
397 |
val invoke_empty = #empty o the_kind; |
|
398 |
||
399 |
fun invoke_merge kind args = |
|
78018
dfa44d85d751
proper system options to control context tracing/timing;
wenzelm
parents:
78004
diff
changeset
|
400 |
if ! data_timing then |
77895 | 401 |
Timing.cond_timeit true ("Theory_Data.merge" ^ Position.here (#pos kind)) |
402 |
(fn () => #merge kind args) |
|
403 |
else #merge kind args; |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
404 |
|
77894 | 405 |
fun declare_data pos empty merge = |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
406 |
let |
77897 | 407 |
val k = data_kind (); |
74561
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
408 |
val kind = {pos = pos, empty = empty, merge = merge}; |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
409 |
val _ = Synchronized.change kinds (Datatab.update (k, kind)); |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
410 |
in k end; |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
411 |
|
77895 | 412 |
fun lookup_data k thy = Datatab.lookup (data_of thy) k; |
413 |
||
77894 | 414 |
fun get_data k thy = |
77895 | 415 |
(case lookup_data k thy of |
77894 | 416 |
SOME x => x |
417 |
| NONE => invoke_empty k); |
|
418 |
||
77895 | 419 |
fun merge_data [] = Datatab.empty |
420 |
| merge_data [thy] = data_of thy |
|
421 |
| merge_data thys = |
|
422 |
let |
|
423 |
fun merge (k, kind) data = |
|
424 |
(case map_filter (fn thy => lookup_data k thy |> Option.map (pair thy)) thys of |
|
425 |
[] => data |
|
426 |
| [(_, x)] => Datatab.default (k, x) data |
|
427 |
| args => Datatab.update (k, invoke_merge kind args) data); |
|
428 |
in Datatab.fold merge (Synchronized.value kinds) (data_of (hd thys)) end; |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
429 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
430 |
end; |
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
431 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
432 |
|
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
433 |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
434 |
(** build theories **) |
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
435 |
|
67623 | 436 |
(* create theory *) |
437 |
||
77895 | 438 |
fun create_thy state ids name stage ancestry data = |
67623 | 439 |
let |
77897 | 440 |
val theory_id = make_theory_id {id = new_id (), ids = ids, name = name, stage = stage}; |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
441 |
val (token, pos, assign) = theory_token (); |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
442 |
val identity = {theory_id = theory_id, theory_token = token, theory_token_pos = pos}; |
77996 | 443 |
in assign (Thy (state, identity, ancestry, data)) end; |
67623 | 444 |
|
445 |
||
446 |
(* primitives *) |
|
33606
2b27020ffcb2
local mutex for theory content/identity operations;
wenzelm
parents:
33517
diff
changeset
|
447 |
|
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
448 |
val pre_pure_thy = |
77886 | 449 |
let |
450 |
val state = make_state (); |
|
451 |
val stage = next_stage state; |
|
77895 | 452 |
in create_thy state Intset.empty PureN stage (make_ancestry [] []) Datatab.empty end; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
453 |
|
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
454 |
local |
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
455 |
|
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
456 |
fun change_thy finish f thy = |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
457 |
let |
77895 | 458 |
val {name, stage, ...} = identity_of thy; |
77994 | 459 |
val Thy (state, _, ancestry, data) = thy; |
74561
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
460 |
val ancestry' = |
77886 | 461 |
if stage_final stage |
74561
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
462 |
then make_ancestry [thy] (extend_ancestors thy (ancestors_of thy)) |
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
463 |
else ancestry; |
77895 | 464 |
val ids' = merge_ids [thy]; |
77886 | 465 |
val stage' = if finish then 0 else next_stage state; |
74561
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
466 |
val data' = f data; |
77895 | 467 |
in create_thy state ids' name stage' ancestry' data' end; |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
468 |
|
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
469 |
in |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
470 |
|
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
471 |
val update_thy = change_thy false; |
74561
8e6c973003c8
discontinued obsolete "val extend = I" for data slots;
wenzelm
parents:
74461
diff
changeset
|
472 |
val finish_thy = change_thy true I; |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
473 |
|
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
474 |
end; |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
475 |
|
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
476 |
|
77895 | 477 |
(* join: unfinished theory nodes *) |
70361
34b271c4f400
support join of anonymous theory nodes, e.g. relevant for parallel theory construction;
wenzelm
parents:
70360
diff
changeset
|
478 |
|
77895 | 479 |
fun join_thys [] = raise List.Empty |
480 |
| join_thys thys = |
|
481 |
let |
|
482 |
val thy0 = hd thys; |
|
483 |
val name0 = theory_long_name thy0; |
|
484 |
val state0 = state_of thy0; |
|
70361
34b271c4f400
support join of anonymous theory nodes, e.g. relevant for parallel theory construction;
wenzelm
parents:
70360
diff
changeset
|
485 |
|
77895 | 486 |
fun ok thy = |
487 |
not (theory_id_final (theory_id thy)) andalso |
|
488 |
theory_long_name thy = name0 andalso |
|
489 |
eq_ancestry (thy0, thy); |
|
490 |
val _ = |
|
491 |
(case filter_out ok thys of |
|
492 |
[] => () |
|
493 |
| bad => raise THEORY ("Cannot join theories", bad)); |
|
70361
34b271c4f400
support join of anonymous theory nodes, e.g. relevant for parallel theory construction;
wenzelm
parents:
70360
diff
changeset
|
494 |
|
77895 | 495 |
val stage = next_stage state0; |
496 |
val ids = merge_ids thys; |
|
497 |
val data = merge_data thys; |
|
498 |
in create_thy state0 ids name0 stage (ancestry_of thy0) data end; |
|
70361
34b271c4f400
support join of anonymous theory nodes, e.g. relevant for parallel theory construction;
wenzelm
parents:
70360
diff
changeset
|
499 |
|
34b271c4f400
support join of anonymous theory nodes, e.g. relevant for parallel theory construction;
wenzelm
parents:
70360
diff
changeset
|
500 |
|
77895 | 501 |
(* merge: finished theory nodes *) |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
502 |
|
77895 | 503 |
fun make_parents thys = |
504 |
let val thys' = distinct eq_thy thys |
|
505 |
in thys' |> filter_out (fn thy => exists (fn thy' => proper_subthy (thy, thy')) thys') end; |
|
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
506 |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
507 |
fun begin_thy name imports = |
52696
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
wenzelm
parents:
52682
diff
changeset
|
508 |
if name = "" then error ("Bad theory name: " ^ quote name) |
77895 | 509 |
else if null imports then error "Missing theory imports" |
24369 | 510 |
else |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
511 |
let |
77895 | 512 |
val parents = make_parents imports; |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
513 |
val ancestors = |
77895 | 514 |
Library.foldl1 merge_ancestors (map ancestors_of parents) |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
515 |
|> fold extend_ancestors parents; |
77895 | 516 |
val ancestry = make_ancestry parents ancestors; |
29093
1cc36c0ec9eb
refined identity and ancestry: explicit draft flat, ids are unnamed, name consistency is checked for ancestry;
wenzelm
parents:
29069
diff
changeset
|
517 |
|
77886 | 518 |
val state = make_state (); |
519 |
val stage = next_stage state; |
|
77895 | 520 |
val ids = merge_ids parents; |
521 |
val data = merge_data parents; |
|
522 |
in create_thy state ids name stage ancestry data |> tap finish_thy end; |
|
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
523 |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
524 |
|
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
525 |
(* theory data *) |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
526 |
|
33033 | 527 |
structure Theory_Data = |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
528 |
struct |
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
529 |
|
77894 | 530 |
val declare = declare_data; |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
531 |
|
77894 | 532 |
fun get k dest thy = dest (get_data k thy); |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
533 |
|
77894 | 534 |
fun put k make x = update_thy (Datatab.update (k, make x)); |
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
535 |
|
77693
068ff989c143
clarified theory_sizeof1_data: count bytes, individually for each data entry;
wenzelm
parents:
74561
diff
changeset
|
536 |
fun sizeof1 k thy = |
068ff989c143
clarified theory_sizeof1_data: count bytes, individually for each data entry;
wenzelm
parents:
74561
diff
changeset
|
537 |
Datatab.lookup (data_of thy) k |> Option.map ML_Heap.sizeof1; |
67621 | 538 |
|
16489
f66ab8a4e98f
improved treatment of intermediate checkpoints: actual copy
wenzelm
parents:
16436
diff
changeset
|
539 |
end; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
540 |
|
77766 | 541 |
fun theory_data_sizeof1 thy = |
74234 | 542 |
build (data_of thy |> Datatab.fold_rev (fn (k, _) => |
77693
068ff989c143
clarified theory_sizeof1_data: count bytes, individually for each data entry;
wenzelm
parents:
74561
diff
changeset
|
543 |
(case Theory_Data.sizeof1 k thy of |
67621 | 544 |
NONE => I |
74234 | 545 |
| SOME n => (cons (invoke_pos k, n))))); |
67621 | 546 |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
547 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
548 |
|
16533 | 549 |
(*** proof context ***) |
550 |
||
551 |
(* proof data kinds *) |
|
552 |
||
553 |
local |
|
554 |
||
51368
2ea5c7c2d825
tuned signature -- prefer terminology of Scala and Axiom;
wenzelm
parents:
50431
diff
changeset
|
555 |
val kinds = Synchronized.var "Proof_Data" (Datatab.empty: (theory -> Any.T) Datatab.table); |
16533 | 556 |
|
59163 | 557 |
fun init_data thy = |
558 |
Synchronized.value kinds |> Datatab.map (fn _ => fn init => init thy); |
|
16533 | 559 |
|
59163 | 560 |
fun init_new_data thy = |
561 |
Synchronized.value kinds |> Datatab.fold (fn (k, init) => fn data => |
|
562 |
if Datatab.defined data k then data |
|
563 |
else Datatab.update (k, init thy) data); |
|
22847
22da6c4bc422
simplified DataFun interfaces: removed name/print, use adhoc value for uninitialized data, init only required for impure data;
wenzelm
parents:
22827
diff
changeset
|
564 |
|
59163 | 565 |
fun init_fallback k thy = |
566 |
(case Datatab.lookup (Synchronized.value kinds) k of |
|
567 |
SOME init => init thy |
|
568 |
| NONE => raise Fail "Invalid proof data identifier"); |
|
16533 | 569 |
|
570 |
in |
|
571 |
||
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
572 |
fun raw_transfer thy' (ctxt as Prf (_, _, thy, data)) = |
77996 | 573 |
if eq_thy (thy, thy') then ctxt |
574 |
else if proper_subthy (thy, thy') then |
|
575 |
let |
|
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
576 |
val (token', pos', assign) = proof_token (); |
77996 | 577 |
val data' = init_new_data thy' data; |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
578 |
in assign (Prf (token', pos', thy', data')) end |
77996 | 579 |
else error "Cannot transfer proof context: not a super theory"; |
22847
22da6c4bc422
simplified DataFun interfaces: removed name/print, use adhoc value for uninitialized data, init only required for impure data;
wenzelm
parents:
22827
diff
changeset
|
580 |
|
42360 | 581 |
structure Proof_Context = |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
582 |
struct |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
583 |
fun theory_of (Prf (_, _, thy, _)) = thy; |
77996 | 584 |
fun init_global thy = |
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
585 |
let val (token, pos, assign) = proof_token () |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
586 |
in assign (Prf (token, pos, thy, init_data thy)) end; |
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
77886
diff
changeset
|
587 |
fun get_global long thy name = init_global (get_theory long thy name); |
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
588 |
end; |
16533 | 589 |
|
33033 | 590 |
structure Proof_Data = |
16533 | 591 |
struct |
592 |
||
22847
22da6c4bc422
simplified DataFun interfaces: removed name/print, use adhoc value for uninitialized data, init only required for impure data;
wenzelm
parents:
22827
diff
changeset
|
593 |
fun declare init = |
16533 | 594 |
let |
77897 | 595 |
val k = data_kind (); |
43684 | 596 |
val _ = Synchronized.change kinds (Datatab.update (k, init)); |
16533 | 597 |
in k end; |
598 |
||
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
599 |
fun get k dest (Prf (_, _, thy, data)) = |
59163 | 600 |
(case Datatab.lookup data k of |
22847
22da6c4bc422
simplified DataFun interfaces: removed name/print, use adhoc value for uninitialized data, init only required for impure data;
wenzelm
parents:
22827
diff
changeset
|
601 |
SOME x => x |
59163 | 602 |
| NONE => init_fallback k thy) |> dest; |
16533 | 603 |
|
78004
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
604 |
fun put k make x (Prf (_, _, thy, data)) = |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
605 |
let |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
606 |
val (token', pos', assign) = proof_token (); |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
607 |
val data' = Datatab.update (k, make x) data; |
19962431aea8
maintain dynamic position where values are created (again, amending afa6117bace4);
wenzelm
parents:
78003
diff
changeset
|
608 |
in assign (Prf (token', pos', thy, data')) end; |
16533 | 609 |
|
610 |
end; |
|
611 |
||
612 |
end; |
|
613 |
||
614 |
||
18632 | 615 |
|
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
616 |
(*** theory certificate ***) |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
617 |
|
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
618 |
datatype certificate = Certificate of theory | Certificate_Id of theory_id; |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
619 |
|
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
620 |
fun certificate_theory (Certificate thy) = thy |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
621 |
| certificate_theory (Certificate_Id thy_id) = |
61062 | 622 |
error ("No content for theory certificate " ^ display_name thy_id); |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
623 |
|
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
624 |
fun certificate_theory_id (Certificate thy) = theory_id thy |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
625 |
| certificate_theory_id (Certificate_Id thy_id) = thy_id; |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
626 |
|
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
627 |
fun eq_certificate (Certificate thy1, Certificate thy2) = eq_thy (thy1, thy2) |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
628 |
| eq_certificate (Certificate_Id thy_id1, Certificate_Id thy_id2) = eq_thy_id (thy_id1, thy_id2) |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
629 |
| eq_certificate _ = false; |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
630 |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
631 |
fun err_join (thy_id1, thy_id2) = |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
632 |
error ("Cannot join unrelated theory certificates " ^ |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
633 |
display_name thy_id1 ^ " and " ^ display_name thy_id2); |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
634 |
|
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
635 |
fun join_certificate (cert1, cert2) = |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
636 |
let val (thy_id1, thy_id2) = apply2 certificate_theory_id (cert1, cert2) in |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
637 |
if eq_thy_id (thy_id1, thy_id2) then (case cert1 of Certificate _ => cert1 | _ => cert2) |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
638 |
else if proper_subthy_id (thy_id2, thy_id1) then cert1 |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
639 |
else if proper_subthy_id (thy_id1, thy_id2) then cert2 |
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
640 |
else err_join (thy_id1, thy_id2) |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
641 |
end; |
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
642 |
|
78062
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
643 |
fun join_certificate_theory (thy1, thy2) = |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
644 |
let val (thy_id1, thy_id2) = apply2 theory_id (thy1, thy2) in |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
645 |
if subthy_id (thy_id2, thy_id1) then thy1 |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
646 |
else if proper_subthy_id (thy_id1, thy_id2) then thy2 |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
647 |
else err_join (thy_id1, thy_id2) |
edb195122938
support for context within morphism (for background theory);
wenzelm
parents:
78035
diff
changeset
|
648 |
end; |
61044
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
649 |
|
b7af255dd200
more abstract theory certificate, which is not necessarily the full theory;
wenzelm
parents:
60948
diff
changeset
|
650 |
|
16533 | 651 |
(*** generic context ***) |
652 |
||
18632 | 653 |
fun cases f _ (Theory thy) = f thy |
654 |
| cases _ g (Proof prf) = g prf; |
|
16533 | 655 |
|
19678 | 656 |
fun mapping f g = cases (Theory o f) (Proof o g); |
21660 | 657 |
fun mapping_result f g = cases (apsnd Theory o f) (apsnd Proof o g); |
19678 | 658 |
|
23595
7ca68a2c8575
the_theory/proof: error instead of exception Fail;
wenzelm
parents:
23355
diff
changeset
|
659 |
val the_theory = cases I (fn _ => error "Ill-typed context: theory expected"); |
7ca68a2c8575
the_theory/proof: error instead of exception Fail;
wenzelm
parents:
23355
diff
changeset
|
660 |
val the_proof = cases (fn _ => error "Ill-typed context: proof expected") I; |
16533 | 661 |
|
18731 | 662 |
fun map_theory f = Theory o f o the_theory; |
663 |
fun map_proof f = Proof o f o the_proof; |
|
664 |
||
26486 | 665 |
fun map_theory_result f = apsnd Theory o f o the_theory; |
666 |
fun map_proof_result f = apsnd Proof o f o the_proof; |
|
667 |
||
18731 | 668 |
fun theory_map f = the_theory o f o Theory; |
669 |
fun proof_map f = the_proof o f o Proof; |
|
18665 | 670 |
|
42360 | 671 |
val theory_of = cases I Proof_Context.theory_of; |
672 |
val proof_of = cases Proof_Context.init_global I; |
|
16533 | 673 |
|
22085
c138cfd500f7
ML context: full generic context, tuned signature;
wenzelm
parents:
21962
diff
changeset
|
674 |
|
c138cfd500f7
ML context: full generic context, tuned signature;
wenzelm
parents:
21962
diff
changeset
|
675 |
|
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
676 |
(** thread data **) |
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
677 |
|
62889 | 678 |
local val generic_context_var = Thread_Data.var () : generic Thread_Data.var in |
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
679 |
|
62889 | 680 |
fun get_generic_context () = Thread_Data.get generic_context_var; |
681 |
val put_generic_context = Thread_Data.put generic_context_var; |
|
682 |
fun setmp_generic_context opt_context = Thread_Data.setmp generic_context_var opt_context; |
|
62876 | 683 |
|
684 |
fun the_generic_context () = |
|
62889 | 685 |
(case get_generic_context () of |
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
686 |
SOME context => context |
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
687 |
| _ => error "Unknown context"); |
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
688 |
|
62876 | 689 |
val the_global_context = theory_of o the_generic_context; |
690 |
val the_local_context = proof_of o the_generic_context; |
|
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
691 |
|
26428 | 692 |
end; |
693 |
||
694 |
fun >>> f = |
|
695 |
let |
|
62876 | 696 |
val (res, context') = f (the_generic_context ()); |
62889 | 697 |
val _ = put_generic_context (SOME context'); |
26428 | 698 |
in res end; |
699 |
||
26421 | 700 |
nonfix >>; |
26463 | 701 |
fun >> f = >>> (fn context => ((), f context)); |
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
702 |
|
62889 | 703 |
val _ = put_generic_context (SOME (Theory pre_pure_thy)); |
26413
003dd6155870
added thread data (formerly global ref in ML/ml_context.ML);
wenzelm
parents:
24559
diff
changeset
|
704 |
|
6185 | 705 |
end; |
706 |
||
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
707 |
structure Basic_Context: BASIC_CONTEXT = Context; |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
708 |
open Basic_Context; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
709 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
710 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
711 |
|
16533 | 712 |
(*** type-safe interfaces for data declarations ***) |
713 |
||
714 |
(** theory data **) |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
715 |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
716 |
signature THEORY_DATA'_ARGS = |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
717 |
sig |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
718 |
type T |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
719 |
val empty: T |
77895 | 720 |
val merge: (theory * T) list -> T |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
721 |
end; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
722 |
|
34259
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
723 |
signature THEORY_DATA_ARGS = |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
724 |
sig |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
725 |
type T |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
726 |
val empty: T |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
727 |
val merge: T * T -> T |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
728 |
end; |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
729 |
|
34253
5930c6391126
removed further remains of mutable theory data (cf. 25bd3ed2ac9f);
wenzelm
parents:
34245
diff
changeset
|
730 |
signature THEORY_DATA = |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
731 |
sig |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
732 |
type T |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
733 |
val get: theory -> T |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
734 |
val put: T -> theory -> theory |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
735 |
val map: (T -> T) -> theory -> theory |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
736 |
end; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
737 |
|
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
738 |
functor Theory_Data'(Data: THEORY_DATA'_ARGS): THEORY_DATA = |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
739 |
struct |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
740 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
741 |
type T = Data.T; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
742 |
exception Data of T; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
743 |
|
42818
128cc195ced3
timing of Theory_Data operations, with implicit thread positions when functor is applied;
wenzelm
parents:
42383
diff
changeset
|
744 |
val kind = |
72060
efb7fd4a6d1f
subtle change of Theory_Data extend/merge semantics due to Theory.join_theory;
wenzelm
parents:
70586
diff
changeset
|
745 |
let val pos = Position.thread_data () in |
efb7fd4a6d1f
subtle change of Theory_Data extend/merge semantics due to Theory.join_theory;
wenzelm
parents:
70586
diff
changeset
|
746 |
Context.Theory_Data.declare |
efb7fd4a6d1f
subtle change of Theory_Data extend/merge semantics due to Theory.join_theory;
wenzelm
parents:
70586
diff
changeset
|
747 |
pos |
efb7fd4a6d1f
subtle change of Theory_Data extend/merge semantics due to Theory.join_theory;
wenzelm
parents:
70586
diff
changeset
|
748 |
(Data Data.empty) |
77895 | 749 |
(Data o Data.merge o map (fn (thy, Data x) => (thy, x))) |
72060
efb7fd4a6d1f
subtle change of Theory_Data extend/merge semantics due to Theory.join_theory;
wenzelm
parents:
70586
diff
changeset
|
750 |
end; |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
751 |
|
33033 | 752 |
val get = Context.Theory_Data.get kind (fn Data x => x); |
753 |
val put = Context.Theory_Data.put kind Data; |
|
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
754 |
fun map f thy = put (f (get thy)) thy; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
755 |
|
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
756 |
end; |
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
757 |
|
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
758 |
functor Theory_Data(Data: THEORY_DATA_ARGS): THEORY_DATA = |
61262
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents:
61062
diff
changeset
|
759 |
Theory_Data' |
34259
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
760 |
( |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
761 |
type T = Data.T; |
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
762 |
val empty = Data.empty; |
77895 | 763 |
fun merge args = Library.foldl (fn (a, (_, b)) => Data.merge (a, b)) (#2 (hd args), tl args) |
34259
2ba492b8b6e8
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
wenzelm
parents:
34253
diff
changeset
|
764 |
); |
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
765 |
|
16533 | 766 |
|
767 |
||
768 |
(** proof data **) |
|
769 |
||
770 |
signature PROOF_DATA_ARGS = |
|
771 |
sig |
|
772 |
type T |
|
773 |
val init: theory -> T |
|
774 |
end; |
|
775 |
||
776 |
signature PROOF_DATA = |
|
777 |
sig |
|
778 |
type T |
|
33031
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
779 |
val get: Proof.context -> T |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
780 |
val put: T -> Proof.context -> Proof.context |
b75c35574e04
backpatching of structure Proof and ProofContext -- avoid odd aliases;
wenzelm
parents:
32784
diff
changeset
|
781 |
val map: (T -> T) -> Proof.context -> Proof.context |
16533 | 782 |
end; |
783 |
||
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
784 |
functor Proof_Data(Data: PROOF_DATA_ARGS): PROOF_DATA = |
16533 | 785 |
struct |
786 |
||
787 |
type T = Data.T; |
|
788 |
exception Data of T; |
|
789 |
||
33033 | 790 |
val kind = Context.Proof_Data.declare (Data o Data.init); |
16533 | 791 |
|
33033 | 792 |
val get = Context.Proof_Data.get kind (fn Data x => x); |
793 |
val put = Context.Proof_Data.put kind Data; |
|
16533 | 794 |
fun map f prf = put (f (get prf)) prf; |
795 |
||
796 |
end; |
|
797 |
||
18632 | 798 |
|
799 |
||
800 |
(** generic data **) |
|
801 |
||
802 |
signature GENERIC_DATA_ARGS = |
|
803 |
sig |
|
804 |
type T |
|
805 |
val empty: T |
|
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
806 |
val merge: T * T -> T |
18632 | 807 |
end; |
808 |
||
809 |
signature GENERIC_DATA = |
|
810 |
sig |
|
811 |
type T |
|
812 |
val get: Context.generic -> T |
|
813 |
val put: T -> Context.generic -> Context.generic |
|
814 |
val map: (T -> T) -> Context.generic -> Context.generic |
|
815 |
end; |
|
816 |
||
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
817 |
functor Generic_Data(Data: GENERIC_DATA_ARGS): GENERIC_DATA = |
18632 | 818 |
struct |
819 |
||
33517
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
820 |
structure Thy_Data = Theory_Data(Data); |
d064fa48f305
modernized/simplified functor Theory_Data, Proof_Data, Generic_Data: eliminated Pretty.pp, discontinued mutable data;
wenzelm
parents:
33033
diff
changeset
|
821 |
structure Prf_Data = Proof_Data(type T = Data.T val init = Thy_Data.get); |
18632 | 822 |
|
823 |
type T = Data.T; |
|
824 |
||
33033 | 825 |
fun get (Context.Theory thy) = Thy_Data.get thy |
826 |
| get (Context.Proof prf) = Prf_Data.get prf; |
|
18632 | 827 |
|
33033 | 828 |
fun put x (Context.Theory thy) = Context.Theory (Thy_Data.put x thy) |
829 |
| put x (Context.Proof prf) = Context.Proof (Prf_Data.put x prf); |
|
18632 | 830 |
|
831 |
fun map f ctxt = put (f (get ctxt)) ctxt; |
|
832 |
||
833 |
end; |
|
834 |
||
16533 | 835 |
(*hide private interface*) |
16436
7eb6b6cbd166
added type theory: generic theory contexts with unique identity,
wenzelm
parents:
15801
diff
changeset
|
836 |
structure Context: CONTEXT = Context; |