src/HOL/Library/Code_Natural.thy
 author wenzelm Thu Feb 16 22:53:24 2012 +0100 (2012-02-16) changeset 46507 1b24c24017dd parent 39781 2053638a2bf2 child 46547 d1dcb91a512e permissions -rw-r--r--
tuned proofs;
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 {*import Data.Array.ST;
14 newtype Natural = Natural Integer deriving (Eq, Show, Read);
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 };
26 instance Ord Natural where {
27   Natural n <= Natural m = n <= m;
28   Natural n < Natural m = n < m;
29 };
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 };
38 instance Real Natural where {
39   toRational (Natural n) = toRational n;
40 };
42 instance Enum Natural where {
43   toEnum k = fromInteger (toEnum k);
45 };
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 };*}
58 code_include Scala "Natural"
59 {*object Natural {
61   def apply(numeral: BigInt): Natural = new Natural(numeral max 0)
62   def apply(numeral: Int): Natural = Natural(BigInt(numeral))
63   def apply(numeral: String): Natural = Natural(BigInt(numeral))
65 }
67 class Natural private(private val value: BigInt) {
69   override def hashCode(): Int = this.value.hashCode()
71   override def equals(that: Any): Boolean = that match {
72     case that: Natural => this equals that
73     case _ => false
74   }
76   override def toString(): String = this.value.toString
78   def equals(that: Natural): Boolean = this.value == that.value
80   def as_BigInt: BigInt = this.value
81   def as_Int: Int = if (this.value >= scala.Int.MinValue && this.value <= scala.Int.MaxValue)
82       this.value.intValue
83     else error("Int value out of range: " + this.value.toString)
85   def +(that: Natural): Natural = new Natural(this.value + that.value)
86   def -(that: Natural): Natural = Natural(this.value - that.value)
87   def *(that: Natural): Natural = new Natural(this.value * that.value)
89   def /%(that: Natural): (Natural, Natural) = if (that.value == 0) (new Natural(0), this)
90     else {
91       val (k, l) = this.value /% that.value
92       (new Natural(k), new Natural(l))
93     }
95   def <=(that: Natural): Boolean = this.value <= that.value
97   def <(that: Natural): Boolean = this.value < that.value
99 }
100 *}
102 code_reserved Scala Natural
104 code_type code_numeral
106   (Scala "Natural")
108 setup {*
111 *}
113 code_instance code_numeral :: equal
116 code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
118   (Scala infixl 7 "+")
120 code_const "op - \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
122   (Scala infixl 7 "-")
124 code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
126   (Scala infixl 8 "*")
128 code_const div_mod_code_numeral