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