[Haskell-beginners] in which monad am I?
Felipe Lessa
felipe.lessa at gmail.com
Sun Jan 3 17:30:59 EST 2010
On Sun, Jan 03, 2010 at 10:38:28PM +0100, Francesco Guerrieri wrote:
> But doesn't ever happen that poor liftM be confused? and do poor
> haskell programmers never feel the need to explicitly state to which
> monad they "wish to lift" ?
If by "explicitly state to which monad" you mean specializing
liftM or fmap, then the answer is no. It is not uncommon to see
fmap (fmap function)
being used to map two layers of structures. If by stating you
mean giving a type signature, then yes, it is a good practice to
give type signatures to all top-level definitions.
The compiler never gets "confused". The only thing that may
happen is having more than one possible type that fulfills your
needs. The classical example is Num overloading:
$ ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :s -Wall
Prelude> :m Data.Int
Prelude Data.Int> let e :: Int; e = 31
Prelude Data.Int> 2^e
<interactive>:1:0:
Warning: Defaulting the following constraint(s) to type `Integer'
`Num t' arising from a use of `^' at <interactive>:1:0-2
In the expression: 2 ^ e
In the definition of `it': it = 2 ^ e
<interactive>:1:0:
Warning: Defaulting the following constraint(s) to type `Integer'
`Num t' arising from a use of `^' at <interactive>:1:0-2
In the expression: 2 ^ e
In the definition of `it': it = 2 ^ e
2147483648
Prelude Data.Int> 2^e :: Int32
-2147483648
Note that using just '2^e' in the GHCi prompt defaults to
Integer. If I say I want Int32 as a type, then the result is
completely different, in this case it overflows.
Another case when you arrive at those oddities is when dealing
with exceptions. The function you use to catch the exceptions
need to explicitly say which type of exceptions it wants to
catch. Failures to list the correct type will cause the
exception to fall through and propagate.
HTH,
--
Felipe.
More information about the Beginners
mailing list