src/HOL/Nat_Transfer.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 64272 f76b6dda2e56
child 66795 420d0080545f
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
haftmann@32554
     1
(* Authors: Jeremy Avigad and Amine Chaieb *)
huffman@31708
     2
wenzelm@60758
     3
section \<open>Generic transfer machinery;  specific transfer from nats to ints and back.\<close>
huffman@31708
     4
haftmann@32558
     5
theory Nat_Transfer
huffman@47255
     6
imports Int
huffman@31708
     7
begin
huffman@31708
     8
wenzelm@60758
     9
subsection \<open>Generic transfer machinery\<close>
haftmann@33318
    10
haftmann@35821
    11
definition transfer_morphism:: "('b \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> bool"
krauss@42870
    12
  where "transfer_morphism f A \<longleftrightarrow> True"
haftmann@35644
    13
krauss@42870
    14
lemma transfer_morphismI[intro]: "transfer_morphism f A"
krauss@42870
    15
  by (simp add: transfer_morphism_def)
haftmann@33318
    16
wenzelm@48891
    17
ML_file "Tools/legacy_transfer.ML"
haftmann@33318
    18
haftmann@33318
    19
wenzelm@60758
    20
subsection \<open>Set up transfer from nat to int\<close>
huffman@31708
    21
wenzelm@60758
    22
text \<open>set up transfer direction\<close>
huffman@31708
    23
krauss@42870
    24
lemma transfer_morphism_nat_int: "transfer_morphism nat (op <= (0::int))" ..
huffman@31708
    25
haftmann@35683
    26
declare transfer_morphism_nat_int [transfer add
haftmann@35683
    27
  mode: manual
huffman@31708
    28
  return: nat_0_le
haftmann@35683
    29
  labels: nat_int
huffman@31708
    30
]
huffman@31708
    31
wenzelm@60758
    32
text \<open>basic functions and relations\<close>
huffman@31708
    33
haftmann@35683
    34
lemma transfer_nat_int_numerals [transfer key: transfer_morphism_nat_int]:
huffman@31708
    35
    "(0::nat) = nat 0"
huffman@31708
    36
    "(1::nat) = nat 1"
huffman@31708
    37
    "(2::nat) = nat 2"
huffman@31708
    38
    "(3::nat) = nat 3"
huffman@31708
    39
  by auto
huffman@31708
    40
huffman@31708
    41
definition
huffman@31708
    42
  tsub :: "int \<Rightarrow> int \<Rightarrow> int"
huffman@31708
    43
where
huffman@31708
    44
  "tsub x y = (if x >= y then x - y else 0)"
huffman@31708
    45
huffman@31708
    46
lemma tsub_eq: "x >= y \<Longrightarrow> tsub x y = x - y"
huffman@31708
    47
  by (simp add: tsub_def)
huffman@31708
    48
haftmann@35683
    49
lemma transfer_nat_int_functions [transfer key: transfer_morphism_nat_int]:
huffman@31708
    50
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) + (nat y) = nat (x + y)"
huffman@31708
    51
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) * (nat y) = nat (x * y)"
huffman@31708
    52
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) - (nat y) = nat (tsub x y)"
huffman@31708
    53
    "(x::int) >= 0 \<Longrightarrow> (nat x)^n = nat (x^n)"
huffman@31708
    54
  by (auto simp add: eq_nat_nat_iff nat_mult_distrib
haftmann@33318
    55
      nat_power_eq tsub_def)
huffman@31708
    56
haftmann@35683
    57
lemma transfer_nat_int_function_closures [transfer key: transfer_morphism_nat_int]:
huffman@31708
    58
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x + y >= 0"
huffman@31708
    59
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x * y >= 0"
huffman@31708
    60
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> tsub x y >= 0"
huffman@31708
    61
    "(x::int) >= 0 \<Longrightarrow> x^n >= 0"
huffman@31708
    62
    "(0::int) >= 0"
huffman@31708
    63
    "(1::int) >= 0"
huffman@31708
    64
    "(2::int) >= 0"
huffman@31708
    65
    "(3::int) >= 0"
huffman@31708
    66
    "int z >= 0"
