src/HOL/Library/Code_Integer.thy
author haftmann
Sat Jul 24 18:08:41 2010 +0200 (2010-07-24)
changeset 37958 9728342bcd56
parent 37947 844977c7abeb
child 37968 52fdcb76c0af
permissions -rw-r--r--
another refinement chapter in the neverending numeral story
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@31203
     8
imports Main
haftmann@24999
     9
begin
haftmann@24999
    10
haftmann@24999
    11
text {*
haftmann@24999
    12
  HOL numeral expressions are mapped to integer literals
haftmann@24999
    13
  in target languages, using predefined target language
haftmann@24999
    14
  operations for abstract integer operations.
haftmann@24999
    15
*}
haftmann@24999
    16
haftmann@37958
    17
text {*
haftmann@37958
    18
  Preliminary: alternative representation of @{typ code_numeral}
haftmann@37958
    19
  for @{text Haskell} and @{text Scala}.
haftmann@37958
    20
*}
haftmann@37958
    21
haftmann@37958
    22
code_include Haskell "Natural" {*
haftmann@37958
    23
newtype Natural = Natural Integer deriving (Eq, Show, Read);
haftmann@37958
    24
haftmann@37958
    25
instance Num Natural where {
haftmann@37958
    26
  fromInteger k = Natural (if k >= 0 then k else 0);
haftmann@37958
    27
  Natural n + Natural m = Natural (n + m);
haftmann@37958
    28
  Natural n - Natural m = fromInteger (n - m);
haftmann@37958
    29
  Natural n * Natural m = Natural (n * m);
haftmann@37958
    30
  abs n = n;
haftmann@37958
    31
  signum _ = 1;
haftmann@37958
    32
  negate n = error "negate Natural";
haftmann@37958
    33
};
haftmann@37958
    34
haftmann@37958
    35
instance Ord Natural where {
haftmann@37958
    36
  Natural n <= Natural m = n <= m;
haftmann@37958
    37
  Natural n < Natural m = n < m;
haftmann@37958
    38
};
haftmann@37958
    39
haftmann@37958
    40
instance Real Natural where {
haftmann@37958
    41
  toRational (Natural n) = toRational n;
haftmann@37958
    42
};
haftmann@37958
    43
haftmann@37958
    44
instance Enum Natural where {
haftmann@37958
    45
  toEnum k = fromInteger (toEnum k);
haftmann@37958
    46
  fromEnum (Natural n) = fromEnum n;
haftmann@37958
    47
};
haftmann@37958
    48
haftmann@37958
    49
instance Integral Natural where {
haftmann@37958
    50
  toInteger (Natural n) = n;
haftmann@37958
    51
  divMod n m = quotRem n m;
haftmann@37958
    52
  quotRem (Natural n) (Natural m)
haftmann@37958
    53
    | (m == 0) = (0, Natural n)
haftmann@37958
    54
    | otherwise = (Natural k, Natural l) where (k, l) = quotRem n m;
haftmann@37958
    55
};
haftmann@37958
    56
*}
haftmann@37958
    57
haftmann@37958
    58
code_reserved Haskell Natural
haftmann@37958
    59
haftmann@37958
    60
code_include Scala "Natural" {*
haftmann@37958
    61
import scala.Math
haftmann@37958
    62
haftmann@37958
    63
object Natural {
haftmann@37958
    64
haftmann@37958
    65
  def apply(numeral: BigInt): Natural = new Natural(numeral max 0)
haftmann@37958
    66
  def apply(numeral: Int): Natural = Natural(BigInt(numeral))
haftmann@37958
    67
  def apply(numeral: String): Natural = Natural(BigInt(numeral))
haftmann@37958
    68
haftmann@37958
    69
}
haftmann@37958
    70
haftmann@37958
    71
class Natural private(private val value: BigInt) {
haftmann@37958
    72
haftmann@37958
    73
  override def hashCode(): Int = this.value.hashCode()
haftmann@37958
    74
haftmann@37958
    75
  override def equals(that: Any): Boolean = that match {
haftmann@37958
    76
    case that: Natural => this equals that
haftmann@37958
    77
    case _ => false
haftmann@37958
    78
  }
haftmann@37958
    79
haftmann@37958
    80
  override def toString(): String = this.value.toString
haftmann@37958
    81
haftmann@37958
    82
  def equals(that: Natural): Boolean = this.value == that.value
haftmann@37958
    83
haftmann@37958
    84
  def as_BigInt: BigInt = this.value
haftmann@37958
    85
  def as_Int: Int = if (this.value >= Int.MinValue && this.value <= Int.MaxValue)
haftmann@37958
    86
      this.value.intValue
haftmann@37958
    87
    else this.value.intValue
haftmann@37958
    88
haftmann@37958
    89
  def +(that: Natural): Natural = new Natural(this.value + that.value)
haftmann@37958
    90
  def -(that: Natural): Natural = Natural(this.value - that.value)
haftmann@37958
    91
  def *(that: Natural): Natural = new Natural(this.value * that.value)
haftmann@37958
    92
haftmann@37958
    93
  def /%(that: Natural): (Natural, Natural) = if (that.value == 0) (new Natural(0), this)
haftmann@37958
    94
    else {
haftmann@37958
    95
      val (k, l) = this.value /% that.value
haftmann@37958
    96
      (new Natural(k), new Natural(l))
haftmann@37958
    97
    }
haftmann@37958
    98
haftmann@37958
    99
  def <=(that: Natural): Boolean = this.value <= that.value
haftmann@37958
   100
haftmann@37958
   101
  def <(that: Natural): Boolean = this.value < that.value
haftmann@37958
   102
haftmann@37958
   103
}
haftmann@37958
   104
*}
haftmann@37958
   105
