src/HOL/Transfer.thy
author huffman
Tue Apr 17 11:03:08 2012 +0200 (2012-04-17)
changeset 47503 cb44d09d9d22
parent 47355 3d9d98e0f1a4
child 47523 1bf0e92c1ca0
permissions -rw-r--r--
add theory data for relator identity rules;
preprocess transfer rules generated by lift_definition using relator rules
     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 Rel_eq_refl: "Rel (op =) x x"
    85   unfolding Rel_def ..
    86 
    87 use "Tools/transfer.ML"
    88 
    89 setup Transfer.setup
    90 
    91 declare fun_rel_eq [relator_eq]
    92 
    93 lemma Rel_App [transfer_raw]:
    94   assumes "Rel (A ===> B) f g" and "Rel A x y"
    95   shows "Rel B (App f x) (App g y)"
    96   using assms unfolding Rel_def App_def fun_rel_def by fast
    97 
    98 lemma Rel_Abs [transfer_raw]:
    99   assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
   100   shows "Rel (A ===> B) (Abs (\<lambda>x. f x)) (Abs (\<lambda>y. g y))"
   101   using assms unfolding Rel_def Abs_def fun_rel_def by fast
   102 
   103 hide_const (open) App Abs Rel
   104 
   105 
   106 subsection {* Predicates on relations, i.e. ``class constraints'' *}
   107 
   108 definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   109   where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
   110 
   111 definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   112   where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
   113 
   114 definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   115   where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
   116 
   117 definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
   118   where "bi_unique R \<longleftrightarrow>
   119     (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
   120     (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
   121 
   122 lemma right_total_alt_def:
   123   "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
   124   unfolding right_total_def fun_rel_def
   125   apply (rule iffI, fast)
   126   apply (rule allI)
   127   apply (drule_tac x="\<lambda>x. True" in spec)
   128   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   129   apply fast
   130   done
   131 
   132 lemma right_unique_alt_def:
   133   "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
   134   unfolding right_unique_def fun_rel_def by auto
   135 
   136 lemma bi_total_alt_def:
   137   "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
   138   unfolding bi_total_def fun_rel_def
   139   apply (rule iffI, fast)
   140   apply safe
   141   apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
   142   apply (drule_tac x="\<lambda>y. True" in spec)
   143   apply fast
   144   apply (drule_tac x="\<lambda>x. True" in spec)
   145   apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
   146   apply fast
   147   done
   148 
   149 lemma bi_unique_alt_def:
   150   "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
   151   unfolding bi_unique_def fun_rel_def by auto
   152 
   153 
   154 subsection {* Properties of relators *}
   155 
   156 lemma right_total_eq [transfer_rule]: "right_total (op =)"
   157   unfolding right_total_def by simp
   158 
   159 lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
   160   unfolding right_unique_def by simp
   161 
   162 lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
   163   unfolding bi_total_def by simp
   164 
   165 lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
   166   unfolding bi_unique_def by simp
   167 
   168 lemma right_total_fun [transfer_rule]:
   169   "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
   170   unfolding right_total_def fun_rel_def
   171   apply (rule allI, rename_tac g)
   172   apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
   173   apply clarify
   174   apply (subgoal_tac "(THE y. A x y) = y", simp)
   175   apply (rule someI_ex)
   176   apply (simp)
   177   apply (rule the_equality)
   178   apply assumption
   179   apply (simp add: right_unique_def)
   180   done
   181 
   182 lemma right_unique_fun [transfer_rule]:
   183   "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
   184   unfolding right_total_def right_unique_def fun_rel_def
   185   by (clarify, rule ext, fast)
   186 
   187 lemma bi_total_fun [transfer_rule]:
   188   "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
   189   unfolding bi_total_def fun_rel_def
   190   apply safe
   191   apply (rename_tac f)
   192   apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
   193   apply clarify
   194   apply (subgoal_tac "(THE x. A x y) = x", simp)
   195   apply (rule someI_ex)
   196   apply (simp)
   197   apply (rule the_equality)
   198   apply assumption
   199   apply (simp add: bi_unique_def)
   200   apply (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: bi_unique_def)
   209   done
   210 
   211 lemma bi_unique_fun [transfer_rule]:
   212   "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
   213   unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
   214   by (safe, metis, fast)
   215 
   216 
   217 subsection {* Correspondence rules *}
   218 
   219 lemma eq_parametric [transfer_rule]:
   220   assumes "bi_unique A"
   221   shows "(A ===> A ===> op =) (op =) (op =)"
   222   using assms unfolding bi_unique_def fun_rel_def by auto
   223 
   224 lemma All_parametric [transfer_rule]:
   225   assumes "bi_total A"
   226   shows "((A ===> op =) ===> op =) All All"
   227   using assms unfolding bi_total_def fun_rel_def by fast
   228 
   229 lemma Ex_parametric [transfer_rule]:
   230   assumes "bi_total A"
   231   shows "((A ===> op =) ===> op =) Ex Ex"
   232   using assms unfolding bi_total_def fun_rel_def by fast
   233 
   234 lemma If_parametric [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
   235   unfolding fun_rel_def by simp
   236 
   237 lemma comp_parametric [transfer_rule]:
   238   "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
   239   unfolding fun_rel_def by simp
   240 
   241 lemma fun_upd_parametric [transfer_rule]:
   242   assumes [transfer_rule]: "bi_unique A"
   243   shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
   244   unfolding fun_upd_def [abs_def] by correspondence
   245 
   246 lemmas transfer_forall_parametric [transfer_rule]
   247   = All_parametric [folded transfer_forall_def]
   248 
   249 end