ScalaDSL

trait ScalaDSL extends TimerDSL with CrashDSL

Extension of CoreDSL, TimerDSL and CrashDSL that adds support for manipulating Scala values via pure Scala functions.

trait CrashDSL
trait TimerDSL
trait CoreDSL
class Object
trait Matchable
class Any

Type members

Types

type Neg[A]

Demand for a Scala value of type A.

Somewhat analogous to scala.concurrent.Promise

type Res[A]

Resource that is a Scala object of type A. Unlike Val, a resource can be mutable, cannot in general be neglected or duplicated, and is automatically cleaned-up in case of crash.

It is recommended to define custom opaque type aliases of resources, such as

opaque type Input = Res[java.io.InputStream]
type Val[A]

Scala value of type A.

Somewhat analogous to scala.concurrent.Future.

Inherited types

type -⚬[A, B]

Libretto arrow, also called a ''component'' or a ''linear function''.

┏━━━━━━━━━━┓
┞───┐      ┞───┐
╎ A │      ╎ B │
┟───┘      ┟───┘
┗━━━━━━━━━━┛

In A -⚬ B, we say that the ''in-port'' is of type A and the ''out-port'' is of type B. Note that the distinction between the in-port and the out-port is only formal. Information or resources may flow in and out through both the in-port and the out-port.

"Linear" means that each input is ''consumed'' exactly once, in particular, it cannot be ignored or used twice.

Inherited from
CoreDSL
type Done

Signal that travels in the direction of -⚬, i.e. the positive direction.

Inherited from
CoreDSL
type LTerminus

A black hole that can absorb (i.e. take over the responsibility to await) Need signals, but from which there is no escape.

Inherited from
CoreDSL
type Need

Signal that travels in the direction opposite to -⚬, i.e. the negative direction.

Inherited from
CoreDSL
type One

No resource. It is the identity element for |*|. There is no flow of information through a One-typed port.

Inherited from
CoreDSL
type RTerminus

A black hole that can absorb (i.e. take over the responsibility to await) Done signals, but from which there is no escape.

Inherited from
CoreDSL
type Rec[F[_]]
Inherited from
CoreDSL
type Zero

Impossible resource. Analogous to Nothing. It is the identity element for |+|.

Inherited from
CoreDSL
type |&|[A, B]

Choice between A and B. The consumer chooses whether to get A or B (but can get only one of them). The producer has to be ready to provide either of them.

Inherited from
CoreDSL
type |*|[A, B]

Concurrent pair. Also called a ''tensor product'' or simply ''times''.

Inherited from
CoreDSL
type |+|[A, B]

Either A or B. Analogous to scala.Either. Whether it is going to be A or B is decided by the producer. The consumer has to be ready to handle either of the two cases.

Inherited from
CoreDSL
type [A, B] = A |+| B

Alias for |+|.

Inherited from
CoreDSL
type [A, B] = A |*| B

Alias for |*|.

Inherited from
CoreDSL

Value members

Methods

def acquire[A, R, B](acquire: A => (R, B), release: Option[R => Unit]): Val[A] -⚬ Res[R] |*| Val[B]

Acquires a resource of type R.

Type Params
A

parameters of the acquire function

B

additional data produced by acquiring the resource

R

type of the resource

Value Params
release

called to release the resource in case of a crash. None means no cleanup is needed

def acquireAsync[A, R, B](acquire: A => Async[(R, B)], release: Option[R => Async[Unit]]): Val[A] -⚬ Res[R] |*| Val[B]
def blocking[A, B](f: A => B): Val[A] -⚬ Val[B]

Executes a potentially blocking operation. The implementation must ensure that the blocking operation does not impede any of the concurrently happening non-blocking computations.

def constNeg[A](a: A): Neg[A] -⚬ Need
def constVal[A](a: A): Done -⚬ Val[A]
def contramapNeg[A, B](f: A => B): Neg[B] -⚬ Neg[A]

Lifts an ordinary Scala function to a linear function on demands, in opposite direction.

def delay: Val[FiniteDuration] -⚬ Done
override def delay(d: FiniteDuration): Done -⚬ Done
Definition Classes
def delayNeed: Need -⚬ Neg[FiniteDuration]
def dup[A]: Val[A] -⚬ Val[A] |*| Val[A]
def dupNeg[A]: Neg[A] |*| Neg[A] -⚬ Neg[A]
def effect[R, A, B](f: (R, A) => B): Res[R] |*| Val[A] -⚬ Res[R] |*| Val[B]

Performs a (potentially) effectful operation on a resource, producing some output.

Type Params
A

additional parameter of the operation

B

additional output of the operation

R

type of the resource

Value Params
f

the effectful operation

