According to pointfree
:
\x -> (x, x)
is equivalent to:
join (,)
What is the derivation that shows this?
Look at the type signatures:
\x -> (x, x) :: a -> (a, a)
(,) :: a -> b -> (a, b)
join :: Monad m => m (m a) -> m a
It should be noted that ((->) r)
is an instance of the Monad
typeclass. Hence, on specializing:
join :: (r -> r -> a) -> (r -> a)
What join
does for functions is apply the given function twice to the same argument:
join f x = f x x
-- or
join f = \x -> f x x
From this, we can see trivially:
join (,) = \x -> (,) x x
-- or
join (,) = \x -> (x, x)
Qed.
I like Aadits intuitive answer. Here's how I'd figure it out by reading the source code.
- I go to Hoogle
- I search for
join
- I click on
join
- I click the "source" button to get to the source code for
join
- I see that
join x = x >>= id
- So I know that
join (,) = (,) >>= id
- I search for
>>=
on Hoogle and click the link - I see that it's part of the monad typeclass, and I know I'm dealing with
(,)
which is a function, so I click "source" on theMonad ((->) r)
instance - I see that
f >>= k = \r -> k (f r) r
- Since we have
f = (,)
andk = id
, we get\r -> id ((,) r) r
- Sooo... new function!
id
! I search for that on Hoogle, and click through to its source code - Turns out
id x = x
- So instead of
join (,)
we now have\r -> ((,) r) r
- Which is the same thing as
\r -> (,) r r
- Which is the same thing as
\r -> (r,r)
Never forget that the Haddocks link through to the source code of the library. That's immensely useful when trying to figure out how things work together.
来源:https://stackoverflow.com/questions/29713442/what-is-the-derivation-that-shows-haskells-x-x-x-equivalent-to-join