All Projects → ISCPIF → freedsl

ISCPIF / freedsl

Licence: other
Practical effect composition library based on abstract wrapping type and the free monad

Programming Languages

scala
5932 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to freedsl

operational
Implement monads by specifying instructions and their desired operational semantics.
Stars: ✭ 77 (+108.11%)
Mutual labels:  monad, free-monad
Eff
Eff monad for cats - https://atnos-org.github.io/eff
Stars: ✭ 479 (+1194.59%)
Mutual labels:  effects, monad
sealed-monad
Scala library for nice business logic oriented, for-comprehension-style error handling
Stars: ✭ 16 (-56.76%)
Mutual labels:  monad, monad-transformers
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+1354.05%)
Mutual labels:  effects, monad
Language Ext
C# functional language extensions - a base class library for functional programming
Stars: ✭ 3,964 (+10613.51%)
Mutual labels:  monad, monad-transformers
Functionaljava
Functional programming in Java
Stars: ✭ 1,472 (+3878.38%)
Mutual labels:  effects, monad
Algebraic Effects
Manage side-effects in your javascript application cleanly with algebraic effects
Stars: ✭ 162 (+337.84%)
Mutual labels:  effects
View Effects
Apply custom effects on view backgrounds
Stars: ✭ 176 (+375.68%)
Mutual labels:  effects
Enviews
🌟A cool dynamic view library
Stars: ✭ 1,771 (+4686.49%)
Mutual labels:  effects
Lawvere
A categorical programming language with effects
Stars: ✭ 142 (+283.78%)
Mutual labels:  effects
future.scala
Stack-safe asynchronous programming
Stars: ✭ 38 (+2.7%)
Mutual labels:  monad
Swiftuiimageeffects
Swift port of Apple UIImage+UIImageEffecs category.
Stars: ✭ 213 (+475.68%)
Mutual labels:  effects
Extensible Effects
Extensible Effects: An Alternative to Monad Transformers
Stars: ✭ 167 (+351.35%)
Mutual labels:  effects
Guitarstack
Digital guitar effects right in your browser!
Stars: ✭ 164 (+343.24%)
Mutual labels:  effects
Capability
Extensional capabilities and deriving combinators
Stars: ✭ 176 (+375.68%)
Mutual labels:  effects
Normalmap.js
normalmap.js is a library for creating simple interactive lighting effects using normal maps.
Stars: ✭ 156 (+321.62%)
Mutual labels:  effects
Giftsurfaceview
🌷 直播间送礼物拼图案动画控件
Stars: ✭ 228 (+516.22%)
Mutual labels:  effects
Learnunityshader
学习Unity Shader过程中的一些记录,特效,动画Demo。
Stars: ✭ 141 (+281.08%)
Mutual labels:  effects
Toucheffects
Android View点击特效TouchEffects,几行代码为所有控件添加点击效果
Stars: ✭ 167 (+351.35%)
Mutual labels:  effects
Redis4cats
🔖 Redis client built on top of Cats Effect, Fs2 and Lettuce
Stars: ✭ 189 (+410.81%)
Mutual labels:  effects

FreeDSL

Overview

FreeDSL is a library for pure composition of side effects, weaving typeclasses on an abstract wrapping type and the Free Monad.

Examples

Creating a new DSL

import freedsl.dsl._

object Random {

  def interpreter(random: util.Random) = new Interpreter {
    def nextDouble(implicit context: Context) = random.nextDouble
    def nextInt(n: Int)(implicit context: Context) = random.nextInt(n)
    def shuffle[A](s: Seq[A])(implicit context: Context) = result(random.shuffle(s))
    def fakeError(implicit context: Context) = failure(FakeError("for some reason there was an error"))
  }

  def interpreter(seed: Long): Interpreter = interpreter(new util.Random(seed))
  
  case class FakeError(cause: String) extends Error

}

@dsl trait Random[M[_]] {
  def nextDouble: M[Double]
  def nextInt(n: Int): M[Int]
  def shuffle[A](s: Seq[A]): M[Seq[A]]
  def fakeError: M[Unit]
}

Using your new DSL

Merging interpreters

import cats._
import cats.implicits._
import freedsl._
import freedsl.random._
import freedsl.util._
import freedsl.log._

import squants.time.TimeConversions._

// Pure functions that describe computations depending on side effects
def randomData[M[_]](implicit randomM: Random[M]): M[Seq[Int]] =
  randomM.shuffle(Seq(1, 2, 2, 3, 3, 3))

