Fact

Feb. 1st, 2017 12:53 am
xacid: (Default)
Read more... )

  val main = "(x -> y -> z -> y * ( x + y * z))"
  println(showAndEval(main, 1, 2, 10))

  val fact =
    """
      | let
      |   fact = x ->
      |     if(x)
      |       x * (fact (x - 1))
      |     else 1
      | in fact 20
    """.stripMargin

  println(showAndEval(fact))
}

//Success(( x -> y -> z -> y * ( x + y * z ) ) 1 2 10 = 42)
//Success(let fact = x -> if(x) x * ( fact ( x - 1 ) ) else 1 in fact 20  = 2432902008176640000)

Sympl

Jan. 26th, 2017 03:15 am
xacid: (Default)
Read more... )
  trait ExprAst[F[_]] extends Expr[F ~> Id] {
    def alg[A](f: F[A]): Expr[A]
    object ExprAst {
      type Ast = F ~> Id
      case class Oper(a: Ast, f: String, b: Ast) extends Ast {
        def apply[A](fa: F[A]): Id[A] = alg(fa).oper(a(fa), f, b(fa))
      }
      case class Parens(a: Ast) extends Ast {
        def apply[A](fa: F[A]): Id[A] = alg(fa).parens(a(fa))
      }
    }
    import ExprAst._
    def parens(a: Ast): Ast = Parens(a)
    def oper(a: Ast, f: String, b: Ast): Ast = (a, f) match {
      case (Oper(a1, op1@("+" | "-"), b1), op2@("*" | "/")) =>
        oper(a1, op1, oper(b1, op2, b))
      case _ => Oper(a, f, b)
    }
  }

  trait ExprParser[F[_]] extends ExprAst[F] with AParser {
    self: Parser =>
    import ExprAst._
    def operChar = CharPredicate('~', '!', '@', '#', '$', '%',
      'ˆ', '&', '*', '-', '+', '/', '?', '<', '>', ':', '.', ',')
    def Parens(p: => Rule1[Ast]): Rule1[Ast] =
      rule('(' ~ sp ~ p ~ sp ~ ')' ~> (parens _))
    def Oper: Rule1[String] = rule(atomic(capture(operChar.+)))
    def Expr: Rule1[Ast] = rule(Operand ~
      oneOrMore(sp ~ Oper ~ sp ~ Operand ~> (oper _)))
    def Operand: Rule1[Ast]
  }
Read more... )
xacid: (Default)
import org.parboiled2._

object Tagless extends scala.App {

  trait Term[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 Literal[T] {
    def int(i: Int): T
  }

Read more... )
  type Id[A] = A

  trait ~>[F[_], G[_]] {
    def apply[A](fa: F[A]): G[A]
  }

  trait FTerm[F[_]] extends Term[F ~> Id] {
    def alg[A](f: F[A]): Term[A]
    def ref(n: String) =
      Lambda[F ~> Id](alg(_).ref(n))
    def lam(n: String, t: F ~> Id) =
      Lambda[F ~> Id](x => alg(x).lam(n, t(x)))
    def app(f: F ~> Id, a: F ~> Id) =
      Lambda[F ~> Id](x => alg(x).app(f(x), a(x)))
  }

  trait FExpr[F[_]] extends Expr[F ~> Id] {
    def alg[A](f: F[A]): Expr[A]
    def oper(a: ~>[F, Id], f: String, b: ~>[F, Id]) =
      Lambda[F ~> Id](x => alg(x).oper(a(x), f, b(x)))
    def parens(a: ~>[F, Id]): ~>[F, Id] =
      Lambda[F ~> Id](x => alg(x).parens(a(x)))
  }

  trait FLiteral[F[_]] extends Literal[F ~> Id] {
    def alg[A](f: F[A]): Literal[A]
    def int(i: Int) = Lambda[F ~> Id](alg(_).int(i))
  }

  trait FSym extends FTerm[Sym]
    with FExpr[Sym] with FLiteral[Sym] {
    def alg[A](f: Sym[A]): Sym[A] = f
  }

  type SymAst = Sym ~> Id

  case class FSymParser(input: ParserInput)
    extends Parser with FSym with SymParser[SymAst]
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)))

}
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)))

}

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

June 2017

S M T W T F S
    123
45678910
11121314151617
18192021222324
252627282930 

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 26th, 2017 06:39 am
Powered by Dreamwidth Studios