[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] example of monad fromhttp://learnyouahaskell.com not working
From:       Damien Mattei <damien.mattei () gmail ! com>
Date:       2019-02-27 23:10:49
Message-ID: CADEOadd4LJtdgi8mgo5UKfZWtKwcm8+Oi5=n4EiunArOz8WWww () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


i mean if y had:
a <- coin
    b <- coin
    c <- loadedCoin

but only compute return (all (==Tails) [a]
what i really does ot understand is how the probability is normalised , yes
monad again keeps an air of mystery for me....

On Thu, Feb 28, 2019 at 12:05 AM Damien Mattei <damien.mattei@gmail.com>
wrote:

> thanks for your code, pretty example, i understand well flatten and all
> the function but at last i cannot figure out how the result come....
> in the example:
> flipThree :: Prob Bool
> flipThree = do
>     a <- coin
>     b <- coin
>     c <- loadedCoin
>     return (all (==Tails) [a,b,c])
>
> the strange thing is if i add more
> variables as :
> d <- coin
> it oes not change the probability but the output yes!
> example:
> Prob {getProb = [(True,1 % 80),(True,1 % 80),(True,9 % 80),(True,9 %
> 80),(True,1 % 80),(True,1 % 80),(True,9 % 80),(True,9 % 80),(True,1 %
> 80),(True,1 % 80),(True,9 % 80),(True,9 % 80),(True,1 % 80),(True,1 %
> 80),(True,9 % 80),(True,9 % 80)]}
>
> On Wed, Feb 27, 2019 at 12:01 PM Jos Kusiek <jos.kusiek@tu-dortmund.de>
> wrote:
>
>> That is most likely, because ap is not in Prelude. You need to import
>> Control.Monad.
>>
>>
>>
>> *Von: *Damien Mattei <damien.mattei@gmail.com>
>> *Gesendet: *Mittwoch, 27. Februar 2019 11:54
>> *An: *Yuji Yamamoto <whosekiteneverfly@gmail.com>
>> *Cc: *haskell-cafe <haskell-cafe@haskell.org>
>> *Betreff: *Re: [Haskell-cafe] example of monad fromhttp://
>> learnyouahaskell.com not working
>>
>>
>>
>> can you give me a complete solution please?
>>
>> i suppose i can set pure = return
>>
>> but have some diffculties with <*> , ap does not works
>>
>>
>>
>> On Wed, Feb 27, 2019 at 11:10 AM Yuji Yamamoto <
>> whosekiteneverfly@gmail.com> wrote:
>>
>> It's the one of the biggest changes of Haskell since LYHG was released.
>>
>> As you guess, now any instance of Monad must be an instance of
>> Applicative.
>>
>>
>>
>> So you have to declare Prob as an instance of Applicative:
>>
>>
>>
>> instance Applicative Prob where
>>
>>    pure = ...
>>
>>    f <*> x = ...
>>
>>
>>
>>
>>
>> 2019年2月27日(水) 18:56 Damien Mattei <damien.mattei@gmail.com>:
>>
>> 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
>>
>> _______________________________________________
>> 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.
>>
>>
>>
>> --
>>
>> å±±æœ¬æ‚ æ»‹
>> twitter: https://twitter.com/igrep
>> GitHub: https://github.com/igrep
>>
>> GitLab: https://gitlab.com/igrep
>> Facebook: http://www.facebook.com/igrep
>>
>>
>>
>

[Attachment #5 (text/html)]

<div dir="ltr"><div class="gmail_default" style="font-size:large">i mean if y \
had:</div><div class="gmail_default" style="font-size:large">a &lt;- coin   <br>      \
b &lt;- coin   <br>       c &lt;- loadedCoin <br></div><div class="gmail_default" \
style="font-size:large"><br></div><div class="gmail_default" \
style="font-size:large">but only compute return (all (==Tails) [a]</div><div \
class="gmail_default" style="font-size:large">what i really does ot understand is how \
the probability is normalised , yes monad again keeps an air of mystery for \
me....<br></div></div><br><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">On Thu, Feb 28, 2019 at 12:05 AM Damien Mattei &lt;<a \
href="mailto:damien.mattei@gmail.com">damien.mattei@gmail.com</a>&gt; \
wrote:<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 class="gmail_default" style="font-size:large">thanks \
for your code, pretty example, i understand well flatten and all the function but at \
last i cannot figure out how the result come....</div><div class="gmail_default" \
style="font-size:large">in the example:</div><div class="gmail_default" \
style="font-size:large">flipThree :: Prob Bool   <br>flipThree = do   <br>       a \
&lt;- coin   <br>       b &lt;- coin   <br>       c &lt;- loadedCoin   <br>       \
return (all (==Tails) [a,b,c])</div><div class="gmail_default" \
style="font-size:large"><br></div><div class="gmail_default" \
style="font-size:large">the strange thing is if i add more <br></div><div \
class="gmail_default" style="font-size:large">variables as :</div><div \
class="gmail_default" style="font-size:large">d &lt;- coin   <br></div><div \
class="gmail_default" style="font-size:large">it oes not change the probability but \
the output yes!</div><div class="gmail_default" \
style="font-size:large">example:<br></div><div class="gmail_default" \
style="font-size:large">Prob {getProb = [(True,1 % 80),(True,1 % 80),(True,9 % \
80),(True,9 % 80),(True,1 % 80),(True,1 % 80),(True,9 % 80),(True,9 % 80),(True,1 % \
80),(True,1 % 80),(True,9 % 80),(True,9 % 80),(True,1 % 80),(True,1 % 80),(True,9 % \
80),(True,9 % 80)]}<br></div></div></div></div><div dir="ltr"><div \
class="gmail_default" style="font-size:large"> </div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 27, 2019 at 12:01 \
PM Jos Kusiek &lt;<a href="mailto:jos.kusiek@tu-dortmund.de" \
target="_blank">jos.kusiek@tu-dortmund.de</a>&gt; wrote:<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 lang="DE"><div \
class="gmail-m_-561931894176650890gmail-m_1550864239361012014gmail-m_6777960215663850944WordSection1"><p \
class="MsoNormal">That is most likely, because ap is not in Prelude. You need to \
import Control.Monad.</p><p class="MsoNormal"><u></u>  <u></u></p><div \
style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid \
none none;border-width:1pt medium medium;padding:3pt 0cm 0cm"><p class="MsoNormal" \
style="border:medium none;padding:0cm"><b>Von: </b><a \
href="mailto:damien.mattei@gmail.com" target="_blank">Damien \
Mattei</a><br><b>Gesendet: </b>Mittwoch, 27. Februar 2019 11:54<br><b>An: </b><a \
href="mailto:whosekiteneverfly@gmail.com" target="_blank">Yuji Yamamoto</a><br><b>Cc: \
</b><a href="mailto:haskell-cafe@haskell.org" \
target="_blank">haskell-cafe</a><br><b>Betreff: </b>Re: [Haskell-cafe] example of \
monad fromhttp://<a href="http://learnyouahaskell.com" \
target="_blank">learnyouahaskell.com</a> not working</p></div><p \
class="MsoNormal"><u></u>  <u></u></p><div><div><p class="MsoNormal"><span \
style="font-size:18pt">can you give me a complete solution \
please?<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt">i suppose i can set pure = \
return<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt">but have some diffculties with &lt;*&gt; , ap does not \
works<u></u><u></u></span></p></div></div><p class="MsoNormal"><u></u>  \
<u></u></p><div><div><p class="MsoNormal">On Wed, Feb 27, 2019 at 11:10 AM Yuji \
Yamamoto &lt;<a href="mailto:whosekiteneverfly@gmail.com" \
target="_blank">whosekiteneverfly@gmail.com</a>&gt; wrote:</p></div><blockquote \
style="border-color:currentcolor currentcolor currentcolor \
rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium \
1pt;padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm"><div><div><p \
class="MsoNormal">It&#39;s the one of the biggest changes of Haskell since LYHG was \
released.</p></div><div><p class="MsoNormal">As you guess, now any instance of Monad \
must be an instance of Applicative.</p></div><div><p class="MsoNormal"><u></u>  \
<u></u></p></div><div><p class="MsoNormal">So you have to declare Prob as an instance \
of Applicative:</p></div><div><p class="MsoNormal"><u></u>  <u></u></p></div><div><p \
class="MsoNormal">instance Applicative Prob where</p></div><div><p class="MsoNormal"> \
pure = ...</p></div><div><p class="MsoNormal">     f &lt;*&gt; x = \
...</p></div><div><p class="MsoNormal"><u></u>  <u></u></p></div></div><p \
class="MsoNormal"><u></u>  <u></u></p><div><div><p class="MsoNormal">2019<span \
style="font-family:&quot;Yu Gothic&quot;,sans-serif">å¹´</span>2<span \
style="font-family:&quot;Yu Gothic&quot;,sans-serif">月</span>27<span \
style="font-family:&quot;Yu Gothic&quot;,sans-serif">æ—¥</span>(<span \
style="font-family:&quot;Yu Gothic&quot;,sans-serif">æ°´</span>) 18:56 Damien Mattei \
&lt;<a href="mailto:damien.mattei@gmail.com" \
target="_blank">damien.mattei@gmail.com</a>&gt;:</p></div><blockquote \
style="border-color:currentcolor currentcolor currentcolor \
rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium \
1pt;padding:0cm 0cm 0cm \
6pt;margin-left:4.8pt;margin-right:0cm"><div><div><div><div><div><p \
class="MsoNormal"><span style="font-size:18pt">i&#39;m trying this example (see code \
below) from :<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><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><u></u><u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt"><u></u>  \
<u></u></span></p></div><div><p class="MsoNormal"><span style="font-size:18pt">when \
trying to compile this:<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt">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) -&gt; (f x,p)) xs<br><br>       \
<br>thisSituation :: Prob (Prob Char)<br>thisSituation = Prob<br>       [( Prob \
[(&#39;a&#39;,1%2),(&#39;b&#39;,1%2)] , 1%4 )<br>       ,( Prob \
[(&#39;c&#39;,1%2),(&#39;d&#39;,1%2)] , 3%4)<br>       ]<br><br>flatten :: Prob (Prob \
a) -&gt; Prob a<br>flatten (Prob xs) = Prob $ concat $ map multAll xs<br>       where \
multAll (Prob innerxs,p) = map (\(x,r) -&gt; (x,p*r)) innerxs     \
<br><br><br>instance Monad Prob where<br>   return x = Prob [(x,1%1)]<br>   m \
&gt;&gt;= f = flatten (fmap f m)<br>   fail _ = Prob []<br><br><br><br>l1 = Prob \
[(&#39;a&#39;,2%3),(&#39;b&#39;,1%3)]<br><br>multAllExt :: (Prob a, Rational) -&gt; \
[(a, Rational)]<br>multAllExt (Prob innerxs,p) = map (\(x,r) -&gt; (x,p*r)) \
innerxs<br><br>--Main&gt; :type multAllExt<br>--multAllExt :: (Prob a, Rational) \
-&gt; [(a, Rational)]<br><br><br>--Main&gt; multAllExt (l1,1 % \
4)<br>--[(&#39;a&#39;,1 % 6),(&#39;b&#39;,1 % \
12)]<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt"><u></u>  \
<u></u></span></p></div><div><p class="MsoNormal"><span style="font-size:18pt">i get \
this error:<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt">GHCi, version 8.4.3: <a \
href="http://www.haskell.org/ghc/" target="_blank">http://www.haskell.org/ghc/</a>   \
:? for help<br>Prelude&gt; :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.<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt">it fails when i add the last part of \
the example:<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt">instance Monad Prob where<br>   return \
x = Prob [(x,1%1)]<br>   m &gt;&gt;= f = flatten (fmap f m)<br>   fail _ = Prob \
[]<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt"><u></u>  \
<u></u></span></p></div><div><p class="MsoNormal"><span style="font-size:18pt">seems \
the Monad needs an instance of the Applicative to be \
instanciated...<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span style="font-size:18pt">what is wrong? \
<u></u><u></u></span></p></div><div><p class="MsoNormal"><span \
style="font-size:18pt"><u></u>  <u></u></span></p></div><div><p \
class="MsoNormal"><span \
style="font-size:18pt">regards,<u></u><u></u></span></p></div><div><p \
class="MsoNormal"><span \
style="font-size:18pt">Damien<u></u><u></u></span></p></div></div></div></div></div><p \
class="MsoNormal">_______________________________________________<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" \
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.</p></blockquote></div><p \
class="MsoNormal"><br clear="all"><br>-- </p><div><div><div><div><div><div><div><p \
class="MsoNormal"><span style="font-family:&quot;Yu Gothic&quot;,sans-serif">å±±æœ¬æ‚ \
滋</span><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></p></div></div></div></div></div></div></div></blockquote></div><p \
class="MsoNormal" style="margin-left:4.8pt">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></p><p class="MsoNormal"><u></u>  \
<u></u></p></div></div></blockquote></div> </blockquote></div>


[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