Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Monad for Pair (Mike Houghton)
2. Re: Monad for Pair (Mike Houghton)
3. Re: Monad for Pair (Marcin Mrotek)
----------------------------------------------------------------------
Message: 1
Date: Thu, 19 Nov 2015 18:30:54 +0000
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Monad for Pair
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
"But how do you take an arbitrary type and turn it into a monoid??
I didn?t - I must have been dreaming about Haskell at that point :) (My wife
once dreamt she was a C compiler - go figure? C !!??)
No, just Monad, Applicative and Functor - my mistake.
Thanks
Mike
> On 19 Nov 2015, at 06:34, Kim-Ee Yeoh <[email protected]> wrote:
>
>
> On Thu, Nov 19, 2015 at 1:33 AM, Mike Houghton <[email protected]
> <mailto:[email protected]>> wrote:
>
> The source is just me exploring.
>
> Nice.
>
> I first looked at
>
> data C a = C a deriving (Show)
>
> and made Monad, Applicative, Monoid and Functors for it.
>
> Even though the null-effect instances for the identity functor are trivial,
> there's value in writing them out, especially for the motivated.
>
> But how do you take an arbitrary type and turn it into a monoid?
>
> -- Kim-Ee
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151119/3689f09f/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 19 Nov 2015 18:31:23 +0000
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Monad for Pair
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Thanks for this - I?ll work through it.
Mike
> On 18 Nov 2015, at 08:28, Marcin Mrotek <[email protected]> wrote:
>
>> You might want to try writing out a test instance in full and re-checking
>> the second law.
>
> Ok, while the part upto Applicative is correct and unambiguous:
>
> data Pair a = Pair a a
>
> instance Functor Pair where
> fmap f (Pair a b) = Pair (f a) (f b)
>
> instance Applicative Pair where
> pure a = Pair a a
> Pair fa fb <*> Pair a b = Pair (fa a) (fb b)
>
> there are at least two implementations of Monad (assuming return=pure,
> also GHC 7.10 allows omitting return and implements it exactly like
> that):
>
> -- implementation (a)
> instance Monad Pair where
> Pair a _ >>= k = k a
>
> -- implementation (b)
> instance Monad Pair where
> Pair _ b >>= k = k b
>
> ... neither of which can satisfy the laws. There are more:
>
> -- implementation (c)
> instance Monad Pair where
> Pair a b >>= k = Pair a' b'
> where
> Pair a' _ = k a
> Pair _ b' = k b
>
> -- implementation (d)
> instance Monad Pair where
> Pair a b >>= k = Pair a' b'
> where
> Pair _ b' = k a
> Pair a' _ = k b
>
> and, well:
>
> instance Monad Pair where
> Pair a b >>= _ = Pair a b
>
> Did I miss anything? Now, trying to get the second law to work:
>
> a) m >>= return = m
> Pair a b >>= (\a -> Pair a a) = Pair a b
> Pair a a = Pair a b
> contradiction.
>
> b) m >>= return = m
> Pair a b >>= (\a -> Pair a a) = Pair a b
> Pair b b = Pair a b
> contradiction.
>
> c) m >>= return = m
> Pair a b >>= (\a -> Pair a a) = Pair a b
> Pair a b = Pair a b
> no contradiction this time, I'll write the other laws after I'm done
> with the second for the other instances.
>
> d) m >>= return = m
> Pair a b >>= (\a -> Pair a a) = Pair a b
> Pair b a = Pair a b
> contradiction.
>
> e) m >>= return = m
> trivially correct.
>
> Testing the first law for (c) and (e) that passed the second law:
>
> c) return a >>= k = k a
> Pair a a >>= k a = k a
> ---
> Pair a' b' = k a
> where
> Pair a' _ = k a
> Pair _ b' = k a
> ---
> no contradiction again.
>
> e) return a >>= k = k a
> return a = k a
> contradiction.
>
> Okay, then testing the third law for (c):
>
> m >>= (\x -> k x >>= h) = (m >>= k) >>= h
>
> Pair a b >>= (\x -> k x >>= h) = (Pair a b >>= k) >>= h (*)
>
> Let's again unpack the application of >>= in some pseud-haskell:
>
> Pair a1 _ = (\x -> k x >>= h) a = k a >>= h (**)
> Pair _ b1 = (\x -> k x >>= h) b = k b >> =h
>
> Pair a2 _ = k a (***)
> Pair _ b2 = k b
>
> plugging it into (*):
>
> Pair a1 b1 = Pair a2 b2 >>= h
>
> Unpacking >>= again:
>
> Pair a3 _ = h a2 (****)
> Pair _ b3 = h b2
>
> Pair a1 b1 = Pair a3 b3
>
> Now, testing if a1 = a3, lets bring in (**), (***), and (****):
>
> Pair a1 _ = k a >>= h
> Pair a2 _ = k a
> Pair a3 _ = h a2
>
> Form the first and the second equations (also using the one for b2
> earlier, but it's going to be dropped anyway sooner or later) we get:
>
> Pair a1 _ = Pair a2 b2 >>= h
>
> Unpacking >>= :
>
> Pair a4 _ = h a2
> Pair _ b4 = h b2
>
> But from the third equation we know that (Pair a3 _ = h a2) so:
>
> Pair a1 _ = Pair a3 _
>
> This does seem to work, I have no idea why. I'm pretty sure there I've
> made a mistake somewhere. Perhaps I shouldn't do equational reasoning
> after just getting up, or just use Agda :-/
>
> Best regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Thu, 19 Nov 2015 19:56:39 +0100
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Monad for Pair
Message-ID:
<CAJcfPz=yvPEpbxQipJKv8u4NEOV13Pe-kiz3z=uzXanniq=o...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
> Thanks for this - I?ll work through it.
Yeah, apparently I accidentaly wrote a correct instance there. You can
also use what Gesh wrote, and try to derive an instance from something
like:
tabulate :: (Bool -> a) -> Pair a
tabulate f = Pair (f True) (f False)
index :: Pair a -> Bool -> a
index (Pair a _) True = a
index (Pair _ b) False = b
instance Applicative Pair where
pure = tabulate . pure
fp <*> fa = tabulate $ index fp <*> index fa
instance Monad Pair where
m >>= f = tabulate $ index m >>= index . f
The instances for a function are (in pseudo-Haskell, I don't think GHC
accepts (a ->) instead of ((->) a), but it looks cleaner that way)
instance Applicative (a ->) where
pure = const
f <*> g = \x -> f x (g x)
instance Monad (a ->) where
f >>= k = \ r -> k (f r) r
> My wife once dreamt she was a C compiler - go figure? C !!??
C isn't exactly known for type safety, so things can indeed get
Kafkaesque sometimes.
Best regards,
Marcin Mrotek
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 89, Issue 34
*****************************************