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