src/HOL/Transfer.thy
author kuncar
Mon May 13 13:59:04 2013 +0200 (2013-05-13)
changeset 51956 a4d81cdebf8b
parent 51955 04d9381bebff
child 52354 acb4f932dd24
permissions -rw-r--r--
better support for domains in Lifting/Transfer = replace Domainp T by the actual invariant in a transferred goal
huffman@47325
     1
(*  Title:      HOL/Transfer.thy
huffman@47325
     2
    Author:     Brian Huffman, TU Muenchen
kuncar@51956
     3
    Author:     Ondrej Kuncar, TU Muenchen
huffman@47325
     4
*)
huffman@47325
     5
huffman@47325
     6
header {* Generic theorem transfer using relations *}
huffman@47325
     7
huffman@47325
     8
theory Transfer
haftmann@51112
     9
imports Hilbert_Choice
huffman@47325
    10
begin
huffman@47325
    11
huffman@47325
    12
subsection {* Relator for function space *}
huffman@47325
    13
huffman@47325
    14
definition
huffman@47325
    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)
huffman@47325
    16
where
huffman@47325
    17
  "fun_rel A B = (\<lambda>f g. \<forall>x y. A x y \<longrightarrow> B (f x) (g y))"
huffman@47325
    18
huffman@47325
    19
lemma fun_relI [intro]:
huffman@47325
    20
  assumes "\<And>x y. A x y \<Longrightarrow> B (f x) (g y)"
huffman@47325
    21
  shows "(A ===> B) f g"
huffman@47325
    22
  using assms by (simp add: fun_rel_def)
huffman@47325
    23
huffman@47325
    24
lemma fun_relD:
huffman@47325
    25
  assumes "(A ===> B) f g" and "A x y"
huffman@47325
    26
  shows "B (f x) (g y)"
huffman@47325
    27
  using assms by (simp add: fun_rel_def)
huffman@47325
    28
kuncar@47937
    29
lemma fun_relD2:
kuncar@47937
    30
  assumes "(A ===> B) f g" and "A x x"
kuncar@47937
    31
  shows "B (f x) (g x)"
kuncar@47937
    32
  using assms unfolding fun_rel_def by auto
kuncar@47937
    33
huffman@47325
    34
lemma fun_relE:
huffman@47325
    35
  assumes "(A ===> B) f g" and "A x y"
huffman@47325
    36
  obtains "B (f x) (g y)"
huffman@47325
    37
  using assms by (simp add: fun_rel_def)
huffman@47325
    38
huffman@47325
    39
lemma fun_rel_eq:
huffman@47325
    40
  shows "((op =) ===> (op =)) = (op =)"
huffman@47325
    41
  by (auto simp add: fun_eq_iff elim: fun_relE)
huffman@47325
    42
huffman@47325
    43
lemma fun_rel_eq_rel:
huffman@47325
    44
  shows "((op =) ===> R) = (\<lambda>f g. \<forall>x. R (f x) (g x))"
huffman@47325
    45
  by (simp add: fun_rel_def)
huffman@47325
    46
huffman@47325
    47
huffman@47325
    48
subsection {* Transfer method *}
huffman@47325
    49
huffman@47789
    50
text {* Explicit tag for relation membership allows for
huffman@47789
    51
  backward proof methods. *}
huffman@47325
    52
huffman@47325
    53
definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
huffman@47325
    54
  where "Rel r \<equiv> r"
huffman@47325
    55
huffman@49975
    56
text {* Handling of equality relations *}
huffman@49975
    57
huffman@49975
    58
definition is_equality :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
huffman@49975
    59
  where "is_equality R \<longleftrightarrow> R = (op =)"
huffman@49975
    60
kuncar@51437
    61
lemma is_equality_eq: "is_equality (op =)"
kuncar@51437
    62
  unfolding is_equality_def by simp
kuncar@51437
    63
huffman@47325
    64
text {* Handling of meta-logic connectives *}
huffman@47325
    65
huffman@47325
    66
definition transfer_forall where
huffman@47325
    67
  "transfer_forall \<equiv> All"
huffman@47325
    68
huffman@47325
    69
definition transfer_implies where
huffman@47325
    70
  "transfer_implies \<equiv> op \<longrightarrow>"
huffman@47325
    71
huffman@47355
    72
definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47355
    73
  where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
huffman@47355
    74
huffman@47325
    75
lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
huffman@47325
    76
  unfolding atomize_all transfer_forall_def ..
huffman@47325
    77
huffman@47325
    78
lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
huffman@47325
    79
  unfolding atomize_imp transfer_implies_def ..
huffman@47325
    80
huffman@47355
    81
