src/HOL/NatTransfer.thy
author haftmann
Tue Jul 21 15:44:30 2009 +0200 (2009-07-21)
changeset 32121 a7bc3141e599
parent 31708 a3fce678c320
child 32264 0be31453f698
permissions -rw-r--r--
UNIV_code now named UNIV_apply
huffman@31708
     1
(*  Title:      HOL/Library/NatTransfer.thy
huffman@31708
     2
    Authors:    Jeremy Avigad and Amine Chaieb
huffman@31708
     3
huffman@31708
     4
    Sets up transfer from nats to ints and
huffman@31708
     5
    back.
huffman@31708
     6
*)
huffman@31708
     7
huffman@31708
     8
huffman@31708
     9
header {* NatTransfer *}
huffman@31708
    10
huffman@31708
    11
theory NatTransfer
huffman@31708
    12
imports Main Parity
huffman@31708
    13
uses ("Tools/transfer_data.ML")
huffman@31708
    14
begin
huffman@31708
    15
huffman@31708
    16
subsection {* A transfer Method between isomorphic domains*}
huffman@31708
    17
huffman@31708
    18
definition TransferMorphism:: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> bool"
huffman@31708
    19
  where "TransferMorphism a B = True"
huffman@31708
    20
huffman@31708
    21
use "Tools/transfer_data.ML"
huffman@31708
    22
huffman@31708
    23
setup TransferData.setup
huffman@31708
    24
huffman@31708
    25
huffman@31708
    26
subsection {* Set up transfer from nat to int *}
huffman@31708
    27
huffman@31708
    28
(* set up transfer direction *)
huffman@31708
    29
huffman@31708
    30
lemma TransferMorphism_nat_int: "TransferMorphism nat (op <= (0::int))"
huffman@31708
    31
  by (simp add: TransferMorphism_def)
huffman@31708
    32
huffman@31708
    33
declare TransferMorphism_nat_int[transfer
huffman@31708
    34
  add mode: manual
huffman@31708
    35
  return: nat_0_le
huffman@31708
    36
  labels: natint
huffman@31708
    37
]
huffman@31708
    38
huffman@31708
    39
(* basic functions and relations *)
huffman@31708
    40
huffman@31708
    41
lemma transfer_nat_int_numerals:
huffman@31708
    42
    "(0::nat) = nat 0"
huffman@31708
    43
    "(1::nat) = nat 1"
huffman@31708
    44
    "(2::nat) = nat 2"
huffman@31708
    45
    "(3::nat) = nat 3"
huffman@31708
    46
  by auto
huffman@31708
    47
huffman@31708
    48
definition
huffman@31708
    49
  tsub :: "int \<Rightarrow> int \<Rightarrow> int"
huffman@31708
    50
where
huffman@31708
    51
  "tsub x y = (if x >= y then x - y else 0)"
huffman@31708
    52
huffman@31708
    53
lemma tsub_eq: "x >= y \<Longrightarrow> tsub x y = x - y"
huffman@31708
    54
  by (simp add: tsub_def)
huffman@31708
    55
huffman@31708
    56
huffman@31708
    57
lemma transfer_nat_int_functions:
huffman@31708
    58
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) + (nat y) = nat (x + y)"
huffman@31708
    59
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) * (nat y) = nat (x * y)"
huffman@31708
    60
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) - (nat y) = nat (tsub x y)"
huffman@31708
    61
    "(x::int) >= 0 \<Longrightarrow> (nat x)^n = nat (x^n)"
huffman@31708
    62
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
huffman@31708
    63
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
huffman@31708
    64
  by (auto simp add: eq_nat_nat_iff nat_mult_distrib
huffman@31708
    65
      nat_power_eq nat_div_distrib nat_mod_distrib tsub_def)
huffman@31708
    66
huffman@31708
    67
lemma transfer_nat_int_function_closures:
huffman@31708
    68
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x + y >= 0"
huffman@31708
    69
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x * y >= 0"
huffman@31708
    70
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> tsub x y >= 0"
huffman@31708
    71
    "(x::int) >= 0 \<Longrightarrow> x^n >= 0"
huffman@31708
    72
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
huffman@31708
    73
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
huffman@31708
    74
    "(0::int) >= 0"
huffman@31708
    75
    "(1::int) >= 0"
huffman@31708
    76
    "(2::int) >= 0"
huffman@31708
    77
    "(3::int) >= 0"
