src/HOL/Transfer.thy
author haftmann
Fri Jun 19 07:53:35 2015 +0200 (2015-06-19)
changeset 60517 f16e4fb20652
parent 60229 4cd6462c1fda
child 60758 d8d85a8172b5
permissions -rw-r--r--
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
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
wenzelm@58889
     6
section {* Generic theorem transfer using relations *}
huffman@47325
     7
huffman@47325
     8
theory Transfer
desharna@59275
     9
imports Basic_BNF_LFPs Hilbert_Choice Metis
huffman@47325
    10
begin
huffman@47325
    11
huffman@47325
    12
subsection {* Relator for function space *}
huffman@47325
    13
kuncar@53011
    14
locale lifting_syntax
kuncar@53011
    15
begin
blanchet@55945
    16
  notation rel_fun (infixr "===>" 55)
kuncar@53011
    17
  notation map_fun (infixr "--->" 55)
kuncar@53011
    18
end
kuncar@53011
    19
kuncar@53011
    20
context
kuncar@53011
    21
begin
kuncar@53011
    22
interpretation lifting_syntax .
kuncar@53011
    23
blanchet@55945
    24
lemma rel_funD2:
blanchet@55945
    25
  assumes "rel_fun A B f g" and "A x x"
kuncar@47937
    26
  shows "B (f x) (g x)"
blanchet@55945
    27
  using assms by (rule rel_funD)
kuncar@47937
    28
blanchet@55945
    29
lemma rel_funE:
blanchet@55945
    30
  assumes "rel_fun A B f g" and "A x y"
huffman@47325
    31
  obtains "B (f x) (g y)"
blanchet@55945
    32
  using assms by (simp add: rel_fun_def)
huffman@47325
    33
blanchet@55945
    34
lemmas rel_fun_eq = fun.rel_eq
huffman@47325
    35
blanchet@55945
    36
lemma rel_fun_eq_rel:
blanchet@55945
    37
shows "rel_fun (op =) R = (\<lambda>f g. \<forall>x. R (f x) (g x))"
blanchet@55945
    38
  by (simp add: rel_fun_def)
huffman@47325
    39
huffman@47325
    40
huffman@47325
    41
subsection {* Transfer method *}
huffman@47325
    42
huffman@47789
    43
text {* Explicit tag for relation membership allows for
huffman@47789
    44
  backward proof methods. *}
huffman@47325
    45
huffman@47325
    46
definition Rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
huffman@47325
    47
  where "Rel r \<equiv> r"
huffman@47325
    48
huffman@49975
    49
text {* Handling of equality relations *}
huffman@49975
    50
huffman@49975
    51
definition is_equality :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool"
huffman@49975
    52
  where "is_equality R \<longleftrightarrow> R = (op =)"
huffman@49975
    53
kuncar@51437
    54
lemma is_equality_eq: "is_equality (op =)"
kuncar@51437
    55
  unfolding is_equality_def by simp
kuncar@51437
    56
huffman@52354
    57
text {* Reverse implication for monotonicity rules *}
huffman@52354
    58
huffman@52354
    59
definition rev_implies where
huffman@52354
    60
  "rev_implies x y \<longleftrightarrow> (y \<longrightarrow> x)"
huffman@52354
    61
huffman@47325
    62
text {* Handling of meta-logic connectives *}
huffman@47325
    63
huffman@47325
    64
definition transfer_forall where
huffman@47325
    65
  "transfer_forall \<equiv> All"
huffman@47325
    66
huffman@47325
    67
definition transfer_implies where
huffman@47325
    68
  "transfer_implies \<equiv> op \<longrightarrow>"
huffman@47325
    69
huffman@47355
    70
definition transfer_bforall :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47355
    71
  where "transfer_bforall \<equiv> (\<lambda>P Q. \<forall>x. P x \<longrightarrow> Q x)"
huffman@47355
    72
huffman@47325
    73
lemma transfer_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (transfer_forall (\<lambda>x. P x))"
huffman@47325
    74
  unfolding atomize_all transfer_forall_def ..
huffman@47325
    75
huffman@47325
    76
lemma transfer_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (transfer_implies A B)"
huffman@47325
    77
  unfolding atomize_imp transfer_implies_def ..
huffman@47325
    78
huffman@47355
    79
lemma transfer_bforall_unfold:
huffman@47355
    80
  "Trueprop (transfer_bforall P (\<lambda>x. Q x)) \<equiv> (\<And>x. P x \<Longrightarrow> Q x)"
huffman@47355
    81
  unfolding transfer_bforall_def atomize_imp atomize_all ..
huffman@47355
    82
huffman@47658
    83
lemma transfer_start: "\<lbrakk>P; Rel (op =) P Q\<rbrakk> \<Longrightarrow> Q"
huffman@47325
    84
  unfolding Rel_def by simp
huffman@47325
    85
huffman@47658
    86
lemma transfer_start': "\<lbrakk>P; Rel (op \<longrightarrow>) P Q\<rbrakk> \<Longrightarrow> Q"
huffman@47325
    87
  unfolding Rel_def by simp
huffman@47325
    88
huffman@47635
    89
lemma transfer_prover_start: "\<lbrakk>x = x'; Rel R x' y\<rbrakk> \<Longrightarrow> Rel R x y"
huffman@47618
    90
  by simp
huffman@47618
    91
huffman@52358
    92
lemma untransfer_start: "\<lbrakk>Q; Rel (op =) P Q\<rbrakk> \<Longrightarrow> P"
huffman@52358
    93
  unfolding Rel_def by simp
huffman@52358
    94
huffman@47325
    95
