Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Library._
- object Desugar {
- def desugarList(es: List[ExprExt]): ExprC = es match {
- case h :: t => ConsC(desugar(h), desugarList(t))
- case h :: Nil => desugar(h)
- case _ => NilC()
- }
- def makeList(rows: List[ExprC]): ExprC = rows match {
- case rowList :: t => ConsC(rowList, makeList(t))
- case Nil => NilC()
- }
- def solveScalarMult(n: ExprC, m: ExprC): ExprC = m match {
- case ConsC(h, t) => ConsC(ListMapC(
- FdC("scalar-mult", MultC(n, IdC("scalar-mult"))), h)
- , solveScalarMult(n, t))
- case NilC() => NilC()
- }
- def desugar(e: ExprExt): ExprC = e match {
- case NumExt(n) => NumC(n)
- case PlusExt(e1, e2) => PlusC(desugar(e1), desugar(e2))
- case MultExt(e1, e2) => MultC(desugar(e1), desugar(e2))
- case FdExt(x, e) => FdC(x, desugar(e))
- case AppExt(f, a) => AppC(desugar(f), desugar(a))
- case IdExt(x) => IdC(x)
- case ConsExt(h, t) => ConsC(desugar(h), desugar(t))
- case NilExt() => NilC()
- case ListExt(es) =>
- desugarList(es)
- case ListMapExt(f, l) =>
- ListMapC(desugar(f), desugar(l))
- case MatrixExt(rs) =>
- val rows: List[ExprC] = rs.map(row => desugarList(row.es))
- makeList(rows)
- case ScalarMultExt(n, m) => solveScalarMult(desugar(n), desugar(m))
- // KEY
- // println(desugar(m))
- // desugar(m) match {
- // case ConsC(h, t) => println(h)
- // }
- // ListMapC(
- // FdC("scalar-mult", MultC(desugar(n), IdC("scalar-mult"))), desugar(m)
- // )
- // m match {
- // case MatrixExt(rs) =>
- // desugar(n) match {
- // case num: NumC =>
- // val rows: List[ExprC] = rs.map(row => row match {
- // case RowExt(es) => desugarListMultiply(es, num)
- // })
- // //println(rows)
- // makeList(rows)
- // case _ => throw new RuntimeException(s"$n Does not evaluate to a number")
- // }
- // case _ => throw new RuntimeException(s"$m Does not evaluate to a matrix")
- // }
- }
- }
- //Expected ConsV(ConsV(NumV(3),ConsV(NumV(6),ConsV(NumV(9),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(15),ConsV(NumV(18),NilV()))),NilV())),
- // but got ConsV(ConsV(NumV(3),ConsV(NumV(2),ConsV(NumV(3),NilV()))),ConsV(ConsV(NumV(12),ConsV(NumV(5),ConsV(NumV(6),NilV()))),NilV()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement