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