<div dir="ltr">How can I fix it so that `ItDoesnt <*> WhatThisIsCalled` works?<div><br></div><div>I have came up with a solution without WhatThisIsCalled</div><div><br></div><div>data WhoCares a = ItDoesnt | Matter a deriving (Eq, Show)<br><br>instance Functor WhoCares where<br> fmap _ ItDoesnt = ItDoesnt<br> fmap f (Matter a) = Matter (f a)<br><br>instance Applicative WhoCares where<br> pure = Matter<br> Matter f <*> Matter a = Matter (f a)<br> ItDoesnt <*> _ = ItDoesnt<br> _ <*> ItDoesnt = ItDoesnt<br><br>instance Monad WhoCares where<br> return x = Matter x<br> (Matter x) >>= k = k x<br> ItDoesnt >>= _ = ItDoesnt<br><br>half x = if even x<br> then Matter (x `div` 2)<br> else ItDoesnt<br><br>incVal :: (Ord a, Num a) => a -> WhoCares a<br>incVal x<br> | x + 1 <= 10 = return (x + 1)<br> | otherwise = ItDoesnt<br><br>decVal :: (Ord a, Num a) => a -> WhoCares a<br>decVal x<br> | x - 1 >= 0 = return (x - 1)<br> | otherwise = ItDoesnt<br><br>main = do<br> -- fmap id == id<br> let funcx = fmap id "Hi Julie"<br> let funcy = id "Hi Julie"<br> print(funcx)<br> print(funcy)<br> print(funcx == funcy)<br><br> -- fmap (f . g) == fmap f . fmap g<br> let funcx' = fmap ((+1) . (*2)) [1..5]<br> let funcy' = fmap (+1) . fmap (*2) $ [1..5]<br> print(funcx')<br> print(funcy')<br> print(funcx' == funcy')<br><br> -- pure id <*> v = v<br> print(pure id <*> (Matter 10))<br><br> -- pure (.) <*> u <*> v <*> w = u <*> (v <*> w)<br> let appx = pure (.) <*> (Matter (+1)) <*> (Matter (*2)) <*> (Matter 10)<br> let appy = (Matter (+1)) <*> ((Matter (*2)) <*> (Matter 10))<br> print(appx)<br> print(appy)<br> print(appx == appy)<br><br> -- pure f <*> pure x = pure (f x)<br> let appx' = pure (+1) <*> pure 1 :: WhoCares Int<br> let appy' = pure ((+1) 1) :: WhoCares Int<br> print(appx')<br> print(appy')<br> print(appx' == appy')<br><br> -- u <*> pure y = pure ($ y) <*> u<br> let appx'' = Matter (+2) <*> pure 2<br> let appy'' = pure ($ 2) <*> Matter (+ 2)<br> print(appx'')<br> print(appy'')<br> print(appx'' == appy'')<br><br> -- m >>= return = m<br> let monx = Matter 20 >>= return<br> let mony = Matter 20<br> print(monx)<br> print(mony)<br> print(monx == mony)<br><br> -- return x >>= f = f x<br> let monx' = return 20 >>= half<br> let mony' = half 20<br> print(monx')<br> print(mony')<br> print(monx' == mony')<br><br> -- (m >>= f) >>= g = m >>= (\x -> f x >>= g)<br> let monx'' = return 20 >>= half >>= half<br> let mony'' = half 20 >>= half<br> print(monx'')<br> print(mony'')<br> print(monx'' == mony'')<br><br> print (Matter 7 >>= incVal >>= incVal >>= incVal)<br> print (Matter 7 >>= incVal >>= incVal >>= incVal >>= incVal)<br> print (Matter 7 >>= incVal >>= incVal >>= incVal >>= incVal >>= decVal >>= decVal)<br> print (Matter 2 >>= decVal >>= decVal >>= decVal)<br> print (Matter 20 >>= half >>= half)<br><div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b style="font-weight:normal"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Thanks and Best Regards,</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre-wrap">Ahmad Ismail</span></p></b></div></div></div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 13, 2022 at 5:08 PM Francesco Ariis <<a href="mailto:fa-ml@ariis.it">fa-ml@ariis.it</a>> 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">Hello Ahmad,<br>
<br>
Il 13 novembre 2022 alle 16:33 Ahmad Ismail ha scritto:<br>
> Due to lack of examples, I am not understanding how to implement >>= and<br>
> >>.<br>
<br>
All you need to implement is (>>=)!<br>
<br>
> The code I came up with so far is:<br>
> <br>
> instance Monad (WhoCares a) where<br>
> (>>=) :: Matter a -> (a -> Matter b) -> Matter b<br>
> (>>) :: Matter a -> Matter b -> Matter b<br>
> return :: a -> Matter a<br>
> return = pure<br>
<br>
The signature for (>>=) is wrong, `Matter` is a *data* constructor, you<br>
need a *type* one instead, so:<br>
<br>
(>>=) :: WhoCares a -> (a -> WhoCares b) -> WhoCares b<br>
<br>
But let us go back to typeclasses. Your `Applicative` instance<br>
<br>
> instance Applicative WhoCares where<br>
> pure = Matter<br>
> Matter f <*> Matter a = Matter (f a)<br>
<br>
is broken:<br>
<br>
λ> ItDoesnt <*> WhatThisIsCalled<br>
*** Exception: /tmp/prova.hs:11:5-40: Non-exhaustive patterns in function <*><br>
<br>
So we need first to fix that. What behaviour would you expect, what are<br>
you trying to model with `WhoCares`?<br>
—F<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>