lemma Rel_eq_refl: "Rel (op =) x x"
huffman@47325
    96
  unfolding Rel_def ..
huffman@47325
    97
huffman@47789
    98
lemma Rel_app:
huffman@47523
    99
  assumes "Rel (A ===> B) f g" and "Rel A x y"
huffman@47789
   100
  shows "Rel B (f x) (g y)"
blanchet@55945
   101
  using assms unfolding Rel_def rel_fun_def by fast
huffman@47523
   102
huffman@47789
   103
lemma Rel_abs:
huffman@47523
   104
  assumes "\<And>x y. Rel A x y \<Longrightarrow> Rel B (f x) (g y)"
huffman@47789
   105
  shows "Rel (A ===> B) (\<lambda>x. f x) (\<lambda>y. g y)"
blanchet@55945
   106
  using assms unfolding Rel_def rel_fun_def by fast
huffman@47523
   107
huffman@47325
   108
subsection {* Predicates on relations, i.e. ``class constraints'' *}
huffman@47325
   109
kuncar@56518
   110
definition left_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
kuncar@56518
   111
  where "left_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y)"
kuncar@56518
   112
kuncar@56518
   113
definition left_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
kuncar@56518
   114
  where "left_unique R \<longleftrightarrow> (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
kuncar@56518
   115
huffman@47325
   116
definition right_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   117
  where "right_total R \<longleftrightarrow> (\<forall>y. \<exists>x. R x y)"
huffman@47325
   118
huffman@47325
   119
definition right_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   120
  where "right_unique R \<longleftrightarrow> (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z)"
huffman@47325
   121
huffman@47325
   122
definition bi_total :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   123
  where "bi_total R \<longleftrightarrow> (\<forall>x. \<exists>y. R x y) \<and> (\<forall>y. \<exists>x. R x y)"
huffman@47325
   124
huffman@47325
   125
definition bi_unique :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> bool"
huffman@47325
   126
  where "bi_unique R \<longleftrightarrow>
huffman@47325
   127
    (\<forall>x y z. R x y \<longrightarrow> R x z \<longrightarrow> y = z) \<and>
huffman@47325
   128
    (\<forall>x y z. R x z \<longrightarrow> R y z \<longrightarrow> x = y)"
huffman@47325
   129
kuncar@56518
   130
lemma left_uniqueI: "(\<And>x y z. \<lbrakk> A x z; A y z \<rbrakk> \<Longrightarrow> x = y) \<Longrightarrow> left_unique A"
kuncar@56518
   131
unfolding left_unique_def by blast
kuncar@56518
   132
kuncar@56518
   133
lemma left_uniqueD: "\<lbrakk> left_unique A; A x z; A y z \<rbrakk> \<Longrightarrow> x = y"
kuncar@56518
   134
unfolding left_unique_def by blast
kuncar@56518
   135
kuncar@56518
   136
lemma left_totalI:
kuncar@56518
   137
  "(\<And>x. \<exists>y. R x y) \<Longrightarrow> left_total R"
kuncar@56518
   138
unfolding left_total_def by blast
kuncar@56518
   139
kuncar@56518
   140
lemma left_totalE:
kuncar@56518
   141
  assumes "left_total R"
kuncar@56518
   142
  obtains "(\<And>x. \<exists>y. R x y)"
kuncar@56518
   143
using assms unfolding left_total_def by blast
kuncar@56518
   144
Andreas@53927
   145
lemma bi_uniqueDr: "\<lbrakk> bi_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
Andreas@53927
   146
by(simp add: bi_unique_def)
Andreas@53927
   147
Andreas@53927
   148
lemma bi_uniqueDl: "\<lbrakk> bi_unique A; A x y; A z y \<rbrakk> \<Longrightarrow> x = z"
Andreas@53927
   149
by(simp add: bi_unique_def)
Andreas@53927
   150
Andreas@53927
   151
lemma right_uniqueI: "(\<And>x y z. \<lbrakk> A x y; A x z \<rbrakk> \<Longrightarrow> y = z) \<Longrightarrow> right_unique A"
blanchet@56085
   152
unfolding right_unique_def by fast
Andreas@53927
   153
Andreas@53927
   154
lemma right_uniqueD: "\<lbrakk> right_unique A; A x y; A x z \<rbrakk> \<Longrightarrow> y = z"
blanchet@56085
   155
unfolding right_unique_def by fast
Andreas@53927
   156
Andreas@59514
   157
lemma right_totalI: "(\<And>y. \<exists>x. A x y) \<Longrightarrow> right_total A"
Andreas@59514
   158
by(simp add: right_total_def)
Andreas@59514
   159
Andreas@59514
   160
lemma right_totalE:
Andreas@59514
   161
  assumes "right_total A"
Andreas@59514
   162
  obtains x where "A x y"
Andreas@59514
   163
using assms by(auto simp add: right_total_def)
Andreas@59514
   164
kuncar@56524
   165
lemma right_total_alt_def2:
huffman@47325
   166
  "right_total R \<longleftrightarrow> ((R ===> op \<longrightarrow>) ===> op \<longrightarrow>) All All"
blanchet@55945
   167
  unfolding right_total_def rel_fun_def
huffman@47325
   168
  apply (rule iffI, fast)
huffman@47325
   169
  apply (rule allI)
huffman@47325
   170
  apply (drule_tac x="\<lambda>x. True" in spec)
huffman@47325
   171
  apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
huffman@47325
   172
  apply fast
huffman@47325
   173
  done
huffman@47325
   174
kuncar@56524
   175
