| author | huffman | 
| Tue, 23 Feb 2010 14:44:43 -0800 | |
| changeset 35349 | f9801fdeb789 | 
| parent 33519 | e31a85f92ce9 | 
| child 35638 | 50655e2ebc85 | 
| permissions | -rw-r--r-- | 
| 
32557
 
3cfe4c13aa6e
plain structure name; signature constraint; shorter lines
 
haftmann 
parents: 
32476 
diff
changeset
 | 
1  | 
(* Author: Amine Chaieb, University of Cambridge, 2009  | 
| 32813 | 2  | 
Author: Jeremy Avigad, Carnegie Mellon University  | 
| 31706 | 3  | 
*)  | 
4  | 
||
| 
32557
 
3cfe4c13aa6e
plain structure name; signature constraint; shorter lines
 
haftmann 
parents: 
32476 
diff
changeset
 | 
5  | 
signature TRANSFER =  | 
| 31706 | 6  | 
sig  | 
7  | 
type data  | 
|
8  | 
type entry  | 
|
9  | 
val get: Proof.context -> data  | 
|
10  | 
val del: attribute  | 
|
11  | 
val setup: theory -> theory  | 
|
12  | 
end;  | 
|
13  | 
||
| 
32557
 
3cfe4c13aa6e
plain structure name; signature constraint; shorter lines
 
haftmann 
parents: 
32476 
diff
changeset
 | 
14  | 
structure Transfer : TRANSFER =  | 
| 31706 | 15  | 
struct  | 
| 
32557
 
3cfe4c13aa6e
plain structure name; signature constraint; shorter lines
 
haftmann 
parents: 
32476 
diff
changeset
 | 
16  | 
|
| 33321 | 17  | 
type entry = { inj : thm list, emb : thm list, ret : thm list, cong : thm list,
 | 
18  | 
guess : bool, hints : string list };  | 
|
19  | 
||
20  | 
fun merge_entry ({ inj = inj1, emb = emb1, ret = ret1, cong = cong1, guess = guess1, hints = hints1 } : entry,
 | 
|
21  | 
  { inj = inj2, emb = emb2, ret = ret2, cong = cong2, guess = guess2, hints = hints2 } : entry) =
 | 
|
22  | 
    { inj = merge Thm.eq_thm (inj1, inj2), emb = merge Thm.eq_thm (emb1, emb2),
 | 
|
23  | 
ret = merge Thm.eq_thm (ret1, ret2), cong = merge Thm.eq_thm (cong1, cong2),  | 
|
24  | 
guess = guess1 andalso guess2, hints = merge (op =) (hints1, hints2) };  | 
|
25  | 
||
| 31706 | 26  | 
type data = simpset * (thm * entry) list;  | 
27  | 
||
| 33519 | 28  | 
structure Data = Generic_Data  | 
| 31706 | 29  | 
(  | 
30  | 
type T = data;  | 
|
31  | 
val empty = (HOL_ss, []);  | 
|
32  | 
val extend = I;  | 
|
| 33519 | 33  | 
fun merge ((ss1, e1), (ss2, e2)) : T =  | 
| 33321 | 34  | 
(merge_ss (ss1, ss2), AList.join Thm.eq_thm (K merge_entry) (e1, e2));  | 
| 31706 | 35  | 
);  | 
36  | 
||
37  | 
val get = Data.get o Context.Proof;  | 
|
38  | 
||
| 32813 | 39  | 
fun del_data key = apsnd (remove (eq_fst Thm.eq_thm) (key, []));  | 
| 31706 | 40  | 
|
41  | 
val del = Thm.declaration_attribute (Data.map o del_data);  | 
|
| 32813 | 42  | 
val add_ss = Thm.declaration_attribute  | 
| 31706 | 43  | 
(fn th => Data.map (fn (ss,data) => (ss addsimps [th], data)));  | 
44  | 
||
| 32813 | 45  | 
val del_ss = Thm.declaration_attribute  | 
| 31706 | 46  | 
(fn th => Data.map (fn (ss,data) => (ss delsimps [th], data)));  | 
47  | 
||
48  | 
val transM_pat = (Thm.dest_arg1 o Thm.dest_arg o cprop_of) @{thm TransferMorphism_def};
 | 
