Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Created February 5, 2019 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blaisorblade/23b9eae6df46bad3f551e51407c9286d to your computer and use it in GitHub Desktop.
Save Blaisorblade/23b9eae6df46bad3f551e51407c9286d to your computer and use it in GitHub Desktop.
Mutual dependencies across objects
trait Eval { outer =>
val t: Trees
val n: Normalize { val t: outer.t.type }
type Val
val eval: t.Exp => Val
val normalizingEval: t.Exp => Val =
exp => eval(n.normalize(exp))
}
trait Normalize { outer =>
val t: Trees
val e: Eval { val t: outer.t.type }
val reify: e.Val => t.Exp
val normalize: t.Exp => t.Exp =
exp => reify(e.eval(exp))
}
class Interpreter { outer =>
object t extends TreesImpl
object e extends EvalImpl {
val t = outer.t; val n = outer.n }
object n extends NormalizeImpl {
val t = outer.t; val e = outer.e }
}
object interpreter extends Interpreter
trait EvalImpl extends Eval {
val t: TreesImpl; import t._
type Val = Int
val eval: Exp => Val = exp => exp match {
case Add(l, r) => eval(l) + eval(r)
case Lit(n) => n
}
}
trait NormalizeImpl extends Normalize { outer =>
val t: TreesImpl
val e: EvalImpl { val t: outer.t.type }
val reify = t.Lit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment