Last active
August 29, 2015 14:14
-
-
Save MasseGuillaume/f44db6a0fd94bd32f570 to your computer and use it in GitHub Desktop.
ttfi abstracted over Numeric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Repr[T] | |
trait ExpSym[Num, repr[_]] { | |
def lit: Num => repr[Num] | |
def neg: repr[Num] => repr[Num] | |
def add: repr[Num] => repr[Num] => repr[Num] | |
} | |
trait MulSym[Num, repr[_]] { | |
def mul: repr[Num] => repr[Num] => repr[Num] | |
} | |
case class NumRepr[T](value: Int) extends Repr[Int] | |
implicit object ExpSymEvalInt extends ExpSym[Int, NumRepr] { | |
def lit = x => NumRepr(x) | |
def neg = x => NumRepr(-x.value) | |
def add = x => y => NumRepr(x.value + y.value) | |
} | |
implicit object MulSymEvalInt extends MulSym[Int, NumRepr] { | |
def mul = x => y => NumRepr(x.value * y.value) | |
} | |
def tf1[repr[_]](implicit s1: ExpSym[Int, repr]): repr[Int] = | |
s1.lit(1) | |
identity(tf1[NumRepr]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MasseGuillaume the main issue is that we're contradicting ourselves. on the one hand we want
tf1
to be generic, but then on the other we hardcode a concrete, specialized witness (i.e. 1). the fix is to parameterizetf1
. the below works. also, the way you're usingNumRepr
, that really should be theEval
case class.the code above can also be seen in this commit