huffman@31708
    78
    "int z >= 0"
huffman@31708
    79
  apply (auto simp add: zero_le_mult_iff tsub_def)
huffman@31708
    80
  apply (case_tac "y = 0")
huffman@31708
    81
  apply auto
huffman@31708
    82
  apply (subst pos_imp_zdiv_nonneg_iff, auto)
huffman@31708
    83
  apply (case_tac "y = 0")
huffman@31708
    84
  apply force
huffman@31708
    85
  apply (rule pos_mod_sign)
huffman@31708
    86
  apply arith
huffman@31708
    87
done
huffman@31708
    88
huffman@31708
    89
lemma transfer_nat_int_relations:
huffman@31708
    90
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    91
      (nat (x::int) = nat y) = (x = y)"
huffman@31708
    92
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    93
      (nat (x::int) < nat y) = (x < y)"
huffman@31708
    94
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    95
      (nat (x::int) <= nat y) = (x <= y)"
huffman@31708
    96
    "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
huffman@31708
    97
      (nat (x::int) dvd nat y) = (x dvd y)"
huffman@31708
    98
  by (auto simp add: zdvd_int even_nat_def)
huffman@31708
    99
huffman@31708
   100
declare TransferMorphism_nat_int[transfer add return:
huffman@31708
   101
  transfer_nat_int_numerals
huffman@31708
   102
  transfer_nat_int_functions
huffman@31708
   103
  transfer_nat_int_function_closures
huffman@31708
   104
  transfer_nat_int_relations
huffman@31708
   105
]
huffman@31708
   106
huffman@31708
   107
huffman@31708
   108
(* first-order quantifiers *)
huffman@31708
   109
huffman@31708
   110
lemma transfer_nat_int_quantifiers:
huffman@31708
   111
    "(ALL (x::nat). P x) = (ALL (x::int). x >= 0 \<longrightarrow> P (nat x))"
huffman@31708
   112
    "(EX (x::nat). P x) = (EX (x::int). x >= 0 & P (nat x))"
huffman@31708
   113
  by (rule all_nat, rule ex_nat)
huffman@31708
   114
huffman@31708
   115
(* should we restrict these? *)
huffman@31708
   116
