xacid: (Default)
https://arxiv.org/pdf/1703.08219.pdf

We present Flare: a new back-end for Spark that brings performance closer to the best SQL engines, without giving up the added expressiveness of Spark. We demonstrate order of magnitude speedups both for relational workloads such as TPC-H, as well as for a range of machine learning kernels that combine relational and iterative functional processing.
Flare achieves these results through (1) compilation to native code, (2) replacing parts of the Spark runtime system, and (3) extending the scope of optimization and code generation to large classes of UDFs.
xacid: (Default)
http://www.scala-native.org/en/latest/

самое приятное - работает!

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

}
xacid: (Default)
object Types extends App {

  trait Typed[F[_]] {
    def Bool: F[Boolean]
    def Int: F[Int]
    def Str: F[String]
    def ->[A, B](a: F[A], b: F[B]): F[A => B]
    def *[A, B](a: F[A], b: F[B]): F[(A, B)]
  }
object Typed ... )
xacid: (Default)
import org.parboiled2._

object Tagless extends App {

  trait Sym[F[_]] {
    def lit[A]: (String, A) => F[A]
    def app[A, B]: F[A => B] => F[A] => F[B]
    def lam[A, B]: String =>
      (F[A] => F[B]) => F[A => B]
  }

  type RunSym[A] = A
  object RunSym extends Sym[RunSym] {
    def lit[A] = (_, a) => a
    def app[A, B] = f => a => f(a)
    def lam[A, B] = s => f => f
  }

  type ShowSym[A] = String
  object ShowSym extends Sym[ShowSym] {
    def lit[A] = (s, _) => s
    def app[A, B] = f => a => s"$f($a)"
    def lam[A, B] = s => f => s"($s => ${f(s)})"
  }

  trait FSym[A] { def apply[F[_] : Sym]: F[A] }
  object FSym {
    def sym[F[_] : Sym] = implicitly[Sym[F]]
    def lit[A](s: String, a: A) = new FSym[A] {
      def apply[F[_] : Sym]: F[A] = sym.lit(s, a)
    }
    def lit[A](a: A) = new FSym[A] {
      def apply[F[_] : Sym]: F[A] = sym.lit(a.toString, a)
    }
    def app[A, B](f: FSym[A => B], a: FSym[A]) = new FSym[B] {
      def apply[F[_] : Sym]: F[B] = sym.app(f[F])(a[F])
    }
  }

  def showRunSym[A](f: FSym[A]) =
    s"${f(ShowSym)} = ${f(RunSym)}"

  object CalcSym {
    def int(x: String) = FSym.lit(x.toInt)
    def calc(f: FSym[Int => Int => Int]) =
      (x: FSym[Int], y: FSym[Int]) =>
        FSym.app(FSym.app(f, x), y)
    val PLUS = calc(FSym.lit("+", (x: Int) => x + _))
    val MINUS = calc(FSym.lit("-", (x: Int) => x - _))
    val MUL = calc(FSym.lit("*", (x: Int) => x * _))
    val DIV = calc(FSym.lit("-", (x: Int) => x / _))
  }

  class CalcSym(val input: ParserInput) extends Parser {
    import CalcSym._
    def parser = rule(Expr ~ EOI)
    def Expr: Rule1[FSym[Int]] =
      rule(Term ~ (Plus | Minus).+)
    def Term = rule(Factor ~ (Mul | Div).+)
    def Factor = rule(Number | Parens)
    def Plus = rule('+' ~ Term ~> PLUS)
    def Minus = rule('-' ~ Term ~> MINUS)
    def Mul = rule('*' ~ Factor ~> MUL)
    def Div = rule('/' ~ Factor ~> DIV)
    def Parens = rule(sp ~ '(' ~ sp ~ Expr ~ sp ~ ')' ~ sp)
    def Number = rule(sp ~ capture(Digits) ~ sp ~> (int _))
    def Digits = rule(CharPredicate.Digit.+)
    def sp = rule(zeroOrMore(' '))
  }

  val main = "(10 * 2 + 1) * 2"

  val answer = new CalcSym(main).parser.run()

  answer map showRunSym map println

}

// *(+(*(10)(2))(1))(2) = 42

