Jan. 22nd, 2017

xacid: (Default)
import org.parboiled2._

object Tagless extends scala.App {

  trait SpParser {
    self: Parser =>
    def sp: Rule0 = rule(quiet(zeroOrMore(' ')))
  }

  trait Forall[F[_]] {
    def apply[T: F]: T
  }

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

  trait TermParser[T] extends Term[T] with SpParser {
    self: Parser =>
    import CharPredicate._
    def Name: Rule1[String] = rule(atomic(capture(Alpha ~ (AlphaNum).*)))
    def Term: Rule1[T] = rule(App | NonApp)
    def App: Rule1[T] = rule(NonApp ~ oneOrMore(' ' ~ sp ~ NonApp ~> (app _)))
    def NonApp: Rule1[T] = rule(Lam | Ref)
    def Lam: Rule1[T] = rule(Name ~ sp ~ "->" ~ sp ~ Term ~> (lam _))
    def Ref: Rule1[T] = rule(Name ~> (ref _))
  }

  trait ATerm[F[_] <: Term[_]] extends Term[Forall[F]] {
    def forall[T: F]: Term[T]
    def ref(n: String): Forall[F] = new Forall[F] {
      def apply[T: F]: T = forall[T].ref(n)
    }
    def lam(n: String, t: Forall[F]): Forall[F] = new Forall[F] {
      def apply[T: F]: T = forall[T].lam(n, t[T])
    }
    def app(f: Forall[F], a: Forall[F]): Forall[F] = new Forall[F] {
      def apply[T: F]: T = forall[T].app(f[T], a[T])
    }
  }

Read more... )
  val main = "(x -> y -> z -> y * ( y * z + x))"
  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

Page Summary

Style Credit

Expand Cut Tags

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