author | haftmann |
Sat, 10 Feb 2007 09:26:25 +0100 | |
changeset 22305 | 0e56750a092b |
parent 22302 | bf5bdb7f7607 |
child 22321 | e5cddafe2629 |
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 |
|
22302 | 10 |
val fork_mixfix: bool -> bool -> mixfix -> mixfix * mixfix |
11 |
||
21954 | 12 |
val class: bstring -> class list -> Element.context_i Locale.element list -> theory -> |
20964 | 13 |
string * Proof.context |
22302 | 14 |
val class_e: bstring -> string list -> Element.context Locale.element list -> theory -> |
15 |
string * Proof.context |
|
21954 | 16 |
val instance_arity: ((bstring * sort list) * sort) list |
22302 | 17 |
-> ((bstring * Attrib.src list) * term) list |
18 |
-> theory -> Proof.state |
|
19 |
val instance_arity_e: ((bstring * string list) * string) list |
|
20 |
-> ((bstring * Attrib.src list) * string) list |
|
18575 | 21 |
-> theory -> Proof.state |
21924 | 22 |
val prove_instance_arity: tactic -> ((string * sort list) * sort) list |
22302 | 23 |
-> ((bstring * Attrib.src list) * term) list |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
24 |
-> theory -> theory |
21954 | 25 |
val instance_sort: class * sort -> theory -> Proof.state |
22302 | 26 |
val instance_sort_e: string * string -> theory -> Proof.state |
19150 | 27 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
18168 | 28 |
|
20175 | 29 |
val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool |
30 |
val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a |
|
31 |
(*'a must not keep any reference to theory*) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
32 |
|
21954 | 33 |
(* experimental class target *) |
22302 | 34 |
val class_of_locale: theory -> string -> class option |
35 |
val add_def_in_class: local_theory -> string |
|
36 |
-> (string * Syntax.mixfix) * ((string * Attrib.src list) * (term * thm)) -> theory -> theory |
|
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 |
|
22302 | 48 |
fun fork_mixfix is_class is_loc mx = |
49 |
let |
|
50 |
val mx' = Syntax.unlocalize_mixfix mx; |
|
51 |
val mx1 = if is_class orelse (is_loc andalso mx = mx') then NoSyn else mx'; |
|
52 |
val mx2 = if is_loc then mx else NoSyn; |
|
53 |
in (mx1, mx2) end; |
|
54 |
||
55 |
||
56 |
(** AxClasses with implicit parameter handling **) |
|
57 |
||
58 |
(* AxClass instances *) |
|
18168 | 59 |
|
21954 | 60 |
local |
19456 | 61 |
|
21954 | 62 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
63 |
let |
|
64 |
fun after_qed' results = |
|
65 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
66 |
in |
|
67 |
thy |
|
68 |
|> ProofContext.init |
|
69 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts) |
|
70 |
end; |
|
18168 | 71 |
|
21954 | 72 |
in |
73 |
||
74 |
val axclass_instance_arity = |
|
75 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
|
76 |
val axclass_instance_sort = |
|
77 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) |
|
78 |
AxClass.add_classrel I o single; |
|
79 |
||
80 |
end; (* local *) |
|
18575 | 81 |
|
19038 | 82 |
|
21954 | 83 |
(* introducing axclasses with implicit parameter handling *) |
19038 | 84 |
|
21954 | 85 |
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms thy = |
21924 | 86 |
let |
21954 | 87 |
val superclasses = map (Sign.certify_class thy) raw_superclasses; |
88 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; |
|
89 |
fun add_const ((c, ty), syn) = |
|
90 |
Sign.add_consts_authentic [(c, ty, syn)] #> pair (Sign.full_name thy c, ty); |
|
91 |
fun mk_axioms cs thy = |
|
92 |
raw_dep_axioms thy cs |
|
93 |
|> (map o apsnd o map) (Sign.cert_prop thy) |
|
94 |
|> rpair thy; |
|
95 |
fun add_constraint class (c, ty) = |
|
96 |
Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty)); |
|
21924 | 97 |
in |
21954 | 98 |
thy |
99 |
|> fold_map add_const consts |
|
100 |
|-> (fn cs => mk_axioms cs |
|
22302 | 101 |
#-> (fn axioms_prop => AxClass.define_class_i (name, superclasses) (map fst cs) axioms_prop |
21954 | 102 |
#-> (fn class => `(fn thy => AxClass.get_definition thy class) |
103 |
#-> (fn {intro, axioms, ...} => fold (add_constraint class) cs |
|
22302 | 104 |
#> pair (class, ((intro, (map snd axioms_prop, axioms)), cs)))))) |
21924 | 105 |
end; |
106 |
||
18168 | 107 |
|
21954 | 108 |
(* contexts with arity assumptions *) |
20175 | 109 |
|
110 |
fun assume_arities_of_sort thy arities ty_sort = |
|
111 |
let |
|
112 |
val pp = Sign.pp thy; |
|
113 |
val algebra = Sign.classes_of thy |
|
114 |
|> fold (fn ((tyco, asorts), sort) => |
|
115 |
Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities; |
|
116 |
in Sorts.of_sort algebra ty_sort end; |
|
117 |
||
118 |
fun assume_arities_thy thy arities f = |
|
119 |
let |
|
120 |
val thy_read = (fold (fn ((tyco, asorts), sort) |
|
121 |
=> Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy |
|
122 |
in f thy_read end; |
|
123 |
||
20455 | 124 |
|
21954 | 125 |
(* instances with implicit parameter handling *) |
19038 | 126 |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
127 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
128 |
|
20175 | 129 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
19038 | 130 |
let |
19957 | 131 |
val (_, t) = read_def thy (raw_name, raw_t); |
132 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
133 |
val atts = map (prep_att thy) raw_atts; |
|
20175 | 134 |
val insts = (Consts.typargs (Sign.consts_of thy) (c, ty)) |
19957 | 135 |
val name = case raw_name |
20385 | 136 |
of "" => NONE |
137 |
| _ => SOME raw_name; |
|
20175 | 138 |
in (c, (insts, ((name, t), atts))) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
139 |
|
22302 | 140 |
fun read_def_e thy = gen_read_def thy Attrib.intern_src read_axm; |
21954 | 141 |
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
|
142 |
|
22302 | 143 |
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
|
144 |
let |
20175 | 145 |
fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort); |
146 |
val arities = map prep_arity' raw_arities; |
|
147 |
val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities; |
|
148 |
val _ = if null arities then error "at least one arity must be given" else (); |
|
149 |
val _ = case (duplicates (op =) o map #1) arities |
|
150 |
of [] => () |
|
151 |
| dupl_tycos => error ("type constructors occur more than once in arities: " |
|
152 |
^ (commas o map quote) dupl_tycos); |
|
21954 | 153 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory |
20175 | 154 |
fun get_consts_class tyco ty class = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
155 |
let |
21954 | 156 |
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
|
157 |
val subst_ty = map_type_tfree (K ty); |
19957 | 158 |
in |
21894 | 159 |
map (fn (c, ty) => (c, ((tyco, class), subst_ty ty))) cs |
19957 | 160 |
end; |
20175 | 161 |
fun get_consts_sort (tyco, asorts, sort) = |
19957 | 162 |
let |
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
163 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
21954 | 164 |
in maps (get_consts_class tyco ty) (super_sort sort) end; |
20175 | 165 |
val cs = maps get_consts_sort arities; |
20385 | 166 |
fun mk_typnorm thy (ty, ty_sc) = |
167 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
168 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
169 |
| NONE => NONE; |
|
20175 | 170 |
fun read_defs defs cs thy_read = |
171 |
let |
|
19957 | 172 |
fun read raw_def cs = |
173 |
let |
|
20385 | 174 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
175 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
20175 | 176 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
19957 | 177 |
of NONE => error ("superfluous definition for constant " ^ quote c) |
19966 | 178 |
| SOME class_ty => class_ty; |
20385 | 179 |
val name = case name_opt |
20628 | 180 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
20385 | 181 |
| SOME name => name; |
182 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
19966 | 183 |
of NONE => error ("superfluous definition for constant " ^ |
184 |
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
|
185 |
| SOME norm => map_types norm t |
20175 | 186 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
19957 | 187 |
in fold_map read defs cs end; |
20175 | 188 |
val (defs, _) = assume_arities_thy theory arities_pair (read_defs raw_defs cs); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
189 |
fun get_remove_contraint c thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
190 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
191 |
val ty = Sign.the_const_constraint thy c; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
192 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
193 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
194 |
|> Sign.add_const_constraint_i (c, NONE) |
20385 | 195 |
|> pair (c, Logic.unvarifyT ty) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
196 |
end; |
19966 | 197 |
fun add_defs defs thy = |
198 |
thy |
|
22302 | 199 |
|> PureThy.add_defs_i true (map ((apsnd o map) (Attrib.attribute thy) o snd) defs) |
19966 | 200 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
201 |
fun after_qed cs thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
202 |
thy |
19412 | 203 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs); |
19038 | 204 |
in |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
205 |
theory |
20385 | 206 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =)) |
19966 | 207 |
||>> add_defs defs |
21954 | 208 |
|-> (fn (cs, _) => do_proof (after_qed cs) arities) |
19038 | 209 |
end; |
210 |
||
22302 | 211 |
fun instance_arity_e' do_proof = gen_instance_arity Sign.read_arity read_def_e do_proof; |
212 |
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof; |
|
21924 | 213 |
fun tactic_proof tac after_qed arities = |
214 |
fold (fn arity => AxClass.prove_arity arity tac) arities |
|
20175 | 215 |
#> after_qed; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
216 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
217 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
218 |
|
21954 | 219 |
val instance_arity_e = instance_arity_e' axclass_instance_arity; |
220 |
val instance_arity = instance_arity' axclass_instance_arity; |
|
221 |
val prove_instance_arity = instance_arity' o tactic_proof; |
|
222 |
||
223 |
end; (* local *) |
|
224 |
||
225 |
||
226 |
||
227 |
(** combining locales and axclasses **) |
|
228 |
||
229 |
(* theory data *) |
|
230 |
||
231 |
datatype class_data = ClassData of { |
|
232 |
locale: string, |
|
233 |
consts: (string * string) list |
|
234 |
(*locale parameter ~> toplevel theory constant*), |
|
22302 | 235 |
witness: Element.witness list, |
21954 | 236 |
propnames: string list, |
22302 | 237 |
(*for ad-hoc instance_in*) |
238 |
primdefs: thm list |
|
21954 | 239 |
(*for experimental class target*) |
240 |
}; |
|
241 |
||
242 |
fun rep_classdata (ClassData c) = c; |
|
243 |
||
22302 | 244 |
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2)); |
245 |
||
21954 | 246 |
structure ClassData = TheoryDataFun ( |
247 |
struct |
|
248 |
val name = "Pure/classes"; |
|
22302 | 249 |
type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*); |
250 |
val empty = (Graph.empty, Symtab.empty); |
|
21954 | 251 |
val copy = I; |
252 |
val extend = I; |
|
22302 | 253 |
fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true)); |
21954 | 254 |
fun print _ _ = (); |
255 |
end |
|
256 |
); |
|
257 |
||
258 |
val _ = Context.add_setup ClassData.init; |
|
259 |
||
260 |
||
261 |
(* queries *) |
|
262 |
||
22302 | 263 |
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get; |
264 |
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy); |
|
265 |
||
21954 | 266 |
fun the_class_data thy class = |
267 |
case lookup_class_data thy class |
|
268 |
of NONE => error ("undeclared class " ^ quote class) |
|
269 |
| SOME data => data; |
|
270 |
||
22302 | 271 |
val ancestry = Graph.all_succs o fst o ClassData.get; |
21954 | 272 |
|
22302 | 273 |
fun param_map thy = |
21954 | 274 |
let |
22302 | 275 |
fun params thy class = |
276 |
let |
|
277 |
val const_typs = (#params o AxClass.get_definition thy) class; |
|
278 |
val const_names = (#consts o the_class_data thy) class; |
|
279 |
in |
|
280 |
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names |
|
281 |
end; |
|
282 |
in maps (params thy) o ancestry thy end; |
|
21954 | 283 |
|
22302 | 284 |
val the_witness = #witness oo the_class_data; |
21954 | 285 |
val the_propnames = #propnames oo the_class_data; |
286 |
||
287 |
fun print_classes thy = |
|
288 |
let |
|
289 |
val algebra = Sign.classes_of thy; |
|
290 |
val arities = |
|
291 |
Symtab.empty |
|
292 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
293 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
294 |
((#arities o Sorts.rep_algebra) algebra); |
|
295 |
val the_arities = these o Symtab.lookup arities; |
|
296 |
fun mk_arity class tyco = |
|
297 |
let |
|
298 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
299 |
in Sign.pretty_arity thy (tyco, Ss, [class]) end; |
|
300 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
|
301 |
^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty); |
|
302 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
|
303 |
(SOME o Pretty.str) ("class " ^ class ^ ":"), |
|
304 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
|
305 |
(Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class], |
|
306 |
Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class), |
|
307 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param |
|
308 |
o these o Option.map #params o try (AxClass.get_definition thy)) class, |
|
309 |
(SOME o Pretty.block o Pretty.breaks) [ |
|
310 |
Pretty.str "instances:", |
|
311 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
312 |
] |
|
313 |
] |
|
314 |
in |
|
315 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes) |
|
316 |
algebra |
|
317 |
end; |
|
318 |
||
319 |
||
320 |
(* updaters *) |
|
321 |
||
22302 | 322 |
fun add_class_data ((class, superclasses), (locale, consts, witness, propnames)) = |
323 |
ClassData.map (fn (gr, tab) => ( |
|
324 |
gr |
|
325 |
|> Graph.new_node (class, ClassData { |
|
21954 | 326 |
locale = locale, |
327 |
consts = consts, |
|
22302 | 328 |
witness = witness, |
21954 | 329 |
propnames = propnames, |
22302 | 330 |
primdefs = []}) |
331 |
|> fold (curry Graph.add_edge class) superclasses, |
|
332 |
tab |
|
333 |
|> Symtab.update (locale, class) |
|
334 |
)); |
|
335 |
||
336 |
fun add_primdef (class, thm) thy = |
|
337 |
(ClassData.map o apfst o Graph.map_node class) |
|
338 |
(fn ClassData { locale, consts, witness, propnames, primdefs } => ClassData { locale = locale, |
|
339 |
consts = consts, witness = witness, propnames = propnames, primdefs = thm :: primdefs }); |
|
340 |
||
21954 | 341 |
|
22302 | 342 |
(* exporting terms and theorems to global toplevel *) |
343 |
(*FIXME should also be used when introducing classes*) |
|
344 |
||
345 |
fun globalize thy classes = |
|
346 |
let |
|
347 |
val consts = param_map thy classes; |
|
348 |
val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) classes; |
|
349 |
val subst_typ = Term.map_type_tfree (fn var as (v, sort) => |
|
350 |
if v = AxClass.param_tyvarname then TFree (v, constrain_sort sort) else TFree var) |
|
351 |
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v |
|
352 |
of SOME (c, _) => Const (c, ty) |
|
353 |
| NONE => t) |
|
354 |
| subst_aterm t = t; |
|
355 |
in (subst_typ, map_types subst_typ #> Term.map_aterms subst_aterm) end; |
|
356 |
||
357 |
val global_term = snd oo globalize |
|
21954 | 358 |
|
359 |
||
360 |
(* tactics and methods *) |
|
361 |
||
362 |
fun intro_classes_tac facts st = |
|
22182 | 363 |
let |
364 |
val thy = Thm.theory_of_thm st; |
|
365 |
val ctxt = ProofContext.init thy; |
|
366 |
val intro_classes_tac = ALLGOALS |
|
367 |
(Method.insert_tac facts THEN' REPEAT_ALL_NEW (resolve_tac (AxClass.class_intros thy))) |
|
368 |
THEN Tactic.distinct_subgoals_tac; |
|
369 |
val intro_locales_tac = Locale.intro_locales_tac true ctxt facts; |
|
370 |
in |
|
371 |
(intro_classes_tac THEN TRY intro_locales_tac) st |
|
372 |
end; |
|
21954 | 373 |
|
374 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
|
375 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
|
376 |
||
377 |
fun default_tac rules ctxt facts = |
|
378 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
379 |
default_intro_classes_tac facts; |
|
380 |
||
381 |
val _ = Context.add_setup (Method.add_methods |
|
382 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
|
383 |
"back-chain introduction rules of classes"), |
|
384 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
385 |
"apply some intro/elim rule")]); |
|
386 |
||
387 |
||
22069 | 388 |
(* FIXME workarounds for locale package *) |
21954 | 389 |
|
390 |
fun prove_interpretation (prfx, atts) expr insts tac thy = |
|
391 |
let |
|
392 |
fun ad_hoc_term (Const (c, ty)) = |
|
393 |
let |
|
394 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_typ thy))) ty; |
|
395 |
val s = c ^ "::" ^ Pretty.output p; |
|
396 |
in s end |
|
397 |
| ad_hoc_term t = |
|
398 |
let |
|
399 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_term thy))) t; |
|
400 |
val s = Pretty.output p; |
|
401 |
in s end; |
|
402 |
in |
|
403 |
thy |
|
404 |
|> Locale.interpretation I (prfx, atts) expr (map (Option.map ad_hoc_term) insts) |
|
405 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
406 |
|> ProofContext.theory_of |
|
407 |
end; |
|
408 |
||
22069 | 409 |
fun prove_interpretation_in tac after_qed (name, expr) thy = |
410 |
thy |
|
411 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr) |
|
412 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
413 |
|> ProofContext.theory_of; |
|
414 |
||
415 |
fun instance_sort' do_proof (class, sort) theory = |
|
416 |
let |
|
417 |
val loc_name = (#locale o the_class_data theory) class; |
|
418 |
val loc_expr = |
|
419 |
(Locale.Merge o map (Locale.Locale o #locale o the_class_data theory)) sort; |
|
420 |
val const_names = (map (NameSpace.base o snd) |
|
421 |
o maps (#consts o the_class_data theory) |
|
22302 | 422 |
o ancestry theory) [class]; |
22069 | 423 |
fun get_thms thy = |
22302 | 424 |
ancestry thy sort |
22069 | 425 |
|> AList.make (the_propnames thy) |
426 |
|> map (apsnd (map (NameSpace.append (Logic.const_of_class loc_name)))) |
|
427 |
|> map_filter (fn (superclass, thm_names) => |
|
428 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
429 |
of [] => NONE |
|
430 |
| thms => SOME (superclass, thms)); |
|
431 |
fun after_qed thy = |
|
432 |
thy |
|
433 |
|> `get_thms |
|
434 |
|-> fold (fn (supclass, thms) => I |
|
435 |
AxClass.prove_classrel (class, supclass) |
|
436 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
437 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
438 |
in |
|
439 |
theory |
|
440 |
|> do_proof after_qed (loc_name, loc_expr) |
|
441 |
end; |
|
442 |
||
22074 | 443 |
val prove_instance_sort = instance_sort' o prove_interpretation_in; |
444 |
||
22069 | 445 |
|
446 |
(* classes and instances *) |
|
447 |
||
448 |
local |
|
449 |
||
450 |
fun add_axclass (name, supsort) params axs thy = |
|
451 |
let |
|
452 |
val (c, thy') = thy |
|
453 |
|> AxClass.define_class_i (name, supsort) params axs; |
|
454 |
val {intro, axioms, ...} = AxClass.get_definition thy' c; |
|
455 |
in ((c, (intro, axioms)), thy') end; |
|
456 |
||
457 |
fun certify_class thy class = |
|
458 |
tap (the_class_data thy) (Sign.certify_class thy class); |
|
459 |
||
460 |
fun read_class thy = |
|
461 |
certify_class thy o Sign.intern_class thy; |
|
462 |
||
21954 | 463 |
fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy = |
464 |
let |
|
465 |
val elems = fold_rev (fn Locale.Elem e => cons e | _ => I) raw_elems []; |
|
22302 | 466 |
(*FIXME add constrains here to elements as hint for locale*) |
21954 | 467 |
val supclasses = map (prep_class thy) raw_supclasses; |
468 |
val supsort = |
|
469 |
supclasses |
|
470 |
|> Sign.certify_sort thy |
|
22302 | 471 |
|> (fn [] => Sign.defaultS thy | S => S); (*FIXME Why syntax defaultS?*) |
21954 | 472 |
val supexpr = Locale.Merge (map (Locale.Locale o #locale o the_class_data thy) supclasses); |
473 |
val supconsts = AList.make |
|
22302 | 474 |
(the o AList.lookup (op =) (param_map thy supclasses)) |
21954 | 475 |
((map (fst o fst) o Locale.parameters_of_expr thy) supexpr); |
476 |
fun check_locale thy name_locale = |
|
477 |
let |
|
478 |
val tfrees = |
|
479 |
[] |
|
480 |
|> fold (fold Term.add_tfrees o snd) (Locale.global_asms_of thy name_locale) |
|
481 |
|> fold (Term.add_tfreesT o snd o fst) (Locale.parameters_of thy name_locale); |
|
482 |
in case tfrees |
|
483 |
of [(_, _)] => () |
|
484 |
(*| [(_, sort)] => error ("Additional sort constraint on class variable: " |
|
485 |
^ Sign.string_of_sort thy sort) FIXME what to do about this?*) |
|
486 |
| [] => error ("No type variable in class specification") |
|
487 |
| tfrees => error ("More than one type variable in class specification: " ^ |
|
488 |
(commas o map fst) tfrees) |
|
489 |
end; |
|
490 |
fun extract_params thy name_locale = |
|
491 |
Locale.parameters_of thy name_locale |
|
492 |
|> (map o apfst o apsnd o Term.map_type_tfree) (K (TFree (AxClass.param_tyvarname, []))) |
|
22302 | 493 |
|> (map o apsnd) (fork_mixfix false true #> fst) |
21954 | 494 |
|> chop (length supconsts) |
495 |
|> snd; |
|
496 |
fun extract_assumes name_locale params thy cs = |
|
497 |
let |
|
498 |
val consts = supconsts @ (map (fst o fst) params ~~ cs); |
|
499 |
(*FIXME is this type handling correct?*) |
|
500 |
fun subst (Free (c, ty)) = |
|
22048 | 501 |
Const ((fst o the o AList.lookup (op =) consts) c, ty) |
502 |
| subst t = t; |
|
21954 | 503 |
fun prep_asm ((name, atts), ts) = |
504 |
(*FIXME*) |
|
505 |
((NameSpace.base name, map (Attrib.attribute thy) atts), (map o map_aterms) subst ts); |
|
506 |
in |
|
22182 | 507 |
Locale.global_asms_of thy name_locale |
21954 | 508 |
|> map prep_asm |
509 |
end; |
|
22048 | 510 |
fun extract_axiom_names thy name_locale = |
511 |
name_locale |
|
22182 | 512 |
|> Locale.global_asms_of thy |
22048 | 513 |
|> map (NameSpace.base o fst o fst) (*FIXME*) |
21954 | 514 |
fun mk_const thy class (c, ty) = |
515 |
Const (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty); |
|
516 |
in |
|
517 |
thy |
|
518 |
|> add_locale bname supexpr elems |
|
519 |
|-> (fn name_locale => ProofContext.theory_result ( |
|
520 |
tap (fn thy => check_locale thy name_locale) |
|
521 |
#> `(fn thy => extract_params thy name_locale) |
|
522 |
#-> (fn params => |
|
523 |
axclass_params (bname, supsort) params (extract_assumes name_locale params) |
|
22302 | 524 |
#-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) => |
22048 | 525 |
`(fn thy => extract_axiom_names thy name_locale) |
526 |
#-> (fn axiom_names => |
|
527 |
add_class_data ((name_axclass, supclasses), |
|
22302 | 528 |
(name_locale, map (fst o fst) params ~~ map fst consts, |
529 |
map2 Element.make_witness (map Logic.mk_conjunction_list ax_terms) ax_axioms, axiom_names)) |
|
21954 | 530 |
#> prove_interpretation (Logic.const_of_class bname, []) |
22182 | 531 |
(Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass) (map snd supconsts @ consts)) |
21954 | 532 |
((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
533 |
#> pair name_axclass |
|
22048 | 534 |
))))) |
21954 | 535 |
end; |
536 |
||
537 |
in |
|
538 |
||
22182 | 539 |
val class_e = gen_class (Locale.add_locale true) read_class; |
540 |
val class = gen_class (Locale.add_locale_i true) certify_class; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
541 |
|
20455 | 542 |
end; (*local*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
543 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
544 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
545 |
|
21954 | 546 |
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory = |
547 |
let |
|
548 |
val class = prep_class theory raw_class; |
|
549 |
val sort = prep_sort theory raw_sort; |
|
22302 | 550 |
val is_class = is_some o lookup_class_data theory; |
551 |
in if is_class class andalso forall is_class sort then |
|
21954 | 552 |
theory |
553 |
|> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort) |
|
554 |
else case sort |
|
555 |
of [class'] => |
|
556 |
theory |
|
557 |
|> axclass_instance_sort (class, class') |
|
558 |
| _ => error ("Exactly one class expected: " ^ Sign.string_of_sort theory sort) |
|
559 |
end; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
560 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
561 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
562 |
|
21954 | 563 |
val instance_sort_e = gen_instance_sort Sign.read_class Sign.read_sort; |
564 |
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
565 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
566 |
end; (* local *) |
19038 | 567 |
|
19957 | 568 |
|
21954 | 569 |
(** experimental class target **) |
570 |
||
22302 | 571 |
fun add_def_in_class lthy loc ((c, syn), ((name, atts), (rhs, eq))) thy = |
21954 | 572 |
let |
22302 | 573 |
val SOME class = class_of_locale thy loc; |
574 |
val rhs' = global_term thy [class] rhs; |
|
575 |
val (syn', _) = fork_mixfix true true syn; |
|
576 |
val ty = Term.fastype_of rhs'; |
|
577 |
fun add_const (c, ty, syn) = |
|
578 |
Sign.add_consts_authentic [(c, ty, syn)] |
|
579 |
#> pair (Sign.full_name thy c, ty); |
|
580 |
fun add_def ((name, atts), prop) thy = |
|
581 |
thy |
|
582 |
|> PureThy.add_defs_i false [((name, prop), map (Attrib.attribute thy) atts)] |
|
583 |
|>> the_single; |
|
584 |
(*val _ = Output.info "------ DEF ------"; |
|
585 |
val _ = Output.info c; |
|
586 |
val _ = Output.info name; |
|
587 |
val _ = (Output.info o Sign.string_of_term thy) rhs'; |
|
588 |
val eq' = Morphism.thm (LocalTheory.target_morphism lthy) eq; |
|
589 |
val _ = (Output.info o string_of_thm) eq; |
|
590 |
val _ = (Output.info o string_of_thm) eq'; |
|
591 |
val witness = the_witness thy class; |
|
592 |
val _ = Output.info "------ WIT ------"; |
|
593 |
fun print_wit (t, thm) = "(" ^ Sign.string_of_term thy t ^ ", " ^ Display.string_of_thm thm ^ ")" |
|
594 |
val _ = (Output.info o cat_lines o map (print_wit o Element.dest_witness)) witness; |
|
595 |
val _ = (Output.info o string_of_thm) eq'; |
|
596 |
val eq'' = Element.satisfy_thm witness eq'; |
|
597 |
val _ = (Output.info o string_of_thm) eq'';*) |
|
21954 | 598 |
in |
22302 | 599 |
thy |
600 |
(*|> add_const (c, ty, syn') |
|
601 |
|-> (fn c => add_def ((name, atts), Logic.mk_equals (Const c, rhs'))) |
|
602 |
|-> (fn global_def_thm => tap (fn _ => (Output.info o string_of_thm) global_def_thm))*) |
|
21954 | 603 |
end; |
604 |
||
20455 | 605 |
end; |