Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active December 13, 2015 10:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travisbrown/43c9dc072bfb2bba2611 to your computer and use it in GitHub Desktop.
Save travisbrown/43c9dc072bfb2bba2611 to your computer and use it in GitHub Desktop.
Quick sketch of a not very general implementation of boilerplate-free coyoneda'd constructors
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
class FreeMacros(val c: whitebox.Context) {
import c.universe._
def smartName(name: String): String = (
name.toList match {
case h :: t => h.toLower :: t
case Nil => Nil
}
).mkString
def liftedImpl[F[_]](implicit t: c.WeakTypeTag[F[_]]): Tree = {
val atc = t.tpe.typeConstructor
val constructors = t.tpe.typeSymbol.asClass.knownDirectSubclasses.map {
case constructor: ClassSymbol =>
val companion = constructor.companion
val ctpe = constructor.typeSignature
val ctc = ctpe.typeConstructor
val methodName = TermName(smartName(constructor.name.toString))
val A = TypeName(c.freshName())
val (params, names) = ctpe.decls.collect {
case m: MethodSymbol if m.isCaseAccessor =>
m.typeSignatureIn(atc) match {
case NullaryMethodType(returnType) =>
val param =
if (returnType.typeSymbol == ctc.typeParams.head)
q"val ${m.name}: $A"
else
q"val ${m.name}: $returnType"
(param, m.name)
}
}.unzip
q"""
def $methodName[$A](..$params): _root_.cats.free.FreeC[$atc, $A] =
_root_.cats.free.Free.liftFC[$atc, $A](
$companion.apply[$A](..$names)
)
"""
}
q"new { ..$constructors }"
}
}
object FreeMacros {
def liftConstructors[F[_]]: Any = macro FreeMacros.liftedImpl[F]
}
@travisbrown
Copy link
Author

Usage:

sealed trait Foo[A]
case class Bar[A](a: A, i: Int) extends Foo[A]
case class Baz[A](s: String, a: A) extends Foo[A]
case class Qux[A](x: Char) extends Foo[A]

val liftedConstructors = FreeMacros.liftConstructors[Foo]

import liftedConstructors._

And then:

scala> bar("a", 1)
res0: cats.free.FreeC[Foo,String] = Suspend(cats.free.Coyoneda$$anon$3@1a72b45e)

scala> qux[String]('a')
res1: cats.free.FreeC[Foo,String] = Suspend(cats.free.Coyoneda$$anon$3@5db78a68)

@travisbrown
Copy link
Author

Next step: macro annotation that puts something like this on the Foo companion object:

object Foo {
  object wellTypedConstructors {
    def bar[A](a: A, i: Int): Foo[A] = Bar(a, i)
    def baz[A](s: String, a: A): Foo[A] = Baz(s, a)
    def qux[A](x: Char): Foo[A] = Qux(x)
  }

  object freeConstructors {
    def bar[A](a: A, i: Int): FreeC[Foo, A] = Free.liftFC[Foo, A](Bar(a, i))
    def baz[A](s: String, a: A): FreeC[Foo, A] = Free.liftFC[Foo, A](Baz(s, a))
    def qux[A](x: Char): FreeC[Foo, A] = Free.liftFC[Foo, A](Qux(x))
  }
}

@adelbertc
Copy link

That would be all sorts of awesome.

@adelbertc
Copy link

Another idea that would probably be similar code is to add a fold/catamorphism on the type constructor. Perhaps also have the option of hiding the data constructors so that you would only introduce ADTs via the well typed constructors and eliminate them with the fold.

If we can do something like given the type constructor return a Set of the type signatures of the data constructors, the free constructors, well types constructors, and the fold should be super easy.

Going to try to hack on the annotation macro version of this some more.

On a related note, it seems the current code falls over for something like:

sealed abstract class KvOp[A]
final case class Put(k: String, v: String) extends KvOp[Unit]
final case class Get(k: String) extends KvOp[Option[String]]

The problem I think being there's no type param on the data constructors.. adding them appears to make it work.

@travisbrown
Copy link
Author

Yeah, the next step is lining up the type parameter on each constructor with the appropriate one on the ADT type (if needed). This will fix your case, as well as the case where the constructors have other type parameters.

I'll take a stab at that this evening. It should look roughly the same for the macro annotation version.

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