typelevel / simulacrum

First class syntax support for type classes in Scala
BSD 3-Clause "New" or "Revised" License
937 stars 61 forks source link

Add `instance` support: #5

Open erikkaplun opened 9 years ago

erikkaplun commented 9 years ago

Currently, in order to support things like:

sealed case class Foo(value: Int)

implicit val fooSemigroup = Semigroup.instance[Foo](
  append = (a, b) => Foo(a.value + b.value)
)

a lot of boilerplate is needed:

object Semigroup {
  def instance[T](append: (T, T) => T) = {
    val append0 = append
    new Semigroup[T] { def append(a: T, b: T): T = append0(a, b) }
  }
}

for type classes with 2 and more member functions, this gets worse:

object Monoid {
  def instance[T](zero: T, append: (T, T) => T) = {
    // all the aliases are needed so that the parameter names could be
    // the same as the function names, while avoiding infinite recursion
    val zero0 = zero
    val append0 = append
    new Monoid[T] {
      def zero = zero0
      def append(a: T, b: T): T = append0(a, b)
    }
  }
}

The default order would match the declaration order (inherited type class members first or last?); if the default order isn't good enough and named arguments are not desired, one could use the hypothetical instance = ... parameter to @typeclass:

@typeclass(instance = ('zero, 'append))
trait Monoid[T] extends Semigroup[T] {
  def zero: T
}

implicit val fooMonoid = Monoid.instance[Foo](Foo(0), (a, b) => Foo(a.value + b.value))
// or
implicit val fooMonoid = Monoid.instance[Foo](
  zero = Foo(0),
  append = (a, b) => Foo(a.value + b.value)
)

...or possibly better alternatives than instance, perhaps order or deriveOrder or something like that?

mpilquist commented 9 years ago

One thing to note with this syntax is that it wouldn't support multiple parameter lists designed to encourage type inference and syntax. For example, Monod.instance might be defined as:

def instance[A](id: A)(append: (A, A) => A): Monoid[A]

This definition works nicer with Scala's type inferencing rules. E.g., in Monoid.instance(0)(_ + _), A is correctly inferred as Int whereas in Monoid.instance(0, _ + _), Scala is not able to infer that the second param is a Function2[Int, Int, Int].

I don't think this is a blocker though -- folks can always define their own instance methods that are hand crafted to these types of situations. Just an observation.

erikkaplun commented 9 years ago

To me, Monoid.instance[Int](...) is preferrable anyway rather than leaving off the type parameter. In Haskell you also declare it explicitly: instance Monoid Int where.

fommil commented 7 years ago

Found myself wanting something like this today. Makes sense when creating instances for typeclasses that have only one abstract method on them.

@typeclass trait UrlEncoded[T] {
  def urlEncoded(t: T): String
}

could helpfully generate

   def instance[T](body: T => String): UrlEncoded[T] = new UrlEncoded[T] {
     override def urlEncoded(t: T): String = body(t)
   }

on the companion allowing all the primitive impls to save on boilerplate. e.g.

implicit val urlEncodedString: UrlEncoded[String] = instance { s => URLEncoder.encode(s, "UTF-8") }

// @SystemFw

fommil commented 7 years ago

I'm wondering if there is something even more radical we could do. Imagine "first class instances" on the companion like this (for a typeclass F with a single abstract method that returns B)

def instance(f: Foo): B = ...

that would get converted into

implicit val FFoo: F[Foo] = instance(f => ... )

(or the longer form without instance).

I'm not sure if the @typeclass on the trait would allow for messing with the companion, or if the overloading of methods named instance would get as far as the macro stage... but it'd be lovely if it worked!