24914
|
1 |
(* Title: Pure/Isar/subclass.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
|
|
5 |
Prove subclass relations between type classes
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature SUBCLASS =
|
|
9 |
sig
|
|
10 |
val subclass: class -> local_theory -> Proof.state
|
|
11 |
val subclass_cmd: xstring -> local_theory -> Proof.state
|
|
12 |
(*FIXME val prove_subclass: tactic -> class * class -> theory -> theory*)
|
|
13 |
end;
|
|
14 |
|
|
15 |
structure Subclass : SUBCLASS =
|
|
16 |
struct
|
|
17 |
|
|
18 |
(** auxiliary **)
|
|
19 |
|
|
20 |
fun prove_interpretation_in tac after_qed (name, expr) =
|
|
21 |
Locale.interpretation_in_locale
|
|
22 |
(ProofContext.theory after_qed) (name, expr)
|
|
23 |
#> Proof.global_terminal_proof
|
|
24 |
(Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
|
|
25 |
#> ProofContext.theory_of;
|
|
26 |
|
|
27 |
fun OF_LAST thm1 thm2 =
|
|
28 |
let
|
|
29 |
val n = (length o Logic.strip_imp_prems o prop_of) thm2;
|
|
30 |
in (thm1 RSN (n, thm2)) end;
|
|
31 |
|
|
32 |
fun strip_all_ofclass thy sort =
|
|
33 |
let
|
|
34 |
val typ = TVar ((Name.aT, 0), sort);
|
|
35 |
fun prem_inclass t =
|
|
36 |
case Logic.strip_imp_prems t
|
|
37 |
of ofcls :: _ => try Logic.dest_inclass ofcls
|
|
38 |
| [] => NONE;
|
|
39 |
fun strip_ofclass class thm =
|
|
40 |
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache;
|
|
41 |
fun strip thm = case (prem_inclass o Thm.prop_of) thm
|
|
42 |
of SOME (_, class) => thm |> strip_ofclass class |> strip
|
|
43 |
| NONE => thm;
|
|
44 |
in strip end;
|
|
45 |
|
|
46 |
|
|
47 |
(** subclassing **)
|
|
48 |
|
|
49 |
local
|
|
50 |
|
|
51 |
fun mk_subclass_rule lthy sup =
|
|
52 |
let
|
|
53 |
(*FIXME check for proper parameter inclusion (consts_of) (?)*)
|
|
54 |
val ctxt = LocalTheory.target_of lthy;
|
|
55 |
val thy = ProofContext.theory_of ctxt;
|
|
56 |
val locale = Class.locale_of_class thy sup;
|
|
57 |
in
|
|
58 |
Locale.global_asms_of thy locale
|
|
59 |
|> maps snd
|
|
60 |
|> map (ObjectLogic.ensure_propT thy)
|
|
61 |
end;
|
|
62 |
|
|
63 |
fun gen_subclass prep_class raw_sup lthy =
|
|
64 |
let
|
|
65 |
(*FIXME cleanup, provide tactical interface*)
|
|
66 |
val ctxt = LocalTheory.target_of lthy;
|
|
67 |
val thy = ProofContext.theory_of ctxt;
|
|
68 |
val ctxt_thy = ProofContext.init thy;
|
|
69 |
val sup = prep_class thy raw_sup;
|
|
70 |
val sub = case Option.mapPartial (Class.class_of_locale thy)
|
|
71 |
(TheoryTarget.peek lthy)
|
|
72 |
of NONE => error "not in class context"
|
|
73 |
| SOME sub => sub;
|
|
74 |
val export =
|
|
75 |
Assumption.export false ctxt ctxt_thy
|
|
76 |
#> singleton (Variable.export ctxt ctxt_thy);
|
|
77 |
val loc_name = Class.locale_of_class thy sub;
|
|
78 |
val loc_expr = Locale.Locale (Class.locale_of_class thy sup);
|
|
79 |
fun prove_classrel interp thy =
|
|
80 |
let
|
|
81 |
val classes = Sign.complete_sort thy [sup]
|
|
82 |
|> filter_out (fn class' => Sign.subsort thy ([sub], [class']));
|
|
83 |
fun instance_subclass (class1, class2) thy =
|
|
84 |
let
|
|
85 |
val ax = #axioms (AxClass.get_definition thy class1);
|
|
86 |
val intro = #intro (AxClass.get_definition thy class2)
|
|
87 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy
|
|
88 |
(TVar ((Name.aT, 0), [class1])))] [];
|
|
89 |
val thm =
|
|
90 |
intro
|
|
91 |
|> OF_LAST (interp OF ax)
|
|
92 |
|> strip_all_ofclass thy (Sign.super_classes thy class2);
|
|
93 |
in
|
|
94 |
thy |> AxClass.add_classrel thm
|
|
95 |
end;
|
|
96 |
in
|
|
97 |
thy |> fold_rev (curry instance_subclass sub) classes
|
|
98 |
end;
|
|
99 |
fun after_qed thmss =
|
|
100 |
let
|
|
101 |
val thm = Conjunction.intr_balanced (the_single thmss);;
|
|
102 |
val interp = export thm;
|
|
103 |
in
|
|
104 |
LocalTheory.theory (prove_classrel interp
|
|
105 |
#> prove_interpretation_in (ProofContext.fact_tac [thm] 1)
|
|
106 |
I (loc_name, loc_expr))
|
|
107 |
(*#> (fn lthy => LocalTheory.reinit lthy thy) FIXME does not work as expected*)
|
|
108 |
end;
|
|
109 |
val goal = Element.Shows
|
|
110 |
[(("", []), map (rpair []) (mk_subclass_rule lthy sup))];
|
|
111 |
in
|
|
112 |
Specification.theorem_i Thm.internalK NONE after_qed ("", []) [] goal true lthy
|
|
113 |
end;
|
|
114 |
|
|
115 |
in
|
|
116 |
|
|
117 |
val subclass = gen_subclass (K I);
|
|
118 |
val subclass_cmd = gen_subclass Sign.read_class;
|
|
119 |
|
|
120 |
end; (*local*)
|
|
121 |
|
|
122 |
end;
|