def effectAsync[R, A, B](f: (R, A) => Async[B]): Res[R] |*| Val[A] -⚬ Res[R] |*| Val[B]
def effectWr[R, A](f: (R, A) => Unit): Res[R] |*| Val[A] -⚬ Res[R]

Variant of effect that does not produce output in addition to performing the effect. Can be viewed as ''wr''iting an A into the resource.

def effectWrAsync[R, A](f: (R, A) => Async[Unit]): Res[R] |*| Val[A] -⚬ Res[R]
def fulfill[A]: Val[A] |*| Neg[A] -⚬ One

Uses the value (eventually) produced by Val to satisfy the demand of Neg.

def inflate[A]: Need -⚬ Neg[A]
def liftEither[A, B]: Val[Either[A, B]] -⚬ Val[A] |+| Val[B]
def liftNegPair[A, B]: Neg[(A, B)] -⚬ Neg[A] |*| Neg[B]
def liftPair[A, B]: Val[(A, B)] -⚬ Val[A] |*| Val[B]
def mapVal[A, B](f: A => B): Val[A] -⚬ Val[B]

Lifts an ordinary Scala function to a linear function on Vals.

def neglect[A]: Val[A] -⚬ Done
def promise[A]: One -⚬ Neg[A] |*| Val[A]

Creates an entangled pair of demand (Neg) and supply (Val) such that when the demand is fulfilled with a value, that value will be produced by the supply.

def release[R]: Res[R] -⚬ Done

Releases a resource using the release function registered during resource acquisition.

def release[R, A, B](f: (R, A) => B): Res[R] |*| Val[A] -⚬ Val[B]

Releases a resource using the given function. The release function previously registered during resource acquisition is not used.

Type Params
A

additional parameter of the release function

B

additional data produced by the release function

R

type of the resource

Value Params
f

the release function

def releaseAsync[R, A, B](f: (R, A) => Async[B]): Res[R] |*| Val[A] -⚬ Val[B]
def splitResource[R, A, S, T, B](f: (R, A) => (S, T, B), release1: Option[S => Unit], release2: Option[T => Unit]): Res[R] |*| Val[A] -⚬ Res[S] |*| Res[T] |*| Val[B]
def splitResourceAsync[R, A, S, T, B](f: (R, A) => Async[(S, T, B)], release1: Option[S => Async[Unit]], release2: Option[T => Async[Unit]]): Res[R] |*| Val[A] -⚬ Res[S] |*| Res[T] |*| Val[B]
def transformResource[R, A, S, B](f: (R, A) => (S, B), release: Option[S => Unit]): Res[R] |*| Val[A] -⚬ Res[S] |*| Val[B]

Transforms a resource into a resource of (possibly) different type.

Type Params
A

additional parameter of the transformation

B

additional output of the transformation

R

type of the input resource

S

type of the output resource

Value Params
f

the transformation function. It receives the input resource and additional input of type A. It returns the new resource and additional output of type B.

release

called to release the new resource in case of a crash. None means no cleanup is needed

def transformResourceAsync[R, A, S, B](f: (R, A) => Async[(S, B)], release: Option[S => Async[Unit]]): Res[R] |*| Val[A] -⚬ Res[S] |*| Val[B]
def tryAcquire[A, R, B, E](acquire: A => Either[E, (R, B)], release: Option[R => Unit]): Val[A] -⚬ Val[E] |+| Res[R] |*| Val[B]

Acquires a resource of type R. Might fail with an error of type E.

Type Params
A

parameters of the acquire function

B

additional data produced by acquiring the resource

E

type of the error

R

type of the resource

Value Params
release

called to release the resource in case of a crash. None means no cleanup is needed

def tryAcquireAsync[A, R, B, E](acquire: A => Async[Either[E, (R, B)]], release: Option[R => Async[Unit]]): Val[A] -⚬ Val[E] |+| Res[R] |*| Val[B]
def trySplitResource[R, A, S, T, B, E](f: (R, A) => Either[E, (S, T, B)], release1: Option[S => Unit], release2: Option[T => Unit]): Res[R] |*| Val[A] -⚬ Val[E] |+| Res[S] |*| Res[T] |*| Val[B]
def trySplitResourceAsync[R, A, S, T, B, E](f: (R, A) => Async[Either[E, (S, T, B)]], release1: Option[S => Async[Unit]], release2: Option[T => Async[Unit]]): Res[R] |*| Val[A] -⚬ Val[E] |+| Res[S] |*| Res[T] |*| Val[B]
def tryTransformResource[R, A, S, B, E](f: (R, A) => Either[E, (S, B)], release: Option[S => Unit]): Res[R] |*| Val[A] -⚬ Val[E] |+| Res[S] |*| Val[B]

Transforms a resource into a resource of (possibly) different type. Might fail with an error of type E.

