I'm trying to learn Shapeless, and I would like to define a monoid which adds together instances of shapeless records. Note that I'm using algebird monoids (not scalaz), but I'm sure they're quite similar. Here's an example of what I'd like to be able to do:
val result = Monoid.sum(
('a ->> 1) :: ('b ->> 1) :: HNil,
('a ->> 4) :: ('b ->> 3) :: HNil,
('a ->> 2) :: ('b ->> 6) :: HNil)
// result should be: ('a ->> 7) :: ('b ->> 10) :: HNil
I figured out how to write monoid instances for HList, as follows:
implicit val HNilGroup: Group[HNil] = new ConstantGroup[HNil](HNil)
implicit val HNilMonoid: Monoid[HNil] = HNilGroup
class HListMonoid[H, T <: HList](implicit hmon: Monoid[H], tmon: Monoid[T]) extends Monoid[::[H, T]] {
def zero = hmon.zero :: tmon.zero
def plus(a: ::[H, T], b: ::[H, T]) =
hmon.plus(a.head, b.head) :: tmon.plus(a.tail, b.tail)
}
implicit def hListMonoid[H, T <: HList](implicit hmon: Monoid[H], tmon: Monoid[T]) = new HListMonoid[H, T]
This allows me to write:
val result = Monoid.sum(
1 :: 1 :: HNil,
4 :: 3 :: HNil,
2 :: 6 :: HNil)
// result is 7 :: 10 :: HNil
Now that I can sum HList instances, the missing piece seems to be defining monoid instances which can sum fields of form ('name ->> 1)
, which my IDE tells me has the following type: Int with record.KeyTag[Symbol with tag.Tagged[Constant(name).type] { .. }, Int] { .. }
. At this point I'm stuck, as I just don't know how to go about doing this.