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