author | haftmann |
Mon, 05 Jan 2009 15:55:04 +0100 | |
changeset 29360 | a5be60c3674e |
parent 29358 | efdfe5dfe008 |
child 29362 | f9ded2d789b9 |
permissions | -rw-r--r-- |
29358 | 1 |
(* Title: Pure/Isar/class_target.ML |
24218 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
29358 | 4 |
Type classes derived from primitive axclasses and locales - mechanisms. |
24218 | 5 |
*) |
6 |
||
29358 | 7 |
signature CLASS_TARGET = |
24218 | 8 |
sig |
25462 | 9 |
(*classes*) |
29358 | 10 |
type raw_morphism = morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list)); |
11 |
val register: class -> class list -> ((string * typ) * (string * typ)) list |
|
12 |
-> sort -> term list -> raw_morphism |
|
13 |
-> thm option -> thm option -> thm -> theory -> theory |
|
25485 | 14 |
|
29358 | 15 |
val begin: class list -> sort -> Proof.context -> Proof.context |
25311 | 16 |
val init: class -> theory -> Proof.context |
28017 | 17 |
val declare: class -> Properties.T |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
18 |
-> (string * mixfix) * term -> theory -> theory |
28017 | 19 |
val abbrev: class -> Syntax.mode -> Properties.T |
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 |
26470 | 24 |
val default_intro_tac: Proof.context -> thm list -> tactic |
29358 | 25 |
val activate_class_morphism: theory -> class -> term list |
26 |
-> raw_morphism -> morphism |
|
27 |
val prove_class_interpretation: class -> term list -> (class * string) list |
|
28 |
-> thm list -> thm list -> theory -> raw_morphism * theory |
|
29 |
val prove_subclass_relation: class * class -> thm option -> theory -> theory |
|
25485 | 30 |
|
31 |
val class_prefix: string -> string |
|
32 |
val is_class: theory -> class -> bool |
|
26518 | 33 |
val these_params: theory -> sort -> (string * (class * (string * typ))) list |
29358 | 34 |
val these_defs: theory -> sort -> thm list |
35 |
val base_sort: theory -> class -> sort |
|
36 |
val rules: theory -> class -> thm option * thm |
|
24589 | 37 |
val print_classes: theory -> unit |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset
|
38 |
|
25462 | 39 |
(*instances*) |
26247 | 40 |
val init_instantiation: string list * (string * sort) list * sort |
41 |
-> theory -> local_theory |
|
42 |
val instantiation_instance: (local_theory -> local_theory) |
|
43 |
-> local_theory -> Proof.state |
|
44 |
val prove_instantiation_instance: (Proof.context -> tactic) |
|
45 |
-> local_theory -> local_theory |
|
28666 | 46 |
val prove_instantiation_exit: (Proof.context -> tactic) |
47 |
-> local_theory -> theory |
|
48 |
val prove_instantiation_exit_result: (morphism -> 'a -> 'b) |
|
49 |
-> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory |
|
25485 | 50 |
val conclude_instantiation: local_theory -> local_theory |
25603 | 51 |
val instantiation_param: local_theory -> string -> string option |
25485 | 52 |
val confirm_declaration: string -> local_theory -> local_theory |
25603 | 53 |
val pretty_instantiation: local_theory -> Pretty.T |
26259 | 54 |
val type_name: string -> string |
25485 | 55 |
|
25462 | 56 |
(*old axclass layer*) |
57 |
val axclass_cmd: bstring * xstring list |
|
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
58 |
-> (Attrib.binding * string list) list |
25462 | 59 |
-> theory -> class * theory |
60 |
val classrel_cmd: xstring * xstring -> theory -> Proof.state |
|
61 |
||
62 |
(*old instance layer*) |
|
25536 | 63 |
val instance_arity: (theory -> theory) -> arity -> theory -> Proof.state |
64 |
val instance_arity_cmd: bstring * xstring list * xstring -> theory -> Proof.state |
|
24218 | 65 |
end; |
66 |
||
29358 | 67 |
structure Class_Target : CLASS_TARGET = |
24218 | 68 |
struct |
69 |
||
29133 | 70 |
(*temporary adaption code to mediate between old and new locale code*) |
71 |
||
72 |
structure Old_Locale = |
|
73 |
struct |
|
74 |
||
29360 | 75 |
val intro_locales_tac = Old_Locale.intro_locales_tac; (*already forked!*) |
29133 | 76 |
|
29360 | 77 |
val interpretation = Old_Locale.interpretation; |
78 |
val interpretation_in_locale = Old_Locale.interpretation_in_locale; |
|
79 |
val get_interpret_morph = Old_Locale.get_interpret_morph; |
|
80 |
val Locale = Old_Locale.Locale; |
|
81 |
val extern = Old_Locale.extern; |
|
82 |
val intros = Old_Locale.intros; |
|
83 |
val dests = Old_Locale.dests; |
|
84 |
val init = Old_Locale.init; |
|
85 |
val Merge = Old_Locale.Merge; |
|
86 |
val parameters_of_expr = Old_Locale.parameters_of_expr; |
|
87 |
val empty = Old_Locale.empty; |
|
88 |
val cert_expr = Old_Locale.cert_expr; |
|
89 |
val read_expr = Old_Locale.read_expr; |
|
90 |
val parameters_of = Old_Locale.parameters_of; |
|
91 |
val add_locale = Old_Locale.add_locale; |
|
29133 | 92 |
|
93 |
end; |
|
94 |
||
29333
496b94152b55
improved boostrap order: theory_target.ML before expression.ML
haftmann
parents:
29133
diff
changeset
|
95 |
(*structure New_Locale = |
29133 | 96 |
struct |
97 |
||
98 |
val intro_locales_tac = Locale.intro_locales_tac; (*already forked!*) |
|
99 |
||
100 |
val interpretation = Locale.interpretation; (*!*) |
|
101 |
val interpretation_in_locale = Locale.interpretation_in_locale; (*!*) |
|
102 |
val get_interpret_morph = Locale.get_interpret_morph; (*!*) |
|
103 |
fun Locale loc = ([(loc, ("", Expression.Positional []))], []); |
|
104 |
val extern = NewLocale.extern; |
|
105 |
val intros = Locale.intros; (*!*) |
|
106 |
val dests = Locale.dests; (*!*) |
|
107 |
val init = NewLocale.init; |
|
108 |
fun Merge locs = (map (fn loc => (loc, ("", Expression.Positional []))) locs, []); |
|
109 |
val parameters_of_expr = Locale.parameters_of_expr; (*!*) |
|
110 |
val empty = ([], []); |
|
111 |
val cert_expr = Locale.cert_expr; (*!"*) |
|
112 |
val read_expr = Locale.read_expr; (*!"*) |
|
113 |
val parameters_of = NewLocale.params_of; (*why typ option?*) |
|
114 |
val add_locale = Expression.add_locale; |
|
115 |
||
29333
496b94152b55
improved boostrap order: theory_target.ML before expression.ML
haftmann
parents:
29133
diff
changeset
|
116 |
end;*) |
29133 | 117 |
|
118 |
structure Locale = Old_Locale; |
|
119 |
||
120 |
(*proper code again*) |
|
121 |
||
122 |
||
24218 | 123 |
(** auxiliary **) |
124 |
||
25002 | 125 |
fun prove_interpretation tac prfx_atts expr inst = |
28822
7ca11ecbc4fb
explicit name morphism function for locale interpretation
haftmann
parents:
28739
diff
changeset
|
126 |
Locale.interpretation I prfx_atts expr inst |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
127 |
##> Proof.global_terminal_proof |
27761
b95e9ba0ca1d
Interpretation command (theory/proof context) no longer simplifies goal.
ballarin
parents:
27708
diff
changeset
|
128 |
(Method.Basic (fn ctxt => Method.SIMPLE_METHOD (tac ctxt), Position.none), NONE) |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
129 |
##> ProofContext.theory_of; |
24589 | 130 |
|
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
131 |
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
|
132 |
Locale.interpretation_in_locale |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
133 |
(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
|
134 |
#> Proof.global_terminal_proof |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
135 |
(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
|
136 |
#> ProofContext.theory_of; |
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset
|
137 |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
138 |
val class_prefix = Logic.const_of_class o Sign.base_name; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
139 |
|
28822
7ca11ecbc4fb
explicit name morphism function for locale interpretation
haftmann
parents:
28739
diff
changeset
|
140 |
fun class_name_morphism class = |
28941 | 141 |
Binding.map_prefix (K (Binding.add_prefix false (class_prefix class))); |
28822
7ca11ecbc4fb
explicit name morphism function for locale interpretation
haftmann
parents:
28739
diff
changeset
|
142 |
|
29358 | 143 |
type raw_morphism = morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list)); |
144 |
||
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
145 |
fun activate_class_morphism thy class inst morphism = |
28822
7ca11ecbc4fb
explicit name morphism function for locale interpretation
haftmann
parents:
28739
diff
changeset
|
146 |
Locale.get_interpret_morph thy (class_name_morphism class) (class, "") morphism class inst; |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
147 |
|
24589 | 148 |
|
25485 | 149 |
(** primitive axclass and instance commands **) |
24589 | 150 |
|
24218 | 151 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy = |
152 |
let |
|
153 |
val ctxt = ProofContext.init thy; |
|
154 |
val superclasses = map (Sign.read_class thy) raw_superclasses; |
|
24589 | 155 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) |
156 |
raw_specs; |
|
157 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) |
|
158 |
raw_specs) |
|
24218 | 159 |
|> snd |
160 |
|> (map o map) fst; |
|
24589 | 161 |
in |
162 |
AxClass.define_class (class, superclasses) [] |
|
163 |
(name_atts ~~ axiomss) thy |
|
164 |
end; |
|
24218 | 165 |
|
166 |
local |
|
167 |
||
168 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
|
169 |
let |
|
170 |
fun after_qed' results = |
|
171 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
172 |
in |
|
173 |
thy |
|
174 |
|> ProofContext.init |
|
24589 | 175 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) |
25536 | 176 |
o mk_prop thy) insts) |
24218 | 177 |
end; |
178 |
||
179 |
in |
|
180 |
||
24589 | 181 |
val instance_arity = |
24218 | 182 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
25502 | 183 |
val instance_arity_cmd = |
184 |
gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity I; |
|
24589 | 185 |
val classrel = |
25536 | 186 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) AxClass.add_classrel I; |
24589 | 187 |
val classrel_cmd = |
25536 | 188 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) AxClass.add_classrel I; |
24218 | 189 |
|
190 |
end; (*local*) |
|
191 |
||
192 |
||
24589 | 193 |
(** class data **) |
24218 | 194 |
|
195 |
datatype class_data = ClassData of { |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
196 |
|
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
197 |
(* static part *) |
24218 | 198 |
consts: (string * string) list |
24836 | 199 |
(*locale parameter ~> constant name*), |
25062 | 200 |
base_sort: sort, |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
201 |
inst: term list |
25083 | 202 |
(*canonical interpretation*), |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
203 |
morphism: Morphism.morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list)) |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
204 |
(*morphism cookie of canonical interpretation*), |
25618 | 205 |
assm_intro: thm option, |
206 |
of_class: thm, |
|
207 |
axiom: thm option, |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
208 |
|
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
209 |
(* dynamic part *) |
24657 | 210 |
defs: thm list, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
211 |
operations: (string * (class * (typ * term))) list |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
212 |
|
24657 | 213 |
}; |
24218 | 214 |
|
24657 | 215 |
fun rep_class_data (ClassData d) = d; |
25618 | 216 |
fun mk_class_data ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
217 |
(defs, operations)) = |
25062 | 218 |
ClassData { consts = consts, base_sort = base_sort, inst = inst, |
26463 | 219 |
morphism = morphism, assm_intro = assm_intro, of_class = of_class, axiom = axiom, |
25618 | 220 |
defs = defs, operations = operations }; |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
221 |
fun map_class_data f (ClassData { consts, base_sort, inst, morphism, assm_intro, |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
222 |
of_class, axiom, defs, operations }) = |
25618 | 223 |
mk_class_data (f ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
224 |
(defs, operations))); |
25038 | 225 |
fun merge_class_data _ (ClassData { consts = consts, |
25618 | 226 |
base_sort = base_sort, inst = inst, morphism = morphism, assm_intro = assm_intro, |
227 |
of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 }, |
|
228 |
ClassData { consts = _, base_sort = _, inst = _, morphism = _, assm_intro = _, |
|
229 |
of_class = _, axiom = _, defs = defs2, operations = operations2 }) = |
|
230 |
mk_class_data ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom), |
|
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
231 |
(Thm.merge_thms (defs1, defs2), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
232 |
AList.merge (op =) (K true) (operations1, operations2))); |
24218 | 233 |
|
234 |
structure ClassData = TheoryDataFun |
|
235 |
( |
|
25038 | 236 |
type T = class_data Graph.T |
237 |
val empty = Graph.empty; |
|
24218 | 238 |
val copy = I; |
239 |
val extend = I; |
|
25038 | 240 |
fun merge _ = Graph.join merge_class_data; |
24218 | 241 |
); |
242 |
||
243 |
||
244 |
(* queries *) |
|
245 |
||
25038 | 246 |
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node o ClassData.get; |
24218 | 247 |
|
24589 | 248 |
fun the_class_data thy class = case lookup_class_data thy class |
25020 | 249 |
of NONE => error ("Undeclared class " ^ quote class) |
24589 | 250 |
| SOME data => data; |
24218 | 251 |
|
25038 | 252 |
val is_class = is_some oo lookup_class_data; |
253 |
||
254 |
val ancestry = Graph.all_succs o ClassData.get; |
|
24218 | 255 |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
256 |
fun the_inst thy = #inst o the_class_data thy; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
257 |
|
25002 | 258 |
fun these_params thy = |
24218 | 259 |
let |
260 |
fun params class = |
|
261 |
let |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
262 |
val const_typs = (#params o AxClass.get_info thy) class; |
24657 | 263 |
val const_names = (#consts o the_class_data thy) class; |
24218 | 264 |
in |
26518 | 265 |
(map o apsnd) |
266 |
(fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names |
|
24218 | 267 |
end; |
268 |
in maps params o ancestry thy end; |
|
269 |
||
25618 | 270 |
fun these_assm_intros thy = |
271 |
Graph.fold (fn (_, (data, _)) => fold (insert Thm.eq_thm) |
|
272 |
((the_list o #assm_intro o rep_class_data) data)) (ClassData.get thy) []; |
|
24218 | 273 |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
274 |
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
275 |
|
24836 | 276 |
fun these_operations thy = |
277 |
maps (#operations o the_class_data thy) o ancestry thy; |
|
24657 | 278 |
|
29358 | 279 |
val base_sort = #base_sort oo the_class_data; |
280 |
||
281 |
fun rules thy class = let |
|
282 |
val { axiom, of_class, ... } = the_class_data thy class |
|
283 |
in (axiom, of_class) end; |
|
284 |
||
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
285 |
fun morphism thy class = |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
286 |
let |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
287 |
val { inst, morphism, ... } = the_class_data thy class; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
288 |
in activate_class_morphism thy class inst morphism end; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
289 |
|
24218 | 290 |
fun print_classes thy = |
291 |
let |
|
24920 | 292 |
val ctxt = ProofContext.init thy; |
24218 | 293 |
val algebra = Sign.classes_of thy; |
294 |
val arities = |
|
295 |
Symtab.empty |
|
296 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
297 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
298 |
((#arities o Sorts.rep_algebra) algebra); |
|
299 |
val the_arities = these o Symtab.lookup arities; |
|
300 |
fun mk_arity class tyco = |
|
301 |
let |
|
302 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
24920 | 303 |
in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end; |
24218 | 304 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
24920 | 305 |
^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty); |
24218 | 306 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
25062 | 307 |
(SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"), |
24218 | 308 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
24920 | 309 |
(Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class], |
25062 | 310 |
if is_class thy class then (SOME o Pretty.str) |
311 |
("locale: " ^ Locale.extern thy class) else NONE, |
|
312 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) |
|
313 |
(Pretty.str "parameters:" :: ps)) o map mk_param |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
314 |
o these o Option.map #params o try (AxClass.get_info thy)) class, |
24218 | 315 |
(SOME o Pretty.block o Pretty.breaks) [ |
316 |
Pretty.str "instances:", |
|
317 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
318 |
] |
|
319 |
] |
|
320 |
in |
|
24589 | 321 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") |
322 |
o map mk_entry o Sorts.all_classes) algebra |
|
24218 | 323 |
end; |
324 |
||
325 |
||
326 |
(* updaters *) |
|
327 |
||
29358 | 328 |
fun register class superclasses params base_sort inst phi |
329 |
axiom assm_intro of_class thy = |
|
25002 | 330 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
331 |
val operations = map (fn (v_ty as (_, ty), (c, _)) => |
25683 | 332 |
(c, (class, (ty, Free v_ty)))) params; |
25038 | 333 |
val add_class = Graph.new_node (class, |
25683 | 334 |
mk_class_data (((map o pairself) fst params, base_sort, |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
335 |
inst, phi, assm_intro, of_class, axiom), ([], operations))) |
25002 | 336 |
#> fold (curry Graph.add_edge class) superclasses; |
25618 | 337 |
in ClassData.map add_class thy end; |
24218 | 338 |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
339 |
fun register_operation class (c, (t, some_def)) thy = |
25062 | 340 |
let |
29358 | 341 |
val base_sort = base_sort thy class; |
26518 | 342 |
val prep_typ = map_type_tvar |
343 |
(fn (vi as (v, _), sort) => if Name.aT = v |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
344 |
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
|
345 |
val t' = map_types prep_typ t; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
346 |
val ty' = Term.fastype_of t'; |
25062 | 347 |
in |
348 |
thy |
|
349 |
|> (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
|
350 |
(fn (defs, operations) => |
25096 | 351 |
(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
|
352 |
(c, (class, (ty', t'))) :: operations)) |
25062 | 353 |
end; |
24218 | 354 |
|
24589 | 355 |
|
29358 | 356 |
(** tactics and methods **) |
24589 | 357 |
|
29358 | 358 |
fun prove_class_interpretation class inst consts hyp_facts def_facts thy = |
25062 | 359 |
let |
29358 | 360 |
val constraints = map (fn (class, c) => map_atyps (K (TFree (Name.aT, |
361 |
[class]))) (Sign.the_const_type thy c)) consts; |
|
362 |
val no_constraints = map (map_atyps (K (TFree (Name.aT, [])))) constraints; |
|
363 |
fun add_constraint c T = Sign.add_const_constraint (c, SOME T); |
|
364 |
fun tac ctxt = ALLGOALS (ProofContext.fact_tac (hyp_facts @ def_facts) |
|
365 |
ORELSE' (fn n => SELECT_GOAL (Locale.intro_locales_tac false ctxt []) n)); |
|
366 |
val prfx = class_prefix class; |
|
367 |
in |
|
368 |
thy |
|
369 |
|> fold2 add_constraint (map snd consts) no_constraints |
|
370 |
|> prove_interpretation tac (class_name_morphism class) (Locale.Locale class) |
|
371 |
(map SOME inst, map (pair (Attrib.empty_binding) o Thm.prop_of) def_facts) |
|
372 |
||> fold2 add_constraint (map snd consts) constraints |
|
373 |
end; |
|
24218 | 374 |
|
29358 | 375 |
fun prove_subclass_relation (sub, sup) some_thm thy = |
25618 | 376 |
let |
29358 | 377 |
val of_class = (snd o rules thy) sup; |
27684
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
378 |
val intro = case some_thm |
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
379 |
of SOME thm => Drule.standard' (of_class OF [Drule.standard' thm]) |
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
380 |
| NONE => Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0)) |
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
381 |
([], [sub])], []) of_class; |
29358 | 382 |
val classrel = (intro OF (the_list o fst o rules thy) sub) |
27684
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
383 |
|> Thm.close_derivation; |
25618 | 384 |
in |
385 |
thy |
|
386 |
|> AxClass.add_classrel classrel |
|
27684
f45fd1159a4b
subclass now also works for subclasses with empty specificaton
haftmann
parents:
27281
diff
changeset
|
387 |
|> prove_interpretation_in (ALLGOALS (ProofContext.fact_tac (the_list some_thm))) |
25618 | 388 |
I (sub, Locale.Locale sup) |
389 |
|> ClassData.map (Graph.add_edge (sub, sup)) |
|
390 |
end; |
|
391 |
||
24218 | 392 |
fun intro_classes_tac facts st = |
393 |
let |
|
394 |
val thy = Thm.theory_of_thm st; |
|
395 |
val classes = Sign.all_classes thy; |
|
396 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
25618 | 397 |
val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes; |
398 |
val assm_intros = these_assm_intros thy; |
|
24218 | 399 |
in |
25618 | 400 |
Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st |
24218 | 401 |
end; |
402 |
||
26470 | 403 |
fun default_intro_tac ctxt [] = |
29360 | 404 |
intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt [] ORELSE |
29029
1945e0185097
NewLocale.intro_locales_tac added to Class.default_intro_tac.
ballarin
parents:
29006
diff
changeset
|
405 |
Locale.intro_locales_tac true ctxt [] |
26470 | 406 |
| default_intro_tac _ _ = no_tac; |
24218 | 407 |
|
408 |
fun default_tac rules ctxt facts = |
|
409 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
26470 | 410 |
default_intro_tac ctxt facts; |
24218 | 411 |
|
26463 | 412 |
val _ = Context.>> (Context.map_theory |
413 |
(Method.add_methods |
|
414 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
|
415 |
"back-chain introduction rules of classes"), |
|
416 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
417 |
"apply some intro/elim rule")])); |
|
418 |
||
24218 | 419 |
|
24589 | 420 |
(** classes and class target **) |
24218 | 421 |
|
25002 | 422 |
(* class context syntax *) |
24748 | 423 |
|
26238 | 424 |
fun synchronize_class_syntax sups base_sort ctxt = |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
425 |
let |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
426 |
val thy = ProofContext.theory_of ctxt; |
26596 | 427 |
val algebra = Sign.classes_of thy; |
25083 | 428 |
val operations = these_operations thy sups; |
26518 | 429 |
fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort))); |
430 |
val primary_constraints = |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
431 |
(map o apsnd) (subst_class_typ base_sort o fst o snd) operations; |
26518 | 432 |
val secondary_constraints = |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
433 |
(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
|
434 |
fun declare_const (c, _) = |
c8352b38d47d
synchronize_syntax: declare operations within the local scope of fixes/consts
wenzelm
parents:
25311
diff
changeset
|
435 |
let val b = Sign.base_name c |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
436 |
in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end; |
26518 | 437 |
fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c |
26238 | 438 |
of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty |
439 |
of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0) |
|
26596 | 440 |
of SOME (_, ty' as TVar (tvar as (vi, sort))) => |
26238 | 441 |
if TypeInfer.is_param vi |
26596 | 442 |
andalso Sorts.sort_le algebra (base_sort, sort) |
443 |
then SOME (ty', TFree (Name.aT, base_sort)) |
|
444 |
else NONE |
|
26238 | 445 |
| _ => NONE) |
446 |
| NONE => NONE) |
|
447 |
| NONE => NONE) |
|
448 |
fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c); |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
449 |
val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) operations; |
25083 | 450 |
in |
451 |
ctxt |
|
26518 | 452 |
|> fold declare_const primary_constraints |
453 |
|> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints), |
|
26730 | 454 |
(((improve, subst), true), unchecks)), false)) |
26518 | 455 |
|> Overloading.set_primary_constraints |
25083 | 456 |
end; |
457 |
||
458 |
fun refresh_syntax class ctxt = |
|
25002 | 459 |
let |
460 |
val thy = ProofContext.theory_of ctxt; |
|
29358 | 461 |
val base_sort = base_sort thy class; |
26238 | 462 |
in synchronize_class_syntax [class] base_sort ctxt end; |
25002 | 463 |
|
29358 | 464 |
fun begin sups base_sort ctxt = |
25083 | 465 |
ctxt |
466 |
|> Variable.declare_term |
|
467 |
(Logic.mk_type (TFree (Name.aT, base_sort))) |
|
26238 | 468 |
|> synchronize_class_syntax sups base_sort |
469 |
|> Overloading.add_improvable_syntax; |
|
24901
d3cbf79769b9
added first version of user-space type system for class target
haftmann
parents:
24847
diff
changeset
|
470 |
|
25311 | 471 |
fun init class thy = |
472 |
thy |
|
473 |
|> Locale.init class |
|
29358 | 474 |
|> begin [class] (base_sort thy class); |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
475 |
|
24748 | 476 |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
477 |
(* class target *) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
478 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
479 |
fun declare class pos ((c, mx), dict) thy = |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
480 |
let |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
481 |
val prfx = class_prefix class; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
482 |
val thy' = thy |> Sign.add_path prfx; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
483 |
val phi = morphism thy' class; |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
484 |
val inst = the_inst thy' class; |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
485 |
val params = map (apsnd fst o snd) (these_params thy' [class]); |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
486 |
|
28965 | 487 |
val c' = Sign.full_bname thy' c; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
488 |
val dict' = Morphism.term phi dict; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
489 |
val dict_def = map_types Logic.unvarifyT dict'; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
490 |
val ty' = Term.fastype_of dict_def; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
491 |
val ty'' = Type.strip_sorts ty'; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
492 |
val def_eq = Logic.mk_equals (Const (c', ty'), dict_def); |
28674 | 493 |
fun get_axiom thy = ((Thm.varifyT o Thm.axiom thy) c', thy); |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
494 |
in |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
495 |
thy' |
28965 | 496 |
|> Sign.declare_const pos ((Binding.name c, ty''), mx) |> snd |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
497 |
|> Thm.add_def false false (c, def_eq) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
498 |
|>> Thm.symmetric |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
499 |
||>> get_axiom |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
500 |
|-> (fn (def, def') => prove_class_interpretation class inst params [] [def] |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
501 |
#> snd |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
502 |
(*assumption: interpretation cookie does not change |
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
503 |
by adding equations to interpretation*) |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
504 |
#> register_operation class (c', (dict', SOME (Thm.symmetric def'))) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
505 |
#> PureThy.store_thm (c ^ "_raw", def') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
506 |
#> snd) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
507 |
|> Sign.restore_naming thy |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
508 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
509 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
510 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
511 |
fun abbrev class prmode pos ((c, mx), rhs) thy = |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
512 |
let |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
513 |
val prfx = class_prefix class; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
514 |
val thy' = thy |> Sign.add_path prfx; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
515 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
516 |
val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
517 |
(these_operations thy [class]); |
28965 | 518 |
val c' = Sign.full_bname thy' c; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
519 |
val rhs' = Pattern.rewrite_term thy unchecks [] rhs; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
520 |
val rhs'' = map_types Logic.varifyT rhs'; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
521 |
val ty' = Term.fastype_of rhs'; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
522 |
in |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
523 |
thy' |
28965 | 524 |
|> Sign.add_abbrev (#1 prmode) pos (Binding.name c, map_types Type.strip_sorts rhs'') |> snd |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
525 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
526 |
|> Sign.notation true prmode [(Const (c', ty'), mx)] |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
527 |
|> not (#1 prmode = PrintMode.input) ? register_operation class (c', (rhs', NONE)) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
528 |
|> Sign.restore_naming thy |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
529 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
530 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
531 |
|
25462 | 532 |
(** instantiation target **) |
533 |
||
534 |
(* bookkeeping *) |
|
535 |
||
536 |
datatype instantiation = Instantiation of { |
|
25864 | 537 |
arities: string list * (string * sort) list * sort, |
25462 | 538 |
params: ((string * string) * (string * typ)) list |
25603 | 539 |
(*(instantiation parameter, type constructor), (local instantiation parameter, typ)*) |
25462 | 540 |
} |
541 |
||
542 |
structure Instantiation = ProofDataFun |
|
543 |
( |
|
544 |
type T = instantiation |
|
25536 | 545 |
fun init _ = Instantiation { arities = ([], [], []), params = [] }; |
25462 | 546 |
); |
547 |
||
25485 | 548 |
fun mk_instantiation (arities, params) = |
549 |
Instantiation { arities = arities, params = params }; |
|
25514 | 550 |
fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy) |
25485 | 551 |
of Instantiation data => data; |
25514 | 552 |
fun map_instantiation f = (LocalTheory.target o Instantiation.map) |
553 |
(fn Instantiation { arities, params } => mk_instantiation (f (arities, params))); |
|
25462 | 554 |
|
25514 | 555 |
fun the_instantiation lthy = case get_instantiation lthy |
25536 | 556 |
of { arities = ([], [], []), ... } => error "No instantiation target" |
25485 | 557 |
| data => data; |
25462 | 558 |
|
25485 | 559 |
val instantiation_params = #params o get_instantiation; |
25462 | 560 |
|
25514 | 561 |
fun instantiation_param lthy v = instantiation_params lthy |
25462 | 562 |
|> find_first (fn (_, (v', _)) => v = v') |
563 |
|> Option.map (fst o fst); |
|
564 |
||
565 |
||
566 |
(* syntax *) |
|
567 |
||
26238 | 568 |
fun synchronize_inst_syntax ctxt = |
25462 | 569 |
let |
26259 | 570 |
val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt; |
26238 | 571 |
val thy = ProofContext.theory_of ctxt; |
572 |
fun subst (c, ty) = case AxClass.inst_tyco_of thy (c, ty) |
|
573 |
of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
|
574 |
of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty) |
|
575 |
| NONE => NONE) |
|
576 |
| NONE => NONE; |
|
577 |
val unchecks = |
|
578 |
map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params; |
|
579 |
in |
|
580 |
ctxt |
|
26259 | 581 |
|> Overloading.map_improvable_syntax |
26730 | 582 |
(fn (((primary_constraints, _), (((improve, _), _), _)), _) => |
583 |
(((primary_constraints, []), (((improve, subst), false), unchecks)), false)) |
|
26238 | 584 |
end; |
25462 | 585 |
|
586 |
||
587 |
(* target *) |
|
588 |
||
25485 | 589 |
val sanatize_name = (*FIXME*) |
590 |
let |
|
25574 | 591 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s |
592 |
orelse s = "'" orelse s = "_"; |
|
25485 | 593 |
val is_junk = not o is_valid andf Symbol.is_regular; |
594 |
val junk = Scan.many is_junk; |
|
595 |
val scan_valids = Symbol.scanner "Malformed input" |
|
596 |
((junk |-- |
|
597 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
598 |
--| junk)) |
|
25999 | 599 |
::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk)); |
25485 | 600 |
in |
601 |
explode #> scan_valids #> implode |
|
602 |
end; |
|
603 |
||
26259 | 604 |
fun type_name "*" = "prod" |
605 |
| type_name "+" = "sum" |
|
606 |
| type_name s = sanatize_name (NameSpace.base s); (*FIXME*) |
|
607 |
||
26518 | 608 |
fun resort_terms pp algebra consts constraints ts = |
609 |
let |
|
610 |
fun matchings (Const (c_ty as (c, _))) = (case constraints c |
|
611 |
of NONE => I |
|
612 |
| SOME sorts => fold2 (curry (Sorts.meet_sort algebra)) |
|
613 |
(Consts.typargs consts c_ty) sorts) |
|
614 |
| matchings _ = I |
|
615 |
val tvartab = (fold o fold_aterms) matchings ts Vartab.empty |
|
26642
454d11701fa4
Sorts.class_error: produce message only (formerly msg_class_error);
wenzelm
parents:
26628
diff
changeset
|
616 |
handle Sorts.CLASS_ERROR e => error (Sorts.class_error pp e); |
27089
480f19078b65
fixed wrong treatment of type variables in instantiation target
haftmann
parents:
26995
diff
changeset
|
617 |
val inst = map_type_tvar |
480f19078b65
fixed wrong treatment of type variables in instantiation target
haftmann
parents:
26995
diff
changeset
|
618 |
(fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi))); |
26518 | 619 |
in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end; |
620 |
||
25864 | 621 |
fun init_instantiation (tycos, vs, sort) thy = |
25462 | 622 |
let |
25536 | 623 |
val _ = if null tycos then error "At least one arity must be given" else (); |
26518 | 624 |
val params = these_params thy sort; |
625 |
fun get_param tyco (param, (_, (c, ty))) = if can (AxClass.param_of_inst thy) (c, tyco) |
|
25603 | 626 |
then NONE else SOME ((c, tyco), |
25864 | 627 |
(param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty)); |
26518 | 628 |
val inst_params = map_product get_param tycos params |> map_filter I; |
629 |
val primary_constraints = map (apsnd |
|
630 |
(map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) params; |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26761
diff
changeset
|
631 |
val pp = Syntax.pp_global thy; |
26518 | 632 |
val algebra = Sign.classes_of thy |
633 |
|> fold (fn tyco => Sorts.add_arities pp |
|
634 |
(tyco, map (fn class => (class, map snd vs)) sort)) tycos; |
|
635 |
val consts = Sign.consts_of thy; |
|
636 |
val improve_constraints = AList.lookup (op =) |
|
637 |
(map (fn (_, (class, (c, _))) => (c, [[class]])) params); |
|
638 |
fun resort_check ts ctxt = case resort_terms pp algebra consts improve_constraints ts |
|
639 |
of NONE => NONE |
|
640 |
| SOME ts' => SOME (ts', ctxt); |
|
641 |
fun improve (c, ty) = case AxClass.inst_tyco_of thy (c, ty) |
|
26329
3e58e4c67a2a
instantiation less liberal with dangling constraints
haftmann
parents:
26259
diff
changeset
|
642 |
of SOME tyco => (case AList.lookup (op =) inst_params (c, tyco) |
26518 | 643 |
of SOME (_, ty') => if Type.raw_instance (ty', ty) then SOME (ty, ty') else NONE |
26259 | 644 |
| NONE => NONE) |
645 |
| NONE => NONE; |
|
25485 | 646 |
in |
647 |
thy |
|
648 |
|> ProofContext.init |
|
26329
3e58e4c67a2a
instantiation less liberal with dangling constraints
haftmann
parents:
26259
diff
changeset
|
649 |
|> Instantiation.put (mk_instantiation ((tycos, vs, sort), inst_params)) |
27281 | 650 |
|> fold (Variable.declare_typ o TFree) vs |
26329
3e58e4c67a2a
instantiation less liberal with dangling constraints
haftmann
parents:
26259
diff
changeset
|
651 |
|> fold (Variable.declare_names o Free o snd) inst_params |
26259 | 652 |
|> (Overloading.map_improvable_syntax o apfst) |
26329
3e58e4c67a2a
instantiation less liberal with dangling constraints
haftmann
parents:
26259
diff
changeset
|
653 |
(fn ((_, _), ((_, subst), unchecks)) => |
26730 | 654 |
((primary_constraints, []), (((improve, K NONE), false), []))) |
26259 | 655 |
|> Overloading.add_improvable_syntax |
26518 | 656 |
|> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check) |
26238 | 657 |
|> synchronize_inst_syntax |
25485 | 658 |
end; |
659 |
||
26238 | 660 |
fun confirm_declaration c = (map_instantiation o apsnd) |
661 |
(filter_out (fn (_, (c', _)) => c' = c)) |
|
662 |
#> LocalTheory.target synchronize_inst_syntax |
|
663 |
||
25485 | 664 |
fun gen_instantiation_instance do_proof after_qed lthy = |
665 |
let |
|
25864 | 666 |
val (tycos, vs, sort) = (#arities o the_instantiation) lthy; |
667 |
val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos; |
|
25462 | 668 |
fun after_qed' results = |
669 |
LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) |
|
670 |
#> after_qed; |
|
671 |
in |
|
672 |
lthy |
|
673 |
|> do_proof after_qed' arities_proof |
|
674 |
end; |
|
675 |
||
25485 | 676 |
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts => |
25462 | 677 |
Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts)); |
678 |
||
25485 | 679 |
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed => |
25502 | 680 |
fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t |
681 |
(fn {context, ...} => tac context)) ts) lthy) I; |
|
25462 | 682 |
|
28666 | 683 |
fun prove_instantiation_exit tac = prove_instantiation_instance tac |
684 |
#> LocalTheory.exit_global; |
|
685 |
||
686 |
fun prove_instantiation_exit_result f tac x lthy = |
|
687 |
let |
|
688 |
val phi = ProofContext.export_morphism lthy |
|
689 |
(ProofContext.init (ProofContext.theory_of lthy)); |
|
690 |
val y = f phi x; |
|
691 |
in |
|
692 |
lthy |
|
693 |
|> prove_instantiation_exit (fn ctxt => tac ctxt y) |
|
694 |
|> pair y |
|
695 |
end; |
|
696 |
||
25462 | 697 |
fun conclude_instantiation lthy = |
698 |
let |
|
25485 | 699 |
val { arities, params } = the_instantiation lthy; |
25864 | 700 |
val (tycos, vs, sort) = arities; |
25462 | 701 |
val thy = ProofContext.theory_of lthy; |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset
|
702 |
val _ = map (fn tyco => if Sign.of_sort thy |
25864 | 703 |
(Type (tyco, map TFree vs), sort) |
25462 | 704 |
then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco))) |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset
|
705 |
tycos; |
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset
|
706 |
in lthy end; |
25462 | 707 |
|
25603 | 708 |
fun pretty_instantiation lthy = |
709 |
let |
|
710 |
val { arities, params } = the_instantiation lthy; |
|
25864 | 711 |
val (tycos, vs, sort) = arities; |
25603 | 712 |
val thy = ProofContext.theory_of lthy; |
25864 | 713 |
fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort); |
25603 | 714 |
fun pr_param ((c, _), (v, ty)) = |
25864 | 715 |
(Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==", |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26761
diff
changeset
|
716 |
(Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty]; |
25603 | 717 |
in |
718 |
(Pretty.block o Pretty.fbreaks) |
|
719 |
(Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params) |
|
720 |
end; |
|
721 |
||
24218 | 722 |
end; |
25683 | 723 |