|
49  | 
||
50  | 
fun merge_update eq m (k,v) [] = [(k,v)]  | 
|
| 32813 | 51  | 
| merge_update eq m (k,v) ((k',v')::al) =  | 
| 31706 | 52  | 
if eq (k,k') then (k',m (v,v')):: al else (k',v') :: merge_update eq m (k,v) al  | 
53  | 
||
| 32813 | 54  | 
fun simpset_of_entry injonly {inj = inj, emb = emb, ret = ret, cong = cg, guess = g, hints = hints} =
 | 
| 31706 | 55  | 
HOL_ss addsimps inj addsimps (if injonly then [] else emb@ret) addcongs cg;  | 
56  | 
||
| 32813 | 57  | 
fun basic_transfer_rule injonly a0 D0 e leave ctxt0 th =  | 
58  | 
let  | 
|
| 
31794
 
71af1fd6a5e4
renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
 
wenzelm 
parents: 
31706 
diff
changeset
 | 
59  | 
val ([a,D], ctxt) = apfst (map Drule.dest_term o snd) (Variable.import true (map Drule.mk_term [a0, D0]) ctxt0)  | 
| 32813 | 60  | 
val (aT,bT) =  | 
61  | 
let val T = typ_of (ctyp_of_term a)  | 
|
| 31706 | 62  | 
in (Term.range_type T, Term.domain_type T)  | 
63  | 
end  | 
|
64  | 
val ctxt' = (Variable.declare_term (term_of a) o Variable.declare_term (term_of D) o Variable.declare_thm th) ctxt  | 
|
65  | 
val ns = filter (fn i => Type.could_unify (snd i, aT) andalso not (fst (fst i) mem_string leave)) (Term.add_vars (prop_of th) [])  | 
|
66  | 
val (ins, ctxt'') = Variable.variant_fixes (map (fst o fst) ns) ctxt'  | 
|
67  | 
val cns = map ((cterm_of o ProofContext.theory_of) ctxt'' o Var) ns  | 
|
68  | 
val cfis = map ((cterm_of o ProofContext.theory_of) ctxt'' o (fn n => Free (n, bT))) ins  | 
|
69  | 
val cis = map (Thm.capply a) cfis  | 
|
70  | 
  val (hs,ctxt''') = Assumption.add_assumes (map (fn ct => Thm.capply @{cterm "Trueprop"} (Thm.capply D ct)) cfis) ctxt''
 | 
|
| 32813 | 71  | 
val th1 = Drule.cterm_instantiate (cns ~~ cis) th  | 
72  | 
val th2 = fold Thm.elim_implies hs (fold_rev implies_intr (map cprop_of hs) th1)  | 
|
73  | 
val th3 = Simplifier.asm_full_simplify (Simplifier.context ctxt''' (simpset_of_entry injonly e))  | 
|
| 31706 | 74  | 
(fold_rev implies_intr (map cprop_of hs) th2)  | 
75  | 
in hd (Variable.export ctxt''' ctxt0 [th3]) end;  | 
|
76  | 
||
77  | 
local  | 
|
| 32813 | 78  | 
fun transfer_ruleh a D leave ctxt th =  | 
| 31706 | 79  | 
let val (ss,al) = get ctxt  | 
80  | 
val a0 = cterm_of (ProofContext.theory_of ctxt) a  | 
|
81  | 
val D0 = cterm_of (ProofContext.theory_of ctxt) D  | 
|
| 32813 | 82  | 
fun h (th', e) = let val (a',D') = (Thm.dest_binop o Thm.dest_arg o cprop_of) th'  | 
| 31706 | 83  | 
in if a0 aconvc a' andalso D0 aconvc D' then SOME e else NONE  | 
84  | 
end  | 
|
85  | 
in case get_first h al of  | 
|
86  | 
SOME e => basic_transfer_rule false a0 D0 e leave ctxt th  | 
|
87  | 
| NONE => error "Transfer: corresponding instance not found in context-data"  | 
|
88  | 
end  | 
|
| 32813 | 89  | 
in fun transfer_rule (a,D) leave (gctxt,th) =  | 
| 31706 | 90  | 
(gctxt, transfer_ruleh a D leave (Context.proof_of gctxt) th)  | 
91  | 
end;  | 
|
92  | 
||
93  | 
fun splits P [] = []  | 
|
| 32813 | 94  | 
| splits P (xxs as (x::xs)) =  | 
| 31706 | 95  | 
let val pss = filter (P x) xxs  | 
96  | 
val qss = filter_out (P x) xxs  | 
|
97  | 
in if null pss then [qss] else if null qss then [pss] else pss:: splits P qss  | 
|
98  | 
end  | 
|
99  | 
||
| 32813 | 100  | 
fun all_transfers leave (gctxt,th) =  | 
101  | 
let  | 
|
| 31706 | 102  | 
val ctxt = Context.proof_of gctxt  | 
103  | 
val tys = map snd (Term.add_vars (prop_of th) [])  | 
|
104  | 
val _ = if null tys then error "transfer: Unable to guess instance" else ()  | 
|
| 32813 | 105  | 
val tyss = splits (curry Type.could_unify) tys  | 
| 31706 | 106  | 
val get_ty = typ_of o ctyp_of_term o fst o Thm.dest_binop o Thm.dest_arg o cprop_of  | 
107  | 
val get_aD = Thm.dest_binop o Thm.dest_arg o cprop_of  | 
|
| 32813 | 108  | 
val insts =  | 
109  | 
map_filter (fn tys =>  | 
|
110  | 
get_first (fn (k,ss) =>  | 
|
111  | 
if Type.could_unify (hd tys, range_type (get_ty k))  | 
|
112  | 
then SOME (get_aD k, ss)  | 
|
113  | 
else NONE) (snd (get ctxt))) tyss  | 
|
114  | 
val _ =  | 
|
115  | 
if null insts then  | 
|
116  | 
error "Transfer guesser: there were no possible instances, use direction: in order to provide a direction"  | 
|
117  | 
else ()  | 
|
| 31706 | 118  | 
val ths = map (fn ((a,D),e) => basic_transfer_rule false a D e leave ctxt th) insts  | 
119  | 
val cth = Conjunction.intr_balanced ths  | 
|
120  | 
in (gctxt, cth)  | 
|
121  | 
end;  | 
|
122  | 
||
| 32813 | 123  | 
fun transfer_rule_by_hint ls leave (gctxt,th) =  | 
124  | 
let  | 
|
| 31706 | 125  | 
val ctxt = Context.proof_of gctxt  | 
126  | 
val get_aD = Thm.dest_binop o Thm.dest_arg o cprop_of  | 
|
| 32813 | 127  | 
val insts =  | 
128  | 
map_filter (fn (k,e) => if exists (member (op =) (#hints e)) ls  | 
|
| 
32557
 
3cfe4c13aa6e
plain structure name; signature constraint; shorter lines
 
haftmann 
parents: 
32476 
diff
changeset
 | 
129  | 
then SOME (get_aD k, e) else NONE)  | 
| 31706 | 130  | 
(snd (get ctxt))  | 
131  | 
val _ = if null insts then error "Transfer: No labels provided are stored in the context" else ()  | 
|
132  | 
val ths = map (fn ((a,D),e) => basic_transfer_rule false a D e leave ctxt th) insts  | 
|
133  | 
val cth = Conjunction.intr_balanced ths  | 
|
134  | 
in (gctxt, cth)  | 
|
135  | 
end;  | 
|
136  | 
||
137  | 
||
| 32813 | 138  | 
fun transferred_attribute ls NONE leave =  | 
| 31706 | 139  | 
if null ls then all_transfers leave else transfer_rule_by_hint ls leave  | 
140  | 
| transferred_attribute _ (SOME (a,D)) leave = transfer_rule (a,D) leave  | 
|
141  | 
||
| 32813 | 142  | 
|
143  | 
(* Add data to the context *)  | 
|
144  | 
||
| 31706 | 145  | 
fun gen_merge_entries {inj = inj0, emb = emb0, ret = ret0, cong = cg0, guess = g0, hints = hints0}
 | 
| 32813 | 146  | 
                      ({inj = inj1, emb = emb1, ret = ret1, cong = cg1, guess = g1, hints = hints1},
 | 
| 31706 | 147  | 
                       {inj = inj2, emb = emb2, ret = ret2, cong = cg2, guess = g2, hints = hints2})
 | 
| 32813 | 148  | 
=  | 
| 31706 | 149  | 
let fun h xs0 xs ys = subtract Thm.eq_thm xs0 (merge Thm.eq_thm (xs,ys)) in  | 
| 32813 | 150  | 
 {inj = h inj0 inj1 inj2, emb = h emb0 emb1 emb2,
 | 
| 31706 | 151  | 
ret = h ret0 ret1 ret2, cong = h cg0 cg1 cg2, guess = g1 andalso g2,  | 
| 32813 | 152  | 
hints = subtract (op = : string*string -> bool) hints0  | 
| 33042 | 153  | 
(union (op =) hints1 hints2)}  | 
| 31706 | 154  | 
end;  | 
155  | 
||
156  | 
local  | 
|
157  | 
val h = curry (merge Thm.eq_thm)  | 
|
158  | 
in  | 
|
| 32813 | 159  | 
fun merge_entries ({inj = inj1, emb = emb1, ret = ret1, cong = cg1, guess = g1, hints = hints1},
 | 
160  | 
                   {inj = inj2, emb = emb2, ret = ret2, cong = cg2, guess = g2, hints = hints2}) =
 | 
|
| 33042 | 161  | 
    {inj = h inj1 inj2, emb = h emb1 emb2, ret = h ret1 ret2, cong = h cg1 cg2, guess = g1 andalso g2, hints = union (op =) hints1 hints2}
 | 
| 32813 | 162  | 
end;  | 
| 31706 | 163  | 
|
164  | 
fun add ((inja,injd), (emba,embd), (reta,retd), (cga,cgd), g, (hintsa, hintsd)) =  | 
|
165  | 
Thm.declaration_attribute (fn key => fn context => context |> Data.map  | 
|
| 32813 | 166  | 
(fn (ss, al) =>  | 
| 31706 | 167  | 
let  | 
| 32813 | 168  | 
val _ = Thm.match (transM_pat, Thm.dest_arg (Thm.cprop_of key))  | 
169  | 
handle Pattern.MATCH =>  | 
|
170  | 
error "Attribute expected Theorem of the form : TransferMorphism A a B b"  | 
|
| 31706 | 171  | 
      val e0 = {inj = inja, emb = emba, ret = reta, cong = cga, guess = g, hints = hintsa}
 | 
172  | 
      val ed = {inj = injd, emb = embd, ret = retd, cong = cgd, guess = g, hints = hintsd}
 | 
|
| 32813 | 173  | 
val entry =  | 
174  | 
if g then  | 
|
| 31706 | 175  | 
let val (a0,D0) = (Thm.dest_binop o Thm.dest_arg o cprop_of) key  | 
176  | 
val ctxt0 = ProofContext.init (Thm.theory_of_thm key)  | 
|
| 32813 | 177  | 
val inj' =  | 
178  | 
if null inja then  | 
|
179  | 
#inj  | 
|
180  | 
(case AList.lookup Thm.eq_thm al key of SOME e => e  | 
|
181  | 
| NONE => error "Transfer: can not generate return rules on the fly, either add injectivity axiom or force manual mode with mode: manual")  | 
|
182  | 
else inja  | 
|
| 31706 | 183  | 
             val ret' = merge Thm.eq_thm (reta,  map (fn th => basic_transfer_rule true a0 D0 {inj = inj', emb = [], ret = [], cong = cga, guess = g, hints = hintsa} [] ctxt0 th RS sym) emba)
 | 
| 32813 | 184  | 
         in {inj = inja, emb = emba, ret = ret', cong = cga, guess = g, hints = hintsa} end
 | 
| 31706 | 185  | 
else e0  | 
| 32813 | 186  | 
in (ss, merge_update Thm.eq_thm (gen_merge_entries ed) (key, entry) al)  | 
| 31706 | 187  | 
end));  | 
188  | 
||
189  | 
||
190  | 
(* concrete syntax *)  | 
|
191  | 
||
192  | 
local  | 
|
193  | 
||
194  | 
fun keyword k = Scan.lift (Args.$$$ k) >> K ()  | 
|
195  | 
fun keywordC k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()  | 
|
196  | 
||
197  | 
val congN = "cong"  | 
|
198  | 
val injN = "inj"  | 
|
199  | 
val embedN = "embed"  | 
|
200  | 
val returnN = "return"  | 
|
201  | 
val addN = "add"  | 
|
202  | 
val delN = "del"  | 
|
203  | 
val modeN = "mode"  | 
|
204  | 
val automaticN = "automatic"  | 
|
205  | 
val manualN = "manual"  | 
|
206  | 
val directionN = "direction"  | 
|
207  | 
val labelsN = "labels"  | 
|
208  | 
val leavingN = "leaving"  | 
|
209  | 
||
210  | 
val any_keyword = keywordC congN || keywordC injN || keywordC embedN || keywordC returnN || keywordC directionN || keywordC modeN || keywordC delN || keywordC labelsN || keywordC leavingN  | 
|
211  | 
||
212  | 
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat  | 
|
213  | 
val terms = thms >> map Drule.dest_term  | 
|
| 32813 | 214  | 
val types = thms >> (Logic.dest_type o HOLogic.dest_Trueprop o prop_of o hd)  | 
| 31706 | 215  | 
val name = Scan.lift Args.name  | 
216  | 
val names = Scan.repeat (Scan.unless any_keyword name)  | 
|
217  | 
fun optional scan = Scan.optional scan []  | 
|
218  | 
fun optional2 scan = Scan.optional scan ([],[])  | 
|
219  | 
||
220  | 
val mode = keywordC modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false) || (Scan.lift (Args.$$$ automaticN) >> K true))  | 
|
221  | 
val inj = (keywordC injN |-- thms) -- optional (keywordC delN |-- thms)  | 
|
222  | 
val embed = (keywordC embedN |-- thms) -- optional (keywordC delN |-- thms)  | 
|
223  | 
val return = (keywordC returnN |-- thms) -- optional (keywordC delN |-- thms)  | 
|
224  | 
val cong = (keywordC congN |-- thms) -- optional (keywordC delN |-- thms)  | 
|
225  | 
val addscan = Scan.unless any_keyword (keyword addN)  | 
|
226  | 
val labels = (keywordC labelsN |-- names) -- optional (keywordC delN |-- names)  | 
|
227  | 
val entry = Scan.optional mode true -- optional2 inj -- optional2 embed -- optional2 return -- optional2 cong -- optional2 labels  | 
|
228  | 
||
229  | 
val transf_add = addscan |-- entry  | 
|
230  | 
in  | 
|
231  | 
||
232  | 
val install_att_syntax =  | 
|
233  | 
(Scan.lift (Args.$$$ delN >> K del) ||  | 
|
234  | 
transf_add  | 
|
235  | 
>> (fn (((((g, inj), embed), ret), cg), hints) => add (inj, embed, ret, cg, g, hints)))  | 
|
236  | 
||
| 32476 | 237  | 
val transferred_att_syntax = (optional names -- Scan.option (keywordC directionN |-- (Args.term -- Args.term))  | 
238  | 
-- optional (keywordC leavingN |-- names) >> (fn ((hints, aD),leave) => transferred_attribute hints aD leave));  | 
|
| 31706 | 239  | 
|
240  | 
end;  | 
|
241  | 
||
242  | 
||
243  | 
(* theory setup *)  | 
|
244  | 
||
245  | 
val setup =  | 
|
246  | 
  Attrib.setup @{binding transfer} install_att_syntax
 | 
|
247  | 
"Installs transfer data" #>  | 
|
248  | 
  Attrib.setup @{binding transfer_simps} (Attrib.add_del add_ss del_ss)
 | 
|
249  | 
"simp rules for transfer" #>  | 
|
250  | 
  Attrib.setup @{binding transferred} transferred_att_syntax
 | 
|
251  | 
"Transfers theorems";  | 
|
252  | 
||
253  | 
end;  |