?

Log in

No account? Create an account

_xacid_

Recent Entries

_xacid_

View

Navigation

February 25th, 2019

Continuation recursion

Share

  import IxCont._

  println(unfold(10)(_ + 1).take(10).toList)

  {
    def nat(n: Int) = loop(for {
      x <- take[Int, Stream[Int]]
      _ <- put(x)
    } yield x + 1)(n)

    println(nat(10).take(10).toList)
  }

  {
    def nat(n: Int) = loop(for {
      x <- channel[Int, Int]
      _ = println("x:" + x)
    } yield x + 1)(n) toStream

    println(nat(10).take(10).toList)
  }

  {
    def f = loop(for {
      x <- take[Int, String @@ Int]
      _ = println("x = " + x)
      y <- suspend[Int, String](s"[$x]")
      _ = println("y = " + y)
    } yield x + y)

    println(f(1)(2)(3)(4))
  }

Read more...Collapse )

February 18th, 2019

import cats._
import cats.implicits._
import scala.annotation.tailrec
import scala.util.control.TailCalls._

object IxCont {
  type ~[A] = TailRec[A]
  type =>>[A, B] = A => ~[B]

  case class Cont[R1, R2, A](cont: ~[(A =>> R2) =>> R1]) extends AnyVal {
    def map[B](f: A => B): Cont[R1, R2, B] = ContIxMonad.map(this)(f)
    def flatMap[R3, B](f: A => Cont[R2, R3, B]): Cont[R1, R3, B] = ContIxMonad.bind(this)(f)
  }

  def ret[S, A](a: A): Cont[S, S, A] = Cont(done(_ (a)))
  def reset[R, A](c: Cont[R, A, A]): R = c.cont.flatMap(_ (done)).result
  def shift[R, B, A](f: (A =>> B) =>> R): Cont[R, B, A] = Cont(done(f(_)))
  def suspend[A, B]: Cont[A =>> B, B, A] = shift(done[A =>> B])
Read more...Collapse )

February 17th, 2019

Continuation Indexed Monad

Share
object IxCont {
  type Cont[R, B, A] = (A => B) => R
  def reset[R, A](c: Cont[R, A, A]): R = c(identity)
  def shift[R, B, A](f: (A => B) => R): Cont[R, B, A] = f(_)
  def suspend[A, B]: Cont[A => B, B, A] = shift(identity[A => B])
Read more...Collapse )

February 16th, 2019

https://www.semanticscholar.org/paper/The-Theory-and-Practice-of-Programming-Languages-Biernacki/bf91e6b4a257faf9a028cef32d2e00364a846ea4

The Theory and Practice of Programming Languages with Delimited Continuations

Dariusz Biernacki
Published 2007

This dissertation presents a study of functional programming languages with first-class delimited continuations. We focus mainly on theoretical and practical aspects of Danvy and Filinski’s hierarchy of static delimited-control operators shiftn and resetn, and of Felleisen’s dynamic delimited-control operators control and prompt. Our study uses the traditional means of specifying semantics of functional languages: higher-order interpreters and abstract machines. We therefore first investigate the essence of interpreters and abstract machines, and we present a systematic and constructive derivation method connecting them. Read more...Collapse )

February 6th, 2019

https://pdfs.semanticscholar.org/3d22/31608c7ba19935c610afd60f13bbe89d6b55.pdf

Monads and composable continuations
PHILIP WADLER

Moggi’s use of monads to factor semantics is used to model the composable continuations of Danvy and Filinski. This yields some insights into the type systems proposed by Murthy and by Danvy and Filinski. Interestingly, modelling some aspects of composable continuations requires a structure that is almost, but not quite, a monad.

February 2nd, 2019

The stack calculus

Share
http://www.dsi.unive.it/~acarraro/CES12.pdf
The stack calculus
We introduce a functional calculus with simple syntax and operational semantics in which the calculi introduced so far in the Curry–Howard correspondence for Classical Logic can be faithfully encoded.Our calculus enjoys confluence without any restriction. Its type system enforces strong normalization of expressions and it is a sound and complete system for full implicational Classical Logic. We give a very simple denotational semantics which allows easy calculations of the interpretation of expressions.

February 1st, 2019

import cats._
import cats.implicits._
import scala.annotation.tailrec
import scala.util.control.TailCalls._

object Cont {
  type ^[A] = TailRec[A]
  type =>>[A, B] = A => ^[B]
  type <<=[R, A] = ^[A =>> R =>> R]

  def ^[R, A](a: A): R <<= A = done(_ (a))
  def shift[R, A](e: A =>> R =>> R): R <<= A = done(e(_))
  def reset[R](k: R <<= R): R = k.flatMap(_ (done)).result

  implicit def monad[R] = new StackSafeMonad[R <<= ?] {
    override final def pure[A](a: A): R <<= A = ^(a)
    override final def flatMap[A, B](fa: R <<= A)(f: A => R <<= B): R <<= B =
      done(k => fa.flatMap(_ (f(_).flatMap(_ (k)))))
  }
}
Read more...Collapse )
Powered by LiveJournal.com