lemma right_unique_alt_def2:
huffman@47325
   176
  "right_unique R \<longleftrightarrow> (R ===> R ===> op \<longrightarrow>) (op =) (op =)"
blanchet@55945
   177
  unfolding right_unique_def rel_fun_def by auto
huffman@47325
   178
kuncar@56524
   179
lemma bi_total_alt_def2:
huffman@47325
   180
  "bi_total R \<longleftrightarrow> ((R ===> op =) ===> op =) All All"
blanchet@55945
   181
  unfolding bi_total_def rel_fun_def
huffman@47325
   182
  apply (rule iffI, fast)
huffman@47325
   183
  apply safe
huffman@47325
   184
  apply (drule_tac x="\<lambda>x. \<exists>y. R x y" in spec)
huffman@47325
   185
  apply (drule_tac x="\<lambda>y. True" in spec)
huffman@47325
   186
  apply fast
huffman@47325
   187
  apply (drule_tac x="\<lambda>x. True" in spec)
huffman@47325
   188
  apply (drule_tac x="\<lambda>y. \<exists>x. R x y" in spec)
huffman@47325
   189
  apply fast
huffman@47325
   190
  done
huffman@47325
   191
kuncar@56524
   192
lemma bi_unique_alt_def2:
huffman@47325
   193
  "bi_unique R \<longleftrightarrow> (R ===> R ===> op =) (op =) (op =)"
blanchet@55945
   194
  unfolding bi_unique_def rel_fun_def by auto
huffman@47325
   195
kuncar@56518
   196
lemma [simp]:
kuncar@56518
   197
  shows left_unique_conversep: "left_unique A\<inverse>\<inverse> \<longleftrightarrow> right_unique A"
kuncar@56518
   198
  and right_unique_conversep: "right_unique A\<inverse>\<inverse> \<longleftrightarrow> left_unique A"
kuncar@56518
   199
by(auto simp add: left_unique_def right_unique_def)
kuncar@56518
   200
kuncar@56518
   201
lemma [simp]:
kuncar@56518
   202
  shows left_total_conversep: "left_total A\<inverse>\<inverse> \<longleftrightarrow> right_total A"
kuncar@56518
   203
  and right_total_conversep: "right_total A\<inverse>\<inverse> \<longleftrightarrow> left_total A"
kuncar@56518
   204
by(simp_all add: left_total_def right_total_def)
kuncar@56518
   205
Andreas@53944
   206
lemma bi_unique_conversep [simp]: "bi_unique R\<inverse>\<inverse> = bi_unique R"
Andreas@53944
   207
by(auto simp add: bi_unique_def)
Andreas@53944
   208
Andreas@53944
   209
lemma bi_total_conversep [simp]: "bi_total R\<inverse>\<inverse> = bi_total R"
Andreas@53944
   210
by(auto simp add: bi_total_def)
Andreas@53944
   211
kuncar@56524
   212
lemma right_unique_alt_def: "right_unique R = (conversep R OO R \<le> op=)" unfolding right_unique_def by blast
kuncar@56524
   213
lemma left_unique_alt_def: "left_unique R = (R OO (conversep R) \<le> op=)" unfolding left_unique_def by blast
kuncar@56524
   214
kuncar@56524
   215
lemma right_total_alt_def: "right_total R = (conversep R OO R \<ge> op=)" unfolding right_total_def by blast
kuncar@56524
   216
lemma left_total_alt_def: "left_total R = (R OO conversep R \<ge> op=)" unfolding left_total_def by blast
kuncar@56524
   217
kuncar@56524
   218
lemma bi_total_alt_def: "bi_total A = (left_total A \<and> right_total A)"
kuncar@56518
   219
unfolding left_total_def right_total_def bi_total_def by blast
kuncar@56518
   220
kuncar@56524
   221
lemma bi_unique_alt_def: "bi_unique A = (left_unique A \<and> right_unique A)"
kuncar@56518
   222
unfolding left_unique_def right_unique_def bi_unique_def by blast
kuncar@56518
   223
kuncar@56518
   224
lemma bi_totalI: "left_total R \<Longrightarrow> right_total R \<Longrightarrow> bi_total R"
kuncar@56524
   225
unfolding bi_total_alt_def ..
kuncar@56518
   226
kuncar@56518
   227
lemma bi_uniqueI: "left_unique R \<Longrightarrow> right_unique R \<Longrightarrow> bi_unique R"
kuncar@56524
   228
unfolding bi_unique_alt_def ..
kuncar@56524
   229
kuncar@56524
   230
end
kuncar@56524
   231
kuncar@56524
   232
subsection {* Equality restricted by a predicate *}
kuncar@56524
   233
blanchet@58182
   234
definition eq_onp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
kuncar@56524
   235
  where "eq_onp R = (\<lambda>x y. R x \<and> x = y)"
kuncar@56524
   236
blanchet@58182
   237
lemma eq_onp_Grp: "eq_onp P = BNF_Def.Grp (Collect P) id"
blanchet@58182
   238
unfolding eq_onp_def Grp_def by auto
kuncar@56524
   239
kuncar@56524
   240
lemma eq_onp_to_eq:
kuncar@56524
   241
  assumes "eq_onp P x y"
kuncar@56524
   242
  shows "x = y"
kuncar@56524
   243
using assms by (simp add: eq_onp_def)
kuncar@56524
   244
blanchet@58182
   245
lemma eq_onp_top_eq_eq: "eq_onp top = op="
kuncar@56677
   246
by (simp add: eq_onp_def)
kuncar@56677
   247
kuncar@56524
   248
lemma eq_onp_same_args:
kuncar@56524
   249
  shows "eq_onp P x x = P x"
