Skip to content

Commit

Permalink
Updated FunctionK.lift as per typelevel#2553 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
drdozer committed Mar 29, 2019
1 parent 355979e commit 2d4cc2c
Showing 1 changed file with 5 additions and 66 deletions.
71 changes: 5 additions & 66 deletions core/src/main/scala/cats/arrow/FunctionK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package arrow

import cats.data.Coproduct

import cats.macros.MacroCompat

/**
* `FunctionK[F[_], G[_]]` is a functor transformation from `F` to `G`
* in the same manner that function `A => B` is a morphism from values
Expand Down Expand Up @@ -80,73 +78,14 @@ object FunctionK {
*
* Additionally, the type parameters on `f` must not be specified.
*/
def lift[F[_], G[_]](f: (F[α] G[α]) forSome { type α }): FunctionK[F, G] =
macro FunctionKMacros.lift[F, G]

}

private[arrow] object FunctionKMacros extends MacroCompat {

def lift[F[_], G[_]](c: Context)(
f: c.Expr[(F[α] G[α]) forSome { type α }]
)(
implicit evF: c.WeakTypeTag[F[_]], evG: c.WeakTypeTag[G[_]]
): c.Expr[FunctionK[F, G]] =
c.Expr[FunctionK[F, G]](new Lifter[c.type ](c).lift[F, G](f.tree))
// ^^note: extra space after c.type to appease scalastyle

private[this] class Lifter[C <: Context](val c: C) {
import c.universe._

def lift[F[_], G[_]](tree: Tree)(
implicit evF: c.WeakTypeTag[F[_]], evG: c.WeakTypeTag[G[_]]
): Tree = unblock(tree) match {
case q"($param) => $trans[..$typeArgs](${ arg: Ident })" if param.name == arg.name
def lift[F[_]]: LiftPartiallyApplied[F] = new LiftPartiallyApplied

typeArgs
.collect { case tt: TypeTree => tt }
.find(tt => Option(tt.original).isDefined)
.foreach { param => c.abort(param.pos,
s"type parameter $param must not be supplied when lifting function $trans to FunctionK")
}
private[arrow] final class LiftPartiallyApplied[F[_]](val dummy: Boolean = true) extends AnyVal {
type T

val F = punchHole(evF.tpe)
val G = punchHole(evG.tpe)

q"""
new FunctionK[$F, $G] {
def apply[A](fa: $F[A]): $G[A] = $trans(fa)
}
"""
case other
c.abort(other.pos, s"Unexpected tree $other when lifting to FunctionK")
}

private[this] def unblock(tree: Tree): Tree = tree match {
case Block(Nil, expr) expr
case _ tree
def apply[G[_]](f: F[T] => G[T]): FunctionK[F, G] = new FunctionK[F, G] {
def apply[A](fa: F[A]): G[A] = f(fa.asInstanceOf[F[T]]).asInstanceOf[G[A]]
}

private[this] def punchHole(tpe: Type): Tree = tpe match {
case PolyType(undet :: Nil, underlying: TypeRef)
val α = compatNewTypeName(c, "α")
def rebind(typeRef: TypeRef): Tree =
if (typeRef.sym == undet) tq""
else {
val args = typeRef.args.map {
case ref: TypeRef => rebind(ref)
case arg => tq"$arg"
}
tq"${typeRef.sym}[..$args]"
}
val rebound = rebind(underlying)
tq"""({type λ[] = $rebound})#λ"""
case TypeRef(pre, sym, Nil)
tq"$sym"
case _ =>
c.abort(c.enclosingPosition, s"Unexpected type $tpe when lifting to FunctionK")
}

}

}

0 comments on commit 2d4cc2c

Please sign in to comment.