src/HOL/Transfer.thy
author traytel
Fri Feb 28 17:54:52 2014 +0100 (2014-02-28)
changeset 55811 aa1acc25126b
parent 55760 aaaccc8e015f
child 55945 e96383acecf9
permissions -rw-r--r--
load Metis a little later
     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 Basic_BNFs Metis
    10 begin
    11 
    12 subsection {* Relator for function space *}
    13 
    14 locale lifting_syntax
    15 begin
    16   notation fun_rel (infixr "===>" 55)
    17   notation map_fun (infixr "--->" 55)
    18 end
    19 
    20 context
    21 begin
    22 interpretation lifting_syntax .
    23 
    24 lemma fun_relD2:
    25   assumes "fun_rel A B f g" and "A x x"
    26   shows "B (f x) (g x)"
    27   using assms by (rule fun_relD)
    28 
    29 lemma fun_relE:
    30   assumes "fun_rel A B f g" and "A x y"
    31   obtains "B (f x) (g y)"
    32   using assms by (simp add: fun_rel_def)
    33 
    34 lemmas fun_rel_eq = fun.rel_eq
    35 
    36 lemma fun_rel_eq_rel:
    37 shows "fun_rel (op =) R = (\<lambda>f g. \<forall>x. R (f x) (g x))"
    38   by (simp add: fun_rel_def)
    39 
    40 
    41 subsection {* Transfer method *}
    42 
    43 text {* Explicit tag for relation membership allows for
    44   backward proof methods. *}
    45 
    46 definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
    47   where "Rel r \<equiv> r"
    48 
    49 text {* Handling of equality relations *}
    50 
    51 definition is_equality :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
    52   where "is_equality R \<longleftrightarrow> R = (op =)"
    53 
    54 lemma is_equality_eq: "is_equality (op =)"
    55   unfolding is_equality_def by simp
    56 
    57 text {* Reverse implication for monotonicity rules *}
    58 
    59 definition rev_implies where
    60   "rev_implies x y \<longleftrightarrow> (y \<longrightarrow> x)"
    61 
    62 text {* Handling of meta-logic connectives *}
    63 
    64 definition transfer_forall where
    65   "transfer_forall \<equiv> All"
    66 
    67 definition transfer_implies where
    68   "transfer_implies \<equiv> op \<longrightarrow>"
    69 
    70 definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
    71   where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
    72 
    73 lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
    74   unfolding atomize_all transfer_forall_def ..
    75 
    76 lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
    77   unfolding atomize_imp transfer_implies_def ..
    78 
    79 lemma transfer_bforall_unfold:
    80   "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
    81   unfolding transfer_bforall_def atomize_imp atomize_all ..
    82 
    83 lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
    84   unfolding Rel_def by simp
    85 
    86 lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
    87   unfolding Rel_def by simp
    88 
    89 lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
    90   by simp
    91 
    92 lemma untransfer_start: "\<lbrakk>Q; Rel (op =) P Q\<rbrakk> \<Longrightarrow> P"
    93   unfolding Rel_def by simp
    94 
    95 lemma Rel_eq_refl: "Rel (op =) x x"
    96   unfolding Rel_def ..
    97 
    98 lemma Rel_app:
    99   assumes "Rel (A ===> B) f g" and "Rel A x y"
   100   shows "Rel B (f x) (g y)"
   101   using assms unfolding Rel_def fun_rel_def by fast
   102 
   103 lemma Rel_abs:
   104   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
   105   shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
   106   using assms unfolding Rel_def fun_rel_def by fast
   107 
   108 end
   109 
   110 ML_file "Tools/transfer.ML"
   111 setup Transfer.setup
   112 
   113 declare refl [transfer_rule]
   114 
   115 declare fun_rel_eq [relator_eq]
   116 
   117 hide_const (open) Rel
   118 
   119 context
   120 begin
   121 interpretation lifting_syntax .
   122 
   123 text {* Handling of domains *}
   124 
   125 lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
   126   by auto
   127 
   128 lemma Domaimp_refl[transfer_domain_rule]:
   129   "Domainp T = Domainp T" ..
   130 
   131 lemma Domainp_prod_fun_eq[transfer_domain_rule]:
   132   assumes "Domainp T = P"
   133   shows "Domainp (op= ===> T) = (\<lambda>f. \<forall>x. P (f x))"
   134 by (auto intro: choice simp: assms[symmetric] Domainp_iff fun_rel_def fun_eq_iff)
   135 
   136 subsection {* Predicates on relations, i.e. ``class constraints'' *}
   137 
   138 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   139   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
   140 
   141 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   142   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
   143 
   144 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   145   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
   146 
   147 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   148   where "bi_unique R \<longleftrightarrow>
   149     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
   150     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
   151 
   152 lemma bi_uniqueDr: "\<lbrakk> bi_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
   153 by(simp add: bi_unique_def)
   154 
   155 lemma bi_uniqueDl: "\<lbrakk> bi_unique A; A x y; A z y \<rbrakk> \<Longrightarrow> x = z"
   156 by(simp add: bi_unique_def)
   157 
   158 lemma right_uniqueI: "(\<And>x y z. \<lbrakk> A x y; A x z \<rbrakk> \<Longrightarrow> y = z) \<Longrightarrow> right_unique A"
   159 unfolding right_unique_def by blast
   160 
   161 lemma right_uniqueD: "\<lbrakk> right_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
   162 unfolding right_unique_def by blast
   163 
   164 lemma right_total_alt_def:
   165   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
   166   unfolding right_total_def fun_rel_def
   167   apply (rule iffI, fast)
   168   apply (rule allI)
   169   apply (drule_tac x="\<lambda>x. True" in spec)
   170   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   171   apply fast
   172   done
   173 
   174 lemma right_unique_alt_def:
   175   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
   176   unfolding right_unique_def fun_rel_def by auto
   177 
   178 lemma bi_total_alt_def:
   179   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
   180   unfolding bi_total_def fun_rel_def
   181   apply (rule iffI, fast)
   182   apply safe
   183   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
   184   apply (drule_tac x="\<lambda>y. True" in spec)
   185   apply fast
   186   apply (drule_tac x="\<lambda>x. True" in spec)
   187   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   188   apply fast
   189   done
   190 
   191 lemma bi_unique_alt_def:
   192   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
   193   unfolding bi_unique_def fun_rel_def by auto
   194 
   195 lemma bi_unique_conversep [simp]: "bi_unique R\<inverse>\<inverse> = bi_unique R"
   196 by(auto simp add: bi_unique_def)
   197 
   198 lemma bi_total_conversep [simp]: "bi_total R\<inverse>\<inverse> = bi_total R"
   199 by(auto simp add: bi_total_def)
   200 
   201 text {* Properties are preserved by relation composition. *}
   202 
   203 lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
   204   by auto
   205 
   206 lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
   207   unfolding bi_total_def OO_def by metis
   208 
   209 lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
   210   unfolding bi_unique_def OO_def by metis
   211 
   212 lemma right_total_OO:
   213   "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
   214   unfolding right_total_def OO_def by metis
   215 
   216 lemma right_unique_OO:
   217   "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
   218   unfolding right_unique_def OO_def by metis
   219 
   220 
   221 subsection {* Properties of relators *}
   222 
   223 lemma right_total_eq [transfer_rule]: "right_total (op =)"
   224   unfolding right_total_def by simp
   225 
   226 lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
   227   unfolding right_unique_def by simp
   228 
   229 lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
   230   unfolding bi_total_def by simp
   231 
   232 lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
   233   unfolding bi_unique_def by simp
   234 
   235 lemma right_total_fun [transfer_rule]:
   236   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
   237   unfolding right_total_def fun_rel_def
   238   apply (rule allI, rename_tac g)
   239   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   240   apply clarify
   241   apply (subgoal_tac "(THE y. A x y) = y", simp)
   242   apply (rule someI_ex)
   243   apply (simp)
   244   apply (rule the_equality)
   245   apply assumption
   246   apply (simp add: right_unique_def)
   247   done
   248 
   249 lemma right_unique_fun [transfer_rule]:
   250   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
   251   unfolding right_total_def right_unique_def fun_rel_def
   252   by (clarify, rule ext, fast)
   253 
   254 lemma bi_total_fun [transfer_rule]:
   255   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
   256   unfolding bi_total_def fun_rel_def
   257   apply safe
   258   apply (rename_tac f)
   259   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
   260   apply clarify
   261   apply (subgoal_tac "(THE x. A x y) = x", simp)
   262   apply (rule someI_ex)
   263   apply (simp)
   264   apply (rule the_equality)
   265   apply assumption
   266   apply (simp add: bi_unique_def)
   267   apply (rename_tac g)
   268   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   269   apply clarify
   270   apply (subgoal_tac "(THE y. A x y) = y", simp)
   271   apply (rule someI_ex)
   272   apply (simp)
   273   apply (rule the_equality)
   274   apply assumption
   275   apply (simp add: bi_unique_def)
   276   done
   277 
   278 lemma bi_unique_fun [transfer_rule]:
   279   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
   280   unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
   281   by (safe, metis, fast)
   282 
   283 
   284 subsection {* Transfer rules *}
   285 
   286 lemma Domainp_forall_transfer [transfer_rule]:
   287   assumes "right_total A"
   288   shows "((A ===> op =) ===> op =)
   289     (transfer_bforall (Domainp A)) transfer_forall"
   290   using assms unfolding right_total_def
   291   unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
   292   by metis
   293 
   294 text {* Transfer rules using implication instead of equality on booleans. *}
   295 
   296 lemma transfer_forall_transfer [transfer_rule]:
   297   "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
   298   "right_total A \<Longrightarrow> ((A ===> op =) ===> implies) transfer_forall transfer_forall"
   299   "right_total A \<Longrightarrow> ((A ===> implies) ===> implies) transfer_forall transfer_forall"
   300   "bi_total A \<Longrightarrow> ((A ===> op =) ===> rev_implies) transfer_forall transfer_forall"
   301   "bi_total A \<Longrightarrow> ((A ===> rev_implies) ===> rev_implies) transfer_forall transfer_forall"
   302   unfolding transfer_forall_def rev_implies_def fun_rel_def right_total_def bi_total_def
   303   by metis+
   304 
   305 lemma transfer_implies_transfer [transfer_rule]:
   306   "(op =        ===> op =        ===> op =       ) transfer_implies transfer_implies"
   307   "(rev_implies ===> implies     ===> implies    ) transfer_implies transfer_implies"
   308   "(rev_implies ===> op =        ===> implies    ) transfer_implies transfer_implies"
   309   "(op =        ===> implies     ===> implies    ) transfer_implies transfer_implies"
   310   "(op =        ===> op =        ===> implies    ) transfer_implies transfer_implies"
   311   "(implies     ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
   312   "(implies     ===> op =        ===> rev_implies) transfer_implies transfer_implies"
   313   "(op =        ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
   314   "(op =        ===> op =        ===> rev_implies) transfer_implies transfer_implies"
   315   unfolding transfer_implies_def rev_implies_def fun_rel_def by auto
   316 
   317 lemma eq_imp_transfer [transfer_rule]:
   318   "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
   319   unfolding right_unique_alt_def .
   320 
   321 lemma eq_transfer [transfer_rule]:
   322   assumes "bi_unique A"
   323   shows "(A ===> A ===> op =) (op =) (op =)"
   324   using assms unfolding bi_unique_def fun_rel_def by auto
   325 
   326 lemma right_total_Ex_transfer[transfer_rule]:
   327   assumes "right_total A"
   328   shows "((A ===> op=) ===> op=) (Bex (Collect (Domainp A))) Ex"
   329 using assms unfolding right_total_def Bex_def fun_rel_def Domainp_iff[abs_def]
   330 by blast
   331 
   332 lemma right_total_All_transfer[transfer_rule]:
   333   assumes "right_total A"
   334   shows "((A ===> op =) ===> op =) (Ball (Collect (Domainp A))) All"
   335 using assms unfolding right_total_def Ball_def fun_rel_def Domainp_iff[abs_def]
   336 by blast
   337 
   338 lemma All_transfer [transfer_rule]:
   339   assumes "bi_total A"
   340   shows "((A ===> op =) ===> op =) All All"
   341   using assms unfolding bi_total_def fun_rel_def by fast
   342 
   343 lemma Ex_transfer [transfer_rule]:
   344   assumes "bi_total A"
   345   shows "((A ===> op =) ===> op =) Ex Ex"
   346   using assms unfolding bi_total_def fun_rel_def by fast
   347 
   348 lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
   349   unfolding fun_rel_def by simp
   350 
   351 lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
   352   unfolding fun_rel_def by simp
   353 
   354 lemma id_transfer [transfer_rule]: "(A ===> A) id id"
   355   unfolding fun_rel_def by simp
   356 
   357 lemma comp_transfer [transfer_rule]:
   358   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
   359   unfolding fun_rel_def by simp
   360 
   361 lemma fun_upd_transfer [transfer_rule]:
   362   assumes [transfer_rule]: "bi_unique A"
   363   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
   364   unfolding fun_upd_def [abs_def] by transfer_prover
   365 
   366 lemma case_nat_transfer [transfer_rule]:
   367   "(A ===> (op = ===> A) ===> op = ===> A) case_nat case_nat"
   368   unfolding fun_rel_def by (simp split: nat.split)
   369 
   370 lemma rec_nat_transfer [transfer_rule]:
   371   "(A ===> (op = ===> A ===> A) ===> op = ===> A) rec_nat rec_nat"
   372   unfolding fun_rel_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
   373 
   374 lemma funpow_transfer [transfer_rule]:
   375   "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
   376   unfolding funpow_def by transfer_prover
   377 
   378 lemma mono_transfer[transfer_rule]:
   379   assumes [transfer_rule]: "bi_total A"
   380   assumes [transfer_rule]: "(A ===> A ===> op=) op\<le> op\<le>"
   381   assumes [transfer_rule]: "(B ===> B ===> op=) op\<le> op\<le>"
   382   shows "((A ===> B) ===> op=) mono mono"
   383 unfolding mono_def[abs_def] by transfer_prover
   384 
   385 lemma right_total_relcompp_transfer[transfer_rule]: 
   386   assumes [transfer_rule]: "right_total B"
   387   shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=) 
   388     (\<lambda>R S x z. \<exists>y\<in>Collect (Domainp B). R x y \<and> S y z) op OO"
   389 unfolding OO_def[abs_def] by transfer_prover
   390 
   391 lemma relcompp_transfer[transfer_rule]: 
   392   assumes [transfer_rule]: "bi_total B"
   393   shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=) op OO op OO"
   394 unfolding OO_def[abs_def] by transfer_prover
   395 
   396 lemma right_total_Domainp_transfer[transfer_rule]:
   397   assumes [transfer_rule]: "right_total B"
   398   shows "((A ===> B ===> op=) ===> A ===> op=) (\<lambda>T x. \<exists>y\<in>Collect(Domainp B). T x y) Domainp"
   399 apply(subst(2) Domainp_iff[abs_def]) by transfer_prover
   400 
   401 lemma Domainp_transfer[transfer_rule]:
   402   assumes [transfer_rule]: "bi_total B"
   403   shows "((A ===> B ===> op=) ===> A ===> op=) Domainp Domainp"
   404 unfolding Domainp_iff[abs_def] by transfer_prover
   405 
   406 lemma reflp_transfer[transfer_rule]: 
   407   "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> op=) reflp reflp"
   408   "right_total A \<Longrightarrow> ((A ===> A ===> implies) ===> implies) reflp reflp"
   409   "right_total A \<Longrightarrow> ((A ===> A ===> op=) ===> implies) reflp reflp"
   410   "bi_total A \<Longrightarrow> ((A ===> A ===> rev_implies) ===> rev_implies) reflp reflp"
   411   "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> rev_implies) reflp reflp"
   412 using assms unfolding reflp_def[abs_def] rev_implies_def bi_total_def right_total_def fun_rel_def 
   413 by fast+
   414 
   415 lemma right_unique_transfer [transfer_rule]:
   416   assumes [transfer_rule]: "right_total A"
   417   assumes [transfer_rule]: "right_total B"
   418   assumes [transfer_rule]: "bi_unique B"
   419   shows "((A ===> B ===> op=) ===> implies) right_unique right_unique"
   420 using assms unfolding right_unique_def[abs_def] right_total_def bi_unique_def fun_rel_def
   421 by metis
   422 
   423 end
   424 
   425 end