src/HOL/Transfer.thy
author huffman
Fri Apr 20 22:49:40 2012 +0200 (2012-04-20)
changeset 47635 ebb79474262c
parent 47627 2b1d3eda59eb
child 47636 b786388b4b3a
permissions -rw-r--r--
rename 'correspondence' method to 'transfer_prover'
     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 uses ("Tools/transfer.ML")
    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_relE:
    30   assumes "(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 lemma fun_rel_eq:
    35   shows "((op =) ===> (op =)) = (op =)"
    36   by (auto simp add: fun_eq_iff elim: fun_relE)
    37 
    38 lemma fun_rel_eq_rel:
    39   shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
    40   by (simp add: fun_rel_def)
    41 
    42 
    43 subsection {* Transfer method *}
    44 
    45 text {* Explicit tags for application, abstraction, and relation
    46 membership allow for backward proof methods. *}
    47 
    48 definition App :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
    49   where "App f \<equiv> f"
    50 
    51 definition Abs :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
    52   where "Abs f \<equiv> f"
    53 
    54 definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
    55   where "Rel r \<equiv> r"
    56 
    57 text {* Handling of meta-logic connectives *}
    58 
    59 definition transfer_forall where
    60   "transfer_forall \<equiv> All"
    61 
    62 definition transfer_implies where
    63   "transfer_implies \<equiv> op \<longrightarrow>"
    64 
    65 definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
    66   where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
    67 
    68 lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
    69   unfolding atomize_all transfer_forall_def ..
    70 
    71 lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
    72   unfolding atomize_imp transfer_implies_def ..
    73 
    74 lemma transfer_bforall_unfold:
    75   "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
    76   unfolding transfer_bforall_def atomize_imp atomize_all ..
    77 
    78 lemma transfer_start: "\<lbrakk>Rel (op =) P Q; P\<rbrakk> \<Longrightarrow> Q"
    79   unfolding Rel_def by simp
    80 
    81 lemma transfer_start': "\<lbrakk>Rel (op \<longrightarrow>) P Q; P\<rbrakk> \<Longrightarrow> Q"
    82   unfolding Rel_def by simp
    83 
    84 lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
    85   by simp
    86 
    87 lemma Rel_eq_refl: "Rel (op =) x x"
    88   unfolding Rel_def ..
    89 
    90 lemma Rel_App:
    91   assumes "Rel (A ===> B) f g" and "Rel A x y"
    92   shows "Rel B (App f x) (App g y)"
    93   using assms unfolding Rel_def App_def fun_rel_def by fast
    94 
    95 lemma Rel_Abs:
    96   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
    97   shows "Rel (A ===> B) (Abs (\<lambda>x. f x)) (Abs (\<lambda>y. g y))"
    98   using assms unfolding Rel_def Abs_def fun_rel_def by fast
    99 
   100 use "Tools/transfer.ML"
   101 
   102 setup Transfer.setup
   103 
   104 declare fun_rel_eq [relator_eq]
   105 
   106 hide_const (open) App Abs Rel
   107 
   108 
   109 subsection {* Predicates on relations, i.e. ``class constraints'' *}
   110 
   111 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   112   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
   113 
   114 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   115   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
   116 
   117 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   118   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
   119 
   120 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   121   where "bi_unique R \<longleftrightarrow>
   122     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
   123     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
   124 
   125 lemma right_total_alt_def:
   126   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
   127   unfolding right_total_def fun_rel_def
   128   apply (rule iffI, fast)
   129   apply (rule allI)
   130   apply (drule_tac x="\<lambda>x. True" in spec)
   131   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   132   apply fast
   133   done
   134 
   135 lemma right_unique_alt_def:
   136   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
   137   unfolding right_unique_def fun_rel_def by auto
   138 
   139 lemma bi_total_alt_def:
   140   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
   141   unfolding bi_total_def fun_rel_def
   142   apply (rule iffI, fast)
   143   apply safe
   144   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
   145   apply (drule_tac x="\<lambda>y. True" in spec)
   146   apply fast
   147   apply (drule_tac x="\<lambda>x. True" in spec)
   148   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   149   apply fast
   150   done
   151 
   152 lemma bi_unique_alt_def:
   153   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
   154   unfolding bi_unique_def fun_rel_def by auto
   155 
   156 
   157 subsection {* Properties of relators *}
   158 
   159 lemma right_total_eq [transfer_rule]: "right_total (op =)"
   160   unfolding right_total_def by simp
   161 
   162 lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
   163   unfolding right_unique_def by simp
   164 
   165 lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
   166   unfolding bi_total_def by simp
   167 
   168 lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
   169   unfolding bi_unique_def by simp
   170 
   171 lemma right_total_fun [transfer_rule]:
   172   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
   173   unfolding right_total_def fun_rel_def
   174   apply (rule allI, rename_tac g)
   175   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   176   apply clarify
   177   apply (subgoal_tac "(THE y. A x y) = y", simp)
   178   apply (rule someI_ex)
   179   apply (simp)
   180   apply (rule the_equality)
   181   apply assumption
   182   apply (simp add: right_unique_def)
   183   done
   184 
   185 lemma right_unique_fun [transfer_rule]:
   186   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
   187   unfolding right_total_def right_unique_def fun_rel_def
   188   by (clarify, rule ext, fast)
   189 
   190 lemma bi_total_fun [transfer_rule]:
   191   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
   192   unfolding bi_total_def fun_rel_def
   193   apply safe
   194   apply (rename_tac f)
   195   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
   196   apply clarify
   197   apply (subgoal_tac "(THE x. A x y) = x", simp)
   198   apply (rule someI_ex)
   199   apply (simp)
   200   apply (rule the_equality)
   201   apply assumption
   202   apply (simp add: bi_unique_def)
   203   apply (rename_tac g)
   204   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   205   apply clarify
   206   apply (subgoal_tac "(THE y. A x y) = y", simp)
   207   apply (rule someI_ex)
   208   apply (simp)
   209   apply (rule the_equality)
   210   apply assumption
   211   apply (simp add: bi_unique_def)
   212   done
   213 
   214 lemma bi_unique_fun [transfer_rule]:
   215   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
   216   unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
   217   by (safe, metis, fast)
   218 
   219 
   220 subsection {* Transfer rules *}
   221 
   222 lemma eq_parametric [transfer_rule]:
   223   assumes "bi_unique A"
   224   shows "(A ===> A ===> op =) (op =) (op =)"
   225   using assms unfolding bi_unique_def fun_rel_def by auto
   226 
   227 lemma All_parametric [transfer_rule]:
   228   assumes "bi_total A"
   229   shows "((A ===> op =) ===> op =) All All"
   230   using assms unfolding bi_total_def fun_rel_def by fast
   231 
   232 lemma Ex_parametric [transfer_rule]:
   233   assumes "bi_total A"
   234   shows "((A ===> op =) ===> op =) Ex Ex"
   235   using assms unfolding bi_total_def fun_rel_def by fast
   236 
   237 lemma If_parametric [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
   238   unfolding fun_rel_def by simp
   239 
   240 lemma Let_parametric [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
   241   unfolding fun_rel_def by simp
   242 
   243 lemma id_parametric [transfer_rule]: "(A ===> A) id id"
   244   unfolding fun_rel_def by simp
   245 
   246 lemma comp_parametric [transfer_rule]:
   247   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
   248   unfolding fun_rel_def by simp
   249 
   250 lemma fun_upd_parametric [transfer_rule]:
   251   assumes [transfer_rule]: "bi_unique A"
   252   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
   253   unfolding fun_upd_def [abs_def] by transfer_prover
   254 
   255 lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
   256   by auto
   257 
   258 text {* Fallback rule for transferring universal quantifiers over
   259   correspondence relations that are not bi-total, and do not have
   260   custom transfer rules (e.g. relations between function types). *}
   261 
   262 lemma Domainp_forall_transfer [transfer_rule]:
   263   assumes "right_total A"
   264   shows "((A ===> op =) ===> op =)
   265     (transfer_bforall (Domainp A)) transfer_forall"
   266   using assms unfolding right_total_def
   267   unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
   268   by metis
   269 
   270 text {* Preferred rule for transferring universal quantifiers over
   271   bi-total correspondence relations (later rules are tried first). *}
   272 
   273 lemma transfer_forall_parametric [transfer_rule]:
   274   "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
   275   unfolding transfer_forall_def by (rule All_parametric)
   276 
   277 end