24218
|
1 |
(* Title: Pure/Isar/class.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Type classes derived from primitive axclasses and locales.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature CLASS =
|
|
9 |
sig
|
|
10 |
val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix
|
|
11 |
|
|
12 |
val axclass_cmd: bstring * xstring list
|
|
13 |
-> ((bstring * Attrib.src list) * string list) list -> theory -> class * theory
|
|
14 |
val class: bstring -> class list -> Element.context_i Locale.element list
|
|
15 |
-> string list -> theory -> string * Proof.context
|
|
16 |
val class_cmd: bstring -> string list -> Element.context Locale.element list
|
|
17 |
-> string list -> theory -> string * Proof.context
|
|
18 |
val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list
|
|
19 |
-> theory -> Proof.state
|
|
20 |
val instance_arity_cmd: (bstring * string list * string) list
|
|
21 |
-> ((bstring * Attrib.src list) * string) list
|
|
22 |
-> theory -> Proof.state
|
|
23 |
val prove_instance_arity: tactic -> arity list
|
|
24 |
-> ((bstring * Attrib.src list) * term) list
|
|
25 |
-> theory -> theory
|
|
26 |
val instance_class: class * class -> theory -> Proof.state
|
|
27 |
val instance_class_cmd: string * string -> theory -> Proof.state
|
|
28 |
val instance_sort: class * sort -> theory -> Proof.state
|
|
29 |
val instance_sort_cmd: string * string -> theory -> Proof.state
|
|
30 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory
|
|
31 |
|
|
32 |
val class_of_locale: theory -> string -> class option
|
|
33 |
val add_const_in_class: string -> (string * term) * Syntax.mixfix
|
|
34 |
-> theory -> theory
|
|
35 |
|
24304
|
36 |
val unoverload: theory -> thm -> thm
|
|
37 |
val overload: theory -> thm -> thm
|
|
38 |
val inst_const: theory -> string * string -> string
|
|
39 |
|
24218
|
40 |
val print_classes: theory -> unit
|
|
41 |
val intro_classes_tac: thm list -> tactic
|
|
42 |
val default_intro_classes_tac: thm list -> tactic
|
|
43 |
end;
|
|
44 |
|
|
45 |
structure Class : CLASS =
|
|
46 |
struct
|
|
47 |
|
|
48 |
(** auxiliary **)
|
|
49 |
|
|
50 |
fun fork_mixfix is_loc some_class mx =
|
|
51 |
let
|
|
52 |
val mx' = Syntax.unlocalize_mixfix mx;
|
|
53 |
val mx_global = if is_some some_class orelse (is_loc andalso mx = mx')
|
|
54 |
then NoSyn else mx';
|
|
55 |
val mx_local = if is_loc then mx else NoSyn;
|
|
56 |
in (mx_global, mx_local) end;
|
|
57 |
|
|
58 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
|
|
59 |
let
|
|
60 |
val ctxt = ProofContext.init thy;
|
|
61 |
val superclasses = map (Sign.read_class thy) raw_superclasses;
|
|
62 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) raw_specs;
|
|
63 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) raw_specs)
|
|
64 |
|> snd
|
|
65 |
|> (map o map) fst;
|
|
66 |
in AxClass.define_class (class, superclasses) [] (name_atts ~~ axiomss) thy end;
|
|
67 |
|
|
68 |
|
|
69 |
(** axclasses with implicit parameter handling **)
|
|
70 |
|
|
71 |
(* axclass instances *)
|
|
72 |
|
|
73 |
local
|
|
74 |
|
|
75 |
fun gen_instance mk_prop add_thm after_qed insts thy =
|
|
76 |
let
|
|
77 |
fun after_qed' results =
|
|
78 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed);
|
|
79 |
in
|
|
80 |
thy
|
|
81 |
|> ProofContext.init
|
|
82 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts)
|
|
83 |
end;
|
|
84 |
|
|
85 |
in
|
|
86 |
|
|
87 |
val axclass_instance_arity =
|
|
88 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
|
|
89 |
val axclass_instance_sort =
|
|
90 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel))
|
|
91 |
AxClass.add_classrel I o single;
|
|
92 |
|
|
93 |
end; (*local*)
|
|
94 |
|
|
95 |
|
|
96 |
(* introducing axclasses with implicit parameter handling *)
|
|
97 |
|
|
98 |
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy =
|
|
99 |
let
|
|
100 |
val superclasses = map (Sign.certify_class thy) raw_superclasses;
|
|
101 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
|
|
102 |
val prefix = Logic.const_of_class name;
|
|
103 |
fun mk_const_name c = NameSpace.map_base (NameSpace.append prefix)
|
|
104 |
(Sign.full_name thy c);
|
|
105 |
fun add_const ((c, ty), syn) =
|
|
106 |
Sign.add_consts_authentic [(c, ty, syn)]
|
|
107 |
#> pair (mk_const_name c, ty);
|
|
108 |
fun mk_axioms cs thy =
|
|
109 |
raw_dep_axioms thy cs
|
|
110 |
|> (map o apsnd o map) (Sign.cert_prop thy)
|
|
111 |
|> rpair thy;
|
|
112 |
fun add_constraint class (c, ty) =
|
|
113 |
Sign.add_const_constraint_i (c, SOME
|
|
114 |
(Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
|
|
115 |
in
|
|
116 |
thy
|
|
117 |
|> Theory.add_path prefix
|
|
118 |
|> fold_map add_const consts
|
|
119 |
||> Theory.restore_naming thy
|
|
120 |
|-> (fn cs => mk_axioms cs
|
|
121 |
#-> (fn axioms_prop => AxClass.define_class (name, superclasses)
|
|
122 |
(map fst cs @ other_consts) axioms_prop
|
|
123 |
#-> (fn class => `(fn thy => AxClass.get_definition thy class)
|
|
124 |
#-> (fn {intro, axioms, ...} => fold (add_constraint class) cs
|
|
125 |
#> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs))))))
|
|
126 |
end;
|
|
127 |
|
|
128 |
|
24304
|
129 |
(* explicit constants for overloaded definitions *)
|
|
130 |
|
|
131 |
structure InstData = TheoryDataFun
|
|
132 |
(
|
|
133 |
type T = (string * thm) Symtab.table Symtab.table;
|
|
134 |
(*constant name ~> type constructor ~> (constant name, equation)*)
|
|
135 |
val empty = Symtab.empty;
|
|
136 |
val copy = I;
|
|
137 |
val extend = I;
|
|
138 |
fun merge _ = Symtab.join (K (Symtab.merge (K true)));
|
|
139 |
);
|
|
140 |
|
|
141 |
fun inst_thms f thy =
|
|
142 |
Symtab.fold (Symtab.fold (cons o f o snd o snd) o snd) (InstData.get thy) [];
|
|
143 |
fun add_inst (c, tyco) inst = (InstData.map o Symtab.map_default (c, Symtab.empty))
|
|
144 |
(Symtab.update_new (tyco, inst));
|
|
145 |
|
|
146 |
fun unoverload thy thm = MetaSimplifier.rewrite_rule (inst_thms I thy) thm;
|
|
147 |
fun overload thy thm = MetaSimplifier.rewrite_rule (inst_thms symmetric thy) thm;
|
|
148 |
|
|
149 |
fun inst_const thy (c, tyco) =
|
|
150 |
(fst o the o Symtab.lookup ((the o Symtab.lookup (InstData.get thy)) c)) tyco;
|
|
151 |
|
|
152 |
fun add_inst_def (class, tyco) (c, ty) thy =
|
|
153 |
let
|
|
154 |
val tyco_base = NameSpace.base tyco;
|
|
155 |
val name_inst = NameSpace.base class ^ "_" ^ tyco_base ^ "_inst";
|
|
156 |
val c_inst_base = NameSpace.base c ^ "_" ^ tyco_base;
|
|
157 |
in
|
|
158 |
thy
|
|
159 |
|> Sign.sticky_prefix name_inst
|
|
160 |
|> Sign.add_consts_i [(c_inst_base, ty, Syntax.NoSyn)]
|
|
161 |
|> `(fn thy => Sign.full_name thy c_inst_base)
|
|
162 |
|-> (fn c_inst => PureThy.add_defs_i true
|
|
163 |
[((Thm.def_name c_inst_base, Logic.mk_equals (Const (c_inst, ty), Const (c, ty))), [])]
|
|
164 |
#-> (fn [def] => add_inst (c, tyco) (c_inst, symmetric def))
|
|
165 |
#> Sign.restore_naming thy)
|
|
166 |
end;
|
|
167 |
|
|
168 |
fun add_inst_def' (class, tyco) (c, ty) thy =
|
|
169 |
if case Symtab.lookup (InstData.get thy) c
|
|
170 |
of NONE => true
|
|
171 |
| SOME tab => is_none (Symtab.lookup tab tyco)
|
|
172 |
then add_inst_def (class, tyco) (c, Logic.unvarifyT ty) thy
|
|
173 |
else thy;
|
|
174 |
|
|
175 |
fun add_def ((class, tyco), ((name, prop), atts)) thy =
|
|
176 |
let
|
|
177 |
val ((lhs as Const (c, ty), args), rhs) = (apfst Term.strip_comb o Logic.dest_equals) prop;
|
|
178 |
fun add_inst' def ([], (Const (c_inst, ty))) =
|
|
179 |
if forall (fn TFree_ => true | _ => false) (Sign.const_typargs thy (c_inst, ty))
|
|
180 |
then add_inst (c, tyco) (c_inst, def)
|
|
181 |
else add_inst_def (class, tyco) (c, ty)
|
|
182 |
| add_inst' _ t = add_inst_def (class, tyco) (c, ty);
|
|
183 |
in
|
|
184 |
thy
|
|
185 |
|> PureThy.add_defs_i true [((name, prop), map (Attrib.attribute thy) atts)]
|
|
186 |
|-> (fn [def] => add_inst' def (args, rhs) #> pair def)
|
|
187 |
end;
|
|
188 |
|
|
189 |
|
24218
|
190 |
(* instances with implicit parameter handling *)
|
|
191 |
|
|
192 |
local
|
|
193 |
|
|
194 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) =
|
|
195 |
let
|
|
196 |
val (_, t) = read_def thy (raw_name, raw_t);
|
|
197 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t;
|
|
198 |
val atts = map (prep_att thy) raw_atts;
|
|
199 |
val insts = Consts.typargs (Sign.consts_of thy) (c, ty);
|
|
200 |
val name = case raw_name
|
|
201 |
of "" => NONE
|
|
202 |
| _ => SOME raw_name;
|
|
203 |
in (c, (insts, ((name, t), atts))) end;
|
|
204 |
|
|
205 |
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Theory.read_axm;
|
|
206 |
fun read_def thy = gen_read_def thy (K I) (K I);
|
|
207 |
|
|
208 |
fun gen_instance_arity prep_arity read_def do_proof raw_arities raw_defs theory =
|
|
209 |
let
|
|
210 |
val arities = map (prep_arity theory) raw_arities;
|
|
211 |
val _ = if null arities then error "at least one arity must be given" else ();
|
|
212 |
val _ = case (duplicates (op =) o map #1) arities
|
|
213 |
of [] => ()
|
|
214 |
| dupl_tycos => error ("type constructors occur more than once in arities: "
|
|
215 |
^ (commas o map quote) dupl_tycos);
|
|
216 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory
|
|
217 |
fun get_consts_class tyco ty class =
|
|
218 |
let
|
|
219 |
val cs = (these o Option.map snd o try (AxClass.params_of_class theory)) class;
|
|
220 |
val subst_ty = map_type_tfree (K ty);
|
|
221 |
in
|
24304
|
222 |
map (fn (c, ty) => (c, ((class, tyco), subst_ty ty))) cs
|
24218
|
223 |
end;
|
|
224 |
fun get_consts_sort (tyco, asorts, sort) =
|
|
225 |
let
|
|
226 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts))
|
|
227 |
in maps (get_consts_class tyco ty) (super_sort sort) end;
|
|
228 |
val cs = maps get_consts_sort arities;
|
|
229 |
fun mk_typnorm thy (ty, ty_sc) =
|
|
230 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
|
|
231 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
|
|
232 |
| NONE => NONE;
|
|
233 |
fun read_defs defs cs thy_read =
|
|
234 |
let
|
|
235 |
fun read raw_def cs =
|
|
236 |
let
|
|
237 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
|
|
238 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
|
24304
|
239 |
val ((class, tyco), ty') = case AList.lookup (op =) cs c
|
24218
|
240 |
of NONE => error ("illegal definition for constant " ^ quote c)
|
|
241 |
| SOME class_ty => class_ty;
|
|
242 |
val name = case name_opt
|
|
243 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
|
|
244 |
| SOME name => name;
|
|
245 |
val t' = case mk_typnorm thy_read (ty', ty)
|
|
246 |
of NONE => error ("illegal definition for constant " ^
|
|
247 |
quote (c ^ "::" ^ setmp show_sorts true
|
|
248 |
(Sign.string_of_typ thy_read) ty))
|
|
249 |
| SOME norm => map_types norm t
|
|
250 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
|
|
251 |
in fold_map read defs cs end;
|
24304
|
252 |
val (defs, other_cs) = read_defs raw_defs cs
|
24218
|
253 |
(fold Sign.primitive_arity arities (Theory.copy theory));
|
|
254 |
fun get_remove_contraint c thy =
|
|
255 |
let
|
|
256 |
val ty = Sign.the_const_constraint thy c;
|
|
257 |
in
|
|
258 |
thy
|
|
259 |
|> Sign.add_const_constraint_i (c, NONE)
|
|
260 |
|> pair (c, Logic.unvarifyT ty)
|
|
261 |
end;
|
24304
|
262 |
fun after_qed cs defs =
|
|
263 |
fold Sign.add_const_constraint_i (map (apsnd SOME) cs)
|
|
264 |
#> fold (Code.add_func false) defs;
|
24218
|
265 |
in
|
|
266 |
theory
|
|
267 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =))
|
24304
|
268 |
||>> fold_map add_def defs
|
|
269 |
||> fold (fn (c, ((class, tyco), ty)) => add_inst_def' (class, tyco) (c, ty)) other_cs
|
24218
|
270 |
|-> (fn (cs, defs) => do_proof (after_qed cs defs) arities)
|
|
271 |
end;
|
|
272 |
|
|
273 |
fun instance_arity_cmd' do_proof = gen_instance_arity Sign.read_arity read_def_cmd do_proof;
|
|
274 |
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof;
|
|
275 |
fun tactic_proof tac after_qed arities =
|
|
276 |
fold (fn arity => AxClass.prove_arity arity tac) arities
|
|
277 |
#> after_qed;
|
|
278 |
|
|
279 |
in
|
|
280 |
|
|
281 |
val instance_arity_cmd = instance_arity_cmd' axclass_instance_arity;
|
|
282 |
val instance_arity = instance_arity' axclass_instance_arity;
|
|
283 |
val prove_instance_arity = instance_arity' o tactic_proof;
|
|
284 |
|
|
285 |
end; (*local*)
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
(** combining locales and axclasses **)
|
|
290 |
|
|
291 |
(* theory data *)
|
|
292 |
|
|
293 |
datatype class_data = ClassData of {
|
|
294 |
locale: string,
|
|
295 |
consts: (string * string) list
|
|
296 |
(*locale parameter ~> toplevel theory constant*),
|
|
297 |
v: string option,
|
|
298 |
intro: thm
|
|
299 |
} * thm list (*derived defs*);
|
|
300 |
|
|
301 |
fun rep_classdata (ClassData c) = c;
|
|
302 |
|
|
303 |
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2));
|
|
304 |
|
|
305 |
structure ClassData = TheoryDataFun
|
|
306 |
(
|
|
307 |
type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*);
|
|
308 |
val empty = (Graph.empty, Symtab.empty);
|
|
309 |
val copy = I;
|
|
310 |
val extend = I;
|
|
311 |
fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true));
|
|
312 |
);
|
|
313 |
|
|
314 |
|
|
315 |
(* queries *)
|
|
316 |
|
|
317 |
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get;
|
|
318 |
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy);
|
|
319 |
|
|
320 |
fun the_class_data thy class =
|
|
321 |
case lookup_class_data thy class
|
|
322 |
of NONE => error ("undeclared class " ^ quote class)
|
|
323 |
| SOME data => data;
|
|
324 |
|
|
325 |
val ancestry = Graph.all_succs o fst o ClassData.get;
|
|
326 |
|
|
327 |
fun param_map thy =
|
|
328 |
let
|
|
329 |
fun params class =
|
|
330 |
let
|
|
331 |
val const_typs = (#params o AxClass.get_definition thy) class;
|
|
332 |
val const_names = (#consts o fst o the_class_data thy) class;
|
|
333 |
in
|
|
334 |
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names
|
|
335 |
end;
|
|
336 |
in maps params o ancestry thy end;
|
|
337 |
|
|
338 |
fun these_defs thy = maps (these o Option.map snd o lookup_class_data thy) o ancestry thy;
|
|
339 |
|
|
340 |
fun these_intros thy =
|
|
341 |
Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o fst o rep_classdata) data))
|
|
342 |
((fst o ClassData.get) thy) [];
|
|
343 |
|
|
344 |
fun print_classes thy =
|
|
345 |
let
|
|
346 |
val algebra = Sign.classes_of thy;
|
|
347 |
val arities =
|
|
348 |
Symtab.empty
|
|
349 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
|
|
350 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities)
|
|
351 |
((#arities o Sorts.rep_algebra) algebra);
|
|
352 |
val the_arities = these o Symtab.lookup arities;
|
|
353 |
fun mk_arity class tyco =
|
|
354 |
let
|
|
355 |
val Ss = Sorts.mg_domain algebra tyco [class];
|
|
356 |
in Sign.pretty_arity thy (tyco, Ss, [class]) end;
|
|
357 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
|
|
358 |
^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty);
|
|
359 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
|
|
360 |
(SOME o Pretty.str) ("class " ^ class ^ ":"),
|
|
361 |
(SOME o Pretty.block) [Pretty.str "supersort: ",
|
|
362 |
(Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class],
|
|
363 |
Option.map (Pretty.str o prefix "locale: " o #locale o fst) (lookup_class_data thy class),
|
|
364 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param
|
|
365 |
o these o Option.map #params o try (AxClass.get_definition thy)) class,
|
|
366 |
(SOME o Pretty.block o Pretty.breaks) [
|
|
367 |
Pretty.str "instances:",
|
|
368 |
Pretty.list "" "" (map (mk_arity class) (the_arities class))
|
|
369 |
]
|
|
370 |
]
|
|
371 |
in
|
|
372 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes)
|
|
373 |
algebra
|
|
374 |
end;
|
|
375 |
|
|
376 |
|
|
377 |
(* updaters *)
|
|
378 |
|
|
379 |
fun add_class_data ((class, superclasses), (locale, consts, v, intro)) =
|
|
380 |
ClassData.map (fn (gr, tab) => (
|
|
381 |
gr
|
|
382 |
|> Graph.new_node (class, ClassData ({ locale = locale, consts = consts,
|
|
383 |
v = v, intro = intro }, []))
|
|
384 |
|> fold (curry Graph.add_edge class) superclasses,
|
|
385 |
tab
|
|
386 |
|> Symtab.update (locale, class)
|
|
387 |
));
|
|
388 |
|
|
389 |
fun add_class_const_thm (class, thm) = (ClassData.map o apfst o Graph.map_node class)
|
|
390 |
(fn ClassData (data, thms) => ClassData (data, thm :: thms));
|
|
391 |
|
|
392 |
(* tactics and methods *)
|
|
393 |
|
|
394 |
fun intro_classes_tac facts st =
|
|
395 |
let
|
|
396 |
val thy = Thm.theory_of_thm st;
|
|
397 |
val classes = Sign.all_classes thy;
|
|
398 |
val class_trivs = map (Thm.class_triv thy) classes;
|
|
399 |
val class_intros = these_intros thy;
|
|
400 |
fun add_axclass_intro class =
|
|
401 |
case try (AxClass.get_definition thy) class of SOME {intro, ...} => cons intro | _ => I;
|
|
402 |
val axclass_intros = fold add_axclass_intro classes [];
|
|
403 |
in
|
|
404 |
st
|
|
405 |
|> ((ALLGOALS (Method.insert_tac facts THEN'
|
|
406 |
REPEAT_ALL_NEW (resolve_tac (class_trivs @ class_intros @ axclass_intros))))
|
|
407 |
THEN Tactic.distinct_subgoals_tac)
|
|
408 |
end;
|
|
409 |
|
|
410 |
fun default_intro_classes_tac [] = intro_classes_tac []
|
|
411 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*)
|
|
412 |
|
|
413 |
fun default_tac rules ctxt facts =
|
|
414 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
|
|
415 |
default_intro_classes_tac facts;
|
|
416 |
|
|
417 |
val _ = Context.add_setup (Method.add_methods
|
|
418 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
|
|
419 |
"back-chain introduction rules of classes"),
|
|
420 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
|
|
421 |
"apply some intro/elim rule")]);
|
|
422 |
|
|
423 |
|
|
424 |
(* tactical interfaces to locale commands *)
|
|
425 |
|
|
426 |
fun prove_interpretation tac prfx_atts expr insts thy =
|
|
427 |
thy
|
|
428 |
|> Locale.interpretation_i I prfx_atts expr insts
|
|
429 |
|> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
|
|
430 |
|> ProofContext.theory_of;
|
|
431 |
|
|
432 |
fun prove_interpretation_in tac after_qed (name, expr) thy =
|
|
433 |
thy
|
|
434 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr)
|
|
435 |
|> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
|
|
436 |
|> ProofContext.theory_of;
|
|
437 |
|
|
438 |
|
|
439 |
(* constructing class introduction and other rules from axclass and locale rules *)
|
|
440 |
|
|
441 |
fun mk_instT class = Symtab.empty
|
|
442 |
|> Symtab.update (AxClass.param_tyvarname, TFree (AxClass.param_tyvarname, [class]));
|
|
443 |
|
|
444 |
fun mk_inst class param_names cs =
|
|
445 |
Symtab.empty
|
|
446 |
|> fold2 (fn v => fn (c, ty) => Symtab.update (v, Const
|
|
447 |
(c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty))) param_names cs;
|
|
448 |
|
|
449 |
fun OF_LAST thm1 thm2 =
|
|
450 |
let
|
|
451 |
val n = (length o Logic.strip_imp_prems o prop_of) thm2;
|
|
452 |
in (thm1 RSN (n, thm2)) end;
|
|
453 |
|
|
454 |
fun strip_all_ofclass thy sort =
|
|
455 |
let
|
|
456 |
val typ = TVar ((AxClass.param_tyvarname, 0), sort);
|
|
457 |
fun prem_inclass t =
|
|
458 |
case Logic.strip_imp_prems t
|
|
459 |
of ofcls :: _ => try Logic.dest_inclass ofcls
|
|
460 |
| [] => NONE;
|
|
461 |
fun strip_ofclass class thm =
|
|
462 |
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache;
|
|
463 |
fun strip thm = case (prem_inclass o Thm.prop_of) thm
|
|
464 |
of SOME (_, class) => thm |> strip_ofclass class |> strip
|
|
465 |
| NONE => thm;
|
|
466 |
in strip end;
|
|
467 |
|
|
468 |
fun class_intro thy locale class sups =
|
|
469 |
let
|
|
470 |
fun class_elim class =
|
|
471 |
case (map Drule.unconstrainTs o #axioms o AxClass.get_definition thy) class
|
|
472 |
of [thm] => SOME thm
|
|
473 |
| [] => NONE;
|
|
474 |
val pred_intro = case Locale.intros thy locale
|
|
475 |
of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME
|
|
476 |
| ([intro], []) => SOME intro
|
|
477 |
| ([], [intro]) => SOME intro
|
|
478 |
| _ => NONE;
|
|
479 |
val pred_intro' = pred_intro
|
|
480 |
|> Option.map (fn intro => intro OF map_filter class_elim sups);
|
|
481 |
val class_intro = (#intro o AxClass.get_definition thy) class;
|
|
482 |
val raw_intro = case pred_intro'
|
|
483 |
of SOME pred_intro => class_intro |> OF_LAST pred_intro
|
|
484 |
| NONE => class_intro;
|
|
485 |
val sort = Sign.super_classes thy class;
|
|
486 |
val typ = TVar ((AxClass.param_tyvarname, 0), sort);
|
|
487 |
val defs = these_defs thy sups;
|
|
488 |
in
|
|
489 |
raw_intro
|
|
490 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] []
|
|
491 |
|> strip_all_ofclass thy sort
|
|
492 |
|> Thm.strip_shyps
|
|
493 |
|> MetaSimplifier.rewrite_rule defs
|
|
494 |
|> Drule.unconstrainTs
|
|
495 |
end;
|
|
496 |
|
|
497 |
fun interpretation_in_rule thy (class1, class2) =
|
|
498 |
let
|
|
499 |
val (params, consts) = split_list (param_map thy [class1]);
|
|
500 |
(*FIXME also remember this at add_class*)
|
|
501 |
fun mk_axioms class =
|
|
502 |
let
|
|
503 |
val name_locale = (#locale o fst o the_class_data thy) class;
|
|
504 |
val inst = mk_inst class params consts;
|
|
505 |
in
|
|
506 |
Locale.global_asms_of thy name_locale
|
|
507 |
|> maps snd
|
|
508 |
|> (map o map_aterms) (fn Free (s, _) => (the o Symtab.lookup inst) s | t => t)
|
|
509 |
|> (map o map_types o map_atyps) (fn TFree _ => TFree ("'a", [class1]) | T => T)
|
|
510 |
|> map (ObjectLogic.ensure_propT thy)
|
|
511 |
end;
|
|
512 |
val (prems, concls) = pairself mk_axioms (class1, class2);
|
|
513 |
in
|
|
514 |
Goal.prove_global thy [] prems (Logic.mk_conjunction_list concls)
|
|
515 |
(Locale.intro_locales_tac true (ProofContext.init thy))
|
|
516 |
end;
|
|
517 |
|
|
518 |
|
|
519 |
(* classes *)
|
|
520 |
|
|
521 |
local
|
|
522 |
|
|
523 |
fun read_param thy raw_t =
|
|
524 |
let
|
|
525 |
val t = Sign.read_term thy raw_t
|
|
526 |
in case try dest_Const t
|
|
527 |
of SOME (c, _) => c
|
|
528 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
|
|
529 |
end;
|
|
530 |
|
|
531 |
fun gen_class add_locale prep_class prep_param bname
|
|
532 |
raw_supclasses raw_elems raw_other_consts thy =
|
|
533 |
let
|
|
534 |
(*FIXME need proper concept for reading locale statements*)
|
|
535 |
fun subst_classtyvar (_, _) =
|
|
536 |
TFree (AxClass.param_tyvarname, [])
|
|
537 |
| subst_classtyvar (v, sort) =
|
|
538 |
error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort);
|
|
539 |
(*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I,
|
|
540 |
typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
|
|
541 |
val other_consts = map (prep_param thy) raw_other_consts;
|
|
542 |
val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
|
|
543 |
| Locale.Expr i => apsnd (cons i)) raw_elems ([], []);
|
|
544 |
val supclasses = map (prep_class thy) raw_supclasses;
|
|
545 |
val sups = filter (is_some o lookup_class_data thy) supclasses
|
|
546 |
|> Sign.certify_sort thy;
|
|
547 |
val supsort = Sign.certify_sort thy supclasses;
|
|
548 |
val suplocales = map (Locale.Locale o #locale o fst o the_class_data thy) sups;
|
|
549 |
val supexpr = Locale.Merge (suplocales @ includes);
|
|
550 |
val supparams = (map fst o Locale.parameters_of_expr thy)
|
|
551 |
(Locale.Merge suplocales);
|
|
552 |
val supconsts = AList.make (the o AList.lookup (op =) (param_map thy sups))
|
|
553 |
(map fst supparams);
|
|
554 |
(*val elems_constrains = map
|
|
555 |
(Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*)
|
|
556 |
fun mk_tyvar (_, sort) = TFree (AxClass.param_tyvarname,
|
|
557 |
if Sign.subsort thy (supsort, sort) then sort else error
|
|
558 |
("Sort " ^ Sign.string_of_sort thy sort
|
|
559 |
^ " is less general than permitted least general sort "
|
|
560 |
^ Sign.string_of_sort thy supsort));
|
|
561 |
fun extract_params thy name_locale =
|
|
562 |
let
|
|
563 |
val params = Locale.parameters_of thy name_locale;
|
|
564 |
val v = case (maps typ_tfrees o map (snd o fst)) params
|
|
565 |
of (v, _) :: _ => SOME v
|
|
566 |
| _ => NONE;
|
|
567 |
in
|
|
568 |
(v, (map (fst o fst) params, params
|
|
569 |
|> (map o apfst o apsnd o Term.map_type_tfree) mk_tyvar
|
|
570 |
|> (map o apsnd) (fork_mixfix true NONE #> fst)
|
|
571 |
|> chop (length supconsts)
|
|
572 |
|> snd))
|
|
573 |
end;
|
|
574 |
fun extract_assumes name_locale params thy cs =
|
|
575 |
let
|
|
576 |
val consts = supconsts @ (map (fst o fst) params ~~ cs);
|
|
577 |
fun subst (Free (c, ty)) =
|
|
578 |
Const ((fst o the o AList.lookup (op =) consts) c, ty)
|
|
579 |
| subst t = t;
|
|
580 |
val super_defs = these_defs thy sups;
|
|
581 |
fun prep_asm ((name, atts), ts) =
|
|
582 |
((NameSpace.base name, map (Attrib.attribute thy) atts),
|
|
583 |
(map o map_aterms) ((*MetaSimplifier.rewrite_term thy super_defs [] o *)subst) ts);
|
|
584 |
in
|
|
585 |
Locale.global_asms_of thy name_locale
|
|
586 |
|> map prep_asm
|
|
587 |
end;
|
|
588 |
fun note_intro name_axclass class_intro =
|
|
589 |
PureThy.note_thmss_qualified "" ((Logic.const_of_class o NameSpace.base) name_axclass)
|
|
590 |
[(("intro", []), [([class_intro], [])])]
|
|
591 |
#> snd
|
|
592 |
in
|
|
593 |
thy
|
|
594 |
|> add_locale (SOME "") bname supexpr ((*elems_constrains @*) elems)
|
|
595 |
|-> (fn name_locale => ProofContext.theory_result (
|
|
596 |
`(fn thy => extract_params thy name_locale)
|
|
597 |
#-> (fn (v, (param_names, params)) =>
|
|
598 |
axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts
|
|
599 |
#-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
|
|
600 |
`(fn thy => class_intro thy name_locale name_axclass sups)
|
|
601 |
#-> (fn class_intro =>
|
|
602 |
add_class_data ((name_axclass, sups),
|
|
603 |
(name_locale, map (fst o fst) params ~~ map fst consts, v,
|
|
604 |
class_intro))
|
|
605 |
(*FIXME: class_data should already contain data relevant
|
|
606 |
for interpretation; use this later for class target*)
|
|
607 |
(*FIXME: general export_fixes which may be parametrized
|
|
608 |
with pieces of an emerging class*)
|
|
609 |
#> note_intro name_axclass class_intro
|
|
610 |
#> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
|
|
611 |
((false, Logic.const_of_class bname), []) (Locale.Locale name_locale)
|
|
612 |
((mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts)), [])
|
|
613 |
#> pair name_axclass
|
|
614 |
)))))
|
|
615 |
end;
|
|
616 |
|
|
617 |
in
|
|
618 |
|
|
619 |
val class_cmd = gen_class Locale.add_locale Sign.intern_class read_param;
|
|
620 |
val class = gen_class Locale.add_locale_i Sign.certify_class (K I);
|
|
621 |
|
|
622 |
end; (*local*)
|
|
623 |
|
|
624 |
local
|
|
625 |
|
|
626 |
fun instance_subclass (class1, class2) thy =
|
|
627 |
let
|
|
628 |
val interp = interpretation_in_rule thy (class1, class2);
|
|
629 |
val ax = #axioms (AxClass.get_definition thy class1);
|
|
630 |
val intro = #intro (AxClass.get_definition thy class2)
|
|
631 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy
|
|
632 |
(TVar ((AxClass.param_tyvarname, 0), [class1])))] [];
|
|
633 |
val thm =
|
|
634 |
intro
|
|
635 |
|> OF_LAST (interp OF ax)
|
|
636 |
|> strip_all_ofclass thy (Sign.super_classes thy class2);
|
|
637 |
in
|
|
638 |
thy |> AxClass.add_classrel thm
|
|
639 |
end;
|
|
640 |
|
|
641 |
fun instance_subsort (class, sort) thy =
|
|
642 |
let
|
|
643 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra
|
|
644 |
o Sign.classes_of) thy sort;
|
|
645 |
val classes = filter_out (fn class' => Sign.subsort thy ([class], [class']))
|
|
646 |
(rev super_sort);
|
|
647 |
in
|
|
648 |
thy |> fold (curry instance_subclass class) classes
|
|
649 |
end;
|
|
650 |
|
|
651 |
fun instance_sort' do_proof (class, sort) theory =
|
|
652 |
let
|
|
653 |
val loc_name = (#locale o fst o the_class_data theory) class;
|
|
654 |
val loc_expr =
|
|
655 |
(Locale.Merge o map (Locale.Locale o #locale o fst o the_class_data theory)) sort;
|
|
656 |
in
|
|
657 |
theory
|
|
658 |
|> do_proof (instance_subsort (class, sort)) (loc_name, loc_expr)
|
|
659 |
end;
|
|
660 |
|
|
661 |
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory =
|
|
662 |
let
|
|
663 |
val class = prep_class theory raw_class;
|
|
664 |
val sort = prep_sort theory raw_sort;
|
|
665 |
in
|
|
666 |
theory
|
|
667 |
|> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort)
|
|
668 |
end;
|
|
669 |
|
|
670 |
fun gen_instance_class prep_class (raw_class, raw_superclass) theory =
|
|
671 |
let
|
|
672 |
val class = prep_class theory raw_class;
|
|
673 |
val superclass = prep_class theory raw_superclass;
|
|
674 |
in
|
|
675 |
theory
|
|
676 |
|> axclass_instance_sort (class, superclass)
|
|
677 |
end;
|
|
678 |
|
|
679 |
in
|
|
680 |
|
24276
|
681 |
val instance_sort_cmd = gen_instance_sort Sign.read_class Syntax.global_read_sort;
|
24218
|
682 |
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort;
|
|
683 |
val prove_instance_sort = instance_sort' o prove_interpretation_in;
|
|
684 |
val instance_class_cmd = gen_instance_class Sign.read_class;
|
|
685 |
val instance_class = gen_instance_class Sign.certify_class;
|
|
686 |
|
|
687 |
end; (*local*)
|
|
688 |
|
|
689 |
|
|
690 |
(** class target **)
|
|
691 |
|
|
692 |
fun export_fixes thy class =
|
|
693 |
let
|
|
694 |
val v = (#v o fst o the_class_data thy) class;
|
|
695 |
val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) [class];
|
|
696 |
val subst_typ = Term.map_type_tfree (fn var as (w, sort) =>
|
|
697 |
if SOME w = v then TFree (w, constrain_sort sort) else TFree var);
|
|
698 |
val consts = param_map thy [class];
|
|
699 |
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v
|
|
700 |
of SOME (c, _) => Const (c, ty)
|
|
701 |
| NONE => t)
|
|
702 |
| subst_aterm t = t;
|
|
703 |
in map_types subst_typ #> Term.map_aterms subst_aterm end;
|
|
704 |
|
|
705 |
fun add_const_in_class class ((c, rhs), syn) thy =
|
|
706 |
let
|
|
707 |
val prfx = (Logic.const_of_class o NameSpace.base) class;
|
|
708 |
fun mk_name inject c =
|
|
709 |
let
|
|
710 |
val n1 = Sign.full_name thy c;
|
|
711 |
val n2 = NameSpace.qualifier n1;
|
|
712 |
val n3 = NameSpace.base n1;
|
|
713 |
in NameSpace.implode (n2 :: inject @ [n3]) end;
|
|
714 |
val abbr' = mk_name [prfx, prfx] c;
|
|
715 |
val rhs' = export_fixes thy class rhs;
|
|
716 |
val ty' = Term.fastype_of rhs';
|
|
717 |
val def = (c, Logic.mk_equals (Const (mk_name [prfx] c, ty'), rhs'));
|
|
718 |
val (syn', _) = fork_mixfix true NONE syn;
|
|
719 |
fun interpret def =
|
|
720 |
let
|
|
721 |
val def' = symmetric def
|
|
722 |
val tac = (ALLGOALS o ProofContext.fact_tac) [def'];
|
|
723 |
val name_locale = (#locale o fst o the_class_data thy) class;
|
|
724 |
val def_eq = Thm.prop_of def';
|
|
725 |
val (params, consts) = split_list (param_map thy [class]);
|
|
726 |
in
|
|
727 |
prove_interpretation tac ((false, prfx), []) (Locale.Locale name_locale)
|
|
728 |
((mk_instT class, mk_inst class params consts), [def_eq])
|
|
729 |
#> add_class_const_thm (class, def')
|
|
730 |
end;
|
|
731 |
in
|
|
732 |
thy
|
|
733 |
|> Sign.hide_consts_i true [abbr']
|
|
734 |
|> Sign.add_path prfx
|
|
735 |
|> Sign.add_consts_authentic [(c, ty', syn')]
|
|
736 |
|> Sign.parent_path
|
|
737 |
|> Sign.sticky_prefix prfx
|
|
738 |
|> PureThy.add_defs_i false [(def, [])]
|
|
739 |
|-> (fn [def] => interpret def)
|
|
740 |
|> Sign.restore_naming thy
|
|
741 |
end;
|
|
742 |
|
|
743 |
end;
|