Skip to content

Instantly share code, notes, and snippets.

@jto
Last active February 27, 2016 13:57
Show Gist options
  • Save jto/3d3d91f39e4520800a76 to your computer and use it in GitHub Desktop.
Save jto/3d3d91f39e4520800a76 to your computer and use it in GitHub Desktop.
sealed class Match[A, B] { type τs <: HList }
object Match {
type Aux[A0, B0, τ0 <: HList] = Match[A0, B0] { type τs = τ0 }
sealed trait τ
sealed trait τ1[A]
implicit def matchEq[A0]: Aux[A0, A0, HNil] =
new Match[A0, A0]{
type τs = HNil
}
implicit def match0[A0]: Aux[τ, A0, A0 :: HNil] =
new Match[τ, A0] { type τs = A0 :: HNil }
implicit def matchTC[F[_], TC[_[_]]]: Aux[TC[τ1], TC[F], F[τ] :: HNil] =
new Match[TC[τ1], TC[F]] { type τs = F[τ] :: HNil }
implicit def match1[F[_], A, B](implicit m0: Match[A, B]): Aux[F[A], F[B], m0.τs] =
new Match[F[A], F[B]] { type τs = m0.τs }
implicit def match2[F[_, _], A0, A1, B0, B1, τs0 <: HList, τs1 <: HList](implicit m0: Aux[A0, B0, τs0], m1: Aux[A1, B1, τs1], p: Prepend[τs0, τs1]): Aux[F[A0, A1], F[B0, B1], p.Out] =
new Match[F[A0, A1], F[B0, B1]] { type τs = p.Out }
def apply[A, B](implicit m: Match[A, B]): Aux[A, B, m.τs] = m
}
@jto
Copy link
Author

jto commented Feb 27, 2016

Matching Functor:

import cats.Functor
import cats.Functor
import Match.τ
import Match.τ1

scala> Match[Functor1], Functor[List]]
res1: jto.validation.free.Match[cats.Functor[jto.validation.free.Match1],cats.Functor[[+A]List[A]]]{type τs = shapeless.::[List[jto.validation.free.Match.τ],shapeless.HNil]} = jto.validation.free.Match$$anon$3@428ef645

I'm not super satisfied by τs being List[τ] :: HNil thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment