src/HOL/Transfer.thy
author huffman
Fri Apr 27 14:07:31 2012 +0200 (2012-04-27)
changeset 47789 71a526ee569a
parent 47684 ead185e60b8c
child 47924 4e951258204b
permissions -rw-r--r--
implement transfer tactic with more scalable forward proof methods
     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 tag for relation membership allows for
    46   backward proof methods. *}
    47 
    48 definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
    49   where "Rel r \<equiv> r"
    50 
    51 text {* Handling of meta-logic connectives *}
    52 
    53 definition transfer_forall where
    54   "transfer_forall \<equiv> All"
    55 
    56 definition transfer_implies where
    57   "transfer_implies \<equiv> op \<longrightarrow>"
    58 
    59 definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
    60   where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
    61 
    62 lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
    63   unfolding atomize_all transfer_forall_def ..
    64 
    65 lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
    66   unfolding atomize_imp transfer_implies_def ..
    67 
    68 lemma transfer_bforall_unfold:
    69   "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
    70   unfolding transfer_bforall_def atomize_imp atomize_all ..
    71 
    72 lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
    73   unfolding Rel_def by simp
    74 
    75 lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
    76   unfolding Rel_def by simp
    77 
    78 lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
    79   by simp
    80 
    81 lemma Rel_eq_refl: "Rel (op =) x x"
    82   unfolding Rel_def ..
    83 
    84 lemma Rel_app:
    85   assumes "Rel (A ===> B) f g" and "Rel A x y"
    86   shows "Rel B (f x) (g y)"
    87   using assms unfolding Rel_def fun_rel_def by fast
    88 
    89 lemma Rel_abs:
    90   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
    91   shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
    92   using assms unfolding Rel_def fun_rel_def by fast
    93 
    94 use "Tools/transfer.ML"
    95 
    96 setup Transfer.setup
    97 
    98 declare fun_rel_eq [relator_eq]
    99 
   100 hide_const (open) Rel
   101 
   102 
   103 subsection {* Predicates on relations, i.e. ``class constraints'' *}
   104 
   105 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   106   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
   107 
   108 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   109   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
   110 
   111 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   112   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
   113 
   114 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   115   where "bi_unique R \<longleftrightarrow>
   116     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
   117     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
   118 
   119 lemma right_total_alt_def:
   120   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
   121   unfolding right_total_def fun_rel_def
   122   apply (rule iffI, fast)
   123   apply (rule allI)
   124   apply (drule_tac x="\<lambda>x. True" in spec)
   125   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   126   apply fast
   127   done
   128 
   129 lemma right_unique_alt_def:
   130   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
   131   unfolding right_unique_def fun_rel_def by auto
   132 
   133 lemma bi_total_alt_def:
   134   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
   135   unfolding bi_total_def fun_rel_def
   136   apply (rule iffI, fast)
   137   apply safe
   138   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
   139   apply (drule_tac x="\<lambda>y. True" in spec)
   140   apply fast
   141   apply (drule_tac x="\<lambda>x. True" in spec)
   142   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   143   apply fast
   144   done
   145 
   146 lemma bi_unique_alt_def:
   147   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
   148   unfolding bi_unique_def fun_rel_def by auto
   149 
   150 text {* Properties are preserved by relation composition. *}
   151 
   152 lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
   153   by auto
   154 
   155 lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
   156   unfolding bi_total_def OO_def by metis
   157 
   158 lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
   159   unfolding bi_unique_def OO_def by metis
   160 
   161 lemma right_total_OO:
   162   "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
   163   unfolding right_total_def OO_def by metis
   164 
   165 lemma right_unique_OO:
   166   "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
   167   unfolding right_unique_def OO_def by metis
   168 
   169 
   170 subsection {* Properties of relators *}
   171 
   172 lemma right_total_eq [transfer_rule]: "right_total (op =)"
   173   unfolding right_total_def by simp
   174 
   175 lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
   176   unfolding right_unique_def by simp
   177 
   178 lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
   179   unfolding bi_total_def by simp
   180 
   181 lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
   182   unfolding bi_unique_def by simp
   183 
   184 lemma right_total_fun [transfer_rule]:
   185   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
   186   unfolding right_total_def fun_rel_def
   187   apply (rule allI, rename_tac g)
   188   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   189   apply clarify
   190   apply (subgoal_tac "(THE y. A x y) = y", simp)
   191   apply (rule someI_ex)
   192   apply (simp)
   193   apply (rule the_equality)
   194   apply assumption
   195   apply (simp add: right_unique_def)
   196   done
   197 
   198 lemma right_unique_fun [transfer_rule]:
   199   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
   200   unfolding right_total_def right_unique_def fun_rel_def
   201   by (clarify, rule ext, fast)
   202 
   203 lemma bi_total_fun [transfer_rule]:
   204   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
   205   unfolding bi_total_def fun_rel_def
   206   apply safe
   207   apply (rename_tac f)
   208   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
   209   apply clarify
   210   apply (subgoal_tac "(THE x. A x y) = x", simp)
   211   apply (rule someI_ex)
   212   apply (simp)
   213   apply (rule the_equality)
   214   apply assumption
   215   apply (simp add: bi_unique_def)
   216   apply (rename_tac g)
   217   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   218   apply clarify
   219   apply (subgoal_tac "(THE y. A x y) = y", simp)
   220   apply (rule someI_ex)
   221   apply (simp)
   222   apply (rule the_equality)
   223   apply assumption
   224   apply (simp add: bi_unique_def)
   225   done
   226 
   227 lemma bi_unique_fun [transfer_rule]:
   228   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
   229   unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
   230   by (safe, metis, fast)
   231 
   232 
   233 subsection {* Transfer rules *}
   234 
   235 text {* Transfer rules using implication instead of equality on booleans. *}
   236 
   237 lemma eq_imp_transfer [transfer_rule]:
   238   "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
   239   unfolding right_unique_alt_def .
   240 
   241 lemma forall_imp_transfer [transfer_rule]:
   242   "right_total A \<Longrightarrow> ((A ===> op \<longrightarrow>) ===> op \<longrightarrow>) transfer_forall transfer_forall"
   243   unfolding right_total_alt_def transfer_forall_def .
   244 
   245 lemma eq_transfer [transfer_rule]:
   246   assumes "bi_unique A"
   247   shows "(A ===> A ===> op =) (op =) (op =)"
   248   using assms unfolding bi_unique_def fun_rel_def by auto
   249 
   250 lemma All_transfer [transfer_rule]:
   251   assumes "bi_total A"
   252   shows "((A ===> op =) ===> op =) All All"
   253   using assms unfolding bi_total_def fun_rel_def by fast
   254 
   255 lemma Ex_transfer [transfer_rule]:
   256   assumes "bi_total A"
   257   shows "((A ===> op =) ===> op =) Ex Ex"
   258   using assms unfolding bi_total_def fun_rel_def by fast
   259 
   260 lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
   261   unfolding fun_rel_def by simp
   262 
   263 lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
   264   unfolding fun_rel_def by simp
   265 
   266 lemma id_transfer [transfer_rule]: "(A ===> A) id id"
   267   unfolding fun_rel_def by simp
   268 
   269 lemma comp_transfer [transfer_rule]:
   270   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
   271   unfolding fun_rel_def by simp
   272 
   273 lemma fun_upd_transfer [transfer_rule]:
   274   assumes [transfer_rule]: "bi_unique A"
   275   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
   276   unfolding fun_upd_def [abs_def] by transfer_prover
   277 
   278 lemma nat_case_transfer [transfer_rule]:
   279   "(A ===> (op = ===> A) ===> op = ===> A) nat_case nat_case"
   280   unfolding fun_rel_def by (simp split: nat.split)
   281 
   282 text {* Fallback rule for transferring universal quantifiers over
   283   correspondence relations that are not bi-total, and do not have
   284   custom transfer rules (e.g. relations between function types). *}
   285 
   286 lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
   287   by auto
   288 
   289 lemma Domainp_forall_transfer [transfer_rule]:
   290   assumes "right_total A"
   291   shows "((A ===> op =) ===> op =)
   292     (transfer_bforall (Domainp A)) transfer_forall"
   293   using assms unfolding right_total_def
   294   unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
   295   by metis
   296 
   297 text {* Preferred rule for transferring universal quantifiers over
   298   bi-total correspondence relations (later rules are tried first). *}
   299 
   300 lemma forall_transfer [transfer_rule]:
   301   "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
   302   unfolding transfer_forall_def by (rule All_transfer)
   303 
   304 end