author | wenzelm |
Tue, 28 Nov 2006 00:35:18 +0100 | |
changeset 21565 | bd28361f4c5b |
parent 21553 | 3dd27eda91b9 |
child 21623 | 17098171d46a |
permissions | -rw-r--r-- |
18168 | 1 |
(* Title: Pure/Tools/class_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
19468 | 5 |
Type classes derived from primitive axclasses and locales. |
18168 | 6 |
*) |
7 |
||
8 |
signature CLASS_PACKAGE = |
|
9 |
sig |
|
20964 | 10 |
val class: bstring -> class list -> Element.context Locale.element list -> theory -> |
11 |
string * Proof.context |
|
12 |
val class_i: bstring -> class list -> Element.context_i Locale.element list -> theory -> |
|
13 |
string * Proof.context |
|
20601 | 14 |
val instance_arity: ((bstring * string list) * string) list |
19136 | 15 |
-> bstring * Attrib.src list -> ((bstring * Attrib.src list) * string) list |
18575 | 16 |
-> theory -> Proof.state |
20601 | 17 |
val instance_arity_i: ((bstring * sort list) * sort) list |
19136 | 18 |
-> bstring * attribute list -> ((bstring * attribute list) * term) list |
18575 | 19 |
-> theory -> Proof.state |
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
20 |
val prove_instance_arity: (thm list -> tactic) -> ((string * sort list) * sort) list |
19136 | 21 |
-> bstring * attribute list -> ((bstring * attribute list) * term) list |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
22 |
-> theory -> theory |
19150 | 23 |
val instance_sort: string * string -> theory -> Proof.state |
24 |
val instance_sort_i: class * sort -> theory -> Proof.state |
|
25 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
|
18168 | 26 |
|
18755 | 27 |
val certify_class: theory -> class -> class |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
28 |
val certify_sort: theory -> sort -> sort |
20385 | 29 |
val read_class: theory -> xstring -> class |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
30 |
val read_sort: theory -> string -> sort |
20175 | 31 |
val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool |
32 |
val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a |
|
33 |
(*'a must not keep any reference to theory*) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
34 |
|
21553 | 35 |
val add_def: class * thm -> theory -> theory |
36 |
val export_typ: theory -> class -> typ -> typ |
|
37 |
val export_def: theory -> class -> term -> term |
|
38 |
val export_thm: theory -> class -> thm -> thm |
|
39 |
||
18702 | 40 |
val print_classes: theory -> unit |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
41 |
val intro_classes_tac: thm list -> tactic |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
42 |
val default_intro_classes_tac: thm list -> tactic |
18168 | 43 |
end; |
44 |
||
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
45 |
structure ClassPackage : CLASS_PACKAGE = |
18168 | 46 |
struct |
47 |
||
48 |
||
20455 | 49 |
(** theory data **) |
18168 | 50 |
|
19456 | 51 |
datatype class_data = ClassData of { |
18575 | 52 |
name_locale: string, |
53 |
name_axclass: string, |
|
54 |
var: string, |
|
20843 | 55 |
consts: (string * (string * typ)) list |
56 |
(*locale parameter ~> toplevel theory constant*), |
|
21553 | 57 |
propnames: string list, |
58 |
defs: thm list |
|
19966 | 59 |
} * thm list Symtab.table; |
18168 | 60 |
|
19456 | 61 |
fun rep_classdata (ClassData c) = c; |
62 |
||
18575 | 63 |
structure ClassData = TheoryDataFun ( |
18168 | 64 |
struct |
65 |
val name = "Pure/classes"; |
|
19966 | 66 |
type T = class_data Symtab.table; |
67 |
val empty = Symtab.empty; |
|
18168 | 68 |
val copy = I; |
69 |
val extend = I; |
|
19966 | 70 |
fun merge _ = Symtab.join (fn _ => fn (ClassData (classd, instd1), ClassData (_, instd2)) => |
71 |
(ClassData (classd, Symtab.merge (K true) (instd1, instd2)))); |
|
72 |
fun print thy data = |
|
18575 | 73 |
let |
19966 | 74 |
fun pretty_class (name, ClassData ({name_locale, name_axclass, var, consts, ...}, _)) = |
18575 | 75 |
(Pretty.block o Pretty.fbreaks) [ |
76 |
Pretty.str ("class " ^ name ^ ":"), |
|
77 |
Pretty.str ("locale: " ^ name_locale), |
|
78 |
Pretty.str ("axclass: " ^ name_axclass), |
|
79 |
Pretty.str ("class variable: " ^ var), |
|
80 |
(Pretty.block o Pretty.fbreaks) ( |
|
81 |
Pretty.str "constants: " |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
82 |
:: map (fn (_, (c, ty)) => Pretty.str (c ^ " :: " ^ Sign.string_of_typ thy ty)) consts |
18575 | 83 |
) |
84 |
] |
|
85 |
in |
|
19966 | 86 |
(Pretty.writeln o Pretty.chunks o map pretty_class o Symtab.dest) data |
18575 | 87 |
end; |
18168 | 88 |
end |
89 |
); |
|
90 |
||
18708 | 91 |
val _ = Context.add_setup ClassData.init; |
18575 | 92 |
val print_classes = ClassData.print; |
93 |
||
19038 | 94 |
|
95 |
(* queries *) |
|
96 |
||
19966 | 97 |
val lookup_class_data = Option.map rep_classdata oo Symtab.lookup o ClassData.get; |
18168 | 98 |
|
19038 | 99 |
fun the_class_data thy class = |
18168 | 100 |
case lookup_class_data thy class |
20455 | 101 |
of NONE => error ("undeclared constructive class " ^ quote class) |
18168 | 102 |
| SOME data => data; |
103 |
||
20455 | 104 |
fun the_ancestry thy classes = |
18360 | 105 |
let |
20455 | 106 |
fun proj_class class = |
20843 | 107 |
if is_some (lookup_class_data thy class) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
108 |
then [class] |
20455 | 109 |
else (Sign.certify_sort thy o maps proj_class o Sign.super_classes thy) class; |
19038 | 110 |
fun ancestry class anc = |
111 |
anc |
|
20385 | 112 |
|> insert (op =) class |
20455 | 113 |
|> fold ancestry ((maps proj_class o Sign.super_classes thy) class); |
19038 | 114 |
in fold ancestry classes [] end; |
115 |
||
20455 | 116 |
val the_parm_map = #consts o fst oo the_class_data; |
117 |
||
118 |
val the_propnames = #propnames o fst oo the_class_data; |
|
119 |
||
19038 | 120 |
fun subst_clsvar v ty_subst = |
121 |
map_type_tfree (fn u as (w, _) => |
|
122 |
if w = v then ty_subst else TFree u); |
|
123 |
||
18168 | 124 |
|
19038 | 125 |
(* updaters *) |
126 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
127 |
fun add_class_data (class, (name_locale, name_axclass, var, consts, propnames)) = |
19957 | 128 |
ClassData.map ( |
19966 | 129 |
Symtab.update_new (class, ClassData ({ |
19957 | 130 |
name_locale = name_locale, |
131 |
name_axclass = name_axclass, |
|
132 |
var = var, |
|
19966 | 133 |
consts = consts, |
21553 | 134 |
propnames = propnames, |
135 |
defs = []}, Symtab.empty)) |
|
19957 | 136 |
); |
19038 | 137 |
|
21553 | 138 |
fun add_def (class, thm) = |
139 |
ClassData.map ( |
|
140 |
Symtab.map_entry class (fn ClassData ({ name_locale, name_axclass, |
|
141 |
var, consts, propnames, defs }, instd) => ClassData ({ name_locale = name_locale, |
|
142 |
name_axclass = name_axclass, var = var, |
|
143 |
consts = consts, propnames = propnames, defs = thm :: defs }, instd)) |
|
144 |
); |
|
145 |
||
146 |
||
19966 | 147 |
fun add_inst_def ((class, tyco), thm) = |
148 |
ClassData.map ( |
|
149 |
Symtab.map_entry class (fn ClassData (classd, instd) => |
|
150 |
ClassData (classd, Symtab.insert_list eq_thm (tyco, thm) instd)) |
|
151 |
); |
|
19038 | 152 |
|
20106 | 153 |
|
21553 | 154 |
(* experimental class target *) |
155 |
||
156 |
fun export_typ thy loc = |
|
157 |
let |
|
158 |
val class = loc (*FIXME*); |
|
159 |
val (v, _) = AxClass.params_of_class thy class; |
|
160 |
in |
|
161 |
Term.map_type_tfree (fn var as (w, sort) => |
|
162 |
if w = v then TFree (w, Sorts.inter_sort (Sign.classes_of thy) |
|
163 |
(sort, [class])) else TFree var) |
|
164 |
end; |
|
165 |
||
166 |
fun export_def thy loc = |
|
167 |
let |
|
168 |
val class = loc (*FIXME*); |
|
169 |
val data = (fst o the_class_data thy) class; |
|
170 |
val consts = #consts data; |
|
171 |
val v = #var data; |
|
172 |
val subst_typ = Term.map_type_tfree (fn var as (w, sort) => |
|
173 |
if w = v then TFree (w, Sorts.inter_sort (Sign.classes_of thy) |
|
174 |
(sort, [class])) else TFree var) |
|
175 |
fun subst (t as Free (v, _)) = (case AList.lookup (op =) consts v |
|
176 |
of SOME c_ty => Const c_ty |
|
177 |
| NONE => t) |
|
178 |
| subst t = t; |
|
179 |
in |
|
180 |
Term.map_aterms subst #> map_types subst_typ |
|
181 |
end; |
|
182 |
||
183 |
fun export_thm thy loc = |
|
184 |
let |
|
185 |
val class = loc (*FIXME*); |
|
186 |
val thms = (#defs o fst o the_class_data thy) class; |
|
187 |
in |
|
188 |
Tactic.rewrite_rule thms |
|
189 |
end; |
|
190 |
||
191 |
||
20385 | 192 |
(* certification and reading *) |
19038 | 193 |
|
194 |
fun certify_class thy class = |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
195 |
(fn class => (the_class_data thy class; class)) (Sign.certify_class thy class); |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
196 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
197 |
fun certify_sort thy sort = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
198 |
map (fn class => (the_class_data thy class; class)) (Sign.certify_sort thy sort); |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
199 |
|
20385 | 200 |
fun read_class thy = |
19957 | 201 |
certify_class thy o Sign.intern_class thy; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
202 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
203 |
fun read_sort thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
204 |
certify_sort thy o Sign.read_sort thy; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
205 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
206 |
|
20455 | 207 |
|
208 |
(** contexts with arity assumptions **) |
|
20175 | 209 |
|
210 |
fun assume_arities_of_sort thy arities ty_sort = |
|
211 |
let |
|
212 |
val pp = Sign.pp thy; |
|
213 |
val algebra = Sign.classes_of thy |
|
214 |
|> fold (fn ((tyco, asorts), sort) => |
|
215 |
Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities; |
|
216 |
in Sorts.of_sort algebra ty_sort end; |
|
217 |
||
218 |
fun assume_arities_thy thy arities f = |
|
219 |
let |
|
220 |
val thy_read = (fold (fn ((tyco, asorts), sort) |
|
221 |
=> Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy |
|
222 |
in f thy_read end; |
|
223 |
||
20455 | 224 |
|
225 |
||
226 |
(** tactics and methods **) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
227 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
228 |
fun intro_classes_tac facts st = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
229 |
(ALLGOALS (Method.insert_tac facts THEN' |
19928 | 230 |
REPEAT_ALL_NEW (resolve_tac (AxClass.class_intros (Thm.theory_of_thm st)))) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
231 |
THEN Tactic.distinct_subgoals_tac) st; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
232 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
233 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
234 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
235 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
236 |
fun default_tac rules ctxt facts = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
237 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
238 |
default_intro_classes_tac facts; |
19038 | 239 |
|
19468 | 240 |
val _ = Context.add_setup (Method.add_methods |
241 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
|
242 |
"back-chain introduction rules of classes"), |
|
243 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
244 |
"apply some intro/elim rule")]); |
|
245 |
||
19038 | 246 |
|
20455 | 247 |
|
248 |
(** axclass instances **) |
|
19246 | 249 |
|
250 |
local |
|
251 |
||
20175 | 252 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
253 |
let |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
254 |
fun after_qed' results = |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
255 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
256 |
in |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
257 |
thy |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
258 |
|> ProofContext.init |
21444 | 259 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts) |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
260 |
end; |
19246 | 261 |
|
262 |
in |
|
263 |
||
264 |
val axclass_instance_arity = |
|
19412 | 265 |
gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity; |
19246 | 266 |
val axclass_instance_arity_i = |
19412 | 267 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
268 |
val axclass_instance_sort = |
21444 | 269 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) |
270 |
AxClass.add_classrel I o single; |
|
19246 | 271 |
|
272 |
end; |
|
273 |
||
274 |
||
20455 | 275 |
|
276 |
(** classes and instances **) |
|
19038 | 277 |
|
278 |
local |
|
279 |
||
19957 | 280 |
fun add_axclass_i (name, supsort) params axs thy = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
281 |
let |
19395 | 282 |
val (c, thy') = thy |
19957 | 283 |
|> AxClass.define_class_i (name, supsort) params axs; |
19518 | 284 |
val {intro, axioms, ...} = AxClass.get_definition thy' c; |
19395 | 285 |
in ((c, (intro, axioms)), thy') end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
286 |
|
20385 | 287 |
(*FIXME proper locale interface*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
288 |
fun prove_interpretation_i (prfx, atts) expr insts tac thy = |
19038 | 289 |
let |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
290 |
fun ad_hoc_term (Const (c, ty)) = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
291 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
292 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_typ thy))) ty; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
293 |
val s = c ^ "::" ^ Pretty.output p; |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
294 |
in s end |
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
295 |
| ad_hoc_term t = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
296 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
297 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_term thy))) t; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
298 |
val s = Pretty.output p; |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
299 |
in s end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
300 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
301 |
thy |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
302 |
|> Locale.interpretation I (prfx, atts) expr (map (Option.map ad_hoc_term) insts) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
303 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
304 |
|> ProofContext.theory_of |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
305 |
end; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
306 |
|
19150 | 307 |
fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
308 |
let |
20428 | 309 |
val (elems, exprs) = fold_rev (fn Locale.Elem e => apfst (cons e) |
310 |
| Locale.Expr e => apsnd (cons e)) |
|
311 |
raw_elems ([], []); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
312 |
val supclasses = map (prep_class thy) raw_supclasses; |
19038 | 313 |
val supsort = |
314 |
supclasses |
|
19966 | 315 |
|> map (#name_axclass o fst o the_class_data thy) |
19468 | 316 |
|> Sign.certify_sort thy |
21565 | 317 |
|> (fn [] => Sign.defaultS thy | S => S); (* FIXME Why syntax defaultS? *) |
20428 | 318 |
val expr_supclasses = Locale.Merge |
319 |
(map (Locale.Locale o #name_locale o fst o the_class_data thy) supclasses); |
|
320 |
val expr = Locale.Merge (map (Locale.Locale o #name_locale o fst o the_class_data thy) supclasses |
|
321 |
@ exprs); |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
322 |
val mapp_sup = AList.make |
19468 | 323 |
(the o AList.lookup (op =) ((flat o map (the_parm_map thy) o the_ancestry thy) supclasses)) |
20428 | 324 |
((map (fst o fst) o Locale.parameters_of_expr thy) expr_supclasses); |
19038 | 325 |
fun extract_tyvar_consts thy name_locale = |
326 |
let |
|
20175 | 327 |
fun extract_classvar ((c, ty), _) w = |
20428 | 328 |
(case Term.add_tfreesT ty [] |
20175 | 329 |
of [(v, _)] => (case w |
20385 | 330 |
of SOME u => if u = v then w else error ("Additonal type variable in type signature of constant " ^ quote c) |
20175 | 331 |
| NONE => SOME v) |
20385 | 332 |
| [] => error ("No type variable in type signature of constant " ^ quote c) |
333 |
| _ => error ("More than one type variable in type signature of constant " ^ quote c)); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
334 |
val consts1 = |
19038 | 335 |
Locale.parameters_of thy name_locale |
20892 | 336 |
|> map (apsnd (Syntax.unlocalize_mixfix true)) |
20175 | 337 |
val SOME v = fold extract_classvar consts1 NONE; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
338 |
val consts2 = map ((apfst o apsnd) (subst_clsvar v (TFree (v, [])))) consts1; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
339 |
in (v, chop (length mapp_sup) consts2) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
340 |
fun add_consts v raw_cs_sup raw_cs_this thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
341 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
342 |
fun add_global_const ((c, ty), syn) thy = |
20265 | 343 |
((c, (Sign.full_name thy c, ty)), |
344 |
thy |
|
20703 | 345 |
|> Sign.add_consts_authentic [(c, ty |> subst_clsvar v (TFree (v, Sign.defaultS thy)), syn)]); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
346 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
347 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
348 |
|> fold_map add_global_const raw_cs_this |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
349 |
end; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
350 |
fun extract_assumes thy name_locale cs_mapp = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
351 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
352 |
val subst_assume = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
353 |
map_aterms (fn Free (c, ty) => Const ((fst o the o AList.lookup (op =) cs_mapp) c, ty) |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
354 |
| t => t) |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
355 |
fun prep_asm ((name, atts), ts) = |
19957 | 356 |
((NameSpace.base name, map (Attrib.attribute thy) atts), map subst_assume ts) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
357 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
358 |
(map prep_asm o Locale.local_asms_of thy) name_locale |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
359 |
end; |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
360 |
fun add_global_constraint v class (_, (c, ty)) thy = |
19038 | 361 |
thy |
19136 | 362 |
|> Sign.add_const_constraint_i (c, SOME (subst_clsvar v (TFree (v, [class])) ty)); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
363 |
fun mk_const thy class v (c, ty) = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
364 |
Const (c, subst_clsvar v (TFree (v, [class])) ty); |
19038 | 365 |
in |
366 |
thy |
|
20428 | 367 |
|> add_locale bname expr elems |
20964 | 368 |
|-> (fn name_locale => ProofContext.theory |
369 |
(`(fn thy => extract_tyvar_consts thy name_locale) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
370 |
#-> (fn (v, (raw_cs_sup, raw_cs_this)) => |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
371 |
add_consts v raw_cs_sup raw_cs_this |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
372 |
#-> (fn mapp_this => |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
373 |
`(fn thy => extract_assumes thy name_locale (mapp_sup @ mapp_this)) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
374 |
#-> (fn loc_axioms => |
19957 | 375 |
add_axclass_i (bname, supsort) (map (fst o snd) mapp_this) loc_axioms |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
376 |
#-> (fn (name_axclass, (_, ax_axioms)) => |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
377 |
fold (add_global_constraint v name_axclass) mapp_this |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
378 |
#> add_class_data (name_locale, (name_locale, name_axclass, v, mapp_this, |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
379 |
map (fst o fst) loc_axioms)) |
20601 | 380 |
#> prove_interpretation_i (bname, []) |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
381 |
(Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass v) (map snd (mapp_sup @ mapp_this))) |
19929 | 382 |
((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
20964 | 383 |
))))) #> pair name_locale) |
19038 | 384 |
end; |
385 |
||
386 |
in |
|
387 |
||
20385 | 388 |
val class = gen_class (Locale.add_locale false) read_class; |
19928 | 389 |
val class_i = gen_class (Locale.add_locale_i false) certify_class; |
19038 | 390 |
|
20455 | 391 |
end; (*local*) |
19038 | 392 |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
393 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
394 |
|
20175 | 395 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
19038 | 396 |
let |
19957 | 397 |
val (_, t) = read_def thy (raw_name, raw_t); |
398 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
399 |
val atts = map (prep_att thy) raw_atts; |
|
20175 | 400 |
val insts = (Consts.typargs (Sign.consts_of thy) (c, ty)) |
19957 | 401 |
val name = case raw_name |
20385 | 402 |
of "" => NONE |
403 |
| _ => SOME raw_name; |
|
20175 | 404 |
in (c, (insts, ((name, t), atts))) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
405 |
|
19957 | 406 |
fun read_def thy = gen_read_def thy Attrib.attribute read_axm; |
407 |
fun read_def_i thy = gen_read_def thy (K I) (K I); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
408 |
|
20175 | 409 |
fun gen_instance_arity prep_arity prep_att read_def do_proof raw_arities (raw_name, raw_atts) raw_defs theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
410 |
let |
20175 | 411 |
fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort); |
412 |
val arities = map prep_arity' raw_arities; |
|
413 |
val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities; |
|
414 |
val _ = if null arities then error "at least one arity must be given" else (); |
|
415 |
val _ = case (duplicates (op =) o map #1) arities |
|
416 |
of [] => () |
|
417 |
| dupl_tycos => error ("type constructors occur more than once in arities: " |
|
418 |
^ (commas o map quote) dupl_tycos); |
|
20393 | 419 |
val (bind_always, name) = case raw_name |
420 |
of "" => (false, Thm.def_name ((space_implode "_" o map NameSpace.base) |
|
20175 | 421 |
(maps (fn (tyco, _, sort) => sort @ [tyco]) |
20393 | 422 |
(sort (fn ((tyco1, _, _), (tyco2, _, _)) => string_ord (tyco1, tyco2)) arities)))) |
423 |
| _ => (true, raw_name); |
|
19136 | 424 |
val atts = map (prep_att theory) raw_atts; |
20175 | 425 |
fun already_defined (c, ty_inst) = |
426 |
is_some (find_first (fn (_, { lhs = [ty_inst'], ...}) => |
|
427 |
Sign.typ_instance theory (ty_inst', ty_inst) orelse Sign.typ_instance theory (ty_inst, ty_inst')) |
|
428 |
(Defs.specifications_of (Theory.defs_of theory) c)); |
|
429 |
fun get_consts_class tyco ty class = |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
430 |
let |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
431 |
val (_, cs) = AxClass.params_of_class theory class; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
432 |
val subst_ty = map_type_tfree (K ty); |
19957 | 433 |
in |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
434 |
map_filter (fn (c, ty) => |
20175 | 435 |
if already_defined (c, ty) |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
436 |
then NONE else SOME ((c, ((tyco, class), subst_ty ty)))) cs |
19957 | 437 |
end; |
20175 | 438 |
fun get_consts_sort (tyco, asorts, sort) = |
19957 | 439 |
let |
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
440 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
20175 | 441 |
in maps (get_consts_class tyco ty) (the_ancestry theory sort) end; |
442 |
val cs = maps get_consts_sort arities; |
|
20385 | 443 |
fun mk_typnorm thy (ty, ty_sc) = |
444 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
445 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
446 |
| NONE => NONE; |
|
20175 | 447 |
fun read_defs defs cs thy_read = |
448 |
let |
|
19957 | 449 |
fun read raw_def cs = |
450 |
let |
|
20385 | 451 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
452 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
20175 | 453 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
19957 | 454 |
of NONE => error ("superfluous definition for constant " ^ quote c) |
19966 | 455 |
| SOME class_ty => class_ty; |
20385 | 456 |
val name = case name_opt |
20628 | 457 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
20385 | 458 |
| SOME name => name; |
459 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
19966 | 460 |
of NONE => error ("superfluous definition for constant " ^ |
461 |
quote c ^ "::" ^ Sign.string_of_typ thy_read ty) |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20465
diff
changeset
|
462 |
| SOME norm => map_types norm t |
20175 | 463 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
19957 | 464 |
in fold_map read defs cs end; |
20175 | 465 |
val (defs, _) = assume_arities_thy theory arities_pair (read_defs raw_defs cs); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
466 |
fun get_remove_contraint c thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
467 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
468 |
val ty = Sign.the_const_constraint thy c; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
469 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
470 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
471 |
|> Sign.add_const_constraint_i (c, NONE) |
20385 | 472 |
|> pair (c, Logic.unvarifyT ty) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
473 |
end; |
19966 | 474 |
fun add_defs defs thy = |
475 |
thy |
|
476 |
|> PureThy.add_defs_i true (map snd defs) |
|
477 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
|
478 |
fun note_all thy = |
|
20175 | 479 |
(*FIXME: should avoid binding duplicated names*) |
19966 | 480 |
let |
20393 | 481 |
val bind = bind_always orelse not (can (PureThy.get_thms thy) (Name name)); |
20175 | 482 |
val thms = maps (fn (tyco, _, sort) => maps (fn class => |
483 |
Symtab.lookup_list |
|
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
484 |
((the_default Symtab.empty o Option.map snd o try (the_class_data thy)) class) tyco) |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
485 |
(the_ancestry thy sort)) arities; |
20393 | 486 |
in if bind then |
19966 | 487 |
thy |
21444 | 488 |
|> PureThy.note_thmss_i (*qualified*) Thm.internalK [((name, atts), [(thms, [])])] |
19966 | 489 |
|> snd |
20393 | 490 |
else |
491 |
thy |
|
19966 | 492 |
end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
493 |
fun after_qed cs thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
494 |
thy |
19412 | 495 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs); |
19038 | 496 |
in |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
497 |
theory |
20385 | 498 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =)) |
19966 | 499 |
||>> add_defs defs |
20076 | 500 |
|-> (fn (cs, def_thms) => |
20175 | 501 |
fold add_inst_def def_thms |
19966 | 502 |
#> note_all |
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
503 |
#> do_proof (map snd def_thms) (after_qed cs) arities) |
19038 | 504 |
end; |
505 |
||
19957 | 506 |
fun instance_arity' do_proof = gen_instance_arity Sign.read_arity Attrib.attribute |
507 |
read_def do_proof; |
|
508 |
fun instance_arity_i' do_proof = gen_instance_arity Sign.cert_arity (K I) |
|
509 |
read_def_i do_proof; |
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
510 |
fun tactic_proof tac def_thms after_qed arities = |
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
511 |
fold (fn arity => AxClass.prove_arity arity (tac def_thms)) arities |
20175 | 512 |
#> after_qed; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
513 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
514 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
515 |
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
516 |
val instance_arity = instance_arity' (K axclass_instance_arity_i); |
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
517 |
val instance_arity_i = instance_arity_i' (K axclass_instance_arity_i); |
19150 | 518 |
val prove_instance_arity = instance_arity_i' o tactic_proof; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
519 |
|
20455 | 520 |
end; (*local*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
521 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
522 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
523 |
|
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
524 |
fun prove_interpretation_in tac after_qed (name, expr) thy = |
19136 | 525 |
thy |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
526 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr) |
19136 | 527 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
528 |
|> ProofContext.theory_of; |
19038 | 529 |
|
20385 | 530 |
(*FIXME very ad-hoc, needs proper locale interface*) |
19150 | 531 |
fun gen_instance_sort prep_class prep_sort do_proof (raw_class, raw_sort) theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
532 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
533 |
val class = prep_class theory raw_class; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
534 |
val sort = prep_sort theory raw_sort; |
19966 | 535 |
val loc_name = (#name_locale o fst o the_class_data theory) class; |
19468 | 536 |
val loc_expr = |
19966 | 537 |
(Locale.Merge o map (Locale.Locale o #name_locale o fst o the_class_data theory)) sort; |
538 |
val const_names = (map (NameSpace.base o fst o snd) |
|
539 |
o maps (#consts o fst o the_class_data theory) |
|
540 |
o the_ancestry theory) [class]; |
|
20106 | 541 |
fun get_thms thy = |
542 |
the_ancestry thy sort |
|
543 |
|> AList.make (the_propnames thy) |
|
544 |
|> map (apsnd (map (NameSpace.append (loc_name)))) |
|
545 |
|> map_filter (fn (superclass, thm_names) => |
|
546 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
547 |
of [] => NONE |
|
548 |
| thms => SOME (superclass, thms)); |
|
549 |
fun after_qed thy = |
|
550 |
thy |
|
551 |
|> `get_thms |
|
552 |
|-> fold (fn (supclass, thms) => I |
|
553 |
AxClass.prove_classrel (class, supclass) |
|
554 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
555 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
556 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
557 |
theory |
19136 | 558 |
|> do_proof after_qed (loc_name, loc_expr) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
559 |
end; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
560 |
|
20385 | 561 |
fun instance_sort' do_proof = gen_instance_sort read_class read_sort do_proof; |
19150 | 562 |
fun instance_sort_i' do_proof = gen_instance_sort certify_class certify_sort do_proof; |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
563 |
val setup_proof = Locale.interpretation_in_locale o ProofContext.theory; |
19136 | 564 |
val tactic_proof = prove_interpretation_in; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
565 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
566 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
567 |
|
19150 | 568 |
val instance_sort = instance_sort' setup_proof; |
569 |
val instance_sort_i = instance_sort_i' setup_proof; |
|
570 |
val prove_instance_sort = instance_sort_i' o tactic_proof; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
571 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
572 |
end; (* local *) |
19038 | 573 |
|
19957 | 574 |
|
20455 | 575 |
(** toplevel interface **) |
18515 | 576 |
|
577 |
local |
|
578 |
||
579 |
structure P = OuterParse |
|
580 |
and K = OuterKeyword |
|
581 |
||
582 |
in |
|
583 |
||
20385 | 584 |
val (classK, instanceK, print_classesK) = ("class", "instance", "print_classes") |
18515 | 585 |
|
20385 | 586 |
fun wrap_add_instance_sort (class, sort) thy = |
587 |
(if forall (is_some o lookup_class_data thy) (Sign.read_sort thy sort) |
|
21094 | 588 |
andalso (is_some o lookup_class_data thy) (Sign.intern_class thy class) |
20385 | 589 |
then instance_sort else axclass_instance_sort) (class, sort) thy; |
19136 | 590 |
|
20385 | 591 |
val class_subP = P.name -- Scan.repeat (P.$$$ "+" |-- P.name) >> (op ::); |
20428 | 592 |
val class_bodyP = P.!!! (Scan.repeat1 P.locale_element); |
20385 | 593 |
|
20601 | 594 |
val parse_arity = |
595 |
(P.xname --| P.$$$ "::" -- P.!!! P.arity) |
|
19136 | 596 |
>> (fn (tyco, (asorts, sort)) => ((tyco, asorts), sort)); |
18911 | 597 |
|
18515 | 598 |
val classP = |
20385 | 599 |
OuterSyntax.command classK "define operational type class" K.thy_decl ( |
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
600 |
P.name --| P.$$$ "=" |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
601 |
-- ( |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
602 |
class_subP --| P.$$$ "+" -- class_bodyP |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
603 |
|| class_subP >> rpair [] |
20964 | 604 |
|| class_bodyP >> pair []) |
605 |
-- P.opt_begin |
|
606 |
>> (fn ((bname, (supclasses, elems)), begin) => |
|
20986 | 607 |
Toplevel.begin_local_theory begin (class bname supclasses elems #-> TheoryTarget.begin))); |
18515 | 608 |
|
18575 | 609 |
val instanceP = |
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
610 |
OuterSyntax.command instanceK "prove type arity or subclass relation" K.thy_goal (( |
20385 | 611 |
P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> wrap_add_instance_sort |
20601 | 612 |
|| P.opt_thm_name ":" -- (P.and_list1 parse_arity -- Scan.repeat (P.opt_thm_name ":" -- P.prop)) |
20175 | 613 |
>> (fn (("", []), ([((tyco, asorts), sort)], [])) => axclass_instance_arity I [(tyco, asorts, sort)] |
614 |
| (natts, (arities, defs)) => instance_arity arities natts defs) |
|
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
615 |
) >> (Toplevel.print oo Toplevel.theory_to_proof)); |
18575 | 616 |
|
20385 | 617 |
val print_classesP = |
618 |
OuterSyntax.improper_command print_classesK "print classes of this theory" K.diag |
|
619 |
(Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory |
|
620 |
o Toplevel.keep (print_classes o Toplevel.theory_of))); |
|
621 |
||
622 |
val _ = OuterSyntax.add_parsers [classP, instanceP, print_classesP]; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
623 |
|
20455 | 624 |
end; (*local*) |
18515 | 625 |
|
20455 | 626 |
end; |