<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://learnyouahaskell.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>