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 <colin@colina.demon.co.uk> wrote:
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 <es@ertes.de> writes:
Ertugrul> Colin Paul Adams <colin@colina.demon.co.uk> wrote: >> 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? 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. -- Colin Adams Preston Lancashire
Colin Paul Adams <colin@colina.demon.co.uk> wrote:
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 <es@ertes.de>" FPrint: 0F12 0912 DFC8 2FC5 E2B8 A23E 6BAC 998E CE40 2012 Keysrv: hkp://subkeys.pgp.net/ -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/
"Ertugrul" == Ertugrul Soeylemez <es@ertes.de> 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 <colin@colina.demon.co.uk> wrote:
"Ertugrul" == Ertugrul Soeylemez <es@ertes.de> 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 <es@ertes.de> writes:
Ertugrul> Colin Paul Adams <colin@colina.demon.co.uk> wrote: >> >>>>> "Ertugrul" == Ertugrul Soeylemez <es@ertes.de> 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? Ertugrul> Sure: Ertugrul> module Even (Even) where Ertugrul> newtype Even a = Even a deriving (Eq, Show) Ertugrul> instance Integral a => Num (Even a) where Even a + Ertugrul> Even b = Even (a+b) -- ... fromInteger x = if even x Ertugrul> then Even (fromInteger x) else undefined Ertugrul> The only way to introduce incorrect Even values would be Ertugrul> to access the constructor directly, but in that example, Ertugrul> it's not exported. The interface to constructing Even Ertugrul> values is the fromInteger function. Thanks for all your help. -- Colin Adams Preston Lancashire
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