haftmann@37743
|
1 |
(* Title: HOL/Tools/transfer.ML
|
haftmann@37743
|
2 |
Author: Amine Chaieb, University of Cambridge, 2009
|
haftmann@37743
|
3 |
Jeremy Avigad, Carnegie Mellon University
|
haftmann@37743
|
4 |
Florian Haftmann, TU Muenchen
|
haftmann@35648
|
5 |
|
haftmann@35648
|
6 |
Simple transfer principle on theorems.
|
huffman@31704
|
7 |
*)
|
huffman@31704
|
8 |
|
haftmann@32557
|
9 |
signature TRANSFER =
|
huffman@31704
|
10 |
sig
|
haftmann@35648
|
11 |
datatype selection = Direction of term * term | Hints of string list | Prop
|
haftmann@35669
|
12 |
val transfer: Context.generic -> selection -> string list -> thm -> thm list
|
huffman@31704
|
13 |
type entry
|
haftmann@35671
|
14 |
val add: thm -> bool -> entry -> Context.generic -> Context.generic
|
haftmann@35671
|
15 |
val del: thm -> entry -> Context.generic -> Context.generic
|
haftmann@35671
|
16 |
val drop: thm -> Context.generic -> Context.generic
|
huffman@31704
|
17 |
val setup: theory -> theory
|
huffman@31704
|
18 |
end;
|
huffman@31704
|
19 |
|
haftmann@32557
|
20 |
structure Transfer : TRANSFER =
|
huffman@31704
|
21 |
struct
|
haftmann@32557
|
22 |
|
haftmann@35638
|
23 |
(* data administration *)
|
haftmann@35638
|
24 |
|
haftmann@35647
|
25 |
val direction_of = Thm.dest_binop o Thm.dest_arg o cprop_of;
|
haftmann@35647
|
26 |
|
haftmann@35821
|
27 |
val transfer_morphism_key = Drule.strip_imp_concl (Thm.cprop_of @{thm transfer_morphismI});
|
haftmann@35821
|
28 |
|
haftmann@35645
|
29 |
fun check_morphism_key ctxt key =
|
haftmann@35645
|
30 |
let
|
haftmann@35821
|
31 |
val _ = Thm.match (transfer_morphism_key, Thm.cprop_of key)
|
haftmann@35821
|
32 |
handle Pattern.MATCH => error ("Transfer: expected theorem of the form "
|
haftmann@35821
|
33 |
^ quote (Syntax.string_of_term ctxt (Thm.term_of transfer_morphism_key)));
|
haftmann@35647
|
34 |
in direction_of key end;
|
haftmann@35638
|
35 |
|
haftmann@35669
|
36 |
type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
|
haftmann@35670
|
37 |
hints : string list };
|
haftmann@33304
|
38 |
|
haftmann@35671
|
39 |
val empty_entry = { inj = [], embed = [], return = [], cong = [], hints = [] };
|
haftmann@35670
|
40 |
fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry,
|
haftmann@35670
|
41 |
{ inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) =
|
haftmann@35669
|
42 |
{ inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
|
haftmann@35669
|
43 |
return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
|
haftmann@35670
|
44 |
hints = merge (op =) (hints1, hints2) };
|
haftmann@33304
|
45 |
|
wenzelm@33519
|
46 |
structure Data = Generic_Data
|
huffman@31704
|
47 |
(
|
haftmann@35638
|
48 |
type T = (thm * entry) list;
|
haftmann@35638
|
49 |
val empty = [];
|
haftmann@35647
|
50 |
val extend = I;
|
haftmann@35638
|
51 |
val merge = AList.join Thm.eq_thm (K merge_entry);
|
huffman@31704
|
52 |
);
|
huffman@31704
|
53 |
|
huffman@31704
|
54 |
|
haftmann@35647
|
55 |
(* data lookup *)
|
huffman@31704
|
56 |
|
haftmann@35708
|
57 |
fun transfer_rules_of ({ inj, embed, return, cong, ... } : entry) =
|
haftmann@35670
|
58 |
(inj, embed, return, cong);
|
haftmann@35670
|
59 |
|
haftmann@35647
|
60 |
fun get_by_direction context (a, D) =
|
haftmann@35647
|
61 |
let
|
haftmann@35647
|
62 |
val ctxt = Context.proof_of context;
|
haftmann@35647
|
63 |
val certify = Thm.cterm_of (Context.theory_of context);
|
haftmann@35647
|
64 |
val a0 = certify a;
|
haftmann@35647
|
65 |
val D0 = certify D;
|
haftmann@35647
|
66 |
fun eq_direction ((a, D), thm') =
|
haftmann@35647
|
67 |
let
|
haftmann@35647
|
68 |
val (a', D') = direction_of thm';
|
haftmann@35669
|
69 |
in a aconvc a' andalso D aconvc D' end;
|
haftmann@35669
|
70 |
in case AList.lookup eq_direction (Data.get context) (a0, D0) of
|
haftmann@35670
|
71 |
SOME e => ((a0, D0), transfer_rules_of e)
|
haftmann@35647
|
72 |
| NONE => error ("Transfer: no such instance: ("
|
haftmann@35647
|
73 |
^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
|
haftmann@35647
|
74 |
end;
|
haftmann@35647
|
75 |
|
haftmann@35647
|
76 |
fun get_by_hints context hints =
|
haftmann@35647
|
77 |
let
|
haftmann@35647
|
78 |
val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
|
haftmann@35670
|
79 |
then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context);
|
wenzelm@46301
|
80 |
val _ = if null insts then error ("Transfer: no such labels: " ^ commas_quote hints) else ();
|
haftmann@35647
|
81 |
in insts end;
|
haftmann@35647
|
82 |
|
haftmann@35647
|
83 |
fun splits P [] = []
|
haftmann@35647
|
84 |
| splits P (xs as (x :: _)) =
|
haftmann@35647
|
85 |
let
|
haftmann@35647
|
86 |
val (pss, qss) = List.partition (P x) xs;
|
haftmann@35647
|
87 |
in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
|
haftmann@35647
|
88 |
|
haftmann@35647
|
89 |
fun get_by_prop context t =
|
haftmann@35647
|
90 |
let
|
haftmann@35647
|
91 |
val tys = map snd (Term.add_vars t []);
|
haftmann@35647
|
92 |
val _ = if null tys then error "Transfer: unable to guess instance" else ();
|
haftmann@35647
|
93 |
val tyss = splits (curry Type.could_unify) tys;
|
haftmann@35647
|
94 |
val get_ty = typ_of o ctyp_of_term o fst o direction_of;
|
haftmann@35670
|
95 |
val insts = map_filter (fn tys => get_first (fn (k, e) =>
|
haftmann@35647
|
96 |
if Type.could_unify (hd tys, range_type (get_ty k))
|
haftmann@35670
|
97 |
then SOME (direction_of k, transfer_rules_of e)
|
haftmann@35647
|
98 |
else NONE) (Data.get context)) tyss;
|
haftmann@35647
|
99 |
val _ = if null insts then
|
haftmann@35647
|
100 |
error "Transfer: no instances, provide direction or hints explicitly" else ();
|
haftmann@35647
|
101 |
in insts end;
|
huffman@31704
|
102 |
|
huffman@31704
|
103 |
|
haftmann@35638
|
104 |
(* applying transfer data *)
|
huffman@31704
|
105 |
|
haftmann@35684
|
106 |
fun transfer_thm ((raw_a, raw_D), (inj, embed, return, cong)) leave ctxt1 thm =
|
haftmann@35645
|
107 |
let
|
haftmann@35684
|
108 |
(* identify morphism function *)
|
haftmann@35684
|
109 |
val ([a, D], ctxt2) = ctxt1
|
haftmann@35684
|
110 |
|> Variable.import true (map Drule.mk_term [raw_a, raw_D])
|
haftmann@35684
|
111 |
|>> map Drule.dest_term o snd;
|
wenzelm@47368
|
112 |
val transform = Thm.apply @{cterm "Trueprop"} o Thm.apply D;
|
haftmann@35684
|
113 |
val T = Thm.typ_of (Thm.ctyp_of_term a);
|
haftmann@35684
|
114 |
val (aT, bT) = (Term.range_type T, Term.domain_type T);
|
haftmann@35684
|
115 |
|
haftmann@35684
|
116 |
(* determine variables to transfer *)
|
haftmann@35684
|
117 |
val ctxt3 = ctxt2
|
haftmann@35684
|
118 |
|> Variable.declare_thm thm
|
haftmann@35684
|
119 |
|> Variable.declare_term (term_of a)
|
haftmann@35684
|
120 |
|> Variable.declare_term (term_of D);
|
wenzelm@43232
|
121 |
val certify = Thm.cterm_of (Proof_Context.theory_of ctxt3);
|
haftmann@35684
|
122 |
val vars = filter (fn ((v, _), T) => Type.could_unify (T, aT) andalso
|
haftmann@35684
|
123 |
not (member (op =) leave v)) (Term.add_vars (Thm.prop_of thm) []);
|
haftmann@35684
|
124 |
val c_vars = map (certify o Var) vars;
|
haftmann@35684
|
125 |
val (vs', ctxt4) = Variable.variant_fixes (map (fst o fst) vars) ctxt3;
|
haftmann@35684
|
126 |
val c_vars' = map (certify o (fn v => Free (v, bT))) vs';
|
wenzelm@47368
|
127 |
val c_exprs' = map (Thm.apply a) c_vars';
|
huffman@31704
|
128 |
|
haftmann@35684
|
129 |
(* transfer *)
|
haftmann@35684
|
130 |
val (hyps, ctxt5) = ctxt4
|
haftmann@35684
|
131 |
|> Assumption.add_assumes (map transform c_vars');
|
wenzelm@46491
|
132 |
val simpset =
|
wenzelm@46491
|
133 |
Simplifier.context ctxt5 HOL_ss addsimps (inj @ embed @ return)
|
wenzelm@46491
|
134 |
|> fold Simplifier.add_cong cong;
|
haftmann@35684
|
135 |
val thm' = thm
|
haftmann@35684
|
136 |
|> Drule.cterm_instantiate (c_vars ~~ c_exprs')
|
haftmann@35684
|
137 |
|> fold_rev Thm.implies_intr (map cprop_of hyps)
|
haftmann@35684
|
138 |
|> Simplifier.asm_full_simplify simpset
|
haftmann@35684
|
139 |
in singleton (Variable.export ctxt5 ctxt1) thm' end;
|
haftmann@35684
|
140 |
|
haftmann@35684
|
141 |
fun transfer_thm_multiple insts leave ctxt thm =
|
haftmann@35684
|
142 |
map (fn inst => transfer_thm inst leave ctxt thm) insts;
|
haftmann@35647
|
143 |
|
haftmann@35648
|
144 |
datatype selection = Direction of term * term | Hints of string list | Prop;
|
huffman@31704
|
145 |
|
haftmann@35648
|
146 |
fun insts_for context thm (Direction direction) = [get_by_direction context direction]
|
haftmann@35648
|
147 |
| insts_for context thm (Hints hints) = get_by_hints context hints
|
haftmann@35648
|
148 |
| insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
|
huffman@31704
|
149 |
|
haftmann@35648
|
150 |
fun transfer context selection leave thm =
|
haftmann@35684
|
151 |
transfer_thm_multiple (insts_for context thm selection) leave (Context.proof_of context) thm;
|
huffman@31704
|
152 |
|
huffman@31704
|
153 |
|
haftmann@35647
|
154 |
(* maintaining transfer data *)
|
wenzelm@32822
|
155 |
|
haftmann@35671
|
156 |
fun extend_entry ctxt (a, D) guess
|
haftmann@35671
|
157 |
{ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }
|
haftmann@35671
|
158 |
{ inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
|
haftmann@35638
|
159 |
let
|
haftmann@35671
|
160 |
fun add_del eq del add = union eq add #> subtract eq del;
|
haftmann@35671
|
161 |
val guessed = if guess
|
haftmann@35684
|
162 |
then map (fn thm => transfer_thm
|
haftmann@35671
|
163 |
((a, D), (if null inj1 then inj2 else inj1, [], [], cong1)) [] ctxt thm RS sym) embed1
|
haftmann@35671
|
164 |
else [];
|
haftmann@35638
|
165 |
in
|
haftmann@35671
|
166 |
{ inj = union Thm.eq_thm inj1 inj2, embed = union Thm.eq_thm embed1 embed2,
|
haftmann@35671
|
167 |
return = union Thm.eq_thm guessed (union Thm.eq_thm return1 return2),
|
haftmann@35671
|
168 |
cong = union Thm.eq_thm cong1 cong2, hints = union (op =) hints1 hints2 }
|
haftmann@35638
|
169 |
end;
|
haftmann@35638
|
170 |
|
haftmann@35671
|
171 |
fun diminish_entry
|
haftmann@35671
|
172 |
{ inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 }
|
haftmann@35671
|
173 |
{ inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
|
haftmann@35671
|
174 |
{ inj = subtract Thm.eq_thm inj0 inj2, embed = subtract Thm.eq_thm embed0 embed2,
|
haftmann@35671
|
175 |
return = subtract Thm.eq_thm return0 return2, cong = subtract Thm.eq_thm cong0 cong2,
|
haftmann@35671
|
176 |
hints = subtract (op =) hints0 hints2 };
|
huffman@31704
|
177 |
|
haftmann@35671
|
178 |
fun add key guess entry context =
|
haftmann@35671
|
179 |
let
|
haftmann@35671
|
180 |
val ctxt = Context.proof_of context;
|
haftmann@35671
|
181 |
val a_D = check_morphism_key ctxt key;
|
haftmann@35671
|
182 |
in
|
haftmann@35671
|
183 |
context
|
haftmann@35671
|
184 |
|> Data.map (AList.map_default Thm.eq_thm
|
haftmann@35671
|
185 |
(key, empty_entry) (extend_entry ctxt a_D guess entry))
|
haftmann@35671
|
186 |
end;
|
haftmann@35671
|
187 |
|
haftmann@35671
|
188 |
fun del key entry = Data.map (AList.map_entry Thm.eq_thm key (diminish_entry entry));
|
haftmann@35671
|
189 |
|
haftmann@35671
|
190 |
fun drop key = Data.map (AList.delete Thm.eq_thm key);
|
haftmann@35647
|
191 |
|
haftmann@35638
|
192 |
|
haftmann@35638
|
193 |
(* syntax *)
|
huffman@31704
|
194 |
|
huffman@31704
|
195 |
local
|
huffman@31704
|
196 |
|
haftmann@35645
|
197 |
fun these scan = Scan.optional scan [];
|
haftmann@35645
|
198 |
fun these_pair scan = Scan.optional scan ([], []);
|
huffman@31704
|
199 |
|
haftmann@35645
|
200 |
fun keyword k = Scan.lift (Args.$$$ k) >> K ();
|
haftmann@35645
|
201 |
fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
|
huffman@31704
|
202 |
|
haftmann@35645
|
203 |
val addN = "add";
|
haftmann@35645
|
204 |
val delN = "del";
|
haftmann@35684
|
205 |
val keyN = "key";
|
haftmann@35645
|
206 |
val modeN = "mode";
|
haftmann@35645
|
207 |
val automaticN = "automatic";
|
haftmann@35645
|
208 |
val manualN = "manual";
|
haftmann@35669
|
209 |
val injN = "inj";
|
haftmann@35669
|
210 |
val embedN = "embed";
|
haftmann@35669
|
211 |
val returnN = "return";
|
haftmann@35669
|
212 |
val congN = "cong";
|
haftmann@35669
|
213 |
val labelsN = "labels";
|
haftmann@35669
|
214 |
|
haftmann@35669
|
215 |
val leavingN = "leaving";
|
haftmann@35645
|
216 |
val directionN = "direction";
|
huffman@31704
|
217 |
|
haftmann@35684
|
218 |
val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon keyN
|
haftmann@35684
|
219 |
|| keyword_colon modeN || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
|
haftmann@35669
|
220 |
|| keyword_colon congN || keyword_colon labelsN
|
haftmann@35669
|
221 |
|| keyword_colon leavingN || keyword_colon directionN;
|
huffman@31704
|
222 |
|
haftmann@35645
|
223 |
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
|
haftmann@35645
|
224 |
val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
|
huffman@31704
|
225 |
|
haftmann@35645
|
226 |
val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
|
haftmann@35645
|
227 |
|| (Scan.lift (Args.$$$ automaticN) >> K true));
|
haftmann@35645
|
228 |
val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
|
haftmann@35645
|
229 |
val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
|
haftmann@35645
|
230 |
val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
|
haftmann@35645
|
231 |
val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
|
haftmann@35645
|
232 |
val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
|
haftmann@35645
|
233 |
|
haftmann@35670
|
234 |
val entry_pair = these_pair inj -- these_pair embed
|
haftmann@35648
|
235 |
-- these_pair return -- these_pair cong -- these_pair labels
|
haftmann@35670
|
236 |
>> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)),
|
haftmann@35669
|
237 |
(hintsa, hintsd)) =>
|
haftmann@35684
|
238 |
({ inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa },
|
haftmann@35684
|
239 |
{ inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }));
|
haftmann@35645
|
240 |
|
haftmann@35648
|
241 |
val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
|
haftmann@35648
|
242 |
|| these names >> (fn hints => if null hints then Prop else Hints hints);
|
haftmann@35645
|
243 |
|
huffman@31704
|
244 |
in
|
huffman@31704
|
245 |
|
haftmann@35684
|
246 |
val transfer_attribute = keyword delN >> K (Thm.declaration_attribute drop)
|
haftmann@35684
|
247 |
|| keyword addN |-- Scan.optional mode true -- entry_pair
|
haftmann@35671
|
248 |
>> (fn (guess, (entry_add, entry_del)) => Thm.declaration_attribute
|
haftmann@35684
|
249 |
(fn thm => add thm guess entry_add #> del thm entry_del))
|
haftmann@35684
|
250 |
|| keyword_colon keyN |-- Attrib.thm
|
haftmann@35684
|
251 |
>> (fn key => Thm.declaration_attribute
|
haftmann@35684
|
252 |
(fn thm => add key false
|
haftmann@35684
|
253 |
{ inj = [], embed = [], return = [thm], cong = [], hints = [] }));
|
huffman@31704
|
254 |
|
haftmann@35648
|
255 |
val transferred_attribute = selection -- these (keyword_colon leavingN |-- names)
|
haftmann@35669
|
256 |
>> (fn (selection, leave) => Thm.rule_attribute (fn context =>
|
haftmann@35669
|
257 |
Conjunction.intr_balanced o transfer context selection leave));
|
huffman@31704
|
258 |
|
huffman@31704
|
259 |
end;
|
huffman@31704
|
260 |
|
huffman@31704
|
261 |
|
huffman@31704
|
262 |
(* theory setup *)
|
huffman@31704
|
263 |
|
huffman@31704
|
264 |
val setup =
|
haftmann@35648
|
265 |
Attrib.setup @{binding transfer} transfer_attribute
|
huffman@31704
|
266 |
"Installs transfer data" #>
|
haftmann@35648
|
267 |
Attrib.setup @{binding transferred} transferred_attribute
|
huffman@31704
|
268 |
"Transfers theorems";
|
huffman@31704
|
269 |
|
huffman@31704
|
270 |
end;
|