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