author | haftmann |
Mon, 20 Jul 2009 16:49:05 +0200 | |
changeset 32113 | bafffa63ebfd |
parent 32074 | 76d6ba08a05f |
child 32800 | 57fcca4e7c0e |
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 *) |
32113 | 71 |
val add_registration_eqs: string * morphism -> thm list -> morphism -> theory -> theory |
30725
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_dependency: string -> string * morphism -> morphism -> theory -> theory |
29361 | 74 |
|
75 |
(* Diagnostic *) |
|
76 |
val print_locales: theory -> unit |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30223
diff
changeset
|
77 |
val print_locale: theory -> bool -> xstring -> unit |
29361 | 78 |
end; |
79 |
||
80 |
structure Locale: LOCALE = |
|
81 |
struct |
|
82 |
||
83 |
datatype ctxt = datatype Element.ctxt; |
|
84 |
||
29392 | 85 |
|
86 |
(*** Theory data ***) |
|
29361 | 87 |
|
88 |
datatype locale = Loc of { |
|
29392 | 89 |
(** static part **) |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
90 |
parameters: (string * sort) list * ((string * typ) * mixfix) list, |
29361 | 91 |
(* type and term parameters *) |
92 |
spec: term option * term list, |
|
93 |
(* assumptions (as a single predicate expression) and defines *) |
|
29441 | 94 |
intros: thm option * thm option, |
95 |
axioms: thm list, |
|
29392 | 96 |
(** dynamic part **) |
29361 | 97 |
decls: (declaration * stamp) list * (declaration * stamp) list, |
98 |
(* type and term syntax declarations *) |
|
99 |
notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list, |
|
100 |
(* theorem declarations *) |
|
29544 | 101 |
dependencies: ((string * morphism) * stamp) list |
29361 | 102 |
(* locale dependencies (sublocale relation) *) |
29392 | 103 |
}; |
29361 | 104 |
|
29441 | 105 |
fun mk_locale ((parameters, spec, intros, axioms), ((decls, notes), dependencies)) = |
106 |
Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec, |
|
107 |
decls = decls, notes = notes, dependencies = dependencies}; |
|
30754 | 108 |
|
29441 | 109 |
fun map_locale f (Loc {parameters, spec, intros, axioms, decls, notes, dependencies}) = |
110 |
mk_locale (f ((parameters, spec, intros, axioms), ((decls, notes), dependencies))); |
|
30754 | 111 |
|
29441 | 112 |
fun merge_locale (Loc {parameters, spec, intros, axioms, decls = (decls1, decls2), |
113 |
notes, dependencies}, Loc {decls = (decls1', decls2'), notes = notes', |
|
114 |
dependencies = dependencies', ...}) = mk_locale |
|
115 |
((parameters, spec, intros, axioms), |
|
116 |
(((merge (eq_snd op =) (decls1, decls1'), merge (eq_snd op =) (decls2, decls2')), |
|
117 |
merge (eq_snd op =) (notes, notes')), |
|
118 |
merge (eq_snd op =) (dependencies, dependencies'))); |
|
29361 | 119 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
120 |
structure Locales = TheoryDataFun |
29361 | 121 |
( |
29392 | 122 |
type T = locale NameSpace.table; |
29361 | 123 |
val empty = NameSpace.empty_table; |
124 |
val copy = I; |
|
125 |
val extend = I; |
|
29392 | 126 |
fun merge _ = NameSpace.join_tables (K merge_locale); |
29361 | 127 |
); |
128 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
129 |
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
|
130 |
val extern = NameSpace.extern o #1 o Locales.get; |
29392 | 131 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
132 |
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
|
133 |
val defined = Symtab.defined o #2 o Locales.get; |
29361 | 134 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
135 |
fun the_locale thy name = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
136 |
(case get_locale thy name of |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
137 |
SOME (Loc loc) => loc |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
138 |
| NONE => error ("Unknown locale " ^ quote name)); |
29361 | 139 |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30223
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
(binding, |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
143 |
mk_locale ((parameters, spec, intros, axioms), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
144 |
((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
|
145 |
map (fn d => (d, stamp ())) dependencies))) #> snd); |
29361 | 146 |
|
29392 | 147 |
fun change_locale name = |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
148 |
Locales.map o apsnd o Symtab.map_entry name o map_locale o apsnd; |
29361 | 149 |
|
150 |
fun print_locales thy = |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
151 |
Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (Locales.get thy))) |
29392 | 152 |
|> Pretty.writeln; |
29361 | 153 |
|
154 |
||
155 |
(*** Primitive operations ***) |
|
156 |
||
29392 | 157 |
fun params_of thy = snd o #parameters o the_locale thy; |
29361 | 158 |
|
29441 | 159 |
fun intros_of thy = #intros o the_locale thy; |
160 |
||
161 |
fun axioms_of thy = #axioms o the_locale thy; |
|
162 |
||
29392 | 163 |
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
|
164 |
map (Morphism.term morph o Free o #1); |
29361 | 165 |
|
29392 | 166 |
fun specification_of thy = #spec o the_locale thy; |
29361 | 167 |
|
29392 | 168 |
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
|
169 |
#decls |> pairself (map fst); |
29361 | 170 |
|
29544 | 171 |
fun dependencies_of thy name = the_locale thy name |> |
172 |
#dependencies |> map fst; |
|
173 |
||
29361 | 174 |
|
175 |
(*** Activate context elements of locale ***) |
|
176 |
||
177 |
(** Identifiers: activated locales in theory or proof context **) |
|
178 |
||
179 |
fun ident_eq thy ((n: string, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts); |
|
180 |
||
181 |
local |
|
182 |
||
30754 | 183 |
datatype 'a delayed = Ready of 'a | ToDo of 'a delayed * 'a delayed; |
29361 | 184 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
185 |
structure Identifiers = GenericDataFun |
29361 | 186 |
( |
30754 | 187 |
type T = (string * term list) list delayed; |
188 |
val empty = Ready []; |
|
29361 | 189 |
val extend = I; |
190 |
fun merge _ = ToDo; |
|
191 |
); |
|
192 |
||
193 |
fun finish thy (ToDo (i1, i2)) = merge (ident_eq thy) (finish thy i1, finish thy i2) |
|
194 |
| finish _ (Ready ids) = ids; |
|
195 |
||
196 |
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
|
197 |
(case Identifiers.get (Context.Theory thy) of |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
198 |
Ready _ => NONE |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
199 |
| ids => SOME (Context.theory_map (Identifiers.put (Ready (finish thy ids))) thy))))); |
29361 | 200 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
201 |
in |
29361 | 202 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
203 |
val get_idents = (fn Ready ids => ids) o Identifiers.get; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
204 |
val put_idents = Identifiers.put o Ready; |
29361 | 205 |
|
206 |
end; |
|
207 |
||
208 |
||
209 |
(** Resolve locale dependencies in a depth-first fashion **) |
|
210 |
||
211 |
local |
|
212 |
||
213 |
val roundup_bound = 120; |
|
214 |
||
215 |
fun add thy depth (name, morph) (deps, marked) = |
|
216 |
if depth > roundup_bound |
|
217 |
then error "Roundup bound exceeded (sublocale relation probably not terminating)." |
|
218 |
else |
|
219 |
let |
|
30754 | 220 |
val dependencies = dependencies_of thy name; |
29361 | 221 |
val instance = instance_of thy name morph; |
222 |
in |
|
223 |
if member (ident_eq thy) marked (name, instance) |
|
224 |
then (deps, marked) |
|
225 |
else |
|
226 |
let |
|
30754 | 227 |
val dependencies' = map (fn (name, morph') => (name, morph' $> morph)) dependencies; |
29361 | 228 |
val marked' = (name, instance) :: marked; |
229 |
val (deps', marked'') = fold_rev (add thy (depth + 1)) dependencies' ([], marked'); |
|
230 |
in |
|
231 |
((name, morph) :: deps' @ deps, marked'') |
|
232 |
end |
|
233 |
end; |
|
234 |
||
235 |
in |
|
236 |
||
237 |
fun roundup thy activate_dep (name, morph) (marked, input) = |
|
238 |
let |
|
239 |
(* Find all dependencies incuding new ones (which are dependencies enriching |
|
240 |
existing registrations). *) |
|
30754 | 241 |
val (dependencies, marked') = add thy 0 (name, morph) ([], []); |
29361 | 242 |
(* Filter out exisiting fragments. *) |
243 |
val dependencies' = filter_out (fn (name, morph) => |
|
244 |
member (ident_eq thy) marked (name, instance_of thy name morph)) dependencies; |
|
245 |
in |
|
30773 | 246 |
(merge (ident_eq thy) (marked, marked'), input |> fold_rev activate_dep dependencies') |
29361 | 247 |
end; |
248 |
||
249 |
end; |
|
250 |
||
251 |
||
252 |
(* Declarations, facts and entire locale content *) |
|
253 |
||
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
254 |
fun activate_decls (name, morph) context = |
29361 | 255 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
256 |
val thy = Context.theory_of context; |
29392 | 257 |
val {decls = (typ_decls, term_decls), ...} = the_locale thy name; |
29361 | 258 |
in |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
259 |
context |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
260 |
|> fold_rev (fn (decl, _) => decl morph) typ_decls |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
261 |
|> fold_rev (fn (decl, _) => decl morph) term_decls |
29361 | 262 |
end; |
263 |
||
264 |
fun activate_notes activ_elem transfer thy (name, morph) input = |
|
265 |
let |
|
29392 | 266 |
val {notes, ...} = the_locale thy name; |
29361 | 267 |
fun activate ((kind, facts), _) input = |
268 |
let |
|
269 |
val facts' = facts |> Element.facts_map (Element.morph_ctxt (transfer input $> morph)) |
|
270 |
in activ_elem (Notes (kind, facts')) input end; |
|
271 |
in |
|
272 |
fold_rev activate notes input |
|
273 |
end; |
|
274 |
||
275 |
fun activate_all name thy activ_elem transfer (marked, input) = |
|
276 |
let |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
277 |
val {parameters = (_, params), spec = (asm, defs), ...} = the_locale thy name; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
278 |
val input' = input |> |
30755
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
279 |
(not (null params) ? |
7ef503d216c2
simplified internal locale parameters: maintain proper name and type, instead of binding and constraint;
wenzelm
parents:
30754
diff
changeset
|
280 |
activ_elem (Fixes (map (fn ((x, T), mx) => (Binding.name x, SOME T, mx)) params))) |> |
29361 | 281 |
(* FIXME type parameters *) |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
282 |
(case asm of SOME A => activ_elem (Assumes [(Attrib.empty_binding, [(A, [])])]) | _ => I) |> |
29361 | 283 |
(if not (null defs) |
284 |
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
|
285 |
else I); |
30773 | 286 |
val activate = activate_notes activ_elem transfer thy; |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
287 |
in |
30773 | 288 |
roundup thy activate (name, Morphism.identity) (marked, input') |
29361 | 289 |
end; |
290 |
||
291 |
||
292 |
(** Public activation functions **) |
|
293 |
||
30775
71f777103225
added Element.init, which unifies former activate_elem in element.ML and init_elem in locale.ML;
wenzelm
parents:
30773
diff
changeset
|
294 |
fun activate_declarations dep = Context.proof_map (fn context => |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
295 |
let |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
296 |
val thy = Context.theory_of context; |
32113 | 297 |
in roundup thy activate_decls dep (get_idents context, context) |-> put_idents end); |
29361 | 298 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
299 |
fun activate_facts dep context = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
300 |
let |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
301 |
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
|
302 |
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
|
303 |
in roundup thy activate dep (get_idents context, context) |-> put_idents end; |
29361 | 304 |
|
305 |
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
|
306 |
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
|
307 |
([], Context.Proof (ProofContext.init thy)) |-> put_idents |> Context.proof_of; |
29361 | 308 |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
309 |
fun print_locale thy show_facts raw_name = |
29361 | 310 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
311 |
val name = intern thy raw_name; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
312 |
val ctxt = init name thy; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
313 |
fun cons_elem (elem as Notes _) = show_facts ? cons elem |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
314 |
| cons_elem elem = cons elem; |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
315 |
val elems = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
316 |
activate_all name thy cons_elem (K (Element.transfer_morphism thy)) ([], []) |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
317 |
|> snd |> rev; |
29361 | 318 |
in |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
319 |
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
|
320 |
|> Pretty.writeln |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
321 |
end; |
29361 | 322 |
|
323 |
||
324 |
(*** Registrations: interpretations in theories ***) |
|
325 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
326 |
structure Registrations = TheoryDataFun |
29361 | 327 |
( |
29544 | 328 |
type T = ((string * (morphism * morphism)) * stamp) list; |
29392 | 329 |
(* FIXME mixins need to be stamped *) |
29361 | 330 |
(* registrations, in reverse order of declaration *) |
331 |
val empty = []; |
|
332 |
val extend = I; |
|
29392 | 333 |
val copy = I; |
29361 | 334 |
fun merge _ data : T = Library.merge (eq_snd op =) data; |
335 |
(* FIXME consolidate with dependencies, consider one data slot only *) |
|
336 |
); |
|
337 |
||
31988 | 338 |
fun reg_morph ((name, (base, export)), _) = (name, base $> export); |
339 |
||
340 |
fun these_registrations thy name = Registrations.get thy |
|
341 |
|> filter (curry (op =) name o fst o fst) |
|
342 |
|> map reg_morph; |
|
343 |
||
344 |
fun all_registrations thy = Registrations.get thy |
|
345 |
|> map reg_morph; |
|
29361 | 346 |
|
29392 | 347 |
fun amend_registration morph (name, base_morph) thy = |
29361 | 348 |
let |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
349 |
val regs = map #1 (Registrations.get thy); |
29361 | 350 |
val base = instance_of thy name base_morph; |
351 |
fun match (name', (morph', _)) = |
|
352 |
name = name' andalso eq_list (op aconv) (base, instance_of thy name' morph'); |
|
353 |
val i = find_index match (rev regs); |
|
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
354 |
val _ = |
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
355 |
if i = ~1 then error ("No registration of locale " ^ |
29361 | 356 |
quote (extern thy name) ^ " and parameter instantiation " ^ |
30764
3e3e7aa0cc7a
simplified Locale.activate operations, using generic context;
wenzelm
parents:
30763
diff
changeset
|
357 |
space_implode " " (map (quote o Syntax.string_of_term_global thy) base) ^ " available") |
29361 | 358 |
else (); |
359 |
in |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
360 |
Registrations.map (nth_map (length regs - 1 - i) |
29361 | 361 |
(fn ((name, (base, export)), stamp) => ((name, (base $> morph, export)), stamp))) thy |
362 |
end; |
|
363 |
||
32074 | 364 |
fun add_registration_eqs (dep, proto_morph) eqns export thy = |
365 |
let |
|
366 |
val morph = if null eqns then proto_morph |
|
367 |
else proto_morph $> Element.eq_morphism thy eqns; |
|
368 |
in |
|
32113 | 369 |
(get_idents (Context.Theory thy), thy) |
370 |
|> roundup thy (fn (dep', morph') => |
|
371 |
Registrations.map (cons ((dep', (morph', export)), stamp ()))) (dep, morph) |
|
372 |
|> snd |
|
32074 | 373 |
|> Context.theory_map (activate_facts (dep, morph $> export)) |
374 |
end; |
|
375 |
||
29361 | 376 |
|
31988 | 377 |
(*** Dependencies ***) |
378 |
||
32074 | 379 |
fun add_dependency loc (dep, morph) export thy = |
31988 | 380 |
thy |
32074 | 381 |
|> (change_locale loc o apsnd) (cons ((dep, morph $> export), stamp ())) |
31988 | 382 |
|> (fn thy => fold_rev (Context.theory_map o activate_facts) |
383 |
(all_registrations thy) thy); |
|
384 |
||
385 |
||
29361 | 386 |
(*** Storing results ***) |
387 |
||
388 |
(* Theorems *) |
|
389 |
||
390 |
fun add_thmss loc kind args ctxt = |
|
391 |
let |
|
30777
9960ff945c52
simplified Element.activate(_i): singleton version;
wenzelm
parents:
30775
diff
changeset
|
392 |
val (Notes args', ctxt') = Element.activate_i (Notes (kind, args)) ctxt; |
29361 | 393 |
val ctxt'' = ctxt' |> ProofContext.theory ( |
29392 | 394 |
(change_locale loc o apfst o apsnd) (cons (args', stamp ())) |
395 |
#> |
|
29361 | 396 |
(* Registrations *) |
31988 | 397 |
(fn thy => fold_rev (fn (_, morph) => |
29361 | 398 |
let |
399 |
val args'' = snd args' |> Element.facts_map (Element.morph_ctxt morph) |> |
|
400 |
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
|
401 |
in PureThy.note_thmss kind args'' #> snd end) |
31988 | 402 |
(these_registrations thy loc) thy)) |
29361 | 403 |
in ctxt'' end; |
404 |
||
405 |
||
406 |
(* Declarations *) |
|
407 |
||
408 |
local |
|
409 |
||
410 |
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi)); |
|
411 |
||
412 |
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
|
413 |
ProofContext.theory ((change_locale loc o apfst o apfst) (add (decl, stamp ()))) #> |
29361 | 414 |
add_thmss loc Thm.internalK |
415 |
[((Binding.empty, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])]; |
|
416 |
||
417 |
in |
|
418 |
||
419 |
val add_type_syntax = add_decls (apfst o cons); |
|
420 |
val add_term_syntax = add_decls (apsnd o cons); |
|
421 |
val add_declaration = add_decls (K I); |
|
422 |
||
423 |
end; |
|
424 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
425 |
|
29361 | 426 |
(*** Reasoning about locales ***) |
427 |
||
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
428 |
(* Storage for witnesses, intro and unfold rules *) |
29361 | 429 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
430 |
structure Thms = GenericDataFun |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
431 |
( |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
432 |
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
|
433 |
val empty = ([], [], []); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
434 |
val extend = I; |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
435 |
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
|
436 |
(Thm.merge_thms (witnesses1, witnesses2), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
437 |
Thm.merge_thms (intros1, intros2), |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
438 |
Thm.merge_thms (unfolds1, unfolds2)); |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
439 |
); |
29361 | 440 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
441 |
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
|
442 |
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
|
443 |
val get_unfolds = #3 o Thms.get o Context.Proof; |
29361 | 444 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
445 |
val witness_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
446 |
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
|
447 |
val intro_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
448 |
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
|
449 |
val unfold_add = |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
450 |
Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (x, y, Thm.add_thm th z))); |
29361 | 451 |
|
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
452 |
|
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
453 |
(* Tactic *) |
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
454 |
|
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
455 |
fun intro_locales_tac eager ctxt = |
29361 | 456 |
Method.intros_tac |
30725
c23a5b3cd1b9
register_locale: produce stamps at the spot where elements are registered;
wenzelm
parents:
30585
diff
changeset
|
457 |
(get_witnesses ctxt @ get_intros ctxt @ (if eager then get_unfolds ctxt else [])); |
29361 | 458 |
|
459 |
val _ = Context.>> (Context.map_theory |
|
30515 | 460 |
(Method.setup (Binding.name "intro_locales") (Scan.succeed (METHOD o intro_locales_tac false)) |
461 |
"back-chain introduction rules of locales without unfolding predicates" #> |
|
462 |
Method.setup (Binding.name "unfold_locales") (Scan.succeed (METHOD o intro_locales_tac true)) |
|
463 |
"back-chain all introduction rules of locales")); |
|
29361 | 464 |
|
465 |
end; |
|
466 |