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