src/HOL/Library/Code_Natural.thy
 author haftmann Thu Aug 26 10:16:22 2010 +0200 (2010-08-26 ago) changeset 38771 f9cd27cbe8a4 parent 37968 52fdcb76c0af child 38774 567b94f8bb6e permissions -rw-r--r--
code_include Scala: qualify module nmae
```     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 code_reserved Haskell Natural
```
```    56
```
```    57 code_include Scala "Natural" {*
```
```    58 import scala.Math
```
```    59
```
```    60 object Nat {
```
```    61
```
```    62   def apply(numeral: BigInt): Nat = new Nat(numeral max 0)
```
```    63   def apply(numeral: Int): Nat = Nat(BigInt(numeral))
```
```    64   def apply(numeral: String): Nat = Nat(BigInt(numeral))
```
```    65
```
```    66 }
```
```    67
```
```    68 class Nat private(private val value: BigInt) {
```
```    69
```
```    70   override def hashCode(): Int = this.value.hashCode()
```
```    71
```
```    72   override def equals(that: Any): Boolean = that match {
```
```    73     case that: Nat => this equals that
```
```    74     case _ => false
```
```    75   }
```
```    76
```
```    77   override def toString(): String = this.value.toString
```
```    78
```
```    79   def equals(that: Nat): Boolean = this.value == that.value
```
```    80
```
```    81   def as_BigInt: BigInt = this.value
```
```    82   def as_Int: Int = if (this.value >= Int.MinValue && this.value <= Int.MaxValue)
```
```    83       this.value.intValue
```
```    84     else error("Int value out of range: " + this.value.toString)
```
```    85
```
```    86   def +(that: Nat): Nat = new Nat(this.value + that.value)
```
```    87   def -(that: Nat): Nat = Nat(this.value - that.value)
```
```    88   def *(that: Nat): Nat = new Nat(this.value * that.value)
```
```    89
```
```    90   def /%(that: Nat): (Nat, Nat) = if (that.value == 0) (new Nat(0), this)
```
```    91     else {
```
```    92       val (k, l) = this.value /% that.value
```
```    93       (new Nat(k), new Nat(l))
```
```    94     }
```
```    95
```
```    96   def <=(that: Nat): Boolean = this.value <= that.value
```
```    97
```
```    98   def <(that: Nat): Boolean = this.value < that.value
```
```    99
```
```   100 }
```
```   101 *}
```
```   102
```
```   103 code_reserved Scala Natural
```
```   104
```
```   105 code_type code_numeral
```
```   106   (Haskell "Natural.Natural")
```
```   107   (Scala "Natural.Nat")
```
```   108
```
```   109 setup {*
```
```   110   fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
```
```   111     false Code_Printer.literal_alternative_numeral) ["Haskell", "Scala"]
```
```   112 *}
```
```   113
```
```   114 code_instance code_numeral :: eq
```
```   115   (Haskell -)
```
```   116
```
```   117 code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
```
```   118   (Haskell infixl 6 "+")
```
```   119   (Scala infixl 7 "+")
```
```   120
```
```   121 code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
```
```   122   (Haskell infixl 6 "-")
```
```   123   (Scala infixl 7 "-")
```
```   124
```
```   125 code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
```
```   126   (Haskell infixl 7 "*")
```
```   127   (Scala infixl 8 "*")
```
```   128
```
```   129 code_const div_mod_code_numeral
```
```   130   (Haskell "divMod")
```
```   131   (Scala infixl 8 "/%")
```
```   132
```
```   133 code_const "eq_class.eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
```
```   134   (Haskell infixl 4 "==")
```
```   135   (Scala infixl 5 "==")
```
```   136
```
```   137 code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
```
```   138   (Haskell infix 4 "<=")
```
```   139   (Scala infixl 4 "<=")
```
```   140
```
```   141 code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
```
```   142   (Haskell infix 4 "<")
```
```   143   (Scala infixl 4 "<")
```
```   144
```
```   145 end
```