xacid: (Default)
import cats._
import cats.implicits._
import scala.concurrent._
import scala.concurrent.duration.Duration._
import ExecutionContext.Implicits.global

object Tagless extends App {

  trait Sym[F[_]] {
    def lit[A]: (String, A) => F[A]
    def app[A, B]: F[A => B] => F[A] => F[B]
    def lam[A, B]: String => (F[A] => F[B]) => F[A => B]
  }

  object Sym {
    def apply[F[_] : Sym] = implicitly[Sym[F]]
    def lit[F[_] : Sym, A](s: String, a: A) =
      Sym[F].lit(s, a)
    def lit[F[_] : Sym, A](a: A) =
      Sym[F].lit(a.toString, a)
    def app[F[_] : Sym, A, B](f: F[A => B])
                             (a: F[A]) = Sym[F].app(f)(a)
    def lam[F[_] : Sym, A, B](s: String)
                             (f: (F[A] => F[B])) = Sym[F].lam(s)(f)
  }

  trait FreeSym[A] {
    def run: A
    def lift[F[_] : Applicative]: F[A]
  }

  object FreeSym extends Sym[FreeSym] {

    case class Lit[A](s: String, a: A) extends FreeSym[A] {
      override def run: A = a
      override def lift[F[_] : Applicative]: F[A] =
        Applicative[F].pure(a)
    }

    case class App[A, B](f: FreeSym[A => B],
                         a: FreeSym[A]) extends FreeSym[B] {
      override def run: B = f.run(a.run)
      override def lift[F[_] : Applicative]: F[B] =
        Applicative[F].ap(f.lift[F])(a.lift[F])
    }

    case class Lam[A, B](s: String,
                         f: FreeSym[A] => FreeSym[B])
      extends FreeSym[A => B] {
      override def run: (A) => B = a => f(Lit(s, a)).run
      override def lift[F[_] : Applicative]: F[(A) => B] =
        Applicative[F].pure(run)
    }

    override def lit[A] = Lit[A] _
    override def app[A, B] = f => a => App(f, a)
    override def lam[A, B] = s => f => Lam(s, f)
  }

  import Sym._

  def PLUS[F[_] : Sym] = lit("+",
    (x: Int) => (y: Int) => x + y)

  def main[F[_] : Sym] = lam("x")((x: F[Int]) =>
    app(app(PLUS)(x))(x))

  def answer[F[_] : Sym] = app(main)(lit(21))

  println(Await.result(answer(FreeSym).lift[Future], Inf))

}
xacid: (Default)
object Tagless extends App {

  trait Sym[F[_]] {
    def lit[A]: (String, A) => F[A]
    def app[A, B]: F[A => B] => F[A] => F[B]
    def lam[A, B]: String => (F[A] => F[B]) => F[A => B]
  }

  object Sym {
    def apply[F[_] : Sym] = implicitly[Sym[F]]
    def lit[F[_] : Sym, A](s: String, a: A) = 
       Sym[F].lit(s, a)
    def lit[F[_] : Sym, A](a: A) = 
       Sym[F].lit(a.toString, a)
    def app[F[_] : Sym, A, B](f: F[A => B])
       (a: F[A]) = Sym[F].app(f)(a)
    def lam[F[_] : Sym, A, B](s: String)
       (f: (F[A] => F[B])) = Sym[F].lam(s)(f)
  }

  type RunSym[A] = A
  object RunSym extends Sym[RunSym] {
    override def lit[A]: (String, A) => RunSym[A] =
      (_, a) => a
    override def app[A, B]: (RunSym[(A) => B]) => 
      (RunSym[A]) => RunSym[B] = f => a => f(a)
    override def lam[A, B]: String => 
      ((RunSym[A]) => RunSym[B]) => RunSym[(A) => B] =
       s => f => f
  }

  type ShowSym[A] = String
  object ShowSym extends Sym[ShowSym] {
    override def lit[A]: (String, A) => ShowSym[A] = 
      (s, _) => s
    override def app[A, B]: (ShowSym[(A) => B]) => 
      (ShowSym[A]) => ShowSym[B] = f => a => s"$f($a)"
    override def lam[A, B]: String => 
      ((ShowSym[A]) => ShowSym[B]) => ShowSym[(A) => B] =
       s => f => s"($s => ${f(s)})"
  }

