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