author | ballarin |
Mon, 24 Nov 2008 18:02:52 +0100 | |
changeset 28878 | 141ed00656ae |
parent 28872 | 686963dbf6cd |
child 28894 | ff724071b902 |
permissions | -rw-r--r-- |
28795 | 1 |
(* Title: Pure/Isar/expression.ML |
2 |
ID: $Id$ |
|
3 |
Author: Clemens Ballarin, TU Muenchen |
|
4 |
||
5 |
New locale development --- experimental. |
|
6 |
*) |
|
7 |
||
8 |
signature NEW_LOCALE = |
|
9 |
sig |
|
28872 | 10 |
type identifiers |
11 |
val empty: identifiers |
|
12 |
||
28795 | 13 |
type locale |
14 |
||
15 |
val test_locale: theory -> string -> bool |
|
16 |
val register_locale: string -> |
|
17 |
(string * sort) list * (Name.binding * typ option * mixfix) list -> |
|
18 |
term option * term list -> |
|
19 |
(declaration * stamp) list * (declaration * stamp) list -> |
|
20 |
((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list -> |
|
21 |
((string * Morphism.morphism) * stamp) list -> theory -> theory |
|
22 |
||
23 |
(* Locale name space *) |
|
24 |
val intern: theory -> xstring -> string |
|
25 |
val extern: theory -> string -> xstring |
|
26 |
||
27 |
(* Specification *) |
|
28 |
val params_of: theory -> string -> (Name.binding * typ option * mixfix) list |
|
28872 | 29 |
val specification_of: theory -> string -> term option * term list |
30 |
val declarations_of: theory -> string -> declaration list * declaration list |
|
28795 | 31 |
|
28818 | 32 |
(* Storing results *) |
28833 | 33 |
val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list -> |
28818 | 34 |
Proof.context -> Proof.context |
35 |
val add_type_syntax: string -> declaration -> Proof.context -> Proof.context |
|
36 |
val add_term_syntax: string -> declaration -> Proof.context -> Proof.context |
|
37 |
val add_declaration: string -> declaration -> Proof.context -> Proof.context |
|
38 |
||
39 |
(* Activate locales *) |
|
28878 | 40 |
val activate_declarations: theory -> string * Morphism.morphism -> |
41 |
identifiers * Proof.context -> identifiers * Proof.context |
|
42 |
val activate_facts: theory -> string * Morphism.morphism -> |
|
43 |
identifiers * Proof.context -> identifiers * Proof.context |
|
44 |
(* val activate: string -> theory -> (Element.context_i -> 'a -> 'a) -> 'a -> 'a *) |
|
28795 | 45 |
val init: string -> theory -> Proof.context |
46 |
||
47 |
(* Diagnostic *) |
|
48 |
val print_locales: theory -> unit |
|
49 |
val print_locale: theory -> bool -> bstring -> unit |
|
50 |
end; |
|
51 |
||
52 |
||
53 |
structure NewLocale: NEW_LOCALE = |
|
54 |
struct |
|
55 |
||
56 |
datatype ctxt = datatype Element.ctxt; |
|
57 |
||
58 |
||
59 |
(*** Basics ***) |
|
60 |
||
61 |
datatype locale = Loc of { |
|
62 |
(* extensible lists are in reverse order: decls, notes, dependencies *) |
|
63 |
parameters: (string * sort) list * (Name.binding * typ option * mixfix) list, |
|
64 |
(* type and term parameters *) |
|
65 |
spec: term option * term list, |
|
66 |
(* assumptions (as a single predicate expression) and defines *) |
|
67 |
decls: (declaration * stamp) list * (declaration * stamp) list, |
|
68 |
(* type and term syntax declarations *) |
|
69 |
notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list, |
|
70 |
(* theorem declarations *) |
|
71 |
dependencies: ((string * Morphism.morphism) * stamp) list |
|
72 |
(* locale dependencies (sublocale relation) *) |
|
73 |
} |
|
74 |
||
75 |
||
76 |
(*** Theory data ***) |
|
77 |
||
78 |
structure LocalesData = TheoryDataFun |
|
79 |
( |
|
80 |
type T = NameSpace.T * locale Symtab.table; |
|
81 |
(* locale namespace and locales of the theory *) |
|
82 |
||
83 |
val empty = (NameSpace.empty, Symtab.empty); |
|
84 |
val copy = I; |
|
85 |
val extend = I; |
|
86 |
||
87 |
fun join_locales _ |
|
88 |
(Loc {parameters, spec, decls = (decls1, decls2), notes, dependencies}, |
|
89 |
Loc {decls = (decls1', decls2'), notes = notes', |
|
90 |
dependencies = dependencies', ...}) = |
|
91 |
let fun s_merge x = merge (eq_snd (op =)) x in |
|
92 |
Loc {parameters = parameters, |
|
93 |
spec = spec, |
|
94 |
decls = (s_merge (decls1, decls1'), s_merge (decls2, decls2')), |
|
95 |
notes = s_merge (notes, notes'), |
|
96 |
dependencies = s_merge (dependencies, dependencies') |
|
97 |
} |
|
98 |
end; |
|
99 |
fun merge _ ((space1, locs1), (space2, locs2)) = |
|
100 |
(NameSpace.merge (space1, space2), Symtab.join join_locales (locs1, locs2)); |
|
101 |
); |
|
102 |
||
103 |
val intern = NameSpace.intern o #1 o LocalesData.get; |
|
104 |
val extern = NameSpace.extern o #1 o LocalesData.get; |
|
105 |
||
106 |
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy)) name; |
|
107 |
||
108 |
fun the_locale thy name = case get_locale thy name |
|
109 |
of SOME loc => loc |
|
110 |
| NONE => error ("Unknown locale " ^ quote name); |
|
111 |
||
112 |
fun test_locale thy name = case get_locale thy name |
|
113 |
of SOME _ => true | NONE => false; |
|
114 |
||
115 |
fun register_locale name parameters spec decls notes dependencies thy = |
|
116 |
thy |> LocalesData.map (fn (space, locs) => |
|
117 |
(Sign.declare_name thy name space, Symtab.update (name, |
|
118 |
Loc {parameters = parameters, spec = spec, decls = decls, notes = notes, |
|
119 |
dependencies = dependencies}) locs)); |
|
120 |
||
121 |
fun change_locale name f thy = |
|
122 |
let |
|
123 |
val Loc {parameters, spec, decls, notes, dependencies} = |
|
124 |
the_locale thy name; |
|
125 |
val (parameters', spec', decls', notes', dependencies') = |
|
126 |
f (parameters, spec, decls, notes, dependencies); |
|
127 |
in |
|
128 |
thy |
|
129 |
|> (LocalesData.map o apsnd) (Symtab.update (name, Loc {parameters = parameters', |
|
130 |
spec = spec', decls = decls', notes = notes', dependencies = dependencies'})) |
|
131 |
end; |
|
132 |
||
133 |
fun print_locales thy = |
|
134 |
let val (space, locs) = LocalesData.get thy in |
|
135 |
Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (space, locs))) |
|
136 |
|> Pretty.writeln |
|
137 |
end; |
|
138 |
||
139 |
||
140 |
(*** Primitive operations ***) |
|
141 |
||
142 |
fun params_of thy name = |
|
143 |
let |
|
144 |
val Loc {parameters = (_, params), ...} = the_locale thy name |
|
145 |
in params end; |
|
146 |
||
28872 | 147 |
fun specification_of thy name = |
28795 | 148 |
let |
28872 | 149 |
val Loc {spec, ...} = the_locale thy name |
150 |
in spec end; |
|
151 |
||
152 |
fun declarations_of thy name = |
|
153 |
let |
|
154 |
val Loc {decls, ...} = the_locale thy name |
|
28795 | 155 |
in |
156 |
decls |> apfst (map fst) |> apsnd (map fst) |
|
157 |
end; |
|
158 |
||
159 |
||
28818 | 160 |
(*** Activate context elements of locale ***) |
28795 | 161 |
|
28818 | 162 |
(* Resolve locale dependencies in a depth-first fashion *) |
28795 | 163 |
|
164 |
local |
|
165 |
||
166 |
structure Idtab = TableFun(type key = string * term list |
|
167 |
val ord = prod_ord string_ord (list_ord Term.fast_term_ord)); |
|
168 |
||
169 |
in |
|
170 |
||
28872 | 171 |
type identifiers = unit Idtab.table |
172 |
||
173 |
val empty = (Idtab.empty : identifiers); |
|
174 |
||
175 |
fun roundup thy (deps, marked) = |
|
176 |
(* FIXME simplify code: should probably only get one dep as argument *) |
|
28795 | 177 |
let |
178 |
fun add (name, morph) (deps, marked) = |
|
179 |
let |
|
180 |
val Loc {parameters = (_, params), dependencies, ...} = the_locale thy name; |
|
181 |
val instance = params |> |
|
182 |
map ((fn (b, T, _) => Free (Name.name_of b, the T)) #> Morphism.term morph); |
|
183 |
in |
|
184 |
if Idtab.defined marked (name, instance) |
|
185 |
then (deps, marked) |
|
186 |
else |
|
187 |
let |
|
188 |
val dependencies' = |
|
189 |
map (fn ((name, morph'), _) => (name, morph' $> morph)) dependencies; |
|
190 |
val marked' = Idtab.insert (op =) ((name, instance), ()) marked; |
|
191 |
val (deps', marked'') = fold_rev add dependencies' ([], marked'); |
|
192 |
in |
|
193 |
(cons (name, morph) deps' @ deps, marked'') |
|
194 |
end |
|
195 |
end |
|
28872 | 196 |
in fold_rev add deps ([], marked) end; |
28795 | 197 |
|
198 |
end; |
|
199 |
||
200 |
||
28818 | 201 |
fun activate_decls thy (name, morph) ctxt = |
202 |
let |
|
203 |
val Loc {decls = (typ_decls, term_decls), ...} = the_locale thy name; |
|
204 |
in |
|
205 |
ctxt |> fold_rev (fn (decl, _) => Context.proof_map (decl morph)) typ_decls |> |
|
206 |
fold_rev (fn (decl, _) => Context.proof_map (decl morph)) term_decls |
|
207 |
end; |
|
28795 | 208 |
|
28878 | 209 |
fun activate_declarations thy (name, morph) (marked, ctxt) = |
28795 | 210 |
let |
28818 | 211 |
val name' = intern thy name; |
28872 | 212 |
val Loc {dependencies, ...} = the_locale thy name'; |
213 |
val (dependencies', marked) = |
|
28878 | 214 |
roundup thy ((name', morph) :: |
215 |
map (fn ((n, morph'), _) => (n, morph' $> morph)) dependencies, marked); |
|
28818 | 216 |
in |
28872 | 217 |
(marked, ctxt |> fold_rev (activate_decls thy) dependencies') |
28818 | 218 |
end; |
219 |
||
220 |
fun activate_notes activ_elem thy (name, morph) input = |
|
221 |
let |
|
222 |
val Loc {notes, ...} = the_locale thy name; |
|
223 |
fun activate ((kind, facts), _) input = |
|
28795 | 224 |
let |
28818 | 225 |
val facts' = facts |> Element.facts_map (Element.morph_ctxt morph) |
226 |
in activ_elem (Notes (kind, facts')) input end; |
|
28795 | 227 |
in |
28818 | 228 |
fold_rev activate notes input |
28795 | 229 |
end; |
230 |
||
28878 | 231 |
fun activate_facts_intern thy activ_elem (name, morph) (marked, ctxt) = |
232 |
let |
|
233 |
val name' = intern thy name; |
|
234 |
val Loc {dependencies, ...} = the_locale thy name'; |
|
235 |
val (dependencies', marked) = |
|
236 |
roundup thy ((name', morph) :: |
|
237 |
map (fn ((n, morph'), _) => (n, morph' $> morph)) dependencies, marked); |
|
238 |
in |
|
239 |
(marked, ctxt |> fold_rev (activate_notes activ_elem thy) dependencies') |
|
240 |
end; |
|
241 |
||
28818 | 242 |
fun activate name thy activ_elem input = |
28795 | 243 |
let |
28818 | 244 |
val name' = intern thy name; |
28795 | 245 |
val Loc {parameters = (_, params), spec = (asm, defs), dependencies, ...} = |
28818 | 246 |
the_locale thy name'; |
28795 | 247 |
val dependencies' = |
28872 | 248 |
(name', Morphism.identity) :: fst (roundup thy (map fst dependencies, empty)); |
28795 | 249 |
in |
28818 | 250 |
input |> |
28851
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
251 |
(if not (null params) then activ_elem (Fixes params) else I) |> |
28795 | 252 |
(* FIXME type parameters *) |
28851
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
253 |
(if is_some asm then activ_elem (Assumes [(Attrib.no_binding, [(the asm, [])])]) else I) |> |
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
254 |
(if not (null defs) |
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
255 |
then activ_elem (Defines (map (fn def => (Attrib.no_binding, (def, []))) defs)) |
368aca388dd9
Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents:
28833
diff
changeset
|
256 |
else I) |> |
28878 | 257 |
pair empty |> activate_facts_intern thy activ_elem (name', Morphism.identity) |> snd |
28795 | 258 |
end; |
259 |
||
28818 | 260 |
local |
261 |
||
262 |
fun init_elem (Fixes fixes) ctxt = ctxt |> |
|
263 |
ProofContext.add_fixes_i fixes |> snd |
|
264 |
| init_elem (Assumes assms) ctxt = |
|
265 |
let |
|
266 |
val assms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) assms |
|
267 |
in |
|
268 |
ctxt |> fold Variable.auto_fixes (maps (map fst o snd) assms') |> |
|
269 |
ProofContext.add_assms_i Assumption.assume_export assms' |> snd |
|
270 |
end |
|
271 |
| init_elem (Defines defs) ctxt = |
|
272 |
let |
|
273 |
val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs |
|
274 |
in |
|
275 |
ctxt |> fold Variable.auto_fixes (map (fst o snd) defs') |> |
|
276 |
ProofContext.add_assms_i LocalDefs.def_export (map (fn (attn, t) => (attn, [t])) defs') |> |
|
277 |
snd |
|
278 |
end |
|
279 |
| init_elem (Notes (kind, facts)) ctxt = |
|
280 |
let |
|
281 |
val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts |
|
28865 | 282 |
in fold (fn args => Locale.local_note_qualified kind args #> snd) facts' ctxt end |
28818 | 283 |
|
284 |
fun cons_elem false (Notes notes) elems = elems |
|
285 |
| cons_elem _ elem elems = elem :: elems |
|
286 |
||
287 |
in |
|
288 |
||
28878 | 289 |
fun activate_facts thy dep = activate_facts_intern thy init_elem dep; |
290 |
||
28818 | 291 |
fun init name thy = activate name thy init_elem (ProofContext.init thy); |
292 |
||
28795 | 293 |
fun print_locale thy show_facts name = |
28818 | 294 |
let val ctxt = init name thy |
28795 | 295 |
in |
296 |
Pretty.big_list "locale elements:" |
|
28818 | 297 |
(activate name thy (cons_elem show_facts) [] |> rev |> |
298 |
map (Element.pretty_ctxt ctxt) |> map Pretty.chunks) |> Pretty.writeln |
|
299 |
end |
|
28795 | 300 |
|
301 |
end; |
|
302 |
||
303 |
||
28818 | 304 |
(*** Storing results ***) |
305 |
||
28833 | 306 |
(* Theorems *) |
307 |
||
308 |
fun add_thmss loc kind args ctxt = |
|
309 |
let |
|
310 |
val (([Notes args'], _), ctxt') = Element.activate_i [Notes (kind, args)] ctxt; |
|
311 |
val ctxt'' = ctxt' |> ProofContext.theory |
|
312 |
(change_locale loc |
|
313 |
(fn (parameters, spec, decls, notes, dependencies) => |
|
314 |
(parameters, spec, decls, (args', stamp ()) :: notes, dependencies))) |
|
315 |
(* FIXME registrations *) |
|
316 |
in ctxt'' end; |
|
317 |
||
318 |
||
28818 | 319 |
(* Declarations *) |
320 |
||
321 |
local |
|
322 |
||
323 |
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi)); |
|
324 |
||
325 |
fun add_decls add loc decl = |
|
326 |
ProofContext.theory (change_locale loc |
|
327 |
(fn (parameters, spec, decls, notes, dependencies) => |
|
28833 | 328 |
(parameters, spec, add (decl, stamp ()) decls, notes, dependencies))) #> |
28818 | 329 |
add_thmss loc Thm.internalK |
330 |
[((Name.no_binding, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])]; |
|
331 |
||
332 |
in |
|
333 |
||
334 |
val add_type_syntax = add_decls (apfst o cons); |
|
335 |
val add_term_syntax = add_decls (apsnd o cons); |
|
336 |
val add_declaration = add_decls (K I); |
|
337 |
||
338 |
end; |
|
339 |
||
340 |
end; |
|
341 |