<div dir="ltr"><div>It's the one of the biggest changes of Haskell since LYHG was released.</div><div>As you guess, now any instance of Monad must be an instance of Applicative.</div><div><br></div><div>So you have to declare Prob as an instance of Applicative:</div><div><br></div><div>instance Applicative Prob where</div><div> pure = ...</div><div> f <*> x = ...<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">2019年2月27日(水) 18:56 Damien Mattei <<a href="mailto:damien.mattei@gmail.com">damien.mattei@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:large">i'm trying this example (see code below) from :</div><div class="gmail_default" style="font-size:large"><a href="http://learnyouahaskell.com/for-a-few-monads-more#making-monads" target="_blank">http://learnyouahaskell.com/for-a-few-monads-more#making-monads</a></div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">when trying to compile this:</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">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)]</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">i get this error:</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">GHCi, version 8.4.3: <a href="http://www.haskell.org/ghc/" target="_blank">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.<br></div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">it fails when i add the last part of the example:</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">instance Monad Prob where<br> return x = Prob [(x,1%1)]<br> m >>= f = flatten (fmap f m)<br> fail _ = Prob []</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">seems the Monad needs an instance of the Applicative to be instanciated...</div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">what is wrong? <br></div><div class="gmail_default" style="font-size:large"><br></div><div class="gmail_default" style="font-size:large">regards,</div><div class="gmail_default" style="font-size:large">Damien<br></div></div></div></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>山本悠滋<br>twitter: <a href="https://twitter.com/igrep" target="_blank">https://twitter.com/igrep</a><br>GitHub: <a href="https://github.com/igrep" target="_blank">https://github.com/igrep</a></div><div>GitLab: <a href="https://gitlab.com/igrep" target="_blank">https://gitlab.com/igrep</a><br>Facebook: <a href="http://www.facebook.com/igrep" target="_blank">http://www.facebook.com/igrep</a></div></div></div></div></div></div></div>