[Haskell-beginners] Re: Restricting integers in a type?

Ertugrul Soeylemez es at ertes.de
Sun Dec 28 17:47:16 EST 2008


Colin Paul Adams <colin at 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 at 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/




More information about the Beginners mailing list