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