Ludovic L'HOURS github.com/madbrain
val x = new Matrix(4, 5)
val y = new Matrix(5, 3)
val z = new Matrix(4, 3)
println(x.mult(y)) // correct
println(x.mult(z)) // incorrect
class Matrix(val n: Int, val m: Int) {
val elements = Array.ofDim[Double](n, m)
def add(o: Matrix): Matrix {
if (o.n != n || o.m != m) = {
throw new RuntimeException("incompatible sizes")
}
// ...
}
def mult(o: Matrix): Matrix = {
if (m != o.n) {
throw new RuntimeException("incompatible sizes")
}
// ...
}
}
trait Nat
class _0 extends Nat
case class Succ[N <: Nat]() extends Nat
val _1 = Succ[_0]
val _2 = Succ[Succ[_0]]
class Matrix[N <: Nat, M <: Nat] {
val elements = Array.ofDim[Double](n, m)
def add(o: Matrix[N, M]): Matrix[N, M] = {
// ...
}
def mult[X <: Nat](o: Matrix[M, X]): Matrix[N, X] = {
// ...
}
}
class Matrix[N <: Nat, M : Nat](
implicit toIntN: ToInt[N], toIntM: ToInt[M]) {
val elements = Array.ofDim[Double](toIntN(), toIntM())
def add(o: Matrix[N, M]): Matrix[N, M] {
// ...
}
def mult[X <: Nat](o: Matrix[M, X]): Matrix[N, X] {
// ...
}
}
class Matrix[N <: Nat, M : Nat] {
def concat[X <: Nat](o: Matrix[N, X])(
implicit sum: Sum[M, X]): Matrix[N, sum.Out] {
// ...
}
}
factorial(0, 1).
factorial(N, F) :-
N1 is N-1,
factorial(N1, F1),
F is N * F1.
factorial(0, 1).
factorial(N, F) :-
sub(N, 1, N1),
factorial(N1, F1),
mult(N, F1, F).
factorial(0, 1).
factorial(succ(N), F) :-
factorial(N, F1),
mult(succ(N), F1, F).
trait Factorial[In <: Nat, Out <: Nat]
object Factorial {
implicit val fact0 = new Factorial[Nat._0, Nat._1] {}
}
trait Factorial[In <: Nat, Out <: Nat]
object Factorial {
implicit val fact0 = new Factorial[Nat._0, Nat._1] {}
implicit def factN[N <: Nat, X <: Nat](
implicit fact: Factorial[N, X],
mult: Prod[Succ[N], X]) =
new Factorial[Succ[N], mult.Out] {}
}
trait Factorial[In <: Nat, Out <: Nat]
object Factorial {
implicit val fact0 = new Factorial[Nat._0, Nat._1] {}
implicit def factN[N <: Nat, X <: Nat](
implicit fact: Factorial[N, X],
mult: Prod[Succ[N], X]) =
new Factorial[Succ[N], mult.Out] {}
def apply[N <: Nat, R <: Nat](x: N)(
implicit fact: Factorial[N, R], toInt: ToInt[R]) = toInt()
}
def main(args: Array[String]) {
println(Factorial(Nat._4))
}
println(Factorial.apply(
_4,
Factorial.factN(
Factorial.factN(
Factorial.factN(
Factorial.factN(Factorial.fact0(),
Prod.prod2(Prod.prod1(), Sum.sum2(Sum.sum1()))),
Prod.prod2(Prod.prod2(Prod.prod1(), Sum.sum2(Sum.sum1())), Sum.sum2(Sum.sum1()))),
Prod.prod2(Prod.prod2(Prod.prod2(Prod.prod1(), Sum.sum2(Sum.sum2(Sum.sum1()))),
Sum.sum2(Sum.sum2(Sum.sum1()))), Sum.sum2(Sum.sum2(Sum.sum1())))),
Prod.prod2(Prod.prod2(Prod.prod2(Prod.prod2(Prod.prod1(),
Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum1()))))))),
Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum1()))))))),
Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum1()))))))),
Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum2(Sum.sum1())))))))),
ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(
ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(
ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(
ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toIntSucc(
ToInt.toIntSucc(ToInt.toIntSucc(
ToInt.toIntSucc(ToInt.toIntSucc(ToInt.toInt0()))))))))))))))))))))))))));
val fooRoute = GET on "foo" to Application.foo
val showRoute = PUT on "show" / * to Application.show
val barRoute = POST on "bar" / * / * to Application.bar
def foo() = {
println("foo")
}
def show(a: Int) = {
println(s"show $a")
}
def bar(a: String, b: Boolean) = {
println(s"bar $a $b")
}
https://madbrain.github.com/pres-shapeless/