kuncar@56524
   250
using assms by (auto simp add: eq_onp_def)
kuncar@56524
   251
kuncar@56524
   252
lemma Ball_Collect: "Ball A P = (A \<subseteq> (Collect P))"
blanchet@57260
   253
by auto
kuncar@56518
   254
kuncar@56524
   255
ML_file "Tools/Transfer/transfer.ML"
kuncar@56524
   256
declare refl [transfer_rule]
kuncar@56524
   257
kuncar@56524
   258
hide_const (open) Rel
kuncar@56524
   259
kuncar@56524
   260
context
kuncar@56524
   261
begin
kuncar@56524
   262
interpretation lifting_syntax .
kuncar@56524
   263
kuncar@56524
   264
text {* Handling of domains *}
kuncar@56524
   265
kuncar@56524
   266
lemma Domainp_iff: "Domainp T x \<longleftrightarrow> (\<exists>y. T x y)"
kuncar@56524
   267
  by auto
kuncar@56524
   268
traytel@58386
   269
lemma Domainp_refl[transfer_domain_rule]:
kuncar@56524
   270
  "Domainp T = Domainp T" ..
kuncar@56524
   271
kuncar@60229
   272
lemma Domain_eq_top: "Domainp op= = top" by auto
kuncar@60229
   273
kuncar@56524
   274
lemma Domainp_prod_fun_eq[relator_domain]:
kuncar@56524
   275
  "Domainp (op= ===> T) = (\<lambda>f. \<forall>x. (Domainp T) (f x))"
kuncar@56524
   276
by (auto intro: choice simp: Domainp_iff rel_fun_def fun_eq_iff)
kuncar@56518
   277
huffman@47660
   278
text {* Properties are preserved by relation composition. *}
huffman@47660
   279
huffman@47660
   280
lemma OO_def: "R OO S = (\<lambda>x z. \<exists>y. R x y \<and> S y z)"
huffman@47660
   281
  by auto
huffman@47660
   282
huffman@47660
   283
lemma bi_total_OO: "\<lbrakk>bi_total A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A OO B)"
blanchet@56085
   284
  unfolding bi_total_def OO_def by fast
huffman@47660
   285
huffman@47660
   286
lemma bi_unique_OO: "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A OO B)"
blanchet@56085
   287
  unfolding bi_unique_def OO_def by blast
huffman@47660
   288
huffman@47660
   289
lemma right_total_OO:
huffman@47660
   290
  "\<lbrakk>right_total A; right_total B\<rbrakk> \<Longrightarrow> right_total (A OO B)"
blanchet@56085
   291
  unfolding right_total_def OO_def by fast
huffman@47660
   292
huffman@47660
   293
lemma right_unique_OO:
huffman@47660
   294
  "\<lbrakk>right_unique A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A OO B)"
blanchet@56085
   295
  unfolding right_unique_def OO_def by fast
huffman@47660
   296
kuncar@56518
   297
lemma left_total_OO: "left_total R \<Longrightarrow> left_total S \<Longrightarrow> left_total (R OO S)"
kuncar@56518
   298
unfolding left_total_def OO_def by fast
kuncar@56518
   299
kuncar@56518
   300
lemma left_unique_OO: "left_unique R \<Longrightarrow> left_unique S \<Longrightarrow> left_unique (R OO S)"
kuncar@56518
   301
unfolding left_unique_def OO_def by blast
kuncar@56518
   302
huffman@47325
   303
huffman@47325
   304
subsection {* Properties of relators *}
huffman@47325
   305
blanchet@58182
   306
lemma left_total_eq[transfer_rule]: "left_total op="
kuncar@56518
   307
  unfolding left_total_def by blast
kuncar@56518
   308
blanchet@58182
   309
lemma left_unique_eq[transfer_rule]: "left_unique op="
kuncar@56518
   310
  unfolding left_unique_def by blast
kuncar@56518
   311
kuncar@56518
   312
lemma right_total_eq [transfer_rule]: "right_total op="
huffman@47325
   313
  unfolding right_total_def by simp
huffman@47325
   314
kuncar@56518
   315
lemma right_unique_eq [transfer_rule]: "right_unique op="
huffman@47325
   316
  unfolding right_unique_def by simp
huffman@47325
   317
kuncar@56518
   318
lemma bi_total_eq[transfer_rule]: "bi_total (op =)"
huffman@47325
   319
  unfolding bi_total_def by simp
huffman@47325
   320
kuncar@56518
   321
lemma bi_unique_eq[transfer_rule]: "bi_unique (op =)"
huffman@47325
   322
  unfolding bi_unique_def by simp
huffman@47325
   323
kuncar@56518
   324
lemma left_total_fun[transfer_rule]:
kuncar@56518
   325
  "\<lbrakk>left_unique A; left_total B\<rbrakk> \<Longrightarrow> left_total (A ===> B)"
kuncar@56518
   326
  unfolding left_total_def rel_fun_def
kuncar@56518
   327
  apply (rule allI, rename_tac f)
kuncar@56518
   328
  apply (rule_tac x="\<lambda>y. SOME z. B (f (THE x. A x y)) z" in exI)
kuncar@56518
   329
  apply clarify
kuncar@56518
   330
  apply (subgoal_tac "(THE x. A x y) = x", simp)
kuncar@56518
   331
  apply (rule someI_ex)
kuncar@56518
   332
  apply (simp)
kuncar@56518
   333
  apply (rule the_equality)
kuncar@56518
   334
  apply assumption
kuncar@56518
   335
  apply (simp add: left_unique_def)
