author | wenzelm |
Wed, 21 Mar 2012 11:25:19 +0100 | |
changeset 47061 | 355317493f34 |
parent 47005 | 421760a1efe7 |
child 47078 | 6890c02c4c12 |
permissions | -rw-r--r-- |
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
1 |
(* Title: Pure/Isar/class.ML |
24218 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
4 |
Type classes derived from primitive axclasses and locales. |
24218 | 5 |
*) |
6 |
||
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
7 |
signature CLASS = |
24218 | 8 |
sig |
25462 | 9 |
(*classes*) |
29526 | 10 |
val is_class: theory -> class -> bool |
11 |
val these_params: theory -> sort -> (string * (class * (string * typ))) list |
|
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
12 |
val base_sort: theory -> class -> sort |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
13 |
val rules: theory -> class -> thm option * thm |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
14 |
val these_defs: theory -> sort -> thm list |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
15 |
val these_operations: theory -> sort |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
16 |
-> (string * (class * (typ * term))) list |
42359 | 17 |
val print_classes: Proof.context -> unit |
25311 | 18 |
val init: class -> theory -> Proof.context |
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
19 |
val begin: class list -> sort -> Proof.context -> Proof.context |
38619
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
20 |
val const: class -> (binding * mixfix) * (term list * term) -> local_theory -> local_theory |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
21 |
val abbrev: class -> Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory |
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
22 |
val redeclare_operations: theory -> sort -> Proof.context -> Proof.context |
29526 | 23 |
val class_prefix: string -> string |
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
24 |
val register: class -> class list -> ((string * typ) * (string * typ)) list |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
25 |
-> sort -> morphism -> morphism -> thm option -> thm option -> thm |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
26 |
-> theory -> theory |
25485 | 27 |
|
25462 | 28 |
(*instances*) |
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
29 |
val instantiation: string list * (string * sort) list * sort -> theory -> local_theory |
26247 | 30 |
val instantiation_instance: (local_theory -> local_theory) |
31 |
-> local_theory -> Proof.state |
|
32 |
val prove_instantiation_instance: (Proof.context -> tactic) |
|
33 |
-> local_theory -> local_theory |
|
28666 | 34 |
val prove_instantiation_exit: (Proof.context -> tactic) |
35 |
-> local_theory -> theory |
|
36 |
val prove_instantiation_exit_result: (morphism -> 'a -> 'b) |
|
37 |
-> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory |
|
31869 | 38 |
val read_multi_arity: theory -> xstring list * xstring list * xstring |
39 |
-> string list * (string * sort) list * sort |
|
38348
cf7b2121ad9d
moved instantiation target formally to class_target.ML
haftmann
parents:
38107
diff
changeset
|
40 |
val instantiation_cmd: xstring list * xstring list * xstring -> theory -> local_theory |
38377 | 41 |
val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state |
25485 | 42 |
|
31635 | 43 |
(*subclasses*) |
44 |
val classrel: class * class -> theory -> Proof.state |
|
45 |
val classrel_cmd: xstring * xstring -> theory -> Proof.state |
|
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
46 |
val register_subclass: class * class -> morphism option -> Element.witness option |
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
47 |
-> morphism -> theory -> theory |
31635 | 48 |
|
49 |
(*tactics*) |
|
29526 | 50 |
val intro_classes_tac: thm list -> tactic |
51 |
val default_intro_tac: Proof.context -> thm list -> tactic |
|
24218 | 52 |
end; |
53 |
||
38379
67d71449e85b
more convenient split of class modules: class and class_declaration
haftmann
parents:
38377
diff
changeset
|
54 |
structure Class: CLASS = |
24218 | 55 |
struct |
56 |
||
24589 | 57 |
(** class data **) |
24218 | 58 |
|
46919 | 59 |
datatype class_data = Class_Data of { |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
60 |
|
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
61 |
(* static part *) |
24218 | 62 |
consts: (string * string) list |
24836 | 63 |
(*locale parameter ~> constant name*), |
25062 | 64 |
base_sort: sort, |
29545 | 65 |
base_morph: morphism |
29439 | 66 |
(*static part of canonical morphism*), |
32850 | 67 |
export_morph: morphism, |
25618 | 68 |
assm_intro: thm option, |
69 |
of_class: thm, |
|
70 |
axiom: thm option, |
|
42359 | 71 |
|
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
72 |
(* dynamic part *) |
24657 | 73 |
defs: thm list, |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
74 |
operations: (string * (class * (typ * term))) list |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
75 |
|
24657 | 76 |
}; |
24218 | 77 |
|
32850 | 78 |
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
|
79 |
(defs, operations)) = |
46919 | 80 |
Class_Data {consts = consts, base_sort = base_sort, |
32850 | 81 |
base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro, |
46919 | 82 |
of_class = of_class, axiom = axiom, defs = defs, operations = operations}; |
83 |
fun map_class_data f (Class_Data {consts, base_sort, base_morph, export_morph, assm_intro, |
|
84 |
of_class, axiom, defs, operations}) = |
|
32850 | 85 |
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
|
86 |
(defs, operations))); |
46919 | 87 |
fun merge_class_data _ (Class_Data {consts = consts, |
32850 | 88 |
base_sort = base_sort, base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro, |
46919 | 89 |
of_class = of_class, axiom = axiom, defs = defs1, operations = operations1}, |
90 |
Class_Data {consts = _, base_sort = _, base_morph = _, export_morph = _, assm_intro = _, |
|
91 |
of_class = _, axiom = _, defs = defs2, operations = operations2}) = |
|
32850 | 92 |
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
|
93 |
(Thm.merge_thms (defs1, defs2), |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
94 |
AList.merge (op =) (K true) (operations1, operations2))); |
24218 | 95 |
|
46919 | 96 |
structure Class_Data = Theory_Data |
24218 | 97 |
( |
25038 | 98 |
type T = class_data Graph.T |
99 |
val empty = Graph.empty; |
|
24218 | 100 |
val extend = I; |
33522 | 101 |
val merge = Graph.join merge_class_data; |
24218 | 102 |
); |
103 |
||
104 |
||
105 |
(* queries *) |
|
106 |
||
46919 | 107 |
fun lookup_class_data thy class = |
108 |
(case try (Graph.get_node (Class_Data.get thy)) class of |
|
109 |
SOME (Class_Data data) => SOME data |
|
110 |
| NONE => NONE); |
|
24218 | 111 |
|
46919 | 112 |
fun the_class_data thy class = |
113 |
(case lookup_class_data thy class of |
|
114 |
NONE => error ("Undeclared class " ^ quote class) |
|
115 |
| SOME data => data); |
|
24218 | 116 |
|
25038 | 117 |
val is_class = is_some oo lookup_class_data; |
118 |
||
46919 | 119 |
val ancestry = Graph.all_succs o Class_Data.get; |
120 |
val heritage = Graph.all_preds o Class_Data.get; |
|
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
121 |
|
25002 | 122 |
fun these_params thy = |
24218 | 123 |
let |
124 |
fun params class = |
|
125 |
let |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
126 |
val const_typs = (#params o AxClass.get_info thy) class; |
24657 | 127 |
val const_names = (#consts o the_class_data thy) class; |
24218 | 128 |
in |
26518 | 129 |
(map o apsnd) |
130 |
(fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names |
|
24218 | 131 |
end; |
132 |
in maps params o ancestry thy end; |
|
133 |
||
29526 | 134 |
val base_sort = #base_sort oo the_class_data; |
135 |
||
136 |
fun rules thy class = |
|
46919 | 137 |
let val {axiom, of_class, ...} = the_class_data thy class |
29526 | 138 |
in (axiom, of_class) end; |
139 |
||
140 |
fun all_assm_intros thy = |
|
46919 | 141 |
Graph.fold (fn (_, (Class_Data {assm_intro, ...}, _)) => fold (insert Thm.eq_thm) |
142 |
(the_list assm_intro)) (Class_Data.get thy) []; |
|
24218 | 143 |
|
29526 | 144 |
fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy; |
145 |
fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy; |
|
29358 | 146 |
|
29526 | 147 |
val base_morphism = #base_morph oo the_class_data; |
46919 | 148 |
fun morphism thy class = |
149 |
(case Element.eq_morphism thy (these_defs thy [class]) of |
|
150 |
SOME eq_morph => base_morphism thy class $> eq_morph |
|
151 |
| NONE => base_morphism thy class); |
|
32850 | 152 |
val export_morphism = #export_morph oo the_class_data; |
28715
238f9966c80e
class morphism stemming from locale interpretation
haftmann
parents:
28674
diff
changeset
|
153 |
|
42359 | 154 |
fun print_classes ctxt = |
24218 | 155 |
let |
42360 | 156 |
val thy = Proof_Context.theory_of ctxt; |
24218 | 157 |
val algebra = Sign.classes_of thy; |
158 |
val arities = |
|
159 |
Symtab.empty |
|
160 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
161 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
36328
4d9deabf6474
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm
parents:
36323
diff
changeset
|
162 |
(Sorts.arities_of algebra); |
24218 | 163 |
val the_arities = these o Symtab.lookup arities; |
164 |
fun mk_arity class tyco = |
|
165 |
let |
|
166 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
24920 | 167 |
in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end; |
42359 | 168 |
fun mk_param (c, ty) = |
42360 | 169 |
Pretty.str (Proof_Context.extern_const ctxt c ^ " :: " ^ |
42359 | 170 |
Syntax.string_of_typ (Config.put show_sorts false ctxt) (Type.strip_sorts ty)); |
24218 | 171 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
42360 | 172 |
(SOME o Pretty.str) ("class " ^ Proof_Context.extern_class ctxt class ^ ":"), |
24218 | 173 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
24920 | 174 |
(Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class], |
25062 | 175 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) |
176 |
(Pretty.str "parameters:" :: ps)) o map mk_param |
|
24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset
|
177 |
o these o Option.map #params o try (AxClass.get_info thy)) class, |
24218 | 178 |
(SOME o Pretty.block o Pretty.breaks) [ |
179 |
Pretty.str "instances:", |
|
180 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
181 |
] |
|
182 |
] |
|
183 |
in |
|
24589 | 184 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") |
185 |
o map mk_entry o Sorts.all_classes) algebra |
|
24218 | 186 |
end; |
187 |
||
188 |
||
189 |
(* updaters *) |
|
190 |
||
32850 | 191 |
fun register class sups params base_sort base_morph export_morph |
29358 | 192 |
axiom assm_intro of_class thy = |
25002 | 193 |
let |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
194 |
val operations = map (fn (v_ty as (_, ty), (c, _)) => |
25683 | 195 |
(c, (class, (ty, Free v_ty)))) params; |
25038 | 196 |
val add_class = Graph.new_node (class, |
31599 | 197 |
make_class_data (((map o pairself) fst params, base_sort, |
32850 | 198 |
base_morph, export_morph, assm_intro, of_class, axiom), ([], operations))) |
29526 | 199 |
#> fold (curry Graph.add_edge class) sups; |
46919 | 200 |
in Class_Data.map add_class thy end; |
24218 | 201 |
|
46919 | 202 |
fun activate_defs class thms thy = |
203 |
(case Element.eq_morphism thy thms of |
|
204 |
SOME eq_morph => fold (fn cls => fn thy => |
|
38107 | 205 |
Context.theory_map (Locale.amend_registration (cls, base_morphism thy cls) |
206 |
(eq_morph, true) (export_morphism thy cls)) thy) (heritage thy [class]) thy |
|
46919 | 207 |
| NONE => thy); |
29526 | 208 |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
209 |
fun register_operation class (c, (t, some_def)) thy = |
25062 | 210 |
let |
29358 | 211 |
val base_sort = base_sort thy class; |
29439 | 212 |
val prep_typ = map_type_tfree |
213 |
(fn (v, sort) => if Name.aT = v |
|
214 |
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
|
215 |
val t' = map_types prep_typ t; |
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
216 |
val ty' = Term.fastype_of t'; |
25062 | 217 |
in |
218 |
thy |
|
46919 | 219 |
|> (Class_Data.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
|
220 |
(fn (defs, operations) => |
25096 | 221 |
(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
|
222 |
(c, (class, (ty', t'))) :: operations)) |
36674
d95f39448121
eq_morphism is always optional: avoid trivial morphism for empty list of equations
haftmann
parents:
36672
diff
changeset
|
223 |
|> activate_defs class (the_list some_def) |
25062 | 224 |
end; |
24218 | 225 |
|
29558 | 226 |
fun register_subclass (sub, sup) some_dep_morph some_wit export thy = |
25618 | 227 |
let |
29558 | 228 |
val intros = (snd o rules thy) sup :: map_filter I |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33969
diff
changeset
|
229 |
[Option.map (Drule.export_without_context_open o Element.conclude_witness) some_wit, |
29558 | 230 |
(fst o rules thy) sub]; |
29610 | 231 |
val tac = EVERY (map (TRYALL o Tactic.rtac) intros); |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32966
diff
changeset
|
232 |
val classrel = Skip_Proof.prove_global thy [] [] (Logic.mk_classrel (sub, sup)) |
29558 | 233 |
(K tac); |
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
234 |
val diff_sort = Sign.complete_sort thy [sup] |
31012 | 235 |
|> subtract (op =) (Sign.complete_sort thy [sub]) |
236 |
|> filter (is_class thy); |
|
46919 | 237 |
val add_dependency = |
238 |
(case some_dep_morph of |
|
239 |
SOME dep_morph => Locale.add_dependency sub |
|
41270 | 240 |
(sup, dep_morph $> Element.satisfy_morphism (the_list some_wit)) NONE export |
46919 | 241 |
| NONE => I); |
25618 | 242 |
in |
243 |
thy |
|
37246 | 244 |
|> AxClass.add_classrel classrel |
46919 | 245 |
|> Class_Data.map (Graph.add_edge (sub, sup)) |
29526 | 246 |
|> activate_defs sub (these_defs thy diff_sort) |
32074 | 247 |
|> add_dependency |
24218 | 248 |
end; |
249 |
||
250 |
||
24589 | 251 |
(** classes and class target **) |
24218 | 252 |
|
25002 | 253 |
(* class context syntax *) |
24748 | 254 |
|
35858
0d394a82337e
handle hidden polymorphism in class target (without class target syntax, though)
haftmann
parents:
35845
diff
changeset
|
255 |
fun these_unchecks thy = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) |
0d394a82337e
handle hidden polymorphism in class target (without class target syntax, though)
haftmann
parents:
35845
diff
changeset
|
256 |
o these_operations thy; |
29577 | 257 |
|
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
258 |
fun redeclare_const thy c = |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30344
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
|
29577 | 262 |
fun synchronize_class_syntax sort base_sort ctxt = |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
263 |
let |
42360 | 264 |
val thy = Proof_Context.theory_of ctxt; |
26596 | 265 |
val algebra = Sign.classes_of thy; |
29577 | 266 |
val operations = these_operations thy sort; |
26518 | 267 |
fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort))); |
268 |
val primary_constraints = |
|
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
269 |
(map o apsnd) (subst_class_typ base_sort o fst o snd) operations; |
26518 | 270 |
val secondary_constraints = |
25368
f12613fda79d
proper implementation of check phase; non-qualified names for class operations
haftmann
parents:
25344
diff
changeset
|
271 |
(map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations; |
46919 | 272 |
fun improve (c, ty) = |
273 |
(case AList.lookup (op =) primary_constraints c of |
|
274 |
SOME ty' => |
|
275 |
(case try (Type.raw_match (ty', ty)) Vartab.empty of |
|
276 |
SOME tyenv => |
|
277 |
(case Vartab.lookup tyenv (Name.aT, 0) of |
|
278 |
SOME (_, ty' as TVar (vi, sort)) => |
|
279 |
if Type_Infer.is_param vi andalso Sorts.sort_le algebra (base_sort, sort) |
|
280 |
then SOME (ty', TFree (Name.aT, base_sort)) |
|
281 |
else NONE |
|
26238 | 282 |
| _ => NONE) |
283 |
| NONE => NONE) |
|
46919 | 284 |
| NONE => NONE); |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46923
diff
changeset
|
285 |
fun subst (c, _) = Option.map snd (AList.lookup (op =) operations c); |
29577 | 286 |
val unchecks = these_unchecks thy sort; |
25083 | 287 |
in |
288 |
ctxt |
|
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
289 |
|> fold (redeclare_const thy o fst) primary_constraints |
26518 | 290 |
|> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints), |
26730 | 291 |
(((improve, subst), true), unchecks)), false)) |
26518 | 292 |
|> Overloading.set_primary_constraints |
25083 | 293 |
end; |
294 |
||
29632
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
295 |
fun redeclare_operations thy sort = |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
296 |
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
|
297 |
|
29577 | 298 |
fun begin sort base_sort ctxt = |
25083 | 299 |
ctxt |
300 |
|> Variable.declare_term |
|
301 |
(Logic.mk_type (TFree (Name.aT, base_sort))) |
|
29577 | 302 |
|> synchronize_class_syntax sort base_sort |
39378
df86b1b4ce10
more precise name for activation of improveable syntax
haftmann
parents:
39134
diff
changeset
|
303 |
|> Overloading.activate_improvable_syntax; |
24901
d3cbf79769b9
added first version of user-space type system for class target
haftmann
parents:
24847
diff
changeset
|
304 |
|
25311 | 305 |
fun init class thy = |
306 |
thy |
|
29509
1ff0f3f08a7b
migrated class package to new locale implementation
haftmann
parents:
29439
diff
changeset
|
307 |
|> Locale.init class |
29358 | 308 |
|> begin [class] (base_sort thy class); |
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset
|
309 |
|
24748 | 310 |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
311 |
(* class target *) |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
312 |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30344
diff
changeset
|
313 |
val class_prefix = Logic.const_of_class o Long_Name.base_name; |
29526 | 314 |
|
38619
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
315 |
fun target_extension f class lthy = |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
316 |
lthy |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
317 |
|> Local_Theory.raw_theory f |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
318 |
|> Local_Theory.target (synchronize_class_syntax [class] |
42360 | 319 |
(base_sort (Proof_Context.theory_of lthy) class)); |
38619
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
320 |
|
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
321 |
local |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
322 |
|
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
323 |
fun target_const class ((c, mx), (type_params, dict)) thy = |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
324 |
let |
29577 | 325 |
val morph = morphism thy class; |
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
326 |
val b = Morphism.binding morph c; |
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
327 |
val b_def = Morphism.binding morph (Binding.suffix_name "_dict" b); |
29577 | 328 |
val c' = Sign.full_name thy b; |
29439 | 329 |
val dict' = Morphism.term morph dict; |
35858
0d394a82337e
handle hidden polymorphism in class target (without class target syntax, though)
haftmann
parents:
35845
diff
changeset
|
330 |
val ty' = map Term.fastype_of type_params ---> Term.fastype_of dict'; |
0d394a82337e
handle hidden polymorphism in class target (without class target syntax, though)
haftmann
parents:
35845
diff
changeset
|
331 |
val def_eq = Logic.mk_equals (list_comb (Const (c', ty'), type_params), dict') |
29577 | 332 |
|> map_types Type.strip_sorts; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
333 |
in |
29577 | 334 |
thy |
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
335 |
|> Sign.declare_const_global ((b, Type.strip_sorts ty'), mx) |
29577 | 336 |
|> snd |
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
337 |
|> Thm.add_def_global false false (b_def, def_eq) |
36106
19deea200358
Thm.add_axiom/add_def: return internal name of foundational axiom;
wenzelm
parents:
35858
diff
changeset
|
338 |
|>> apsnd Thm.varifyT_global |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
39438
diff
changeset
|
339 |
|-> (fn (_, def_thm) => Global_Theory.store_thm (b_def, def_thm) |
29577 | 340 |
#> snd |
35858
0d394a82337e
handle hidden polymorphism in class target (without class target syntax, though)
haftmann
parents:
35845
diff
changeset
|
341 |
#> null type_params ? 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
|
342 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
343 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
344 |
|
38619
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
345 |
fun target_abbrev class prmode ((c, mx), rhs) thy = |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
346 |
let |
29577 | 347 |
val morph = morphism thy class; |
348 |
val unchecks = these_unchecks thy [class]; |
|
30344
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
wenzelm
parents:
30280
diff
changeset
|
349 |
val b = Morphism.binding morph c; |
29577 | 350 |
val c' = Sign.full_name thy b; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
351 |
val rhs' = Pattern.rewrite_term thy unchecks [] rhs; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
352 |
val ty' = Term.fastype_of rhs'; |
45344
e209da839ff4
added Logic.varify_types_global/unvarify_types_global, which avoids somewhat expensive Term.map_types;
wenzelm
parents:
45310
diff
changeset
|
353 |
val rhs'' = Logic.varify_types_global rhs'; |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
354 |
in |
29577 | 355 |
thy |
33173
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
wenzelm
parents:
32970
diff
changeset
|
356 |
|> Sign.add_abbrev (#1 prmode) (b, rhs'') |
29577 | 357 |
|> snd |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
358 |
|> Sign.add_const_constraint (c', SOME ty') |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
359 |
|> Sign.notation true prmode [(Const (c', ty'), mx)] |
37146
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents:
37145
diff
changeset
|
360 |
|> not (#1 prmode = Print_Mode.input) ? register_operation class (c', (rhs', NONE)) |
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
361 |
end; |
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
362 |
|
38619
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
363 |
in |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
364 |
|
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
365 |
fun const class arg = target_extension (target_const class arg) class; |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
366 |
fun abbrev class prmode arg = target_extension (target_abbrev class prmode arg) class; |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
367 |
|
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
368 |
end; |
25e401d53900
tuned: less formal noise in Named_Target, more coherence in Class
haftmann
parents:
38392
diff
changeset
|
369 |
|
27690
24738db98d34
some steps towards explicit class target for canonical interpretation
haftmann
parents:
27684
diff
changeset
|
370 |
|
31635 | 371 |
(* simple subclasses *) |
372 |
||
373 |
local |
|
374 |
||
375 |
fun gen_classrel mk_prop classrel thy = |
|
376 |
let |
|
377 |
fun after_qed results = |
|
42360 | 378 |
Proof_Context.background_theory ((fold o fold) AxClass.add_classrel results); |
31635 | 379 |
in |
380 |
thy |
|
42360 | 381 |
|> Proof_Context.init_global |
36323
655e2d74de3a
modernized naming conventions of main Isar proof elements;
wenzelm
parents:
36106
diff
changeset
|
382 |
|> Proof.theorem NONE after_qed [[(mk_prop thy classrel, [])]] |
31635 | 383 |
end; |
384 |
||
385 |
in |
|
386 |
||
387 |
val classrel = |
|
388 |
gen_classrel (Logic.mk_classrel oo AxClass.cert_classrel); |
|
389 |
val classrel_cmd = |
|
390 |
gen_classrel (Logic.mk_classrel oo AxClass.read_classrel); |
|
391 |
||
392 |
end; (*local*) |
|
393 |
||
394 |
||
25462 | 395 |
(** instantiation target **) |
396 |
||
397 |
(* bookkeeping *) |
|
398 |
||
399 |
datatype instantiation = Instantiation of { |
|
25864 | 400 |
arities: string list * (string * sort) list * sort, |
25462 | 401 |
params: ((string * string) * (string * typ)) list |
25603 | 402 |
(*(instantiation parameter, type constructor), (local instantiation parameter, typ)*) |
25462 | 403 |
} |
404 |
||
33519 | 405 |
structure Instantiation = Proof_Data |
25462 | 406 |
( |
46919 | 407 |
type T = instantiation; |
408 |
fun init _ = Instantiation {arities = ([], [], []), params = []}; |
|
25462 | 409 |
); |
410 |
||
25485 | 411 |
fun mk_instantiation (arities, params) = |
46919 | 412 |
Instantiation {arities = arities, params = params}; |
413 |
||
414 |
val get_instantiation = |
|
415 |
(fn Instantiation data => data) o Instantiation.get o Local_Theory.target_of; |
|
25462 | 416 |
|
46919 | 417 |
fun map_instantiation f = |
418 |
(Local_Theory.target o Instantiation.map) |
|
419 |
(fn Instantiation {arities, params} => mk_instantiation (f (arities, params))); |
|
420 |
||
421 |
fun the_instantiation lthy = |
|
422 |
(case get_instantiation lthy of |
|
423 |
{arities = ([], [], []), ...} => error "No instantiation target" |
|
424 |
| data => data); |
|
25462 | 425 |
|
25485 | 426 |
val instantiation_params = #params o get_instantiation; |
25462 | 427 |
|
30519
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
428 |
fun instantiation_param lthy b = instantiation_params lthy |
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
30364
diff
changeset
|
429 |
|> find_first (fn (_, (v, _)) => Binding.name_of b = v) |
25462 | 430 |
|> Option.map (fst o fst); |
431 |
||
31869 | 432 |
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) = |
433 |
let |
|
42360 | 434 |
val ctxt = Proof_Context.init_global thy; |
435 |
val all_arities = map (fn raw_tyco => Proof_Context.read_arity ctxt |
|
31869 | 436 |
(raw_tyco, raw_sorts, raw_sort)) raw_tycos; |
437 |
val tycos = map #1 all_arities; |
|
438 |
val (_, sorts, sort) = hd all_arities; |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
42402
diff
changeset
|
439 |
val vs = Name.invent_names Name.context Name.aT sorts; |
31869 | 440 |
in (tycos, vs, sort) end; |
441 |
||
25462 | 442 |
|
443 |
(* syntax *) |
|
444 |
||
26238 | 445 |
fun synchronize_inst_syntax ctxt = |
25462 | 446 |
let |
46919 | 447 |
val Instantiation {params, ...} = Instantiation.get ctxt; |
31249 | 448 |
|
33969 | 449 |
val lookup_inst_param = AxClass.lookup_inst_param |
42360 | 450 |
(Sign.consts_of (Proof_Context.theory_of ctxt)) params; |
46919 | 451 |
fun subst (c, ty) = |
452 |
(case lookup_inst_param (c, ty) of |
|
453 |
SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty) |
|
454 |
| NONE => NONE); |
|
26238 | 455 |
val unchecks = |
456 |
map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params; |
|
457 |
in |
|
458 |
ctxt |
|
26259 | 459 |
|> Overloading.map_improvable_syntax |
46919 | 460 |
(fn (((primary_constraints, _), (((improve, _), _), _)), _) => |
461 |
(((primary_constraints, []), (((improve, subst), false), unchecks)), false)) |
|
26238 | 462 |
end; |
25462 | 463 |
|
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
464 |
fun resort_terms ctxt algebra consts constraints ts = |
38382 | 465 |
let |
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
466 |
fun matchings (Const (c_ty as (c, _))) = |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
467 |
(case constraints c of |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
468 |
NONE => I |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
469 |
| SOME sorts => |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
470 |
fold2 (curry (Sorts.meet_sort algebra)) (Consts.typargs consts c_ty) sorts) |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
471 |
| matchings _ = I; |
38382 | 472 |
val tvartab = (fold o fold_aterms) matchings ts Vartab.empty |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46923
diff
changeset
|
473 |
handle Sorts.CLASS_ERROR e => error (Sorts.class_error (Context.pretty ctxt) e); |
38382 | 474 |
val inst = map_type_tvar |
475 |
(fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi))); |
|
42402
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents:
42388
diff
changeset
|
476 |
in if Vartab.is_empty tvartab then ts else (map o map_types) inst ts end; |
38382 | 477 |
|
25462 | 478 |
|
479 |
(* target *) |
|
480 |
||
38757
2b3e054ae6fc
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents:
38756
diff
changeset
|
481 |
fun define_overloaded (c, U) v (b_def, rhs) = Local_Theory.background_theory_result |
38382 | 482 |
(AxClass.declare_overloaded (c, U) ##>> AxClass.define_overloaded b_def (c, rhs)) |
483 |
##> (map_instantiation o apsnd) (filter_out (fn (_, (v', _)) => v' = v)) |
|
484 |
##> Local_Theory.target synchronize_inst_syntax; |
|
485 |
||
486 |
fun foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy = |
|
46919 | 487 |
(case instantiation_param lthy b of |
488 |
SOME c => |
|
489 |
if mx <> NoSyn then error ("Illegal mixfix syntax for overloaded constant " ^ quote c) |
|
490 |
else lthy |> define_overloaded (c, U) (Binding.name_of b) (b_def, rhs) |
|
491 |
| NONE => lthy |> |
|
492 |
Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)); |
|
38382 | 493 |
|
494 |
fun pretty lthy = |
|
26518 | 495 |
let |
46919 | 496 |
val {arities = (tycos, vs, sort), params} = the_instantiation lthy; |
38382 | 497 |
fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort); |
498 |
fun pr_param ((c, _), (v, ty)) = |
|
42359 | 499 |
Pretty.block (Pretty.breaks |
42360 | 500 |
[Pretty.str v, Pretty.str "==", Pretty.str (Proof_Context.extern_const lthy c), |
42359 | 501 |
Pretty.str "::", Syntax.pretty_typ lthy ty]); |
46923 | 502 |
in Pretty.command "instantiation" :: map pr_arity tycos @ map pr_param params end; |
26518 | 503 |
|
38382 | 504 |
fun conclude lthy = |
505 |
let |
|
42359 | 506 |
val (tycos, vs, sort) = #arities (the_instantiation lthy); |
42360 | 507 |
val thy = Proof_Context.theory_of lthy; |
42359 | 508 |
val _ = tycos |> List.app (fn tyco => |
46919 | 509 |
if Sign.of_sort thy (Type (tyco, map TFree vs), sort) then () |
42360 | 510 |
else error ("Missing instance proof for type " ^ quote (Proof_Context.extern_type lthy tyco))); |
38382 | 511 |
in lthy end; |
512 |
||
513 |
fun instantiation (tycos, vs, sort) thy = |
|
25462 | 514 |
let |
47061
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47005
diff
changeset
|
515 |
val naming = Sign.naming_of thy; |
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47005
diff
changeset
|
516 |
|
25536 | 517 |
val _ = if null tycos then error "At least one arity must be given" else (); |
31249 | 518 |
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
|
519 |
fun get_param tyco (param, (_, (c, ty))) = |
c3d576157244
fixed reading of class specs: declare class operations in context
haftmann
parents:
29610
diff
changeset
|
520 |
if can (AxClass.param_of_inst thy) (c, tyco) |
25603 | 521 |
then NONE else SOME ((c, tyco), |
46917 | 522 |
(param ^ "_" ^ Long_Name.base_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty)); |
31249 | 523 |
val params = map_product get_param tycos class_params |> map_filter I; |
26518 | 524 |
val primary_constraints = map (apsnd |
31249 | 525 |
(map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params; |
26518 | 526 |
val algebra = Sign.classes_of thy |
47005
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents:
46923
diff
changeset
|
527 |
|> fold (fn tyco => Sorts.add_arities (Context.pretty_global thy) |
26518 | 528 |
(tyco, map (fn class => (class, map snd vs)) sort)) tycos; |
529 |
val consts = Sign.consts_of thy; |
|
530 |
val improve_constraints = AList.lookup (op =) |
|
31249 | 531 |
(map (fn (_, (class, (c, _))) => (c, [[class]])) class_params); |
42402
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents:
42388
diff
changeset
|
532 |
fun resort_check ctxt ts = resort_terms ctxt algebra consts improve_constraints ts; |
31249 | 533 |
val lookup_inst_param = AxClass.lookup_inst_param consts params; |
42388 | 534 |
fun improve (c, ty) = |
535 |
(case lookup_inst_param (c, ty) of |
|
536 |
SOME (_, ty') => if Sign.typ_instance thy (ty', ty) then SOME (ty, ty') else NONE |
|
537 |
| NONE => NONE); |
|
25485 | 538 |
in |
539 |
thy |
|
32379
a97e9caebd60
additional checkpoints avoid problems in error situations
haftmann
parents:
32074
diff
changeset
|
540 |
|> Theory.checkpoint |
42360 | 541 |
|> Proof_Context.init_global |
31249 | 542 |
|> Instantiation.put (mk_instantiation ((tycos, vs, sort), params)) |
27281 | 543 |
|> fold (Variable.declare_typ o TFree) vs |
31249 | 544 |
|> fold (Variable.declare_names o Free o snd) params |
26259 | 545 |
|> (Overloading.map_improvable_syntax o apfst) |
29969
9dbb046136d0
more precise improvement in instantiation user space type system
haftmann
parents:
29632
diff
changeset
|
546 |
(K ((primary_constraints, []), (((improve, K NONE), false), []))) |
39378
df86b1b4ce10
more precise name for activation of improveable syntax
haftmann
parents:
39134
diff
changeset
|
547 |
|> Overloading.activate_improvable_syntax |
45429 | 548 |
|> Context.proof_map (Syntax_Phases.term_check 0 "resorting" resort_check) |
26238 | 549 |
|> synchronize_inst_syntax |
47061
355317493f34
clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents:
47005
diff
changeset
|
550 |
|> Local_Theory.init naming |
38382 | 551 |
{define = Generic_Target.define foundation, |
552 |
notes = Generic_Target.notes |
|
553 |
(fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts), |
|
554 |
abbrev = Generic_Target.abbrev |
|
42385
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
555 |
(fn prmode => fn (b, mx) => fn (t, _) => fn _ => |
b46b47775cbe
simplified Sorts.class_error: plain Proof.context;
wenzelm
parents:
42383
diff
changeset
|
556 |
Generic_Target.theory_abbrev prmode ((b, mx), t)), |
45310 | 557 |
declaration = K Generic_Target.theory_declaration, |
38382 | 558 |
pretty = pretty, |
559 |
exit = Local_Theory.target_of o conclude} |
|
25485 | 560 |
end; |
561 |
||
38382 | 562 |
fun instantiation_cmd arities thy = |
563 |
instantiation (read_multi_arity thy arities) thy; |
|
26238 | 564 |
|
25485 | 565 |
fun gen_instantiation_instance do_proof after_qed lthy = |
566 |
let |
|
25864 | 567 |
val (tycos, vs, sort) = (#arities o the_instantiation) lthy; |
568 |
val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos; |
|
25462 | 569 |
fun after_qed' results = |
38757
2b3e054ae6fc
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents:
38756
diff
changeset
|
570 |
Local_Theory.background_theory (fold (AxClass.add_arity o Thm.varifyT_global) results) |
25462 | 571 |
#> after_qed; |
572 |
in |
|
573 |
lthy |
|
574 |
|> do_proof after_qed' arities_proof |
|
575 |
end; |
|
576 |
||
25485 | 577 |
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts => |
36323
655e2d74de3a
modernized naming conventions of main Isar proof elements;
wenzelm
parents:
36106
diff
changeset
|
578 |
Proof.theorem NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts)); |
25462 | 579 |
|
25485 | 580 |
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed => |
25502 | 581 |
fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t |
582 |
(fn {context, ...} => tac context)) ts) lthy) I; |
|
25462 | 583 |
|
28666 | 584 |
fun prove_instantiation_exit tac = prove_instantiation_instance tac |
33671 | 585 |
#> Local_Theory.exit_global; |
28666 | 586 |
|
587 |
fun prove_instantiation_exit_result f tac x lthy = |
|
588 |
let |
|
42360 | 589 |
val morph = Proof_Context.export_morphism lthy |
590 |
(Proof_Context.init_global (Proof_Context.theory_of lthy)); |
|
29439 | 591 |
val y = f morph x; |
28666 | 592 |
in |
593 |
lthy |
|
594 |
|> prove_instantiation_exit (fn ctxt => tac ctxt y) |
|
595 |
|> pair y |
|
596 |
end; |
|
597 |
||
29526 | 598 |
|
31635 | 599 |
(* simplified instantiation interface with no class parameter *) |
600 |
||
31869 | 601 |
fun instance_arity_cmd raw_arities thy = |
31635 | 602 |
let |
31869 | 603 |
val (tycos, vs, sort) = read_multi_arity thy raw_arities; |
604 |
val sorts = map snd vs; |
|
605 |
val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos; |
|
42360 | 606 |
fun after_qed results = Proof_Context.background_theory |
37246 | 607 |
((fold o fold) AxClass.add_arity results); |
31635 | 608 |
in |
609 |
thy |
|
42360 | 610 |
|> Proof_Context.init_global |
36323
655e2d74de3a
modernized naming conventions of main Isar proof elements;
wenzelm
parents:
36106
diff
changeset
|
611 |
|> Proof.theorem NONE after_qed (map (fn t => [(t, [])]) arities) |
31635 | 612 |
end; |
613 |
||
614 |
||
29526 | 615 |
(** tactics and methods **) |
616 |
||
617 |
fun intro_classes_tac facts st = |
|
618 |
let |
|
619 |
val thy = Thm.theory_of_thm st; |
|
620 |
val classes = Sign.all_classes thy; |
|
621 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
622 |
val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes; |
|
623 |
val assm_intros = all_assm_intros thy; |
|
624 |
in |
|
625 |
Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st |
|
626 |
end; |
|
627 |
||
628 |
fun default_intro_tac ctxt [] = |
|
39438
c5ece2a7a86e
Isar "default" step needs to fail for solved problems, for clear distinction of '.' and '..' for example -- amending lapse introduced in 9de4d64eee3b (April 2004);
wenzelm
parents:
39378
diff
changeset
|
629 |
COND Thm.no_prems no_tac |
c5ece2a7a86e
Isar "default" step needs to fail for solved problems, for clear distinction of '.' and '..' for example -- amending lapse introduced in 9de4d64eee3b (April 2004);
wenzelm
parents:
39378
diff
changeset
|
630 |
(intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt []) |
29526 | 631 |
| default_intro_tac _ _ = no_tac; |
632 |
||
633 |
fun default_tac rules ctxt facts = |
|
634 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
635 |
default_intro_tac ctxt facts; |
|
636 |
||
637 |
val _ = Context.>> (Context.map_theory |
|
30515 | 638 |
(Method.setup (Binding.name "intro_classes") (Scan.succeed (K (METHOD intro_classes_tac))) |
639 |
"back-chain introduction rules of classes" #> |
|
640 |
Method.setup (Binding.name "default") (Attrib.thms >> (METHOD oo default_tac)) |
|
641 |
"apply some intro/elim rule")); |
|
29526 | 642 |
|
24218 | 643 |
end; |
25683 | 644 |