  import Sym._

  def PLUS[F[_] : Sym] = lit("+",
    (x: Int) => (y: Int) => x + y)

  def main[F[_] : Sym] = lam("x")((x: F[Int]) =>
    app(app(PLUS)(x))(x))

  def answer[F[_] : Sym] = app(main)(lit(21))

  println(s"${answer(ShowSym)} = ${answer(RunSym)}")

}

// (x => +(x)(x))(21) = 42
xacid: (Default)
object Tagless extends App {

  trait Sym[F[_]] {
    def lit[A](a: A): F[A]
    def app[A, B](f: F[A => B])(a: F[A]): F[B]
    def lam[A, B](f: F[A] => F[B]): F[A => B]
  }

  object Sym {
    def apply[F[_] : Sym] = implicitly[Sym[F]]
    def lit[F[_] : Sym, A](a: A): F[A] = Sym[F].lit(a)
    def app[F[_] : Sym, A, B](f: F[A => B])(a: F[A]): F[B] = 
      Sym[F].app(f)(a)
    def lam[F[_] : Sym, A, B](f: F[A] => F[B]): F[A => B] = 
      Sym[F].lam(f)
  }

  type RunSym[A] = A
  object RunSym {
    implicit object runSym extends Sym[RunSym] {
      override def lit[A](a: A): RunSym[A] = a
      override def app[A, B](f: RunSym[(A) => B])
         (a: RunSym[A]): RunSym[B] = f(a)
      override def lam[A, B](f: (RunSym[A]) => 
          RunSym[B]): RunSym[(A) => B] = f
    }
  }

  import Sym._

  def PLUS(x: Int)(y: Int) = x + y

  def answer[F[_] : Sym] = lam((x: F[Int]) =>
    app(app(lit(PLUS _))(x))(x))

  import RunSym._

  println(answer[RunSym].apply(21))

}
xacid: (Default)
  import cats._
  import cats.implicits._
  import scala.concurrent._
  import scala.concurrent.duration.Duration._
  import scala.concurrent.ExecutionContext.Implicits.global

  trait Lam[F[_]] {
    def lit[A](a: A): F[A]
    def op[A, B](f: A => B): F[A => B]
    def app[A, B](f: F[A => B])(a: F[A]): F[B]
  }

  object Lam {
    def apply[F[_] : Lam] = implicitly[Lam[F]]
    def lit[F[_] : Lam, A](a: A): F[A] = Lam[F].lit(a)
    def op[F[_] : Lam, A, B](f: A => B): F[A => B] =
      Lam[F].op(f)

    def app[F[_] : Lam, A, B](f: F[A => B])
                             (a: F[A]): F[B] =
      Lam[F].app(f)(a)

    def lam[F[_] : Lam, A, B](f: F[A] => F[B]) = f
  }

  implicit def applam[F[_] : Applicative]: Lam[F] =
    new Lam[F] {
      def lit[A](a: A): F[A] =
        Applicative[F].pure(a)

      def op[A, B](f: A => B): F[(A) => B] =
        Applicative[F].pure(f)

      def app[A, B](f: F[A => B])(a: F[A]): F[B] =
        Applicative[F].ap(f)(a)
    }

  val PLUS = ((_: Int) + (_: Int)).curried

  import Lam._

  def answer[F[_] : Lam] = lam((x: F[Int]) =>
    app(app(op(PLUS))(x))(x))

  def main[F[_] : Lam : Functor] =
    answer[F].andThen(_.map(println))

  main[Id].apply(21)

  Await.ready(main[Future].apply(Future(21)), Inf)

SK lambda

Nov. 23rd, 2016 02:34 am
xacid: (Default)

import cats._
import cats.data._
import cats.implicits._

import scala.concurrent._
import scala.concurrent.duration.Duration._
import ExecutionContext.Implicits.global

object Sk extends App {
  val PLUS = ((_: Int) + (_: Int)).curried

  trait Lam_[->>[_, _]] {
    def app[E, A, B]: E ->> (A => B) => E ->> A => E ->> B