haftmann@37958
   106
code_reserved Scala Natural
haftmann@37958
   107
haftmann@37958
   108
code_type code_numeral
haftmann@37958
   109
  (Haskell "Natural.Natural")
haftmann@37958
   110
  (Scala "Natural")
haftmann@37958
   111
haftmann@37958
   112
setup {*
haftmann@37958
   113
  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@37958
   114
    false Code_Printer.literal_alternative_numeral) ["Haskell", "Scala"]
haftmann@37958
   115
*}
haftmann@37958
   116
haftmann@37958
   117
code_instance code_numeral :: eq
haftmann@37958
   118
  (Haskell -)
haftmann@37958
   119
haftmann@37958
   120
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   121
  (Haskell infixl 6 "+")
haftmann@37958
   122
  (Scala infixl 7 "+")
haftmann@37958
   123
haftmann@37958
   124
code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   125
  (Haskell infixl 6 "-")
haftmann@37958
   126
  (Scala infixl 7 "-")
haftmann@37958
   127
haftmann@37958
   128
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   129
  (Haskell infixl 7 "*")
haftmann@37958
   130
  (Scala infixl 8 "*")
haftmann@37958
   131
haftmann@37958
   132
code_const div_mod_code_numeral
haftmann@37958
   133
  (Haskell "divMod")
haftmann@37958
   134
  (Scala infixl 8 "/%")
haftmann@37958
   135
haftmann@37958
   136
code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   137
  (Haskell infixl 4 "==")
haftmann@37958
   138
  (Scala infixl 5 "==")
haftmann@37958
   139
haftmann@37958
   140
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   141
  (Haskell infix 4 "<=")
haftmann@37958
   142
  (Scala infixl 4 "<=")
haftmann@37958
   143
haftmann@37958
   144
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   145
  (Haskell infix 4 "<")
haftmann@37958
   146
  (Scala infixl 4 "<")
haftmann@37958
   147
haftmann@37958
   148
text {*
haftmann@37958
   149
  Setup for @{typ int} proper.
haftmann@37958
   150
*}
haftmann@37958
   151
haftmann@37958
   152
haftmann@24999
   153
code_type int
haftmann@24999
   154
  (SML "IntInf.int")
haftmann@24999
   155
  (OCaml "Big'_int.big'_int")
haftmann@24999
   156
  (Haskell "Integer")
haftmann@34899
   157
  (Scala "BigInt")
haftmann@24999
   158
haftmann@24999
   159
code_instance int :: eq
haftmann@24999
   160
  (Haskell -)
haftmann@24999
   161
haftmann@24999
   162
setup {*
haftmann@25928
   163
  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
haftmann@34944
   164
    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
haftmann@24999
   165
*}
haftmann@24999
   166
huffman@26086
   167
code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
haftmann@24999
   168
  (SML "raise/ Fail/ \"Pls\""
haftmann@24999
   169
     and "raise/ Fail/ \"Min\""
huffman@26086
   170
     and "!((_);/ raise/ Fail/ \"Bit0\")"
huffman@26086
   171
     and "!((_);/ raise/ Fail/ \"Bit1\")")
haftmann@24999
   172
  (OCaml "failwith/ \"Pls\""
haftmann@24999
   173
     and "failwith/ \"Min\""
huffman@26086
   174
     and "!((_);/ failwith/ \"Bit0\")"
huffman@26086
   175
     and "!((_);/ failwith/ \"Bit1\")")
haftmann@24999
   176
  (Haskell "error/ \"Pls\""
haftmann@24999
   177
     and "error/ \"Min\""
huffman@26086
   178
     and "error/ \"Bit0\""
huffman@26086
   179
     and "error/ \"Bit1\"")
haftmann@34899
   180
  (Scala "!error(\"Pls\")"
haftmann@34899
   181
     and "!error(\"Min\")"
haftmann@34899
   182
     and "!error(\"Bit0\")"
haftmann@34899
   183
     and "!error(\"Bit1\")")
haftmann@34899
   184
