Last active
February 27, 2016 13:57
-
-
Save jto/3d3d91f39e4520800a76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Matching Functor:
import cats.Functor
import cats.Functor
import Match.τ
import Match.τ1
scala> Match[Functor[τ1], Functor[List]]
res1: jto.validation.free.Match[cats.Functor[jto.validation.free.Match.τ1],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
@milessabin. I knew you would ask ;)
I think so. I need to create an new type wildcard
τ1[_]
and maybeMatch1
but it should work :)It should also be possible to use
Match[τ1[Int], List[Int]]
I don't see how to completely abstract over arity thought.