src/HOL/Library/Code_Target_Int.thy
author haftmann
Sun Oct 16 09:31:05 2016 +0200 (2016-10-16)
changeset 64242 93c6f0da5c70
parent 63351 e5d08b1d8fea
child 64997 067a6cca39f0
permissions -rw-r--r--
more standardized theorem names for facts involving the div and mod identity
     1 (*  Title:      HOL/Library/Code_Target_Int.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 section \<open>Implementation of integer numbers by target-language integers\<close>
     6 
     7 theory Code_Target_Int
     8 imports "../GCD"
     9 begin
    10 
    11 code_datatype int_of_integer
    12 
    13 declare [[code drop: integer_of_int]]
    14 
    15 context
    16 includes integer.lifting
    17 begin
    18 
    19 lemma [code]:
    20   "integer_of_int (int_of_integer k) = k"
    21   by transfer rule
    22 
    23 lemma [code]:
    24   "Int.Pos = int_of_integer \<circ> integer_of_num"
    25   by transfer (simp add: fun_eq_iff) 
    26 
    27 lemma [code]:
    28   "Int.Neg = int_of_integer \<circ> uminus \<circ> integer_of_num"
    29   by transfer (simp add: fun_eq_iff) 
    30 
    31 lemma [code_abbrev]:
    32   "int_of_integer (numeral k) = Int.Pos k"
    33   by transfer simp
    34 
    35 lemma [code_abbrev]:
    36   "int_of_integer (- numeral k) = Int.Neg k"
    37   by transfer simp
    38   
    39 lemma [code, symmetric, code_post]:
    40   "0 = int_of_integer 0"
    41   by transfer simp
    42 
    43 lemma [code, symmetric, code_post]:
    44   "1 = int_of_integer 1"
    45   by transfer simp
    46 
    47 lemma [code_post]:
    48   "int_of_integer (- 1) = - 1"
    49   by simp
    50 
    51 lemma [code]:
    52   "k + l = int_of_integer (of_int k + of_int l)"
    53   by transfer simp
    54 
    55 lemma [code]:
    56   "- k = int_of_integer (- of_int k)"
    57   by transfer simp
    58 
    59 lemma [code]:
    60   "k - l = int_of_integer (of_int k - of_int l)"
    61   by transfer simp
    62 
    63 lemma [code]:
    64   "Int.dup k = int_of_integer (Code_Numeral.dup (of_int k))"
    65   by transfer simp
    66 
    67 declare [[code drop: Int.sub]]
    68 
    69 lemma [code]:
    70   "k * l = int_of_integer (of_int k * of_int l)"
    71   by simp
    72 
    73 lemma [code]:
    74   "k div l = int_of_integer (of_int k div of_int l)"
    75   by simp
    76 
    77 lemma [code]:
    78   "k mod l = int_of_integer (of_int k mod of_int l)"
    79   by simp
    80 
    81 lemma [code]:
    82   "divmod m n = map_prod int_of_integer int_of_integer (divmod m n)"
    83   unfolding prod_eq_iff divmod_def map_prod_def case_prod_beta fst_conv snd_conv
    84   by transfer simp
    85 
    86 lemma [code]:
    87   "HOL.equal k l = HOL.equal (of_int k :: integer) (of_int l)"
    88   by transfer (simp add: equal)
    89 
    90 lemma [code]:
    91   "k \<le> l \<longleftrightarrow> (of_int k :: integer) \<le> of_int l"
    92   by transfer rule
    93 
    94 lemma [code]:
    95   "k < l \<longleftrightarrow> (of_int k :: integer) < of_int l"
    96   by transfer rule
    97 
    98 declare [[code drop: "gcd :: int \<Rightarrow> _" "lcm :: int \<Rightarrow> _"]]
    99 
   100 lemma gcd_int_of_integer [code]:
   101   "gcd (int_of_integer x) (int_of_integer y) = int_of_integer (gcd x y)"
   102 by transfer rule
   103 
   104 lemma lcm_int_of_integer [code]:
   105   "lcm (int_of_integer x) (int_of_integer y) = int_of_integer (lcm x y)"
   106 by transfer rule
   107 
   108 end
   109 
   110 lemma (in ring_1) of_int_code_if:
   111   "of_int k = (if k = 0 then 0
   112      else if k < 0 then - of_int (- k)
   113      else let
   114        l = 2 * of_int (k div 2);
   115        j = k mod 2
   116      in if j = 0 then l else l + 1)"
   117 proof -
   118   from div_mult_mod_eq have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
   119   show ?thesis
   120     by (simp add: Let_def of_int_add [symmetric]) (simp add: * mult.commute)
   121 qed
   122 
   123 declare of_int_code_if [code]
   124 
   125 lemma [code]:
   126   "nat = nat_of_integer \<circ> of_int"
   127   including integer.lifting by transfer (simp add: fun_eq_iff)
   128 
   129 code_identifier
   130   code_module Code_Target_Int \<rightharpoonup>
   131     (SML) Arith and (OCaml) Arith and (Haskell) Arith
   132 
   133 end