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