haftmann@33340
    67
  by (auto simp add: zero_le_mult_iff tsub_def)
huffman@31708
    68
haftmann@35683
    69
lemma transfer_nat_int_relations [transfer key: transfer_morphism_nat_int]:
huffman@31708
    70
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    71
      (nat (x::int) = nat y) = (x = y)"
huffman@31708
    72
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    73
      (nat (x::int) < nat y) = (x < y)"
huffman@31708
    74
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    75
      (nat (x::int) <= nat y) = (x <= y)"
huffman@31708
    76
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    77
      (nat (x::int) dvd nat y) = (x dvd y)"
haftmann@32558
    78
  by (auto simp add: zdvd_int)
huffman@31708
    79
huffman@31708
    80
wenzelm@60758
    81
text \<open>first-order quantifiers\<close>
haftmann@33318
    82
haftmann@33318
    83
lemma all_nat: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x\<ge>0. P (nat x))"
nipkow@63648
    84
  by (simp split: split_nat)
haftmann@33318
    85
haftmann@33318
    86
lemma ex_nat: "(\<exists>x. P x) \<longleftrightarrow> (\<exists>x. 0 \<le> x \<and> P (nat x))"
haftmann@33318
    87
proof
haftmann@33318
    88
  assume "\<exists>x. P x"
haftmann@33318
    89
  then obtain x where "P x" ..
haftmann@33318
    90
  then have "int x \<ge> 0 \<and> P (nat (int x))" by simp
haftmann@33318
    91
  then show "\<exists>x\<ge>0. P (nat x)" ..
haftmann@33318
    92
next
haftmann@33318
    93
  assume "\<exists>x\<ge>0. P (nat x)"
haftmann@33318
    94
  then show "\<exists>x. P x" by auto
haftmann@33318
    95
qed
huffman@31708
    96
haftmann@35683
    97
lemma transfer_nat_int_quantifiers [transfer key: transfer_morphism_nat_int]:
huffman@31708
    98
    "(ALL (x::nat). P x) = (ALL (x::int). x >= 0 \<longrightarrow> P (nat x))"
huffman@31708
    99
    "(EX (x::nat). P x) = (EX (x::int). x >= 0 & P (nat x))"
huffman@31708
   100
  by (rule all_nat, rule ex_nat)
huffman@31708
   101
huffman@31708
   102
(* should we restrict these? *)
huffman@31708
   103
