1 of 12

Embedded Domain Specific Languages (eDSL) in Scala

An informal uninformed introduction

2 of 12

Today's menu

Domain Specific Languages

    Uses

External DSLs

    Scala support (parser combinators)

Internal/embedded DSLs

    Sample(s)

    Scala support

    Let's try yo build one?

    

3 of 12

Domain Specific Languages

What are they?

 "A Domain-Specific Language is a programming language that mimics the terms, idioms, and expressions used among experts in the targeted domain" Programming Scala - A. Payne, D. Wampler

 

Why?

  • communication
  • modeling
  • efficiency/quality

Two types

  •     external
  •     internal/embedded

4 of 12

Domain Specific Languages

Advantages

  • High level of abstraction
  • Self-documenting code
  • Validation with domain

Disadvantages

  • Cost of learning vs applicability
  • Have to build/maintain it
  • Proliferation! 

 

5 of 12

External DSL

  • More flexible
  • In Scala: parser combinators library
    • Still you have to write the rest
  • Example: JSON parsing (Programming in Scala)
  • Express the syntax of the language close to EBNF

  val ID = """[a-zA-Z]([a-zA-Z0-9]|_[a-zA-Z0-9])*"""r  val NUM = """[1-9][0-9]*"""r  def program = clazz*  def classPrefix = "class" ~ ID ~ "(" ~ formals ~ ")"  def classExt = "extends" ~ ID ~ "(" ~ actuals ~ ")"  def clazz = classPrefix ~ opt(classExt) ~ "{" ~ (member*) ~ "}"  def formals = repsep(ID ~ ":" ~ ID, ",")  def actuals = expr*  def member = (      "val" ~ ID ~ ":" ~ ID ~ "=" ~ expr    | "var" ~ ID ~ ":" ~ ID ~ "=" ~ expr    | "def" ~ ID ~ "(" ~ formals ~ ")" ~ ":" ~ ID ~ "=" ~ expr    | "def" ~ ID ~ ":" ~ ID ~ "=" ~ expr    | "type" ~ ID ~ "=" ~ ID  )

6 of 12

embedded DSLs

  • Uses the language as a host
    • Constrained by it
  • Simpler to create
  • Closely related to Fluent interfaces and DDD
  • "Just a glorified API"
  • The IDE can help you 
    • syntax highlighting
    • code completion

7 of 12

Some (Scala) DSL examples

Tools (embedded DSL)

  • SBT
  • Specs (BDD framework)

 

EDF Trading (external DSL)

  • DSL for derivatives trading and pricing

 

 

8 of 12

Some (Scala) eDSL examples

def expr(a:Boolean,b:Boolean,c:Boolean,d:Boolean)=�        ( (a Λ b) V ¬(a Λ c) ) Λ ( (c V d) Λ ¬(b) )

...

val payrollCalculator = rules { employee =>

employee salary_for 2.weeks minus_deductions_for { gross =>

federalIncomeTax is (25. percent_of gross)stateIncomeTax is (5. percent_of gross)insurancePremiums are (500. in gross.currency)retirementFundContributions are (10. percent_of gross)

}

}

...�  // use premium pricing strategy for order  new Order to buy(100 sharesOf "IBM")            maxUnitPrice 300            using premiumPricing,

9 of 12

embedded DSLs: Support in Scala

Scala building blocks for embedded DSLs:

  • Implicit conversions
  • Optional dots, semi-colons and parentheses
  • Higher order functions
  • Symbols as methods
  • Currying
  • Implicit parameters

(and more)

    (... thinking about it... most of Scala  )

10 of 12

Interesting links

http://debasishg.blogspot.com/2008/05/designing-internal-dsls-in-scala.html

 (he's working on a book all bout DSLs)

http://programming-scala.labs.oreilly.com/ch11.html

11 of 12

Show me the code!!

 

12 of 12

 

Let's keep in touch!

    Twitter: @gclaramunt

    Blog: http://gabrielsw.blogspot.com