def randomSleep[M[_]: Monad](implicit randomM: Random[M], utilM: Util[M], logM: Log[M]): M[Unit] = for {
  s <- randomM.nextInt(10)
  _ <- logM.print(s"Sleeping for $s seconds")
  _ <- utilM.sleep(s seconds)
} yield ()


// Construct the interpreter and a type M along with implicit instances of Random[M], Util[M] and Log[M]
// they are build using the free monad and the freek library

val interpreter = merge(Util.interpreter, Random.interpreter(42), Log.interpreter)
import interpreter.implicits._

def prg =
  for {
    b  randomData[interpreter.M]
    _  randomSleep[interpreter.M]
  } yield b
  

// All the side effects take place here in the interpreter
interpreter.run(prg) match {
  case Right(v) => println(s"This is a success: $v")
  case Left(e) => println(s"OhOh, error: $e")
}

Merging DSLs

Types can be merged independently from intepreters. For instance:

val merged = merge(Util, Random, Log)
import merged.implicits._

def prg =
  for {
    b  randomData[merged.M]
    _  randomSleep[merged.M]
  } yield b

  
val interpreter = merge(Util.interpreter, Random.interpreter(42), Log.interpreter)

// All the side effects take place here in the interpreter
interpreter.run(prg) match {
  case Right(v) => println(s"This is a success: $v")
  case Left(e) => println(s"OhOh, error: $e")
}

Mutli-level merging

DSLs and interpreters can be merged at multiple scala. For instance:

val partialMerge = merge(Util, Random)
val merged = merge(partialMerge, Log)
import merged.implicits._

def prg =
  for {
    b  randomData[merged.M]
    _  randomSleep[merged.M]
  } yield b

  
val interpreter = merge(Util.interpreter, Random.interpreter(42), Log.interpreter)

// All the side effects take place here in the interpreter
interpreter.run(prg) match {
  case Right(v) => println(s"This is a success: $v")
  case Left(e) => println(s"OhOh, error: $e")
}

And for interpreters:

// Construct the interpreter and a type M along with implicit instances of Random[M], Util[M] and Log[M]
// they are build using the free monad and the freek library

val partialMerge = merge(Util.interpreter, Random.interpreter(42))

// In case several interpreters are provided for a given DSL, the first one in the list is retained
val interpreter = merge(partialMerge, Log.interpreter)
import interpreter.implicits._

def prg =
  for {
    b  randomData[interpreter.M]
    _  randomSleep[interpreter.M]
  } yield b
  

// All the side effects take place here in the interpreter
interpreter.run(prg) match {
  case Right(v) => println(s"This is a success: $v")
  case Left(e) => println(s"OhOh, error: $e")
}

Wrapping errors

To return meaningful errors, it is sometimes necessary to wrap errors produced by side effects:

  object DSLTest1M {
    def interpreter = new Interpreter {
      def get(implicit context: Context) = failure(DSL1Error("Boooh"))
    }

    case class DSL1Error(s: String) extends Error
  }

  @dsl trait DSLTest1M[M[_]] {
    def get: M[String]
  }

  case class WrapError(e: Error) extends Error

  def prg[M[_]](implicit dSLTest1M: DSLTest1M[M], errorWrapping: ErrorWrapping[M]) = errorWrapping.wrap(e => WrapError(e)) {
    dSLTest1M.get
  }

Getting FreeDSL

FreeDSL is published to sonatype.

FreeDSL supports Scala 2.11 and 2.12. If you use SBT, you can include FreeDSL via the following build.sbt snippets:

// Repository for Freek
resolvers += Resolver.bintrayRepo("projectseptemberinc", "maven")

// For scala 2.12, for 2.11 use Miles Sabin's plugin for type unification.
scalacOptions := Seq("-Ypartial-unification")

def freedslVersion = "0.9-SNAPSHOT"

// pick a particular subproject
libraryDependencies += "fr.iscpif.freedsl" %% "util" % freedslVersion,
libraryDependencies += "fr.iscpif.freedsl" %% "random" % freedslVersion % "test"

Detailed Information

TODO

Known Issues

DSLs don't suport default parameter value. It raises an error at runtime, such as:

java.lang.AbstractMethodError: freedsl.dsl.DSLTest$InterpretationContext$1$MergedDSL$1$$anon$12$$anon$10.defaultValue$default$2()Ljava/lang/String;

Future Work

TODO

Copyright and License

All code is available to you under the LGPL license, available at https://www.gnu.org/licenses/lgpl.html and also in the COPYING file.

Copyright Romain Reuillon, 2016

No Warranty

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].