[Haskell-beginners] Re: in which monad am I?
Maciej Piechotka
uzytkownik2 at gmail.com
Sun Jan 3 17:21:45 EST 2010
On Sun, 2010-01-03 at 22:38 +0100, Francesco Guerrieri wrote:
> Hello, this is my first post to the list.
>
> I am learning Haskell and I think I understand monads (well, that
> doesn't mean that I actually understand them :-) )
> Nevertheless, I would like to check something with you.
> My question is: how do you know in which monad you are computing at a
> given time?
> What I mean is that, when dealing with more than a monad at the same
> time (Maybe, List, IO come to my mind) if I understand correctly you
> have to lift values back and forth from a monad to the other. How do
> you "keep" track of the monad you are in a given time? I think that
> the type system will help to resolve to the relevant function (eg,
> thinking to liftM signature, liftM :: (Monad m) => (a1 -> r) -> m a1
> -> m r
>
> 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" ?
>
> I hope it's not a too silly question. If so, please point me to the
> relevant resouces (Are monad transformers relevant to my issue?)
>
> Thanks in advance,
> Francesco
'In which monad' question is the same as 'can type system be confused'.
Except for syntax sugar (do) monads are nothing special in Haskell. Not
more special then Read or Show classes.
For example (for type system):
> f :: [String] -> [String]
> f = map show . map read
>
> main = interact (unlines . f . lines)
test.lhs:6:21:
Ambiguous type variable `a' in the constraints:
`Read a' arising from a use of `read' at test.lhs:6:21-24
`Show a' arising from a use of `show' at test.lhs:6:10-13
Probable fix: add a type signature that fixes these type variable(s)
With monad it is harder (as monad is one-way) but it is possible to do
it:
> extract $ liftM (+1) (return 0)
results in:
Ambiguous type variable `f' in the constraints:
`Monad f' arising from a use of `liftM' at <interactive>:1:10-30
`Copointed f'
arising from a use of `extract' at <interactive>:1:0-6
Probable fix: add a type signature that fixes these type variable(s)
We only know that we operate on some monad which is copointed. But we
have no idea what is it. However in 99% of cases we don't have to.
(You man notice the signature of extract is - extract :: (Copointed f)
=> f a -> a so the problem is when we remove type [type does not occures
on RHS of last ->])
Regards
More information about the Beginners
mailing list