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