kuncar@56518
   336
  done
kuncar@56518
   337
kuncar@56518
   338
lemma left_unique_fun[transfer_rule]:
kuncar@56518
   339
  "\<lbrakk>left_total A; left_unique B\<rbrakk> \<Longrightarrow> left_unique (A ===> B)"
kuncar@56518
   340
  unfolding left_total_def left_unique_def rel_fun_def
kuncar@56518
   341
  by (clarify, rule ext, fast)
kuncar@56518
   342
huffman@47325
   343
lemma right_total_fun [transfer_rule]:
huffman@47325
   344
  "\<lbrakk>right_unique A; right_total B\<rbrakk> \<Longrightarrow> right_total (A ===> B)"
blanchet@55945
   345
  unfolding right_total_def rel_fun_def
huffman@47325
   346
  apply (rule allI, rename_tac g)
huffman@47325
   347
  apply (rule_tac x="\<lambda>x. SOME z. B z (g (THE y. A x y))" in exI)
huffman@47325
   348
  apply clarify
huffman@47325
   349
  apply (subgoal_tac "(THE y. A x y) = y", simp)
huffman@47325
   350
  apply (rule someI_ex)
huffman@47325
   351
  apply (simp)
huffman@47325
   352
  apply (rule the_equality)
huffman@47325
   353
  apply assumption
huffman@47325
   354
  apply (simp add: right_unique_def)
huffman@47325
   355
  done
huffman@47325
   356
huffman@47325
   357
lemma right_unique_fun [transfer_rule]:
huffman@47325
   358
  "\<lbrakk>right_total A; right_unique B\<rbrakk> \<Longrightarrow> right_unique (A ===> B)"
blanchet@55945
   359
  unfolding right_total_def right_unique_def rel_fun_def
huffman@47325
   360
  by (clarify, rule ext, fast)
huffman@47325
   361
kuncar@56518
   362
lemma bi_total_fun[transfer_rule]:
huffman@47325
   363
  "\<lbrakk>bi_unique A; bi_total B\<rbrakk> \<Longrightarrow> bi_total (A ===> B)"
kuncar@56524
   364
  unfolding bi_unique_alt_def bi_total_alt_def
kuncar@56518
   365
  by (blast intro: right_total_fun left_total_fun)
huffman@47325
   366
kuncar@56518
   367
lemma bi_unique_fun[transfer_rule]:
huffman@47325
   368
  "\<lbrakk>bi_total A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (A ===> B)"
kuncar@56524
   369
  unfolding bi_unique_alt_def bi_total_alt_def
kuncar@56518
   370
  by (blast intro: right_unique_fun left_unique_fun)
huffman@47325
   371
kuncar@56543
   372
end
kuncar@56543
   373
desharna@59275
   374
lemma if_conn:
desharna@59275
   375
  "(if P \<and> Q then t else e) = (if P then if Q then t else e else e)"
desharna@59275
   376
  "(if P \<or> Q then t else e) = (if P then t else if Q then t else e)"
desharna@59275
   377
  "(if P \<longrightarrow> Q then t else e) = (if P then if Q then t else e else t)"
desharna@59275
   378
  "(if \<not> P then t else e) = (if P then e else t)"
desharna@59275
   379
by auto
desharna@59275
   380
blanchet@58182
   381
ML_file "Tools/Transfer/transfer_bnf.ML"
desharna@59275
   382
ML_file "Tools/BNF/bnf_fp_rec_sugar_transfer.ML"
desharna@59275
   383
kuncar@56543
   384
declare pred_fun_def [simp]
kuncar@56543
   385
declare rel_fun_eq [relator_eq]
kuncar@56543
   386
huffman@47635
   387
subsection {* Transfer rules *}
huffman@47325
   388
kuncar@56543
   389
context
kuncar@56543
   390
begin
kuncar@56543
   391
interpretation lifting_syntax .
kuncar@56543
   392
kuncar@53952
   393
lemma Domainp_forall_transfer [transfer_rule]:
kuncar@53952
   394
  assumes "right_total A"
kuncar@53952
   395
  shows "((A ===> op =) ===> op =)
kuncar@53952
   396
    (transfer_bforall (Domainp A)) transfer_forall"
kuncar@53952
   397
  using assms unfolding right_total_def
blanchet@55945
   398
  unfolding transfer_forall_def transfer_bforall_def rel_fun_def Domainp_iff
blanchet@56085
   399
  by fast
kuncar@53952
   400
huffman@47684
   401
text {* Transfer rules using implication instead of equality on booleans. *}
huffman@47684
   402
huffman@52354
   403
lemma transfer_forall_transfer [transfer_rule]:
huffman@52354
   404
  "bi_total A \<Longrightarrow> ((A ===> op =) ===> op =) transfer_forall transfer_forall"
huffman@52354
   405
  "right_total A \<Longrightarrow> ((A ===> op =) ===> implies) transfer_forall transfer_forall"
huffman@52354
   406
  "right_total A \<Longrightarrow> ((A ===> implies) ===> implies) transfer_forall transfer_forall"
huffman@52354
   407
  "bi_total A \<Longrightarrow> ((A ===> op =) ===> rev_implies) transfer_forall transfer_forall"
huffman@52354
   408
  "bi_total A \<Longrightarrow> ((A ===> rev_implies) ===> rev_implies) transfer_forall transfer_forall"
blanchet@55945
   409
  unfolding transfer_forall_def rev_implies_def rel_fun_def right_total_def bi_total_def
blanchet@56085
   410
  by fast+
huffman@52354
   411
huffman@52354
   412
