Announcing Dsl.scala, a simple framework for creating embedded DSL in Scala control flow


#1

Happy Women’s Day everyone,

I just release Dsl.scala 1.0.0-RC2, a simple framework for creating embedded Domain-Specific Languages in Scala control flow.

A DSL author is able to create language keywords by implementing the Dsl trait, which contains only one simple function. No knowledge about Scala compiler or AST macros is required.

DSLs written in Dsl.scala are collaborative with others DSLs and Scala control flows. A DSL user can create functions that contains interleaved DSLs implemented by different vendors, along with ordinary Scala control flows.

We also provide some built-in keywords, including:

  • The Shift keyword for asynchronous programming, similar to await / async in C#, Python and JavaScript.
  • The Yield keyword for generating lazy streams, similar to yield in C#, Python and JavaScript.
  • The Fork keyword for duplicating current thread, similar to the fork system call in POSIX.
  • The AutoClose keyword to automatically close resources when exiting a scope, similar to the destructor feature in C++.
  • The Monadic keyword for creating Scalaz or Cats monadic control flow, similar to the !-notation in Idris.

All the above keywords can be used together with each others.

Getting Started

Suppose you want to create a random number generator. The generated numbers should be stored in a lazily evaluated infinite stream, which can be built with the help of our built-in domain-specific keyword Yield.

So, you need to add the library that contains the implementation of the keyword Yield:

// Add the following setting in your build.sbt 
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-yield" % "1.0.0-RC2"

And the Dsl.scala compiler plug-ins that are shared by all DSLs:

// Add the following settings in your build.sbt 
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-bangnotation" % "1.0.0-RC2")
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-reseteverywhere" % "1.0.0-RC2")

See MVNRepository or Scaladex for the settings of other built-in DSLs for your build tools.

The random number generator which can be implemented as a recursive function that produce the next random number in each iteration.

import com.thoughtworks.dsl.instructions.Yield
def xorshiftRandomGenerator(seed: Int): Stream[Int] = {
  val tmp1 = seed ^ (seed << 13)
  val tmp2 = tmp1 ^ (tmp1 >>> 17)
  val tmp3 = tmp2 ^ (tmp2 << 5)
  !Yield(tmp3)
  xorshiftRandomGenerator(tmp3)
}

Note that a keyword is a simply case class. You need a ! prefix to the keyword to activate the DSL.

It’s done. We can test it in ScalaTest:

val myGenerator = xorshiftRandomGenerator(seed = 123)
myGenerator(0) should be(31682556)
myGenerator(1) should be(-276305998)
myGenerator(2) should be(2101636938)

The call to xorshiftRandomGenerator does not throw a StackOverflowError because the execution of xorshiftRandomGenerator will be paused at the keyword Yield, and it will be resumed when the caller is looking for the next number.

Our DSLs are efficient. I have not compared it with scala.concurrent.Future or other implementation of asynchronous tasks, but a rough benchmark shows our DSL for asynchronous task performs 0.9x ~ 2x better than Monix’s Task. I will publish the complete report of benchmark in couple of days. Check the Scaladoc to find examples about using or creating DSLs.


#2

The complete benchmark report can be found at Benchmarks: Dsl.scala vs Monix vs Cats Effect vs Scalaz Concurrent vs Scala Async vs Scala Continuation
.

When using a direct style DSL, our !-bang notation is the fastest implementation among for-comprehension, Scala Async, and Scala Continuation. Especially, when performing a complex task to manipulate collections, our !-notation can be 12.5 times faster than for comprehension when running in current thread, and more than 3.1 times faster when running in a thread pool, despite conciser syntax of we provided.