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 *)
5 theory Code_Natural
6 imports Main
7 begin
9 section {* Alternative representation of @{typ code_numeral} for @{text Haskell} and @{text Scala} *}
12 {*newtype Natural = Natural Integer deriving (Eq, Show, Read);
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 };
24 instance Ord Natural where {
25   Natural n <= Natural m = n <= m;
26   Natural n < Natural m = n < m;
27 };
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 };
36 instance Real Natural where {
37   toRational (Natural n) = toRational n;
38 };
40 instance Enum Natural where {
41   toEnum k = fromInteger (toEnum k);
43 };
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 };*}
55 code_include Scala "Natural"
56 {*import scala.Math
58 object Nat {
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))
64 }
66 class Nat private(private val value: BigInt) {
68   override def hashCode(): Int = this.value.hashCode()
70   override def equals(that: Any): Boolean = that match {
71     case that: Nat => this equals that
72     case _ => false
73   }
75   override def toString(): String = this.value.toString
77   def equals(that: Nat): Boolean = this.value == that.value
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)
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)
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     }
94   def <=(that: Nat): Boolean = this.value <= that.value
96   def <(that: Nat): Boolean = this.value < that.value
98 }
99 *}
101 code_reserved Scala Natural
103 code_type code_numeral
105   (Scala "Natural.Nat")
107 setup {*
110 *}
112 code_instance code_numeral :: eq
115 code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
117   (Scala infixl 7 "+")
119 code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
121   (Scala infixl 7 "-")
123 code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
125   (Scala infixl 8 "*")
127 code_const div_mod_code_numeral