src/HOL/Library/Code_Natural.thy
author haftmann
Mon Jul 26 11:10:35 2010 +0200 (2010-07-26)
changeset 37968 52fdcb76c0af
parent 37958 src/HOL/Library/Code_Integer.thy@9728342bcd56
child 38771 f9cd27cbe8a4
permissions -rw-r--r--
added Code_Natural.thy
haftmann@37968
     1
(*  Title:      HOL/Library/Code_Natural.thy
haftmann@24999
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24999
     3
*)
haftmann@24999
     4
haftmann@37968
     5
theory Code_Natural
haftmann@31203
     6
imports Main
haftmann@24999
     7
begin
haftmann@24999
     8
haftmann@37968
     9
section {* Alternative representation of @{typ code_numeral} for @{text Haskell} and @{text Scala} *}
haftmann@24999
    10
haftmann@37968
    11
code_include Haskell "Natural"
haftmann@37968
    12
{*import Data.Array.ST;
haftmann@37958
    13
haftmann@37958
    14
newtype Natural = Natural Integer deriving (Eq, Show, Read);
haftmann@37958
    15
haftmann@37958
    16
instance Num Natural where {
haftmann@37958
    17
  fromInteger k = Natural (if k >= 0 then k else 0);
haftmann@37958
    18
  Natural n + Natural m = Natural (n + m);
haftmann@37958
    19
  Natural n - Natural m = fromInteger (n - m);
haftmann@37958
    20
  Natural n * Natural m = Natural (n * m);
haftmann@37958
    21
  abs n = n;
haftmann@37958
    22
  signum _ = 1;
haftmann@37958
    23
  negate n = error "negate Natural";
haftmann@37958
    24
};
haftmann@37958
    25
haftmann@37958
    26
instance Ord Natural where {
haftmann@37958
    27
  Natural n <= Natural m = n <= m;
haftmann@37958
    28
  Natural n < Natural m = n < m;
haftmann@37958
    29
};
haftmann@37958
    30
haftmann@37968
    31
instance Ix Natural where {
haftmann@37968
    32
  range (Natural n, Natural m) = map Natural (range (n, m));
haftmann@37968
    33
  index (Natural n, Natural m) (Natural q) = index (n, m) q;
haftmann@37968
    34
  inRange (Natural n, Natural m) (Natural q) = inRange (n, m) q;
haftmann@37968
    35
  rangeSize (Natural n, Natural m) = rangeSize (n, m);
haftmann@37968
    36
};
haftmann@37968
    37
haftmann@37958
    38
instance Real Natural where {
haftmann@37958
    39
  toRational (Natural n) = toRational n;
haftmann@37958
    40
};
haftmann@37958
    41
haftmann@37958
    42
instance Enum Natural where {
haftmann@37958
    43
  toEnum k = fromInteger (toEnum k);
haftmann@37958
    44
  fromEnum (Natural n) = fromEnum n;
haftmann@37958
    45
};
haftmann@37958
    46
haftmann@37958
    47
instance Integral Natural where {
haftmann@37958
    48
  toInteger (Natural n) = n;
haftmann@37958
    49
  divMod n m = quotRem n m;
haftmann@37958
    50
  quotRem (Natural n) (Natural m)
haftmann@37958
    51
    | (m == 0) = (0, Natural n)
haftmann@37958
    52
    | otherwise = (Natural k, Natural l) where (k, l) = quotRem n m;
haftmann@37968
    53
};*}
haftmann@37958
    54
haftmann@37958
    55
code_reserved Haskell Natural
haftmann@37958
    56
haftmann@37958
    57
