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