src/HOL/Nat_Transfer.thy
author haftmann
Thu Sep 10 15:26:51 2009 +0200 (2009-09-10)
changeset 32558 e6e1fc2e73cb
parent 32554 src/HOL/NatTransfer.thy@4ccd84fb19d3
child 33318 ddd97d9dfbfb
permissions -rw-r--r--
obey underscore naming convention
huffman@31708
     1
haftmann@32554
     2
(* Authors: Jeremy Avigad and Amine Chaieb *)
huffman@31708
     3
haftmann@32554
     4
header {* Sets up transfer from nats to ints and back. *}
huffman@31708
     5
haftmann@32558
     6
theory Nat_Transfer
huffman@31708
     7
imports Main Parity
huffman@31708
     8
begin
huffman@31708
     9
huffman@31708
    10
subsection {* Set up transfer from nat to int *}
huffman@31708
    11
huffman@31708
    12
(* set up transfer direction *)
huffman@31708
    13
huffman@31708
    14
lemma TransferMorphism_nat_int: "TransferMorphism nat (op <= (0::int))"
huffman@31708
    15
  by (simp add: TransferMorphism_def)
huffman@31708
    16
huffman@31708
    17
declare TransferMorphism_nat_int[transfer
huffman@31708
    18
  add mode: manual
huffman@31708
    19
  return: nat_0_le
huffman@31708
    20
  labels: natint
huffman@31708
    21
]
huffman@31708
    22
huffman@31708
    23
(* basic functions and relations *)
huffman@31708
    24
huffman@31708
    25
lemma transfer_nat_int_numerals:
huffman@31708
    26
    "(0::nat) = nat 0"
huffman@31708
    27
    "(1::nat) = nat 1"
huffman@31708
    28
    "(2::nat) = nat 2"
huffman@31708
    29
    "(3::nat) = nat 3"
huffman@31708
    30
  by auto
huffman@31708
    31
huffman@31708
    32
definition
huffman@31708
    33
  tsub :: "int \<Rightarrow> int \<Rightarrow> int"
huffman@31708
    34
where
huffman@31708
    35
  "tsub x y = (if x >= y then x - y else 0)"
huffman@31708
    36
huffman@31708
    37
lemma tsub_eq: "x >= y \<Longrightarrow> tsub x y = x - y"
huffman@31708
    38
  by (simp add: tsub_def)
huffman@31708
    39
huffman@31708
    40
huffman@31708
    41
lemma transfer_nat_int_functions:
huffman@31708
    42
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) + (nat y) = nat (x + y)"
huffman@31708
    43
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) * (nat y) = nat (x * y)"
huffman@31708
    44
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) - (nat y) = nat (tsub x y)"
huffman@31708
    45
    "(x::int) >= 0 \<Longrightarrow> (nat x)^n = nat (x^n)"
huffman@31708
    46
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
huffman@31708
    47
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
huffman@31708
    48
  by (auto simp add: eq_nat_nat_iff nat_mult_distrib
huffman@31708
    49
      nat_power_eq nat_div_distrib nat_mod_distrib tsub_def)
huffman@31708
    50
huffman@31708
    51
lemma transfer_nat_int_function_closures:
huffman@31708
    52
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x + y >= 0"
huffman@31708
    53
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x * y >= 0"
huffman@31708
    54
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> tsub x y >= 0"
huffman@31708
    55
    "(x::int) >= 0 \<Longrightarrow> x^n >= 0"
huffman@31708
    56
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
huffman@31708
    57
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
huffman@31708
    58
    "(0::int) >= 0"
huffman@31708
    59
    "(1::int) >= 0"
huffman@31708
    60
    "(2::int) >= 0"
huffman@31708
    61
    "(3::int) >= 0"
huffman@31708
    62
    "int z >= 0"
huffman@31708
    63
  apply (auto simp add: zero_le_mult_iff tsub_def)
huffman@31708
    64
  apply (case_tac "y = 0")
huffman@31708
    65
  apply auto
huffman@31708
    66
  apply (subst pos_imp_zdiv_nonneg_iff, auto)
huffman@31708
    67
  apply (case_tac "y = 0")
