Skip to content

Deriving typeclass instances for CopK #218

Open
@lukaszwawrzyk

Description

@lukaszwawrzyk

@andyscott
Hi!
I am working on migration of quasar from scalaz.Coproduct to iotaz.CopK. We need a way to create instances of arbitrary typeclasses for arbitrary CopKs.

It seems like there is no way to do this with iota currently. Did you think about that? Do you know how could it be solved? Any suggestions are welcome. I am happy to submit a PR. We will be working on that anyway, but any guidelines would be appriciated.

The only solution that comes to my mind that would surely work but is far far far from ideal is to create a separate macro for each coproduct.

To give some example of what needs to be migrated:

  implicit def coproductEqual[F[_], G[_]](implicit F: Delay[Equal, F], G: Delay[Equal, G]): Delay[Equal, Coproduct[F, G, ?]] =
    Delay.fromNT(λ[Equal ~> Equal[Coproduct[F, G, ?]](eq =>
      Equal equal ((cp1, cp2) =>
        (cp1.run, cp2.run) match {
          case (-\/(f1), -\/(f2)) => F(eq).equal(f1, f2)
          case (\/-(g1), \/-(g2)) => G(eq).equal(g1, g2)
          case (_,       _)       => false
        })))
  implicit def coproduct[M[_], FMT, F[_], G[_], J](
    implicit F: Lazy[Planner[M, FMT, F, J]], G: Lazy[Planner[M, FMT, G, J]]
  ): Planner[M, FMT, Coproduct[F, G, ?], J] =
    new Planner[M, FMT, Coproduct[F, G, ?], J] {
      def plan[Q](
        implicit Q: Birecursive.Aux[Q, Query[J, ?]]
      ): AlgebraM[M, Coproduct[F, G, ?], Search[Q] \/ XQuery] =
        _.run.fold(F.value.plan, G.value.plan)
    }
  implicit def coproduct[T[_[_]], F[_], G[_], H[_]]
    (implicit F: DiscoverPath.Aux[T, F, H], G: DiscoverPath.Aux[T, G, H])
      : DiscoverPath.Aux[T, Coproduct[F, G, ?], H] =
    new DiscoverPath[Coproduct[F, G, ?]] {
      type IT[F[_]] = T[F]
      type OUT[A] = H[A]

      def discoverPath[M[_]: Monad: MonadFsErr](g: ListContents[M]) =
        _.run.fold(F.discoverPath(g), G.discoverPath(g))
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions