Difference between revisions of "Embedded domain specific language"

From HaskellWiki
Jump to navigation Jump to search
(Added a link to "AwesomePrelude presentation (video)")
(degrees of embedding)
Line 1: Line 1:
 
'''Embedded Domain Specific Language''' means that you embed a [http://en.wikipedia.org/wiki/Domain_specific_language Domain specific language] in a language like Haskell.
 
'''Embedded Domain Specific Language''' means that you embed a [http://en.wikipedia.org/wiki/Domain_specific_language Domain specific language] in a language like Haskell.
 
E.g. using the [http://cryp.to/funcmp/ 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.
 
E.g. using the [http://cryp.to/funcmp/ 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.
  +
  +
== 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 <hask>a+b</hask> is translated to a <hask>String</hask> like <hask>"a + b"</hask> containing that target language expression.
  +
* Deep embedding: Haskell operations only build an interim Haskell data structure that reflects the expression tree. E.g. the Haskell expression <hask>a+b</hask> is translated to the Haskell data structure <hask>Add (Var "a") (Var "b")</hask>. This structure allows transformations like optimizations before translating to the target language.
   
 
== Discussion of common problems ==
 
== Discussion of common problems ==
   
 
[[Sharing]] and [http://en.wikipedia.org/wiki/Recursion recursion] are common problems when implementing DSLs.
 
[[Sharing]] and [http://en.wikipedia.org/wiki/Recursion recursion] are common problems when implementing DSLs.
Often some kind of [[observable sharing]] is requested.
+
Often some kind of [[observable sharing]] is requested
  +
that requires a deep embedding.
   
 
* Oleg in Haskell-Cafe about [http://www.haskell.org/pipermail/haskell-cafe/2008-February/039639.html Designing DSL with explicit sharing (was: I love purity, but it's killing me)]
 
* Oleg in Haskell-Cafe about [http://www.haskell.org/pipermail/haskell-cafe/2008-February/039639.html Designing DSL with explicit sharing (was: I love purity, but it's killing me)]

Revision as of 13:09, 23 January 2011

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.

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 a+b is translated to a String like "a + b" containing that target language expression.
  • Deep embedding: Haskell operations only build an interim Haskell data structure that reflects the expression tree. E.g. the Haskell expression a+b is translated to the Haskell data structure Add (Var "a") (Var "b"). This structure allows transformations like optimizations before translating to the target language.

Discussion of common problems

Sharing and recursion are common problems when implementing DSLs. Often some kind of observable sharing is requested that requires a deep embedding.