|
| 1 | +package shapelessex |
| 2 | + |
| 3 | +import org.scalatest._ |
| 4 | +import shapeless._ |
| 5 | + |
| 6 | +trait Monoid[T] { |
| 7 | + def zero: T |
| 8 | + def append(a: T, b: T): T |
| 9 | +} |
| 10 | + |
| 11 | +object Monoid extends ProductTypeClassCompanion[Monoid] { |
| 12 | + def mzero[T](implicit mt: Monoid[T]) = mt.zero |
| 13 | + |
| 14 | + implicit def booleanMonoid: Monoid[Boolean] = new Monoid[Boolean] { |
| 15 | + def zero = false |
| 16 | + def append(a: Boolean, b: Boolean) = a || b |
| 17 | + } |
| 18 | + |
| 19 | + implicit def intMonoid: Monoid[Int] = new Monoid[Int] { |
| 20 | + def zero = 0 |
| 21 | + def append(a: Int, b: Int) = a + b |
| 22 | + } |
| 23 | + |
| 24 | + implicit def doubleMonoid: Monoid[Double] = new Monoid[Double] { |
| 25 | + def zero = 0.0 |
| 26 | + def append(a: Double, b: Double) = a + b |
| 27 | + } |
| 28 | + |
| 29 | + implicit def stringMonoid: Monoid[String] = new Monoid[String] { |
| 30 | + def zero = "" |
| 31 | + def append(a: String, b: String) = a + b |
| 32 | + } |
| 33 | + |
| 34 | + object typeClass extends ProductTypeClass[Monoid] { |
| 35 | + def emptyProduct = new Monoid[HNil] { |
| 36 | + def zero = HNil |
| 37 | + def append(a: HNil, b: HNil) = HNil |
| 38 | + } |
| 39 | + |
| 40 | + def product[F, T <: HList](mh: Monoid[F], mt: Monoid[T]) = new Monoid[F :: T] { |
| 41 | + def zero = mh.zero :: mt.zero |
| 42 | + def append(a: F :: T, b: F :: T) = mh.append(a.head, b.head) :: mt.append(a.tail, b.tail) |
| 43 | + } |
| 44 | + |
| 45 | + def project[F, G](instance: ⇒ Monoid[G], to: F ⇒ G, from: G ⇒ F) = new Monoid[F] { |
| 46 | + def zero = from(instance.zero) |
| 47 | + def append(a: F, b: F) = from(instance.append(to(a), to(b))) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +trait MonoidSyntax[T] { |
| 53 | + def |+|(b: T): T |
| 54 | +} |
| 55 | + |
| 56 | +object MonoidSyntax { |
| 57 | + implicit def monoidSyntax[T](a: T)(implicit mt: Monoid[T]): MonoidSyntax[T] = new MonoidSyntax[T] { |
| 58 | + def |+|(b: T) = mt.append(a, b) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** == Automatic type class instance derivation == |
| 63 | + * |
| 64 | + * Based on and extending `Generic` and `LabelledGeneric`, Lars Hupel ([[https://twitter.com/larsr_h @larsr_h]]) has contributed the `TypeClass` |
| 65 | + * family of type classes, which provide automatic type class derivation facilities roughly equivalent to those available |
| 66 | + * with GHC as described in [[http://dreixel.net/research/pdf/gdmh.pdf "A Generic Deriving Mechanism for Haskell"]]. There is a description of an |
| 67 | + * earlier iteration of the Scala mechanism [[http://typelevel.org/blog/2013/06/24/deriving-instances-1.html here]], and examples of its use deriving `Show` and `Monoid` |
| 68 | + * instances [[https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/shows.scala here]] |
| 69 | + * and [[https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/monoids.scala here]] for labelled coproducts and unlabelled products respectively. |
| 70 | + * |
| 71 | + * For example, in the `Monoid` case, once the general deriving infrastructure for monoids is in place, instances are |
| 72 | + * automatically available for arbitrary case classes without any additional boilerplate |
| 73 | + * |
| 74 | + * {{{ |
| 75 | + * trait Monoid[T] { |
| 76 | + * def zero: T |
| 77 | + * def append(a: T, b: T): T |
| 78 | + * } |
| 79 | + * |
| 80 | + * object Monoid extends ProductTypeClassCompanion[Monoid] { |
| 81 | + * def mzero[T](implicit mt: Monoid[T]) = mt.zero |
| 82 | + * |
| 83 | + * implicit def booleanMonoid: Monoid[Boolean] = new Monoid[Boolean] { |
| 84 | + * def zero = false |
| 85 | + * def append(a: Boolean, b: Boolean) = a || b |
| 86 | + * } |
| 87 | + * |
| 88 | + * implicit def intMonoid: Monoid[Int] = new Monoid[Int] { |
| 89 | + * def zero = 0 |
| 90 | + * def append(a: Int, b: Int) = a+b |
| 91 | + * } |
| 92 | + * |
| 93 | + * implicit def doubleMonoid: Monoid[Double] = new Monoid[Double] { |
| 94 | + * def zero = 0.0 |
| 95 | + * def append(a: Double, b: Double) = a+b |
| 96 | + * } |
| 97 | + * |
| 98 | + * implicit def stringMonoid: Monoid[String] = new Monoid[String] { |
| 99 | + * def zero = "" |
| 100 | + * def append(a: String, b: String) = a+b |
| 101 | + * } |
| 102 | + * |
| 103 | + * object typeClass extends ProductTypeClass[Monoid] { |
| 104 | + * def emptyProduct = new Monoid[HNil] { |
| 105 | + * def zero = HNil |
| 106 | + * def append(a: HNil, b: HNil) = HNil |
| 107 | + * } |
| 108 | + * |
| 109 | + * def product[F, T <: HList](mh: Monoid[F], mt: Monoid[T]) = new Monoid[F :: T] { |
| 110 | + * def zero = mh.zero :: mt.zero |
| 111 | + * def append(a: F :: T, b: F :: T) = mh.append(a.head, b.head) :: mt.append(a.tail, b.tail) |
| 112 | + * } |
| 113 | + * |
| 114 | + * def project[F, G](instance: => Monoid[G], to: F => G, from: G => F) = new Monoid[F] { |
| 115 | + * def zero = from(instance.zero) |
| 116 | + * def append(a: F, b: F) = from(instance.append(to(a), to(b))) |
| 117 | + * } |
| 118 | + * } |
| 119 | + * } |
| 120 | + * |
| 121 | + * trait MonoidSyntax[T] { |
| 122 | + * def |+|(b: T): T |
| 123 | + * } |
| 124 | + * |
| 125 | + * object MonoidSyntax { |
| 126 | + * implicit def monoidSyntax[T](a: T)(implicit mt: Monoid[T]): MonoidSyntax[T] = new MonoidSyntax[T] { |
| 127 | + * def |+|(b: T) = mt.append(a, b) |
| 128 | + * } |
| 129 | + * } |
| 130 | + * }}} |
| 131 | + * The [[https://github.com/typelevel/shapeless-contrib shapeless-contrib]] project also contains automatically derived type class instances for |
| 132 | + * [[https://github.com/typelevel/shapeless-contrib/blob/master/scalaz/main/scala/typeclass.scala Scalaz]], |
| 133 | + * [[https://github.com/typelevel/shapeless-contrib/blob/master/spire/main/scala/typeclass.scala Spire]] and |
| 134 | + * [[https://github.com/typelevel/shapeless-contrib/blob/master/scalacheck/main/scala/package.scala Scalacheck]]. |
| 135 | + * |
| 136 | + * @param name auto_typeclass_derivation |
| 137 | + * |
| 138 | + */ |
| 139 | +object AutoTypeClassExercises extends FlatSpec with Matchers with exercise.Section { |
| 140 | + |
| 141 | + object Helper { |
| 142 | + |
| 143 | + // A pair of arbitrary case classes |
| 144 | + case class Foo(i: Int, s: String) |
| 145 | + case class Bar(b: Boolean, s: String, d: Double) |
| 146 | + } |
| 147 | + |
| 148 | + import Helper._ |
| 149 | + |
| 150 | + /** {{{ |
| 151 | + * |
| 152 | + * // A pair of arbitrary case classes |
| 153 | + * case class Foo(i : Int, s : String) |
| 154 | + * case class Bar(b : Boolean, s : String, d : Double) |
| 155 | + * |
| 156 | + * }}} |
| 157 | + */ |
| 158 | + def monoidDerivation(res0: Int, res1: String, res2: Boolean, res3: String, res4: Double) = { |
| 159 | + |
| 160 | + import MonoidSyntax._ |
| 161 | + import Monoid.typeClass._ |
| 162 | + |
| 163 | + val fooCombined = Foo(13, "foo") |+| Foo(23, "bar") |
| 164 | + fooCombined should be(Foo(res0, res1)) |
| 165 | + |
| 166 | + val barCombined = Bar(true, "foo", 1.0) |+| Bar(false, "bar", 3.0) |
| 167 | + barCombined should be(Bar(res2, res3, res4)) |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments