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