Jan. 20th, 2017

Simple

Jan. 20th, 2017 12:41 am
xacid: (Default)
import org.parboiled2._

object Simple extends App {

  trait LambdaCalculus[T] {
    def ref(n: String): T
    def lam(n: String, t: T): T
    def app(f: T, a: T): T
  }

  trait Expr[T] {
    def oper(a: T, f: String, b: T): T
    def parens(a: T): T
  }

  trait Literals[T] {
    def int(i: Int): T
  }

  trait Sym[T] extends LambdaCalculus[T]
    with Expr[T] with Literals[T]

  trait Ast {
    def apply[T: Sym]: T
  }

  trait SymAst extends Sym[Ast] {
    import Ast._

    def ref(n: String): Ast = Ref(n)
    def lam(n: String, t: Ast): Ast = Lam(n, t)
    def app(f: Ast, a: Ast): Ast = App(f, a)
    def oper(a: Ast, f: String, b: Ast): Ast = Oper(a, f, b)
    def parens(a: Ast): Ast = Parens(a)
    def int(i: Int): Ast = IntV(i)
  }
Read more... )

  val main = "(x -> y -> z -> y * ( x + y * z))"
  val answer = parser(main)

  println(answer.map(ShowSym(_)))
  println(answer.map(SymVM(_, 1, 2, 10)))

  println(parser(main + " 1 2 10").map(x =>
    ShowSym(x) + " = " + SymVM(x)))

}

Profile

xacid: (Default)
xacid

August 2017

S M T W T F S
  123 45
67891011 12
13 141516171819
20212223242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 19th, 2017 05:02 pm
Powered by Dreamwidth Studios