Skip to content

Commit 139eec4

Browse files
committed
Complete test case
1 parent 32ac2f3 commit 139eec4

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

tests/run/tagless.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ Node(Add,WrappedArray(Node(Lit,WrappedArray(Leaf(8))), Node(Neg,WrappedArray(Nod
1616
(8 + ((-1) + (-2)))
1717
(8 + ((-1) + (-2)))
1818
(8 + ((-1) + (-2)))
19+
(7 + 1 * (-2))
20+
7 * (8 + ((-1) + (-2)))

tests/run/tagless.scala

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ object Test extends App {
9999
private class Exc(msg: String) extends Exception(msg)
100100
def _throw(msg: String) with CanThrow: Nothing = throw new Exc(msg)
101101
def _try[T](op: CanThrow |=> T)(handler: String => T): T = {
102-
val ct = new CanThrow()
103-
try op with ct
102+
instance of CanThrow
103+
try op
104104
catch {
105105
case ex: Exception => handler(ex.getMessage)
106106
}
@@ -185,7 +185,7 @@ object Test extends App {
185185

186186
enum NCtx { case Pos, Neg }
187187

188-
instance NegExp[T] with (e: Exp[T]) of Exp[NCtx => T] {
188+
instance [T] with (e: Exp[T]) of Exp[NCtx => T] {
189189
import NCtx._
190190
def lit(i: Int) = {
191191
case Pos => e.lit(i)
@@ -202,9 +202,38 @@ object Test extends App {
202202
println(tf1[NCtx => String](NCtx.Pos))
203203

204204
def pushNeg[T](e: NCtx => T): T = e(NCtx.Pos)
205-
206205
println(pushNeg(tf1[NCtx => String]))
207-
208206
println(pushNeg(pushNeg(pushNeg(tf1))): String)
209207

208+
instance [T] with (e: Mult[T]) of Mult[NCtx => T] {
209+
import NCtx._
210+
def mul(l: NCtx => T, r: NCtx => T): NCtx => T = {
211+
case Pos => e.mul(l(Pos), r(Pos))
212+
case Neg => e.mul(l(Pos), r(Neg))
213+
}
214+
}
215+
216+
println(pushNeg(tfm1[NCtx => String]))
217+
println(pushNeg(tfm2[NCtx => String]))
218+
219+
import IExp._
220+
221+
// Going from type class encoding to ADT encoding
222+
instance initialize of Exp[IExp] {
223+
def lit(i: Int): IExp = Lit(i)
224+
def neg(t: IExp): IExp = Neg(t)
225+
def add(l: IExp, r: IExp): IExp = Add(l, r)
226+
}
227+
228+
// Going from ADT encoding to type class encoding
229+
def finalize[T](i: IExp) with (e: Exp[T]): T = i match {
230+
case Lit(l) => e.lit(l)
231+
case Neg(n) => e.neg(finalize[T](n))
232+
case Add(l, r) => e.add(finalize[T](l), finalize[T](r))
233+
}
234+
235+
type Ring[T] = Exp[T] |=> Mult[T] |=> T
236+
237+
def tfm1a[T]: Ring[T] = add(lit(7), neg(mul(lit(1), lit(2))))
238+
def tfm2a[T]: Ring[T] = mul(lit(7), tf1)
210239
}

0 commit comments

Comments
 (0)