author | ballarin |
Tue, 16 Dec 2008 20:18:46 +0100 | |
changeset 29218 | f7ffe90879e2 |
parent 29217 | a1c992fb3184 |
child 29220 | db37b657a326 |
permissions | -rw-r--r-- |
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
1 |
(* Title: Pure/Isar/new_locale.ML |
28795 | 2 |
Author: Clemens Ballarin, TU Muenchen |
3 |
||
4 |
New locale development --- experimental. |
|
5 |
*) |
|
6 |
||
7 |
signature NEW_LOCALE = |
|
8 |
sig |
|
9 |
type locale |
|
10 |
||
11 |
val test_locale: theory -> string -> bool |
|
28991 | 12 |
val register_locale: bstring -> |
28965 | 13 |
(string * sort) list * (Binding.T * typ option * mixfix) list -> |
28795 | 14 |
term option * term list -> |
15 |
(declaration * stamp) list * (declaration * stamp) list -> |
|
16 |
((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list -> |
|
17 |
((string * Morphism.morphism) * stamp) list -> theory -> theory |
|
18 |
||
19 |
(* Locale name space *) |
|
20 |
val intern: theory -> xstring -> string |
|
21 |
val extern: theory -> string -> xstring |
|
22 |
||
23 |
(* Specification *) |
|
28965 | 24 |
val params_of: theory -> string -> (Binding.T * typ option * mixfix) list |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
25 |
val instance_of: theory -> string -> Morphism.morphism -> term list |
28872 | 26 |
val specification_of: theory -> string -> term option * term list |
27 |
val declarations_of: theory -> string -> declaration list * declaration list |
|
28795 | 28 |
|
28818 | 29 |
(* Storing results *) |
28833 | 30 |
val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list -> |
28818 | 31 |
Proof.context -> Proof.context |
32 |
val add_type_syntax: string -> declaration -> Proof.context -> Proof.context |
|
33 |
val add_term_syntax: string -> declaration -> Proof.context -> Proof.context |
|
34 |
val add_declaration: string -> declaration -> Proof.context -> Proof.context |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
35 |
val add_dependency: string -> (string * Morphism.morphism) -> theory -> theory |
28818 | 36 |
|
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
37 |
(* Activation *) |
28878 | 38 |
val activate_declarations: theory -> string * Morphism.morphism -> |
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
39 |
Proof.context -> Proof.context |
29018 | 40 |
val activate_global_facts: string * Morphism.morphism -> theory -> theory |
41 |
val activate_local_facts: string * Morphism.morphism -> Proof.context -> Proof.context |
|
28795 | 42 |
val init: string -> theory -> Proof.context |
43 |
||
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
44 |
(* Reasoning about locales *) |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
45 |
val witness_attrib: attribute |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
46 |
val intro_attrib: attribute |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
47 |
val unfold_attrib: attribute |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
48 |
val intro_locales_tac: bool -> Proof.context -> thm list -> tactic |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
49 |
|
29206 | 50 |
(* Registrations *) |
29210
4025459e3f83
Interpretation in theories: first version with equations.
ballarin
parents:
29206
diff
changeset
|
51 |
val add_global_registration: (string * (Morphism.morphism * Morphism.morphism)) -> |
4025459e3f83
Interpretation in theories: first version with equations.
ballarin
parents:
29206
diff
changeset
|
52 |
theory -> theory |
29211 | 53 |
val amend_global_registration: Morphism.morphism -> (string * Morphism.morphism) -> |
29210
4025459e3f83
Interpretation in theories: first version with equations.
ballarin
parents:
29206
diff
changeset
|
54 |
theory -> theory |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
55 |
val get_global_registrations: theory -> (string * Morphism.morphism) list |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
56 |
|
28795 | 57 |
(* Diagnostic *) |
58 |
val print_locales: theory -> unit |
|
59 |
val print_locale: theory -> bool -> bstring -> unit |
|
60 |
end; |
|
61 |
||
62 |
||
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
63 |
(*** Theorem list extensible via attribute --- to control intro_locales_tac ***) |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
64 |
|
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
65 |
(* FIXME: consider moving named_thms.ML up in the build hierarchy and using that *) |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
66 |
functor ThmsFun() = |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
67 |
struct |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
68 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
69 |
structure Data = GenericDataFun |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
70 |
( |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
71 |
type T = thm list; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
72 |
val empty = []; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
73 |
val extend = I; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
74 |
fun merge _ = Thm.merge_thms; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
75 |
); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
76 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
77 |
val get = Data.get o Context.Proof; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
78 |
val add = Thm.declaration_attribute (Data.map o Thm.add_thm); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
79 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
80 |
end; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
81 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
82 |
|
28795 | 83 |
structure NewLocale: NEW_LOCALE = |
84 |
struct |
|
85 |
||
86 |
datatype ctxt = datatype Element.ctxt; |
|
87 |
||
88 |
||
89 |
(*** Basics ***) |
|
90 |
||
91 |
datatype locale = Loc of { |
|
92 |
(* extensible lists are in reverse order: decls, notes, dependencies *) |
|
28965 | 93 |
parameters: (string * sort) list * (Binding.T * typ option * mixfix) list, |
28795 | 94 |
(* type and term parameters *) |
95 |
spec: term option * term list, |
|
96 |
(* assumptions (as a single predicate expression) and defines *) |
|
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 *) |
|
101 |
dependencies: ((string * Morphism.morphism) * stamp) list |
|
102 |
(* locale dependencies (sublocale relation) *) |
|
103 |
} |
|
104 |
||
105 |
||
106 |
(*** Theory data ***) |
|
107 |
||
108 |
structure LocalesData = TheoryDataFun |
|
109 |
( |
|
110 |
type T = NameSpace.T * locale Symtab.table; |
|
111 |
(* locale namespace and locales of the theory *) |
|
112 |
||
28991 | 113 |
val empty = NameSpace.empty_table; |
28795 | 114 |
val copy = I; |
115 |
val extend = I; |
|
116 |
||
117 |
fun join_locales _ |
|
118 |
(Loc {parameters, spec, decls = (decls1, decls2), notes, dependencies}, |
|
119 |
Loc {decls = (decls1', decls2'), notes = notes', |
|
120 |
dependencies = dependencies', ...}) = |
|
121 |
let fun s_merge x = merge (eq_snd (op =)) x in |
|
122 |
Loc {parameters = parameters, |
|
123 |
spec = spec, |
|
124 |
decls = (s_merge (decls1, decls1'), s_merge (decls2, decls2')), |
|
125 |
notes = s_merge (notes, notes'), |
|
126 |
dependencies = s_merge (dependencies, dependencies') |
|
127 |
} |
|
128 |
end; |
|
28991 | 129 |
fun merge _ = NameSpace.join_tables join_locales; |
28795 | 130 |
); |
131 |
||
132 |
val intern = NameSpace.intern o #1 o LocalesData.get; |
|
133 |
val extern = NameSpace.extern o #1 o LocalesData.get; |
|
134 |
||
135 |
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy)) name; |
|
136 |
||
137 |
fun the_locale thy name = case get_locale thy name |
|
138 |
of SOME loc => loc |
|
139 |
| NONE => error ("Unknown locale " ^ quote name); |
|
140 |
||
141 |
fun test_locale thy name = case get_locale thy name |
|
142 |
of SOME _ => true | NONE => false; |
|
143 |
||
28991 | 144 |
fun register_locale bname parameters spec decls notes dependencies thy = |
145 |
thy |> LocalesData.map (NameSpace.bind (Sign.naming_of thy) (Binding.name bname, |
|
146 |
Loc {parameters = parameters, spec = spec, decls = decls, notes = notes, |
|
147 |
dependencies = dependencies}) #> snd); |
|
28795 | 148 |
|
149 |
fun change_locale name f thy = |
|
150 |
let |
|
151 |
val Loc {parameters, spec, decls, notes, dependencies} = |
|
152 |
the_locale thy name; |
|
153 |
val (parameters', spec', decls', notes', dependencies') = |
|
154 |
f (parameters, spec, decls, notes, dependencies); |
|
155 |
in |
|
156 |
thy |
|
157 |
|> (LocalesData.map o apsnd) (Symtab.update (name, Loc {parameters = parameters', |
|
158 |
spec = spec', decls = decls', notes = notes', dependencies = dependencies'})) |
|
159 |
end; |
|
160 |
||
161 |
fun print_locales thy = |
|
162 |
let val (space, locs) = LocalesData.get thy in |
|
163 |
Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (space, locs))) |
|
164 |
|> Pretty.writeln |
|
165 |
end; |
|
166 |
||
167 |
||
168 |
(*** Primitive operations ***) |
|
169 |
||
170 |
fun params_of thy name = |
|
171 |
let |
|
172 |
val Loc {parameters = (_, params), ...} = the_locale thy name |
|
173 |
in params end; |
|
174 |
||
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
175 |
fun instance_of thy name morph = |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
176 |
params_of thy name |> |
29006 | 177 |
map ((fn (b, T, _) => Free (Binding.base_name b, the T)) #> Morphism.term morph); |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
178 |
|
28872 | 179 |
fun specification_of thy name = |
28795 | 180 |
let |
28872 | 181 |
val Loc {spec, ...} = the_locale thy name |
182 |
in spec end; |
|
183 |
||
184 |
fun declarations_of thy name = |
|
185 |
let |
|
186 |
val Loc {decls, ...} = the_locale thy name |
|
28795 | 187 |
in |
188 |
decls |> apfst (map fst) |> apsnd (map fst) |
|
189 |
end; |
|
190 |
||
191 |
||
28818 | 192 |
(*** Activate context elements of locale ***) |
28795 | 193 |
|
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
194 |
(** Identifiers: activated locales in theory or proof context **) |
28795 | 195 |
|
28899
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
196 |
type identifiers = (string * term list) list; |
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
197 |
|
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
198 |
val empty = ([] : identifiers); |
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
199 |
|
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
200 |
fun ident_eq thy ((n, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts); |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
201 |
|
28795 | 202 |
local |
203 |
||
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
204 |
datatype 'a delayed = Ready of 'a | ToDo of ('a delayed * 'a delayed); |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
205 |
|
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
206 |
structure IdentifiersData = GenericDataFun |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
207 |
( |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
208 |
type T = identifiers delayed; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
209 |
val empty = Ready empty; |
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
210 |
val extend = I; |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
211 |
fun merge _ = ToDo; |
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
212 |
); |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
213 |
|
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
214 |
in |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
215 |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
216 |
fun finish thy (ToDo (i1, i2)) = merge (ident_eq thy) (finish thy i1, finish thy i2) |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
217 |
| finish _ (Ready ids) = ids; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
218 |
|
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
219 |
val _ = Context.>> (Context.map_theory (Theory.at_begin (fn thy => |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
220 |
(case IdentifiersData.get (Context.Theory thy) of |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
221 |
Ready _ => NONE | |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
222 |
ids => SOME (Context.theory_map (IdentifiersData.put (Ready (finish thy ids))) thy)) |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
223 |
))); |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
224 |
|
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
225 |
fun get_global_idents thy = |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
226 |
let val (Ready ids) = (IdentifiersData.get o Context.Theory) thy in ids end; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
227 |
val put_global_idents = Context.theory_map o IdentifiersData.put o Ready; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
228 |
|
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
229 |
fun get_local_idents ctxt = |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
230 |
let val (Ready ids) = (IdentifiersData.get o Context.Proof) ctxt in ids end; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
231 |
val put_local_idents = Context.proof_map o IdentifiersData.put o Ready; |
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
232 |
|
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
233 |
end; |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
234 |
|
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
235 |
|
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
236 |
(** Resolve locale dependencies in a depth-first fashion **) |
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
237 |
|
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
238 |
local |
28872 | 239 |
|
28897 | 240 |
val roundup_bound = 120; |
241 |
||
242 |
fun add thy depth (name, morph) (deps, marked) = |
|
243 |
if depth > roundup_bound |
|
244 |
then error "Roundup bound exceeded (sublocale relation probably not terminating)." |
|
245 |
else |
|
246 |
let |
|
247 |
val Loc {parameters = (_, params), dependencies, ...} = the_locale thy name; |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
248 |
val instance = instance_of thy name morph; |
28897 | 249 |
in |
28899
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
250 |
if member (ident_eq thy) marked (name, instance) |
28897 | 251 |
then (deps, marked) |
252 |
else |
|
253 |
let |
|
254 |
val dependencies' = |
|
255 |
map (fn ((name, morph'), _) => (name, morph' $> morph)) dependencies; |
|
28899
7bf5d7f154b8
Perform higher-order pattern matching during round-up.
ballarin
parents:
28897
diff
changeset
|
256 |
val marked' = (name, instance) :: marked; |
28897 | 257 |
val (deps', marked'') = fold_rev (add thy (depth + 1)) dependencies' ([], marked'); |
258 |
in |
|
259 |
((name, morph) :: deps' @ deps, marked'') |
|
260 |
end |
|
261 |
end; |
|
262 |
||
263 |
in |
|
264 |
||
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
265 |
fun roundup thy activate_dep (name, morph) (marked, input) = |
28795 | 266 |
let |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
267 |
(* Find all dependencies incuding new ones (which are dependencies enriching |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
268 |
existing registrations). *) |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
269 |
val (dependencies, marked') = add thy 0 (name, morph) ([], empty); |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
270 |
(* Filter out exisiting fragments. *) |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
271 |
val dependencies' = filter_out (fn (name, morph) => |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
272 |
member (ident_eq thy) marked (name, instance_of thy name morph)) dependencies; |
28894
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
273 |
in |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
274 |
(merge (ident_eq thy) (marked, marked'), input |> fold_rev (activate_dep thy) dependencies') |
28894
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
275 |
end; |
28795 | 276 |
|
277 |
end; |
|
278 |
||
28897 | 279 |
|
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
280 |
(* Declarations, facts and entire locale content *) |
28795 | 281 |
|
28818 | 282 |
fun activate_decls thy (name, morph) ctxt = |
283 |
let |
|
284 |
val Loc {decls = (typ_decls, term_decls), ...} = the_locale thy name; |
|
285 |
in |
|
286 |
ctxt |> fold_rev (fn (decl, _) => Context.proof_map (decl morph)) typ_decls |> |
|
287 |
fold_rev (fn (decl, _) => Context.proof_map (decl morph)) term_decls |
|
288 |
end; |
|
28795 | 289 |
|
29216 | 290 |
fun activate_notes activ_elem transfer thy (name, morph) input = |
28818 | 291 |
let |
292 |
val Loc {notes, ...} = the_locale thy name; |
|
293 |
fun activate ((kind, facts), _) input = |
|
28795 | 294 |
let |
29218 | 295 |
val facts' = facts |> Element.facts_map (Element.morph_ctxt (transfer input $> morph)) |
28818 | 296 |
in activ_elem (Notes (kind, facts')) input end; |
28795 | 297 |
in |
28818 | 298 |
fold_rev activate notes input |
28795 | 299 |
end; |
300 |
||
29216 | 301 |
fun activate_all name thy activ_elem transfer (marked, input) = |
28795 | 302 |
let |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
303 |
val Loc {parameters = (_, params), spec = (asm, defs), ...} = |
28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28899
diff
changeset
|
304 |
the_locale thy name; |
28795 | 305 |
in |
28818 | 306 |
input |> |
28851
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
307 |
(if not (null params) then activ_elem (Fixes params) else I) |> |
28795 | 308 |
(* FIXME type parameters *) |
28965 | 309 |
(if is_some asm then activ_elem (Assumes [(Attrib.empty_binding, [(the asm, [])])]) else I) |> |
28851
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
310 |
(if not (null defs) |
28965 | 311 |
then activ_elem (Defines (map (fn def => (Attrib.empty_binding, (def, []))) defs)) |
28851
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
312 |
else I) |> |
29216 | 313 |
pair marked |> roundup thy (activate_notes activ_elem transfer) (name, Morphism.identity) |
28795 | 314 |
end; |
315 |
||
28897 | 316 |
|
317 |
(** Public activation functions **) |
|
318 |
||
28818 | 319 |
local |
320 |
||
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
321 |
fun init_global_elem (Notes (kind, facts)) thy = |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
322 |
let |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
323 |
val facts' = Attrib.map_facts (Attrib.attribute_i thy) facts |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
324 |
in Locale.global_note_qualified kind facts' thy |> snd end |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
325 |
|
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
326 |
fun init_local_elem (Fixes fixes) ctxt = ctxt |> |
28818 | 327 |
ProofContext.add_fixes_i fixes |> snd |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
328 |
| init_local_elem (Assumes assms) ctxt = |
28818 | 329 |
let |
330 |
val assms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) assms |
|
331 |
in |
|
332 |
ctxt |> fold Variable.auto_fixes (maps (map fst o snd) assms') |> |
|
333 |
ProofContext.add_assms_i Assumption.assume_export assms' |> snd |
|
334 |
end |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
335 |
| init_local_elem (Defines defs) ctxt = |
28818 | 336 |
let |
337 |
val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs |
|
338 |
in |
|
339 |
ctxt |> fold Variable.auto_fixes (map (fst o snd) defs') |> |
|
340 |
ProofContext.add_assms_i LocalDefs.def_export (map (fn (attn, t) => (attn, [t])) defs') |> |
|
341 |
snd |
|
342 |
end |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
343 |
| init_local_elem (Notes (kind, facts)) ctxt = |
28818 | 344 |
let |
345 |
val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts |
|
28940 | 346 |
in Locale.local_note_qualified kind facts' ctxt |> snd end |
28818 | 347 |
|
348 |
fun cons_elem false (Notes notes) elems = elems |
|
349 |
| cons_elem _ elem elems = elem :: elems |
|
350 |
||
351 |
in |
|
352 |
||
28951
e89dde5f365c
Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents:
28940
diff
changeset
|
353 |
fun activate_declarations thy dep ctxt = |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
354 |
roundup thy activate_decls dep (get_local_idents ctxt, ctxt) |> uncurry put_local_idents; |
29019 | 355 |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
356 |
fun activate_global_facts dep thy = |
29218 | 357 |
roundup thy (activate_notes init_global_elem Element.transfer_morphism) |
358 |
dep (get_global_idents thy, thy) |> |
|
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
359 |
uncurry put_global_idents; |
28897 | 360 |
|
29018 | 361 |
fun activate_local_facts dep ctxt = |
29218 | 362 |
roundup (ProofContext.theory_of ctxt) |
363 |
(activate_notes init_local_elem (Element.transfer_morphism o ProofContext.theory_of)) dep |
|
29018 | 364 |
(get_local_idents ctxt, ctxt) |> |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
365 |
uncurry put_local_idents; |
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
366 |
|
29216 | 367 |
fun init name thy = |
29218 | 368 |
activate_all name thy init_local_elem (Element.transfer_morphism o ProofContext.theory_of) |
29216 | 369 |
(empty, ProofContext.init thy) |> |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
370 |
uncurry put_local_idents; |
28818 | 371 |
|
28795 | 372 |
fun print_locale thy show_facts name = |
28902
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28899
diff
changeset
|
373 |
let |
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28899
diff
changeset
|
374 |
val name' = intern thy name; |
2019bcc9d8bf
Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents:
28899
diff
changeset
|
375 |
val ctxt = init name' thy |
28795 | 376 |
in |
377 |
Pretty.big_list "locale elements:" |
|
29218 | 378 |
(activate_all name' thy (cons_elem show_facts) (K Morphism.identity) (empty, []) |> |
379 |
snd |> rev |> |
|
28818 | 380 |
map (Element.pretty_ctxt ctxt) |> map Pretty.chunks) |> Pretty.writeln |
381 |
end |
|
28795 | 382 |
|
383 |
end; |
|
384 |
||
385 |
||
29213 | 386 |
(*** Registrations: interpretations in theories ***) |
387 |
||
388 |
(* FIXME only global variant needed *) |
|
389 |
structure RegistrationsData = GenericDataFun |
|
390 |
( |
|
391 |
type T = ((string * (Morphism.morphism * Morphism.morphism)) * stamp) list; |
|
392 |
(* FIXME mixins need to be stamped *) |
|
393 |
(* registrations, in reverse order of declaration *) |
|
394 |
val empty = []; |
|
395 |
val extend = I; |
|
396 |
fun merge _ = Library.merge (eq_snd (op =)); |
|
397 |
(* FIXME consolidate with dependencies, consider one data slot only *) |
|
398 |
); |
|
399 |
||
400 |
val get_global_registrations = |
|
401 |
Context.Theory #> RegistrationsData.get #> map fst #> map (apsnd op $>); |
|
402 |
fun add_global_registration reg = |
|
403 |
(Context.theory_map o RegistrationsData.map) (cons (reg, stamp ())); |
|
404 |
||
405 |
fun amend_global_registration morph (name, base_morph) thy = |
|
406 |
let |
|
407 |
val regs = (Context.Theory #> RegistrationsData.get #> map fst) thy; |
|
408 |
val base = instance_of thy name base_morph; |
|
409 |
fun match (name', (morph', _)) = |
|
410 |
name = name' andalso eq_list (op aconv) (base, instance_of thy name' morph'); |
|
411 |
val i = find_index match (rev regs); |
|
412 |
val _ = if i = ~1 then error ("No interpretation of locale " ^ |
|
413 |
quote (extern thy name) ^ " and parameter instantiation " ^ |
|
414 |
space_implode " " (map (quote o Syntax.string_of_term_global thy) base) ^ " available.") |
|
415 |
else (); |
|
416 |
in |
|
417 |
(Context.theory_map o RegistrationsData.map) (nth_map (length regs - 1 - i) |
|
418 |
(fn ((name, (base, export)), stamp) => ((name, (base $> morph, export)), stamp))) thy |
|
419 |
end; |
|
420 |
||
421 |
||
28818 | 422 |
(*** Storing results ***) |
423 |
||
28833 | 424 |
(* Theorems *) |
425 |
||
426 |
fun add_thmss loc kind args ctxt = |
|
427 |
let |
|
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29216
diff
changeset
|
428 |
val ([Notes args'], ctxt') = Element.activate_i [Notes (kind, args)] ctxt; |
29213 | 429 |
val ctxt'' = ctxt' |> ProofContext.theory ( |
430 |
change_locale loc |
|
28833 | 431 |
(fn (parameters, spec, decls, notes, dependencies) => |
29213 | 432 |
(parameters, spec, decls, (args', stamp ()) :: notes, dependencies)) #> |
433 |
(* Registrations *) |
|
434 |
(fn thy => fold_rev (fn (name, morph) => |
|
435 |
let |
|
436 |
val args'' = snd args' |> Element.facts_map (Element.morph_ctxt morph) |> |
|
437 |
Attrib.map_facts (Attrib.attribute_i thy) |
|
438 |
in Locale.global_note_qualified kind args'' #> snd end) |
|
439 |
(get_global_registrations thy |> filter (fn (name, _) => name = loc)) thy)) |
|
28833 | 440 |
in ctxt'' end; |
441 |
||
442 |
||
28818 | 443 |
(* Declarations *) |
444 |
||
445 |
local |
|
446 |
||
447 |
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi)); |
|
448 |
||
449 |
fun add_decls add loc decl = |
|
450 |
ProofContext.theory (change_locale loc |
|
451 |
(fn (parameters, spec, decls, notes, dependencies) => |
|
28833 | 452 |
(parameters, spec, add (decl, stamp ()) decls, notes, dependencies))) #> |
28818 | 453 |
add_thmss loc Thm.internalK |
28965 | 454 |
[((Binding.empty, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])]; |
28818 | 455 |
|
456 |
in |
|
457 |
||
458 |
val add_type_syntax = add_decls (apfst o cons); |
|
459 |
val add_term_syntax = add_decls (apsnd o cons); |
|
460 |
val add_declaration = add_decls (K I); |
|
461 |
||
462 |
end; |
|
463 |
||
28894
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
464 |
(* Dependencies *) |
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
465 |
|
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
466 |
fun add_dependency loc dep = |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
467 |
change_locale loc |
28894
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
468 |
(fn (parameters, spec, decls, notes, dependencies) => |
28993
829e684b02ef
Interpretation in theories including interaction with subclass relation.
ballarin
parents:
28951
diff
changeset
|
469 |
(parameters, spec, decls, notes, (dep, stamp ()) :: dependencies)); |
28894
ff724071b902
Command to add dependencies, fixed processing of dependencies.
ballarin
parents:
28878
diff
changeset
|
470 |
|
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
471 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
472 |
(*** Reasoning about locales ***) |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
473 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
474 |
(** Storage for witnesses, intro and unfold rules **) |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
475 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
476 |
structure Witnesses = ThmsFun(); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
477 |
structure Intros = ThmsFun(); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
478 |
structure Unfolds = ThmsFun(); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
479 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
480 |
val witness_attrib = Witnesses.add; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
481 |
val intro_attrib = Intros.add; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
482 |
val unfold_attrib = Unfolds.add; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
483 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
484 |
(** Tactic **) |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
485 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
486 |
fun intro_locales_tac eager ctxt facts st = |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
487 |
Method.intros_tac |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
488 |
(Witnesses.get ctxt @ Intros.get ctxt @ (if eager then Unfolds.get ctxt else [])) facts st; |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
489 |
|
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
490 |
val _ = Context.>> (Context.map_theory |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
491 |
(Method.add_methods |
28927
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
492 |
[("intro_locales", |
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
493 |
Method.ctxt_args (fn ctxt => Method.METHOD (intro_locales_tac false ctxt ORELSE' |
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
494 |
Locale.intro_locales_tac false ctxt)), |
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
495 |
"back-chain introduction rules of locales without unfolding predicates"), |
28927
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
496 |
("unfold_locales", |
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
497 |
Method.ctxt_args (fn ctxt => Method.METHOD (intro_locales_tac true ctxt ORELSE' |
7e631979922f
Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents:
28903
diff
changeset
|
498 |
Locale.intro_locales_tac true ctxt)), |
28903
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
499 |
"back-chain all introduction rules of locales")])); |
b3fc3a62247a
Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents:
28902
diff
changeset
|
500 |
|
28818 | 501 |
end; |
502 |