lemma transfer_bforall_unfold:
huffman@47355
    82
  "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
huffman@47355
    83
  unfolding transfer_bforall_def atomize_imp atomize_all ..
huffman@47355
    84
huffman@47658
    85
lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
huffman@47325
    86
  unfolding Rel_def by simp
huffman@47325
    87
huffman@47658
    88
lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
huffman@47325
    89
  unfolding Rel_def by simp
huffman@47325
    90
huffman@47635
    91
lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
huffman@47618
    92
  by simp
huffman@47618
    93
huffman@47325
    94
lemma Rel_eq_refl: "Rel (op =) x x"
huffman@47325
    95
  unfolding Rel_def ..
huffman@47325
    96
huffman@47789
    97
lemma Rel_app:
huffman@47523
    98
  assumes "Rel (A ===> B) f g" and "Rel A x y"
huffman@47789
    99
  shows "Rel B (f x) (g y)"
huffman@47789
   100
  using assms unfolding Rel_def fun_rel_def by fast
huffman@47523
   101
huffman@47789
   102
lemma Rel_abs:
huffman@47523
   103
  assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
huffman@47789
   104
  shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
huffman@47789
   105
  using assms unfolding Rel_def fun_rel_def by fast
huffman@47523
   106
wenzelm@48891
   107
ML_file "Tools/transfer.ML"
huffman@47325
   108
setup Transfer.setup
huffman@47325
   109
huffman@49975
   110
declare refl [transfer_rule]
huffman@49975
   111
huffman@47503
   112
declare fun_rel_eq [relator_eq]
huffman@47503
   113
huffman@47789
   114
hide_const (open) Rel
huffman@47325
   115
kuncar@51956
   116
text {* Handling of domains *}
kuncar@51956
   117
kuncar@51956
   118
lemma Domaimp_refl[transfer_domain_rule]:
kuncar@51956
   119
  "Domainp T = Domainp T" ..
huffman@47325
   120
huffman@47325
   121
subsection {* Predicates on relations, i.e. ``class constraints'' *}
huffman@47325
   122
huffman@47325
   123
definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   124
  where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
huffman@47325
   125
huffman@47325
   126
definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   127
  where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
huffman@47325
   128
huffman@47325
   129
definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   130
  where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
huffman@47325
   131
huffman@47325
   132
definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   133
  where "bi_unique R \<longleftrightarrow>
