https://kotlinlang.org logo
#arrow-contributors
Title
# arrow-contributors
i

Imran/Malic

12/20/2019, 6:13 PM
May someone help me out here with Contravariant 🙂 . The Prelude looks like this:
Copy code
class ForOp private constructor() {
  companion object
}
typealias OpOf<A, X> = arrow.Kind2<ForOp, A, X>
typealias OpPartialOf<A> = arrow.Kind<ForEither, A>

@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
inline fun <A, X> OpOf<A, X>.fix(): Op<A, X> =
  this as Op<A, X>


@higherkind
data class Op<A, X>(val f: (X) -> A) {
  fun <B> compose(other: Op<B, A>): Op<B, X> =
    Op(other.f compose f)

  fun <B> compose(g: (A) -> B): Op<B, X> =
    compose(Op(g))
}

inline fun <A, X> Op<A, X>.k(): Kind2<ForOp, A, X> = TODO() // does that make sense?
now I want to define:
Copy code
interface OpContravariant<C> : Contravariant<Conested<ForOp, C>>
How does that look like. I tried it, but I could not find something tangible. cc @Ariostonj
j

Jannis

12/20/2019, 6:18 PM
Since
Op<A, X>
looks like a wrapper like
Function1<A, X>
it should have a similar contravariant instance. The instance is here: https://github.com/arrow-kt/arrow/blob/52bc681a41d44a3f14a726a125dff1c5c7e29a46/modules/core/arrow-core/src/main/kotlin/arrow/core/extensions/function1.kt#L56 And contramap is just compose
Actually nvm the parameters are reversed so the contravariant instance is slightly different I think (could probably be andThen instead)
Copy code
interface OpContravariant<I> : Contravariant<OpPartialOf<I>> {
  override fun <A, B> Kind<OpPartialOf<I>, A>.contramap(f: (B) -> A): Kind<OpPartialOf<I>, B> =
    Op(fix().f compose f)
}
Isn't this equal to the haskell version? Not sure how exactly that looked but I think this was it
i

Imran/Malic

12/20/2019, 6:41 PM
Thanks @Jannis is there a way to upcast to Hkt. I forgot how to do that
j

Jannis

12/20/2019, 6:41 PM
Btw the reason coming up with
Contravariant<Conested<ForOp>, X>
is hard, is that
Op<A, X>
is a wrapper for
(X) -> A
and functions are contravariant in their input and covariant in their output.
Contravariant<Conested<ForOp>, X>
is trying to implement contravariance in the output
i

Imran/Malic

12/20/2019, 6:42 PM
yes
I was so confused by that signature. Yours make total sense
j

Jannis

12/20/2019, 6:44 PM
is there a way to upcast to Hkt. I forgot how to do that
That is never necessary in arrow because we inherit from
Kind<F, A>
with our datatypes so inheritance takes care of that.
fix()
exists because we also need to cast the other direction
So
data class Op<A, X>(val f: (X) -> A): OpOf<A, X>
i

Imran/Malic

12/20/2019, 6:46 PM
😅 Oh man, I need to fresh up my Arrow in my holidays. Thanks bud.
arrow 1
7 Views