MLscript demonstration
The code is available on github.
Tip: additional definitions from the paper – copy and paste to try them out:
class Mul[out E](lhs: E, rhs: E)
mixin EvalMul {
fun eval(override Mul(l, r)) = this.eval(l) * this.eval(r)
}
mixin EvalNegNeg {
fun eval(override Neg(Neg(d))) = this.eval(d)
}
module TestLang2 extends EvalAddLit, EvalNeg, EvalMul, EvalNegNeg
let res2 = TestLang2.eval(add2negadd11)
class Some[out A](value: A)
mixin Foo { fun foo: Int = 1 }
mixin Bar { fun foo = Some(super.foo) }
module ClsFoo extends Foo, Bar, Bar
ClsFoo.foo
class Some[out A](value: A)
module None
mixin ComparePoint {
fun compare(lhs, rhs) = (lhs.x == rhs.x) && (lhs.y == rhs.y)
}
mixin CompareColored {
fun compare(lhs, rhs) =
super.compare(lhs, rhs) && eq(lhs.color)(rhs.color)
}
mixin CompareNested {
fun compare(lhs, rhs) =
super.compare(lhs, rhs) &&
if lhs.parent is Some(p)
then rhs.parent is Some(q) and this.compare(p, q)
else rhs.parent is None
}
class MyPoint(x: Int, y: Int, color: Str, parent: Some[MyPoint] | None)
module CompareMyPoint extends ComparePoint, CompareColored, CompareNested
let Red = "red"
let p0 = MyPoint(0, 0, Red, None)
let p1 = MyPoint(0, 1, Red, None)
let p2 = MyPoint(0, 1, Red, None)
let p3 = MyPoint(0, 1, Red, Some(p1))
let p4 = MyPoint(0, 1, Red, Some(p2))
let p5 = MyPoint(0, 1, Red, Some(p3))
CompareMyPoint.compare(p0, p1)
CompareMyPoint.compare(p1, p2)
CompareMyPoint.compare(p3, p4)
CompareMyPoint.compare(p3, p5)