huffman@47325
   134
    (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
huffman@47325
   135
    (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
huffman@47325
   136
huffman@47325
   137
lemma right_total_alt_def:
huffman@47325
   138
  "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
huffman@47325
   139
  unfolding right_total_def fun_rel_def
huffman@47325
   140
  apply (rule iffI, fast)
huffman@47325
   141
  apply (rule allI)
huffman@47325
   142
  apply (drule_tac x="\<lambda>x. True" in spec)
huffman@47325
   143
  apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
huffman@47325
   144
  apply fast
huffman@47325
   145
  done
huffman@47325
   146
huffman@47325
   147
lemma right_unique_alt_def:
huffman@47325
   148
  "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
huffman@47325
   149
  unfolding right_unique_def fun_rel_def by auto
huffman@47325
   150
huffman@47325
   151
lemma bi_total_alt_def:
huffman@47325
   152
  "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
huffman@47325
   153
  unfolding bi_total_def fun_rel_def
huffman@47325
   154
  apply (rule iffI, fast)
huffman@47325
   155
  apply safe
huffman@47325
   156
  apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
huffman@47325
   157
  apply (drule_tac x="\<lambda>y. True" in spec)
huffman@47325
   158
  apply fast
huffman@47325
   159
  apply (drule_tac x="\<lambda>x. True" in spec)
huffman@47325
   160
  apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
huffman@47325
   161
  apply fast
huffman@47325
   162
  done
huffman@47325
   163
huffman@47325
   164
lemma bi_unique_alt_def:
huffman@47325
   165
  "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
huffman@47325
   166
  unfolding bi_unique_def fun_rel_def by auto
huffman@47325
   167
huffman@47660
   168
text {* Properties are preserved by relation composition. *}
huffman@47660
   169
huffman@47660
   170
lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
huffman@47660
   171
  by auto
huffman@47660
   172
huffman@47660
   173
lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
huffman@47660
   174
  unfolding bi_total_def OO_def by metis
huffman@47660
   175
huffman@47660
   176
lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
huffman@47660
   177
  unfolding bi_unique_def OO_def by metis
huffman@47660
   178
huffman@47660
   179
lemma right_total_OO:
huffman@47660
   180
  "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
huffman@47660
   181
  unfolding right_total_def OO_def by metis
huffman@47660
   182
huffman@47660
   183
lemma right_unique_OO:
huffman@47660
   184
  "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
huffman@47660
   185
  unfolding right_unique_def OO_def by metis
huffman@47660
   186
huffman@47325
   187
huffman@47325
   188
subsection {* Properties of relators *}
huffman@47325
   189
huffman@47325
   190
lemma right_total_eq [transfer_rule]: "right_total (op =)"
huffman@47325
   191
  unfolding right_total_def by simp
huffman@47325
   192
huffman@47325
   193
lemma right_unique_eq [transfer_rule]: "right_unique (op =)"
huffman@47325
   194
  unfolding right_unique_def by simp
huffman@47325
   195
huffman@47325
   196
lemma bi_total_eq [transfer_rule]: "bi_total (op =)"
huffman@47325
   197
  unfolding bi_total_def by simp
huffman@47325
   198
huffman@47325
   199
lemma bi_unique_eq [transfer_rule]: "bi_unique (op =)"
huffman@47325
   200
  unfolding bi_unique_def by simp
huffman@47325
   201
huffman@47325
   202
lemma right_total_fun [transfer_rule]:
huffman@47325
   203
  "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
huffman@47325
   204
  unfolding right_total_def fun_rel_def
huffman@47325
   205
  apply (rule allI, rename_tac g)
huffman@47325
   206
  apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
huffman@47325
   207
  apply clarify
huffman@47325
   208
  apply (subgoal_tac "(THE y. A x y) = y", simp)
huffman@47325
   209
  apply (rule someI_ex)
huffman@47325
   210
  apply (simp)
huffman@47325
   211
  apply (rule the_equality)
huffman@47325
   212
  apply assumption
huffman@47325
   213
  apply (simp add: right_unique_def)
huffman@47325
   214
  done
huffman@47325
   215
huffman@47325
   216
lemma right_unique_fun [transfer_rule]:
huffman@47325
   217
  "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
huffman@47325
   218
  unfolding right_total_def right_unique_def fun_rel_def
huffman@47325
   219
  by (clarify, rule ext, fast)
huffman@47325
   220
huffman@47325
   221
lemma bi_total_fun [transfer_rule]:
huffman@47325
   222
  "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
huffman@47325
   223
  unfolding bi_total_def fun_rel_def
huffman@47325
   224
  apply safe
huffman@47325
   225
  apply (rename_tac f)
huffman@47325
   226
  apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
huffman@47325
   227
  apply clarify
huffman@47325
   228
  apply (subgoal_tac "(THE x. A x y) = x", simp)
huffman@47325
   229
  apply (rule someI_ex)
huffman@47325
   230
  apply (simp)
huffman@47325
   231
  apply (rule the_equality)
huffman@47325
   232
  apply assumption
huffman@47325
   233
  apply (simp add: bi_unique_def)
huffman@47325
   234
  apply (rename_tac g)
huffman@47325
   235
  apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
huffman@47325
   236
  apply clarify
huffman@47325
   237
  apply (subgoal_tac "(THE y. A x y) = y", simp)
huffman@47325
   238
  apply (rule someI_ex)
huffman@47325
   239
  apply (simp)
huffman@47325
   240
  apply (rule the_equality)
huffman@47325
   241
  apply assumption
huffman@47325
   242
  apply (simp add: bi_unique_def)
huffman@47325
   243
  done
huffman@47325
   244
huffman@47325
   245
lemma bi_unique_fun [transfer_rule]:
huffman@47325
   246
  "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
huffman@47325
   247
  unfolding bi_total_def bi_unique_def fun_rel_def fun_eq_iff
huffman@47325
   248
  by (safe, metis, fast)
huffman@47325
   249
huffman@47325
   250
huffman@47635
   251
subsection {* Transfer rules *}
huffman@47325
   252
huffman@47684
   253
text {* Transfer rules using implication instead of equality on booleans. *}
huffman@47684
   254
huffman@47684
   255
lemma eq_imp_transfer [transfer_rule]:
huffman@47684
   256
  "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
huffman@47684
   257
  unfolding right_unique_alt_def .
huffman@47684
   258
huffman@47684
   259
lemma forall_imp_transfer [transfer_rule]:
huffman@47684
   260
  "right_total A \<Longrightarrow> ((A ===> op \<longrightarrow>) ===> op \<longrightarrow>) transfer_forall transfer_forall"
huffman@47684
   261
  unfolding right_total_alt_def transfer_forall_def .
huffman@47684
   262
huffman@47636
   263
lemma eq_transfer [transfer_rule]:
huffman@47325
   264
  assumes "bi_unique A"
huffman@47325
   265
  shows "(A ===> A ===> op =) (op =) (op =)"
huffman@47325
   266
  using assms unfolding bi_unique_def fun_rel_def by auto
huffman@47325
   267
kuncar@51956
   268
lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
kuncar@51956
   269
  by auto
kuncar@51956
   270
kuncar@51956
   271
lemma right_total_Ex_transfer[transfer_rule]:
kuncar@51956
   272
  assumes "right_total A"
kuncar@51956
   273
  shows "((A ===> op=) ===> op=) (Bex (Collect (Domainp A))) Ex"
kuncar@51956
   274
using assms unfolding right_total_def Bex_def fun_rel_def Domainp_iff[abs_def]
kuncar@51956
   275
by blast
kuncar@51956
   276
kuncar@51956
   277
lemma right_total_All_transfer[transfer_rule]:
kuncar@51956
   278
  assumes "right_total A"
kuncar@51956
   279
  shows "((A ===> op =) ===> op =) (Ball (Collect (Domainp A))) All"
kuncar@51956
   280
using assms unfolding right_total_def Ball_def fun_rel_def Domainp_iff[abs_def]
kuncar@51956
   281
by blast
kuncar@51956
   282
huffman@47636
   283
lemma All_transfer [transfer_rule]:
huffman@47325
   284
  assumes "bi_total A"
huffman@47325
   285
  shows "((A ===> op =) ===> op =) All All"
huffman@47325
   286
  using assms unfolding bi_total_def fun_rel_def by fast
huffman@47325
   287
huffman@47636
   288
lemma Ex_transfer [transfer_rule]:
huffman@47325
   289
  assumes "bi_total A"
huffman@47325
   290
  shows "((A ===> op =) ===> op =) Ex Ex"
huffman@47325
   291
  using assms unfolding bi_total_def fun_rel_def by fast
huffman@47325
   292
huffman@47636
   293
lemma If_transfer [transfer_rule]: "(op = ===> A ===> A ===> A) If If"
huffman@47325
   294
  unfolding fun_rel_def by simp
huffman@47325
   295
huffman@47636
   296
lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
huffman@47612
   297
  unfolding fun_rel_def by simp
huffman@47612
   298
huffman@47636
   299
lemma id_transfer [transfer_rule]: "(A ===> A) id id"
huffman@47625
   300
  unfolding fun_rel_def by simp
huffman@47625
   301
huffman@47636
   302
lemma comp_transfer [transfer_rule]:
huffman@47325
   303
  "((B ===> C) ===> (A ===> B) ===> (A ===> C)) (op \<circ>) (op \<circ>)"
huffman@47325
   304
  unfolding fun_rel_def by simp
huffman@47325
   305
huffman@47636
   306
lemma fun_upd_transfer [transfer_rule]:
huffman@47325
   307
  assumes [transfer_rule]: "bi_unique A"
huffman@47325
   308
  shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
huffman@47635
   309
  unfolding fun_upd_def [abs_def] by transfer_prover
huffman@47325
   310
huffman@47637
   311
lemma nat_case_transfer [transfer_rule]:
huffman@47637
   312
  "(A ===> (op = ===> A) ===> op = ===> A) nat_case nat_case"
huffman@47637
   313
  unfolding fun_rel_def by (simp split: nat.split)
huffman@47627
   314
huffman@47924
   315
lemma nat_rec_transfer [transfer_rule]:
huffman@47924
   316
  "(A ===> (op = ===> A ===> A) ===> op = ===> A) nat_rec nat_rec"
huffman@47924
   317
  unfolding fun_rel_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
huffman@47924
   318
huffman@47924
   319
lemma funpow_transfer [transfer_rule]:
huffman@47924
   320
  "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
huffman@47924
   321
  unfolding funpow_def by transfer_prover
huffman@47924
   322
huffman@47627
   323
lemma Domainp_forall_transfer [transfer_rule]:
huffman@47627
   324
  assumes "right_total A"
huffman@47627
   325
  shows "((A ===> op =) ===> op =)
huffman@47627
   326
    (transfer_bforall (Domainp A)) transfer_forall"
huffman@47627
   327
  using assms unfolding right_total_def
huffman@47627
   328
  unfolding transfer_forall_def transfer_bforall_def fun_rel_def Domainp_iff
huffman@47627
   329
  by metis
huffman@47627
   330
huffman@47636
   331
lemma forall_transfer [transfer_rule]:
huffman@47627
   332
  "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
huffman@47636
   333
  unfolding transfer_forall_def by (rule All_transfer)
huffman@47325
   334
huffman@47325
   335
end