简体   繁体   中英

kind-projector returns strange results

I have these types: SomeTypeClass
A higher kinded type which has one type parameter of kind * => * => *

trait SomeTypeClass[P[_, _]] {
    def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                              (pab: P[A, B])
                              (implicit ev: Strong[P],
                              ev2: Choice[P],
                              ev3: Applicative[F]): P[S, T]
}

Target which accepts three type parameters:
type constructor F[_] and two polymorphic types A, B

case class Target[F[_], A, B](f: A => F[B])

I want to implement an instance of SomeTypeClass of Target.
I am using the kind-projector plugin in order to create a partially applied type.
My desired method signature should be:

implicit def instance: SomeTypeClass[Target[F, *, *]] = new SomeTypeClass[Target[F, *, *]] {
  override def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                                      (pab: Target[F, A, B])
                                      (implicit ev: Strong[Target[F, *, *]], 
                                       ev2: Choice[Target[F, *, *]], 
                                       ev3: Applicative[F]): Target[F, S, T] = ???
}

I've tried using this syntax using two star parameters:

 implicit def instance[F[_]]: SomeTypeClass[Target[F, *, *]] = new SomeTypeClass[Target[F, *, *]] {
    override def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                                     (pab: Target[F, A, B])
                                     (implicit ev: Strong[Target[F, *, *]],
                                      ev2: Choice[Target[F, *, *]],
                                      ev3: Applicative[F]): Target[F, S, T] = ???
}

But the F[_] declared at the instance level shadows the F[_] declared at the test method (I want them to be the same F), so I've moved to the λ syntax and got two different unwanted results.

The first one using λ[(F, A, B) => Target[F, A, B]] generated for the pab paramter, pab: Target[A, B, B] instead of pab: Target[F, A, B] and also for the return type Target[S, T, B] instead of Target[F, S, T]

The second one using the F at the end of the triple type lambda parameters (why???)
λ[(A, B, F) => Target[F, A, B]] generated the correct types for the pab parameter and the return type, but for each one of the implicit parameters the type Strong[λ[(A, B, F) => Target[F, A, B]]] instead of Strong[Target[F, *, *]]]

The full code:

  import cats.Applicative
  import cats.arrow.{Choice, Strong}

  final case class Target[F[_], A, B](f: A => F[B])

  trait SomeTypeClass[P[_, _]] {
    def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                              (pab: P[A, B])
                              (implicit ev: Strong[P],
                               ev2: Choice[P],
                               ev3: Applicative[F]): P[S, T]
  }

  object SomeTypeClass {
    implicit def instance1: SomeTypeClass[λ[(F, A, B) => Target[F, A, B]]] = new SomeTypeClass[λ[(F, A, B) => Target[F, A, B]]] {
      override def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                                         (pab: Target[A, B, B])
                                         (implicit ev: Strong[Target],
                                          ev2: Choice[Target],
                                          ev3: Applicative[F]): Target[S, T, B] = ???
    }

    implicit def instance2: SomeTypeClass[λ[(A, B, F) => Target[F, A, B]]] = new SomeTypeClass[λ[(A, B, F) => Target[F, A, B]]] {
      override def test[F[_], S, T, A, B](f: (A => F[B]) => S => F[T])
                                         (pab: Target[F, A, B])
                                         (implicit ev: Strong[λ[(A, B, F) => Target[F, A, B]]],
                                          ev2: Choice[λ[(A, B, F) => Target[F, A, B]]],
                                          ev3: Applicative[F]): Target[F, S, T] = ???
    }
  }

Can I achieve the desired syntax using this plugin?
Why does the plugin generate different types for different order of type lambda's 'parameters'?

If I understood

But the F[_] declared at the instance level shadows the F[_] declared at the test method (I want them to be the same F )

correctly, you want your instance for SomeTypeClass[Target[...]] to fix the F[_] parameter of test . But that's simply not possible with this test type signature. Once you have (for example)

val inst = implicitly[SomeTypeClass[Target[...]]

you can call

val res1 = inst.test[List, ...]
val res2 = inst.test[Option, ...]

Type lambdas don't offer a way around this problem. You need to either move F[_] parameter to SomeTypeClass or implement

implicit def instance[F[_]]: SomeTypeClass[Target[F, *, *]] = new SomeTypeClass[Target[F, *, *]] {
  override def test[G[_], S, T, A, B](f: (A => G[B]) => S => G[T])
                                     (pab: Target[F, A, B])
                                     (implicit ev: Strong[Target[F, *, *]],
                                      ev2: Choice[Target[F, *, *]],
                                      ev3: Applicative[G]): Target[G, S, T] = ???
}

which I expect is impossible as you can't pass pab.f to f .

EDIT: the type of wander

class (Choice p, Strong p) => Traversing p where
  traverse' :: Traversable f => p a b -> p (f a) (f b)
  traverse' = wander traverse

  wander :: (forall f. Applicative f => (a -> f b) -> s -> f t) -> p a b -> p s t
  wander f pab = dimap (\s -> Baz $ \afb -> f afb s) sold (traverse' pab)

is a rank-2 type which aren't supported in Scala directly; instead you need to introduce a helper (which can't just be a type alias as it is in Control.Lens.Type )

trait Traversal[S, T, A, B] {
  def apply[F[_]: Applicative](f: A => F[B]): S => F[T]
}

Then

trait Traversing[P[_, _]] extends Strong[P] with Choice[P] {
  def wander[S, T, A, B](t: Traversal[S, T, A, B], pab: P[A, B]): P[S, T]
}

implicit def instance[F[_]: Applicative]: Traversing[Target[F, *, *]] = new Traversing[Target[F, *, *]] {
  def wander[S, T, A, B](t: Traversal[S, T, A, B], pab: Target[F, A, B]): Target[F, S, T] = Target(t(pab.f))
  // define Strong and Choice methods too
}

should work. (Though I am not sure this is the cats way to deal with Strong and Choice requirements.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM