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