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