lemma transfer_implies_transfer [transfer_rule]:
huffman@52354
   413
  "(op =        ===> op =        ===> op =       ) transfer_implies transfer_implies"
huffman@52354
   414
  "(rev_implies ===> implies     ===> implies    ) transfer_implies transfer_implies"
huffman@52354
   415
  "(rev_implies ===> op =        ===> implies    ) transfer_implies transfer_implies"
huffman@52354
   416
  "(op =        ===> implies     ===> implies    ) transfer_implies transfer_implies"
huffman@52354
   417
  "(op =        ===> op =        ===> implies    ) transfer_implies transfer_implies"
huffman@52354
   418
  "(implies     ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
huffman@52354
   419
  "(implies     ===> op =        ===> rev_implies) transfer_implies transfer_implies"
huffman@52354
   420
  "(op =        ===> rev_implies ===> rev_implies) transfer_implies transfer_implies"
huffman@52354
   421
  "(op =        ===> op =        ===> rev_implies) transfer_implies transfer_implies"
blanchet@55945
   422
  unfolding transfer_implies_def rev_implies_def rel_fun_def by auto
huffman@52354
   423
huffman@47684
   424
lemma eq_imp_transfer [transfer_rule]:
huffman@47684
   425
  "right_unique A \<Longrightarrow> (A ===> A ===> op \<longrightarrow>) (op =) (op =)"
kuncar@56524
   426
  unfolding right_unique_alt_def2 .
huffman@47684
   427
kuncar@56518
   428
text {* Transfer rules using equality. *}
kuncar@56518
   429
kuncar@56518
   430
lemma left_unique_transfer [transfer_rule]:
kuncar@56518
   431
  assumes "right_total A"
kuncar@56518
   432
  assumes "right_total B"
kuncar@56518
   433
  assumes "bi_unique A"
kuncar@56518
   434
  shows "((A ===> B ===> op=) ===> implies) left_unique left_unique"
kuncar@56518
   435
using assms unfolding left_unique_def[abs_def] right_total_def bi_unique_def rel_fun_def
kuncar@56518
   436
by metis
kuncar@56518
   437
huffman@47636
   438
lemma eq_transfer [transfer_rule]:
huffman@47325
   439
  assumes "bi_unique A"
huffman@47325
   440
  shows "(A ===> A ===> op =) (op =) (op =)"
blanchet@55945
   441
  using assms unfolding bi_unique_def rel_fun_def by auto
huffman@47325
   442
kuncar@51956
   443
lemma right_total_Ex_transfer[transfer_rule]:
kuncar@51956
   444
  assumes "right_total A"
kuncar@51956
   445
  shows "((A ===> op=) ===> op=) (Bex (Collect (Domainp A))) Ex"
blanchet@55945
   446
using assms unfolding right_total_def Bex_def rel_fun_def Domainp_iff[abs_def]
blanchet@56085
   447
by fast
kuncar@51956
   448
kuncar@51956
   449
lemma right_total_All_transfer[transfer_rule]:
kuncar@51956
   450
  assumes "right_total A"
kuncar@51956
   451
  shows "((A ===> op =) ===> op =) (Ball (Collect (Domainp A))) All"
blanchet@55945
   452
using assms unfolding right_total_def Ball_def rel_fun_def Domainp_iff[abs_def]
blanchet@56085
   453
by fast
kuncar@51956
   454
huffman@47636
   455
lemma All_transfer [transfer_rule]:
huffman@47325
   456
  assumes "bi_total A"
huffman@47325
   457
  shows "((A ===> op =) ===> op =) All All"
blanchet@55945
   458
  using assms unfolding bi_total_def rel_fun_def by fast
huffman@47325
   459
huffman@47636
   460
lemma Ex_transfer [transfer_rule]:
huffman@47325
   461
  assumes "bi_total A"
huffman@47325
   462
  shows "((A ===> op =) ===> op =) Ex Ex"
blanchet@55945
   463
  using assms unfolding bi_total_def rel_fun_def by fast
huffman@47325
   464
Andreas@59515
   465
lemma Ex1_parametric [transfer_rule]:
Andreas@59515
   466
  assumes [transfer_rule]: "bi_unique A" "bi_total A"
Andreas@59515
   467
  shows "((A ===> op =) ===> op =) Ex1 Ex1"
Andreas@59515
   468
unfolding Ex1_def[abs_def] by transfer_prover
Andreas@59515
   469
desharna@58448
   470
declare If_transfer [transfer_rule]
huffman@47325
   471
huffman@47636
   472
lemma Let_transfer [transfer_rule]: "(A ===> (A ===> B) ===> B) Let Let"
blanchet@55945
   473
  unfolding rel_fun_def by simp
huffman@47612
   474
traytel@58916
   475
declare id_transfer [transfer_rule]
huffman@47625
   476
desharna@58444
   477
declare comp_transfer [transfer_rule]
huffman@47325
   478
traytel@58916
   479
lemma curry_transfer [transfer_rule]:
traytel@58916
   480
  "((rel_prod A B ===> C) ===> A ===> B ===> C) curry curry"
traytel@58916
   481
  unfolding curry_def by transfer_prover
traytel@58916
   482
huffman@47636
   483
lemma fun_upd_transfer [transfer_rule]:
huffman@47325
   484
  assumes [transfer_rule]: "bi_unique A"
huffman@47325
   485
  shows "((A ===> B) ===> A ===> B ===> A ===> B) fun_upd fun_upd"
huffman@47635
   486
  unfolding fun_upd_def [abs_def] by transfer_prover
huffman@47325
   487
blanchet@55415
   488
