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