[Haskell-cafe] Making monadic code more concise

Bas van Dijk v.dijk.bas at gmail.com
Mon Nov 15 16:20:54 EST 2010


On Mon, Nov 15, 2010 at 6:43 PM, Ling Yang <lyang at cs.stanford.edu> wrote:
> ...
> One alternate way of doing this, however, is instancing the
> typeclasses of the ordinary values with their monadic versions:
>
> instance (Num a) => Num (Prob a) where
>        (+) = liftM2 (+)
>        (*) = liftM2 (*)
>        abs = liftM abs
>        signum = liftM signum
>        fromInteger = return . fromInteger
>
> instance (Fractional a) => Fractional (Prob a) where
>        fromRational = return . fromRational
>        (/) = liftM2 (/)

You may also like to look at Conal Elliott's applicative-numbers package:

http://hackage.haskell.org/package/applicative-numbers

Bas


More information about the Haskell-Cafe mailing list