author | haftmann |
Wed, 15 Jul 2009 18:20:08 +0200 | |
changeset 32074 | 76d6ba08a05f |
parent 31988 | 801aabf9f376 |
child 32113 | bafffa63ebfd |
permissions | -rw-r--r-- |
29361 | 1 |
(* Title: Pure/Isar/locale.ML |
2 |
Author: Clemens Ballarin, TU Muenchen |
|
3 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
4 |
Locales -- managed Isar proof contexts, based on Pure predicates. |
29361 | 5 |
|
6 |
Draws basic ideas from Florian Kammueller's original version of |
|
7 |
locales, but uses the richer infrastructure of Isar instead of the raw |
|
8 |
meta-logic. Furthermore, structured import of contexts (with merge |
|
9 |
and rename operations) are provided, as well as type-inference of the |
|
10 |
signature parts, and predicate definitions of the specification text. |
|
11 |
||
12 |
Interpretation enables the reuse of theorems of locales in other |
|
13 |
contexts, namely those defined by theories, structured proofs and |
|
14 |
locales themselves. |
|
15 |
||
16 |
See also: |
|
17 |
||
18 |
[1] Clemens Ballarin. Locales and Locale Expressions in Isabelle/Isar. |
|
19 |
In Stefano Berardi et al., Types for Proofs and Programs: International |
|
20 |
Workshop, TYPES 2003, Torino, Italy, LNCS 3085, pages 34-50, 2004. |
|
21 |
[2] Clemens Ballarin. Interpretation of Locales in Isabelle: Managing |
|
22 |
Dependencies between Locales. Technical Report TUM-I0607, Technische |
|
23 |
Universitaet Muenchen, 2006. |
|
24 |
[3] Clemens Ballarin. Interpretation of Locales in Isabelle: Theories and |
|
25 |
Proof Contexts. In J.M. Borwein and W.M. Farmer, MKM 2006, LNAI 4108, |
|
26 |
pages 31-43, 2006. |
|
27 |
*) |
|
28 |
||
29 |
signature LOCALE = |
|
30 |
sig |
|
29576 | 31 |
(* Locale specification *) |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30223
diff
changeset
|
32 |
val register_locale: binding -> |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
33 |
(string * sort) list * ((string * typ) * mixfix) list -> |
29361 | 34 |
term option * term list -> |
29441 | 35 |
thm option * thm option -> thm list -> |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
36 |
declaration list * declaration list -> |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
37 |
(string * (Attrib.binding * (thm list * Attrib.src list) list) list) list -> |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
38 |
(string * morphism) list -> theory -> theory |
29361 | 39 |
val intern: theory -> xstring -> string |
40 |
val extern: theory -> string -> xstring |
|
29392 | 41 |
val defined: theory -> string -> bool |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
42 |
val params_of: theory -> string -> ((string * typ) * mixfix) list |
29441 | 43 |
val intros_of: theory -> string -> thm option * thm option |
44 |
val axioms_of: theory -> string -> thm list |
|
29544 | 45 |
val instance_of: theory -> string -> morphism -> term list |
29361 | 46 |
val specification_of: theory -> string -> term option * term list |
47 |
val declarations_of: theory -> string -> declaration list * declaration list |
|
48 |
||
49 |
(* Storing results *) |
|
50 |
val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list -> |
|
51 |
Proof.context -> Proof.context |
|
52 |
val add_type_syntax: string -> declaration -> Proof.context -> Proof.context |
|
53 |
val add_term_syntax: string -> declaration -> Proof.context -> Proof.context |
|
54 |
val add_declaration: string -> declaration -> Proof.context -> Proof.context |
|
55 |
||
56 |
(* Activation *) |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
57 |
val activate_declarations: string * morphism -> Proof.context -> Proof.context |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
58 |
val activate_facts: string * morphism -> Context.generic -> Context.generic |
29361 | 59 |
val init: string -> theory -> Proof.context |
60 |
||
61 |
(* Reasoning about locales *) |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
62 |
val get_witnesses: Proof.context -> thm list |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
63 |
val get_intros: Proof.context -> thm list |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
64 |
val get_unfolds: Proof.context -> thm list |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
65 |
val witness_add: attribute |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
66 |
val intro_add: attribute |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
67 |
val unfold_add: attribute |
29361 | 68 |
val intro_locales_tac: bool -> Proof.context -> thm list -> tactic |
69 |
||
31988 | 70 |
(* Registrations and dependencies *) |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
71 |
val add_registration: string * (morphism * morphism) -> theory -> theory |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
72 |
val amend_registration: morphism -> string * morphism -> theory -> theory |
32074 | 73 |
val add_registration_eqs: string * morphism -> thm list -> morphism -> theory -> theory |
74 |
val add_dependency: string -> string * morphism -> morphism -> theory -> theory |
|
29361 | 75 |
|
76 |
(* Diagnostic *) |
|
77 |
val print_locales: theory -> unit |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30223
diff
changeset
|
78 |
val print_locale: theory -> bool -> xstring -> unit |
29361 | 79 |
end; |
80 |
||
81 |
structure Locale: LOCALE = |
|
82 |
struct |
|
83 |
||
84 |
datatype ctxt = datatype Element.ctxt; |
|
85 |
||
29392 | 86 |
|
87 |
(*** Theory data ***) |
|
29361 | 88 |
|
89 |
datatype locale = Loc of { |
|
29392 | 90 |
(** static part **) |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
91 |
parameters: (string * sort) list * ((string * typ) * mixfix) list, |
29361 | 92 |
(* type and term parameters *) |
93 |
spec: term option * term list, |
|
94 |
(* assumptions (as a single predicate expression) and defines *) |
|
29441 | 95 |
intros: thm option * thm option, |
96 |
axioms: thm list, |
|
29392 | 97 |
(** dynamic part **) |
29361 | 98 |
decls: (declaration * stamp) list * (declaration * stamp) list, |
99 |
(* type and term syntax declarations *) |
|
100 |
notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list, |
|
101 |
(* theorem declarations *) |
|
29544 | 102 |
dependencies: ((string * morphism) * stamp) list |
29361 | 103 |
(* locale dependencies (sublocale relation) *) |
29392 | 104 |
}; |
29361 | 105 |
|
29441 | 106 |
fun mk_locale ((parameters, spec, intros, axioms), ((decls, notes), dependencies)) = |
107 |
Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec, |
|
108 |
decls = decls, notes = notes, dependencies = dependencies}; |
|
30754 | 109 |
|
29441 | 110 |
fun map_locale f (Loc {parameters, spec, intros, axioms, decls, notes, dependencies}) = |
111 |
mk_locale (f ((parameters, spec, intros, axioms), ((decls, notes), dependencies))); |
|
30754 | 112 |
|
29441 | 113 |
fun merge_locale (Loc {parameters, spec, intros, axioms, decls = (decls1, decls2), |
114 |
notes, dependencies}, Loc {decls = (decls1', decls2'), notes = notes', |
|
115 |
dependencies = dependencies', ...}) = mk_locale |
|
116 |
((parameters, spec, intros, axioms), |
|
117 |
(((merge (eq_snd op =) (decls1, decls1'), merge (eq_snd op =) (decls2, decls2')), |
|
118 |
merge (eq_snd op =) (notes, notes')), |
|
119 |
merge (eq_snd op =) (dependencies, dependencies'))); |
|
29361 | 120 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
121 |
structure Locales = TheoryDataFun |
29361 | 122 |
( |
29392 | 123 |
type T = locale NameSpace.table; |
29361 | 124 |
val empty = NameSpace.empty_table; |
125 |
val copy = I; |
|
126 |
val extend = I; |
|
29392 | 127 |
fun merge _ = NameSpace.join_tables (K merge_locale); |
29361 | 128 |
); |
129 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
130 |
val intern = NameSpace.intern o #1 o Locales.get; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
131 |
val extern = NameSpace.extern o #1 o Locales.get; |
29392 | 132 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
133 |
val get_locale = Symtab.lookup o #2 o Locales.get; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
134 |
val defined = Symtab.defined o #2 o Locales.get; |
29361 | 135 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
136 |
fun the_locale thy name = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
137 |
(case get_locale thy name of |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
138 |
SOME (Loc loc) => loc |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
139 |
| NONE => error ("Unknown locale " ^ quote name)); |
29361 | 140 |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30223
diff
changeset
|
141 |
fun register_locale binding parameters spec intros axioms decls notes dependencies thy = |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
142 |
thy |> Locales.map (NameSpace.define (Sign.naming_of thy) |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
143 |
(binding, |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
144 |
mk_locale ((parameters, spec, intros, axioms), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
145 |
((pairself (map (fn decl => (decl, stamp ()))) decls, map (fn n => (n, stamp ())) notes), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
146 |
map (fn d => (d, stamp ())) dependencies))) #> snd); |
29361 | 147 |
|
29392 | 148 |
fun change_locale name = |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
149 |
Locales.map o apsnd o Symtab.map_entry name o map_locale o apsnd; |
29361 | 150 |
|
151 |
fun print_locales thy = |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
152 |
Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (Locales.get thy))) |
29392 | 153 |
|> Pretty.writeln; |
29361 | 154 |
|
155 |
||
156 |
(*** Primitive operations ***) |
|
157 |
||
29392 | 158 |
fun params_of thy = snd o #parameters o the_locale thy; |
29361 | 159 |
|
29441 | 160 |
fun intros_of thy = #intros o the_locale thy; |
161 |
||
162 |
fun axioms_of thy = #axioms o the_locale thy; |
|
163 |
||
29392 | 164 |
fun instance_of thy name morph = params_of thy name |> |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
165 |
map (Morphism.term morph o Free o #1); |
29361 | 166 |
|
29392 | 167 |
fun specification_of thy = #spec o the_locale thy; |
29361 | 168 |
|
29392 | 169 |
fun declarations_of thy name = the_locale thy name |> |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
170 |
#decls |> pairself (map fst); |
29361 | 171 |
|
29544 | 172 |
fun dependencies_of thy name = the_locale thy name |> |
173 |
#dependencies |> map fst; |
|
174 |
||
29361 | 175 |
|
176 |
(*** Activate context elements of locale ***) |
|
177 |
||
178 |
(** Identifiers: activated locales in theory or proof context **) |
|
179 |
||
180 |
fun ident_eq thy ((n: string, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts); |
|
181 |
||
182 |
local |
|
183 |
||
30754 | 184 |
datatype 'a delayed = Ready of 'a | ToDo of 'a delayed * 'a delayed; |
29361 | 185 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
186 |
structure Identifiers = GenericDataFun |
29361 | 187 |
( |
30754 | 188 |
type T = (string * term list) list delayed; |
189 |
val empty = Ready []; |
|
29361 | 190 |
val extend = I; |
191 |
fun merge _ = ToDo; |
|
192 |
); |
|
193 |
||
194 |
fun finish thy (ToDo (i1, i2)) = merge (ident_eq thy) (finish thy i1, finish thy i2) |
|
195 |
| finish _ (Ready ids) = ids; |
|
196 |
||
197 |
val _ = Context.>> (Context.map_theory (Theory.at_begin (fn thy => |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
198 |
(case Identifiers.get (Context.Theory thy) of |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
199 |
Ready _ => NONE |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
200 |
| ids => SOME (Context.theory_map (Identifiers.put (Ready (finish thy ids))) thy))))); |
29361 | 201 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
202 |
in |
29361 | 203 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
204 |
val get_idents = (fn Ready ids => ids) o Identifiers.get; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
205 |
val put_idents = Identifiers.put o Ready; |
29361 | 206 |
|
207 |
end; |
|
208 |
||
209 |
||
210 |
(** Resolve locale dependencies in a depth-first fashion **) |
|
211 |
||
212 |
local |
|
213 |
||
214 |
val roundup_bound = 120; |
|
215 |
||
216 |
fun add thy depth (name, morph) (deps, marked) = |
|
217 |
if depth > roundup_bound |
|
218 |
then error "Roundup bound exceeded (sublocale relation probably not terminating)." |
|
219 |
else |
|
220 |
let |
|
30754 | 221 |
val dependencies = dependencies_of thy name; |
29361 | 222 |
val instance = instance_of thy name morph; |
223 |
in |
|
224 |
if member (ident_eq thy) marked (name, instance) |
|
225 |
then (deps, marked) |
|
226 |
else |
|
227 |
let |
|
30754 | 228 |
val dependencies' = map (fn (name, morph') => (name, morph' $> morph)) dependencies; |
29361 | 229 |
val marked' = (name, instance) :: marked; |
230 |
val (deps', marked'') = fold_rev (add thy (depth + 1)) dependencies' ([], marked'); |
|
231 |
in |
|
232 |
((name, morph) :: deps' @ deps, marked'') |
|
233 |
end |
|
234 |
end; |
|
235 |
||
236 |
in |
|
237 |
||
238 |
fun roundup thy activate_dep (name, morph) (marked, input) = |
|
239 |
let |
|
240 |
(* Find all dependencies incuding new ones (which are dependencies enriching |
|
241 |
existing registrations). *) |
|
30754 | 242 |
val (dependencies, marked') = add thy 0 (name, morph) ([], []); |
29361 | 243 |
(* Filter out exisiting fragments. *) |
244 |
val dependencies' = filter_out (fn (name, morph) => |
|
245 |
member (ident_eq thy) marked (name, instance_of thy name morph)) dependencies; |
|
246 |
in |
|
30773 | 247 |
(merge (ident_eq thy) (marked, marked'), input |> fold_rev activate_dep dependencies') |
29361 | 248 |
end; |
249 |
||
250 |
end; |
|
251 |
||
252 |
||
253 |
(* Declarations, facts and entire locale content *) |
|
254 |
||
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
255 |
fun activate_decls (name, morph) context = |
29361 | 256 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
257 |
val thy = Context.theory_of context; |
29392 | 258 |
val {decls = (typ_decls, term_decls), ...} = the_locale thy name; |
29361 | 259 |
in |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
260 |
context |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
261 |
|> fold_rev (fn (decl, _) => decl morph) typ_decls |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
262 |
|> fold_rev (fn (decl, _) => decl morph) term_decls |
29361 | 263 |
end; |
264 |
||
265 |
fun activate_notes activ_elem transfer thy (name, morph) input = |
|
266 |
let |
|
29392 | 267 |
val {notes, ...} = the_locale thy name; |
29361 | 268 |
fun activate ((kind, facts), _) input = |
269 |
let |
|
270 |
val facts' = facts |> Element.facts_map (Element.morph_ctxt (transfer input $> morph)) |
|
271 |
in activ_elem (Notes (kind, facts')) input end; |
|
272 |
in |
|
273 |
fold_rev activate notes input |
|
274 |
end; |
|
275 |
||
276 |
fun activate_all name thy activ_elem transfer (marked, input) = |
|
277 |
let |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
278 |
val {parameters = (_, params), spec = (asm, defs), ...} = the_locale thy name; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
279 |
val input' = input |> |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
280 |
(not (null params) ? |
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
281 |
activ_elem (Fixes (map (fn ((x, T), mx) => (Binding.name x, SOME T, mx)) params))) |> |
29361 | 282 |
(* FIXME type parameters *) |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
283 |
(case asm of SOME A => activ_elem (Assumes [(Attrib.empty_binding, [(A, [])])]) | _ => I) |> |
29361 | 284 |
(if not (null defs) |
285 |
then activ_elem (Defines (map (fn def => (Attrib.empty_binding, (def, []))) defs)) |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
286 |
else I); |
30773 | 287 |
val activate = activate_notes activ_elem transfer thy; |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
288 |
in |
30773 | 289 |
roundup thy activate (name, Morphism.identity) (marked, input') |
29361 | 290 |
end; |
291 |
||
292 |
||
293 |
(** Public activation functions **) |
|
294 |
||
30775
71f777103225
added Element.init, which unifies former activate_elem in element.ML and init_elem in locale.ML;
wenzelm
parents:
30773
diff
changeset
|
295 |
fun activate_declarations dep = Context.proof_map (fn context => |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
296 |
let |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
297 |
val thy = Context.theory_of context; |
30773 | 298 |
val context' = roundup thy activate_decls dep (get_idents context, context) |-> put_idents; |
30775
71f777103225
added Element.init, which unifies former activate_elem in element.ML and init_elem in locale.ML;
wenzelm
parents:
30773
diff
changeset
|
299 |
in context' end); |
29361 | 300 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
301 |
fun activate_facts dep context = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
302 |
let |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
303 |
val thy = Context.theory_of context; |
30775
71f777103225
added Element.init, which unifies former activate_elem in element.ML and init_elem in locale.ML;
wenzelm
parents:
30773
diff
changeset
|
304 |
val activate = activate_notes Element.init (Element.transfer_morphism o Context.theory_of) thy; |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
305 |
in roundup thy activate dep (get_idents context, context) |-> put_idents end; |
29361 | 306 |
|
307 |
fun init name thy = |
|
30775
71f777103225
added Element.init, which unifies former activate_elem in element.ML and init_elem in locale.ML;
wenzelm
parents:
30773
diff
changeset
|
308 |
activate_all name thy Element.init (Element.transfer_morphism o Context.theory_of) |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
309 |
([], Context.Proof (ProofContext.init thy)) |-> put_idents |> Context.proof_of; |
29361 | 310 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
311 |
fun print_locale thy show_facts raw_name = |
29361 | 312 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
313 |
val name = intern thy raw_name; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
314 |
val ctxt = init name thy; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
315 |
fun cons_elem (elem as Notes _) = show_facts ? cons elem |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
316 |
| cons_elem elem = cons elem; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
317 |
val elems = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
318 |
activate_all name thy cons_elem (K (Element.transfer_morphism thy)) ([], []) |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
319 |
|> snd |> rev; |
29361 | 320 |
in |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
321 |
Pretty.big_list "locale elements:" (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems) |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
322 |
|> Pretty.writeln |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
323 |
end; |
29361 | 324 |
|
325 |
||
326 |
(*** Registrations: interpretations in theories ***) |
|
327 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
328 |
structure Registrations = TheoryDataFun |
29361 | 329 |
( |
29544 | 330 |
type T = ((string * (morphism * morphism)) * stamp) list; |
29392 | 331 |
(* FIXME mixins need to be stamped *) |
29361 | 332 |
(* registrations, in reverse order of declaration *) |
333 |
val empty = []; |
|
334 |
val extend = I; |
|
29392 | 335 |
val copy = I; |
29361 | 336 |
fun merge _ data : T = Library.merge (eq_snd op =) data; |
337 |
(* FIXME consolidate with dependencies, consider one data slot only *) |
|
338 |
); |
|
339 |
||
31988 | 340 |
fun reg_morph ((name, (base, export)), _) = (name, base $> export); |
341 |
||
342 |
fun these_registrations thy name = Registrations.get thy |
|
343 |
|> filter (curry (op =) name o fst o fst) |
|
344 |
|> map reg_morph; |
|
345 |
||
346 |
fun all_registrations thy = Registrations.get thy |
|
347 |
|> map reg_morph; |
|
29361 | 348 |
|
29392 | 349 |
fun add_registration (name, (base_morph, export)) thy = |
30773 | 350 |
roundup thy (fn (name', morph') => Registrations.map (cons ((name', (morph', export)), stamp ()))) |
31988 | 351 |
(name, base_morph) (get_idents (Context.Theory thy), thy) |
352 |
(* FIXME |-> (Context.theory_map o put_idents_diag)*) |> snd; |
|
29361 | 353 |
|
29392 | 354 |
fun amend_registration morph (name, base_morph) thy = |
29361 | 355 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
356 |
val regs = map #1 (Registrations.get thy); |
29361 | 357 |
val base = instance_of thy name base_morph; |
358 |
fun match (name', (morph', _)) = |
|
359 |
name = name' andalso eq_list (op aconv) (base, instance_of thy name' morph'); |
|
360 |
val i = find_index match (rev regs); |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
361 |
val _ = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
362 |
if i = ~1 then error ("No registration of locale " ^ |
29361 | 363 |
quote (extern thy name) ^ " and parameter instantiation " ^ |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
364 |
space_implode " " (map (quote o Syntax.string_of_term_global thy) base) ^ " available") |
29361 | 365 |
else (); |
366 |
in |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
367 |
Registrations.map (nth_map (length regs - 1 - i) |
29361 | 368 |
(fn ((name, (base, export)), stamp) => ((name, (base $> morph, export)), stamp))) thy |
369 |
end; |
|
370 |
||
32074 | 371 |
fun add_registration_eqs (dep, proto_morph) eqns export thy = |
372 |
let |
|
373 |
val morph = if null eqns then proto_morph |
|
374 |
else proto_morph $> Element.eq_morphism thy eqns; |
|
375 |
in |
|
376 |
thy |
|
377 |
|> add_registration (dep, (morph, export)) |
|
378 |
|> Context.theory_map (activate_facts (dep, morph $> export)) |
|
379 |
end; |
|
380 |
||
29361 | 381 |
|
31988 | 382 |
(*** Dependencies ***) |
383 |
||
32074 | 384 |
fun add_dependency loc (dep, morph) export thy = |
31988 | 385 |
thy |
32074 | 386 |
|> (change_locale loc o apsnd) (cons ((dep, morph $> export), stamp ())) |
31988 | 387 |
|> (fn thy => fold_rev (Context.theory_map o activate_facts) |
388 |
(all_registrations thy) thy); |
|
389 |
||
390 |
||
29361 | 391 |
(*** Storing results ***) |
392 |
||
393 |
(* Theorems *) |
|
394 |
||
395 |
fun add_thmss loc kind args ctxt = |
|
396 |
let |
|
30777
9960ff945c52
simplified Element.activate(_i): singleton version;
wenzelm
parents:
30775
diff
changeset
|
397 |
val (Notes args', ctxt') = Element.activate_i (Notes (kind, args)) ctxt; |
29361 | 398 |
val ctxt'' = ctxt' |> ProofContext.theory ( |
29392 | 399 |
(change_locale loc o apfst o apsnd) (cons (args', stamp ())) |
400 |
#> |
|
29361 | 401 |
(* Registrations *) |
31988 | 402 |
(fn thy => fold_rev (fn (_, morph) => |
29361 | 403 |
let |
404 |
val args'' = snd args' |> Element.facts_map (Element.morph_ctxt morph) |> |
|
405 |
Attrib.map_facts (Attrib.attribute_i thy) |
|
30438
c2d49315b93b
eliminated qualified_names naming policy: qualified names are only permitted via explicit Binding.qualify/qualified_name etc. (NB: user-level outer syntax should never do this);
wenzelm
parents:
30344
diff
changeset
|
406 |
in PureThy.note_thmss kind args'' #> snd end) |
31988 | 407 |
(these_registrations thy loc) thy)) |
29361 | 408 |
in ctxt'' end; |
409 |
||
410 |
||
411 |
(* Declarations *) |
|
412 |
||
413 |
local |
|
414 |
||
415 |
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi)); |
|
416 |
||
417 |
fun add_decls add loc decl = |
|
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
29576
diff
changeset
|
418 |
ProofContext.theory ((change_locale loc o apfst o apfst) (add (decl, stamp ()))) #> |
29361 | 419 |
add_thmss loc Thm.internalK |
420 |
[((Binding.empty, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])]; |
|
421 |
||
422 |
in |
|
423 |
||
424 |
val add_type_syntax = add_decls (apfst o cons); |
|
425 |
val add_term_syntax = add_decls (apsnd o cons); |
|
426 |
val add_declaration = add_decls (K I); |
|
427 |
||
428 |
end; |
|
429 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
430 |
|
29361 | 431 |
(*** Reasoning about locales ***) |
432 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
433 |
(* Storage for witnesses, intro and unfold rules *) |
29361 | 434 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
435 |
structure Thms = GenericDataFun |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
436 |
( |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
437 |
type T = thm list * thm list * thm list; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
438 |
val empty = ([], [], []); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
439 |
val extend = I; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
440 |
fun merge _ ((witnesses1, intros1, unfolds1), (witnesses2, intros2, unfolds2)) = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
441 |
(Thm.merge_thms (witnesses1, witnesses2), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
442 |
Thm.merge_thms (intros1, intros2), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
443 |
Thm.merge_thms (unfolds1, unfolds2)); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
444 |
); |
29361 | 445 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
446 |
val get_witnesses = #1 o Thms.get o Context.Proof; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
447 |
val get_intros = #2 o Thms.get o Context.Proof; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
448 |
val get_unfolds = #3 o Thms.get o Context.Proof; |
29361 | 449 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
450 |
val witness_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
451 |
Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (Thm.add_thm th x, y, z))); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
452 |
val intro_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
453 |
Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (x, Thm.add_thm th y, z))); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
454 |
val unfold_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
455 |
Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (x, y, Thm.add_thm th z))); |
29361 | 456 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
457 |
|
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
458 |
(* Tactic *) |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
459 |
|
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
460 |
fun intro_locales_tac eager ctxt = |
29361 | 461 |
Method.intros_tac |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
462 |
(get_witnesses ctxt @ get_intros ctxt @ (if eager then get_unfolds ctxt else [])); |
29361 | 463 |
|
464 |
val _ = Context.>> (Context.map_theory |
|
30515 | 465 |
(Method.setup (Binding.name "intro_locales") (Scan.succeed (METHOD o intro_locales_tac false)) |
466 |
"back-chain introduction rules of locales without unfolding predicates" #> |
|
467 |
Method.setup (Binding.name "unfold_locales") (Scan.succeed (METHOD o intro_locales_tac true)) |
|
468 |
"back-chain all introduction rules of locales")); |
|
29361 | 469 |
|
470 |
end; |
|
471 |