lemma all_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   104
    (ALL x. Q x \<longrightarrow> P x) = (ALL x. Q x \<longrightarrow> P' x)"
huffman@31708
   105
  by auto
huffman@31708
   106
huffman@31708
   107
lemma ex_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   108
    (EX x. Q x \<and> P x) = (EX x. Q x \<and> P' x)"
huffman@31708
   109
  by auto
huffman@31708
   110
haftmann@35644
   111
declare transfer_morphism_nat_int [transfer add
huffman@31708
   112
  cong: all_cong ex_cong]
huffman@31708
   113
huffman@31708
   114
wenzelm@60758
   115
text \<open>if\<close>
huffman@31708
   116
haftmann@35683
   117
lemma nat_if_cong [transfer key: transfer_morphism_nat_int]:
haftmann@35683
   118
  "(if P then (nat x) else (nat y)) = nat (if P then x else y)"
huffman@31708
   119
  by auto
huffman@31708
   120
huffman@31708
   121
wenzelm@60758
   122
text \<open>operations with sets\<close>
huffman@31708
   123
huffman@31708
   124
definition
huffman@31708
   125
  nat_set :: "int set \<Rightarrow> bool"
huffman@31708
   126
where
huffman@31708
   127
  "nat_set S = (ALL x:S. x >= 0)"
huffman@31708
   128
huffman@31708
   129
lemma transfer_nat_int_set_functions:
huffman@31708
   130
    "card A = card (int ` A)"
huffman@31708
   131
    "{} = nat ` ({}::int set)"
huffman@31708
   132
    "A Un B = nat ` (int ` A Un int ` B)"
huffman@31708
   133
    "A Int B = nat ` (int ` A Int int ` B)"
huffman@31708
   134
    "{x. P x} = nat ` {x. x >= 0 & P(nat x)}"
huffman@31708
   135
  apply (rule card_image [symmetric])
huffman@31708
   136
  apply (auto simp add: inj_on_def image_def)
huffman@31708
   137
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   138
  apply auto
huffman@31708
   139
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   140
  apply auto
huffman@31708
   141
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   142
  apply auto
huffman@31708
   143
  apply (rule_tac x = "int x" in exI)
huffman@31708
   144
  apply auto
huffman@31708
   145
done
huffman@31708
   146
huffman@31708
   147
lemma transfer_nat_int_set_function_closures:
huffman@31708
   148
    "nat_set {}"
huffman@31708
   149
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
huffman@31708
   150
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
huffman@31708
   151
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   152
    "nat_set (int ` C)"
huffman@31708
   153
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> x >= 0" (* does it hurt to turn this on? *)
huffman@31708
   154
  unfolding nat_set_def apply auto
huffman@31708
   155
done
huffman@31708
   156
huffman@31708
   157
lemma transfer_nat_int_set_relations:
huffman@31708
   158
    "(finite A) = (finite (int ` A))"
huffman@31708
   159
    "(x : A) = (int x : int ` A)"
huffman@31708
   160
    "(A = B) = (int ` A = int ` B)"
huffman@31708
   161
    "(A < B) = (int ` A < int ` B)"
huffman@31708
   162
    "(A <= B) = (int ` A <= int ` B)"
huffman@31708
   163
  apply (rule iffI)
huffman@31708
   164
  apply (erule finite_imageI)
huffman@31708
   165
  apply (erule finite_imageD)
nipkow@39302
   166
  apply (auto simp add: image_def set_eq_iff inj_on_def)
huffman@31708
   167
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   168
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   169
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   170
done
huffman@31708
   171
huffman@31708
   172
lemma transfer_nat_int_set_return_embed: "nat_set A \<Longrightarrow>
huffman@31708
   173
    (int ` nat ` A = A)"
huffman@31708
   174
  by (auto simp add: nat_set_def image_def)
huffman@31708
   175
huffman@31708
   176
lemma transfer_nat_int_set_cong: "(!!x. x >= 0 \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   177
    {(x::int). x >= 0 & P x} = {x. x >= 0 & P' x}"
huffman@31708
   178
  by auto
huffman@31708
   179
haftmann@35644
   180
declare transfer_morphism_nat_int [transfer add
huffman@31708
   181
  return: transfer_nat_int_set_functions
huffman@31708
   182
    transfer_nat_int_set_function_closures
huffman@31708
   183
    transfer_nat_int_set_relations
huffman@31708
   184
    transfer_nat_int_set_return_embed
huffman@31708
   185
  cong: transfer_nat_int_set_cong
huffman@31708
   186
]
huffman@31708
   187
huffman@31708
   188
nipkow@64272
   189
text \<open>sum and prod\<close>
huffman@31708
   190
huffman@31708
   191
(* this handles the case where the *domain* of f is nat *)
huffman@31708
   192
lemma transfer_nat_int_sum_prod:
nipkow@64267
   193
    "sum f A = sum (%x. f (nat x)) (int ` A)"
nipkow@64272
   194
    "prod f A = prod (%x. f (nat x)) (int ` A)"
nipkow@64267
   195
  apply (subst sum.reindex)
huffman@31708
   196
  apply (unfold inj_on_def, auto)
nipkow@64272
   197
  apply (subst prod.reindex)
huffman@31708
   198
  apply (unfold inj_on_def o_def, auto)
huffman@31708
   199
done
huffman@31708
   200
huffman@31708
   201
(* this handles the case where the *range* of f is nat *)
huffman@31708
   202
lemma transfer_nat_int_sum_prod2:
nipkow@64267
   203
    "sum f A = nat(sum (%x. int (f x)) A)"
nipkow@64272
   204
    "prod f A = nat(prod (%x. int (f x)) A)"
nipkow@64267
   205
  apply (simp only: int_sum [symmetric] nat_int)
nipkow@64272
   206
  apply (simp only: int_prod [symmetric] nat_int)
lp15@61649
   207
  done
huffman@31708
   208
huffman@31708
   209
lemma transfer_nat_int_sum_prod_closure:
nipkow@64267
   210
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> sum f A >= 0"
nipkow@64272
   211
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> prod f A >= 0"
huffman@31708
   212
  unfolding nat_set_def
nipkow@64267
   213
  apply (rule sum_nonneg)
huffman@31708
   214
  apply auto
nipkow@64272
   215
  apply (rule prod_nonneg)
huffman@31708
   216
  apply auto
huffman@31708
   217
done
huffman@31708
   218
huffman@31708
   219
(* this version doesn't work, even with nat_set A \<Longrightarrow>
huffman@31708
   220
      x : A \<Longrightarrow> x >= 0 turned on. Why not?
huffman@31708
   221
huffman@31708
   222
  also: what does =simp=> do?
huffman@31708
   223
huffman@31708
   224
lemma transfer_nat_int_sum_prod_closure:
nipkow@64267
   225
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> sum f A >= 0"
nipkow@64272
   226
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> prod f A >= 0"
huffman@31708
   227
  unfolding nat_set_def simp_implies_def
nipkow@64267
   228
  apply (rule sum_nonneg)
huffman@31708
   229
  apply auto
nipkow@64272
   230
  apply (rule prod_nonneg)
huffman@31708
   231
  apply auto
huffman@31708
   232
done
huffman@31708
   233
*)
huffman@31708
   234
huffman@31708
   235
(* Making A = B in this lemma doesn't work. Why not?
nipkow@64272
   236
   Also, why aren't sum.cong and prod.cong enough,
huffman@31708
   237
   with the previously mentioned rule turned on? *)
huffman@31708
   238
huffman@31708
   239
lemma transfer_nat_int_sum_prod_cong:
huffman@31708
   240
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
nipkow@64267
   241
      sum f A = sum g B"
huffman@31708
   242
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
nipkow@64272
   243
      prod f A = prod g B"
huffman@31708
   244
  unfolding nat_set_def
nipkow@64267
   245
  apply (subst sum.cong, assumption)
huffman@31708
   246
  apply auto [2]
nipkow@64272
   247
  apply (subst prod.cong, assumption, auto)
huffman@31708
   248
done
huffman@31708
   249
haftmann@35644
   250
declare transfer_morphism_nat_int [transfer add
huffman@31708
   251
  return: transfer_nat_int_sum_prod transfer_nat_int_sum_prod2
huffman@31708
   252
    transfer_nat_int_sum_prod_closure
huffman@31708
   253
  cong: transfer_nat_int_sum_prod_cong]
huffman@31708
   254
huffman@31708
   255
wenzelm@60758
   256
subsection \<open>Set up transfer from int to nat\<close>
huffman@31708
   257
wenzelm@60758
   258
text \<open>set up transfer direction\<close>
huffman@31708
   259
krauss@42870
   260
lemma transfer_morphism_int_nat: "transfer_morphism int (\<lambda>n. True)" ..
huffman@31708
   261
haftmann@35644
   262
declare transfer_morphism_int_nat [transfer add
huffman@31708
   263
  mode: manual
huffman@31708
   264
  return: nat_int
haftmann@35683
   265
  labels: int_nat
huffman@31708
   266
]
huffman@31708
   267
huffman@31708
   268
wenzelm@60758
   269
text \<open>basic functions and relations\<close>
haftmann@33318
   270
huffman@31708
   271
definition
huffman@31708
   272
  is_nat :: "int \<Rightarrow> bool"
huffman@31708
   273
where
huffman@31708
   274
  "is_nat x = (x >= 0)"
huffman@31708
   275
huffman@31708
   276
lemma transfer_int_nat_numerals:
huffman@31708
   277
    "0 = int 0"
huffman@31708
   278
    "1 = int 1"
huffman@31708
   279
    "2 = int 2"
huffman@31708
   280
    "3 = int 3"
huffman@31708
   281
  by auto
huffman@31708
   282
huffman@31708
   283
lemma transfer_int_nat_functions:
huffman@31708
   284
    "(int x) + (int y) = int (x + y)"
huffman@31708
   285
    "(int x) * (int y) = int (x * y)"
huffman@31708
   286
    "tsub (int x) (int y) = int (x - y)"
huffman@31708
   287
    "(int x)^n = int (x^n)"
haftmann@62348
   288
  by (auto simp add: of_nat_mult tsub_def of_nat_power)
huffman@31708
   289
huffman@31708
   290
lemma transfer_int_nat_function_closures:
huffman@31708
   291
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x + y)"
huffman@31708
   292
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x * y)"
huffman@31708
   293
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (tsub x y)"
huffman@31708
   294
    "is_nat x \<Longrightarrow> is_nat (x^n)"
huffman@31708
   295
    "is_nat 0"
huffman@31708
   296
    "is_nat 1"
huffman@31708
   297
    "is_nat 2"
huffman@31708
   298
    "is_nat 3"
huffman@31708
   299
    "is_nat (int z)"
huffman@31708
   300
  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
huffman@31708
   301
huffman@31708
   302
lemma transfer_int_nat_relations:
huffman@31708
   303
    "(int x = int y) = (x = y)"
huffman@31708
   304
    "(int x < int y) = (x < y)"
huffman@31708
   305
    "(int x <= int y) = (x <= y)"
huffman@31708
   306
    "(int x dvd int y) = (x dvd y)"
haftmann@33318
   307
  by (auto simp add: zdvd_int)
haftmann@32121
   308
haftmann@35644
   309
declare transfer_morphism_int_nat [transfer add return:
huffman@31708
   310
  transfer_int_nat_numerals
huffman@31708
   311
  transfer_int_nat_functions
huffman@31708
   312
  transfer_int_nat_function_closures
huffman@31708
   313
  transfer_int_nat_relations
huffman@31708
   314
]
huffman@31708
   315
huffman@31708
   316
wenzelm@60758
   317
text \<open>first-order quantifiers\<close>
huffman@31708
   318
huffman@31708
   319
lemma transfer_int_nat_quantifiers:
huffman@31708
   320
    "(ALL (x::int) >= 0. P x) = (ALL (x::nat). P (int x))"
huffman@31708
   321
    "(EX (x::int) >= 0. P x) = (EX (x::nat). P (int x))"
huffman@31708
   322
  apply (subst all_nat)
huffman@31708
   323
  apply auto [1]
huffman@31708
   324
  apply (subst ex_nat)
huffman@31708
   325
  apply auto
huffman@31708
   326
done
huffman@31708
   327
haftmann@35644
   328
declare transfer_morphism_int_nat [transfer add
huffman@31708
   329
  return: transfer_int_nat_quantifiers]
huffman@31708
   330
huffman@31708
   331
wenzelm@60758
   332
text \<open>if\<close>
huffman@31708
   333
huffman@31708
   334
lemma int_if_cong: "(if P then (int x) else (int y)) =
huffman@31708
   335
    int (if P then x else y)"
huffman@31708
   336
  by auto
huffman@31708
   337
haftmann@35644
   338
declare transfer_morphism_int_nat [transfer add return: int_if_cong]
huffman@31708
   339
huffman@31708
   340
huffman@31708
   341
wenzelm@60758
   342
text \<open>operations with sets\<close>
huffman@31708
   343
huffman@31708
   344
lemma transfer_int_nat_set_functions:
huffman@31708
   345
    "nat_set A \<Longrightarrow> card A = card (nat ` A)"
huffman@31708
   346
    "{} = int ` ({}::nat set)"
huffman@31708
   347
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Un B = int ` (nat ` A Un nat ` B)"
huffman@31708
   348
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Int B = int ` (nat ` A Int nat ` B)"
huffman@31708
   349
    "{x. x >= 0 & P x} = int ` {x. P(int x)}"
huffman@31708
   350
       (* need all variants of these! *)
huffman@31708
   351
  by (simp_all only: is_nat_def transfer_nat_int_set_functions
huffman@31708
   352
          transfer_nat_int_set_function_closures
huffman@31708
   353
          transfer_nat_int_set_return_embed nat_0_le
huffman@31708
   354
          cong: transfer_nat_int_set_cong)
huffman@31708
   355
huffman@31708
   356
lemma transfer_int_nat_set_function_closures:
huffman@31708
   357
    "nat_set {}"
huffman@31708
   358
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
huffman@31708
   359
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
huffman@31708
   360
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   361
    "nat_set (int ` C)"
huffman@31708
   362
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> is_nat x"
huffman@31708
   363
  by (simp_all only: transfer_nat_int_set_function_closures is_nat_def)
huffman@31708
   364
huffman@31708
   365
lemma transfer_int_nat_set_relations:
huffman@31708
   366
    "nat_set A \<Longrightarrow> finite A = finite (nat ` A)"
huffman@31708
   367
    "is_nat x \<Longrightarrow> nat_set A \<Longrightarrow> (x : A) = (nat x : nat ` A)"
huffman@31708
   368
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A = B) = (nat ` A = nat ` B)"
huffman@31708
   369
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A < B) = (nat ` A < nat ` B)"
huffman@31708
   370
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A <= B) = (nat ` A <= nat ` B)"
huffman@31708
   371
  by (simp_all only: is_nat_def transfer_nat_int_set_relations
huffman@31708
   372
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   373
huffman@31708
   374
lemma transfer_int_nat_set_return_embed: "nat ` int ` A = A"
huffman@31708
   375
  by (simp only: transfer_nat_int_set_relations
huffman@31708
   376
    transfer_nat_int_set_function_closures
huffman@31708
   377
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   378
huffman@31708
   379
lemma transfer_int_nat_set_cong: "(!!x. P x = P' x) \<Longrightarrow>
huffman@31708
   380
    {(x::nat). P x} = {x. P' x}"
huffman@31708
   381
  by auto
huffman@31708
   382
haftmann@35644
   383
declare transfer_morphism_int_nat [transfer add
huffman@31708
   384
  return: transfer_int_nat_set_functions
huffman@31708
   385
    transfer_int_nat_set_function_closures
huffman@31708
   386
    transfer_int_nat_set_relations
huffman@31708
   387
    transfer_int_nat_set_return_embed
huffman@31708
   388
  cong: transfer_int_nat_set_cong
huffman@31708
   389
]
huffman@31708
   390
huffman@31708
   391
nipkow@64272
   392
text \<open>sum and prod\<close>
huffman@31708
   393
huffman@31708
   394
(* this handles the case where the *domain* of f is int *)
huffman@31708
   395
lemma transfer_int_nat_sum_prod:
nipkow@64267
   396
    "nat_set A \<Longrightarrow> sum f A = sum (%x. f (int x)) (nat ` A)"
nipkow@64272
   397
    "nat_set A \<Longrightarrow> prod f A = prod (%x. f (int x)) (nat ` A)"
nipkow@64267
   398
  apply (subst sum.reindex)
huffman@31708
   399
  apply (unfold inj_on_def nat_set_def, auto simp add: eq_nat_nat_iff)
nipkow@64272
   400
  apply (subst prod.reindex)
huffman@31708
   401
  apply (unfold inj_on_def nat_set_def o_def, auto simp add: eq_nat_nat_iff
nipkow@64272
   402
            cong: prod.cong)
huffman@31708
   403
done
huffman@31708
   404
huffman@31708
   405
(* this handles the case where the *range* of f is int *)
huffman@31708
   406
lemma transfer_int_nat_sum_prod2:
nipkow@64267
   407
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow> sum f A = int(sum (%x. nat (f x)) A)"
huffman@31708
   408
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow>
nipkow@64272
   409
      prod f A = int(prod (%x. nat (f x)) A)"
huffman@31708
   410
  unfolding is_nat_def
nipkow@64267
   411
  by (subst int_sum) auto
huffman@31708
   412
haftmann@35644
   413
declare transfer_morphism_int_nat [transfer add
huffman@31708
   414
  return: transfer_int_nat_sum_prod transfer_int_nat_sum_prod2
nipkow@64272
   415
  cong: sum.cong prod.cong]
huffman@31708
   416
huffman@31708
   417
end