Type Params
A

additional parameter of the transformation

B

additional output of the transformation

E

type of the error

R

type of the input resource

S

type of the output resource

Value Params
f

the transformation function. It receives the input resource and additional input of type A. It returns either an error of type E or the new resource and additional output of type B. In case the transformation results in an error, the original resource is ''not'' released automatically— the passing of the original resource R to the transformation function f indicates transfer of responsibility for the resource to the function f.

release

called to release the new resource in case of a crash. None means no cleanup is needed

def tryTransformResourceAsync[R, A, S, B, E](f: (R, A) => Async[Either[E, (S, B)]], release: Option[S => Async[Unit]]): Res[R] |*| Val[A] -⚬ Val[E] |+| Res[S] |*| Val[B]
def unliftEither[A, B]: Val[A] |+| Val[B] -⚬ Val[Either[A, B]]
def unliftNegPair[A, B]: Neg[A] |*| Neg[B] -⚬ Neg[(A, B)]
def unliftPair[A, B]: Val[A] |*| Val[B] -⚬ Val[(A, B)]

Inherited methods

def andThen[A, B, C](f: A -⚬ B, g: B -⚬ C): A -⚬ C
Inherited from
CoreDSL
def assocLR[A, B, C]: A |*| B |*| C -⚬ A |*| B |*| C
Inherited from
CoreDSL
def assocRL[A, B, C]: A |*| B |*| C -⚬ A |*| B |*| C
Inherited from
CoreDSL
def choice[A, B, C](f: A -⚬ B, g: A -⚬ C): A -⚬ B |&| C
Inherited from
CoreDSL
def chooseL[A, B]: A |&| B -⚬ A
Inherited from
CoreDSL
def chooseLWhenNeed[A, B]: Need |*| A |&| B -⚬ Need |*| A
Inherited from
CoreDSL
def chooseR[A, B]: A |&| B -⚬ B
Inherited from
CoreDSL
def chooseRWhenNeed[A, B]: A |&| Need |*| B -⚬ Need |*| B
Inherited from
CoreDSL
def coDistributeL[A, B, C]: A |*| B |&| A |*| C -⚬ A |*| B |&| C

Inverse of coFactorL.

Inherited from
CoreDSL
def coDistributeR[A, B, C]: A |*| C |&| B |*| C -⚬ A |&| B |*| C

Inverse of coFactorR.

Inherited from
CoreDSL
def coFactorL[A, B, C]: A |*| B |&| C -⚬ A |*| B |&| A |*| C
Inherited from
CoreDSL
def coFactorR[A, B, C]: A |&| B |*| C -⚬ A |*| C |&| B |*| C
Inherited from
CoreDSL
def cocrash[A, B](msg: String): Need |*| A -⚬ Need |*| B
Inherited from
CrashDSL
def crash[A, B](msg: String): Done |*| A -⚬ Done |*| B

Crashes the program. Use only for irrecoverable errors.

Recoverable errors should be expressed in function signature and handled appropriately.

Done in the input is the trigger to crash. Done in the output is an obligation to propagate the crash. A in the input allows to consume any unhandled resources. B in the output allows to fulfill any obligation to produce resources.

Inherited from
CrashDSL
def crashd(msg: String): Done -⚬ Done
Inherited from
CrashDSL
def crashn(msg: String): Need -⚬ Need
Inherited from
CrashDSL
def delayNeed(d: FiniteDuration): Need -⚬ Need
Inherited from
TimerDSL
def distributeL[A, B, C]: A |*| B |+| C -⚬ A |*| B |+| A |*| C

Distribute the factor on the left into the summands on the right. Inverse of factorL.

Inherited from
CoreDSL
def distributeR[A, B, C]: A |+| B |*| C -⚬ A |*| C |+| B |*| C

Distribute the factor on the right into the summands on the left. Inverse of factorR.

Inherited from
CoreDSL
Inherited from
CoreDSL
def either[A, B, C](f: A -⚬ C, g: B -⚬ C): A |+| B -⚬ C
Inherited from
CoreDSL
def elimFst[A, B](f: A -⚬ One): A |*| B -⚬ B
Inherited from
CoreDSL
def elimFst[B]: One |*| B -⚬ B
Inherited from
CoreDSL
def elimSnd[A, B](f: B -⚬ One): A |*| B -⚬ A
Inherited from
CoreDSL
def elimSnd[A]: A |*| One -⚬ A
Inherited from
CoreDSL
def factorL[A, B, C]: A |*| B |+| A |*| C -⚬ A |*| B |+| C

Factor out the factor A on the left of both summands.

Inherited from
CoreDSL
def factorR[A, B, C]: A |*| C |+| B |*| C -⚬ A |+| B |*| C