lemma case_nat_transfer [transfer_rule]:
blanchet@55415
   489
  "(A ===> (op = ===> A) ===> op = ===> A) case_nat case_nat"
blanchet@55945
   490
  unfolding rel_fun_def by (simp split: nat.split)
huffman@47627
   491
blanchet@55415
   492
lemma rec_nat_transfer [transfer_rule]:
blanchet@55415
   493
  "(A ===> (op = ===> A ===> A) ===> op = ===> A) rec_nat rec_nat"
blanchet@55945
   494
  unfolding rel_fun_def by (clarsimp, rename_tac n, induct_tac n, simp_all)
huffman@47924
   495
huffman@47924
   496
lemma funpow_transfer [transfer_rule]:
huffman@47924
   497
  "(op = ===> (A ===> A) ===> (A ===> A)) compow compow"
huffman@47924
   498
  unfolding funpow_def by transfer_prover
huffman@47924
   499
kuncar@53952
   500
lemma mono_transfer[transfer_rule]:
kuncar@53952
   501
  assumes [transfer_rule]: "bi_total A"
kuncar@53952
   502
  assumes [transfer_rule]: "(A ===> A ===> op=) op\<le> op\<le>"
kuncar@53952
   503
  assumes [transfer_rule]: "(B ===> B ===> op=) op\<le> op\<le>"
kuncar@53952
   504
  shows "((A ===> B) ===> op=) mono mono"
kuncar@53952
   505
unfolding mono_def[abs_def] by transfer_prover
kuncar@53952
   506
blanchet@58182
   507
lemma right_total_relcompp_transfer[transfer_rule]:
kuncar@53952
   508
  assumes [transfer_rule]: "right_total B"
blanchet@58182
   509
  shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=)
kuncar@53952
   510
    (\<lambda>R S x z. \<exists>y\<in>Collect (Domainp B). R x y \<and> S y z) op OO"
kuncar@53952
   511
unfolding OO_def[abs_def] by transfer_prover
kuncar@53952
   512
blanchet@58182
   513
lemma relcompp_transfer[transfer_rule]:
kuncar@53952
   514
  assumes [transfer_rule]: "bi_total B"
kuncar@53952
   515
  shows "((A ===> B ===> op=) ===> (B ===> C ===> op=) ===> A ===> C ===> op=) op OO op OO"
kuncar@53952
   516
unfolding OO_def[abs_def] by transfer_prover
huffman@47627
   517
kuncar@53952
   518
lemma right_total_Domainp_transfer[transfer_rule]:
kuncar@53952
   519
  assumes [transfer_rule]: "right_total B"
kuncar@53952
   520
  shows "((A ===> B ===> op=) ===> A ===> op=) (\<lambda>T x. \<exists>y\<in>Collect(Domainp B). T x y) Domainp"
kuncar@53952
   521
apply(subst(2) Domainp_iff[abs_def]) by transfer_prover
kuncar@53952
   522
kuncar@53952
   523
lemma Domainp_transfer[transfer_rule]:
kuncar@53952
   524
  assumes [transfer_rule]: "bi_total B"
kuncar@53952
   525
  shows "((A ===> B ===> op=) ===> A ===> op=) Domainp Domainp"
kuncar@53952
   526
unfolding Domainp_iff[abs_def] by transfer_prover
kuncar@53952
   527
blanchet@58182
   528
lemma reflp_transfer[transfer_rule]:
kuncar@53952
   529
  "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> op=) reflp reflp"
kuncar@53952
   530
  "right_total A \<Longrightarrow> ((A ===> A ===> implies) ===> implies) reflp reflp"
kuncar@53952
   531
  "right_total A \<Longrightarrow> ((A ===> A ===> op=) ===> implies) reflp reflp"
kuncar@53952
   532
  "bi_total A \<Longrightarrow> ((A ===> A ===> rev_implies) ===> rev_implies) reflp reflp"
kuncar@53952
   533
  "bi_total A \<Longrightarrow> ((A ===> A ===> op=) ===> rev_implies) reflp reflp"
blanchet@58182
   534
using assms unfolding reflp_def[abs_def] rev_implies_def bi_total_def right_total_def rel_fun_def
kuncar@53952
   535
by fast+
kuncar@53952
   536
kuncar@53952
   537
lemma right_unique_transfer [transfer_rule]:
Andreas@59523
   538
  "\<lbrakk> right_total A; right_total B; bi_unique B \<rbrakk>
Andreas@59523
   539
  \<Longrightarrow> ((A ===> B ===> op=) ===> implies) right_unique right_unique"
Andreas@59523
   540
unfolding right_unique_def[abs_def] right_total_def bi_unique_def rel_fun_def
kuncar@53952
   541
by metis
huffman@47325
   542
Andreas@59523
   543
lemma left_total_parametric [transfer_rule]:
Andreas@59523
   544
  assumes [transfer_rule]: "bi_total A" "bi_total B"
Andreas@59523
   545
  shows "((A ===> B ===> op =) ===> op =) left_total left_total"
Andreas@59523
   546
unfolding left_total_def[abs_def] by transfer_prover
Andreas@59523
   547
Andreas@59523
   548
lemma right_total_parametric [transfer_rule]:
Andreas@59523
   549
  assumes [transfer_rule]: "bi_total A" "bi_total B"
Andreas@59523
   550
  shows "((A ===> B ===> op =) ===> op =) right_total right_total"
Andreas@59523
   551
unfolding right_total_def[abs_def] by transfer_prover
Andreas@59523
   552
Andreas@59523
   553