    def fun[A, B]: (A => B) => A ->> (A => B)

    def lam[A, B]: (A ->> A => A ->> B) => A ->> B
  }

  trait Lam[F[_]] extends Lam_[Kleisli[F, ?, ?]]

  implicit def lama[F[_] : Applicative]: Lam[F] =
    new Lam[F] {
      def AP[E] = Applicative[Kleisli[F, E, ?]]

      def app[E, A, B] = AP[E].ap[A, B]

      def fun[A, B] = AP[A].pure[A => B]

      def lam[A, B] = _ (Kleisli.ask[F, A])
    }

  object Lam {
    def apply[F[_] : Lam] = implicitly[Lam[F]]

    def app[F[_] : Lam, E, A, B](f: Kleisli[F, E, (A => B)])
                                (a: Kleisli[F, E, A]) =
      Lam[F].app(f)(a)

    def fun[F[_] : Lam, A, B](f: A => B) = Lam[F].fun(f)

    def lam[F[_] : Lam, A, B](f: Kleisli[F, A, A] =>
      Kleisli[F, A, B]) = Lam[F].lam(f)
  }

  import Lam._

  def answer[F[_] : Lam]: Kleisli[F, Int, Int] =
    lam(x => app(app(fun(PLUS))(x))(x))

  def main[F[_] : Lam : Functor] = answer[F].map(println)

  answer[Id].apply(21)

  Await.ready(main[Future].apply(21), Inf)

}

xacid: (Default)
  import cats._

  trait Sym[F[_]] {
    def lit[A]: A => F[A] // pure

    def app[A, B]: F[A => B] => F[A] => F[B] // ap

    def lam[A, B]: (F[A] => F[B]) => F[A => B] // coap?
  }

  implicit def symm[F[_] : Bimonad]: Sym[F] =
    new Sym[F] {

      val M = Bimonad[F]

      def lit[A] = M.pure[A]

      def app[A, B] = M.ap[A, B]

      def lam[A, B] = f =>
        M.pure[A => B](a =>
          M.extract(f(M.pure(a))))
    }

  val PLUS = ((_: Int) + (_: Int)).curried

  def answer[F[_] : Sym]: F[Int => Int] = {
    val sym = implicitly[Sym[F]]
    import sym._
    lam(x => app(app(lit(PLUS))(x))(x))
  }

SK arrows

Nov. 19th, 2016 09:38 pm
xacid: (Default)
import cats._
import cats.data._
import cats.arrow._
import cats.implicits._

import scala.concurrent._
import scala.concurrent.duration.Duration._
import ExecutionContext.Implicits.global

object Sk extends App {
  val PLUS = ((_: Int) + (_: Int)).curried

  trait SK_[->>[_, _]] {
    def S[E, A, B]: E ->> (A => B) =>
      E ->> A => E ->> B

    def K[E, A]: (E => A) => E ->> (E => A)

    def I[A]: A ->> A
  }

  object SK_ {
    def apply[->>[_, _] : SK_] = implicitly[SK_[->>]]

    def S[->>[_, _] : SK_, E, A, B](f: E ->> (A => B))
                                   (a: E ->> A) =
      SK_[->>].S(f)(a)

    def K[->>[_, _] : SK_, E, A](f: E => A) = SK_[->>].K(f)

    def I[->>[_, _] : SK_, A] = SK_[->>].I[A]
  }

  implicit def sk_[->>[_, _] : Arrow]: SK_[->>] =
    new SK_[->>] {
      val arrow = Arrow[->>]

      def S[E, A, B] = f => a =>
        arrow.lift((e: E) => (e, e)) >>>
          (f.first >>> a.second) >>>
          arrow.lift(fa => fa._1(fa._2))

      def K[E, A] = a =>
        arrow.lift((_: E) => a)

      def I[A] = arrow.id[A]
    }

  trait SK[F[_]] extends SK_[Kleisli[F, ?, ?]]

  object SK {
    def apply[F[_] : SK] = implicitly[SK[F]]

    def S[F[_] : SK, E, A, B](f: Kleisli[F, E, (A => B)])
                             (a: Kleisli[F, E, A]) =
      SK[F].S(f)(a)