huffman@31708
    68
  apply force
huffman@31708
    69
  apply (rule pos_mod_sign)
huffman@31708
    70
  apply arith
huffman@31708
    71
done
huffman@31708
    72
huffman@31708
    73
lemma transfer_nat_int_relations:
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) <= nat y) = (x <= y)"
huffman@31708
    80
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    81
      (nat (x::int) dvd nat y) = (x dvd y)"
haftmann@32558
    82
  by (auto simp add: zdvd_int)
huffman@31708
    83
huffman@31708
    84
declare TransferMorphism_nat_int[transfer add return:
huffman@31708
    85
  transfer_nat_int_numerals
huffman@31708
    86
  transfer_nat_int_functions
huffman@31708
    87
  transfer_nat_int_function_closures
huffman@31708
    88
  transfer_nat_int_relations
huffman@31708
    89
]
huffman@31708
    90
huffman@31708
    91
huffman@31708
    92
(* first-order quantifiers *)
huffman@31708
    93
huffman@31708
    94
lemma transfer_nat_int_quantifiers:
huffman@31708
    95
    "(ALL (x::nat). P x) = (ALL (x::int). x >= 0 \<longrightarrow> P (nat x))"
huffman@31708
    96
    "(EX (x::nat). P x) = (EX (x::int). x >= 0 & P (nat x))"
huffman@31708
    97
  by (rule all_nat, rule ex_nat)
huffman@31708
    98
huffman@31708
    99
(* should we restrict these? *)
huffman@31708
   100
