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