lemma all_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   117
    (ALL x. Q x \<longrightarrow> P x) = (ALL x. Q x \<longrightarrow> P' x)"
huffman@31708
   118
  by auto
huffman@31708
   119
huffman@31708
   120
lemma ex_cong: "(\<And>x. Q x \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   121
    (EX x. Q x \<and> P x) = (EX x. Q x \<and> P' x)"
huffman@31708
   122
  by auto
huffman@31708
   123
huffman@31708
   124
declare TransferMorphism_nat_int[transfer add
huffman@31708
   125
  return: transfer_nat_int_quantifiers
huffman@31708
   126
  cong: all_cong ex_cong]
huffman@31708
   127
huffman@31708
   128
huffman@31708
   129
(* if *)
huffman@31708
   130
huffman@31708
   131
lemma nat_if_cong: "(if P then (nat x) else (nat y)) =
huffman@31708
   132
    nat (if P then x else y)"
huffman@31708
   133
  by auto
huffman@31708
   134
huffman@31708
   135
declare TransferMorphism_nat_int [transfer add return: nat_if_cong]
huffman@31708
   136
huffman@31708
   137
huffman@31708
   138
(* operations with sets *)
huffman@31708
   139
huffman@31708
   140
definition
huffman@31708
   141
  nat_set :: "int set \<Rightarrow> bool"
huffman@31708
   142
where
huffman@31708
   143
  "nat_set S = (ALL x:S. x >= 0)"
huffman@31708
   144
huffman@31708
   145
lemma transfer_nat_int_set_functions:
huffman@31708
   146
    "card A = card (int ` A)"
huffman@31708
   147
    "{} = nat ` ({}::int set)"
huffman@31708
   148
    "A Un B = nat ` (int ` A Un int ` B)"
huffman@31708
   149
    "A Int B = nat ` (int ` A Int int ` B)"
huffman@31708
   150
    "{x. P x} = nat ` {x. x >= 0 & P(nat x)}"
huffman@31708
   151
    "{..n} = nat ` {0..int n}"
huffman@31708
   152
    "{m..n} = nat ` {int m..int n}"  (* need all variants of these! *)
huffman@31708
   153
  apply (rule card_image [symmetric])
huffman@31708
   154
  apply (auto simp add: inj_on_def image_def)
huffman@31708
   155
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   156
  apply auto
huffman@31708
   157
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   158
  apply auto
huffman@31708
   159
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   160
  apply auto
huffman@31708
   161
  apply (rule_tac x = "int x" in exI)
huffman@31708
   162
  apply auto
huffman@31708
   163
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   164
  apply auto
huffman@31708
   165
  apply (rule_tac x = "int x" in bexI)
huffman@31708
   166
  apply auto
huffman@31708
   167
done
huffman@31708
   168
huffman@31708
   169
lemma transfer_nat_int_set_function_closures:
huffman@31708
   170
    "nat_set {}"
huffman@31708
   171
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
huffman@31708
   172
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
huffman@31708
   173
    "x >= 0 \<Longrightarrow> nat_set {x..y}"
huffman@31708
   174
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   175
    "nat_set (int ` C)"
huffman@31708
   176
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> x >= 0" (* does it hurt to turn this on? *)
huffman@31708
   177
  unfolding nat_set_def apply auto
huffman@31708
   178
done
huffman@31708
   179
huffman@31708
   180
lemma transfer_nat_int_set_relations:
huffman@31708
   181
    "(finite A) = (finite (int ` A))"
huffman@31708
   182
    "(x : A) = (int x : int ` A)"
huffman@31708
   183
    "(A = B) = (int ` A = int ` B)"
huffman@31708
   184
    "(A < B) = (int ` A < int ` B)"
huffman@31708
   185
    "(A <= B) = (int ` A <= int ` B)"
huffman@31708
   186
huffman@31708
   187
  apply (rule iffI)
huffman@31708
   188
  apply (erule finite_imageI)
huffman@31708
   189
  apply (erule finite_imageD)
huffman@31708
   190
  apply (auto simp add: image_def expand_set_eq inj_on_def)
huffman@31708
   191
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   192
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   193
  apply (drule_tac x = "int x" in spec, auto)
huffman@31708
   194
done
huffman@31708
   195
huffman@31708
   196
lemma transfer_nat_int_set_return_embed: "nat_set A \<Longrightarrow>
huffman@31708
   197
    (int ` nat ` A = A)"
huffman@31708
   198
  by (auto simp add: nat_set_def image_def)
huffman@31708
   199
huffman@31708
   200
lemma transfer_nat_int_set_cong: "(!!x. x >= 0 \<Longrightarrow> P x = P' x) \<Longrightarrow>
huffman@31708
   201
    {(x::int). x >= 0 & P x} = {x. x >= 0 & P' x}"
huffman@31708
   202
  by auto
huffman@31708
   203
huffman@31708
   204
declare TransferMorphism_nat_int[transfer add
huffman@31708
   205
  return: transfer_nat_int_set_functions
huffman@31708
   206
    transfer_nat_int_set_function_closures
huffman@31708
   207
    transfer_nat_int_set_relations
huffman@31708
   208
    transfer_nat_int_set_return_embed
huffman@31708
   209
  cong: transfer_nat_int_set_cong
huffman@31708
   210
]
huffman@31708
   211
huffman@31708
   212
huffman@31708
   213
(* setsum and setprod *)
huffman@31708
   214
huffman@31708
   215
(* this handles the case where the *domain* of f is nat *)
huffman@31708
   216
lemma transfer_nat_int_sum_prod:
huffman@31708
   217
    "setsum f A = setsum (%x. f (nat x)) (int ` A)"
huffman@31708
   218
    "setprod f A = setprod (%x. f (nat x)) (int ` A)"
huffman@31708
   219
  apply (subst setsum_reindex)
huffman@31708
   220
  apply (unfold inj_on_def, auto)
huffman@31708
   221
  apply (subst setprod_reindex)
huffman@31708
   222
  apply (unfold inj_on_def o_def, auto)
huffman@31708
   223
done
huffman@31708
   224
huffman@31708
   225
(* this handles the case where the *range* of f is nat *)
huffman@31708
   226
lemma transfer_nat_int_sum_prod2:
huffman@31708
   227
    "setsum f A = nat(setsum (%x. int (f x)) A)"
huffman@31708
   228
    "setprod f A = nat(setprod (%x. int (f x)) A)"
huffman@31708
   229
  apply (subst int_setsum [symmetric])
huffman@31708
   230
  apply auto
huffman@31708
   231
  apply (subst int_setprod [symmetric])
huffman@31708
   232
  apply auto
huffman@31708
   233
done
huffman@31708
   234
huffman@31708
   235
lemma transfer_nat_int_sum_prod_closure:
huffman@31708
   236
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> setsum f A >= 0"
huffman@31708
   237
    "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> setprod f A >= 0"
huffman@31708
   238
  unfolding nat_set_def
huffman@31708
   239
  apply (rule setsum_nonneg)
huffman@31708
   240
  apply auto
huffman@31708
   241
  apply (rule setprod_nonneg)
huffman@31708
   242
  apply auto
huffman@31708
   243
done
huffman@31708
   244
huffman@31708
   245
(* this version doesn't work, even with nat_set A \<Longrightarrow>
huffman@31708
   246
      x : A \<Longrightarrow> x >= 0 turned on. Why not?
huffman@31708
   247
huffman@31708
   248
  also: what does =simp=> do?
huffman@31708
   249
huffman@31708
   250
lemma transfer_nat_int_sum_prod_closure:
huffman@31708
   251
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> setsum f A >= 0"
huffman@31708
   252
    "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> setprod f A >= 0"
huffman@31708
   253
  unfolding nat_set_def simp_implies_def
huffman@31708
   254
  apply (rule setsum_nonneg)
huffman@31708
   255
  apply auto
huffman@31708
   256
  apply (rule setprod_nonneg)
huffman@31708
   257
  apply auto
huffman@31708
   258
done
huffman@31708
   259
*)
huffman@31708
   260
huffman@31708
   261
(* Making A = B in this lemma doesn't work. Why not?
huffman@31708
   262
   Also, why aren't setsum_cong and setprod_cong enough,
huffman@31708
   263
   with the previously mentioned rule turned on? *)
huffman@31708
   264
huffman@31708
   265
lemma transfer_nat_int_sum_prod_cong:
huffman@31708
   266
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
huffman@31708
   267
      setsum f A = setsum g B"
huffman@31708
   268
    "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
huffman@31708
   269
      setprod f A = setprod g B"
huffman@31708
   270
  unfolding nat_set_def
huffman@31708
   271
  apply (subst setsum_cong, assumption)
huffman@31708
   272
  apply auto [2]
huffman@31708
   273
  apply (subst setprod_cong, assumption, auto)
huffman@31708
   274
done
huffman@31708
   275
huffman@31708
   276
declare TransferMorphism_nat_int[transfer add
huffman@31708
   277
  return: transfer_nat_int_sum_prod transfer_nat_int_sum_prod2
huffman@31708
   278
    transfer_nat_int_sum_prod_closure
huffman@31708
   279
  cong: transfer_nat_int_sum_prod_cong]
huffman@31708
   280
huffman@31708
   281
(* lists *)
huffman@31708
   282
huffman@31708
   283
definition
huffman@31708
   284
  embed_list :: "nat list \<Rightarrow> int list"
huffman@31708
   285
where
huffman@31708
   286
  "embed_list l = map int l";
huffman@31708
   287
huffman@31708
   288
definition
huffman@31708
   289
  nat_list :: "int list \<Rightarrow> bool"
huffman@31708
   290
where
huffman@31708
   291
  "nat_list l = nat_set (set l)";
huffman@31708
   292
huffman@31708
   293
definition
huffman@31708
   294
  return_list :: "int list \<Rightarrow> nat list"
huffman@31708
   295
where
huffman@31708
   296
  "return_list l = map nat l";
huffman@31708
   297
huffman@31708
   298
thm nat_0_le;
huffman@31708
   299
huffman@31708
   300
lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow>
huffman@31708
   301
    embed_list (return_list l) = l";
huffman@31708
   302
  unfolding embed_list_def return_list_def nat_list_def nat_set_def
huffman@31708
   303
  apply (induct l);
huffman@31708
   304
  apply auto;
huffman@31708
   305
done;
huffman@31708
   306
huffman@31708
   307
lemma transfer_nat_int_list_functions:
huffman@31708
   308
  "l @ m = return_list (embed_list l @ embed_list m)"
huffman@31708
   309
  "[] = return_list []";
huffman@31708
   310
  unfolding return_list_def embed_list_def;
huffman@31708
   311
  apply auto;
huffman@31708
   312
  apply (induct l, auto);
huffman@31708
   313
  apply (induct m, auto);
huffman@31708
   314
done;
huffman@31708
   315
huffman@31708
   316
(*
huffman@31708
   317
lemma transfer_nat_int_fold1: "fold f l x =
huffman@31708
   318
    fold (%x. f (nat x)) (embed_list l) x";
huffman@31708
   319
*)
huffman@31708
   320
huffman@31708
   321
huffman@31708
   322
huffman@31708
   323
huffman@31708
   324
subsection {* Set up transfer from int to nat *}
huffman@31708
   325
huffman@31708
   326
(* set up transfer direction *)
huffman@31708
   327
huffman@31708
   328
lemma TransferMorphism_int_nat: "TransferMorphism int (UNIV :: nat set)"
huffman@31708
   329
  by (simp add: TransferMorphism_def)
huffman@31708
   330
huffman@31708
   331
declare TransferMorphism_int_nat[transfer add
huffman@31708
   332
  mode: manual
huffman@31708
   333
(*  labels: int-nat *)
huffman@31708
   334
  return: nat_int
huffman@31708
   335
]
huffman@31708
   336
huffman@31708
   337
huffman@31708
   338
(* basic functions and relations *)
huffman@31708
   339
huffman@31708
   340
definition
huffman@31708
   341
  is_nat :: "int \<Rightarrow> bool"
huffman@31708
   342
where
huffman@31708
   343
  "is_nat x = (x >= 0)"
huffman@31708
   344
huffman@31708
   345
lemma transfer_int_nat_numerals:
huffman@31708
   346
    "0 = int 0"
huffman@31708
   347
    "1 = int 1"
huffman@31708
   348
    "2 = int 2"
huffman@31708
   349
    "3 = int 3"
huffman@31708
   350
  by auto
huffman@31708
   351
huffman@31708
   352
lemma transfer_int_nat_functions:
huffman@31708
   353
    "(int x) + (int y) = int (x + y)"
huffman@31708
   354
    "(int x) * (int y) = int (x * y)"
huffman@31708
   355
    "tsub (int x) (int y) = int (x - y)"
huffman@31708
   356
    "(int x)^n = int (x^n)"
huffman@31708
   357
    "(int x) div (int y) = int (x div y)"
huffman@31708
   358
    "(int x) mod (int y) = int (x mod y)"
huffman@31708
   359
  by (auto simp add: int_mult tsub_def int_power zdiv_int zmod_int)
huffman@31708
   360
huffman@31708
   361
lemma transfer_int_nat_function_closures:
huffman@31708
   362
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x + y)"
huffman@31708
   363
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x * y)"
huffman@31708
   364
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (tsub x y)"
huffman@31708
   365
    "is_nat x \<Longrightarrow> is_nat (x^n)"
huffman@31708
   366
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
huffman@31708
   367
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
huffman@31708
   368
    "is_nat 0"
huffman@31708
   369
    "is_nat 1"
huffman@31708
   370
    "is_nat 2"
huffman@31708
   371
    "is_nat 3"
huffman@31708
   372
    "is_nat (int z)"
huffman@31708
   373
  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
huffman@31708
   374
huffman@31708
   375
lemma transfer_int_nat_relations:
huffman@31708
   376
    "(int x = int y) = (x = y)"
huffman@31708
   377
    "(int x < int y) = (x < y)"
huffman@31708
   378
    "(int x <= int y) = (x <= y)"
huffman@31708
   379
    "(int x dvd int y) = (x dvd y)"
huffman@31708
   380
    "(even (int x)) = (even x)"
huffman@31708
   381
  by (auto simp add: zdvd_int even_nat_def)
huffman@31708
   382
haftmann@32121
   383
lemma UNIV_apply:
haftmann@32121
   384
  "UNIV x = True"
haftmann@32121
   385
  by (simp add: top_set_eq [symmetric] top_fun_eq top_bool_eq)
haftmann@32121
   386
huffman@31708
   387
declare TransferMorphism_int_nat[transfer add return:
huffman@31708
   388
  transfer_int_nat_numerals
huffman@31708
   389
  transfer_int_nat_functions
huffman@31708
   390
  transfer_int_nat_function_closures
huffman@31708
   391
  transfer_int_nat_relations
haftmann@32121
   392
  UNIV_apply
huffman@31708
   393
]
huffman@31708
   394
huffman@31708
   395
huffman@31708
   396
(* first-order quantifiers *)
huffman@31708
   397
huffman@31708
   398
lemma transfer_int_nat_quantifiers:
huffman@31708
   399
    "(ALL (x::int) >= 0. P x) = (ALL (x::nat). P (int x))"
huffman@31708
   400
    "(EX (x::int) >= 0. P x) = (EX (x::nat). P (int x))"
huffman@31708
   401
  apply (subst all_nat)
huffman@31708
   402
  apply auto [1]
huffman@31708
   403
  apply (subst ex_nat)
huffman@31708
   404
  apply auto
huffman@31708
   405
done
huffman@31708
   406
huffman@31708
   407
declare TransferMorphism_int_nat[transfer add
huffman@31708
   408
  return: transfer_int_nat_quantifiers]
huffman@31708
   409
huffman@31708
   410
huffman@31708
   411
(* if *)
huffman@31708
   412
huffman@31708
   413
lemma int_if_cong: "(if P then (int x) else (int y)) =
huffman@31708
   414
    int (if P then x else y)"
huffman@31708
   415
  by auto
huffman@31708
   416
huffman@31708
   417
declare TransferMorphism_int_nat [transfer add return: int_if_cong]
huffman@31708
   418
huffman@31708
   419
huffman@31708
   420
huffman@31708
   421
(* operations with sets *)
huffman@31708
   422
huffman@31708
   423
lemma transfer_int_nat_set_functions:
huffman@31708
   424
    "nat_set A \<Longrightarrow> card A = card (nat ` A)"
huffman@31708
   425
    "{} = int ` ({}::nat set)"
huffman@31708
   426
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Un B = int ` (nat ` A Un nat ` B)"
huffman@31708
   427
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Int B = int ` (nat ` A Int nat ` B)"
huffman@31708
   428
    "{x. x >= 0 & P x} = int ` {x. P(int x)}"
huffman@31708
   429
    "is_nat m \<Longrightarrow> is_nat n \<Longrightarrow> {m..n} = int ` {nat m..nat n}"
huffman@31708
   430
       (* need all variants of these! *)
huffman@31708
   431
  by (simp_all only: is_nat_def transfer_nat_int_set_functions
huffman@31708
   432
          transfer_nat_int_set_function_closures
huffman@31708
   433
          transfer_nat_int_set_return_embed nat_0_le
huffman@31708
   434
          cong: transfer_nat_int_set_cong)
huffman@31708
   435
huffman@31708
   436
lemma transfer_int_nat_set_function_closures:
huffman@31708
   437
    "nat_set {}"
huffman@31708
   438
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
huffman@31708
   439
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
huffman@31708
   440
    "is_nat x \<Longrightarrow> nat_set {x..y}"
huffman@31708
   441
    "nat_set {x. x >= 0 & P x}"
huffman@31708
   442
    "nat_set (int ` C)"
huffman@31708
   443
    "nat_set A \<Longrightarrow> x : A \<Longrightarrow> is_nat x"
huffman@31708
   444
  by (simp_all only: transfer_nat_int_set_function_closures is_nat_def)
huffman@31708
   445
huffman@31708
   446
lemma transfer_int_nat_set_relations:
huffman@31708
   447
    "nat_set A \<Longrightarrow> finite A = finite (nat ` A)"
huffman@31708
   448
    "is_nat x \<Longrightarrow> nat_set A \<Longrightarrow> (x : A) = (nat x : nat ` A)"
huffman@31708
   449
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A = B) = (nat ` A = nat ` B)"
huffman@31708
   450
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A < B) = (nat ` A < nat ` B)"
huffman@31708
   451
    "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A <= B) = (nat ` A <= nat ` B)"
