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