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