Factor out the factor C on the right of both summands.

Inherited from
CoreDSL
def fork[A, B](f: Done -⚬ A, g: Done -⚬ B): Done -⚬ A |*| B
Inherited from
CoreDSL
Inherited from
CoreDSL
def forkNeed[A, B](f: A -⚬ Need, g: B -⚬ Need): A |*| B -⚬ Need
Inherited from
CoreDSL
Inherited from
CoreDSL
def id[A]: A -⚬ A
Inherited from
CoreDSL
def injectL[A, B]: A -⚬ A |+| B
Inherited from
CoreDSL
def injectLWhenDone[A, B]: Done |*| A -⚬ Done |*| A |+| B
Inherited from
CoreDSL
def injectR[A, B]: B -⚬ A |+| B
Inherited from
CoreDSL
def injectRWhenDone[A, B]: Done |*| B -⚬ A |+| Done |*| B
Inherited from
CoreDSL
def introFst[A, X](f: One -⚬ X): A -⚬ X |*| A
Inherited from
CoreDSL
def introFst[B]: B -⚬ One |*| B
Inherited from
CoreDSL
def introSnd[A, X](f: One -⚬ X): A -⚬ A |*| X
Inherited from
CoreDSL
def introSnd[A]: A -⚬ A |*| One
Inherited from
CoreDSL
def join[A, B](f: A -⚬ Done, g: B -⚬ Done): A |*| B -⚬ Done
Inherited from
CoreDSL
Inherited from
CoreDSL
def joinNeed[A, B](f: Need -⚬ A, g: Need -⚬ B): Need -⚬ A |*| B
Inherited from
CoreDSL
Inherited from
CoreDSL

Reverses the Need signal (flowing in the negative direciton, i.e. against the -⚬ arrow) into a Done signal (flowing in the positive direction, i.e. along the -⚬ arrow).

 ┏━━━━━━┓
 ┃      ┞────┐
 ┃   ┌┄┄╎Need│
 ┃   ┆  ┟────┘
 ┃   ┆  ┃
 ┃   ┆  ┞────┐
 ┃   └┄→╎Done│
 ┃      ┟────┘
 ┗━━━━━━┛
Inherited from
CoreDSL
Inherited from
CoreDSL
def pack[F[_]]: F[Rec[F]] -⚬ Rec[F]

Hides one level of a recursive type definition.

Inherited from
CoreDSL
def par[A, B, C, D](f: A -⚬ B, g: C -⚬ D): A |*| C -⚬ B |*| D
Inherited from
CoreDSL

Reverses the Done signal (flowing in the positive direction, i.e. along the -⚬ arrow) into a Need signal (flowing in the negative direciton, i.e. against the -⚬ arrow).

 ┏━━━━━━━━━━━┓
 ┞────┐      ┃
 ╎Done│┄┄┐   ┃
 ┟────┘  ┆   ┃
 ┃       ┆   ┃
 ┞────┐  ┆   ┃
 ╎Need│←┄┘   ┃
 ┟────┘      ┃
 ┗━━━━━━━━━━━┛
Inherited from
CoreDSL

Races the two Done signals and - produces left if the first signal wins, in which case it returns the second signal that still has to be awaited; - produces right if the second signal wins, in which case it returns the first signal that still has to be awaited. It is biased to the left: if both signals have arrived by the time of inquiry, returns left.

Inherited from
CoreDSL
def rec[A, B](f: A -⚬ B => A -⚬ B): A -⚬ B
Inherited from
CoreDSL

Races two Need signals, i.e. signals traveling in the negative direction (i.e. opposite the -⚬ arrow). Based on which Need signal from the out-port wins the race, selects one of the two Need signals from the in-port: - If the first signal from the out-port wins the race, selects the left signal from the in-port and pipes to it the remaining (i.e. the right) signal from the out-port. - If the second signal from the out-port wins the race, selects the right signal from the in-port and pipes to it the reamining (i.e. the left) signal from the out-port. It is biased to the left: if both signals from the out-port have arrived by the time of inquiry, selects the left signal from the in-port.

Inherited from
CoreDSL
def signalChoice[A, B]: Need |*| A |&| B -⚬ A |&| B

Signals (in the negative direction) when it is known which side of the choice (A |&| B) has been chosen.

Inherited from
CoreDSL
def signalEither[A, B]: A |+| B -⚬ Done |*| A |+| B

Signals when it is decided whether A |+| B actually contains the left side or the right side.

Inherited from
CoreDSL
def swap[A, B]: A |*| B -⚬ B |*| A
Inherited from
CoreDSL
def unpack[F[_]]: Rec[F] -⚬ F[Rec[F]]

Unpacks one level of a recursive type definition.

Inherited from
CoreDSL