Embedded domain specific language
Embedded Domain Specific Language means that you embed a Domain specific language in a language like Haskell. E.g. using the Functional MetaPost library you can write Haskell expressions, which are then translated to MetaPost, MetaPost is run on the generated code and the result of MetaPost can be post-processed in Haskell.
1 Degree of embedding
There are two major degrees of embedding:
- Shallow embedding: All Haskell operations immediately translate to the target language. E.g. the Haskell expression is translated to aa+blikeStringcontaining that target language expression."a + b"
- Deep embedding: Haskell operations only build an interim Haskell data structure that reflects the expression tree. E.g. the Haskell expression is translated to the Haskell data structurea+b. This structure allows transformations like optimizations before translating to the target language.Add (Var "a") (Var "b")
2 Discussion of common problems
- Oleg in Haskell-Cafe about Designing DSL with explicit sharing (was: I love purity, but it's killing me)
- Koen Classen: Observable Sharing for Functional Circuit Description
- Andy Gill: Type-Safe Observable Sharing
- Tom Lokhorst AwesomePrelude presentation (video)
- Leandro Lisboa Penz Haskell eDSL Tutorial - Shared expenses
- Bruno Oliveira and William Cook: Functional Programming with Structured Graphs
- Emil Axelsson and Koen Claessen: Using Circular Programs for Higher-Order Syntax
- Oleg Kiselyov: Implementing explicit and finding implicit sharing in embedded DSLs
3 Examples of Domain Specific Languages
- Functional MetaPost (funcmp) is a Haskell frontend to the MetaPost language by John Hobby. Users write their graphics as Haskell programs, which then emit MetaPost code that can be compiled into encapsulated PostScript files and smoothly included into e.g. LaTeX.
- orc: Orchestration-style coordination EDSL
- synthesizer-llvm provides audio signal processing functions that are translated to LLVM assembly language. This warrants very fast computation. It uses shallow embedding.
- The diagrams-cairo package. A full-featured backend for rendering diagrams using the cairo rendering engine. To get started, see Diagrams.Backend.Cairo.CmdLine.
- Workflow: library for transparent execution of interruptible computations
4 More to read/view
- DSL for the Uninitiated, an introduction to DSLs, not Haskell-specific
- Haskell for embedded domain-specific languages (podcast)
- Research papers/Domain specific languages
- Practical Haskell: shell scripting with error handling and privilege separation (blog article)
- What's the best practice for building a DSL in haskell?, a discussion at Reddit