author | wenzelm |
Fri, 11 May 2007 17:54:36 +0200 | |
changeset 22936 | 284b56463da8 |
parent 22930 | f99617e7103f |
child 22997 | d4f3b015b50b |
permissions | -rw-r--r-- |
18168 | 1 |
(* Title: Pure/Tools/class_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
19468 | 5 |
Type classes derived from primitive axclasses and locales. |
18168 | 6 |
*) |
7 |
||
8 |
signature CLASS_PACKAGE = |
|
9 |
sig |
|
22353 | 10 |
val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix |
22302 | 11 |
|
22737 | 12 |
val axclass_cmd: bstring * xstring list |
13 |
-> ((bstring * Attrib.src list) * string list) list -> theory -> class * theory |
|
22385 | 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 |
|
22331 | 18 |
val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list |
22302 | 19 |
-> theory -> Proof.state |
22331 | 20 |
val instance_arity_cmd: (bstring * string list * string) list |
22302 | 21 |
-> ((bstring * Attrib.src list) * string) list |
18575 | 22 |
-> theory -> Proof.state |
22331 | 23 |
val prove_instance_arity: tactic -> arity list |
22302 | 24 |
-> ((bstring * Attrib.src list) * term) list |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
25 |
-> theory -> theory |
22321 | 26 |
val instance_class: class * class -> theory -> Proof.state |
27 |
val instance_class_cmd: string * string -> theory -> Proof.state |
|
21954 | 28 |
val instance_sort: class * sort -> theory -> Proof.state |
22321 | 29 |
val instance_sort_cmd: string * string -> theory -> Proof.state |
19150 | 30 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
18168 | 31 |
|
21954 | 32 |
(* experimental class target *) |
22302 | 33 |
val class_of_locale: theory -> string -> class option |
34 |
val add_def_in_class: local_theory -> string |
|
35 |
-> (string * Syntax.mixfix) * ((string * Attrib.src list) * (term * thm)) -> theory -> theory |
|
22930 | 36 |
val CLASS_TARGET: bool ref |
21553 | 37 |
|
18702 | 38 |
val print_classes: theory -> unit |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
39 |
val intro_classes_tac: thm list -> tactic |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
40 |
val default_intro_classes_tac: thm list -> tactic |
18168 | 41 |
end; |
42 |
||
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
43 |
structure ClassPackage : CLASS_PACKAGE = |
18168 | 44 |
struct |
45 |
||
22302 | 46 |
(** auxiliary **) |
18168 | 47 |
|
22353 | 48 |
fun fork_mixfix is_loc some_class mx = |
22302 | 49 |
let |
50 |
val mx' = Syntax.unlocalize_mixfix mx; |
|
22737 | 51 |
val mx_global = if is_some some_class orelse (is_loc andalso mx = mx') |
52 |
then NoSyn else mx'; |
|
22353 | 53 |
val mx_local = if is_loc then mx else NoSyn; |
54 |
in (mx_global, mx_local) end; |
|
22302 | 55 |
|
22737 | 56 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy = |
57 |
let |
|
58 |
val ctxt = ProofContext.init thy; |
|
59 |
val superclasses = map (Sign.read_class thy) raw_superclasses; |
|
60 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) raw_specs; |
|
61 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) raw_specs) |
|
62 |
|> snd |
|
63 |
|> (map o map) fst; |
|
64 |
in AxClass.define_class (class, superclasses) [] (name_atts ~~ axiomss) thy end; |
|
65 |
||
22302 | 66 |
|
67 |
(** AxClasses with implicit parameter handling **) |
|
68 |
||
69 |
(* AxClass instances *) |
|
18168 | 70 |
|
21954 | 71 |
local |
19456 | 72 |
|
21954 | 73 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
74 |
let |
|
75 |
fun after_qed' results = |
|
76 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
77 |
in |
|
78 |
thy |
|
79 |
|> ProofContext.init |
|
80 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts) |
|
81 |
end; |
|
18168 | 82 |
|
21954 | 83 |
in |
84 |
||
85 |
val axclass_instance_arity = |
|
86 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
|
87 |
val axclass_instance_sort = |
|
88 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) |
|
89 |
AxClass.add_classrel I o single; |
|
90 |
||
91 |
end; (* local *) |
|
18575 | 92 |
|
19038 | 93 |
|
21954 | 94 |
(* introducing axclasses with implicit parameter handling *) |
19038 | 95 |
|
22385 | 96 |
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy = |
21924 | 97 |
let |
21954 | 98 |
val superclasses = map (Sign.certify_class thy) raw_superclasses; |
99 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; |
|
100 |
fun add_const ((c, ty), syn) = |
|
22930 | 101 |
Sign.add_consts_authentic [(c, ty, syn)] |
102 |
#> pair (Sign.full_name thy c, ty); |
|
21954 | 103 |
fun mk_axioms cs thy = |
104 |
raw_dep_axioms thy cs |
|
105 |
|> (map o apsnd o map) (Sign.cert_prop thy) |
|
106 |
|> rpair thy; |
|
107 |
fun add_constraint class (c, ty) = |
|
108 |
Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty)); |
|
21924 | 109 |
in |
21954 | 110 |
thy |
22930 | 111 |
(* |> Theory.add_path name *) |
21954 | 112 |
|> fold_map add_const consts |
22930 | 113 |
(* ||> Theory.restore_naming thy *) |
21954 | 114 |
|-> (fn cs => mk_axioms cs |
22737 | 115 |
#-> (fn axioms_prop => AxClass.define_class (name, superclasses) (map fst cs @ other_consts) axioms_prop |
21954 | 116 |
#-> (fn class => `(fn thy => AxClass.get_definition thy class) |
117 |
#-> (fn {intro, axioms, ...} => fold (add_constraint class) cs |
|
22353 | 118 |
#> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs)))))) |
21924 | 119 |
end; |
120 |
||
18168 | 121 |
|
21954 | 122 |
(* instances with implicit parameter handling *) |
19038 | 123 |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
124 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
125 |
|
20175 | 126 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
19038 | 127 |
let |
19957 | 128 |
val (_, t) = read_def thy (raw_name, raw_t); |
129 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
130 |
val atts = map (prep_att thy) raw_atts; |
|
22507 | 131 |
val insts = Consts.typargs (Sign.consts_of thy) (c, ty); |
19957 | 132 |
val name = case raw_name |
20385 | 133 |
of "" => NONE |
134 |
| _ => SOME raw_name; |
|
20175 | 135 |
in (c, (insts, ((name, t), atts))) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
136 |
|
22507 | 137 |
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Theory.read_axm; |
21954 | 138 |
fun read_def thy = gen_read_def thy (K I) (K I); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
139 |
|
22302 | 140 |
fun gen_instance_arity prep_arity read_def do_proof raw_arities raw_defs theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
141 |
let |
22331 | 142 |
val arities = map (prep_arity theory) raw_arities; |
20175 | 143 |
val _ = if null arities then error "at least one arity must be given" else (); |
144 |
val _ = case (duplicates (op =) o map #1) arities |
|
145 |
of [] => () |
|
146 |
| dupl_tycos => error ("type constructors occur more than once in arities: " |
|
22423 | 147 |
^ (commas o map quote) dupl_tycos); |
21954 | 148 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory |
20175 | 149 |
fun get_consts_class tyco ty class = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
150 |
let |
21954 | 151 |
val cs = (these o Option.map snd o try (AxClass.params_of_class theory)) class; |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
152 |
val subst_ty = map_type_tfree (K ty); |
19957 | 153 |
in |
21894 | 154 |
map (fn (c, ty) => (c, ((tyco, class), subst_ty ty))) cs |
19957 | 155 |
end; |
20175 | 156 |
fun get_consts_sort (tyco, asorts, sort) = |
19957 | 157 |
let |
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
158 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
21954 | 159 |
in maps (get_consts_class tyco ty) (super_sort sort) end; |
20175 | 160 |
val cs = maps get_consts_sort arities; |
20385 | 161 |
fun mk_typnorm thy (ty, ty_sc) = |
162 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
163 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
164 |
| NONE => NONE; |
|
20175 | 165 |
fun read_defs defs cs thy_read = |
166 |
let |
|
19957 | 167 |
fun read raw_def cs = |
168 |
let |
|
20385 | 169 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
170 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
20175 | 171 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
22507 | 172 |
of NONE => error ("illegal definition for constant " ^ quote c) |
19966 | 173 |
| SOME class_ty => class_ty; |
20385 | 174 |
val name = case name_opt |
20628 | 175 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
20385 | 176 |
| SOME name => name; |
177 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
22507 | 178 |
of NONE => error ("illegal definition for constant " ^ |
19966 | 179 |
quote c ^ "::" ^ Sign.string_of_typ thy_read ty) |
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20465
diff
changeset
|
180 |
| SOME norm => map_types norm t |
20175 | 181 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
19957 | 182 |
in fold_map read defs cs end; |
22331 | 183 |
val (defs, _) = read_defs raw_defs cs |
184 |
(fold Sign.primitive_arity arities (Theory.copy theory)); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
185 |
fun get_remove_contraint c thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
186 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
187 |
val ty = Sign.the_const_constraint thy c; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
188 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
189 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
190 |
|> Sign.add_const_constraint_i (c, NONE) |
20385 | 191 |
|> pair (c, Logic.unvarifyT ty) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
192 |
end; |
19966 | 193 |
fun add_defs defs thy = |
194 |
thy |
|
22302 | 195 |
|> PureThy.add_defs_i true (map ((apsnd o map) (Attrib.attribute thy) o snd) defs) |
19966 | 196 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
22737 | 197 |
fun after_qed cs defs thy = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
198 |
thy |
22737 | 199 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs) |
200 |
|> fold (CodegenData.add_func false o snd) defs; |
|
19038 | 201 |
in |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
202 |
theory |
20385 | 203 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =)) |
19966 | 204 |
||>> add_defs defs |
22737 | 205 |
|-> (fn (cs, defs) => do_proof (after_qed cs defs) arities) |
19038 | 206 |
end; |
207 |
||
22321 | 208 |
fun instance_arity_e' do_proof = gen_instance_arity Sign.read_arity read_def_cmd do_proof; |
22302 | 209 |
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof; |
21924 | 210 |
fun tactic_proof tac after_qed arities = |
211 |
fold (fn arity => AxClass.prove_arity arity tac) arities |
|
20175 | 212 |
#> after_qed; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
213 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
214 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
215 |
|
22321 | 216 |
val instance_arity_cmd = instance_arity_e' axclass_instance_arity; |
21954 | 217 |
val instance_arity = instance_arity' axclass_instance_arity; |
218 |
val prove_instance_arity = instance_arity' o tactic_proof; |
|
219 |
||
220 |
end; (* local *) |
|
221 |
||
222 |
||
223 |
||
224 |
(** combining locales and axclasses **) |
|
225 |
||
226 |
(* theory data *) |
|
227 |
||
228 |
datatype class_data = ClassData of { |
|
229 |
locale: string, |
|
230 |
consts: (string * string) list |
|
231 |
(*locale parameter ~> toplevel theory constant*), |
|
22524 | 232 |
intro: thm, |
22302 | 233 |
witness: Element.witness list, |
22524 | 234 |
primdefs: thm list, |
235 |
(*for experimental class target*) |
|
236 |
propnames: string list |
|
22302 | 237 |
(*for ad-hoc instance_in*) |
21954 | 238 |
}; |
239 |
||
240 |
fun rep_classdata (ClassData c) = c; |
|
241 |
||
22302 | 242 |
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2)); |
243 |
||
22846 | 244 |
structure ClassData = TheoryDataFun |
245 |
( |
|
246 |
type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*); |
|
247 |
val empty = (Graph.empty, Symtab.empty); |
|
248 |
val copy = I; |
|
249 |
val extend = I; |
|
250 |
fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true)); |
|
21954 | 251 |
); |
252 |
||
253 |
||
254 |
(* queries *) |
|
255 |
||
22302 | 256 |
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get; |
257 |
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy); |
|
258 |
||
21954 | 259 |
fun the_class_data thy class = |
260 |
case lookup_class_data thy class |
|
261 |
of NONE => error ("undeclared class " ^ quote class) |
|
262 |
| SOME data => data; |
|
263 |
||
22302 | 264 |
val ancestry = Graph.all_succs o fst o ClassData.get; |
21954 | 265 |
|
22331 | 266 |
fun param_map thy = |
21954 | 267 |
let |
22302 | 268 |
fun params thy class = |
269 |
let |
|
270 |
val const_typs = (#params o AxClass.get_definition thy) class; |
|
271 |
val const_names = (#consts o the_class_data thy) class; |
|
272 |
in |
|
273 |
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names |
|
274 |
end; |
|
275 |
in maps (params thy) o ancestry thy end; |
|
21954 | 276 |
|
22524 | 277 |
fun these_intros thy = |
278 |
Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_classdata) data)) |
|
279 |
((fst o ClassData.get) thy) []; |
|
22302 | 280 |
val the_witness = #witness oo the_class_data; |
21954 | 281 |
val the_propnames = #propnames oo the_class_data; |
282 |
||
283 |
fun print_classes thy = |
|
284 |
let |
|
285 |
val algebra = Sign.classes_of thy; |
|
286 |
val arities = |
|
287 |
Symtab.empty |
|
288 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
289 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
290 |
((#arities o Sorts.rep_algebra) algebra); |
|
291 |
val the_arities = these o Symtab.lookup arities; |
|
292 |
fun mk_arity class tyco = |
|
293 |
let |
|
294 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
295 |
in Sign.pretty_arity thy (tyco, Ss, [class]) end; |
|
296 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
|
297 |
^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty); |
|
298 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
|
299 |
(SOME o Pretty.str) ("class " ^ class ^ ":"), |
|
300 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
|
301 |
(Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class], |
|
302 |
Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class), |
|
303 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param |
|
304 |
o these o Option.map #params o try (AxClass.get_definition thy)) class, |
|
305 |
(SOME o Pretty.block o Pretty.breaks) [ |
|
306 |
Pretty.str "instances:", |
|
307 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
308 |
] |
|
309 |
] |
|
310 |
in |
|
311 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes) |
|
312 |
algebra |
|
313 |
end; |
|
314 |
||
315 |
||
316 |
(* updaters *) |
|
317 |
||
22524 | 318 |
fun add_class_data ((class, superclasses), (locale, consts, intro, witness, propnames)) = |
22302 | 319 |
ClassData.map (fn (gr, tab) => ( |
320 |
gr |
|
321 |
|> Graph.new_node (class, ClassData { |
|
21954 | 322 |
locale = locale, |
323 |
consts = consts, |
|
22524 | 324 |
intro = intro, |
22302 | 325 |
witness = witness, |
21954 | 326 |
propnames = propnames, |
22302 | 327 |
primdefs = []}) |
328 |
|> fold (curry Graph.add_edge class) superclasses, |
|
329 |
tab |
|
330 |
|> Symtab.update (locale, class) |
|
331 |
)); |
|
332 |
||
333 |
fun add_primdef (class, thm) thy = |
|
334 |
(ClassData.map o apfst o Graph.map_node class) |
|
22524 | 335 |
(fn ClassData { locale, consts, intro, witness, propnames, primdefs } => |
336 |
ClassData { locale = locale, consts = consts, intro = intro, |
|
337 |
witness = witness, propnames = propnames, primdefs = thm :: primdefs }); |
|
22302 | 338 |
|
21954 | 339 |
|
22302 | 340 |
(* exporting terms and theorems to global toplevel *) |
341 |
||
342 |
fun globalize thy classes = |
|
343 |
let |
|
344 |
val consts = param_map thy classes; |
|
345 |
val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) classes; |
|
346 |
val subst_typ = Term.map_type_tfree (fn var as (v, sort) => |
|
347 |
if v = AxClass.param_tyvarname then TFree (v, constrain_sort sort) else TFree var) |
|
348 |
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v |
|
349 |
of SOME (c, _) => Const (c, ty) |
|
350 |
| NONE => t) |
|
351 |
| subst_aterm t = t; |
|
352 |
in (subst_typ, map_types subst_typ #> Term.map_aterms subst_aterm) end; |
|
353 |
||
354 |
val global_term = snd oo globalize |
|
21954 | 355 |
|
356 |
||
357 |
(* tactics and methods *) |
|
358 |
||
22524 | 359 |
fun intro_classes_tac facts st = |
360 |
let |
|
361 |
val thy = Thm.theory_of_thm st; |
|
362 |
val classes = Sign.all_classes thy; |
|
363 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
364 |
val class_intros = these_intros thy; |
|
365 |
fun add_axclass_intro class = |
|
366 |
case try (AxClass.get_definition thy) class of SOME {intro, ...} => cons intro | _ => I; |
|
367 |
val axclass_intros = fold add_axclass_intro classes []; |
|
368 |
in |
|
369 |
st |
|
370 |
|> ((ALLGOALS (Method.insert_tac facts THEN' |
|
371 |
REPEAT_ALL_NEW (resolve_tac (class_trivs @ class_intros @ axclass_intros)))) |
|
372 |
THEN Tactic.distinct_subgoals_tac) |
|
373 |
end; |
|
374 |
||
21954 | 375 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
376 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
|
377 |
||
378 |
fun default_tac rules ctxt facts = |
|
379 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
380 |
default_intro_classes_tac facts; |
|
381 |
||
382 |
val _ = Context.add_setup (Method.add_methods |
|
22737 | 383 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
21954 | 384 |
"back-chain introduction rules of classes"), |
385 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
386 |
"apply some intro/elim rule")]); |
|
387 |
||
22423 | 388 |
|
22321 | 389 |
(* tactical interfaces to locale commands *) |
21954 | 390 |
|
22321 | 391 |
fun prove_interpretation tac prfx_atts expr insts thy = |
392 |
thy |
|
22658
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22588
diff
changeset
|
393 |
|> Locale.interpretation_i I prfx_atts expr (insts, []) |
22321 | 394 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
395 |
|> ProofContext.theory_of; |
|
21954 | 396 |
|
22069 | 397 |
fun prove_interpretation_in tac after_qed (name, expr) thy = |
398 |
thy |
|
399 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr) |
|
400 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
401 |
|> ProofContext.theory_of; |
|
402 |
||
403 |
fun instance_sort' do_proof (class, sort) theory = |
|
404 |
let |
|
405 |
val loc_name = (#locale o the_class_data theory) class; |
|
406 |
val loc_expr = |
|
407 |
(Locale.Merge o map (Locale.Locale o #locale o the_class_data theory)) sort; |
|
408 |
val const_names = (map (NameSpace.base o snd) |
|
409 |
o maps (#consts o the_class_data theory) |
|
22302 | 410 |
o ancestry theory) [class]; |
22069 | 411 |
fun get_thms thy = |
22302 | 412 |
ancestry thy sort |
22069 | 413 |
|> AList.make (the_propnames thy) |
414 |
|> map (apsnd (map (NameSpace.append (Logic.const_of_class loc_name)))) |
|
415 |
|> map_filter (fn (superclass, thm_names) => |
|
416 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
417 |
of [] => NONE |
|
418 |
| thms => SOME (superclass, thms)); |
|
419 |
fun after_qed thy = |
|
420 |
thy |
|
421 |
|> `get_thms |
|
422 |
|-> fold (fn (supclass, thms) => I |
|
423 |
AxClass.prove_classrel (class, supclass) |
|
424 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
425 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
426 |
in |
|
427 |
theory |
|
428 |
|> do_proof after_qed (loc_name, loc_expr) |
|
429 |
end; |
|
430 |
||
22074 | 431 |
val prove_instance_sort = instance_sort' o prove_interpretation_in; |
432 |
||
22069 | 433 |
|
22524 | 434 |
(* constructing class introduction rules from axclass and locale rules *) |
22069 | 435 |
|
22524 | 436 |
fun class_intro thy locale class sups = |
437 |
let |
|
438 |
fun OF_LAST thm1 thm2 = |
|
439 |
let |
|
440 |
val n = (length o Logic.strip_imp_prems o prop_of) thm2; |
|
441 |
in (thm1 RSN (n, thm2)) end; |
|
442 |
fun prem_inclass t = |
|
443 |
case Logic.strip_imp_prems t |
|
444 |
of ofcls :: _ => try Logic.dest_inclass ofcls |
|
445 |
| [] => NONE; |
|
446 |
val typ = TVar ((AxClass.param_tyvarname, 0), Sign.super_classes thy class); |
|
447 |
fun strip_ofclass class thm = |
|
448 |
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache; |
|
449 |
fun strip_all_ofclass thm = |
|
450 |
case (prem_inclass o Thm.prop_of) thm |
|
451 |
of SOME (_, class) => thm |> strip_ofclass class |> strip_all_ofclass |
|
452 |
| NONE => thm; |
|
453 |
fun class_elim class = |
|
454 |
case (map Drule.unconstrainTs o #axioms o AxClass.get_definition thy) class |
|
455 |
of [thm] => SOME thm |
|
456 |
| [] => NONE; |
|
457 |
val pred_intro = case Locale.intros thy locale |
|
458 |
of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME |
|
459 |
| ([intro], []) => SOME intro |
|
460 |
| ([], [intro]) => SOME intro |
|
461 |
| _ => NONE; |
|
462 |
val pred_intro' = pred_intro |
|
463 |
|> Option.map (fn intro => intro OF map_filter class_elim sups); |
|
464 |
val class_intro = (#intro o AxClass.get_definition thy) class; |
|
465 |
val raw_intro = case pred_intro' |
|
466 |
of SOME pred_intro => class_intro |> OF_LAST pred_intro |
|
467 |
| NONE => class_intro; |
|
468 |
in |
|
469 |
raw_intro |
|
470 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] [] |
|
471 |
|> strip_all_ofclass |
|
472 |
|> Thm.strip_shyps |
|
473 |
|> Drule.unconstrainTs |
|
474 |
end; |
|
475 |
||
476 |
||
477 |
(* classes and instances *) |
|
22385 | 478 |
|
22069 | 479 |
local |
480 |
||
22737 | 481 |
fun read_param thy raw_t = |
482 |
let |
|
483 |
val t = Sign.read_term thy raw_t |
|
484 |
in case try dest_Const t |
|
485 |
of SOME (c, _) => c |
|
486 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t) |
|
487 |
end; |
|
488 |
||
22473 | 489 |
fun gen_class add_locale prep_class prep_param bname |
490 |
raw_supclasses raw_elems raw_other_consts thy = |
|
21954 | 491 |
let |
22321 | 492 |
(*FIXME need proper concept for reading locale statements*) |
493 |
fun subst_classtyvar (_, _) = |
|
494 |
TFree (AxClass.param_tyvarname, []) |
|
495 |
| subst_classtyvar (v, sort) = |
|
496 |
error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort); |
|
497 |
(*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I, |
|
498 |
typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*) |
|
22385 | 499 |
val other_consts = map (prep_param thy) raw_other_consts; |
22423 | 500 |
val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e) |
501 |
| Locale.Expr i => apsnd (cons i)) raw_elems ([], []); |
|
21954 | 502 |
val supclasses = map (prep_class thy) raw_supclasses; |
22524 | 503 |
val sups = filter (is_some o lookup_class_data thy) supclasses |
504 |
|> Sign.certify_sort thy; |
|
22473 | 505 |
val supsort = Sign.certify_sort thy supclasses; |
506 |
val suplocales = map (Locale.Locale o #locale o the_class_data thy) sups; |
|
22423 | 507 |
val supexpr = Locale.Merge (suplocales @ includes); |
508 |
val supparams = (map fst o Locale.parameters_of_expr thy) |
|
509 |
(Locale.Merge suplocales); |
|
22473 | 510 |
val supconsts = AList.make (the o AList.lookup (op =) (param_map thy sups)) |
511 |
(map fst supparams); |
|
22321 | 512 |
(*val elems_constrains = map |
513 |
(Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*) |
|
22473 | 514 |
fun mk_tyvar (_, sort) = TFree (AxClass.param_tyvarname, |
515 |
if Sign.subsort thy (supsort, sort) then sort else error |
|
516 |
("Sort " ^ Sign.string_of_sort thy sort |
|
517 |
^ " is less general than permitted least general sort " |
|
518 |
^ Sign.string_of_sort thy supsort)); |
|
22321 | 519 |
fun extract_params thy name_locale = |
22473 | 520 |
let |
22321 | 521 |
val params = Locale.parameters_of thy name_locale; |
522 |
in |
|
523 |
(map (fst o fst) params, params |
|
22473 | 524 |
|> (map o apfst o apsnd o Term.map_type_tfree) mk_tyvar |
22353 | 525 |
|> (map o apsnd) (fork_mixfix true NONE #> fst) |
22321 | 526 |
|> chop (length supconsts) |
527 |
|> snd) |
|
21954 | 528 |
end; |
529 |
fun extract_assumes name_locale params thy cs = |
|
530 |
let |
|
531 |
val consts = supconsts @ (map (fst o fst) params ~~ cs); |
|
532 |
fun subst (Free (c, ty)) = |
|
22048 | 533 |
Const ((fst o the o AList.lookup (op =) consts) c, ty) |
534 |
| subst t = t; |
|
21954 | 535 |
fun prep_asm ((name, atts), ts) = |
22423 | 536 |
((NameSpace.base name, map (Attrib.attribute thy) atts), |
537 |
(map o map_aterms) subst ts); |
|
21954 | 538 |
in |
22182 | 539 |
Locale.global_asms_of thy name_locale |
21954 | 540 |
|> map prep_asm |
541 |
end; |
|
22048 | 542 |
fun extract_axiom_names thy name_locale = |
543 |
name_locale |
|
22182 | 544 |
|> Locale.global_asms_of thy |
22524 | 545 |
|> map (NameSpace.base o fst o fst) (*FIXME - is finally dropped*); |
22321 | 546 |
fun mk_instT class = Symtab.empty |
547 |
|> Symtab.update (AxClass.param_tyvarname, TFree (AxClass.param_tyvarname, [class])); |
|
548 |
fun mk_inst class param_names cs = |
|
549 |
Symtab.empty |
|
550 |
|> fold2 (fn v => fn (c, ty) => Symtab.update (v, Const |
|
551 |
(c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty))) param_names cs; |
|
552 |
fun make_witness t thm = Element.make_witness t (Goal.protect thm); |
|
22524 | 553 |
fun note_intro name_axclass class_intro = |
554 |
PureThy.note_thmss_qualified "" ((Logic.const_of_class o NameSpace.base) name_axclass) |
|
555 |
[(("intro", []), [([class_intro], [])])] |
|
556 |
#> snd |
|
21954 | 557 |
in |
558 |
thy |
|
22353 | 559 |
|> add_locale (SOME (bname ^ "_pred")) bname supexpr ((*elems_constrains @*) elems) |
21954 | 560 |
|-> (fn name_locale => ProofContext.theory_result ( |
22321 | 561 |
`(fn thy => extract_params thy name_locale) |
562 |
#-> (fn (param_names, params) => |
|
22385 | 563 |
axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts |
22302 | 564 |
#-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) => |
22524 | 565 |
`(fn thy => class_intro thy name_locale name_axclass sups) |
566 |
##>> `(fn thy => extract_axiom_names thy name_locale) |
|
567 |
#-> (fn (class_intro, axiom_names) => |
|
22473 | 568 |
add_class_data ((name_axclass, sups), |
22302 | 569 |
(name_locale, map (fst o fst) params ~~ map fst consts, |
22524 | 570 |
class_intro, map2 make_witness ax_terms ax_axioms, axiom_names)) |
571 |
#> note_intro name_axclass class_intro |
|
22321 | 572 |
#> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
22524 | 573 |
((false, Logic.const_of_class bname), []) (Locale.Locale name_locale) |
22321 | 574 |
(mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts)) |
21954 | 575 |
#> pair name_axclass |
22048 | 576 |
))))) |
21954 | 577 |
end; |
578 |
||
579 |
in |
|
580 |
||
22737 | 581 |
val class_cmd = gen_class Locale.add_locale Sign.intern_class read_param; |
22473 | 582 |
val class = gen_class Locale.add_locale_i Sign.certify_class (K I); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
583 |
|
20455 | 584 |
end; (*local*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
585 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
586 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
587 |
|
21954 | 588 |
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory = |
589 |
let |
|
590 |
val class = prep_class theory raw_class; |
|
591 |
val sort = prep_sort theory raw_sort; |
|
22321 | 592 |
in |
21954 | 593 |
theory |
594 |
|> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort) |
|
22321 | 595 |
end; |
596 |
||
597 |
fun gen_instance_class prep_class (raw_class, raw_superclass) theory = |
|
598 |
let |
|
599 |
val class = prep_class theory raw_class; |
|
600 |
val superclass = prep_class theory raw_superclass; |
|
601 |
in |
|
602 |
theory |
|
603 |
|> axclass_instance_sort (class, superclass) |
|
21954 | 604 |
end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
605 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
606 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
607 |
|
22321 | 608 |
val instance_sort_cmd = gen_instance_sort Sign.read_class Sign.read_sort; |
21954 | 609 |
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort; |
22321 | 610 |
val instance_class_cmd = gen_instance_class Sign.read_class; |
611 |
val instance_class = gen_instance_class Sign.certify_class; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
612 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
613 |
end; (* local *) |
19038 | 614 |
|
19957 | 615 |
|
21954 | 616 |
(** experimental class target **) |
617 |
||
22353 | 618 |
fun print_witness wit = |
21954 | 619 |
let |
22353 | 620 |
val (t, thm) = Element.dest_witness wit; |
621 |
val prop = Thm.prop_of thm; |
|
622 |
fun string_of_tfree (v, sort) = v ^ "::" ^ Display.raw_string_of_sort sort; |
|
623 |
fun string_of_tvar (v, sort) = Library.string_of_indexname v ^ "::" ^ Display.raw_string_of_sort sort; |
|
624 |
fun print_term t = |
|
625 |
let |
|
626 |
val term = Display.raw_string_of_term t; |
|
627 |
val tfrees = map string_of_tfree (Term.add_tfrees t []); |
|
628 |
val tvars = map string_of_tvar (Term.add_tvars t []); |
|
629 |
in term :: tfrees @ tvars end; |
|
22588 | 630 |
in (map warning (print_term t); map warning (print_term prop)) end; |
22353 | 631 |
|
22930 | 632 |
val CLASS_TARGET = ref true; |
633 |
||
22353 | 634 |
fun add_def_in_class lthy class ((c, syn), ((name, atts), (rhs, eq))) thy = |
635 |
let |
|
22423 | 636 |
val prfx = (Logic.const_of_class o NameSpace.base) class; |
22302 | 637 |
val rhs' = global_term thy [class] rhs; |
22353 | 638 |
val (syn', _) = fork_mixfix true NONE syn; |
22302 | 639 |
val ty = Term.fastype_of rhs'; |
22423 | 640 |
fun mk_name thy 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, prfx, n3] end; |
|
22302 | 646 |
fun add_const (c, ty, syn) = |
647 |
Sign.add_consts_authentic [(c, ty, syn)] |
|
22423 | 648 |
#> pair (mk_name thy c, ty); |
22302 | 649 |
fun add_def ((name, atts), prop) thy = |
650 |
thy |
|
651 |
|> PureThy.add_defs_i false [((name, prop), map (Attrib.attribute thy) atts)] |
|
652 |
|>> the_single; |
|
22737 | 653 |
(*val _ = warning "------ DEF ------"; |
22588 | 654 |
val _ = warning c; |
655 |
val _ = warning name; |
|
656 |
val _ = (warning o Sign.string_of_term thy) rhs'; |
|
22302 | 657 |
val eq' = Morphism.thm (LocalTheory.target_morphism lthy) eq; |
22588 | 658 |
val _ = (warning o string_of_thm) eq; |
659 |
val _ = (warning o string_of_thm) eq'; |
|
22302 | 660 |
val witness = the_witness thy class; |
22588 | 661 |
val _ = warning "------ WIT ------"; |
22302 | 662 |
fun print_wit (t, thm) = "(" ^ Sign.string_of_term thy t ^ ", " ^ Display.string_of_thm thm ^ ")" |
22353 | 663 |
fun print_raw_wit (t, thm) = "(" ^ Display.raw_string_of_term t ^ ", " ^ (Display.raw_string_of_term o Thm.prop_of) thm ^ ")" |
22588 | 664 |
val _ = (warning o cat_lines o map (print_wit o Element.dest_witness)) witness; |
22353 | 665 |
val _ = map print_witness witness; |
22588 | 666 |
val _ = (warning o string_of_thm) eq'; |
22302 | 667 |
val eq'' = Element.satisfy_thm witness eq'; |
22737 | 668 |
val _ = (warning o string_of_thm) eq'';*) |
21954 | 669 |
in |
22302 | 670 |
thy |
22737 | 671 |
(* |> Sign.add_path prfx |
22423 | 672 |
|> add_const (c, ty, syn') |
22302 | 673 |
|-> (fn c => add_def ((name, atts), Logic.mk_equals (Const c, rhs'))) |
22588 | 674 |
|-> (fn global_def_thm => tap (fn _ => (warning o string_of_thm) global_def_thm)) |
22737 | 675 |
|> Sign.restore_naming thy *) |
21954 | 676 |
end; |
677 |
||
20455 | 678 |
end; |