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