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