src/HOL/Library/Code_Integer.thy
author haftmann
Mon, 23 Jul 2012 09:28:03 +0200
changeset 49446 6efff142bb54
parent 49088 1b609a7837ef
permissions -rw-r--r--
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
     1 (*  Title:      HOL/Library/Code_Integer.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Pretty integer literals for code generation *}
     6 
     7 theory Code_Integer
     8 imports Main Code_Natural
     9 begin
    10 
    11 text {*
    12   Representation-ignorant code equations for conversions.
    13 *}
    14 
    15 lemma nat_code [code]:
    16   "nat k = (if k \<le> 0 then 0 else
    17      let
    18        (l, j) = divmod_int k 2;
    19        n = nat l;
    20        l' = n + n
    21      in if j = 0 then l' else Suc l')"
    22 proof -
    23   have "2 = nat 2" by simp
    24   show ?thesis
    25     apply (subst mult_2 [symmetric])
    26     apply (auto simp add: Let_def divmod_int_mod_div not_le
    27      nat_div_distrib nat_mult_distrib mult_div_cancel mod_2_not_eq_zero_eq_one_int)
    28     apply (unfold `2 = nat 2`)
    29     apply (subst nat_mod_distrib [symmetric])
    30     apply simp_all
    31   done
    32 qed
    33 
    34 lemma (in ring_1) of_int_code:
    35   "of_int k = (if k = 0 then 0
    36      else if k < 0 then - of_int (- k)
    37      else let
    38        (l, j) = divmod_int k 2;
    39        l' = 2 * of_int l
    40      in if j = 0 then l' else l' + 1)"
    41 proof -
    42   from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
    43   show ?thesis
    44     by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
    45       of_int_add [symmetric]) (simp add: * mult_commute)
    46 qed
    47 
    48 declare of_int_code [code]
    49 
    50 text {*
    51   HOL numeral expressions are mapped to integer literals
    52   in target languages, using predefined target language
    53   operations for abstract integer operations.
    54 *}
    55 
    56 code_type int
    57   (SML "IntInf.int")
    58   (OCaml "Big'_int.big'_int")
    59   (Haskell "Integer")
    60   (Scala "BigInt")
    61   (Eval "int")
    62 
    63 code_instance int :: equal
    64   (Haskell -)
    65 
    66 code_const "0::int"
    67   (SML "0")
    68   (OCaml "Big'_int.zero'_big'_int")
    69   (Haskell "0")
    70   (Scala "BigInt(0)")
    71 
    72 setup {*
    73   fold (Numeral.add_code @{const_name Int.Pos}
    74     false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
    75 *}
    76 
    77 setup {*
    78   fold (Numeral.add_code @{const_name Int.Neg}
    79     true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
    80 *}
    81 
    82 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    83   (SML "IntInf.+ ((_), (_))")
    84   (OCaml "Big'_int.add'_big'_int")
    85   (Haskell infixl 6 "+")
    86   (Scala infixl 7 "+")
    87   (Eval infixl 8 "+")
    88 
    89 code_const "uminus \<Colon> int \<Rightarrow> int"
    90   (SML "IntInf.~")
    91   (OCaml "Big'_int.minus'_big'_int")
    92   (Haskell "negate")
    93   (Scala "!(- _)")
    94   (Eval "~/ _")
    95 
    96 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
    97   (SML "IntInf.- ((_), (_))")
    98   (OCaml "Big'_int.sub'_big'_int")
    99   (Haskell infixl 6 "-")
   100   (Scala infixl 7 "-")
   101   (Eval infixl 8 "-")
   102 
   103 code_const Int.dup
   104   (SML "IntInf.*/ (2,/ (_))")
   105   (OCaml "Big'_int.mult'_big'_int/ 2")
   106   (Haskell "!(2 * _)")
   107   (Scala "!(2 * _)")
   108   (Eval "!(2 * _)")
   109 
   110 code_const Int.sub
   111   (SML "!(raise/ Fail/ \"sub\")")
   112   (OCaml "failwith/ \"sub\"")
   113   (Haskell "error/ \"sub\"")
   114   (Scala "!sys.error(\"sub\")")
   115 
   116 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
   117   (SML "IntInf.* ((_), (_))")
   118   (OCaml "Big'_int.mult'_big'_int")
   119   (Haskell infixl 7 "*")
   120   (Scala infixl 8 "*")
   121   (Eval infixl 9 "*")
   122 
   123 code_const pdivmod
   124   (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
   125   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
   126   (Haskell "divMod/ (abs _)/ (abs _)")
   127   (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
   128   (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
   129 
   130 code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   131   (SML "!((_ : IntInf.int) = _)")
   132   (OCaml "Big'_int.eq'_big'_int")
   133   (Haskell infix 4 "==")
   134   (Scala infixl 5 "==")
   135   (Eval infixl 6 "=")
   136 
   137 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   138   (SML "IntInf.<= ((_), (_))")
   139   (OCaml "Big'_int.le'_big'_int")
   140   (Haskell infix 4 "<=")
   141   (Scala infixl 4 "<=")
   142   (Eval infixl 6 "<=")
   143 
   144 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
   145   (SML "IntInf.< ((_), (_))")
   146   (OCaml "Big'_int.lt'_big'_int")
   147   (Haskell infix 4 "<")
   148   (Scala infixl 4 "<")
   149   (Eval infixl 6 "<")
   150 
   151 code_const Code_Numeral.int_of
   152   (SML "IntInf.fromInt")
   153   (OCaml "_")
   154   (Haskell "Prelude.toInteger")
   155   (Scala "!_.as'_BigInt")
   156   (Eval "_")
   157 
   158 code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
   159   (Eval "HOLogic.mk'_number/ HOLogic.intT")
   160 
   161 end