    def K[F[_] : SK, E, A](f: E => A) = SK[F].K(f)

    def I[F[_] : SK, A] = SK[F].I[A]
  }

  implicit def ska[F[_] : Applicative]: SK[F] =
    new SK[F] {
      def AP[E] = Applicative[Kleisli[F, E, ?]]

      def S[E, A, B] = AP[E].ap[A, B]

      def K[E, A] = AP[E].pure[E => A]

      def I[A] = Kleisli.ask[F, A]
    }

  import SK._

  def answer[F[_] : SK] = S(S(K(PLUS))(I))(I)

  def main[F[_] : SK : Functor] = answer[F].map(println)

  main[Id].apply(21)

  Await.ready(main[Future].apply(21), Inf)

}

xacid: (Default)
import cats._
import cats.data._
import cats.implicits._

import scala.concurrent._
import scala.concurrent.duration.Duration._
import ExecutionContext.Implicits.global

object Sk extends App {
  val PLUS = ((_: Int) + (_: Int)).curried

  trait SK[F[_]] {
    type ->>[A, B] = Kleisli[F, A, B]

    def S[E, A, B]: E ->> (A => B) =>
      E ->> A => E ->> B

    def K[E, A]: (E => A) => E ->> (E => A)

    def I[A]: A ->> A
  }

  object SK {
    def apply[F[_] : SK] = implicitly[SK[F]]

    def S[F[_] : SK, E, A, B](f: SK[F]# ->>[E, A => B])
                             (a: SK[F]# ->>[E, A]) =
      SK[F].S(f)(a)

    def K[F[_] : SK, E, A](f: E => A) = SK[F].K(f)

    def I[F[_] : SK, A] = SK[F].I[A]
  }

  implicit def ska[F[_] : Applicative]: SK[F] =
    new SK[F] {
      def AP[E] = Applicative[E ->> ?]

      def S[E, A, B] = AP[E].ap[A, B]

      def K[E, A] = AP[E].pure[E => A]

      def I[A] = Kleisli.ask[F, A]
    }

  import SK._

  def answer[F[_] : SK] = S(S(K(PLUS))(I))(I)

  def main[F[_] : SK : Functor] =
    answer[F].map(println)

  main[Id].apply(21)
  
  Await.ready(main[Future].apply(21), Inf)

}

more SK

Nov. 17th, 2016 07:28 pm
xacid: (Default)
import cats._
import cats.data._
import cats.implicits._
import scala.concurrent._
import duration.Duration._
import ExecutionContext.Implicits.global

object Sk extends App {
  val PLUS = ((_: Int) + (_: Int)).curried

  trait SK[F[_]] {
    def S[E, A, B]: Kleisli[F, E, A => B] =>
      Kleisli[F, E, A] => Kleisli[F, E, B]

    def K[E, A]: (E => A) => Kleisli[F, E, E => A]

    def I[A]: Kleisli[F, A, A]
  }

  object SK {
    def apply[F[_] : SK] = implicitly[SK[F]]
  }

  implicit def ska[F[_] : Applicative]: SK[F] =
    new SK[F] {
      def AP[E] = Applicative[Kleisli[F, E, ?]]

      def S[E, A, B] = AP[E].ap[A, B]

      def K[E, A] = AP[E].pure[E => A]

      def I[A] = Kleisli.ask[F, A]
    }

  def answer[F[_] : SK] = {
    val sk = SK[F]
    import sk._
    S(S(K(PLUS))(I))(I)
  }

  def main[F[_] : SK : Functor] =
    answer[F].map(println)

  main[Id].apply(21)
  Await.ready(main[Future].apply(21), Inf)

}

Typed SK

Nov. 17th, 2016 01:48 am
xacid: (Default)
object Sk extends App {
  def S[X, Y, Z] = (x: Z => Y => X) =>
    (y: Z => Y) => (z: Z) => x(z)(y(z))

  def K[X, Y] = (x: Y => X) => (y: Y) => x

  def I[X] = (x: X) => x

  val PLUS = ((_: Int) + (_: Int)).curried

  val main = S(S(K(PLUS))(I))(I)

  println(main(21))
}

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:41 am
Powered by Dreamwidth Studios