
Hello, I want to declare a type thus: type Coordinate = (Int, Int) But the two integers must be confined to the inclusive range 0-11. Can i express that in the type system? -- Colin Adams Preston Lancashire

Colin Paul Adams
I want to declare a type thus:
type Coordinate = (Int, Int)
But the two integers must be confined to the inclusive range 0-11. Can i express that in the type system?
Well, the type system allows you to create your own ranged integer type. Here is a rather inflexible, but working method to do it: newtype Int12 = Int12 Int deriving (Eq, Read, Show) instance Num Int12 where Int12 x + Int12 y | x+y <= 11 = Int12 (x+y) | otherwise = error "Int12 addition out of range" ... Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/

"Ertugrul" == Ertugrul Soeylemez
writes:
Ertugrul> Colin Paul Adams

Colin Paul Adams
Ertugrul> Well, the type system allows you to create your own Ertugrul> ranged integer type. Here is a rather inflexible, but Ertugrul> working method to do it:
Ertugrul> newtype Int12 = Int12 Int deriving (Eq, Read, Show)
Ertugrul> instance Num Int12 where Int12 x + Int12 y | x+y <= 11 Ertugrul> = Int12 (x+y) | otherwise = error "Int12 addition out of Ertugrul> range" ...
That would seem to allow x = -3 and y = 13, for instance.
Not if you disallow an x = -3 and y = 13 to happen in the first place.
Haskell's module and type system allows you to do that.
Greets,
Ertugrul.
--
Key-ID: E5DD8D11 "Ertugrul Soeylemez

"Ertugrul" == Ertugrul Soeylemez
writes:
>> That would seem to allow x = -3 and y = 13, for instance. Ertugrul> Not if you disallow an x = -3 and y = 13 to happen in Ertugrul> the first place. Haskell's module and type system Ertugrul> allows you to do that. Can you explain how to do that please? -- Colin Adams Preston Lancashire

Colin Paul Adams
"Ertugrul" == Ertugrul Soeylemez
writes: >> That would seem to allow x = -3 and y = 13, for instance.
Ertugrul> Not if you disallow an x = -3 and y = 13 to happen in Ertugrul> the first place. Haskell's module and type system Ertugrul> allows you to do that.
Can you explain how to do that please?
Sure: module Even (Even) where newtype Even a = Even a deriving (Eq, Show) instance Integral a => Num (Even a) where Even a + Even b = Even (a+b) -- ... fromInteger x = if even x then Even (fromInteger x) else undefined The only way to introduce incorrect Even values would be to access the constructor directly, but in that example, it's not exported. The interface to constructing Even values is the fromInteger function. Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/

"Ertugrul" == Ertugrul Soeylemez
writes:
Ertugrul> Colin Paul Adams

type Coordinate = (RInt,RInt) data RInt = Zero | One | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Eleven You might also want to add an instance of Num so that you can define them simply by typing the relevant number, but then you'll lose the type system checking the bounds. Bob On 28 Dec 2008, at 10:07, Colin Paul Adams wrote:
Hello,
I want to declare a type thus:
type Coordinate = (Int, Int)
But the two integers must be confined to the inclusive range 0-11. Can i express that in the type system? -- Colin Adams Preston Lancashire _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Colin Paul Adams
-
Ertugrul Soeylemez
-
Thomas Davie