[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-cafe
Subject: Re: [Haskell-cafe] example of monad from http://learnyouahaskell.comnot working
From: Jos Kusiek <jos.kusiek () tu-dortmund ! de>
Date: 2019-02-27 10:55:13
Message-ID: 201902271055.x1RAt42f006676 () unimail ! uni-dortmund ! de
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
The Monad class has changed, since a few GHC versions. The old class dependency chain \
was Functor <- Monad, but it is now Functor <- Applicative <- Monad. You can just \
ignore it and always instanciate it with:
instance Applicative M where
pure = return
(<*>) = ap
Where M is a Monad. So in your case replace "M" with "Prob".
If you are interested in properly instanciating the Applicative class, then the \
operator (<*>) is the new thing. The function "pure" should always do exactly the \
same as "return". The function "ap" should also always do the same as (<*>). The type \
of both is just a bit stricter and limited on Monads and not just on Applictive \
functors. If you look at the type it is a bit like fmap but with the function to lift \
"boxed" in a Functor/Applicative/Monad:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
So what you need to do is unwrap the function the same way as you unwrap the \
parameter, apply the function to the parameter and then wrap it again:
instance Applicative Prob where
pure a = Prob [(a,1%1)]
Prob fs <*> Prob as = Prob [(f a,x*y) | (f,x) <- fs, (a,y) <- as]
If you do the Applicative class first you can skip defining return, since it is \
defaulted with "return = pure":
instance Monad Prob where
m >>= f = flatten (fmap f m)
or
instance Monad Prob where
Prob as >>= f = Prob [(b,x*y) | (a,x) <- as, let Prob bs = f a, (b,y) <- bs]
Also fail from the Monad class is no longer used. It has been moved to the class \
MonadFail from the Control.Monad.Fail module:
import Control.Monad.Fail
instance MonadFail Prob where
fail _ = Prob []
Von: Damien Mattei
Gesendet: Mittwoch, 27. Februar 2019 10:57
An: haskell-cafe
Betreff: [Haskell-cafe] example of monad from http://learnyouahaskell.comnot working
i'm trying this example (see code below) from :
http://learnyouahaskell.com/for-a-few-monads-more#making-monads
when trying to compile this:
import Data.Ratio
newtype Prob a = Prob { getProb :: [(a,Rational)] } deriving Show
instance Functor Prob where
fmap f (Prob xs) = Prob $ map (\(x,p) -> (f x,p)) xs
thisSituation :: Prob (Prob Char)
thisSituation = Prob
[( Prob [('a',1%2),('b',1%2)] , 1%4 )
,( Prob [('c',1%2),('d',1%2)] , 3%4)
]
flatten :: Prob (Prob a) -> Prob a
flatten (Prob xs) = Prob $ concat $ map multAll xs
where multAll (Prob innerxs,p) = map (\(x,r) -> (x,p*r)) innerxs
instance Monad Prob where
return x = Prob [(x,1%1)]
m >>= f = flatten (fmap f m)
fail _ = Prob []
l1 = Prob [('a',2%3),('b',1%3)]
multAllExt :: (Prob a, Rational) -> [(a, Rational)]
multAllExt (Prob innerxs,p) = map (\(x,r) -> (x,p*r)) innerxs
--Main> :type multAllExt
--multAllExt :: (Prob a, Rational) -> [(a, Rational)]
--Main> multAllExt (l1,1 % 4)
--[('a',1 % 6),('b',1 % 12)]
i get this error:
GHCi, version 8.4.3: http://www.haskell.org/ghc/ :? for help
Prelude> :load monade.hs
[1 of 1] Compiling Main ( monade.hs, interpreted )
monade.hs:21:10: error:
• No instance for (Applicative Prob)
arising from the superclasses of an instance declaration
• In the instance declaration for ‘Monad Prob'
|
21 | instance Monad Prob where
| ^^^^^^^^^^
Failed, no modules loaded.
it fails when i add the last part of the example:
instance Monad Prob where
return x = Prob [(x,1%1)]
m >>= f = flatten (fmap f m)
fail _ = Prob []
seems the Monad needs an instance of the Applicative to be instanciated...
what is wrong?
regards,
Damien
[Attachment #5 (unknown)]
<html xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 \
(filtered medium)"><style><!-- /* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang=DE link=blue vlink="#954F72"><div class=WordSection1><p \
class=MsoNormal>The Monad class has changed, since a few GHC versions. The old class \
dependency chain was Functor <- Monad, but it is now Functor <- Applicative \
<- Monad. You can just ignore it and always instanciate it with:</p><p \
class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>instance Applicative M \
where</p><p class=MsoNormal> pure = return</p><p class=MsoNormal> \
(<*>) = ap</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Where \
M is a Monad. So in your case replace "M" with "Prob".</p><p \
class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>If you are interested in \
properly instanciating the Applicative class, then the operator (<*>) is the \
new thing. The function "pure" should always do exactly the same as \
"return". The function "ap" should also always do the same as \
(<*>). The type of both is just a bit stricter and limited on Monads and not \
just on Applictive functors. If you look at the type it is a bit like fmap but with \
the function to lift "boxed" in a Functor/Applicative/Monad:</p><p \
class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>(<*>) :: Applicative f \
=> f (a -> b) -> f a -> f b</p><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal>So what you need to do is unwrap the function the same way as you \
unwrap the parameter, apply the function to the parameter and then wrap it \
again:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>instance \
Applicative Prob where</p><p class=MsoNormal> pure a = Prob [(a,1%1)]</p><p \
class=MsoNormal> Prob fs <*> Prob as = Prob [(f a,x*y) | (f,x) <- fs, \
(a,y) <- as]</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>If you \
do the Applicative class first you can skip defining return, since it is defaulted \
with "return = pure":</p><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal>instance Monad Prob where</p><p class=MsoNormal> m >>= f \
= flatten (fmap f m)</p><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal>or</p><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal>instance Monad Prob where</p><p class=MsoNormal> Prob as >>= \
f = Prob [(b,x*y) | (a,x) <- as, let Prob bs = f a, (b,y) <- bs]</p><p \
class=MsoNormal> </p><p class=MsoNormal>Also fail from the Monad class is no \
longer used. It has been moved to the class MonadFail from the Control.Monad.Fail \
module:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>import \
Control.Monad.Fail</p><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal>instance MonadFail Prob where</p><p class=MsoNormal> fail _ = \
Prob []</p><p class=MsoNormal><o:p> </o:p></p><div \
style='mso-element:para-border-div;border:none;border-top:solid #E1E1E1 \
1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal \
style='border:none;padding:0cm'><b>Von: </b><a \
href="mailto:damien.mattei@gmail.com">Damien Mattei</a><br><b>Gesendet: </b>Mittwoch, \
27. Februar 2019 10:57<br><b>An: </b><a \
href="mailto:haskell-cafe@haskell.org">haskell-cafe</a><br><b>Betreff: \
</b>[Haskell-cafe] example of monad from http://learnyouahaskell.comnot \
working</p></div><p class=MsoNormal><o:p> </o:p></p><div><div><div><div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>i'm trying this example (see code \
below) from :<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><a \
href="http://learnyouahaskell.com/for-a-few-monads-more#making-monads">http://learnyou \
ahaskell.com/for-a-few-monads-more#making-monads</a><o:p></o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>when trying to compile \
this:<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>import Data.Ratio<br><br>newtype Prob \
a = Prob { getProb :: [(a,Rational)] } deriving Show<br><br><br>instance Functor Prob \
where<br> fmap f (Prob xs) = Prob $ map (\(x,p) -> (f x,p)) \
xs<br><br> <br>thisSituation :: Prob (Prob Char)<br>thisSituation = \
Prob<br> [( Prob [('a',1%2),('b',1%2)] , 1%4 \
)<br> ,( Prob [('c',1%2),('d',1%2)] , 3%4)<br> \
]<br><br>flatten :: Prob (Prob a) -> Prob a<br>flatten (Prob xs) = Prob $ concat $ \
map multAll xs<br> where multAll (Prob innerxs,p) = map (\(x,r) \
-> (x,p*r)) innerxs <br><br><br>instance Monad Prob where<br> \
return x = Prob [(x,1%1)]<br> m >>= f = flatten (fmap f m)<br> fail \
_ = Prob []<br><br><br><br>l1 = Prob [('a',2%3),('b',1%3)]<br><br>multAllExt :: (Prob \
a, Rational) -> [(a, Rational)]<br>multAllExt (Prob innerxs,p) = map (\(x,r) -> \
(x,p*r)) innerxs<br><br>--Main> :type multAllExt<br>--multAllExt :: (Prob a, \
Rational) -> [(a, Rational)]<br><br><br>--Main> multAllExt (l1,1 % \
4)<br>--[('a',1 % 6),('b',1 % 12)]<o:p></o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>i get this \
error:<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>GHCi, version 8.4.3: <a \
href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</a> :? for \
help<br>Prelude> :load monade.hs<br>[1 of 1] Compiling \
Main ( \
monade.hs, interpreted )<br><br>monade.hs:21:10: error:<br> • No \
instance for (Applicative Prob)<br> arising \
from the superclasses of an instance declaration<br> • In the \
instance declaration for ‘Monad Prob'<br> |<br>21 | instance Monad Prob \
where<br> | \
^^^^^^^^^^<br>Failed, no modules loaded.<o:p></o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>it fails when i add the last part of \
the example:<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>instance Monad Prob where<br> \
return x = Prob [(x,1%1)]<br> m >>= f = flatten (fmap f m)<br> fail \
_ = Prob []<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>seems the Monad needs an instance of \
the Applicative to be instanciated...<o:p></o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span style='font-size:18.0pt'>what is wrong? \
<o:p></o:p></span></p></div><div><p class=MsoNormal><span \
style='font-size:18.0pt'><o:p> </o:p></span></p></div><div><p \
class=MsoNormal><span \
style='font-size:18.0pt'>regards,<o:p></o:p></span></p></div></div></div></div></div><p \
class=MsoNormal><span style='font-size:18.0pt'>Damien<o:p></o:p></span></p><p \
class=MsoNormal><o:p> </o:p></p></div></body></html>
[Attachment #6 (text/plain)]
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic