author | haftmann |
Wed, 28 Nov 2007 09:01:42 +0100 | |
changeset 25485 | 33840a854e63 |
parent 25462 | dad0291cb76a |
child 25502 | 9200b36280c0 |
permissions | -rw-r--r-- |
24218 | 1 |
(* Title: Pure/Isar/class.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Type classes derived from primitive axclasses and locales. |
|
6 |
*) |
|
7 |
||
8 |
signature CLASS = |
|
9 |
sig |
|
25462 | 10 |
(*classes*) |
25002 | 11 |
val class: bstring -> class list -> Element.context_i Locale.element list |
24218 | 12 |
-> string list -> theory -> string * Proof.context |
25002 | 13 |
val class_cmd: bstring -> xstring list -> Element.context Locale.element list |
24589 | 14 |
-> xstring list -> theory -> string * Proof.context |
25485 | 15 |
|
25311 | 16 |
val init: class -> theory -> Proof.context |
25485 | 17 |
val logical_const: string -> Markup.property list |
25104 | 18 |
-> (string * mixfix) * term -> theory -> theory |
25485 | 19 |
val syntactic_const: string -> Syntax.mode -> Markup.property list |
25104 | 20 |
-> (string * mixfix) * term -> theory -> theory |
25083 | 21 |
val refresh_syntax: class -> Proof.context -> Proof.context |
25485 | 22 |
|
24589 | 23 |
val intro_classes_tac: thm list -> tactic |
24 |
val default_intro_classes_tac: thm list -> tactic |
|
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
25 |
val prove_subclass: class * class -> thm list -> Proof.context |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
26 |
-> theory -> theory |
25485 | 27 |
|
28 |
val class_prefix: string -> string |
|
29 |
val is_class: theory -> class -> bool |
|
30 |
val these_params: theory -> sort -> (string * (string * typ)) list |
|
24589 | 31 |
val print_classes: theory -> unit |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
32 |
|
25462 | 33 |
(*instances*) |
25485 | 34 |
val init_instantiation: arity list -> theory -> local_theory |
35 |
val instantiation_instance: (local_theory -> local_theory) -> local_theory -> Proof.state |
|
36 |
val prove_instantiation_instance: (Proof.context -> tactic) -> local_theory -> local_theory |
|
37 |
val conclude_instantiation: local_theory -> local_theory |
|
38 |
||
39 |
val overloaded_const: string * typ * mixfix -> theory -> term * theory |
|
40 |
val overloaded_def: string -> string * term -> theory -> thm * theory |
|
41 |
val instantiation_param: Proof.context -> string -> string option |
|
42 |
val confirm_declaration: string -> local_theory -> local_theory |
|
43 |
||
44 |
val unoverload: theory -> thm -> thm |
|
45 |
val overload: theory -> thm -> thm |
|
46 |
val unoverload_conv: theory -> conv |
|
47 |
val overload_conv: theory -> conv |
|
25462 | 48 |
val unoverload_const: theory -> string * typ -> string |
25485 | 49 |
val param_of_inst: theory -> string * string -> string |
50 |
val inst_of_param: theory -> string -> (string * string) option |
|
25462 | 51 |
|
52 |
(*old axclass layer*) |
|
53 |
val axclass_cmd: bstring * xstring list |
|
54 |
-> ((bstring * Attrib.src list) * string list) list |
|
55 |
-> theory -> class * theory |
|
56 |
val classrel_cmd: xstring * xstring -> theory -> Proof.state |
|
57 |
||
58 |
(*old instance layer*) |
|
24589 | 59 |
val instance_arity: (theory -> theory) -> arity list -> theory -> Proof.state |
60 |
val instance: arity list -> ((bstring * Attrib.src list) * term) list |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
61 |
-> (thm list -> theory -> theory) |
24218 | 62 |
-> theory -> Proof.state |
24589 | 63 |
val instance_cmd: (bstring * xstring list * xstring) list |
64 |
-> ((bstring * Attrib.src list) * xstring) list |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
65 |
-> (thm list -> theory -> theory) |
24218 | 66 |
-> theory -> Proof.state |
24589 | 67 |
val prove_instance: tactic -> arity list |
24218 | 68 |
-> ((bstring * Attrib.src list) * term) list |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
69 |
-> theory -> thm list * theory |
24218 | 70 |
end; |
71 |
||
72 |
structure Class : CLASS = |
|
73 |
struct |
|
74 |
||
75 |
(** auxiliary **) |
|
76 |
||
25062 | 77 |
val classN = "class"; |
78 |
val introN = "intro"; |
|
79 |
||
25002 | 80 |
fun prove_interpretation tac prfx_atts expr inst = |
81 |
Locale.interpretation_i I prfx_atts expr inst |
|
24589 | 82 |
#> Proof.global_terminal_proof |
83 |
(Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) |
|
84 |
#> ProofContext.theory_of; |
|
85 |
||
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
86 |
fun prove_interpretation_in tac after_qed (name, expr) = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
87 |
Locale.interpretation_in_locale |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
88 |
(ProofContext.theory after_qed) (name, expr) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
89 |
#> Proof.global_terminal_proof |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
90 |
(Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
91 |
#> ProofContext.theory_of; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
92 |
|
25020 | 93 |
fun OF_LAST thm1 thm2 = thm1 RSN (Thm.nprems_of thm2, thm2); |
24589 | 94 |
|
95 |
fun strip_all_ofclass thy sort = |
|
96 |
let |
|
24847 | 97 |
val typ = TVar ((Name.aT, 0), sort); |
24589 | 98 |
fun prem_inclass t = |
99 |
case Logic.strip_imp_prems t |
|
100 |
of ofcls :: _ => try Logic.dest_inclass ofcls |
|
101 |
| [] => NONE; |
|
102 |
fun strip_ofclass class thm = |
|
103 |
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache; |
|
104 |
fun strip thm = case (prem_inclass o Thm.prop_of) thm |
|
105 |
of SOME (_, class) => thm |> strip_ofclass class |> strip |
|
106 |
| NONE => thm; |
|
107 |
in strip end; |
|
108 |
||
25038 | 109 |
fun get_remove_global_constraint c thy = |
110 |
let |
|
111 |
val ty = Sign.the_const_constraint thy c; |
|
112 |
in |
|
113 |
thy |
|
114 |
|> Sign.add_const_constraint (c, NONE) |
|
115 |
|> pair (c, Logic.unvarifyT ty) |
|
116 |
end; |
|
117 |
||
24589 | 118 |
|
25485 | 119 |
(** primitive axclass and instance commands **) |
24589 | 120 |
|
24218 | 121 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy = |
122 |
let |
|
123 |
val ctxt = ProofContext.init thy; |
|
124 |
val superclasses = map (Sign.read_class thy) raw_superclasses; |
|
24589 | 125 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) |
126 |
raw_specs; |
|
127 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) |
|
128 |
raw_specs) |
|
24218 | 129 |
|> snd |
130 |
|> (map o map) fst; |
|
24589 | 131 |
in |
132 |
AxClass.define_class (class, superclasses) [] |
|
133 |
(name_atts ~~ axiomss) thy |
|
134 |
end; |
|
24218 | 135 |
|
136 |
local |
|
137 |
||
138 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
|
139 |
let |
|
140 |
fun after_qed' results = |
|
141 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
142 |
in |
|
143 |
thy |
|
144 |
|> ProofContext.init |
|
24589 | 145 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) |
146 |
o maps (mk_prop thy)) insts) |
|
24218 | 147 |
end; |
148 |
||
149 |
in |
|
150 |
||
24589 | 151 |
val instance_arity = |
24218 | 152 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
24589 | 153 |
val classrel = |
24218 | 154 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) |
155 |
AxClass.add_classrel I o single; |
|
24589 | 156 |
val classrel_cmd = |
157 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) |
|
158 |
AxClass.add_classrel I o single; |
|
24218 | 159 |
|
160 |
end; (*local*) |
|
161 |
||
162 |
||
25462 | 163 |
(** basic overloading **) |
164 |
||
165 |
(* bookkeeping *) |
|
24304 | 166 |
|
167 |
structure InstData = TheoryDataFun |
|
168 |
( |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
169 |
type T = (string * thm) Symtab.table Symtab.table * (string * string) Symtab.table; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
170 |
(*constant name ~> type constructor ~> (constant name, equation), |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
171 |
constant name ~> (constant name, type constructor)*) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
172 |
val empty = (Symtab.empty, Symtab.empty); |
24304 | 173 |
val copy = I; |
174 |
val extend = I; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
175 |
fun merge _ ((taba1, tabb1), (taba2, tabb2)) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
176 |
(Symtab.join (K (Symtab.merge (K true))) (taba1, taba2), |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
177 |
Symtab.merge (K true) (tabb1, tabb2)); |
24304 | 178 |
); |
179 |
||
25462 | 180 |
val inst_tyco = Option.map fst o try (dest_Type o the_single) oo Sign.const_typargs; |
181 |
||
182 |
fun inst thy (c, tyco) = |
|
183 |
(the o Symtab.lookup ((the o Symtab.lookup (fst (InstData.get thy))) c)) tyco; |
|
184 |
||
25485 | 185 |
val param_of_inst = fst oo inst; |
25462 | 186 |
|
25020 | 187 |
fun inst_thms thy = (Symtab.fold (Symtab.fold (cons o snd o snd) o snd) o fst) |
25462 | 188 |
(InstData.get thy) []; |
189 |
||
25485 | 190 |
val inst_of_param = Symtab.lookup o snd o InstData.get; |
25462 | 191 |
|
24589 | 192 |
fun add_inst (c, tyco) inst = (InstData.map o apfst |
193 |
o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst)) |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
194 |
#> (InstData.map o apsnd) (Symtab.update_new (fst inst, (c, tyco))); |
24304 | 195 |
|
25485 | 196 |
fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy); |
197 |
fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy)); |
|
198 |
||
199 |
fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy); |
|
200 |
fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy)); |
|
24304 | 201 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
202 |
fun unoverload_const thy (c_ty as (c, _)) = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
203 |
case AxClass.class_of_param thy c |
25462 | 204 |
of SOME class => (case inst_tyco thy c_ty |
25485 | 205 |
of SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c |
25462 | 206 |
| NONE => c) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
207 |
| NONE => c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
208 |
|
25462 | 209 |
|
210 |
(* declaration and definition of instances of overloaded constants *) |
|
211 |
||
212 |
fun primitive_note kind (name, thm) = |
|
213 |
PureThy.note_thmss_i kind [((name, []), [([thm], [])])] |
|
214 |
#>> (fn [(_, [thm])] => thm); |
|
215 |
||
25485 | 216 |
fun overloaded_const (c, ty, mx) thy = |
25462 | 217 |
let |
25485 | 218 |
val _ = if mx <> NoSyn then |
219 |
error ("Illegal mixfix syntax for constant to be instantiated " ^ quote c) |
|
220 |
else () |
|
25462 | 221 |
val SOME class = AxClass.class_of_param thy c; |
222 |
val SOME tyco = inst_tyco thy (c, ty); |
|
25485 | 223 |
val name_inst = AxClass.instance_name (tyco, class) ^ "_inst"; |
25462 | 224 |
val c' = NameSpace.base c; |
225 |
val ty' = Type.strip_sorts ty; |
|
226 |
in |
|
227 |
thy |
|
228 |
|> Sign.sticky_prefix name_inst |
|
229 |
|> Sign.no_base_names |
|
230 |
|> Sign.declare_const [] (c', ty', NoSyn) |
|
231 |
|-> (fn const' as Const (c'', _) => Thm.add_def true |
|
232 |
(Thm.def_name c', Logic.mk_equals (Const (c, ty'), const')) |
|
233 |
#>> Thm.varifyT |
|
234 |
#-> (fn thm => add_inst (c, tyco) (c'', thm) |
|
235 |
#> primitive_note Thm.internalK (c', thm) |
|
236 |
#> snd |
|
237 |
#> Sign.restore_naming thy |
|
238 |
#> pair (Const (c, ty)))) |
|
239 |
end; |
|
240 |
||
25485 | 241 |
fun overloaded_def name (c, t) thy = |
25462 | 242 |
let |
243 |
val ty = Term.fastype_of t; |
|
244 |
val SOME tyco = inst_tyco thy (c, ty); |
|
245 |
val (c', eq) = inst thy (c, tyco); |
|
246 |
val prop = Logic.mk_equals (Const (c', ty), t); |
|
25485 | 247 |
val name' = Thm.def_name_optional |
248 |
(NameSpace.base c ^ "_" ^ NameSpace.base tyco) name; |
|
25462 | 249 |
in |
250 |
thy |
|
251 |
|> Thm.add_def false (name', prop) |
|
25485 | 252 |
|>> (fn thm => Drule.transitive_thm OF [eq, thm]) |
25462 | 253 |
end; |
254 |
||
255 |
||
256 |
(* legacy *) |
|
24304 | 257 |
|
258 |
fun add_inst_def (class, tyco) (c, ty) thy = |
|
259 |
let |
|
25024 | 260 |
val tyco_base = Sign.base_name tyco; |
261 |
val name_inst = Sign.base_name class ^ "_" ^ tyco_base ^ "_inst"; |
|
262 |
val c_inst_base = Sign.base_name c ^ "_" ^ tyco_base; |
|
24304 | 263 |
in |
264 |
thy |
|
265 |
|> Sign.sticky_prefix name_inst |
|
25020 | 266 |
|> Sign.declare_const [] (c_inst_base, ty, NoSyn) |
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
267 |
|-> (fn const as Const (c_inst, _) => |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
268 |
PureThy.add_defs_i false |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
269 |
[((Thm.def_name c_inst_base, Logic.mk_equals (const, Const (c, ty))), [])] |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
270 |
#-> (fn [thm] => add_inst (c, tyco) (c_inst, Thm.symmetric thm))) |
24435 | 271 |
|> Sign.restore_naming thy |
24304 | 272 |
end; |
273 |
||
274 |
fun add_inst_def' (class, tyco) (c, ty) thy = |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
275 |
if case Symtab.lookup (fst (InstData.get thy)) c |
24304 | 276 |
of NONE => true |
277 |
| SOME tab => is_none (Symtab.lookup tab tyco) |
|
278 |
then add_inst_def (class, tyco) (c, Logic.unvarifyT ty) thy |
|
279 |
else thy; |
|
280 |
||
281 |
fun add_def ((class, tyco), ((name, prop), atts)) thy = |
|
282 |
let |
|
24589 | 283 |
val ((lhs as Const (c, ty), args), rhs) = |
284 |
(apfst Term.strip_comb o Logic.dest_equals) prop; |
|
24304 | 285 |
in |
286 |
thy |
|
25020 | 287 |
|> PureThy.add_defs_i true [((name, prop), map (Attrib.attribute_i thy) atts)] |
25462 | 288 |
|-> (fn [def] => add_inst_def (class, tyco) (c, ty) #> pair def) |
24304 | 289 |
end; |
290 |
||
291 |
||
24589 | 292 |
(** instances with implicit parameter handling **) |
24218 | 293 |
|
294 |
local |
|
295 |
||
24707 | 296 |
fun gen_read_def thy prep_att parse_prop ((raw_name, raw_atts), raw_t) = |
24218 | 297 |
let |
24707 | 298 |
val ctxt = ProofContext.init thy; |
299 |
val t = parse_prop ctxt raw_t |> Syntax.check_prop ctxt; |
|
24981
4ec3f95190bf
dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents:
24968
diff
changeset
|
300 |
val ((c, ty), _) = Sign.cert_def ctxt t; |
24218 | 301 |
val atts = map (prep_att thy) raw_atts; |
302 |
val insts = Consts.typargs (Sign.consts_of thy) (c, ty); |
|
303 |
val name = case raw_name |
|
304 |
of "" => NONE |
|
305 |
| _ => SOME raw_name; |
|
306 |
in (c, (insts, ((name, t), atts))) end; |
|
307 |
||
24707 | 308 |
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Syntax.parse_prop; |
24218 | 309 |
fun read_def thy = gen_read_def thy (K I) (K I); |
310 |
||
24589 | 311 |
fun gen_instance prep_arity read_def do_proof raw_arities raw_defs after_qed theory = |
24218 | 312 |
let |
313 |
val arities = map (prep_arity theory) raw_arities; |
|
25020 | 314 |
val _ = if null arities then error "At least one arity must be given" else (); |
24218 | 315 |
val _ = case (duplicates (op =) o map #1) arities |
316 |
of [] => () |
|
25020 | 317 |
| dupl_tycos => error ("Type constructors occur more than once in arities: " |
318 |
^ commas_quote dupl_tycos); |
|
24218 | 319 |
fun get_consts_class tyco ty class = |
320 |
let |
|
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
321 |
val cs = (these o try (#params o AxClass.get_info theory)) class; |
24218 | 322 |
val subst_ty = map_type_tfree (K ty); |
323 |
in |
|
24304 | 324 |
map (fn (c, ty) => (c, ((class, tyco), subst_ty ty))) cs |
24218 | 325 |
end; |
326 |
fun get_consts_sort (tyco, asorts, sort) = |
|
327 |
let |
|
24589 | 328 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) |
24847 | 329 |
(Name.names Name.context Name.aT asorts)) |
24731 | 330 |
in maps (get_consts_class tyco ty) (Sign.complete_sort theory sort) end; |
24218 | 331 |
val cs = maps get_consts_sort arities; |
332 |
fun mk_typnorm thy (ty, ty_sc) = |
|
333 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
334 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
335 |
| NONE => NONE; |
|
336 |
fun read_defs defs cs thy_read = |
|
337 |
let |
|
338 |
fun read raw_def cs = |
|
339 |
let |
|
340 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
|
341 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
24304 | 342 |
val ((class, tyco), ty') = case AList.lookup (op =) cs c |
25020 | 343 |
of NONE => error ("Illegal definition for constant " ^ quote c) |
24218 | 344 |
| SOME class_ty => class_ty; |
345 |
val name = case name_opt |
|
346 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
|
347 |
| SOME name => name; |
|
348 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
25020 | 349 |
of NONE => error ("Illegal definition for constant " ^ |
24218 | 350 |
quote (c ^ "::" ^ setmp show_sorts true |
351 |
(Sign.string_of_typ thy_read) ty)) |
|
352 |
| SOME norm => map_types norm t |
|
353 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
|
354 |
in fold_map read defs cs end; |
|
24304 | 355 |
val (defs, other_cs) = read_defs raw_defs cs |
24218 | 356 |
(fold Sign.primitive_arity arities (Theory.copy theory)); |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
357 |
fun after_qed' cs defs = |
24766 | 358 |
fold Sign.add_const_constraint (map (apsnd SOME) cs) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
359 |
#> after_qed defs; |
24218 | 360 |
in |
361 |
theory |
|
25038 | 362 |
|> fold_map get_remove_global_constraint (map fst cs |> distinct (op =)) |
24304 | 363 |
||>> fold_map add_def defs |
364 |
||> fold (fn (c, ((class, tyco), ty)) => add_inst_def' (class, tyco) (c, ty)) other_cs |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
365 |
|-> (fn (cs, defs) => do_proof (after_qed' cs defs) arities defs) |
24218 | 366 |
end; |
367 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
368 |
fun tactic_proof tac f arities defs = |
24218 | 369 |
fold (fn arity => AxClass.prove_arity arity tac) arities |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
370 |
#> f |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
371 |
#> pair defs; |
24218 | 372 |
|
373 |
in |
|
374 |
||
24589 | 375 |
val instance = gen_instance Sign.cert_arity read_def |
376 |
(fn f => fn arities => fn defs => instance_arity f arities); |
|
377 |
val instance_cmd = gen_instance Sign.read_arity read_def_cmd |
|
378 |
(fn f => fn arities => fn defs => instance_arity f arities); |
|
379 |
fun prove_instance tac arities defs = |
|
380 |
gen_instance Sign.cert_arity read_def |
|
381 |
(tactic_proof tac) arities defs (K I); |
|
24218 | 382 |
|
383 |
end; (*local*) |
|
384 |
||
385 |
||
386 |
||
24589 | 387 |
(** class data **) |
24218 | 388 |
|
389 |
datatype class_data = ClassData of { |
|
390 |
consts: (string * string) list |
|
24836 | 391 |
(*locale parameter ~> constant name*), |
25062 | 392 |
base_sort: sort, |
25083 | 393 |
inst: term option list |
394 |
(*canonical interpretation*), |
|
25062 | 395 |
morphism: morphism, |
396 |
(*partial morphism of canonical interpretation*) |
|
24657 | 397 |
intro: thm, |
398 |
defs: thm list, |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
399 |
operations: (string * (class * (typ * term))) list |
24657 | 400 |
}; |
24218 | 401 |
|
24657 | 402 |
fun rep_class_data (ClassData d) = d; |
25062 | 403 |
fun mk_class_data ((consts, base_sort, inst, morphism, intro), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
404 |
(defs, operations)) = |
25062 | 405 |
ClassData { consts = consts, base_sort = base_sort, inst = inst, |
406 |
morphism = morphism, intro = intro, defs = defs, |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
407 |
operations = operations }; |
25062 | 408 |
fun map_class_data f (ClassData { consts, base_sort, inst, morphism, intro, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
409 |
defs, operations }) = |
25062 | 410 |
mk_class_data (f ((consts, base_sort, inst, morphism, intro), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
411 |
(defs, operations))); |
25038 | 412 |
fun merge_class_data _ (ClassData { consts = consts, |
25062 | 413 |
base_sort = base_sort, inst = inst, morphism = morphism, intro = intro, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
414 |
defs = defs1, operations = operations1 }, |
25062 | 415 |
ClassData { consts = _, base_sort = _, inst = _, morphism = _, intro = _, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
416 |
defs = defs2, operations = operations2 }) = |
25062 | 417 |
mk_class_data ((consts, base_sort, inst, morphism, intro), |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
418 |
(Thm.merge_thms (defs1, defs2), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
419 |
AList.merge (op =) (K true) (operations1, operations2))); |
24218 | 420 |
|
421 |
structure ClassData = TheoryDataFun |
|
422 |
( |
|
25038 | 423 |
type T = class_data Graph.T |
424 |
val empty = Graph.empty; |
|
24218 | 425 |
val copy = I; |
426 |
val extend = I; |
|
25038 | 427 |
fun merge _ = Graph.join merge_class_data; |
24218 | 428 |
); |
429 |
||
430 |
||
431 |
(* queries *) |
|
432 |
||
25038 | 433 |
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node o ClassData.get; |
24218 | 434 |
|
24589 | 435 |
fun the_class_data thy class = case lookup_class_data thy class |
25020 | 436 |
of NONE => error ("Undeclared class " ^ quote class) |
24589 | 437 |
| SOME data => data; |
24218 | 438 |
|
25038 | 439 |
val is_class = is_some oo lookup_class_data; |
440 |
||
441 |
val ancestry = Graph.all_succs o ClassData.get; |
|
24218 | 442 |
|
25002 | 443 |
fun these_params thy = |
24218 | 444 |
let |
445 |
fun params class = |
|
446 |
let |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
447 |
val const_typs = (#params o AxClass.get_info thy) class; |
24657 | 448 |
val const_names = (#consts o the_class_data thy) class; |
24218 | 449 |
in |
450 |
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names |
|
451 |
end; |
|
452 |
in maps params o ancestry thy end; |
|
453 |
||
24657 | 454 |
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy; |
24218 | 455 |
|
25062 | 456 |
fun morphism thy = #morphism o the_class_data thy; |
457 |
||
24218 | 458 |
fun these_intros thy = |
24657 | 459 |
Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_class_data) data)) |
25038 | 460 |
(ClassData.get thy) []; |
24218 | 461 |
|
24836 | 462 |
fun these_operations thy = |
463 |
maps (#operations o the_class_data thy) o ancestry thy; |
|
24657 | 464 |
|
24218 | 465 |
fun print_classes thy = |
466 |
let |
|
24920 | 467 |
val ctxt = ProofContext.init thy; |
24218 | 468 |
val algebra = Sign.classes_of thy; |
469 |
val arities = |
|
470 |
Symtab.empty |
|
471 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
472 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
473 |
((#arities o Sorts.rep_algebra) algebra); |
|
474 |
val the_arities = these o Symtab.lookup arities; |
|
475 |
fun mk_arity class tyco = |
|
476 |
let |
|
477 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
24920 | 478 |
in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end; |
24218 | 479 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
24920 | 480 |
^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty); |
24218 | 481 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
25062 | 482 |
(SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"), |
24218 | 483 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
24920 | 484 |
(Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class], |
25062 | 485 |
if is_class thy class then (SOME o Pretty.str) |
486 |
("locale: " ^ Locale.extern thy class) else NONE, |
|
487 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) |
|
488 |
(Pretty.str "parameters:" :: ps)) o map mk_param |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
489 |
o these o Option.map #params o try (AxClass.get_info thy)) class, |
24218 | 490 |
(SOME o Pretty.block o Pretty.breaks) [ |
491 |
Pretty.str "instances:", |
|
492 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
493 |
] |
|
494 |
] |
|
495 |
in |
|
24589 | 496 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") |
497 |
o map mk_entry o Sorts.all_classes) algebra |
|
24218 | 498 |
end; |
499 |
||
500 |
||
501 |
(* updaters *) |
|
502 |
||
25163 | 503 |
fun add_class_data ((class, superclasses), (cs, base_sort, inst, phi, intro)) thy = |
25002 | 504 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
505 |
val operations = map (fn (v_ty as (_, ty), (c, _)) => |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
506 |
(c, (class, (ty, Free v_ty)))) cs; |
25002 | 507 |
val cs = (map o pairself) fst cs; |
25038 | 508 |
val add_class = Graph.new_node (class, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
509 |
mk_class_data ((cs, base_sort, map (SOME o Const) inst, phi, intro), ([], operations))) |
25002 | 510 |
#> fold (curry Graph.add_edge class) superclasses; |
511 |
in |
|
25038 | 512 |
ClassData.map add_class thy |
25002 | 513 |
end; |
24218 | 514 |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
515 |
fun register_operation class (c, (t, some_def)) thy = |
25062 | 516 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
517 |
val base_sort = (#base_sort o the_class_data thy) class; |
25239 | 518 |
val prep_typ = map_atyps |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
519 |
(fn TVar (vi as (v, _), sort) => if Name.aT = v |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
520 |
then TFree (v, base_sort) else TVar (vi, sort)); |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
521 |
val t' = map_types prep_typ t; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
522 |
val ty' = Term.fastype_of t'; |
25062 | 523 |
in |
524 |
thy |
|
525 |
|> (ClassData.map o Graph.map_node class o map_class_data o apsnd) |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
526 |
(fn (defs, operations) => |
25096 | 527 |
(fold cons (the_list some_def) defs, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
528 |
(c, (class, (ty', t'))) :: operations)) |
25062 | 529 |
end; |
24218 | 530 |
|
24589 | 531 |
|
532 |
(** rule calculation, tactics and methods **) |
|
533 |
||
25024 | 534 |
val class_prefix = Logic.const_of_class o Sign.base_name; |
535 |
||
25062 | 536 |
fun calculate_morphism class cs = |
537 |
let |
|
538 |
val subst_typ = Term.map_type_tfree (fn var as (v, sort) => |
|
539 |
if v = Name.aT then TVar ((v, 0), [class]) else TVar ((v, 0), sort)); |
|
540 |
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) cs v |
|
541 |
of SOME (c, _) => Const (c, ty) |
|
542 |
| NONE => t) |
|
543 |
| subst_aterm t = t; |
|
544 |
val subst_term = map_aterms subst_aterm #> map_types subst_typ; |
|
545 |
in |
|
25209 | 546 |
Morphism.term_morphism subst_term |
25062 | 547 |
$> Morphism.typ_morphism subst_typ |
548 |
end; |
|
549 |
||
25038 | 550 |
fun class_intro thy class sups = |
24589 | 551 |
let |
552 |
fun class_elim class = |
|
25020 | 553 |
case (#axioms o AxClass.get_info thy) class |
554 |
of [thm] => SOME (Drule.unconstrainTs thm) |
|
24589 | 555 |
| [] => NONE; |
25038 | 556 |
val pred_intro = case Locale.intros thy class |
24589 | 557 |
of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME |
558 |
| ([intro], []) => SOME intro |
|
559 |
| ([], [intro]) => SOME intro |
|
560 |
| _ => NONE; |
|
561 |
val pred_intro' = pred_intro |
|
562 |
|> Option.map (fn intro => intro OF map_filter class_elim sups); |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
563 |
val class_intro = (#intro o AxClass.get_info thy) class; |
24589 | 564 |
val raw_intro = case pred_intro' |
565 |
of SOME pred_intro => class_intro |> OF_LAST pred_intro |
|
566 |
| NONE => class_intro; |
|
567 |
val sort = Sign.super_classes thy class; |
|
24847 | 568 |
val typ = TVar ((Name.aT, 0), sort); |
24589 | 569 |
val defs = these_defs thy sups; |
570 |
in |
|
571 |
raw_intro |
|
572 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] [] |
|
573 |
|> strip_all_ofclass thy sort |
|
574 |
|> Thm.strip_shyps |
|
575 |
|> MetaSimplifier.rewrite_rule defs |
|
576 |
|> Drule.unconstrainTs |
|
577 |
end; |
|
578 |
||
579 |
fun class_interpretation class facts defs thy = |
|
580 |
let |
|
25038 | 581 |
val params = these_params thy [class]; |
25083 | 582 |
val inst = (#inst o the_class_data thy) class; |
25020 | 583 |
val tac = ALLGOALS (ProofContext.fact_tac facts); |
25038 | 584 |
val prfx = class_prefix class; |
24589 | 585 |
in |
25038 | 586 |
thy |
587 |
|> fold_map (get_remove_global_constraint o fst o snd) params |
|
25094
ba43514068fd
Interpretation equations may have name and/or attribute.
ballarin
parents:
25083
diff
changeset
|
588 |
||> prove_interpretation tac ((false, prfx), []) (Locale.Locale class) |
ba43514068fd
Interpretation equations may have name and/or attribute.
ballarin
parents:
25083
diff
changeset
|
589 |
(inst, map (fn def => (("", []), def)) defs) |
25038 | 590 |
|-> (fn cs => fold (Sign.add_const_constraint o apsnd SOME) cs) |
24589 | 591 |
end; |
24218 | 592 |
|
593 |
fun intro_classes_tac facts st = |
|
594 |
let |
|
595 |
val thy = Thm.theory_of_thm st; |
|
596 |
val classes = Sign.all_classes thy; |
|
597 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
598 |
val class_intros = these_intros thy; |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
599 |
val axclass_intros = map_filter (try (#intro o AxClass.get_info thy)) classes; |
24218 | 600 |
in |
25268 | 601 |
Method.intros_tac (class_trivs @ class_intros @ axclass_intros) facts st |
24218 | 602 |
end; |
603 |
||
604 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
605 |
| default_intro_classes_tac _ = no_tac; |
24218 | 606 |
|
607 |
fun default_tac rules ctxt facts = |
|
608 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
609 |
default_intro_classes_tac facts; |
|
610 |
||
611 |
val _ = Context.add_setup (Method.add_methods |
|
612 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
|
613 |
"back-chain introduction rules of classes"), |
|
614 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
615 |
"apply some intro/elim rule")]); |
|
616 |
||
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
617 |
fun subclass_rule thy (sub, sup) = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
618 |
let |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
619 |
val ctxt = Locale.init sub thy; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
620 |
val ctxt_thy = ProofContext.init thy; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
621 |
val props = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
622 |
Locale.global_asms_of thy sup |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
623 |
|> maps snd |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
624 |
|> map (ObjectLogic.ensure_propT thy); |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
625 |
fun tac { prems, context } = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
626 |
Locale.intro_locales_tac true context prems |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
627 |
ORELSE ALLGOALS assume_tac; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
628 |
in |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
629 |
Goal.prove_multi ctxt [] [] props tac |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
630 |
|> map (Assumption.export false ctxt ctxt_thy) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
631 |
|> Variable.export ctxt ctxt_thy |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
632 |
end; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
633 |
|
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
634 |
fun prove_single_subclass (sub, sup) thms ctxt thy = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
635 |
let |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
636 |
val ctxt_thy = ProofContext.init thy; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
637 |
val subclass_rule = Conjunction.intr_balanced thms |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
638 |
|> Assumption.export false ctxt ctxt_thy |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
639 |
|> singleton (Variable.export ctxt ctxt_thy); |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
640 |
val sub_inst = Thm.ctyp_of thy (TVar ((Name.aT, 0), [sub])); |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
641 |
val sub_ax = #axioms (AxClass.get_info thy sub); |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
642 |
val classrel = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
643 |
#intro (AxClass.get_info thy sup) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
644 |
|> Drule.instantiate' [SOME sub_inst] [] |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
645 |
|> OF_LAST (subclass_rule OF sub_ax) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
646 |
|> strip_all_ofclass thy (Sign.super_classes thy sup) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
647 |
|> Thm.strip_shyps |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
648 |
in |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
649 |
thy |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
650 |
|> AxClass.add_classrel classrel |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
651 |
|> prove_interpretation_in (ALLGOALS (ProofContext.fact_tac thms)) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
652 |
I (sub, Locale.Locale sup) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
653 |
|> ClassData.map (Graph.add_edge (sub, sup)) |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
654 |
end; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
655 |
|
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
656 |
fun prove_subclass (sub, sup) thms ctxt thy = |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
657 |
let |
25268 | 658 |
val classes = ClassData.get thy; |
659 |
val is_sup = not o null o curry (Graph.irreducible_paths classes) sub; |
|
660 |
val supclasses = Graph.all_succs classes [sup] |> filter_out is_sup; |
|
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
661 |
fun transform sup' = subclass_rule thy (sup, sup') |> map (fn thm => thm OF thms); |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
662 |
in |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
663 |
thy |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
664 |
|> fold_rev (fn sup' => prove_single_subclass (sub, sup') |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
665 |
(transform sup') ctxt) supclasses |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
666 |
end; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
667 |
|
24218 | 668 |
|
24589 | 669 |
(** classes and class target **) |
24218 | 670 |
|
25002 | 671 |
(* class context syntax *) |
24748 | 672 |
|
25083 | 673 |
structure ClassSyntax = ProofDataFun( |
674 |
type T = { |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
675 |
local_constraints: (string * typ) list, |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
676 |
global_constraints: (string * typ) list, |
25083 | 677 |
base_sort: sort, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
678 |
operations: (string * (typ * term)) list, |
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
679 |
unchecks: (term * term) list, |
25083 | 680 |
passed: bool |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
681 |
}; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
682 |
fun init _ = { |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
683 |
local_constraints = [], |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
684 |
global_constraints = [], |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
685 |
base_sort = [], |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
686 |
operations = [], |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
687 |
unchecks = [], |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
688 |
passed = true |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
689 |
};; |
25083 | 690 |
); |
691 |
||
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
692 |
fun synchronize_syntax sups base_sort ctxt = |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
693 |
let |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
694 |
val thy = ProofContext.theory_of ctxt; |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
695 |
fun subst_class_typ sort = map_atyps |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
696 |
(fn TFree _ => TVar ((Name.aT, 0), sort) | ty' => ty'); |
25083 | 697 |
val operations = these_operations thy sups; |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
698 |
val local_constraints = |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
699 |
(map o apsnd) (subst_class_typ base_sort o fst o snd) operations; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
700 |
val global_constraints = |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
701 |
(map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations; |
25318
c8352b38d47d
synchronize_syntax: declare operations within the local scope of fixes/consts
wenzelm
parents:
25311
diff
changeset
|
702 |
fun declare_const (c, _) = |
c8352b38d47d
synchronize_syntax: declare operations within the local scope of fixes/consts
wenzelm
parents:
25311
diff
changeset
|
703 |
let val b = Sign.base_name c |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
704 |
in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end; |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
705 |
val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) operations; |
25083 | 706 |
in |
707 |
ctxt |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
708 |
|> fold declare_const local_constraints |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
709 |
|> fold (ProofContext.add_const_constraint o apsnd SOME) local_constraints |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
710 |
|> ClassSyntax.put { |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
711 |
local_constraints = local_constraints, |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
712 |
global_constraints = global_constraints, |
25083 | 713 |
base_sort = base_sort, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
714 |
operations = (map o apsnd) snd operations, |
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
715 |
unchecks = unchecks, |
25083 | 716 |
passed = false |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
717 |
} |
25083 | 718 |
end; |
719 |
||
720 |
fun refresh_syntax class ctxt = |
|
25002 | 721 |
let |
722 |
val thy = ProofContext.theory_of ctxt; |
|
25062 | 723 |
val base_sort = (#base_sort o the_class_data thy) class; |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
724 |
in synchronize_syntax [class] base_sort ctxt end; |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
725 |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
726 |
val mark_passed = ClassSyntax.map |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
727 |
(fn { local_constraints, global_constraints, base_sort, operations, unchecks, passed } => |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
728 |
{ local_constraints = local_constraints, global_constraints = global_constraints, |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
729 |
base_sort = base_sort, operations = operations, unchecks = unchecks, passed = true }); |
25083 | 730 |
|
731 |
fun sort_term_check ts ctxt = |
|
24748 | 732 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
733 |
val { local_constraints, global_constraints, base_sort, operations, passed, ... } = |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
734 |
ClassSyntax.get ctxt; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
735 |
fun check_improve (Const (c, ty)) = (case AList.lookup (op =) local_constraints c |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
736 |
of SOME ty0 => (case try (Type.raw_match (ty0, ty)) Vartab.empty |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
737 |
of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
738 |
of SOME (_, TVar (tvar as (vi, _))) => |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
739 |
if TypeInfer.is_param vi then cons tvar else I |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
740 |
| _ => I) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
741 |
| NONE => I) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
742 |
| NONE => I) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
743 |
| check_improve _ = I; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
744 |
val improvements = (fold o fold_aterms) check_improve ts []; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
745 |
val ts' = (map o map_types o map_atyps) (fn ty as TVar tvar => |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
746 |
if member (op =) improvements tvar |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
747 |
then TFree (Name.aT, base_sort) else ty | ty => ty) ts; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
748 |
fun check t0 = Envir.expand_term (fn Const (c, ty) => (case AList.lookup (op =) operations c |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
749 |
of SOME (ty0, t) => |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
750 |
if Type.typ_instance (ProofContext.tsig_of ctxt) (ty, ty0) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
751 |
then SOME (ty0, check t) else NONE |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
752 |
| NONE => NONE) |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
753 |
| _ => NONE) t0; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
754 |
val ts'' = map check ts'; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
755 |
in if eq_list (op aconv) (ts, ts'') andalso passed then NONE |
25083 | 756 |
else |
757 |
ctxt |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
758 |
|> fold (ProofContext.add_const_constraint o apsnd SOME) global_constraints |
25083 | 759 |
|> mark_passed |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
760 |
|> pair ts'' |
25083 | 761 |
|> SOME |
762 |
end; |
|
24748 | 763 |
|
25083 | 764 |
fun sort_term_uncheck ts ctxt = |
25002 | 765 |
let |
766 |
val thy = ProofContext.theory_of ctxt; |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
767 |
val unchecks = (#unchecks o ClassSyntax.get) ctxt; |
25462 | 768 |
val ts' = map (Pattern.rewrite_term thy unchecks []) ts; |
25060 | 769 |
in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end; |
25002 | 770 |
|
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
771 |
fun init_ctxt sups base_sort ctxt = |
25083 | 772 |
ctxt |
773 |
|> Variable.declare_term |
|
774 |
(Logic.mk_type (TFree (Name.aT, base_sort))) |
|
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
775 |
|> synchronize_syntax sups base_sort |
25083 | 776 |
|> Context.proof_map ( |
777 |
Syntax.add_term_check 0 "class" sort_term_check |
|
25103 | 778 |
#> Syntax.add_term_uncheck 0 "class" sort_term_uncheck) |
24901
d3cbf79769b9
added first version of user-space type system for class target
haftmann
parents:
24847
diff
changeset
|
779 |
|
25311 | 780 |
fun init class thy = |
781 |
thy |
|
782 |
|> Locale.init class |
|
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
783 |
|> init_ctxt [class] ((#base_sort o the_class_data thy) class); |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
784 |
|
24748 | 785 |
|
24589 | 786 |
(* class definition *) |
24218 | 787 |
|
788 |
local |
|
789 |
||
24748 | 790 |
fun gen_class_spec prep_class prep_expr process_expr thy raw_supclasses raw_includes_elems = |
24218 | 791 |
let |
24748 | 792 |
val supclasses = map (prep_class thy) raw_supclasses; |
25209 | 793 |
val sups = filter (is_class thy) supclasses; |
794 |
fun the_base_sort class = lookup_class_data thy class |
|
795 |
|> Option.map #base_sort |
|
796 |
|> the_default [class]; |
|
797 |
val base_sort = Sign.minimize_sort thy (maps the_base_sort supclasses); |
|
24748 | 798 |
val supsort = Sign.minimize_sort thy supclasses; |
25038 | 799 |
val suplocales = map Locale.Locale sups; |
24748 | 800 |
val (raw_elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e) |
801 |
| Locale.Expr i => apsnd (cons (prep_expr thy i))) raw_includes_elems ([], []); |
|
802 |
val supexpr = Locale.Merge suplocales; |
|
803 |
val supparams = (map fst o Locale.parameters_of_expr thy) supexpr; |
|
25002 | 804 |
val supconsts = AList.make (the o AList.lookup (op =) (these_params thy sups)) |
24748 | 805 |
(map fst supparams); |
806 |
val mergeexpr = Locale.Merge (suplocales @ includes); |
|
807 |
val constrain = Element.Constrains ((map o apsnd o map_atyps) |
|
24847 | 808 |
(fn TFree (_, sort) => TFree (Name.aT, sort)) supparams); |
24748 | 809 |
in |
810 |
ProofContext.init thy |
|
811 |
|> Locale.cert_expr supexpr [constrain] |
|
812 |
|> snd |
|
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
813 |
|> init_ctxt sups base_sort |
24748 | 814 |
|> process_expr Locale.empty raw_elems |
815 |
|> fst |
|
25062 | 816 |
|> (fn elems => ((((sups, supconsts), (supsort, base_sort, mergeexpr)), |
24748 | 817 |
(*FIXME*) if null includes then constrain :: elems else elems))) |
818 |
end; |
|
819 |
||
820 |
val read_class_spec = gen_class_spec Sign.intern_class Locale.intern_expr Locale.read_expr; |
|
821 |
val check_class_spec = gen_class_spec (K I) (K I) Locale.cert_expr; |
|
822 |
||
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
823 |
fun define_class_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy = |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
824 |
let |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
825 |
val superclasses = map (Sign.certify_class thy) raw_superclasses; |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
826 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; |
25083 | 827 |
fun add_const ((c, ty), syn) = |
828 |
Sign.declare_const [] (c, Type.strip_sorts ty, syn) #>> Term.dest_Const; |
|
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
829 |
fun mk_axioms cs thy = |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
830 |
raw_dep_axioms thy cs |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
831 |
|> (map o apsnd o map) (Sign.cert_prop thy) |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
832 |
|> rpair thy; |
25002 | 833 |
fun constrain_typs class = (map o apsnd o Term.map_type_tfree) |
834 |
(fn (v, _) => TFree (v, [class])) |
|
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
835 |
in |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
836 |
thy |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
837 |
|> Sign.add_path (Logic.const_of_class name) |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
838 |
|> fold_map add_const consts |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
839 |
||> Sign.restore_naming thy |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
840 |
|-> (fn cs => mk_axioms cs |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
841 |
#-> (fn axioms_prop => AxClass.define_class (name, superclasses) |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
842 |
(map fst cs @ other_consts) axioms_prop |
25002 | 843 |
#-> (fn class => `(fn _ => constrain_typs class cs) |
844 |
#-> (fn cs' => `(fn thy => AxClass.get_info thy class) |
|
845 |
#-> (fn {axioms, ...} => fold (Sign.add_const_constraint o apsnd SOME) cs' |
|
846 |
#> pair (class, (cs', axioms))))))) |
|
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
847 |
end; |
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
848 |
|
25002 | 849 |
fun gen_class prep_spec prep_param bname |
24748 | 850 |
raw_supclasses raw_includes_elems raw_other_consts thy = |
851 |
let |
|
25038 | 852 |
val class = Sign.full_name thy bname; |
25062 | 853 |
val (((sups, supconsts), (supsort, base_sort, mergeexpr)), elems_syn) = |
24748 | 854 |
prep_spec thy raw_supclasses raw_includes_elems; |
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
855 |
val other_consts = map (tap (Sign.the_const_type thy) o prep_param thy) raw_other_consts; |
25163 | 856 |
fun mk_inst class cs = |
857 |
(map o apsnd o Term.map_type_tfree) (fn (v, _) => TFree (v, [class])) cs; |
|
25062 | 858 |
fun fork_syntax (Element.Fixes xs) = |
859 |
fold_map (fn (c, ty, syn) => cons (c, syn) #> pair (c, ty, NoSyn)) xs |
|
860 |
#>> Element.Fixes |
|
861 |
| fork_syntax x = pair x; |
|
862 |
val (elems, global_syn) = fold_map fork_syntax elems_syn []; |
|
25326 | 863 |
fun globalize (c, ty) = |
25062 | 864 |
((c, Term.map_type_tfree (K (TFree (Name.aT, base_sort))) ty), |
865 |
(the_default NoSyn o AList.lookup (op =) global_syn) c); |
|
25038 | 866 |
fun extract_params thy = |
24218 | 867 |
let |
25062 | 868 |
val params = map fst (Locale.parameters_of thy class); |
24218 | 869 |
in |
25062 | 870 |
(params, (map globalize o snd o chop (length supconsts)) params) |
24218 | 871 |
end; |
25038 | 872 |
fun extract_assumes params thy cs = |
24218 | 873 |
let |
874 |
val consts = supconsts @ (map (fst o fst) params ~~ cs); |
|
875 |
fun subst (Free (c, ty)) = |
|
876 |
Const ((fst o the o AList.lookup (op =) consts) c, ty) |
|
877 |
| subst t = t; |
|
878 |
fun prep_asm ((name, atts), ts) = |
|
25024 | 879 |
((Sign.base_name name, map (Attrib.attribute_i thy) atts), |
24589 | 880 |
(map o map_aterms) subst ts); |
24218 | 881 |
in |
25038 | 882 |
Locale.global_asms_of thy class |
24218 | 883 |
|> map prep_asm |
884 |
end; |
|
885 |
in |
|
886 |
thy |
|
24748 | 887 |
|> Locale.add_locale_i (SOME "") bname mergeexpr elems |
25038 | 888 |
|> snd |
25311 | 889 |
|> ProofContext.theory_of |
890 |
|> `extract_params |
|
891 |
|-> (fn (all_params, params) => |
|
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset
|
892 |
define_class_params (bname, supsort) params |
25038 | 893 |
(extract_assumes params) other_consts |
894 |
#-> (fn (_, (consts, axioms)) => |
|
895 |
`(fn thy => class_intro thy class sups) |
|
24218 | 896 |
#-> (fn class_intro => |
25062 | 897 |
PureThy.note_thmss_qualified "" (NameSpace.append class classN) |
898 |
[((introN, []), [([class_intro], [])])] |
|
899 |
#-> (fn [(_, [class_intro])] => |
|
25038 | 900 |
add_class_data ((class, sups), |
25062 | 901 |
(map fst params ~~ consts, base_sort, |
25163 | 902 |
mk_inst class (map snd supconsts @ consts), |
25062 | 903 |
calculate_morphism class (supconsts @ (map (fst o fst) params ~~ consts)), class_intro)) |
25038 | 904 |
#> class_interpretation class axioms [] |
25311 | 905 |
)))) |
25268 | 906 |
|> init class |
25038 | 907 |
|> pair class |
24218 | 908 |
end; |
909 |
||
25326 | 910 |
fun read_const thy = #1 o Term.dest_Const o ProofContext.read_const (ProofContext.init thy); |
911 |
||
24218 | 912 |
in |
913 |
||
25326 | 914 |
val class_cmd = gen_class read_class_spec read_const; |
24748 | 915 |
val class = gen_class check_class_spec (K I); |
24218 | 916 |
|
917 |
end; (*local*) |
|
918 |
||
919 |
||
25485 | 920 |
(* class target *) |
24218 | 921 |
|
25485 | 922 |
fun logical_const class pos ((c, mx), dict) thy = |
24218 | 923 |
let |
25024 | 924 |
val prfx = class_prefix class; |
925 |
val thy' = thy |> Sign.add_path prfx; |
|
25062 | 926 |
val phi = morphism thy' class; |
25024 | 927 |
|
25062 | 928 |
val c' = Sign.full_name thy' c; |
25239 | 929 |
val dict' = Morphism.term phi dict; |
930 |
val dict_def = map_types Logic.unvarifyT dict'; |
|
931 |
val ty' = Term.fastype_of dict_def; |
|
25083 | 932 |
val ty'' = Type.strip_sorts ty'; |
25239 | 933 |
val def_eq = Logic.mk_equals (Const (c', ty'), dict_def); |
24218 | 934 |
in |
25024 | 935 |
thy' |
25096 | 936 |
|> Sign.declare_const pos (c, ty'', mx) |> snd |
25485 | 937 |
|> Thm.add_def false (c, def_eq) |
25062 | 938 |
|>> Thm.symmetric |
25083 | 939 |
|-> (fn def => class_interpretation class [def] [Thm.prop_of def] |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
940 |
#> register_operation class (c', (dict', SOME (Thm.varifyT def)))) |
24218 | 941 |
|> Sign.restore_naming thy |
25083 | 942 |
|> Sign.add_const_constraint (c', SOME ty') |
24218 | 943 |
end; |
944 |
||
25485 | 945 |
fun syntactic_const class prmode pos ((c, mx), rhs) thy = |
24836 | 946 |
let |
25024 | 947 |
val prfx = class_prefix class; |
25096 | 948 |
val thy' = thy |> Sign.add_path prfx; |
25062 | 949 |
val phi = morphism thy class; |
950 |
||
25096 | 951 |
val c' = Sign.full_name thy' c; |
25146 | 952 |
val rews = map (Logic.dest_equals o Thm.prop_of) (these_defs thy' [class]) |
953 |
val rhs' = (Pattern.rewrite_term thy rews [] o Morphism.term phi) rhs; |
|
25239 | 954 |
val ty' = Logic.unvarifyT (Term.fastype_of rhs'); |
24836 | 955 |
in |
25096 | 956 |
thy' |
25146 | 957 |
|> Sign.add_abbrev (#1 prmode) pos (c, map_types Type.strip_sorts rhs') |> snd |
25096 | 958 |
|> Sign.add_const_constraint (c', SOME ty') |
25024 | 959 |
|> Sign.notation true prmode [(Const (c', ty'), mx)] |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
960 |
|> register_operation class (c', (rhs', NONE)) |
25096 | 961 |
|> Sign.restore_naming thy |
24836 | 962 |
end; |
963 |
||
25462 | 964 |
|
965 |
(** instantiation target **) |
|
966 |
||
967 |
(* bookkeeping *) |
|
968 |
||
969 |
datatype instantiation = Instantiation of { |
|
970 |
arities: arity list, |
|
971 |
params: ((string * string) * (string * typ)) list |
|
972 |
} |
|
973 |
||
974 |
structure Instantiation = ProofDataFun |
|
975 |
( |
|
976 |
type T = instantiation |
|
977 |
fun init _ = Instantiation { arities = [], params = [] }; |
|
978 |
); |
|
979 |
||
25485 | 980 |
fun mk_instantiation (arities, params) = |
981 |
Instantiation { arities = arities, params = params }; |
|
982 |
fun get_instantiation ctxt = case Instantiation.get ctxt |
|
983 |
of Instantiation data => data; |
|
25462 | 984 |
fun map_instantiation f (Instantiation { arities, params }) = |
985 |
mk_instantiation (f (arities, params)); |
|
986 |
||
25485 | 987 |
fun the_instantiation ctxt = case get_instantiation ctxt |
988 |
of { arities = [], ... } => error "No instantiation target" |
|
989 |
| data => data; |
|
25462 | 990 |
|
25485 | 991 |
val instantiation_params = #params o get_instantiation; |
25462 | 992 |
|
25485 | 993 |
fun instantiation_param ctxt v = instantiation_params ctxt |
25462 | 994 |
|> find_first (fn (_, (v', _)) => v = v') |
995 |
|> Option.map (fst o fst); |
|
996 |
||
25485 | 997 |
fun confirm_declaration c = (Instantiation.map o map_instantiation o apsnd) |
998 |
(filter_out (fn (_, (c', _)) => c' = c)); |
|
999 |
||
25462 | 1000 |
|
1001 |
(* syntax *) |
|
1002 |
||
1003 |
fun inst_term_check ts ctxt = |
|
1004 |
let |
|
1005 |
val params = instantiation_params ctxt; |
|
1006 |
val tsig = ProofContext.tsig_of ctxt; |
|
1007 |
val thy = ProofContext.theory_of ctxt; |
|
1008 |
||
1009 |
fun check_improve (Const (c, ty)) = (case inst_tyco thy (c, ty) |
|
1010 |
of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
|
1011 |
of SOME (_, ty') => Type.typ_match tsig (ty, ty') |
|
1012 |
| NONE => I) |
|
1013 |
| NONE => I) |
|
1014 |
| check_improve _ = I; |
|
1015 |
val improvement = (fold o fold_aterms) check_improve ts Vartab.empty; |
|
1016 |
val ts' = (map o map_types) (Envir.typ_subst_TVars improvement) ts; |
|
1017 |
val ts'' = (map o map_aterms) (fn t as Const (c, ty) => (case inst_tyco thy (c, ty) |
|
1018 |
of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
|
1019 |
of SOME v_ty => Free v_ty |
|
1020 |
| NONE => t) |
|
1021 |
| NONE => t) |
|
1022 |
| t => t) ts'; |
|
1023 |
in if eq_list (op aconv) (ts, ts'') then NONE else SOME (ts'', ctxt) end; |
|
1024 |
||
1025 |
fun inst_term_uncheck ts ctxt = |
|
1026 |
let |
|
1027 |
val params = instantiation_params ctxt; |
|
1028 |
val ts' = (map o map_aterms) (fn t as Free (v, ty) => |
|
1029 |
(case get_first (fn ((c, _), (v', _)) => if v = v' then SOME c else NONE) params |
|
1030 |
of SOME c => Const (c, ty) |
|
1031 |
| NONE => t) |
|
1032 |
| t => t) ts; |
|
1033 |
in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end; |
|
1034 |
||
1035 |
||
1036 |
(* target *) |
|
1037 |
||
25485 | 1038 |
val sanatize_name = (*FIXME*) |
1039 |
let |
|
1040 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'"; |
|
1041 |
val is_junk = not o is_valid andf Symbol.is_regular; |
|
1042 |
val junk = Scan.many is_junk; |
|
1043 |
val scan_valids = Symbol.scanner "Malformed input" |
|
1044 |
((junk |-- |
|
1045 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
1046 |
--| junk)) |
|
1047 |
-- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::); |
|
1048 |
in |
|
1049 |
explode #> scan_valids #> implode |
|
1050 |
end; |
|
1051 |
||
25462 | 1052 |
|
25485 | 1053 |
fun init_instantiation arities thy = |
25462 | 1054 |
let |
25485 | 1055 |
val _ = if null arities then error "At least one arity must be given" else (); |
1056 |
val _ = case (duplicates (op =) o map #1) arities |
|
1057 |
of [] => () |
|
1058 |
| dupl_tycos => error ("Type constructors occur more than once in arities: " |
|
1059 |
^ commas_quote dupl_tycos); |
|
1060 |
val ty_insts = map (fn (tyco, sorts, _) => |
|
1061 |
(tyco, Type (tyco, map TFree (Name.names Name.context Name.aT sorts)))) |
|
1062 |
arities; |
|
1063 |
val ty_inst = the o AList.lookup (op =) ty_insts; |
|
1064 |
fun type_name "*" = "prod" |
|
1065 |
| type_name "+" = "sum" |
|
1066 |
| type_name s = sanatize_name (NameSpace.base s); (*FIXME*) |
|
1067 |
fun get_param tyco sorts (param, (c, ty)) = |
|
1068 |
((unoverload_const thy (c, ty), tyco), |
|
1069 |
(param ^ "_" ^ type_name tyco, |
|
1070 |
map_atyps (K (ty_inst tyco)) ty)); |
|
1071 |
fun get_params (tyco, sorts, sort) = |
|
1072 |
map (get_param tyco sorts) (these_params thy sort) |
|
1073 |
val params = maps get_params arities; |
|
1074 |
in |
|
1075 |
thy |
|
1076 |
|> ProofContext.init |
|
1077 |
|> Instantiation.put (mk_instantiation (arities, params)) |
|
1078 |
|> fold (Variable.declare_term o Logic.mk_type o snd) ty_insts |
|
1079 |
|> fold ProofContext.add_arity arities |
|
1080 |
|> Context.proof_map ( |
|
1081 |
Syntax.add_term_check 0 "instance" inst_term_check |
|
1082 |
#> Syntax.add_term_uncheck 0 "instance" inst_term_uncheck) |
|
1083 |
end; |
|
1084 |
||
1085 |
fun gen_instantiation_instance do_proof after_qed lthy = |
|
1086 |
let |
|
25462 | 1087 |
val ctxt = LocalTheory.target_of lthy; |
1088 |
val arities = (#arities o the_instantiation) ctxt; |
|
25485 | 1089 |
val arities_proof = maps Logic.mk_arities arities; |
25462 | 1090 |
fun after_qed' results = |
1091 |
LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) |
|
1092 |
#> after_qed; |
|
1093 |
in |
|
1094 |
lthy |
|
1095 |
|> do_proof after_qed' arities_proof |
|
1096 |
end; |
|
1097 |
||
25485 | 1098 |
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts => |
25462 | 1099 |
Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts)); |
1100 |
||
25485 | 1101 |
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed => |
25462 | 1102 |
fn ts => fn lthy => after_qed (Goal.prove_multi lthy [] [] ts |
1103 |
(fn {context, ...} => tac context)) lthy) I; |
|
1104 |
||
1105 |
fun conclude_instantiation lthy = |
|
1106 |
let |
|
25485 | 1107 |
val { arities, params } = the_instantiation lthy; |
25462 | 1108 |
val thy = ProofContext.theory_of lthy; |
1109 |
(*val _ = map (fn (tyco, sorts, sort) => |
|
1110 |
if Sign.of_sort thy |
|
1111 |
(Type (tyco, map TFree (Name.names Name.context Name.aT sorts)), sort) |
|
1112 |
then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco))) |
|
1113 |
arities; FIXME activate when old instance command is gone*) |
|
1114 |
val params_of = maps (these o try (#params o AxClass.get_info thy)) |
|
1115 |
o Sign.complete_sort thy; |
|
1116 |
val missing_params = arities |
|
1117 |
|> maps (fn (tyco, _, sort) => params_of sort |> map (rpair tyco)) |
|
1118 |
|> filter_out (can (inst thy) o apfst fst); |
|
25485 | 1119 |
fun declare_missing ((c, ty0), tyco) thy = |
1120 |
(*fun declare_missing ((c, tyco), (_, ty)) thy =*) |
|
25462 | 1121 |
let |
1122 |
val SOME class = AxClass.class_of_param thy c; |
|
25485 | 1123 |
val name_inst = AxClass.instance_name (tyco, class) ^ "_inst"; |
1124 |
val c' = NameSpace.base c; |
|
25462 | 1125 |
val vs = Name.names Name.context Name.aT (replicate (Sign.arity_number thy tyco) []); |
25485 | 1126 |
val ty = map_atyps (fn _ => Type (tyco, map TFree vs)) ty0; |
25462 | 1127 |
in |
1128 |
thy |
|
1129 |
|> Sign.sticky_prefix name_inst |
|
1130 |
|> Sign.no_base_names |
|
25485 | 1131 |
|> Sign.declare_const [] (c', ty, NoSyn) |
25462 | 1132 |
|-> (fn const' as Const (c'', _) => Thm.add_def true |
25485 | 1133 |
(Thm.def_name c', Logic.mk_equals (const', Const (c, ty))) |
25462 | 1134 |
#>> Thm.varifyT |
1135 |
#-> (fn thm => add_inst (c, tyco) (c'', Thm.symmetric thm) |
|
1136 |
#> primitive_note Thm.internalK (c', thm) |
|
1137 |
#> snd |
|
1138 |
#> Sign.restore_naming thy)) |
|
1139 |
end; |
|
1140 |
in |
|
1141 |
lthy |
|
1142 |
|> LocalTheory.theory (fold declare_missing missing_params) |
|
1143 |
end; |
|
1144 |
||
24218 | 1145 |
end; |