author | haftmann |
Tue, 22 Sep 2009 08:58:08 +0200 | |
changeset 32640 | ba6531df2c64 |
parent 32379 | a97e9caebd60 |
child 32805 | 9b535493ac8d |
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 |
val register: class -> class list -> ((string * typ) * (string * typ)) list |
29526 | 11 |
-> sort -> morphism -> thm option -> thm option -> thm |
12 |
-> theory -> theory |
|
25485 | 13 |
|
29526 | 14 |
val is_class: theory -> class -> bool |
15 |
val base_sort: theory -> class -> sort |
|
16 |
val rules: theory -> class -> thm option * thm |
|
17 |
val these_params: theory -> sort -> (string * (class * (string * typ))) list |
|
18 |
val these_defs: theory -> sort -> thm list |
|
29577 | 19 |
val these_operations: theory -> sort -> (string * (class * (typ * term))) list |
29526 | 20 |
val print_classes: theory -> unit |
21 |
||
29358 | 22 |
val begin: class list -> sort -> Proof.context -> Proof.context |
25311 | 23 |
val init: class -> theory -> Proof.context |
28017 | 24 |
val declare: class -> Properties.T |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
25 |
-> (binding * mixfix) * term -> theory -> theory |
28017 | 26 |
val abbrev: class -> Syntax.mode -> Properties.T |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
27 |
-> (binding * mixfix) * term -> theory -> theory |
29526 | 28 |
val class_prefix: string -> string |
25083 | 29 |
val refresh_syntax: class -> Proof.context -> Proof.context |
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
30 |
val redeclare_operations: theory -> sort -> Proof.context -> Proof.context |
25485 | 31 |
|
25462 | 32 |
(*instances*) |
26247 | 33 |
val init_instantiation: string list * (string * sort) list * sort |
34 |
-> theory -> local_theory |
|
31869 | 35 |
val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state |
26247 | 36 |
val instantiation_instance: (local_theory -> local_theory) |
37 |
-> local_theory -> Proof.state |
|
38 |
val prove_instantiation_instance: (Proof.context -> tactic) |
|
39 |
-> local_theory -> local_theory |
|
28666 | 40 |
val prove_instantiation_exit: (Proof.context -> tactic) |
41 |
-> local_theory -> theory |
|
42 |
val prove_instantiation_exit_result: (morphism -> 'a -> 'b) |
|
43 |
-> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory |
|
25485 | 44 |
val conclude_instantiation: local_theory -> local_theory |
30519
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
45 |
val instantiation_param: local_theory -> binding -> string option |
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
46 |
val confirm_declaration: binding -> local_theory -> local_theory |
25603 | 47 |
val pretty_instantiation: local_theory -> Pretty.T |
31869 | 48 |
val read_multi_arity: theory -> xstring list * xstring list * xstring |
49 |
-> string list * (string * sort) list * sort |
|
26259 | 50 |
val type_name: string -> string |
25485 | 51 |
|
31635 | 52 |
(*subclasses*) |
53 |
val register_subclass: class * class -> morphism option -> Element.witness option |
|
54 |
-> morphism -> theory -> theory |
|
55 |
val classrel: class * class -> theory -> Proof.state |
|
56 |
val classrel_cmd: xstring * xstring -> theory -> Proof.state |
|
57 |
||
58 |
(*tactics*) |
|
29526 | 59 |
val intro_classes_tac: thm list -> tactic |
60 |
val default_intro_tac: Proof.context -> thm list -> tactic |
|
61 |
||
25462 | 62 |
(*old axclass layer*) |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
63 |
val axclass_cmd: binding * xstring list |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28083
diff
changeset
|
64 |
-> (Attrib.binding * string list) list |
25462 | 65 |
-> theory -> class * theory |
24218 | 66 |
end; |
67 |
||
29358 | 68 |
structure Class_Target : CLASS_TARGET = |
24218 | 69 |
struct |
70 |
||
24589 | 71 |
(** class data **) |
24218 | 72 |
|
73 |
datatype class_data = ClassData of { |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
74 |
|
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
75 |
(* static part *) |
24218 | 76 |
consts: (string * string) list |
24836 | 77 |
(*locale parameter ~> constant name*), |
25062 | 78 |
base_sort: sort, |
29545 | 79 |
base_morph: morphism |
29439 | 80 |
(*static part of canonical morphism*), |
25618 | 81 |
assm_intro: thm option, |
82 |
of_class: thm, |
|
83 |
axiom: thm option, |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
84 |
|
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
85 |
(* dynamic part *) |
24657 | 86 |
defs: thm list, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
87 |
operations: (string * (class * (typ * term))) list |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
88 |
|
24657 | 89 |
}; |
24218 | 90 |
|
31599 | 91 |
fun make_class_data ((consts, base_sort, base_morph, assm_intro, of_class, axiom), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
92 |
(defs, operations)) = |
29526 | 93 |
ClassData { consts = consts, base_sort = base_sort, |
29439 | 94 |
base_morph = base_morph, assm_intro = assm_intro, of_class = of_class, axiom = axiom, |
25618 | 95 |
defs = defs, operations = operations }; |
29526 | 96 |
fun map_class_data f (ClassData { consts, base_sort, base_morph, assm_intro, |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
97 |
of_class, axiom, defs, operations }) = |
31599 | 98 |
make_class_data (f ((consts, base_sort, base_morph, assm_intro, of_class, axiom), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
99 |
(defs, operations))); |
25038 | 100 |
fun merge_class_data _ (ClassData { consts = consts, |
29526 | 101 |
base_sort = base_sort, base_morph = base_morph, assm_intro = assm_intro, |
25618 | 102 |
of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 }, |
29526 | 103 |
ClassData { consts = _, base_sort = _, base_morph = _, assm_intro = _, |
25618 | 104 |
of_class = _, axiom = _, defs = defs2, operations = operations2 }) = |
31599 | 105 |
make_class_data ((consts, base_sort, base_morph, assm_intro, of_class, axiom), |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
106 |
(Thm.merge_thms (defs1, defs2), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
107 |
AList.merge (op =) (K true) (operations1, operations2))); |
24218 | 108 |
|
109 |
structure ClassData = TheoryDataFun |
|
110 |
( |
|
25038 | 111 |
type T = class_data Graph.T |
112 |
val empty = Graph.empty; |
|
24218 | 113 |
val copy = I; |
114 |
val extend = I; |
|
25038 | 115 |
fun merge _ = Graph.join merge_class_data; |
24218 | 116 |
); |
117 |
||
118 |
||
119 |
(* queries *) |
|
120 |
||
31599 | 121 |
fun lookup_class_data thy class = case try (Graph.get_node (ClassData.get thy)) class |
122 |
of SOME (ClassData data) => SOME data |
|
123 |
| NONE => NONE; |
|
24218 | 124 |
|
24589 | 125 |
fun the_class_data thy class = case lookup_class_data thy class |
25020 | 126 |
of NONE => error ("Undeclared class " ^ quote class) |
24589 | 127 |
| SOME data => data; |
24218 | 128 |
|
25038 | 129 |
val is_class = is_some oo lookup_class_data; |
130 |
||
131 |
val ancestry = Graph.all_succs o ClassData.get; |
|
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
132 |
val heritage = Graph.all_preds o ClassData.get; |
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
133 |
|
25002 | 134 |
fun these_params thy = |
24218 | 135 |
let |
136 |
fun params class = |
|
137 |
let |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
138 |
val const_typs = (#params o AxClass.get_info thy) class; |
24657 | 139 |
val const_names = (#consts o the_class_data thy) class; |
24218 | 140 |
in |
26518 | 141 |
(map o apsnd) |
142 |
(fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names |
|
24218 | 143 |
end; |
144 |
in maps params o ancestry thy end; |
|
145 |
||
29526 | 146 |
val base_sort = #base_sort oo the_class_data; |
147 |
||
148 |
fun rules thy class = |
|
149 |
let val { axiom, of_class, ... } = the_class_data thy class |
|
150 |
in (axiom, of_class) end; |
|
151 |
||
152 |
fun all_assm_intros thy = |
|
31599 | 153 |
Graph.fold (fn (_, (ClassData { assm_intro, ... }, _)) => fold (insert Thm.eq_thm) |
154 |
(the_list assm_intro)) (ClassData.get thy) []; |
|
24218 | 155 |
|
29526 | 156 |
fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy; |
157 |
fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy; |
|
29358 | 158 |
|
29526 | 159 |
val base_morphism = #base_morph oo the_class_data; |
160 |
fun morphism thy class = base_morphism thy class |
|
161 |
$> Element.eq_morphism thy (these_defs thy [class]); |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
162 |
|
24218 | 163 |
fun print_classes thy = |
164 |
let |
|
24920 | 165 |
val ctxt = ProofContext.init thy; |
24218 | 166 |
val algebra = Sign.classes_of thy; |
167 |
val arities = |
|
168 |
Symtab.empty |
|
169 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
170 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
171 |
((#arities o Sorts.rep_algebra) algebra); |
|
172 |
val the_arities = these o Symtab.lookup arities; |
|
173 |
fun mk_arity class tyco = |
|
174 |
let |
|
175 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
24920 | 176 |
in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end; |
24218 | 177 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
24920 | 178 |
^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty); |
24218 | 179 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
25062 | 180 |
(SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"), |
24218 | 181 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
24920 | 182 |
(Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class], |
25062 | 183 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) |
184 |
(Pretty.str "parameters:" :: ps)) o map mk_param |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
185 |
o these o Option.map #params o try (AxClass.get_info thy)) class, |
24218 | 186 |
(SOME o Pretty.block o Pretty.breaks) [ |
187 |
Pretty.str "instances:", |
|
188 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
189 |
] |
|
190 |
] |
|
191 |
in |
|
24589 | 192 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") |
193 |
o map mk_entry o Sorts.all_classes) algebra |
|
24218 | 194 |
end; |
195 |
||
196 |
||
197 |
(* updaters *) |
|
198 |
||
29526 | 199 |
fun register class sups params base_sort morph |
29358 | 200 |
axiom assm_intro of_class thy = |
25002 | 201 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
202 |
val operations = map (fn (v_ty as (_, ty), (c, _)) => |
25683 | 203 |
(c, (class, (ty, Free v_ty)))) params; |
25038 | 204 |
val add_class = Graph.new_node (class, |
31599 | 205 |
make_class_data (((map o pairself) fst params, base_sort, |
29526 | 206 |
morph, assm_intro, of_class, axiom), ([], operations))) |
207 |
#> fold (curry Graph.add_edge class) sups; |
|
25618 | 208 |
in ClassData.map add_class thy end; |
24218 | 209 |
|
29526 | 210 |
fun activate_defs class thms thy = |
211 |
let |
|
212 |
val eq_morph = Element.eq_morphism thy thms; |
|
213 |
fun amend cls thy = Locale.amend_registration eq_morph |
|
214 |
(cls, morphism thy cls) thy; |
|
215 |
in fold amend (heritage thy [class]) thy end; |
|
216 |
||
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
217 |
fun register_operation class (c, (t, some_def)) thy = |
25062 | 218 |
let |
29358 | 219 |
val base_sort = base_sort thy class; |
29439 | 220 |
val prep_typ = map_type_tfree |
221 |
(fn (v, sort) => if Name.aT = v |
|
222 |
then TFree (v, base_sort) else TVar ((v, 0), sort)); |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
223 |
val t' = map_types prep_typ t; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
224 |
val ty' = Term.fastype_of t'; |
25062 | 225 |
in |
226 |
thy |
|
227 |
|> (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
|
228 |
(fn (defs, operations) => |
25096 | 229 |
(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
|
230 |
(c, (class, (ty', t'))) :: operations)) |
29526 | 231 |
|> is_some some_def ? activate_defs class (the_list some_def) |
25062 | 232 |
end; |
24218 | 233 |
|
29558 | 234 |
fun register_subclass (sub, sup) some_dep_morph some_wit export thy = |
25618 | 235 |
let |
29558 | 236 |
val intros = (snd o rules thy) sup :: map_filter I |
237 |
[Option.map (Drule.standard' o Element.conclude_witness) some_wit, |
|
238 |
(fst o rules thy) sub]; |
|
29610 | 239 |
val tac = EVERY (map (TRYALL o Tactic.rtac) intros); |
31635 | 240 |
val classrel = SkipProof.prove_global thy [] [] (Logic.mk_classrel (sub, sup)) |
29558 | 241 |
(K tac); |
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
242 |
val diff_sort = Sign.complete_sort thy [sup] |
31012 | 243 |
|> subtract (op =) (Sign.complete_sort thy [sub]) |
244 |
|> filter (is_class thy); |
|
32074 | 245 |
val add_dependency = case some_dep_morph |
246 |
of SOME dep_morph => Locale.add_dependency sub |
|
247 |
(sup, dep_morph $> Element.satisfy_morphism (the_list some_wit)) export |
|
248 |
| NONE => I |
|
25618 | 249 |
in |
250 |
thy |
|
251 |
|> AxClass.add_classrel classrel |
|
252 |
|> ClassData.map (Graph.add_edge (sub, sup)) |
|
29526 | 253 |
|> activate_defs sub (these_defs thy diff_sort) |
32074 | 254 |
|> add_dependency |
24218 | 255 |
end; |
256 |
||
257 |
||
24589 | 258 |
(** classes and class target **) |
24218 | 259 |
|
25002 | 260 |
(* class context syntax *) |
24748 | 261 |
|
29577 | 262 |
fun these_unchecks thy = |
263 |
map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy; |
|
264 |
||
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
265 |
fun redeclare_const thy c = |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30344
diff
changeset
|
266 |
let val b = Long_Name.base_name c |
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
267 |
in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end; |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
268 |
|
29577 | 269 |
fun synchronize_class_syntax sort base_sort ctxt = |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
270 |
let |
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset
|
271 |
val thy = ProofContext.theory_of ctxt; |
26596 | 272 |
val algebra = Sign.classes_of thy; |
29577 | 273 |
val operations = these_operations thy sort; |
26518 | 274 |
fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort))); |
275 |
val primary_constraints = |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
276 |
(map o apsnd) (subst_class_typ base_sort o fst o snd) operations; |
26518 | 277 |
val secondary_constraints = |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
278 |
(map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations; |
26518 | 279 |
fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c |
26238 | 280 |
of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty |
281 |
of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0) |
|
26596 | 282 |
of SOME (_, ty' as TVar (tvar as (vi, sort))) => |
26238 | 283 |
if TypeInfer.is_param vi |
26596 | 284 |
andalso Sorts.sort_le algebra (base_sort, sort) |
285 |
then SOME (ty', TFree (Name.aT, base_sort)) |
|
286 |
else NONE |
|
26238 | 287 |
| _ => NONE) |
288 |
| NONE => NONE) |
|
289 |
| NONE => NONE) |
|
290 |
fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c); |
|
29577 | 291 |
val unchecks = these_unchecks thy sort; |
25083 | 292 |
in |
293 |
ctxt |
|
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
294 |
|> fold (redeclare_const thy o fst) primary_constraints |
26518 | 295 |
|> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints), |
26730 | 296 |
(((improve, subst), true), unchecks)), false)) |
26518 | 297 |
|> Overloading.set_primary_constraints |
25083 | 298 |
end; |
299 |
||
300 |
fun refresh_syntax class ctxt = |
|
25002 | 301 |
let |
302 |
val thy = ProofContext.theory_of ctxt; |
|
29358 | 303 |
val base_sort = base_sort thy class; |
26238 | 304 |
in synchronize_class_syntax [class] base_sort ctxt end; |
25002 | 305 |
|
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
306 |
fun redeclare_operations thy sort = |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
307 |
fold (redeclare_const thy o fst) (these_operations thy sort); |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
308 |
|
29577 | 309 |
fun begin sort base_sort ctxt = |
25083 | 310 |
ctxt |
311 |
|> Variable.declare_term |
|
312 |
(Logic.mk_type (TFree (Name.aT, base_sort))) |
|
29577 | 313 |
|> synchronize_class_syntax sort base_sort |
26238 | 314 |
|> Overloading.add_improvable_syntax; |
24901
d3cbf79769b9
added first version of user-space type system for class target
haftmann
parents:
24847
diff
changeset
|
315 |
|
25311 | 316 |
fun init class thy = |
317 |
thy |
|
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
318 |
|> Locale.init class |
29358 | 319 |
|> begin [class] (base_sort thy class); |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
320 |
|
24748 | 321 |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
322 |
(* class target *) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
323 |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30344
diff
changeset
|
324 |
val class_prefix = Logic.const_of_class o Long_Name.base_name; |
29526 | 325 |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
326 |
fun declare class pos ((c, mx), dict) thy = |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
327 |
let |
29577 | 328 |
val morph = morphism thy class; |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
329 |
val b = Morphism.binding morph c; |
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
330 |
val b_def = Morphism.binding morph (Binding.suffix_name "_dict" b); |
29577 | 331 |
val c' = Sign.full_name thy b; |
29439 | 332 |
val dict' = Morphism.term morph dict; |
333 |
val ty' = Term.fastype_of dict'; |
|
29577 | 334 |
val def_eq = Logic.mk_equals (Const (c', ty'), dict') |
335 |
|> map_types Type.strip_sorts; |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
336 |
in |
29577 | 337 |
thy |
338 |
|> Sign.declare_const pos ((b, Type.strip_sorts ty'), mx) |
|
339 |
|> snd |
|
340 |
|> Thm.add_def false false (b_def, def_eq) |
|
341 |
|>> Thm.varifyT |
|
342 |
|-> (fn def_thm => PureThy.store_thm (b_def, def_thm) |
|
343 |
#> snd |
|
344 |
#> register_operation class (c', (dict', SOME (Thm.symmetric def_thm)))) |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
345 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
346 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
347 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
348 |
fun abbrev class prmode pos ((c, mx), rhs) thy = |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
349 |
let |
29577 | 350 |
val morph = morphism thy class; |
351 |
val unchecks = these_unchecks thy [class]; |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
352 |
val b = Morphism.binding morph c; |
29577 | 353 |
val c' = Sign.full_name thy b; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
354 |
val rhs' = Pattern.rewrite_term thy unchecks [] rhs; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
355 |
val ty' = Term.fastype_of rhs'; |
31697 | 356 |
val rhs'' = map_types Logic.varifyT rhs'; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
357 |
in |
29577 | 358 |
thy |
359 |
|> Sign.add_abbrev (#1 prmode) pos (b, rhs'') |
|
360 |
|> snd |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
361 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
362 |
|> Sign.notation true prmode [(Const (c', ty'), mx)] |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
363 |
|> 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
|
364 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
365 |
|
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
366 |
|
31635 | 367 |
(* simple subclasses *) |
368 |
||
369 |
local |
|
370 |
||
371 |
fun gen_classrel mk_prop classrel thy = |
|
372 |
let |
|
373 |
fun after_qed results = |
|
374 |
ProofContext.theory ((fold o fold) AxClass.add_classrel results); |
|
375 |
in |
|
376 |
thy |
|
377 |
|> ProofContext.init |
|
378 |
|> Proof.theorem_i NONE after_qed [[(mk_prop thy classrel, [])]] |
|
379 |
end; |
|
380 |
||
381 |
in |
|
382 |
||
383 |
val classrel = |
|
384 |
gen_classrel (Logic.mk_classrel oo AxClass.cert_classrel); |
|
385 |
val classrel_cmd = |
|
386 |
gen_classrel (Logic.mk_classrel oo AxClass.read_classrel); |
|
387 |
||
388 |
end; (*local*) |
|
389 |
||
390 |
||
25462 | 391 |
(** instantiation target **) |
392 |
||
393 |
(* bookkeeping *) |
|
394 |
||
395 |
datatype instantiation = Instantiation of { |
|
25864 | 396 |
arities: string list * (string * sort) list * sort, |
25462 | 397 |
params: ((string * string) * (string * typ)) list |
25603 | 398 |
(*(instantiation parameter, type constructor), (local instantiation parameter, typ)*) |
25462 | 399 |
} |
400 |
||
401 |
structure Instantiation = ProofDataFun |
|
402 |
( |
|
403 |
type T = instantiation |
|
25536 | 404 |
fun init _ = Instantiation { arities = ([], [], []), params = [] }; |
25462 | 405 |
); |
406 |
||
25485 | 407 |
fun mk_instantiation (arities, params) = |
408 |
Instantiation { arities = arities, params = params }; |
|
25514 | 409 |
fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy) |
25485 | 410 |
of Instantiation data => data; |
25514 | 411 |
fun map_instantiation f = (LocalTheory.target o Instantiation.map) |
412 |
(fn Instantiation { arities, params } => mk_instantiation (f (arities, params))); |
|
25462 | 413 |
|
25514 | 414 |
fun the_instantiation lthy = case get_instantiation lthy |
25536 | 415 |
of { arities = ([], [], []), ... } => error "No instantiation target" |
25485 | 416 |
| data => data; |
25462 | 417 |
|
25485 | 418 |
val instantiation_params = #params o get_instantiation; |
25462 | 419 |
|
30519
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
420 |
fun instantiation_param lthy b = instantiation_params lthy |
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
421 |
|> find_first (fn (_, (v, _)) => Binding.name_of b = v) |
25462 | 422 |
|> Option.map (fst o fst); |
423 |
||
31869 | 424 |
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) = |
425 |
let |
|
426 |
val all_arities = map (fn raw_tyco => Sign.read_arity thy |
|
427 |
(raw_tyco, raw_sorts, raw_sort)) raw_tycos; |
|
428 |
val tycos = map #1 all_arities; |
|
429 |
val (_, sorts, sort) = hd all_arities; |
|
430 |
val vs = Name.names Name.context Name.aT sorts; |
|
431 |
in (tycos, vs, sort) end; |
|
432 |
||
25462 | 433 |
|
434 |
(* syntax *) |
|
435 |
||
26238 | 436 |
fun synchronize_inst_syntax ctxt = |
25462 | 437 |
let |
26259 | 438 |
val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt; |
31249 | 439 |
|
440 |
val lookup_inst_param = AxClass.lookup_inst_param (Sign.consts_of (ProofContext.theory_of ctxt)) params; |
|
441 |
fun subst (c, ty) = case lookup_inst_param (c, ty) |
|
442 |
of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty) |
|
443 |
| NONE => NONE; |
|
26238 | 444 |
val unchecks = |
445 |
map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params; |
|
446 |
in |
|
447 |
ctxt |
|
26259 | 448 |
|> Overloading.map_improvable_syntax |
26730 | 449 |
(fn (((primary_constraints, _), (((improve, _), _), _)), _) => |
450 |
(((primary_constraints, []), (((improve, subst), false), unchecks)), false)) |
|
26238 | 451 |
end; |
25462 | 452 |
|
453 |
||
454 |
(* target *) |
|
455 |
||
25485 | 456 |
val sanatize_name = (*FIXME*) |
457 |
let |
|
25574 | 458 |
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s |
459 |
orelse s = "'" orelse s = "_"; |
|
25485 | 460 |
val is_junk = not o is_valid andf Symbol.is_regular; |
461 |
val junk = Scan.many is_junk; |
|
462 |
val scan_valids = Symbol.scanner "Malformed input" |
|
463 |
((junk |-- |
|
464 |
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) |
|
465 |
--| junk)) |
|
25999 | 466 |
::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk)); |
25485 | 467 |
in |
468 |
explode #> scan_valids #> implode |
|
469 |
end; |
|
470 |
||
26259 | 471 |
fun type_name "*" = "prod" |
472 |
| type_name "+" = "sum" |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30344
diff
changeset
|
473 |
| type_name s = sanatize_name (Long_Name.base_name s); |
26259 | 474 |
|
26518 | 475 |
fun resort_terms pp algebra consts constraints ts = |
476 |
let |
|
477 |
fun matchings (Const (c_ty as (c, _))) = (case constraints c |
|
478 |
of NONE => I |
|
479 |
| SOME sorts => fold2 (curry (Sorts.meet_sort algebra)) |
|
480 |
(Consts.typargs consts c_ty) sorts) |
|
481 |
| matchings _ = I |
|
482 |
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
|
483 |
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
|
484 |
val inst = map_type_tvar |
480f19078b65
fixed wrong treatment of type variables in instantiation target
haftmann
parents:
26995
diff
changeset
|
485 |
(fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi))); |
26518 | 486 |
in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end; |
487 |
||
25864 | 488 |
fun init_instantiation (tycos, vs, sort) thy = |
25462 | 489 |
let |
25536 | 490 |
val _ = if null tycos then error "At least one arity must be given" else (); |
31249 | 491 |
val class_params = these_params thy (filter (can (AxClass.get_info thy)) sort); |
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
492 |
fun get_param tyco (param, (_, (c, ty))) = |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
493 |
if can (AxClass.param_of_inst thy) (c, tyco) |
25603 | 494 |
then NONE else SOME ((c, tyco), |
25864 | 495 |
(param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty)); |
31249 | 496 |
val params = map_product get_param tycos class_params |> map_filter I; |
26518 | 497 |
val primary_constraints = map (apsnd |
31249 | 498 |
(map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params; |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26761
diff
changeset
|
499 |
val pp = Syntax.pp_global thy; |
26518 | 500 |
val algebra = Sign.classes_of thy |
501 |
|> fold (fn tyco => Sorts.add_arities pp |
|
502 |
(tyco, map (fn class => (class, map snd vs)) sort)) tycos; |
|
503 |
val consts = Sign.consts_of thy; |
|
504 |
val improve_constraints = AList.lookup (op =) |
|
31249 | 505 |
(map (fn (_, (class, (c, _))) => (c, [[class]])) class_params); |
26518 | 506 |
fun resort_check ts ctxt = case resort_terms pp algebra consts improve_constraints ts |
507 |
of NONE => NONE |
|
508 |
| SOME ts' => SOME (ts', ctxt); |
|
31249 | 509 |
val lookup_inst_param = AxClass.lookup_inst_param consts params; |
31210 | 510 |
val typ_instance = Type.typ_instance (Sign.tsig_of thy); |
31249 | 511 |
fun improve (c, ty) = case lookup_inst_param (c, ty) |
512 |
of SOME (_, ty') => if typ_instance (ty', ty) then SOME (ty, ty') else NONE |
|
26259 | 513 |
| NONE => NONE; |
25485 | 514 |
in |
515 |
thy |
|
32379
a97e9caebd60
additional checkpoints avoid problems in error situations
haftmann
parents:
32074
diff
changeset
|
516 |
|> Theory.checkpoint |
25485 | 517 |
|> ProofContext.init |
31249 | 518 |
|> Instantiation.put (mk_instantiation ((tycos, vs, sort), params)) |
27281 | 519 |
|> fold (Variable.declare_typ o TFree) vs |
31249 | 520 |
|> fold (Variable.declare_names o Free o snd) params |
26259 | 521 |
|> (Overloading.map_improvable_syntax o apfst) |
29969
9dbb046136d0
more precise improvement in instantiation user space type system
haftmann
parents:
29632
diff
changeset
|
522 |
(K ((primary_constraints, []), (((improve, K NONE), false), []))) |
26259 | 523 |
|> Overloading.add_improvable_syntax |
26518 | 524 |
|> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check) |
26238 | 525 |
|> synchronize_inst_syntax |
25485 | 526 |
end; |
527 |
||
30519
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
528 |
fun confirm_declaration b = (map_instantiation o apsnd) |
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
529 |
(filter_out (fn (_, (c', _)) => c' = Binding.name_of b)) |
26238 | 530 |
#> LocalTheory.target synchronize_inst_syntax |
531 |
||
25485 | 532 |
fun gen_instantiation_instance do_proof after_qed lthy = |
533 |
let |
|
25864 | 534 |
val (tycos, vs, sort) = (#arities o the_instantiation) lthy; |
535 |
val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos; |
|
25462 | 536 |
fun after_qed' results = |
537 |
LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) |
|
538 |
#> after_qed; |
|
539 |
in |
|
540 |
lthy |
|
541 |
|> do_proof after_qed' arities_proof |
|
542 |
end; |
|
543 |
||
25485 | 544 |
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts => |
25462 | 545 |
Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts)); |
546 |
||
25485 | 547 |
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed => |
25502 | 548 |
fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t |
549 |
(fn {context, ...} => tac context)) ts) lthy) I; |
|
25462 | 550 |
|
28666 | 551 |
fun prove_instantiation_exit tac = prove_instantiation_instance tac |
552 |
#> LocalTheory.exit_global; |
|
553 |
||
554 |
fun prove_instantiation_exit_result f tac x lthy = |
|
555 |
let |
|
29439 | 556 |
val morph = ProofContext.export_morphism lthy |
28666 | 557 |
(ProofContext.init (ProofContext.theory_of lthy)); |
29439 | 558 |
val y = f morph x; |
28666 | 559 |
in |
560 |
lthy |
|
561 |
|> prove_instantiation_exit (fn ctxt => tac ctxt y) |
|
562 |
|> pair y |
|
563 |
end; |
|
564 |
||
25462 | 565 |
fun conclude_instantiation lthy = |
566 |
let |
|
25485 | 567 |
val { arities, params } = the_instantiation lthy; |
25864 | 568 |
val (tycos, vs, sort) = arities; |
25462 | 569 |
val thy = ProofContext.theory_of lthy; |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset
|
570 |
val _ = map (fn tyco => if Sign.of_sort thy |
25864 | 571 |
(Type (tyco, map TFree vs), sort) |
25462 | 572 |
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
|
573 |
tycos; |
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset
|
574 |
in lthy end; |
25462 | 575 |
|
25603 | 576 |
fun pretty_instantiation lthy = |
577 |
let |
|
578 |
val { arities, params } = the_instantiation lthy; |
|
25864 | 579 |
val (tycos, vs, sort) = arities; |
25603 | 580 |
val thy = ProofContext.theory_of lthy; |
25864 | 581 |
fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort); |
25603 | 582 |
fun pr_param ((c, _), (v, ty)) = |
25864 | 583 |
(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
|
584 |
(Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty]; |
25603 | 585 |
in |
586 |
(Pretty.block o Pretty.fbreaks) |
|
587 |
(Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params) |
|
588 |
end; |
|
589 |
||
29526 | 590 |
|
31635 | 591 |
(* simplified instantiation interface with no class parameter *) |
592 |
||
31869 | 593 |
fun instance_arity_cmd raw_arities thy = |
31635 | 594 |
let |
31869 | 595 |
val (tycos, vs, sort) = read_multi_arity thy raw_arities; |
596 |
val sorts = map snd vs; |
|
597 |
val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos; |
|
31635 | 598 |
fun after_qed results = ProofContext.theory |
599 |
((fold o fold) AxClass.add_arity results); |
|
600 |
in |
|
601 |
thy |
|
602 |
|> ProofContext.init |
|
31869 | 603 |
|> Proof.theorem_i NONE after_qed (map (fn t => [(t, [])]) arities) |
31635 | 604 |
end; |
605 |
||
606 |
||
29526 | 607 |
(** tactics and methods **) |
608 |
||
609 |
fun intro_classes_tac facts st = |
|
610 |
let |
|
611 |
val thy = Thm.theory_of_thm st; |
|
612 |
val classes = Sign.all_classes thy; |
|
613 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
614 |
val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes; |
|
615 |
val assm_intros = all_assm_intros thy; |
|
616 |
in |
|
617 |
Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st |
|
618 |
end; |
|
619 |
||
620 |
fun default_intro_tac ctxt [] = |
|
29577 | 621 |
intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt [] |
29526 | 622 |
| default_intro_tac _ _ = no_tac; |
623 |
||
624 |
fun default_tac rules ctxt facts = |
|
625 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
626 |
default_intro_tac ctxt facts; |
|
627 |
||
628 |
val _ = Context.>> (Context.map_theory |
|
30515 | 629 |
(Method.setup (Binding.name "intro_classes") (Scan.succeed (K (METHOD intro_classes_tac))) |
630 |
"back-chain introduction rules of classes" #> |
|
631 |
Method.setup (Binding.name "default") (Attrib.thms >> (METHOD oo default_tac)) |
|
632 |
"apply some intro/elim rule")); |
|
29526 | 633 |
|
31635 | 634 |
|
635 |
(** old axclass command **) |
|
636 |
||
637 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy = |
|
638 |
let |
|
639 |
val _ = Output.legacy_feature "command \"axclass\" deprecated; use \"class\" instead."; |
|
640 |
val ctxt = ProofContext.init thy; |
|
641 |
val superclasses = map (Sign.read_class thy) raw_superclasses; |
|
642 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) |
|
643 |
raw_specs; |
|
644 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) |
|
645 |
raw_specs) |
|
646 |
|> snd |
|
647 |
|> (map o map) fst; |
|
648 |
in |
|
649 |
AxClass.define_class (class, superclasses) [] |
|
650 |
(name_atts ~~ axiomss) thy |
|
651 |
end; |
|
652 |
||
24218 | 653 |
end; |
25683 | 654 |