code_include Scala "Natural" {*
haftmann@37958
    58
import scala.Math
haftmann@37958
    59
haftmann@37958
    60
object Natural {
haftmann@37958
    61
haftmann@37958
    62
  def apply(numeral: BigInt): Natural = new Natural(numeral max 0)
haftmann@37958
    63
  def apply(numeral: Int): Natural = Natural(BigInt(numeral))
haftmann@37958
    64
  def apply(numeral: String): Natural = Natural(BigInt(numeral))
haftmann@37958
    65
haftmann@37958
    66
}
haftmann@37958
    67
haftmann@37958
    68
class Natural private(private val value: BigInt) {
haftmann@37958
    69
haftmann@37958
    70
  override def hashCode(): Int = this.value.hashCode()
haftmann@37958
    71
haftmann@37958
    72
  override def equals(that: Any): Boolean = that match {
haftmann@37958
    73
    case that: Natural => this equals that
haftmann@37958
    74
    case _ => false
haftmann@37958
    75
  }
haftmann@37958
    76
haftmann@37958
    77
  override def toString(): String = this.value.toString
haftmann@37958
    78
haftmann@37958
    79
  def equals(that: Natural): Boolean = this.value == that.value
haftmann@37958
    80
haftmann@37958
    81
  def as_BigInt: BigInt = this.value
haftmann@37958
    82
  def as_Int: Int = if (this.value >= Int.MinValue && this.value <= Int.MaxValue)
haftmann@37958
    83
      this.value.intValue
haftmann@37968
    84
    else error("Int value out of range: " + this.value.toString)
haftmann@37958
    85
haftmann@37958
    86
  def +(that: Natural): Natural = new Natural(this.value + that.value)
haftmann@37958
    87
  def -(that: Natural): Natural = Natural(this.value - that.value)
haftmann@37958
    88
  def *(that: Natural): Natural = new Natural(this.value * that.value)
haftmann@37958
    89
haftmann@37958
    90
  def /%(that: Natural): (Natural, Natural) = if (that.value == 0) (new Natural(0), this)
haftmann@37958
    91
    else {
haftmann@37958
    92
      val (k, l) = this.value /% that.value
haftmann@37958
    93
      (new Natural(k), new Natural(l))
haftmann@37958
    94
    }
haftmann@37958
    95
haftmann@37958
    96
  def <=(that: Natural): Boolean = this.value <= that.value
haftmann@37958
    97
haftmann@37958
    98
  def <(that: Natural): Boolean = this.value < that.value
haftmann@37958
    99
haftmann@37958
   100
}
haftmann@37958
   101
*}
haftmann@37958
   102
haftmann@37958
   103
code_reserved Scala Natural
haftmann@37958
   104
haftmann@37958
   105
code_type code_numeral
haftmann@37958
   106
  (Haskell "Natural.Natural")
haftmann@37958
   107
  (Scala "Natural")
haftmann@37958
   108
haftmann@37958
   109
setup {*
haftmann@37958
   110
  fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
haftmann@37958
   111
    false Code_Printer.literal_alternative_numeral) ["Haskell", "Scala"]
haftmann@37958
   112
*}
haftmann@37958
   113
haftmann@37958
   114
code_instance code_numeral :: eq
haftmann@37958
   115
  (Haskell -)
haftmann@37958
   116
haftmann@37958
   117
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   118
  (Haskell infixl 6 "+")
haftmann@37958
   119
  (Scala infixl 7 "+")
haftmann@37958
   120
haftmann@37958
   121
code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   122
  (Haskell infixl 6 "-")
haftmann@37958
   123
  (Scala infixl 7 "-")
haftmann@37958
   124
haftmann@37958
   125
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
haftmann@37958
   126
  (Haskell infixl 7 "*")
haftmann@37958
   127
  (Scala infixl 8 "*")
haftmann@37958
   128
haftmann@37958
   129
code_const div_mod_code_numeral
haftmann@37958
   130
  (Haskell "divMod")
haftmann@37958
   131
  (Scala infixl 8 "/%")
haftmann@37958
   132
haftmann@37958
   133
code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   134
  (Haskell infixl 4 "==")
haftmann@37958
   135
  (Scala infixl 5 "==")
haftmann@37958
   136
haftmann@37958
   137
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   138
  (Haskell infix 4 "<=")
haftmann@37958
   139
  (Scala infixl 4 "<=")
haftmann@37958
   140
haftmann@37958
   141
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
haftmann@37958
   142
  (Haskell infix 4 "<")
haftmann@37958
   143
  (Scala infixl 4 "<")
haftmann@37958
   144
haftmann@37968
   145
end