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