src/HOL/Nat_Transfer.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (23 months ago)
changeset 66831 29ea2b900a05
parent 66817 0b12755ccbb2
child 66836 4eb431c3f974
permissions -rw-r--r--
tuned: each session has at most one defining entry;
     1 (* Authors: Jeremy Avigad and Amine Chaieb *)
     2 
     3 section \<open>Generic transfer machinery;  specific transfer from nats to ints and back.\<close>
     4 
     5 theory Nat_Transfer
     6 imports Int Divides
     7 begin
     8 
     9 subsection \<open>Generic transfer machinery\<close>
    10 
    11 definition transfer_morphism:: "('b \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> bool"
    12   where "transfer_morphism f A \<longleftrightarrow> True"
    13 
    14 lemma transfer_morphismI[intro]: "transfer_morphism f A"
    15   by (simp add: transfer_morphism_def)
    16 
    17 ML_file "Tools/legacy_transfer.ML"
    18 
    19 
    20 subsection \<open>Set up transfer from nat to int\<close>
    21 
    22 text \<open>set up transfer direction\<close>
    23 
    24 lemma transfer_morphism_nat_int [no_atp]:
    25   "transfer_morphism nat (op <= (0::int))" ..
    26 
    27 declare transfer_morphism_nat_int [transfer add
    28   mode: manual
    29   return: nat_0_le
    30   labels: nat_int
    31 ]
    32 
    33 text \<open>basic functions and relations\<close>
    34 
    35 lemma transfer_nat_int_numerals [no_atp, transfer key: transfer_morphism_nat_int]:
    36     "(0::nat) = nat 0"
    37     "(1::nat) = nat 1"
    38     "(2::nat) = nat 2"
    39     "(3::nat) = nat 3"
    40   by auto
    41 
    42 definition
    43   tsub :: "int \<Rightarrow> int \<Rightarrow> int"
    44 where
    45   "tsub x y = (if x >= y then x - y else 0)"
    46 
    47 lemma tsub_eq: "x >= y \<Longrightarrow> tsub x y = x - y"
    48   by (simp add: tsub_def)
    49 
    50 lemma transfer_nat_int_functions [no_atp, transfer key: transfer_morphism_nat_int]:
    51     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) + (nat y) = nat (x + y)"
    52     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) * (nat y) = nat (x * y)"
    53     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) - (nat y) = nat (tsub x y)"
    54     "(x::int) >= 0 \<Longrightarrow> (nat x)^n = nat (x^n)"
    55     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
    56     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
    57   by (auto simp add: eq_nat_nat_iff nat_mult_distrib
    58       nat_power_eq tsub_def nat_div_distrib nat_mod_distrib)
    59 
    60 lemma transfer_nat_int_function_closures [no_atp, transfer key: transfer_morphism_nat_int]:
    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     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
    71     "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
    72             apply (auto simp add: zero_le_mult_iff tsub_def pos_imp_zdiv_nonneg_iff)
    73    apply (cases "y = 0")
    74     apply (auto simp add: pos_imp_zdiv_nonneg_iff)
    75   apply (cases "y = 0")
    76    apply auto
    77   done
    78 
    79 lemma transfer_nat_int_relations [no_atp, transfer key: transfer_morphism_nat_int]:
    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) < nat y) = (x < y)"
    84     "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
    85       (nat (x::int) <= nat y) = (x <= y)"
    86     "x >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow>
    87       (nat (x::int) dvd nat y) = (x dvd y)"
    88   by (auto simp add: zdvd_int)
    89 
    90 
    91 text \<open>first-order quantifiers\<close>
    92 
    93 lemma all_nat: "(\<forall>x. P x) \<longleftrightarrow> (\<forall>x\<ge>0. P (nat x))"
    94   by (simp split: 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 [no_atp, transfer key: transfer_morphism_nat_int]:
   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 transfer_morphism_nat_int [transfer add
   122   cong: all_cong ex_cong]
   123 
   124 
   125 text \<open>if\<close>
   126 
   127 lemma nat_if_cong [transfer key: transfer_morphism_nat_int]:
   128   "(if P then (nat x) else (nat y)) = nat (if P then x else y)"
   129   by auto
   130 
   131 
   132 text \<open>operations with sets\<close>
   133 
   134 definition
   135   nat_set :: "int set \<Rightarrow> bool"
   136 where
   137   "nat_set S = (ALL x:S. x >= 0)"
   138 
   139 lemma transfer_nat_int_set_functions [no_atp]:
   140     "card A = card (int ` A)"
   141     "{} = nat ` ({}::int set)"
   142     "A Un B = nat ` (int ` A Un int ` B)"
   143     "A Int B = nat ` (int ` A Int int ` B)"
   144     "{x. P x} = nat ` {x. x >= 0 & P(nat x)}"
   145   apply (rule card_image [symmetric])
   146   apply (auto simp add: inj_on_def image_def)
   147   apply (rule_tac x = "int x" in bexI)
   148   apply auto
   149   apply (rule_tac x = "int x" in bexI)
   150   apply auto
   151   apply (rule_tac x = "int x" in bexI)
   152   apply auto
   153   apply (rule_tac x = "int x" in exI)
   154   apply auto
   155 done
   156 
   157 lemma transfer_nat_int_set_function_closures [no_atp]:
   158     "nat_set {}"
   159     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
   160     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
   161     "nat_set {x. x >= 0 & P x}"
   162     "nat_set (int ` C)"
   163     "nat_set A \<Longrightarrow> x : A \<Longrightarrow> x >= 0" (* does it hurt to turn this on? *)
   164   unfolding nat_set_def apply auto
   165 done
   166 
   167 lemma transfer_nat_int_set_relations [no_atp]:
   168     "(finite A) = (finite (int ` A))"
   169     "(x : A) = (int x : int ` A)"
   170     "(A = B) = (int ` A = int ` B)"
   171     "(A < B) = (int ` A < int ` B)"
   172     "(A <= B) = (int ` A <= int ` B)"
   173   apply (rule iffI)
   174   apply (erule finite_imageI)
   175   apply (erule finite_imageD)
   176   apply (auto simp add: image_def set_eq_iff inj_on_def)
   177   apply (drule_tac x = "int x" in spec, auto)
   178   apply (drule_tac x = "int x" in spec, auto)
   179   apply (drule_tac x = "int x" in spec, auto)
   180 done
   181 
   182 lemma transfer_nat_int_set_return_embed [no_atp]: "nat_set A \<Longrightarrow>
   183     (int ` nat ` A = A)"
   184   by (auto simp add: nat_set_def image_def)
   185 
   186 lemma transfer_nat_int_set_cong [no_atp]: "(!!x. x >= 0 \<Longrightarrow> P x = P' x) \<Longrightarrow>
   187     {(x::int). x >= 0 & P x} = {x. x >= 0 & P' x}"
   188   by auto
   189 
   190 declare transfer_morphism_nat_int [transfer add
   191   return: transfer_nat_int_set_functions
   192     transfer_nat_int_set_function_closures
   193     transfer_nat_int_set_relations
   194     transfer_nat_int_set_return_embed
   195   cong: transfer_nat_int_set_cong
   196 ]
   197 
   198 
   199 text \<open>sum and prod\<close>
   200 
   201 (* this handles the case where the *domain* of f is nat *)
   202 lemma transfer_nat_int_sum_prod [no_atp]:
   203     "sum f A = sum (%x. f (nat x)) (int ` A)"
   204     "prod f A = prod (%x. f (nat x)) (int ` A)"
   205   apply (subst sum.reindex)
   206   apply (unfold inj_on_def, auto)
   207   apply (subst prod.reindex)
   208   apply (unfold inj_on_def o_def, auto)
   209 done
   210 
   211 (* this handles the case where the *range* of f is nat *)
   212 lemma transfer_nat_int_sum_prod2 [no_atp]:
   213     "sum f A = nat(sum (%x. int (f x)) A)"
   214     "prod f A = nat(prod (%x. int (f x)) A)"
   215   apply (simp only: int_sum [symmetric] nat_int)
   216   apply (simp only: int_prod [symmetric] nat_int)
   217   done
   218 
   219 lemma transfer_nat_int_sum_prod_closure [no_atp]:
   220     "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> sum f A >= 0"
   221     "nat_set A \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x >= (0::int)) \<Longrightarrow> prod f A >= 0"
   222   unfolding nat_set_def
   223   apply (rule sum_nonneg)
   224   apply auto
   225   apply (rule prod_nonneg)
   226   apply auto
   227 done
   228 
   229 (* this version doesn't work, even with nat_set A \<Longrightarrow>
   230       x : A \<Longrightarrow> x >= 0 turned on. Why not?
   231 
   232   also: what does =simp=> do?
   233 
   234 lemma transfer_nat_int_sum_prod_closure:
   235     "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> sum f A >= 0"
   236     "(!!x. x : A  ==> f x >= (0::int)) \<Longrightarrow> prod f A >= 0"
   237   unfolding nat_set_def simp_implies_def
   238   apply (rule sum_nonneg)
   239   apply auto
   240   apply (rule prod_nonneg)
   241   apply auto
   242 done
   243 *)
   244 
   245 (* Making A = B in this lemma doesn't work. Why not?
   246    Also, why aren't sum.cong and prod.cong enough,
   247    with the previously mentioned rule turned on? *)
   248 
   249 lemma transfer_nat_int_sum_prod_cong [no_atp]:
   250     "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
   251       sum f A = sum g B"
   252     "A = B \<Longrightarrow> nat_set B \<Longrightarrow> (!!x. x >= 0 \<Longrightarrow> f x = g x) \<Longrightarrow>
   253       prod f A = prod g B"
   254   unfolding nat_set_def
   255   apply (subst sum.cong, assumption)
   256   apply auto [2]
   257   apply (subst prod.cong, assumption, auto)
   258 done
   259 
   260 declare transfer_morphism_nat_int [transfer add
   261   return: transfer_nat_int_sum_prod transfer_nat_int_sum_prod2
   262     transfer_nat_int_sum_prod_closure
   263   cong: transfer_nat_int_sum_prod_cong]
   264 
   265 
   266 subsection \<open>Set up transfer from int to nat\<close>
   267 
   268 text \<open>set up transfer direction\<close>
   269 
   270 lemma transfer_morphism_int_nat [no_atp]: "transfer_morphism int (\<lambda>n. True)" ..
   271 
   272 declare transfer_morphism_int_nat [transfer add
   273   mode: manual
   274   return: nat_int
   275   labels: int_nat
   276 ]
   277 
   278 
   279 text \<open>basic functions and relations\<close>
   280 
   281 definition
   282   is_nat :: "int \<Rightarrow> bool"
   283 where
   284   "is_nat x = (x >= 0)"
   285 
   286 lemma transfer_int_nat_numerals [no_atp]:
   287     "0 = int 0"
   288     "1 = int 1"
   289     "2 = int 2"
   290     "3 = int 3"
   291   by auto
   292 
   293 lemma transfer_int_nat_functions [no_atp]:
   294     "(int x) + (int y) = int (x + y)"
   295     "(int x) * (int y) = int (x * y)"
   296     "tsub (int x) (int y) = int (x - y)"
   297     "(int x)^n = int (x^n)"
   298     "(int x) div (int y) = int (x div y)"
   299     "(int x) mod (int y) = int (x mod y)"
   300   by (auto simp add: zdiv_int zmod_int tsub_def)
   301 
   302 lemma transfer_int_nat_function_closures [no_atp]:
   303     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x + y)"
   304     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x * y)"
   305     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (tsub x y)"
   306     "is_nat x \<Longrightarrow> is_nat (x^n)"
   307     "is_nat 0"
   308     "is_nat 1"
   309     "is_nat 2"
   310     "is_nat 3"
   311     "is_nat (int z)"
   312     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
   313     "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
   314   by (simp_all only: is_nat_def transfer_nat_int_function_closures)
   315 
   316 lemma transfer_int_nat_relations [no_atp]:
   317     "(int x = int y) = (x = y)"
   318     "(int x < int y) = (x < y)"
   319     "(int x <= int y) = (x <= y)"
   320     "(int x dvd int y) = (x dvd y)"
   321   by (auto simp add: zdvd_int)
   322 
   323 declare transfer_morphism_int_nat [transfer add return:
   324   transfer_int_nat_numerals
   325   transfer_int_nat_functions
   326   transfer_int_nat_function_closures
   327   transfer_int_nat_relations
   328 ]
   329 
   330 
   331 text \<open>first-order quantifiers\<close>
   332 
   333 lemma transfer_int_nat_quantifiers [no_atp]:
   334     "(ALL (x::int) >= 0. P x) = (ALL (x::nat). P (int x))"
   335     "(EX (x::int) >= 0. P x) = (EX (x::nat). P (int x))"
   336   apply (subst all_nat)
   337   apply auto [1]
   338   apply (subst ex_nat)
   339   apply auto
   340 done
   341 
   342 declare transfer_morphism_int_nat [transfer add
   343   return: transfer_int_nat_quantifiers]
   344 
   345 
   346 text \<open>if\<close>
   347 
   348 lemma int_if_cong: "(if P then (int x) else (int y)) =
   349     int (if P then x else y)"
   350   by auto
   351 
   352 declare transfer_morphism_int_nat [transfer add return: int_if_cong]
   353 
   354 
   355 
   356 text \<open>operations with sets\<close>
   357 
   358 lemma transfer_int_nat_set_functions [no_atp]:
   359     "nat_set A \<Longrightarrow> card A = card (nat ` A)"
   360     "{} = int ` ({}::nat set)"
   361     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Un B = int ` (nat ` A Un nat ` B)"
   362     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> A Int B = int ` (nat ` A Int nat ` B)"
   363     "{x. x >= 0 & P x} = int ` {x. P(int x)}"
   364        (* need all variants of these! *)
   365   by (simp_all only: is_nat_def transfer_nat_int_set_functions
   366           transfer_nat_int_set_function_closures
   367           transfer_nat_int_set_return_embed nat_0_le
   368           cong: transfer_nat_int_set_cong)
   369 
   370 lemma transfer_int_nat_set_function_closures [no_atp]:
   371     "nat_set {}"
   372     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Un B)"
   373     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> nat_set (A Int B)"
   374     "nat_set {x. x >= 0 & P x}"
   375     "nat_set (int ` C)"
   376     "nat_set A \<Longrightarrow> x : A \<Longrightarrow> is_nat x"
   377   by (simp_all only: transfer_nat_int_set_function_closures is_nat_def)
   378 
   379 lemma transfer_int_nat_set_relations [no_atp]:
   380     "nat_set A \<Longrightarrow> finite A = finite (nat ` A)"
   381     "is_nat x \<Longrightarrow> nat_set A \<Longrightarrow> (x : A) = (nat x : nat ` A)"
   382     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A = B) = (nat ` A = nat ` B)"
   383     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A < B) = (nat ` A < nat ` B)"
   384     "nat_set A \<Longrightarrow> nat_set B \<Longrightarrow> (A <= B) = (nat ` A <= nat ` B)"
   385   by (simp_all only: is_nat_def transfer_nat_int_set_relations
   386     transfer_nat_int_set_return_embed nat_0_le)
   387 
   388 lemma transfer_int_nat_set_return_embed [no_atp]: "nat ` int ` A = A"
   389   by (simp only: transfer_nat_int_set_relations
   390     transfer_nat_int_set_function_closures
   391     transfer_nat_int_set_return_embed nat_0_le)
   392 
   393 lemma transfer_int_nat_set_cong [no_atp]: "(!!x. P x = P' x) \<Longrightarrow>
   394     {(x::nat). P x} = {x. P' x}"
   395   by auto
   396 
   397 declare transfer_morphism_int_nat [transfer add
   398   return: transfer_int_nat_set_functions
   399     transfer_int_nat_set_function_closures
   400     transfer_int_nat_set_relations
   401     transfer_int_nat_set_return_embed
   402   cong: transfer_int_nat_set_cong
   403 ]
   404 
   405 
   406 text \<open>sum and prod\<close>
   407 
   408 (* this handles the case where the *domain* of f is int *)
   409 lemma transfer_int_nat_sum_prod [no_atp]:
   410     "nat_set A \<Longrightarrow> sum f A = sum (%x. f (int x)) (nat ` A)"
   411     "nat_set A \<Longrightarrow> prod f A = prod (%x. f (int x)) (nat ` A)"
   412   apply (subst sum.reindex)
   413   apply (unfold inj_on_def nat_set_def, auto simp add: eq_nat_nat_iff)
   414   apply (subst prod.reindex)
   415   apply (unfold inj_on_def nat_set_def o_def, auto simp add: eq_nat_nat_iff
   416             cong: prod.cong)
   417 done
   418 
   419 (* this handles the case where the *range* of f is int *)
   420 lemma transfer_int_nat_sum_prod2 [no_atp]:
   421     "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow> sum f A = int(sum (%x. nat (f x)) A)"
   422     "(!!x. x:A \<Longrightarrow> is_nat (f x)) \<Longrightarrow>
   423       prod f A = int(prod (%x. nat (f x)) A)"
   424   unfolding is_nat_def
   425   by (subst int_sum) auto
   426 
   427 declare transfer_morphism_int_nat [transfer add
   428   return: transfer_int_nat_sum_prod transfer_int_nat_sum_prod2
   429   cong: sum.cong prod.cong]
   430 
   431 declare transfer_morphism_int_nat [transfer add return: even_int_iff]
   432 
   433 end