huffman@31708
   452
  by (simp_all only: is_nat_def transfer_nat_int_set_relations
huffman@31708
   453
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   454
huffman@31708
   455
lemma transfer_int_nat_set_return_embed: "nat ` int ` A = A"
huffman@31708
   456
  by (simp only: transfer_nat_int_set_relations
huffman@31708
   457
    transfer_nat_int_set_function_closures
huffman@31708
   458
    transfer_nat_int_set_return_embed nat_0_le)
huffman@31708
   459
huffman@31708
   460
lemma transfer_int_nat_set_cong: "(!!x. P x = P' x) \<Longrightarrow>
huffman@31708
   461
    {(x::nat). P x} = {x. P' x}"
huffman@31708
   462
  by auto
huffman@31708
   463
huffman@31708
   464
declare TransferMorphism_int_nat[transfer add
huffman@31708
   465
  return: transfer_int_nat_set_functions
huffman@31708
   466
    transfer_int_nat_set_function_closures
huffman@31708
   467
    transfer_int_nat_set_relations
huffman@31708
   468
    transfer_int_nat_set_return_embed
huffman@31708
   469
  cong: transfer_int_nat_set_cong
huffman@31708
   470
]
huffman@31708
   471
huffman@31708
   472
huffman@31708
   473
(* setsum and setprod *)
huffman@31708
   474
huffman@31708
   475
(* this handles the case where the *domain* of f is int *)
huffman@31708
   476
lemma transfer_int_nat_sum_prod:
huffman@31708
   477
    "nat_set A \<Longrightarrow> setsum f A = setsum (%x. f (int x)) (nat ` A)"
huffman@31708
   478
    "nat_set A \<Longrightarrow> setprod f A = setprod (%x. f (int x)) (nat ` A)"
huffman@31708
   479
  apply (subst setsum_reindex)
huffman@31708
   480
  apply (unfold inj_on_def nat_set_def, auto simp add: eq_nat_nat_iff)
huffman@31708
   481
  apply (subst setprod_reindex)
huffman@31708
   482
  apply (unfold inj_on_def nat_set_def o_def, auto simp add: eq_nat_nat_iff
huffman@31708
   483
            cong: setprod_cong)
huffman@31708
   484
done
huffman@31708
   485
huffman@31708
   486
(* this handles the case where the *range* of f is int *)
huffman@31708
   487
lemma transfer_int_nat_sum_prod2:
huffman@31708
   488
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow> setsum f A = int(setsum (%x. nat (f x)) A)"
huffman@31708
   489
    "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow>
huffman@31708
   490
      setprod f A = int(setprod (%x. nat (f x)) A)"
huffman@31708
   491
  unfolding is_nat_def
huffman@31708
   492
  apply (subst int_setsum, auto)
huffman@31708
   493
  apply (subst int_setprod, auto simp add: cong: setprod_cong)
huffman@31708
   494
done
huffman@31708
   495
huffman@31708
   496
declare TransferMorphism_int_nat[transfer add
huffman@31708
   497
  return: transfer_int_nat_sum_prod transfer_int_nat_sum_prod2
huffman@31708
   498
  cong: setsum_cong setprod_cong]
huffman@31708
   499
huffman@31708
   500
huffman@31708
   501
subsection {* Test it out *}
huffman@31708
   502
huffman@31708
   503
(* nat to int *)
huffman@31708
   504
huffman@31708
   505
lemma ex1: "(x::nat) + y = y + x"
huffman@31708
   506
  by auto
huffman@31708
   507
huffman@31708
   508
thm ex1 [transferred]
huffman@31708
   509
huffman@31708
   510
lemma ex2: "(a::nat) div b * b + a mod b = a"
huffman@31708
   511
  by (rule mod_div_equality)
huffman@31708
   512
huffman@31708
   513
thm ex2 [transferred]
huffman@31708
   514
huffman@31708
   515
lemma ex3: "ALL (x::nat). ALL y. EX z. z >= x + y"
huffman@31708
   516
  by auto
huffman@31708
   517
huffman@31708
   518
thm ex3 [transferred natint]
huffman@31708
   519
huffman@31708
   520
lemma ex4: "(x::nat) >= y \<Longrightarrow> (x - y) + y = x"
huffman@31708
   521
  by auto
huffman@31708
   522
huffman@31708
   523
thm ex4 [transferred]
huffman@31708
   524
huffman@31708
   525
lemma ex5: "(2::nat) * (SUM i <= n. i) = n * (n + 1)"
huffman@31708
   526
  by (induct n rule: nat_induct, auto)
huffman@31708
   527
huffman@31708
   528
thm ex5 [transferred]
huffman@31708
   529
huffman@31708
   530
theorem ex6: "0 <= (n::int) \<Longrightarrow> 2 * \<Sum>{0..n} = n * (n + 1)"
huffman@31708
   531
  by (rule ex5 [transferred])
huffman@31708
   532
huffman@31708
   533
thm ex6 [transferred]
huffman@31708
   534
huffman@31708
   535
thm ex5 [transferred, transferred]
huffman@31708
   536
huffman@31708
   537
end