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