src/HOL/Library/Code_Target_Nat.thy
author haftmann
Fri Mar 22 19:18:08 2019 +0000 (2 months ago)
changeset 69946 494934c30f38
parent 69593 3dda49e08b9d
permissions -rw-r--r--
improved code equations taken over from AFP
     1 (*  Title:      HOL/Library/Code_Target_Nat.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 section \<open>Implementation of natural numbers by target-language integers\<close>
     6 
     7 theory Code_Target_Nat
     8 imports Code_Abstract_Nat
     9 begin
    10 
    11 subsection \<open>Implementation for \<^typ>\<open>nat\<close>\<close>
    12 
    13 context
    14 includes natural.lifting integer.lifting
    15 begin
    16 
    17 lift_definition Nat :: "integer \<Rightarrow> nat"
    18   is nat
    19   .
    20 
    21 lemma [code_post]:
    22   "Nat 0 = 0"
    23   "Nat 1 = 1"
    24   "Nat (numeral k) = numeral k"
    25   by (transfer, simp)+
    26 
    27 lemma [code_abbrev]:
    28   "integer_of_nat = of_nat"
    29   by transfer rule
    30 
    31 lemma [code_unfold]:
    32   "Int.nat (int_of_integer k) = nat_of_integer k"
    33   by transfer rule
    34 
    35 lemma [code abstype]:
    36   "Code_Target_Nat.Nat (integer_of_nat n) = n"
    37   by transfer simp
    38 
    39 lemma [code abstract]:
    40   "integer_of_nat (nat_of_integer k) = max 0 k"
    41   by transfer auto
    42 
    43 lemma [code_abbrev]:
    44   "nat_of_integer (numeral k) = nat_of_num k"
    45   by transfer (simp add: nat_of_num_numeral)
    46 
    47 context
    48 begin  
    49 
    50 qualified definition natural :: "num \<Rightarrow> nat"
    51   where [simp]: "natural = nat_of_num"
    52 
    53 lemma [code_computation_unfold]:
    54   "numeral = natural"
    55   "nat_of_num = natural"
    56   by (simp_all add: nat_of_num_numeral)
    57 
    58 end
    59 
    60 lemma [code abstract]:
    61   "integer_of_nat (nat_of_num n) = integer_of_num n"
    62   by (simp add: nat_of_num_numeral integer_of_nat_numeral)
    63 
    64 lemma [code abstract]:
    65   "integer_of_nat 0 = 0"
    66   by transfer simp
    67 
    68 lemma [code abstract]:
    69   "integer_of_nat 1 = 1"
    70   by transfer simp
    71 
    72 lemma [code]:
    73   "Suc n = n + 1"
    74   by simp
    75 
    76 lemma [code abstract]:
    77   "integer_of_nat (m + n) = of_nat m + of_nat n"
    78   by transfer simp
    79 
    80 lemma [code abstract]:
    81   "integer_of_nat (m - n) = max 0 (of_nat m - of_nat n)"
    82   by transfer simp
    83 
    84 lemma [code abstract]:
    85   "integer_of_nat (m * n) = of_nat m * of_nat n"
    86   by transfer (simp add: of_nat_mult)
    87 
    88 lemma [code abstract]:
    89   "integer_of_nat (m div n) = of_nat m div of_nat n"
    90   by transfer (simp add: zdiv_int)
    91 
    92 lemma [code abstract]:
    93   "integer_of_nat (m mod n) = of_nat m mod of_nat n"
    94   by transfer (simp add: zmod_int)
    95 
    96 context
    97   includes integer.lifting
    98 begin
    99 
   100 lemma divmod_nat_code [code]: \<^marker>\<open>contributor \<open>René Thiemann\<close>\<close> \<^marker>\<open>contributor \<open>Akihisa Yamada\<close>\<close>
   101   "Divides.divmod_nat m n = (
   102      let k = integer_of_nat m; l = integer_of_nat n
   103      in map_prod nat_of_integer nat_of_integer
   104        (if k = 0 then (0, 0)
   105         else if l = 0 then (0, k) else
   106           Code_Numeral.divmod_abs k l))"
   107   by (simp add: prod_eq_iff Let_def; transfer)
   108     (simp add: nat_div_distrib nat_mod_distrib)
   109 
   110 end
   111 
   112 lemma [code]:
   113   "divmod m n = map_prod nat_of_integer nat_of_integer (divmod m n)"
   114   by (simp only: prod_eq_iff divmod_def map_prod_def case_prod_beta fst_conv snd_conv; transfer)
   115     (simp_all only: nat_div_distrib nat_mod_distrib
   116         zero_le_numeral nat_numeral)
   117   
   118 lemma [code]:
   119   "HOL.equal m n = HOL.equal (of_nat m :: integer) (of_nat n)"
   120   by transfer (simp add: equal)
   121 
   122 lemma [code]:
   123   "m \<le> n \<longleftrightarrow> (of_nat m :: integer) \<le> of_nat n"
   124   by simp
   125 
   126 lemma [code]:
   127   "m < n \<longleftrightarrow> (of_nat m :: integer) < of_nat n"
   128   by simp
   129 
   130 lemma num_of_nat_code [code]:
   131   "num_of_nat = num_of_integer \<circ> of_nat"
   132   by transfer (simp add: fun_eq_iff)
   133 
   134 end
   135 
   136 lemma (in semiring_1) of_nat_code_if:
   137   "of_nat n = (if n = 0 then 0
   138      else let
   139        (m, q) = Divides.divmod_nat n 2;
   140        m' = 2 * of_nat m
   141      in if q = 0 then m' else m' + 1)"
   142 proof -
   143   from div_mult_mod_eq have *: "of_nat n = of_nat (n div 2 * 2 + n mod 2)" by simp
   144   show ?thesis
   145     by (simp add: Let_def divmod_nat_def of_nat_add [symmetric])
   146       (simp add: * mult.commute of_nat_mult add.commute)
   147 qed
   148 
   149 declare of_nat_code_if [code]
   150 
   151 definition int_of_nat :: "nat \<Rightarrow> int" where
   152   [code_abbrev]: "int_of_nat = of_nat"
   153 
   154 lemma [code]:
   155   "int_of_nat n = int_of_integer (of_nat n)"
   156   by (simp add: int_of_nat_def)
   157 
   158 lemma [code abstract]:
   159   "integer_of_nat (nat k) = max 0 (integer_of_int k)"
   160   including integer.lifting by transfer auto
   161 
   162 definition char_of_nat :: "nat \<Rightarrow> char"
   163   where [code_abbrev]: "char_of_nat = char_of"
   164 
   165 definition nat_of_char :: "char \<Rightarrow> nat"
   166   where [code_abbrev]: "nat_of_char = of_char"
   167 
   168 lemma [code]:
   169   "char_of_nat = char_of_integer \<circ> integer_of_nat"
   170   including integer.lifting unfolding char_of_integer_def char_of_nat_def
   171   by transfer (simp add: fun_eq_iff)
   172 
   173 lemma [code abstract]:
   174   "integer_of_nat (nat_of_char c) = integer_of_char c"
   175   by (cases c) (simp add: nat_of_char_def integer_of_char_def integer_of_nat_eq_of_nat)
   176 
   177 lemma term_of_nat_code [code]:
   178   \<comment> \<open>Use \<^term>\<open>Code_Numeral.nat_of_integer\<close> in term reconstruction
   179         instead of \<^term>\<open>Code_Target_Nat.Nat\<close> such that reconstructed
   180         terms can be fed back to the code generator\<close>
   181   "term_of_class.term_of n =
   182    Code_Evaluation.App
   183      (Code_Evaluation.Const (STR ''Code_Numeral.nat_of_integer'')
   184         (typerep.Typerep (STR ''fun'')
   185            [typerep.Typerep (STR ''Code_Numeral.integer'') [],
   186          typerep.Typerep (STR ''Nat.nat'') []]))
   187      (term_of_class.term_of (integer_of_nat n))"
   188   by (simp add: term_of_anything)
   189 
   190 lemma nat_of_integer_code_post [code_post]:
   191   "nat_of_integer 0 = 0"
   192   "nat_of_integer 1 = 1"
   193   "nat_of_integer (numeral k) = numeral k"
   194   including integer.lifting by (transfer, simp)+
   195 
   196 code_identifier
   197   code_module Code_Target_Nat \<rightharpoonup>
   198     (SML) Arith and (OCaml) Arith and (Haskell) Arith
   199 
   200 end