haftmann@25919
   185
code_const Int.pred
haftmann@24999
   186
  (SML "IntInf.- ((_), 1)")
haftmann@24999
   187
  (OCaml "Big'_int.pred'_big'_int")
haftmann@24999
   188
  (Haskell "!(_/ -/ 1)")
haftmann@34899
   189
  (Scala "!(_/ -/ 1)")
haftmann@37958
   190
  (Eval "!(_/ -/ 1)")
haftmann@24999
   191
haftmann@25919
   192
code_const Int.succ
haftmann@24999
   193
  (SML "IntInf.+ ((_), 1)")
haftmann@24999
   194
  (OCaml "Big'_int.succ'_big'_int")
haftmann@24999
   195
  (Haskell "!(_/ +/ 1)")
haftmann@34899
   196
  (Scala "!(_/ +/ 1)")
haftmann@37958
   197
  (Eval "!(_/ +/ 1)")
haftmann@24999
   198
haftmann@24999
   199
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
   200
  (SML "IntInf.+ ((_), (_))")
haftmann@24999
   201
  (OCaml "Big'_int.add'_big'_int")
haftmann@24999
   202
  (Haskell infixl 6 "+")
haftmann@34899
   203
  (Scala infixl 7 "+")
haftmann@37958
   204
  (Eval infixl 8 "+")
haftmann@24999
   205
haftmann@24999
   206
code_const "uminus \<Colon> int \<Rightarrow> int"
haftmann@24999
   207
  (SML "IntInf.~")
haftmann@24999
   208
  (OCaml "Big'_int.minus'_big'_int")
haftmann@24999
   209
  (Haskell "negate")
haftmann@34899
   210
  (Scala "!(- _)")
haftmann@37958
   211
  (Eval "~/ _")
haftmann@24999
   212
haftmann@24999
   213
code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
   214
  (SML "IntInf.- ((_), (_))")
haftmann@24999
   215
  (OCaml "Big'_int.sub'_big'_int")
haftmann@24999
   216
  (Haskell infixl 6 "-")
haftmann@34899
   217
  (Scala infixl 7 "-")
haftmann@37958
   218
  (Eval infixl 8 "-")
haftmann@24999
   219
haftmann@24999
   220
code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
haftmann@24999
   221
  (SML "IntInf.* ((_), (_))")
haftmann@24999
   222
  (OCaml "Big'_int.mult'_big'_int")
haftmann@24999
   223
  (Haskell infixl 7 "*")
haftmann@34899
   224
  (Scala infixl 8 "*")
haftmann@37958
   225
  (Eval infixl 9 "*")
haftmann@24999
   226
haftmann@29936
   227
code_const pdivmod
haftmann@34899
   228
  (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
haftmann@34899
   229
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
haftmann@34944
   230
  (Haskell "divMod/ (abs _)/ (abs _)")
haftmann@37958
   231
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
haftmann@37958
   232
  (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
haftmann@29936
   233
haftmann@28346
   234
code_const "eq_class.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   235
  (SML "!((_ : IntInf.int) = _)")
haftmann@24999
   236
  (OCaml "Big'_int.eq'_big'_int")
haftmann@24999
   237
  (Haskell infixl 4 "==")
haftmann@34899
   238
  (Scala infixl 5 "==")
haftmann@37958
   239
  (Eval infixl 6 "=")
haftmann@24999
   240
haftmann@24999
   241
code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   242
  (SML "IntInf.<= ((_), (_))")
haftmann@24999
   243
  (OCaml "Big'_int.le'_big'_int")
haftmann@24999
   244
  (Haskell infix 4 "<=")
haftmann@34899
   245
  (Scala infixl 4 "<=")
haftmann@37958
   246
  (Eval infixl 6 "<=")
haftmann@24999
   247
haftmann@24999
   248
code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
haftmann@24999
   249
  (SML "IntInf.< ((_), (_))")
haftmann@24999
   250
  (OCaml "Big'_int.lt'_big'_int")
haftmann@24999
   251
  (Haskell infix 4 "<")
haftmann@37958
   252
  (Scala infixl 4 "<")
haftmann@37958
   253
  (Eval infixl 6 "<")
haftmann@24999
   254
haftmann@31205
   255
code_const Code_Numeral.int_of
haftmann@31192
   256
  (SML "IntInf.fromInt")
haftmann@31377
   257
  (OCaml "_")
haftmann@37958
   258
  (Haskell "toInteger")
haftmann@37958
   259
  (Scala "!_.as'_BigInt")
haftmann@37958
   260
  (Eval "_")
haftmann@24999
   261
haftmann@28228
   262
text {* Evaluation *}
haftmann@28228
   263
haftmann@32657
   264
code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
haftmann@31192
   265
  (Eval "HOLogic.mk'_number/ HOLogic.intT")
haftmann@28228
   266
haftmann@24999
   267
end