lemma left_unique_parametric [transfer_rule]:
Andreas@59523
   554
  assumes [transfer_rule]: "bi_unique A" "bi_total A" "bi_total B"
Andreas@59523
   555
  shows "((A ===> B ===> op =) ===> op =) left_unique left_unique"
Andreas@59523
   556
unfolding left_unique_def[abs_def] by transfer_prover
Andreas@59523
   557
Andreas@59523
   558
lemma prod_pred_parametric [transfer_rule]:
Andreas@59523
   559
  "((A ===> op =) ===> (B ===> op =) ===> rel_prod A B ===> op =) pred_prod pred_prod"
Andreas@59523
   560
unfolding pred_prod_def[abs_def] Basic_BNFs.fsts_def Basic_BNFs.snds_def fstsp.simps sndsp.simps 
Andreas@59523
   561
by simp transfer_prover
Andreas@59523
   562
Andreas@59523
   563
lemma apfst_parametric [transfer_rule]:
Andreas@59523
   564
  "((A ===> B) ===> rel_prod A C ===> rel_prod B C) apfst apfst"
Andreas@59523
   565
unfolding apfst_def[abs_def] by transfer_prover
Andreas@59523
   566
kuncar@56524
   567
lemma rel_fun_eq_eq_onp: "(op= ===> eq_onp P) = eq_onp (\<lambda>f. \<forall>x. P(f x))"
kuncar@56524
   568
unfolding eq_onp_def rel_fun_def by auto
kuncar@56524
   569
kuncar@56524
   570
lemma rel_fun_eq_onp_rel:
kuncar@56524
   571
  shows "((eq_onp R) ===> S) = (\<lambda>f g. \<forall>x. R x \<longrightarrow> S (f x) (g x))"
kuncar@56524
   572
by (auto simp add: eq_onp_def rel_fun_def)
kuncar@56524
   573
kuncar@56524
   574
lemma eq_onp_transfer [transfer_rule]:
kuncar@56524
   575
  assumes [transfer_rule]: "bi_unique A"
kuncar@56524
   576
  shows "((A ===> op=) ===> A ===> A ===> op=) eq_onp eq_onp"
kuncar@56524
   577
unfolding eq_onp_def[abs_def] by transfer_prover
kuncar@56524
   578
Andreas@57599
   579
lemma rtranclp_parametric [transfer_rule]:
Andreas@57599
   580
  assumes "bi_unique A" "bi_total A"
Andreas@57599
   581
  shows "((A ===> A ===> op =) ===> A ===> A ===> op =) rtranclp rtranclp"
Andreas@57599
   582
proof(rule rel_funI iffI)+
Andreas@57599
   583
  fix R :: "'a \<Rightarrow> 'a \<Rightarrow> bool" and R' x y x' y'
Andreas@57599
   584
  assume R: "(A ===> A ===> op =) R R'" and "A x x'"
Andreas@57599
   585
  {
Andreas@57599
   586
    assume "R\<^sup>*\<^sup>* x y" "A y y'"
Andreas@57599
   587
    thus "R'\<^sup>*\<^sup>* x' y'"
Andreas@57599
   588
    proof(induction arbitrary: y')
Andreas@57599
   589
      case base
Andreas@57599
   590
      with `bi_unique A` `A x x'` have "x' = y'" by(rule bi_uniqueDr)
Andreas@57599
   591
      thus ?case by simp
Andreas@57599
   592
    next
Andreas@57599
   593
      case (step y z z')
Andreas@57599
   594
      from `bi_total A` obtain y' where "A y y'" unfolding bi_total_def by blast
Andreas@57599
   595
      hence "R'\<^sup>*\<^sup>* x' y'" by(rule step.IH)
Andreas@57599
   596
      moreover from R `A y y'` `A z z'` `R y z`
Andreas@57599
   597
      have "R' y' z'" by(auto dest: rel_funD)
Andreas@57599
   598
      ultimately show ?case ..
Andreas@57599
   599
    qed
Andreas@57599
   600
  next
Andreas@57599
   601
    assume "R'\<^sup>*\<^sup>* x' y'" "A y y'"
Andreas@57599
   602
    thus "R\<^sup>*\<^sup>* x y"
Andreas@57599
   603
    proof(induction arbitrary: y)
Andreas@57599
   604
      case base
Andreas@57599
   605
      with `bi_unique A` `A x x'` have "x = y" by(rule bi_uniqueDl)
Andreas@57599
   606
      thus ?case by simp
Andreas@57599
   607
    next
Andreas@57599
   608
      case (step y' z' z)
Andreas@57599
   609
      from `bi_total A` obtain y where "A y y'" unfolding bi_total_def by blast
Andreas@57599
   610
      hence "R\<^sup>*\<^sup>* x y" by(rule step.IH)
Andreas@57599
   611
      moreover from R `A y y'` `A z z'` `R' y' z'`
Andreas@57599
   612
      have "R y z" by(auto dest: rel_funD)
Andreas@57599
   613
      ultimately show ?case ..
Andreas@57599
   614
    qed
Andreas@57599
   615
  }
Andreas@57599
   616
qed
Andreas@57599
   617
Andreas@59523
   618
lemma right_unique_parametric [transfer_rule]:
Andreas@59523
   619
  assumes [transfer_rule]: "bi_total A" "bi_unique B" "bi_total B"
Andreas@59523
   620
  shows "((A ===> B ===> op =) ===> op =) right_unique right_unique"
Andreas@59523
   621
unfolding right_unique_def[abs_def] by transfer_prover
Andreas@59523
   622
huffman@47325
   623
end
kuncar@53011
   624
kuncar@53011
   625
end