src/HOL/Transfer.thy
author Christian Sternagel
Thu Aug 30 15:44:03 2012 +0900 (2012-08-30)
changeset 49093 fdc301f592c4
parent 48891 c0eafbd55de3
child 49975 faf4afed009f
permissions -rw-r--r--
forgot to add lemmas
     1 (*  Title:      HOL/Transfer.thy
     2     Author:     Brian Huffman, TU Muenchen
     3 *)
     4 
     5 header {* Generic theorem transfer using relations *}
     6 
     7 theory Transfer
     8 imports Plain Hilbert_Choice
     9 begin
    10 
    11 subsection {* Relator for function space *}
    12 
    13 definition
    14   fun_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> bool" (infixr "===>" 55)
    15 where
    16   "fun_rel A B = (\<lambda>f g. \<forall>x y. A x y \<longrightarrow> B (f x) (g y))"
    17 
    18 lemma fun_relI [intro]:
    19   assumes "\<And>x y. A x y \<Longrightarrow> B (f x) (g y)"
    20   shows "(A ===> B) f g"
    21   using assms by (simp add: fun_rel_def)
    22 
    23 lemma fun_relD:
    24   assumes "(A ===> B) f g" and "A x y"
    25   shows "B (f x) (g y)"
    26   using assms by (simp add: fun_rel_def)
    27 
    28 lemma fun_relD2:
    29   assumes "(A ===> B) f g" and "A x x"
    30   shows "B (f x) (g x)"
    31   using assms unfolding fun_rel_def by auto
    32 
    33 lemma fun_relE:
    34   assumes "(A ===> B) f g" and "A x y"
    35   obtains "B (f x) (g y)"
    36   using assms by (simp add: fun_rel_def)
    37 
    38 lemma fun_rel_eq:
    39   shows "((op =) ===> (op =)) = (op =)"
    40   by (auto simp add: fun_eq_iff elim: fun_relE)
    41 
    42 lemma fun_rel_eq_rel:
    43   shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
    44   by (simp add: fun_rel_def)
    45 
    46 
    47 subsection {* Transfer method *}
    48 
    49 text {* Explicit tag for relation membership allows for
    50   backward proof methods. *}
    51 
    52 definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
    53   where "Rel r \<equiv> r"
    54 
    55 text {* Handling of meta-logic connectives *}
    56 
    57 definition transfer_forall where
    58   "transfer_forall \<equiv> All"
    59 
    60 definition transfer_implies where
    61   "transfer_implies \<equiv> op \<longrightarrow>"
    62 
    63 definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
    64   where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
    65 
    66 lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
    67   unfolding atomize_all transfer_forall_def ..
    68 
    69 lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
    70   unfolding atomize_imp transfer_implies_def ..
    71 
    72 lemma transfer_bforall_unfold:
    73   "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
    74   unfolding transfer_bforall_def atomize_imp atomize_all ..
    75 
    76 lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
    77   unfolding Rel_def by simp
    78 
    79 lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
    80   unfolding Rel_def by simp
    81 
    82 lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
    83   by simp
    84 
    85 lemma Rel_eq_refl: "Rel (op =) x x"
    86   unfolding Rel_def ..
    87 
    88 lemma Rel_app:
    89   assumes "Rel (A ===> B) f g" and "Rel A x y"
    90   shows "Rel B (f x) (g y)"
    91   using assms unfolding Rel_def fun_rel_def by fast
    92 
    93 lemma Rel_abs:
    94   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
    95   shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
    96   using assms unfolding Rel_def fun_rel_def by fast
    97 
    98 ML_file "Tools/transfer.ML"
    99 setup Transfer.setup
   100 
   101 declare fun_rel_eq [relator_eq]
   102 
   103 hide_const (open) Rel
   104 
   105 
   106 subsection {* Predicates on relations, i.e. ``class constraints'' *}
   107 
   108 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   109   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
   110 
   111 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   112   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
   113 
   114 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   115   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
   116 
   117 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   118   where "bi_unique R \<longleftrightarrow>
   119     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
   120     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
   121 
   122 lemma right_total_alt_def:
   123   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
   124   unfolding right_total_def fun_rel_def
   125   apply (rule iffI, fast)
   126   apply (rule allI)
   127   apply (drule_tac x="\<lambda>x. True" in spec)
   128   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   129   apply fast
   130   done
   131 
   132 lemma right_unique_alt_def:
   133   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
   134   unfolding right_unique_def fun_rel_def by auto
   135 
   136 lemma bi_total_alt_def:
   137   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
   138   unfolding bi_total_def fun_rel_def
   139   apply (rule iffI, fast)
   140   apply safe
   141   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
   142   apply (drule_tac x="\<lambda>y. True" in spec)
   143   apply fast
   144   apply (drule_tac x="\<lambda>x. True" in spec)
   145   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   146   apply fast
   147   done
   148 
   149 lemma bi_unique_alt_def:
   150   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
   151   unfolding bi_unique_def fun_rel_def by auto
   152 
   153 text {* Properties are preserved by relation composition. *}
   154 
   155 lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
   156   by auto
   157 
   158 lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
   159   unfolding bi_total_def OO_def by metis
   160 
   161 lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
   162   unfolding bi_unique_def OO_def by metis
   163 
   164 lemma right_total_OO:
   165   "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
   166   unfolding right_total_def OO_def by metis
   167 
   168 lemma right_unique_OO:
   169   "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
   170   unfolding right_unique_def OO_def by metis
   171 
   172 
   173 subsection {* Properties of relators *}
   174 
   175 lemma right_total_eq [transfer_rule]: "right_total (op =)"
   176   unfolding right_total_def by simp
   177 
   178 lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
   179   unfolding right_unique_def by simp
   180 
   181 lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
   182   unfolding bi_total_def by simp
   183 
   184 lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
   185   unfolding bi_unique_def by simp
   186 
   187 lemma right_total_fun [transfer_rule]:
   188   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
   189   unfolding right_total_def fun_rel_def
   190   apply (rule allI, rename_tac g)
   191   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   192   apply clarify
   193   apply (subgoal_tac "(THE y. A x y) = y", simp)
   194   apply (rule someI_ex)
   195   apply (simp)
   196   apply (rule the_equality)
   197   apply assumption
   198   apply (simp add: right_unique_def)
   199   done
   200 
   201 lemma right_unique_fun [transfer_rule]:
   202   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
   203   unfolding right_total_def right_unique_def fun_rel_def
   204   by (clarify, rule ext, fast)
   205 
   206 lemma bi_total_fun [transfer_rule]:
   207   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
   208   unfolding bi_total_def fun_rel_def
   209   apply safe
   210   apply (rename_tac f)
   211   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
   212   apply clarify
   213   apply (subgoal_tac "(THE x. A x y) = x", simp)
   214   apply (rule someI_ex)
   215   apply (simp)
   216   apply (rule the_equality)
   217   apply assumption
   218   apply (simp add: bi_unique_def)
   219   apply (rename_tac g)
   220   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   221   apply clarify
   222   apply (subgoal_tac "(THE y. A x y) = y", simp)
   223   apply (rule someI_ex)
   224   apply (simp)
   225   apply (rule the_equality)
   226   apply assumption
   227   apply (simp add: bi_unique_def)
   228   done
   229 
   230 lemma bi_unique_fun [transfer_rule]:
   231   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
   232   unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
   233   by (safe, metis, fast)
   234 
   235 
   236 subsection {* Transfer rules *}
   237 
   238 text {* Transfer rules using implication instead of equality on booleans. *}
   239 
   240 lemma eq_imp_transfer [transfer_rule]:
   241   "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
   242   unfolding right_unique_alt_def .
   243 
   244 lemma forall_imp_transfer [transfer_rule]:
   245   "right_total A \<Longrightarrow> ((A ===> op \<longrightarrow>) ===> op \<longrightarrow>) transfer_forall transfer_forall"
   246   unfolding right_total_alt_def transfer_forall_def .
   247 
   248 lemma eq_transfer [transfer_rule]:
   249   assumes "bi_unique A"
   250   shows "(A ===> A ===> op =) (op =) (op =)"
   251   using assms unfolding bi_unique_def fun_rel_def by auto
   252 
   253 lemma All_transfer [transfer_rule]:
   254   assumes "bi_total A"
   255   shows "((A ===> op =) ===> op =) All All"
   256   using assms unfolding bi_total_def fun_rel_def by fast
   257 
   258 lemma Ex_transfer [transfer_rule]:
   259   assumes "bi_total A"
   260   shows "((A ===> op =) ===> op =) Ex Ex"
   261   using assms unfolding bi_total_def fun_rel_def by fast
   262 
   263 lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
   264   unfolding fun_rel_def by simp
   265 
   266 lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
   267   unfolding fun_rel_def by simp
   268 
   269 lemma id_transfer [transfer_rule]: "(A ===> A) id id"
   270   unfolding fun_rel_def by simp
   271 
   272 lemma comp_transfer [transfer_rule]:
   273   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
   274   unfolding fun_rel_def by simp
   275 
   276 lemma fun_upd_transfer [transfer_rule]:
   277   assumes [transfer_rule]: "bi_unique A"
   278   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
   279   unfolding fun_upd_def [abs_def] by transfer_prover
   280 
   281 lemma nat_case_transfer [transfer_rule]:
   282   "(A ===> (op = ===> A) ===> op = ===> A) nat_case nat_case"
   283   unfolding fun_rel_def by (simp split: nat.split)
   284 
   285 lemma nat_rec_transfer [transfer_rule]:
   286   "(A ===> (op = ===> A ===> A) ===> op = ===> A) nat_rec nat_rec"
   287   unfolding fun_rel_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
   288 
   289 lemma funpow_transfer [transfer_rule]:
   290   "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
   291   unfolding funpow_def by transfer_prover
   292 
   293 text {* Fallback rule for transferring universal quantifiers over
   294   correspondence relations that are not bi-total, and do not have
   295   custom transfer rules (e.g. relations between function types). *}
   296 
   297 lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
   298   by auto
   299 
   300 lemma Domainp_forall_transfer [transfer_rule]:
   301   assumes "right_total A"
   302   shows "((A ===> op =) ===> op =)
   303     (transfer_bforall (Domainp A)) transfer_forall"
   304   using assms unfolding right_total_def
   305   unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
   306   by metis
   307 
   308 text {* Preferred rule for transferring universal quantifiers over
   309   bi-total correspondence relations (later rules are tried first). *}
   310 
   311 lemma forall_transfer [transfer_rule]:
   312   "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
   313   unfolding transfer_forall_def by (rule All_transfer)
   314 
   315 end