renamed Tools/transfer.ML to Tools/legacy_transfer.ML
authorhuffman
Tue Apr 03 22:04:50 2012 +0200 (2012-04-03)
changeset 47324ed2bad9b7c6a
parent 47318 dac11ab96277
child 47325 ec6187036495
renamed Tools/transfer.ML to Tools/legacy_transfer.ML
src/HOL/IsaMakefile
src/HOL/Nat_Transfer.thy
src/HOL/Tools/legacy_transfer.ML
src/HOL/Tools/transfer.ML
     1.1 --- a/src/HOL/IsaMakefile	Tue Apr 03 20:56:32 2012 +0200
     1.2 +++ b/src/HOL/IsaMakefile	Tue Apr 03 22:04:50 2012 +0200
     1.3 @@ -316,6 +316,7 @@
     1.4    Tools/code_evaluation.ML \
     1.5    Tools/groebner.ML \
     1.6    Tools/int_arith.ML \
     1.7 +  Tools/legacy_transfer.ML \
     1.8    Tools/Lifting/lifting_def.ML \
     1.9    Tools/Lifting/lifting_info.ML \
    1.10    Tools/Lifting/lifting_term.ML \
    1.11 @@ -393,7 +394,6 @@
    1.12    Tools/SMT/z3_proof_tools.ML \
    1.13    Tools/string_code.ML \
    1.14    Tools/string_syntax.ML \
    1.15 -  Tools/transfer.ML \
    1.16  
    1.17  $(OUT)/HOL-Main: main.ML $(MAIN_DEPENDENCIES)
    1.18  	@$(ISABELLE_TOOL) usedir -b -f main.ML -g true $(OUT)/Pure HOL-Main
     2.1 --- a/src/HOL/Nat_Transfer.thy	Tue Apr 03 20:56:32 2012 +0200
     2.2 +++ b/src/HOL/Nat_Transfer.thy	Tue Apr 03 22:04:50 2012 +0200
     2.3 @@ -5,7 +5,7 @@
     2.4  
     2.5  theory Nat_Transfer
     2.6  imports Int
     2.7 -uses ("Tools/transfer.ML")
     2.8 +uses ("Tools/legacy_transfer.ML")
     2.9  begin
    2.10  
    2.11  subsection {* Generic transfer machinery *}
    2.12 @@ -16,9 +16,9 @@
    2.13  lemma transfer_morphismI[intro]: "transfer_morphism f A"
    2.14    by (simp add: transfer_morphism_def)
    2.15  
    2.16 -use "Tools/transfer.ML"
    2.17 +use "Tools/legacy_transfer.ML"
    2.18  
    2.19 -setup Transfer.setup
    2.20 +setup Legacy_Transfer.setup
    2.21  
    2.22  
    2.23  subsection {* Set up transfer from nat to int *}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Tools/legacy_transfer.ML	Tue Apr 03 22:04:50 2012 +0200
     3.3 @@ -0,0 +1,270 @@
     3.4 +(*  Title:      HOL/Tools/transfer.ML
     3.5 +    Author:     Amine Chaieb, University of Cambridge, 2009
     3.6 +                Jeremy Avigad, Carnegie Mellon University
     3.7 +                Florian Haftmann, TU Muenchen
     3.8 +
     3.9 +Simple transfer principle on theorems.
    3.10 +*)
    3.11 +
    3.12 +signature LEGACY_TRANSFER =
    3.13 +sig
    3.14 +  datatype selection = Direction of term * term | Hints of string list | Prop
    3.15 +  val transfer: Context.generic -> selection -> string list -> thm -> thm list
    3.16 +  type entry
    3.17 +  val add: thm -> bool -> entry -> Context.generic -> Context.generic
    3.18 +  val del: thm -> entry -> Context.generic -> Context.generic
    3.19 +  val drop: thm -> Context.generic -> Context.generic
    3.20 +  val setup: theory -> theory
    3.21 +end;
    3.22 +
    3.23 +structure Legacy_Transfer : LEGACY_TRANSFER =
    3.24 +struct
    3.25 +
    3.26 +(* data administration *)
    3.27 +
    3.28 +val direction_of = Thm.dest_binop o Thm.dest_arg o cprop_of;
    3.29 +
    3.30 +val transfer_morphism_key = Drule.strip_imp_concl (Thm.cprop_of @{thm transfer_morphismI});
    3.31 +
    3.32 +fun check_morphism_key ctxt key =
    3.33 +  let
    3.34 +    val _ = Thm.match (transfer_morphism_key, Thm.cprop_of key)
    3.35 +      handle Pattern.MATCH => error ("Transfer: expected theorem of the form "
    3.36 +        ^ quote (Syntax.string_of_term ctxt (Thm.term_of transfer_morphism_key)));
    3.37 +  in direction_of key end;
    3.38 +
    3.39 +type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
    3.40 +  hints : string list };
    3.41 +
    3.42 +val empty_entry = { inj = [], embed = [], return = [], cong = [], hints = [] };
    3.43 +fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry,
    3.44 +  { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) =
    3.45 +    { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
    3.46 +      return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
    3.47 +      hints = merge (op =) (hints1, hints2) };
    3.48 +
    3.49 +structure Data = Generic_Data
    3.50 +(
    3.51 +  type T = (thm * entry) list;
    3.52 +  val empty = [];
    3.53 +  val extend = I;
    3.54 +  val merge = AList.join Thm.eq_thm (K merge_entry);
    3.55 +);
    3.56 +
    3.57 +
    3.58 +(* data lookup *)
    3.59 +
    3.60 +fun transfer_rules_of ({ inj, embed, return, cong, ... } : entry) =
    3.61 +  (inj, embed, return, cong);
    3.62 +
    3.63 +fun get_by_direction context (a, D) =
    3.64 +  let
    3.65 +    val ctxt = Context.proof_of context;
    3.66 +    val certify = Thm.cterm_of (Context.theory_of context);
    3.67 +    val a0 = certify a;
    3.68 +    val D0 = certify D;
    3.69 +    fun eq_direction ((a, D), thm') =
    3.70 +      let
    3.71 +        val (a', D') = direction_of thm';
    3.72 +      in a aconvc a' andalso D aconvc D' end;
    3.73 +  in case AList.lookup eq_direction (Data.get context) (a0, D0) of
    3.74 +      SOME e => ((a0, D0), transfer_rules_of e)
    3.75 +    | NONE => error ("Transfer: no such instance: ("
    3.76 +        ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
    3.77 +  end;
    3.78 +
    3.79 +fun get_by_hints context hints =
    3.80 +  let
    3.81 +    val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
    3.82 +      then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context);
    3.83 +    val _ = if null insts then error ("Transfer: no such labels: " ^ commas_quote hints) else ();
    3.84 +  in insts end;
    3.85 +
    3.86 +fun splits P [] = []
    3.87 +  | splits P (xs as (x :: _)) =
    3.88 +      let
    3.89 +        val (pss, qss) = List.partition (P x) xs;
    3.90 +      in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
    3.91 +
    3.92 +fun get_by_prop context t =
    3.93 +  let
    3.94 +    val tys = map snd (Term.add_vars t []);
    3.95 +    val _ = if null tys then error "Transfer: unable to guess instance" else ();
    3.96 +    val tyss = splits (curry Type.could_unify) tys;
    3.97 +    val get_ty = typ_of o ctyp_of_term o fst o direction_of;
    3.98 +    val insts = map_filter (fn tys => get_first (fn (k, e) =>
    3.99 +      if Type.could_unify (hd tys, range_type (get_ty k))
   3.100 +      then SOME (direction_of k, transfer_rules_of e)
   3.101 +      else NONE) (Data.get context)) tyss;
   3.102 +    val _ = if null insts then
   3.103 +      error "Transfer: no instances, provide direction or hints explicitly" else ();
   3.104 +  in insts end;
   3.105 +
   3.106 +
   3.107 +(* applying transfer data *)
   3.108 +
   3.109 +fun transfer_thm ((raw_a, raw_D), (inj, embed, return, cong)) leave ctxt1 thm =
   3.110 +  let
   3.111 +    (* identify morphism function *)
   3.112 +    val ([a, D], ctxt2) = ctxt1
   3.113 +      |> Variable.import true (map Drule.mk_term [raw_a, raw_D])
   3.114 +      |>> map Drule.dest_term o snd;
   3.115 +    val transform = Thm.apply @{cterm "Trueprop"} o Thm.apply D;
   3.116 +    val T = Thm.typ_of (Thm.ctyp_of_term a);
   3.117 +    val (aT, bT) = (Term.range_type T, Term.domain_type T);
   3.118 +    
   3.119 +    (* determine variables to transfer *)
   3.120 +    val ctxt3 = ctxt2
   3.121 +      |> Variable.declare_thm thm
   3.122 +      |> Variable.declare_term (term_of a)
   3.123 +      |> Variable.declare_term (term_of D);
   3.124 +    val certify = Thm.cterm_of (Proof_Context.theory_of ctxt3);
   3.125 +    val vars = filter (fn ((v, _), T) => Type.could_unify (T, aT) andalso
   3.126 +      not (member (op =) leave v)) (Term.add_vars (Thm.prop_of thm) []);
   3.127 +    val c_vars = map (certify o Var) vars;
   3.128 +    val (vs', ctxt4) = Variable.variant_fixes (map (fst o fst) vars) ctxt3;
   3.129 +    val c_vars' = map (certify o (fn v => Free (v, bT))) vs';
   3.130 +    val c_exprs' = map (Thm.apply a) c_vars';
   3.131 +
   3.132 +    (* transfer *)
   3.133 +    val (hyps, ctxt5) = ctxt4
   3.134 +      |> Assumption.add_assumes (map transform c_vars');
   3.135 +    val simpset =
   3.136 +      Simplifier.context ctxt5 HOL_ss addsimps (inj @ embed @ return)
   3.137 +      |> fold Simplifier.add_cong cong;
   3.138 +    val thm' = thm
   3.139 +      |> Drule.cterm_instantiate (c_vars ~~ c_exprs')
   3.140 +      |> fold_rev Thm.implies_intr (map cprop_of hyps)
   3.141 +      |> Simplifier.asm_full_simplify simpset
   3.142 +  in singleton (Variable.export ctxt5 ctxt1) thm' end;
   3.143 +
   3.144 +fun transfer_thm_multiple insts leave ctxt thm =
   3.145 +  map (fn inst => transfer_thm inst leave ctxt thm) insts;
   3.146 +
   3.147 +datatype selection = Direction of term * term | Hints of string list | Prop;
   3.148 +
   3.149 +fun insts_for context thm (Direction direction) = [get_by_direction context direction]
   3.150 +  | insts_for context thm (Hints hints) = get_by_hints context hints
   3.151 +  | insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
   3.152 +
   3.153 +fun transfer context selection leave thm =
   3.154 +  transfer_thm_multiple (insts_for context thm selection) leave (Context.proof_of context) thm;
   3.155 +
   3.156 +
   3.157 +(* maintaining transfer data *)
   3.158 +
   3.159 +fun extend_entry ctxt (a, D) guess
   3.160 +    { inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }
   3.161 +    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   3.162 +  let
   3.163 +    fun add_del eq del add = union eq add #> subtract eq del;
   3.164 +    val guessed = if guess
   3.165 +      then map (fn thm => transfer_thm
   3.166 +        ((a, D), (if null inj1 then inj2 else inj1, [], [], cong1)) [] ctxt thm RS sym) embed1
   3.167 +      else [];
   3.168 +  in
   3.169 +    { inj = union Thm.eq_thm inj1 inj2, embed = union Thm.eq_thm embed1 embed2,
   3.170 +      return = union Thm.eq_thm guessed (union Thm.eq_thm return1 return2),
   3.171 +      cong = union Thm.eq_thm cong1 cong2, hints = union (op =) hints1 hints2 }
   3.172 +  end;
   3.173 +
   3.174 +fun diminish_entry 
   3.175 +    { inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 }
   3.176 +    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   3.177 +  { inj = subtract Thm.eq_thm inj0 inj2, embed = subtract Thm.eq_thm embed0 embed2,
   3.178 +    return = subtract Thm.eq_thm return0 return2, cong = subtract Thm.eq_thm cong0 cong2,
   3.179 +    hints = subtract (op =) hints0 hints2 };
   3.180 +
   3.181 +fun add key guess entry context =
   3.182 +  let
   3.183 +    val ctxt = Context.proof_of context;
   3.184 +    val a_D = check_morphism_key ctxt key;
   3.185 +  in
   3.186 +    context
   3.187 +    |> Data.map (AList.map_default Thm.eq_thm
   3.188 +         (key, empty_entry) (extend_entry ctxt a_D guess entry))
   3.189 +  end;
   3.190 +
   3.191 +fun del key entry = Data.map (AList.map_entry Thm.eq_thm key (diminish_entry entry));
   3.192 +
   3.193 +fun drop key = Data.map (AList.delete Thm.eq_thm key);
   3.194 +
   3.195 +
   3.196 +(* syntax *)
   3.197 +
   3.198 +local
   3.199 +
   3.200 +fun these scan = Scan.optional scan [];
   3.201 +fun these_pair scan = Scan.optional scan ([], []);
   3.202 +
   3.203 +fun keyword k = Scan.lift (Args.$$$ k) >> K ();
   3.204 +fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
   3.205 +
   3.206 +val addN = "add";
   3.207 +val delN = "del";
   3.208 +val keyN = "key";
   3.209 +val modeN = "mode";
   3.210 +val automaticN = "automatic";
   3.211 +val manualN = "manual";
   3.212 +val injN = "inj";
   3.213 +val embedN = "embed";
   3.214 +val returnN = "return";
   3.215 +val congN = "cong";
   3.216 +val labelsN = "labels";
   3.217 +
   3.218 +val leavingN = "leaving";
   3.219 +val directionN = "direction";
   3.220 +
   3.221 +val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon keyN
   3.222 +  || keyword_colon modeN || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
   3.223 +  || keyword_colon congN || keyword_colon labelsN
   3.224 +  || keyword_colon leavingN || keyword_colon directionN;
   3.225 +
   3.226 +val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   3.227 +val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
   3.228 +
   3.229 +val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
   3.230 +  || (Scan.lift (Args.$$$ automaticN) >> K true));
   3.231 +val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
   3.232 +val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
   3.233 +val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
   3.234 +val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
   3.235 +val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
   3.236 +
   3.237 +val entry_pair = these_pair inj -- these_pair embed
   3.238 +  -- these_pair return -- these_pair cong -- these_pair labels
   3.239 +  >> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)),
   3.240 +       (hintsa, hintsd)) =>
   3.241 +      ({ inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa },
   3.242 +        { inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }));
   3.243 +
   3.244 +val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
   3.245 +  || these names >> (fn hints => if null hints then Prop else Hints hints);
   3.246 +
   3.247 +in
   3.248 +
   3.249 +val transfer_attribute = keyword delN >> K (Thm.declaration_attribute drop)
   3.250 +  || keyword addN |-- Scan.optional mode true -- entry_pair
   3.251 +    >> (fn (guess, (entry_add, entry_del)) => Thm.declaration_attribute
   3.252 +      (fn thm => add thm guess entry_add #> del thm entry_del))
   3.253 +  || keyword_colon keyN |-- Attrib.thm
   3.254 +    >> (fn key => Thm.declaration_attribute
   3.255 +      (fn thm => add key false
   3.256 +        { inj = [], embed = [], return = [thm], cong = [], hints = [] }));
   3.257 +
   3.258 +val transferred_attribute = selection -- these (keyword_colon leavingN |-- names)
   3.259 +  >> (fn (selection, leave) => Thm.rule_attribute (fn context =>
   3.260 +      Conjunction.intr_balanced o transfer context selection leave));
   3.261 +
   3.262 +end;
   3.263 +
   3.264 +
   3.265 +(* theory setup *)
   3.266 +
   3.267 +val setup =
   3.268 +  Attrib.setup @{binding transfer} transfer_attribute
   3.269 +    "Installs transfer data" #>
   3.270 +  Attrib.setup @{binding transferred} transferred_attribute
   3.271 +    "Transfers theorems";
   3.272 +
   3.273 +end;
     4.1 --- a/src/HOL/Tools/transfer.ML	Tue Apr 03 20:56:32 2012 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,270 +0,0 @@
     4.4 -(*  Title:      HOL/Tools/transfer.ML
     4.5 -    Author:     Amine Chaieb, University of Cambridge, 2009
     4.6 -                Jeremy Avigad, Carnegie Mellon University
     4.7 -                Florian Haftmann, TU Muenchen
     4.8 -
     4.9 -Simple transfer principle on theorems.
    4.10 -*)
    4.11 -
    4.12 -signature TRANSFER =
    4.13 -sig
    4.14 -  datatype selection = Direction of term * term | Hints of string list | Prop
    4.15 -  val transfer: Context.generic -> selection -> string list -> thm -> thm list
    4.16 -  type entry
    4.17 -  val add: thm -> bool -> entry -> Context.generic -> Context.generic
    4.18 -  val del: thm -> entry -> Context.generic -> Context.generic
    4.19 -  val drop: thm -> Context.generic -> Context.generic
    4.20 -  val setup: theory -> theory
    4.21 -end;
    4.22 -
    4.23 -structure Transfer : TRANSFER =
    4.24 -struct
    4.25 -
    4.26 -(* data administration *)
    4.27 -
    4.28 -val direction_of = Thm.dest_binop o Thm.dest_arg o cprop_of;
    4.29 -
    4.30 -val transfer_morphism_key = Drule.strip_imp_concl (Thm.cprop_of @{thm transfer_morphismI});
    4.31 -
    4.32 -fun check_morphism_key ctxt key =
    4.33 -  let
    4.34 -    val _ = Thm.match (transfer_morphism_key, Thm.cprop_of key)
    4.35 -      handle Pattern.MATCH => error ("Transfer: expected theorem of the form "
    4.36 -        ^ quote (Syntax.string_of_term ctxt (Thm.term_of transfer_morphism_key)));
    4.37 -  in direction_of key end;
    4.38 -
    4.39 -type entry = { inj : thm list, embed : thm list, return : thm list, cong : thm list,
    4.40 -  hints : string list };
    4.41 -
    4.42 -val empty_entry = { inj = [], embed = [], return = [], cong = [], hints = [] };
    4.43 -fun merge_entry ({ inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 } : entry,
    4.44 -  { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } : entry) =
    4.45 -    { inj = merge Thm.eq_thm (inj1, inj2), embed = merge Thm.eq_thm (embed1, embed2),
    4.46 -      return = merge Thm.eq_thm (return1, return2), cong = merge Thm.eq_thm (cong1, cong2),
    4.47 -      hints = merge (op =) (hints1, hints2) };
    4.48 -
    4.49 -structure Data = Generic_Data
    4.50 -(
    4.51 -  type T = (thm * entry) list;
    4.52 -  val empty = [];
    4.53 -  val extend = I;
    4.54 -  val merge = AList.join Thm.eq_thm (K merge_entry);
    4.55 -);
    4.56 -
    4.57 -
    4.58 -(* data lookup *)
    4.59 -
    4.60 -fun transfer_rules_of ({ inj, embed, return, cong, ... } : entry) =
    4.61 -  (inj, embed, return, cong);
    4.62 -
    4.63 -fun get_by_direction context (a, D) =
    4.64 -  let
    4.65 -    val ctxt = Context.proof_of context;
    4.66 -    val certify = Thm.cterm_of (Context.theory_of context);
    4.67 -    val a0 = certify a;
    4.68 -    val D0 = certify D;
    4.69 -    fun eq_direction ((a, D), thm') =
    4.70 -      let
    4.71 -        val (a', D') = direction_of thm';
    4.72 -      in a aconvc a' andalso D aconvc D' end;
    4.73 -  in case AList.lookup eq_direction (Data.get context) (a0, D0) of
    4.74 -      SOME e => ((a0, D0), transfer_rules_of e)
    4.75 -    | NONE => error ("Transfer: no such instance: ("
    4.76 -        ^ Syntax.string_of_term ctxt a ^ ", " ^ Syntax.string_of_term ctxt D ^ ")")
    4.77 -  end;
    4.78 -
    4.79 -fun get_by_hints context hints =
    4.80 -  let
    4.81 -    val insts = map_filter (fn (k, e) => if exists (member (op =) (#hints e)) hints
    4.82 -      then SOME (direction_of k, transfer_rules_of e) else NONE) (Data.get context);
    4.83 -    val _ = if null insts then error ("Transfer: no such labels: " ^ commas_quote hints) else ();
    4.84 -  in insts end;
    4.85 -
    4.86 -fun splits P [] = []
    4.87 -  | splits P (xs as (x :: _)) =
    4.88 -      let
    4.89 -        val (pss, qss) = List.partition (P x) xs;
    4.90 -      in if null pss then [qss] else if null qss then [pss] else pss :: splits P qss end;
    4.91 -
    4.92 -fun get_by_prop context t =
    4.93 -  let
    4.94 -    val tys = map snd (Term.add_vars t []);
    4.95 -    val _ = if null tys then error "Transfer: unable to guess instance" else ();
    4.96 -    val tyss = splits (curry Type.could_unify) tys;
    4.97 -    val get_ty = typ_of o ctyp_of_term o fst o direction_of;
    4.98 -    val insts = map_filter (fn tys => get_first (fn (k, e) =>
    4.99 -      if Type.could_unify (hd tys, range_type (get_ty k))
   4.100 -      then SOME (direction_of k, transfer_rules_of e)
   4.101 -      else NONE) (Data.get context)) tyss;
   4.102 -    val _ = if null insts then
   4.103 -      error "Transfer: no instances, provide direction or hints explicitly" else ();
   4.104 -  in insts end;
   4.105 -
   4.106 -
   4.107 -(* applying transfer data *)
   4.108 -
   4.109 -fun transfer_thm ((raw_a, raw_D), (inj, embed, return, cong)) leave ctxt1 thm =
   4.110 -  let
   4.111 -    (* identify morphism function *)
   4.112 -    val ([a, D], ctxt2) = ctxt1
   4.113 -      |> Variable.import true (map Drule.mk_term [raw_a, raw_D])
   4.114 -      |>> map Drule.dest_term o snd;
   4.115 -    val transform = Thm.apply @{cterm "Trueprop"} o Thm.apply D;
   4.116 -    val T = Thm.typ_of (Thm.ctyp_of_term a);
   4.117 -    val (aT, bT) = (Term.range_type T, Term.domain_type T);
   4.118 -    
   4.119 -    (* determine variables to transfer *)
   4.120 -    val ctxt3 = ctxt2
   4.121 -      |> Variable.declare_thm thm
   4.122 -      |> Variable.declare_term (term_of a)
   4.123 -      |> Variable.declare_term (term_of D);
   4.124 -    val certify = Thm.cterm_of (Proof_Context.theory_of ctxt3);
   4.125 -    val vars = filter (fn ((v, _), T) => Type.could_unify (T, aT) andalso
   4.126 -      not (member (op =) leave v)) (Term.add_vars (Thm.prop_of thm) []);
   4.127 -    val c_vars = map (certify o Var) vars;
   4.128 -    val (vs', ctxt4) = Variable.variant_fixes (map (fst o fst) vars) ctxt3;
   4.129 -    val c_vars' = map (certify o (fn v => Free (v, bT))) vs';
   4.130 -    val c_exprs' = map (Thm.apply a) c_vars';
   4.131 -
   4.132 -    (* transfer *)
   4.133 -    val (hyps, ctxt5) = ctxt4
   4.134 -      |> Assumption.add_assumes (map transform c_vars');
   4.135 -    val simpset =
   4.136 -      Simplifier.context ctxt5 HOL_ss addsimps (inj @ embed @ return)
   4.137 -      |> fold Simplifier.add_cong cong;
   4.138 -    val thm' = thm
   4.139 -      |> Drule.cterm_instantiate (c_vars ~~ c_exprs')
   4.140 -      |> fold_rev Thm.implies_intr (map cprop_of hyps)
   4.141 -      |> Simplifier.asm_full_simplify simpset
   4.142 -  in singleton (Variable.export ctxt5 ctxt1) thm' end;
   4.143 -
   4.144 -fun transfer_thm_multiple insts leave ctxt thm =
   4.145 -  map (fn inst => transfer_thm inst leave ctxt thm) insts;
   4.146 -
   4.147 -datatype selection = Direction of term * term | Hints of string list | Prop;
   4.148 -
   4.149 -fun insts_for context thm (Direction direction) = [get_by_direction context direction]
   4.150 -  | insts_for context thm (Hints hints) = get_by_hints context hints
   4.151 -  | insts_for context thm Prop = get_by_prop context (Thm.prop_of thm);
   4.152 -
   4.153 -fun transfer context selection leave thm =
   4.154 -  transfer_thm_multiple (insts_for context thm selection) leave (Context.proof_of context) thm;
   4.155 -
   4.156 -
   4.157 -(* maintaining transfer data *)
   4.158 -
   4.159 -fun extend_entry ctxt (a, D) guess
   4.160 -    { inj = inj1, embed = embed1, return = return1, cong = cong1, hints = hints1 }
   4.161 -    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   4.162 -  let
   4.163 -    fun add_del eq del add = union eq add #> subtract eq del;
   4.164 -    val guessed = if guess
   4.165 -      then map (fn thm => transfer_thm
   4.166 -        ((a, D), (if null inj1 then inj2 else inj1, [], [], cong1)) [] ctxt thm RS sym) embed1
   4.167 -      else [];
   4.168 -  in
   4.169 -    { inj = union Thm.eq_thm inj1 inj2, embed = union Thm.eq_thm embed1 embed2,
   4.170 -      return = union Thm.eq_thm guessed (union Thm.eq_thm return1 return2),
   4.171 -      cong = union Thm.eq_thm cong1 cong2, hints = union (op =) hints1 hints2 }
   4.172 -  end;
   4.173 -
   4.174 -fun diminish_entry 
   4.175 -    { inj = inj0, embed = embed0, return = return0, cong = cong0, hints = hints0 }
   4.176 -    { inj = inj2, embed = embed2, return = return2, cong = cong2, hints = hints2 } =
   4.177 -  { inj = subtract Thm.eq_thm inj0 inj2, embed = subtract Thm.eq_thm embed0 embed2,
   4.178 -    return = subtract Thm.eq_thm return0 return2, cong = subtract Thm.eq_thm cong0 cong2,
   4.179 -    hints = subtract (op =) hints0 hints2 };
   4.180 -
   4.181 -fun add key guess entry context =
   4.182 -  let
   4.183 -    val ctxt = Context.proof_of context;
   4.184 -    val a_D = check_morphism_key ctxt key;
   4.185 -  in
   4.186 -    context
   4.187 -    |> Data.map (AList.map_default Thm.eq_thm
   4.188 -         (key, empty_entry) (extend_entry ctxt a_D guess entry))
   4.189 -  end;
   4.190 -
   4.191 -fun del key entry = Data.map (AList.map_entry Thm.eq_thm key (diminish_entry entry));
   4.192 -
   4.193 -fun drop key = Data.map (AList.delete Thm.eq_thm key);
   4.194 -
   4.195 -
   4.196 -(* syntax *)
   4.197 -
   4.198 -local
   4.199 -
   4.200 -fun these scan = Scan.optional scan [];
   4.201 -fun these_pair scan = Scan.optional scan ([], []);
   4.202 -
   4.203 -fun keyword k = Scan.lift (Args.$$$ k) >> K ();
   4.204 -fun keyword_colon k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
   4.205 -
   4.206 -val addN = "add";
   4.207 -val delN = "del";
   4.208 -val keyN = "key";
   4.209 -val modeN = "mode";
   4.210 -val automaticN = "automatic";
   4.211 -val manualN = "manual";
   4.212 -val injN = "inj";
   4.213 -val embedN = "embed";
   4.214 -val returnN = "return";
   4.215 -val congN = "cong";
   4.216 -val labelsN = "labels";
   4.217 -
   4.218 -val leavingN = "leaving";
   4.219 -val directionN = "direction";
   4.220 -
   4.221 -val any_keyword = keyword_colon addN || keyword_colon delN || keyword_colon keyN
   4.222 -  || keyword_colon modeN || keyword_colon injN || keyword_colon embedN || keyword_colon returnN
   4.223 -  || keyword_colon congN || keyword_colon labelsN
   4.224 -  || keyword_colon leavingN || keyword_colon directionN;
   4.225 -
   4.226 -val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   4.227 -val names = Scan.repeat (Scan.unless any_keyword (Scan.lift Args.name))
   4.228 -
   4.229 -val mode = keyword_colon modeN |-- ((Scan.lift (Args.$$$ manualN) >> K false)
   4.230 -  || (Scan.lift (Args.$$$ automaticN) >> K true));
   4.231 -val inj = (keyword_colon injN |-- thms) -- these (keyword_colon delN |-- thms);
   4.232 -val embed = (keyword_colon embedN |-- thms) -- these (keyword_colon delN |-- thms);
   4.233 -val return = (keyword_colon returnN |-- thms) -- these (keyword_colon delN |-- thms);
   4.234 -val cong = (keyword_colon congN |-- thms) -- these (keyword_colon delN |-- thms);
   4.235 -val labels = (keyword_colon labelsN |-- names) -- these (keyword_colon delN |-- names);
   4.236 -
   4.237 -val entry_pair = these_pair inj -- these_pair embed
   4.238 -  -- these_pair return -- these_pair cong -- these_pair labels
   4.239 -  >> (fn (((((inja, injd), (embeda, embedd)), (returna, returnd)), (conga, congd)),
   4.240 -       (hintsa, hintsd)) =>
   4.241 -      ({ inj = inja, embed = embeda, return = returna, cong = conga, hints = hintsa },
   4.242 -        { inj = injd, embed = embedd, return = returnd, cong = congd, hints = hintsd }));
   4.243 -
   4.244 -val selection = (keyword_colon directionN |-- (Args.term -- Args.term) >> Direction)
   4.245 -  || these names >> (fn hints => if null hints then Prop else Hints hints);
   4.246 -
   4.247 -in
   4.248 -
   4.249 -val transfer_attribute = keyword delN >> K (Thm.declaration_attribute drop)
   4.250 -  || keyword addN |-- Scan.optional mode true -- entry_pair
   4.251 -    >> (fn (guess, (entry_add, entry_del)) => Thm.declaration_attribute
   4.252 -      (fn thm => add thm guess entry_add #> del thm entry_del))
   4.253 -  || keyword_colon keyN |-- Attrib.thm
   4.254 -    >> (fn key => Thm.declaration_attribute
   4.255 -      (fn thm => add key false
   4.256 -        { inj = [], embed = [], return = [thm], cong = [], hints = [] }));
   4.257 -
   4.258 -val transferred_attribute = selection -- these (keyword_colon leavingN |-- names)
   4.259 -  >> (fn (selection, leave) => Thm.rule_attribute (fn context =>
   4.260 -      Conjunction.intr_balanced o transfer context selection leave));
   4.261 -
   4.262 -end;
   4.263 -
   4.264 -
   4.265 -(* theory setup *)
   4.266 -
   4.267 -val setup =
   4.268 -  Attrib.setup @{binding transfer} transfer_attribute
   4.269 -    "Installs transfer data" #>
   4.270 -  Attrib.setup @{binding transferred} transferred_attribute
   4.271 -    "Transfers theorems";
   4.272 -
   4.273 -end;