I am new to shapeless and have been trying to practice some type level programming. I took Problem #1 from Project Euler as my first challenge.
I started by writing regular scala code:
object ProjectEuler1 {
def e1(limit: Int) = (1 until limit).foldLeft(0) {
case (acc, x) if x % 3 * x % 5 == 0 => acc + x
case (acc, _) => acc
}
val out = e1(10)
assert(out == 23)
}
Then, I came up with this working shapeless implementation using poly
:
object ProjectEuler1Shapeless extends App {
import shapeless._
import nat._
import ops.nat._
import poly._
import test.typed
trait eLP extends Poly1 {
implicit def default[A <: Nat] = at[A] { _ => _0 }
}
object e extends eLP {
implicit def match3[A <: Nat](implicit ev: Mod.Aux[A, _3, _0]) = at[A](identity)
implicit def match5[A <: Nat](implicit ev: Mod.Aux[A, _5, _0]) = at[A](identity)
}
object sum extends Poly2 {
implicit def sum[A <: Nat, B <: Nat, Z <: Nat](implicit s: Sum.Aux[A, B, Z],
z: Witness.Aux[Z]) =
at[A, B] { (_, _) => z.value }
}
type _23 = Succ[_22]
val l = _1 :: _2 :: _3 :: _4 :: _5 :: _6 :: _7 :: _8 :: _9 :: HNil
val out = l.map(e).foldLeft(_0)(sum)
typed[_23](out)
}
Next, I wanted to change the function so that I don't need to manually create a list. Instead it accepts a "limit" as an argument like the regular scala code. I came up with this:
object ProjectEuler1Shapeless2 extends App {
import shapeless._
import nat._
import ops.nat._
import test.typed
class E1[I <: Nat, N <: Nat]
trait ELP0 {
implicit def default[I <: Nat, M <: Nat] = new E1[I, _0]
}
trait ELP1 extends E1LP0 {
implicit def match3[A <: Nat](implicit ev: Mod.Aux[A, _3, _0]) = new E1[A, A]
implicit def match5[A <: Nat](implicit ev: Mod.Aux[A, _5, _0]) = new E1[A, A]
}
object E1 extends E1LP1 {
implicit def combine[I <: Nat, L <: Nat, M <: Nat](implicit e1: E1[I, L],
m: E1[Succ[I], M],
sum: Sum[L, M]) =
new E1[Succ[Succ[I]], sum.Out]
}
def e1[N <: Nat](limit: Nat)(implicit e: E1[limit.N, N], w: Witness.Aux[N]): N = w.value
val f1 = e1(1)
typed[_0](f1)
val f2 = e1(2)
typed[_0](f2)
val f3 = e1(3)
typed[_3](f3) // Does not compile!
}
I've gotten stuck here. The compiler is telling me it found _0
. I guess it's picking up the instance from def default
.
Any tips on how I can fix this? I have a feeling my strategy for solving this problem might be a little weird also. Any pointers on how I can make this shapeless code more idiomatic are greatly appreciated.
My original strategy was to create a hylomorphism. I noticed there is an unfold example in the shapeless git but its complexity escapes me at the moment.
I find it a little easier to think about this problem inductively (at the type level, at least). First we can define a helper type class that returns N
if N
is a multiple of one of the numbers in M
, and _0
otherwise:
import shapeless._, nat._0, ops.nat.Mod
trait IfMultiple[N <: Nat, M <: HList] { type Out <: Nat }
trait LowPriorityIfMultiple {
type Aux[N <: Nat, M <: HList, Out0 <: Nat] = IfMultiple[N, M] {
type Out = Out0
}
implicit def isMultiple1[N <: Nat, H <: Nat, T <: HList](implicit
ifMultiple: IfMultiple[N, T]
): Aux[N, H :: T, ifMultiple.Out] = new IfMultiple[N, H :: T] {
type Out = ifMultiple.Out
}
}
object IfMultiple extends LowPriorityIfMultiple {
implicit def ifMultiple0[N <: Nat]: Aux[N, HNil, _0] =
new IfMultiple[N, HNil] {
type Out = _0
}
implicit def ifMultiple2[N <: Nat, H <: Nat, T <: HList](implicit
mod: Mod.Aux[N, H, _0]
): Aux[N, H :: T, N] = new IfMultiple[N, H :: T] {
type Out = N
}
}
And now we just need a type class to add up all these values from _0
to N - _1
:
import nat._1, ops.nat.Sum
trait SumOfMultiples[N <: Nat, M <: HList] extends DepFn0 { type Out <: Nat }
object SumOfMultiples {
type Aux[N <: Nat, M <: HList, Out0 <: Nat] = SumOfMultiples[N, M] {
type Out = Out0
}
def apply[N <: Nat, M <: HList](implicit
som: SumOfMultiples[N, M]
): Aux[N, M, som.Out] = som
implicit def sum0[M <: HList]: Aux[_1, M, _0] =
new SumOfMultiples[_1, M] {
type Out = _0
def apply(): _0 = _0
}
implicit def sumN[P <: Nat, M <: HList, NV <: Nat, PT <: Nat, NT <: Nat](implicit
ifMultiple: IfMultiple.Aux[P, M, NV],
som: Aux[P, M, PT],
sum: Sum.Aux[NV, PT, NT],
wit: Witness.Aux[NT]
): Aux[Succ[P], M, NT] = new SumOfMultiples[Succ[P], M] {
type Out = NT
def apply(): NT = wit.value
}
}
And then we're done:
import nat._, test.typed
val result = SumOfMultiples[_10, _3 :: _5 :: HNil]
typed[Succ[_22]](result())
Which compiles as expected.
It's worth noting that there are other ways you could solve this problem. You could create a type class that would provide Nat
ranges and then fold over that with a Poly2
using IfMultiple
. You could also define an IsMultiple
type class that just witnesses that N
is a multiple of one of the numbers in M
—my first quick attempt did this, but I ran into ambiguity issues, so I went with the similar version above. The implementation here is fairly straightforward, though, and unless you have other applications for e.g. Nat
ranges, I think it's a pretty reasonable solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With