Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created January 5, 2019 14:19
Show Gist options
  • Save DonaldKellett/5fa87679f9901d5d76d1f93e1bd6da3e to your computer and use it in GitHub Desktop.
Save DonaldKellett/5fa87679f9901d5d76d1f93e1bd6da3e to your computer and use it in GitHub Desktop.
PureScript by Example - 8.7 Monads and Applicatives - Exercise 6 Solution (Written Exercise)

Theorem

In PureScript, the expression:

lift2 f (pure a) (pure b)

is equivalent to:

pure (f a b)

for all monads m (satisfying the monad laws) and all argument types a, b in function f :: a -> b -> c.

Proof

lift2 f (pure a) (pure b)

is equivalent to (by definition of lift2):

f <$> (pure a) <*> (pure b)

which is equivalent to (by definition of <$> and <*>, taking into account m is a monad):

ap (map f (pure a)) (pure b)

which translates to the following in do notation:

do
  f' <- do
    x <- pure a
    pure (f x)
  y <- pure b
  pure (f' y)

which, by the associativity law, reduces to:

do
  x <- pure a
  f' <- pure (f x)
  y <- pure b
  pure (f' y)

which, by repeated application of the left identity law, reduces to:

do
  f' <- pure (f a)
  y <- pure b
  pure (f' y)
do
  y <- pure b
  pure (f a y) -- equivalent to pure ((f a) y)
do
  pure (f a b)

The final expression is then trivially equivalent to pure (f a b), completing the proof.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment