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