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