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