1

I have a function:

someFun :: Applicative f => f a -> b -> f c
someFun x y = …

The argument for y that I need to give someFun is an “f b” Lets say I have values

someX :: Applicative f => f a
someY :: Applicative f => f b

I tried to do

LiftA (someFun someX) someY

But that gives me f (f c)

I need to result in an f c

chi
  • 111,837
  • 3
  • 133
  • 218
error_null_pointer
  • 457
  • 1
  • 6
  • 21

1 Answers1

8

What you are asking for is the thing that Monads can do but Applicatives cannot. With a Monad instance, this is just join:

join :: Monad m => m (m a) -> m a

What you ask for is impossible, but at least now you have a perfect example of what you can do with a Monad that you can't do with an Applicative.

Rein Henrichs
  • 15,437
  • 1
  • 45
  • 55
  • That was the thought I was coming to from my reading. I was just making sure I was on the right track. – error_null_pointer Sep 15 '16 at 01:50
  • ...but it may still be possible with mere `Applicative`s if you can "peer inside" the implementation of `someFun`. – Daniel Wagner Sep 15 '16 at 04:54
  • Are you referring to Lift or is there something else? – error_null_pointer Sep 15 '16 at 12:37
  • @DanielWagner I don't believe it is. `LiftA` (a.k.a. `fmap`) needs to be replaced with a function `f b -> (b -> f c) -> f c`, but this function is exactly `(>>=)`. – Rein Henrichs Sep 15 '16 at 18:24
  • 2
    @ReinHenrichs That assumes that we must use `someFun` as-is from outside. If we can change the source code of `someFun` to produce a new function `someFun'`, we may find that we can change the internals in a way that results in a function of type `someFun' :: Applicative f => f a -> f b -> f c`. It's really impossible to say that this can't be done without seeing the source for `someFun`. – Daniel Wagner Sep 15 '16 at 18:55
  • @DanielWagner I get what you're saying. On the other hand, if we can change the requirements to suit our solution then we can come up with lots of solutions! – Rein Henrichs Sep 15 '16 at 19:50
  • 2
    @ReinHenrichs Absolutely! That's why this is a comment, and not an answer: I believe OP asked the wrong question, and I'm just trying to alert him to possible (in fact, I believe *probable*) ways his question may be more flexible than he thinks. – Daniel Wagner Sep 15 '16 at 20:50