1

I understand that in previous versions of Arrow-Kt was an instance of the Applicative typeclass. In the current version 1.0.x, most typeclasses are no longer present/visible (to make the library more accessible, I presume?).

There are some excellent explanations why an error accumulating Validation data type can't be a Monad, for instance Haskell 1 and Haskell 2 and also Scalaz, Cats or arrow-kt. Hoogle also states that

Furthermore, the Monad and Applicative operations should relate as follows:
pure = return
m1 <*> m2 = m1 >>= (x1 -> m2 >>= (x2 -> return (x1 x2)))

My problem is that I can't see an instance of the Applicative Functor instance in the current implementation of Validated in arrow-kt. More precisely, I don't see an ap function anywhere:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

my intuition tells me that the zip function behaves somewhat similar to a map/ap composition

Haskell/Purescript:

Person <$> validateName first <*> validateName last <*> validateAge age

Kotlin with arrow-kt:

validateName(first).zip(validateName(last), validateAge(age), ::Person)

is zip an ap in disguise? Anything else I might be missing in my reasoning?

1
  • 1
    You are correct, zip is map in disquise. It was renamed to be more inline with the kotlin standard library naming Apr 12, 2022 at 21:07

1 Answer 1

3

Like @LordRaydenMK has already mentioned in a comment zip is ap in disguise to be more in line with the Kotlin Standard Library.

Another strong reason for not having ap in Kotlin is because currying is not 1st class supported by Kotlin as it is in Haskell, and we cannot define multiple parameter lists like in Scala. So far that reason ap is actually quite cumbersome to use, and not user-friendly at all.

So for the sake of user-friendliness, being more idiomatic in Kotlin and decreasing the learning curve Arrow decided to remove ap and offer zip instead.

2
  • 1
    thanks for the confirmation! Just to make sure, I understand it correctly, I tried to implement ap by using the existing zip function: kotlin fun <E, A, B> ValidatedNel<E, (A) -> B>.ap(fa: ValidatedNel<E, A>): ValidatedNel<E, B> = zip(fa) { f, a -> f(a) } it works surprisingly well, even on curried functions kotlin fun add(x: Int, y: Int) = x + y ::add.curried().validNel().ap(42.validNel()).ap(5.validNel()) But I see your point why this is not idiomatic Kotlin ;-)
    – Chris
    Apr 21, 2022 at 3:32
  • Chris from the future here. There is another great explanation why zip and ap are equivalent and sufficient to define a datatype as an Applicative (besides the need of a proper type constructor). Instead of implementing ap, you could also implement liftA2 which has the following type signature: ``` liftA2 :: (a -> b -> c) -> f a -> f b -> f c ```. It converts a binary function to a function where all its arguments and return type are lifted inside an applicative. It is also basically just the zip function with flipped arguments.
    – Chris
    May 10 at 0:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.