lemma all_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   101
    (ALL x. Q x \<longrightarrow> P x) = (ALL x. Q x \<longrightarrow> P' x)"
huffman@31708
   102
  by auto
huffman@31708
   103
huffman@31708
   104
lemma ex_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   105
    (EX x. Q x \<and> P x) = (EX x. Q x \<and> P' x)"
huffman@31708
   106
  by auto
huffman@31708
   107
huffman@31708
   108
declare TransferMorphism_nat_int[transfer add
huffman@31708
   109
  return: transfer_nat_int_quantifiers
huffman@31708
   110
  cong: all_cong ex_cong]
huffman@31708
   111
huffman@31708
   112
huffman@31708
   113
(* if *)
huffman@31708
   114
huffman@31708
   115
lemma nat_if_cong: "(if P then (nat x) else (nat y)) =
huffman@31708
   116
    nat (if P then x else y)"
huffman@31708
   117
  by auto
huffman@31708
   118
huffman@31708
   119
declare TransferMorphism_nat_int [transfer add return: nat_if_cong]
huffman@31708
   120
huffman@31708
   121
huffman@31708
   122
(* operations with sets *)
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
    "{..n} = nat ` {0..int n}"
huffman@31708
   136
    "{m..n} = nat ` {int m..int n}"  (* need all variants of these! *)
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
  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
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
    "x >= 0 \<Longrightarrow> nat_set {x..y}"
huffman@31708
   158
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   159
    "nat_set (int ` C)"
huffman@31708
   160
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> x >= 0" (* does it hurt to turn this on? *)
huffman@31708
   161
  unfolding nat_set_def apply auto
huffman@31708
   162
done
huffman@31708
   163
huffman@31708
   164
lemma transfer_nat_int_set_relations:
huffman@31708
   165
    "(finite A) = (finite (int ` A))"
huffman@31708
   166
    "(x : A) = (int x : int ` A)"
huffman@31708
   167
    "(A = B) = (int ` A = int ` B)"
huffman@31708
   168
    "(A < B) = (int ` A < int ` B)"
huffman@31708
   169
    "(A <= B) = (int ` A <= int ` B)"
huffman@31708
   170
huffman@31708
   171
  apply (rule iffI)
huffman@31708
   172
  apply (erule finite_imageI)
huffman@31708
   173
  apply (erule finite_imageD)
huffman@31708
   174
  apply (auto simp add: image_def expand_set_eq inj_on_def)
huffman@31708
   175
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   176
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   177
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   178
done
huffman@31708
   179
huffman@31708
   180
lemma transfer_nat_int_set_return_embed: "nat_set A \<Longrightarrow>
huffman@31708
   181
    (int ` nat ` A = A)"
huffman@31708
   182
  by (auto simp add: nat_set_def image_def)
huffman@31708
   183
huffman@31708
   184
lemma transfer_nat_int_set_cong: "(!!x. x >= 0 \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   185
    {(x::int). x >= 0 & P x} = {x. x >= 0 & P' x}"
huffman@31708
   186
  by auto
huffman@31708
   187
huffman@31708
   188
declare TransferMorphism_nat_int[transfer add
huffman@31708
   189
  return: transfer_nat_int_set_functions
huffman@31708
   190
    transfer_nat_int_set_function_closures
huffman@31708
   191
    transfer_nat_int_set_relations
huffman@31708
   192
    transfer_nat_int_set_return_embed
huffman@31708
   193
  cong: transfer_nat_int_set_cong
huffman@31708
   194
]
huffman@31708
   195
huffman@31708
   196
huffman@31708
   197
(* setsum and setprod *)
huffman@31708
   198
huffman@31708
   199
(* this handles the case where the *domain* of f is nat *)
huffman@31708
   200
lemma transfer_nat_int_sum_prod:
huffman@31708
   201
    "setsum f A = setsum (%x. f (nat x)) (int ` A)"
huffman@31708
   202
    "setprod f A = setprod (%x. f (nat x)) (int ` A)"
huffman@31708
   203
  apply (subst setsum_reindex)
huffman@31708
   204
  apply (unfold inj_on_def, auto)
huffman@31708
   205
  apply (subst setprod_reindex)
huffman@31708
   206
  apply (unfold inj_on_def o_def, auto)
huffman@31708
   207
done
huffman@31708
   208
huffman@31708
   209
(* this handles the case where the *range* of f is nat *)
huffman@31708
   210
lemma transfer_nat_int_sum_prod2:
huffman@31708
   211
    "setsum f A = nat(setsum (%x. int (f x)) A)"
huffman@31708
   212
    "setprod f A = nat(setprod (%x. int (f x)) A)"
huffman@31708
   213
  apply (subst int_setsum [symmetric])
huffman@31708
   214
  apply auto
huffman@31708
   215
  apply (subst int_setprod [symmetric])
huffman@31708
   216
  apply auto
huffman@31708
   217
done
huffman@31708
   218
huffman@31708
   219
lemma transfer_nat_int_sum_prod_closure:
huffman@31708
   220
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> setsum f A >= 0"
huffman@31708
   221
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> setprod f A >= 0"
huffman@31708
   222
  unfolding nat_set_def
huffman@31708
   223
  apply (rule setsum_nonneg)
huffman@31708
   224
  apply auto
huffman@31708
   225
  apply (rule setprod_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:
huffman@31708
   235
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> setsum f A >= 0"
huffman@31708
   236
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> setprod f A >= 0"
huffman@31708
   237
  unfolding nat_set_def simp_implies_def
huffman@31708
   238
  apply (rule setsum_nonneg)
huffman@31708
   239
  apply auto
huffman@31708
   240
  apply (rule setprod_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?
huffman@31708
   246
   Also, why aren't setsum_cong and setprod_cong enough,
huffman@31708
   247
   with the previously mentioned rule turned on? *)
huffman@31708
   248
huffman@31708
   249
lemma transfer_nat_int_sum_prod_cong:
huffman@31708
   250
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
huffman@31708
   251
      setsum f A = setsum g B"
huffman@31708
   252
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
huffman@31708
   253
      setprod f A = setprod g B"
huffman@31708
   254
  unfolding nat_set_def
huffman@31708
   255
  apply (subst setsum_cong, assumption)
huffman@31708
   256
  apply auto [2]
huffman@31708
   257
  apply (subst setprod_cong, assumption, auto)
huffman@31708
   258
done
huffman@31708
   259
huffman@31708
   260
declare TransferMorphism_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
(* lists *)
huffman@31708
   266
huffman@31708
   267
definition
huffman@31708
   268
  embed_list :: "nat list \<Rightarrow> int list"
huffman@31708
   269
where
huffman@31708
   270
  "embed_list l = map int l";
huffman@31708
   271
huffman@31708
   272
definition
huffman@31708
   273
  nat_list :: "int list \<Rightarrow> bool"
huffman@31708
   274
where
huffman@31708
   275
  "nat_list l = nat_set (set l)";
huffman@31708
   276
huffman@31708
   277
definition
huffman@31708
   278
  return_list :: "int list \<Rightarrow> nat list"
huffman@31708
   279
where
huffman@31708
   280
  "return_list l = map nat l";
huffman@31708
   281
huffman@31708
   282
thm nat_0_le;
huffman@31708
   283
huffman@31708
   284
lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow>
huffman@31708
   285
    embed_list (return_list l) = l";
huffman@31708
   286
  unfolding embed_list_def return_list_def nat_list_def nat_set_def
huffman@31708
   287
  apply (induct l);
huffman@31708
   288
  apply auto;
huffman@31708
   289
done;
huffman@31708
   290
huffman@31708
   291
lemma transfer_nat_int_list_functions:
huffman@31708
   292
  "l @ m = return_list (embed_list l @ embed_list m)"
huffman@31708
   293
  "[] = return_list []";
huffman@31708
   294
  unfolding return_list_def embed_list_def;
huffman@31708
   295
  apply auto;
huffman@31708
   296
  apply (induct l, auto);
huffman@31708
   297
  apply (induct m, auto);
huffman@31708
   298
done;
huffman@31708
   299
huffman@31708
   300
(*
huffman@31708
   301
lemma transfer_nat_int_fold1: "fold f l x =
huffman@31708
   302
    fold (%x. f (nat x)) (embed_list l) x";
huffman@31708
   303
*)
huffman@31708
   304
huffman@31708
   305
huffman@31708
   306
huffman@31708
   307
huffman@31708
   308
subsection {* Set up transfer from int to nat *}
huffman@31708
   309
huffman@31708
   310
(* set up transfer direction *)
huffman@31708
   311
huffman@31708
   312
lemma TransferMorphism_int_nat: "TransferMorphism int (UNIV :: nat set)"
huffman@31708
   313
  by (simp add: TransferMorphism_def)
huffman@31708
   314
huffman@31708
   315
declare TransferMorphism_int_nat[transfer add
huffman@31708
   316
  mode: manual
huffman@31708
   317
(*  labels: int-nat *)
huffman@31708
   318
  return: nat_int
huffman@31708
   319
]
huffman@31708
   320
huffman@31708
   321
huffman@31708
   322
(* basic functions and relations *)
huffman@31708
   323
huffman@31708
   324
definition
huffman@31708
   325
  is_nat :: "int \<Rightarrow> bool"
huffman@31708
   326
where
huffman@31708
   327
  "is_nat x = (x >= 0)"
huffman@31708
   328
huffman@31708
   329
lemma transfer_int_nat_numerals:
huffman@31708
   330
    "0 = int 0"
huffman@31708
   331
    "1 = int 1"
huffman@31708
   332
    "2 = int 2"
huffman@31708
   333
    "3 = int 3"
huffman@31708
   334
  by auto
huffman@31708
   335
huffman@31708
   336
lemma transfer_int_nat_functions:
huffman@31708
   337
    "(int x) + (int y) = int (x + y)"
huffman@31708
   338
    "(int x) * (int y) = int (x * y)"
huffman@31708
   339
    "tsub (int x) (int y) = int (x - y)"
huffman@31708
   340
    "(int x)^n = int (x^n)"
huffman@31708
   341
    "(int x) div (int y) = int (x div y)"
huffman@31708
   342
    "(int x) mod (int y) = int (x mod y)"
huffman@31708
   343
  by (auto simp add: int_mult tsub_def int_power zdiv_int zmod_int)
huffman@31708
   344
huffman@31708
   345
lemma transfer_int_nat_function_closures:
huffman@31708
   346
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x + y)"
huffman@31708
   347
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x * y)"
huffman@31708
   348
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (tsub x y)"
huffman@31708
   349
    "is_nat x \<Longrightarrow> is_nat (x^n)"
huffman@31708
   350
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
huffman@31708
   351
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
huffman@31708
   352
    "is_nat 0"
huffman@31708
   353
    "is_nat 1"
huffman@31708
   354
    "is_nat 2"
huffman@31708
   355
    "is_nat 3"
huffman@31708
   356
    "is_nat (int z)"
huffman@31708
   357
  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
huffman@31708
   358
huffman@31708
   359
lemma transfer_int_nat_relations:
huffman@31708
   360
    "(int x = int y) = (x = y)"
huffman@31708
   361
    "(int x < int y) = (x < y)"
huffman@31708
   362
    "(int x <= int y) = (x <= y)"
huffman@31708
   363
    "(int x dvd int y) = (x dvd y)"
huffman@31708
   364
    "(even (int x)) = (even x)"
huffman@31708
   365
  by (auto simp add: zdvd_int even_nat_def)
huffman@31708
   366
haftmann@32121
   367
lemma UNIV_apply:
haftmann@32121
   368
  "UNIV x = True"
haftmann@32264
   369
  by (simp add: top_fun_eq top_bool_eq)
haftmann@32121
   370
huffman@31708
   371
declare TransferMorphism_int_nat[transfer add return:
huffman@31708
   372
  transfer_int_nat_numerals
huffman@31708
   373
  transfer_int_nat_functions
huffman@31708
   374
  transfer_int_nat_function_closures
huffman@31708
   375
  transfer_int_nat_relations
haftmann@32121
   376
  UNIV_apply
huffman@31708
   377
]
huffman@31708
   378
huffman@31708
   379
huffman@31708
   380
(* first-order quantifiers *)
huffman@31708
   381
huffman@31708
   382
lemma transfer_int_nat_quantifiers:
huffman@31708
   383
    "(ALL (x::int) >= 0. P x) = (ALL (x::nat). P (int x))"
huffman@31708
   384
    "(EX (x::int) >= 0. P x) = (EX (x::nat). P (int x))"
huffman@31708
   385
  apply (subst all_nat)
huffman@31708
   386
  apply auto [1]
huffman@31708
   387
  apply (subst ex_nat)
huffman@31708
   388
  apply auto
huffman@31708
   389
done
huffman@31708
   390
huffman@31708
   391
declare TransferMorphism_int_nat[transfer add
huffman@31708
   392
  return: transfer_int_nat_quantifiers]
huffman@31708
   393
huffman@31708
   394
huffman@31708
   395
(* if *)
huffman@31708
   396
huffman@31708
   397
lemma int_if_cong: "(if P then (int x) else (int y)) =
huffman@31708
   398
    int (if P then x else y)"
huffman@31708
   399
  by auto
huffman@31708
   400
huffman@31708
   401
declare TransferMorphism_int_nat [transfer add return: int_if_cong]
huffman@31708
   402
huffman@31708
   403
huffman@31708
   404
huffman@31708
   405
(* operations with sets *)
huffman@31708
   406
huffman@31708
   407
lemma transfer_int_nat_set_functions:
huffman@31708
   408
    "nat_set A \<Longrightarrow> card A = card (nat ` A)"
huffman@31708
   409
    "{} = int ` ({}::nat set)"
huffman@31708
   410
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Un B = int ` (nat ` A Un nat ` B)"
huffman@31708
   411
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Int B = int ` (nat ` A Int nat ` B)"
huffman@31708
   412
    "{x. x >= 0 & P x} = int ` {x. P(int x)}"
huffman@31708
   413
    "is_nat m \<Longrightarrow> is_nat n \<Longrightarrow> {m..n} = int ` {nat m..nat n}"
huffman@31708
   414
       (* need all variants of these! *)
huffman@31708
   415
  by (simp_all only: is_nat_def transfer_nat_int_set_functions
huffman@31708
   416
          transfer_nat_int_set_function_closures
huffman@31708
   417
          transfer_nat_int_set_return_embed nat_0_le
huffman@31708
   418
          cong: transfer_nat_int_set_cong)
huffman@31708
   419
huffman@31708
   420
lemma transfer_int_nat_set_function_closures:
huffman@31708
   421
    "nat_set {}"
huffman@31708
   422
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
huffman@31708
   423
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
huffman@31708
   424
    "is_nat x \<Longrightarrow> nat_set {x..y}"
huffman@31708
   425
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   426
    "nat_set (int ` C)"
huffman@31708
   427
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> is_nat x"
huffman@31708
   428
  by (simp_all only: transfer_nat_int_set_function_closures is_nat_def)
huffman@31708
   429
huffman@31708
   430
lemma transfer_int_nat_set_relations:
huffman@31708
   431
    "nat_set A \<Longrightarrow> finite A = finite (nat ` A)"
huffman@31708
   432
    "is_nat x \<Longrightarrow> nat_set A \<Longrightarrow> (x : A) = (nat x : nat ` A)"
huffman@31708
   433
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A = B) = (nat ` A = nat ` B)"
huffman@31708
   434
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A < B) = (nat ` A < nat ` B)"
huffman@31708
   435
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A <= B) = (nat ` A <= nat ` B)"
huffman@31708
   436
  by (simp_all only: is_nat_def transfer_nat_int_set_relations
huffman@31708
   437
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   438
huffman@31708
   439
lemma transfer_int_nat_set_return_embed: "nat ` int ` A = A"
huffman@31708
   440
  by (simp only: transfer_nat_int_set_relations
huffman@31708
   441
    transfer_nat_int_set_function_closures
huffman@31708
   442
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   443
huffman@31708
   444
lemma transfer_int_nat_set_cong: "(!!x. P x = P' x) \<Longrightarrow>
huffman@31708
   445
    {(x::nat). P x} = {x. P' x}"
huffman@31708
   446
  by auto
huffman@31708
   447
huffman@31708
   448
declare TransferMorphism_int_nat[transfer add
huffman@31708
   449
  return: transfer_int_nat_set_functions
huffman@31708
   450
    transfer_int_nat_set_function_closures
huffman@31708
   451
    transfer_int_nat_set_relations
huffman@31708
   452
    transfer_int_nat_set_return_embed
huffman@31708
   453
  cong: transfer_int_nat_set_cong
huffman@31708
   454
]
huffman@31708
   455
huffman@31708
   456
huffman@31708
   457
(* setsum and setprod *)
huffman@31708
   458
huffman@31708
   459
(* this handles the case where the *domain* of f is int *)
huffman@31708
   460
lemma transfer_int_nat_sum_prod:
huffman@31708
   461
    "nat_set A \<Longrightarrow> setsum f A = setsum (%x. f (int x)) (nat ` A)"
huffman@31708
   462
    "nat_set A \<Longrightarrow> setprod f A = setprod (%x. f (int x)) (nat ` A)"
huffman@31708
   463
  apply (subst setsum_reindex)
huffman@31708
   464
  apply (unfold inj_on_def nat_set_def, auto simp add: eq_nat_nat_iff)
huffman@31708
   465
  apply (subst setprod_reindex)
huffman@31708
   466
  apply (unfold inj_on_def nat_set_def o_def, auto simp add: eq_nat_nat_iff
huffman@31708
   467
            cong: setprod_cong)
huffman@31708
   468
done
huffman@31708
   469
huffman@31708
   470
(* this handles the case where the *range* of f is int *)
huffman@31708
   471
lemma transfer_int_nat_sum_prod2:
huffman@31708
   472
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow> setsum f A = int(setsum (%x. nat (f x)) A)"
huffman@31708
   473
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow>
huffman@31708
   474
      setprod f A = int(setprod (%x. nat (f x)) A)"
huffman@31708
   475
  unfolding is_nat_def
huffman@31708
   476
  apply (subst int_setsum, auto)
huffman@31708
   477
  apply (subst int_setprod, auto simp add: cong: setprod_cong)
huffman@31708
   478
done
huffman@31708
   479
huffman@31708
   480
declare TransferMorphism_int_nat[transfer add
huffman@31708
   481
  return: transfer_int_nat_sum_prod transfer_int_nat_sum_prod2
huffman@31708
   482
  cong: setsum_cong setprod_cong]
huffman@31708
   483
huffman@31708
   484
end