<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tom Lokhorst&#039;s blog &#187; DSLs</title>
	<atom:link href="http://tom.lokhorst.eu/category/dsls/feed" rel="self" type="application/rss+xml" />
	<link>http://tom.lokhorst.eu</link>
	<description>Writings from a happy Haskell coder.</description>
	<lastBuildDate>Thu, 11 Feb 2010 11:05:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AwesomePrelude presentation (video)</title>
		<link>http://tom.lokhorst.eu/2010/02/awesomeprelude-presentation-video</link>
		<comments>http://tom.lokhorst.eu/2010/02/awesomeprelude-presentation-video#comments</comments>
		<pubDate>Wed, 10 Feb 2010 21:37:23 +0000</pubDate>
		<dc:creator>Tom Lokhorst</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://tom.lokhorst.eu/?p=258</guid>
		<description><![CDATA[Last monday (February 8, 2010) Sebastiaan Visser and I presented our ongoing work on the AwesomePrelude at the Dutch Haskell User Group meeting.
In a previous post &#8220;Deeply embedded DSLs in Haskell&#8221;, I introduced the AwesomePrelude. While the ideas behind that post haven&#8217;t changed, the implementation of the AwesomePrelude is radically different now.
The AwesomePrelude is reimplementation [...]]]></description>
			<content:encoded><![CDATA[<p>Last monday (February 8, 2010) Sebastiaan Visser and I presented our ongoing work on the <a href="http://github.com/tomlokhorst/AwesomePrelude">AwesomePrelude</a> at the <a href="http://haskell.org/haskellwiki/Dutch_HUG">Dutch Haskell User Group</a> meeting.</p>
<p>In a previous post &#8220;<a href="http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls">Deeply embedded DSLs in Haskell</a>&#8221;, I introduced the AwesomePrelude. While the ideas behind that post haven&#8217;t changed, the implementation of the AwesomePrelude is radically different now.</p>
<p>The AwesomePrelude is reimplementation of the Haskell prelude in which all data types have been replaced with type classes. Embedded Domain Specific Languages (DSLs) can implement these type classes to get access to the functions defined in terms of the type classes. For example: by implementing the <code>BoolC</code> type class, a DSL gets to use the <code>(&amp;&amp;)</code>, <code>(||)</code> and <code>not</code> functions.</p>
<p>Here&#8217;s a <a href="http://vimeo.com/9351844">recording</a> of our presentation:</p>
<p><iframe src="http://player.vimeo.com/video/9351844" width="640" height="360" frameborder="0"></iframe></p>
<p>The <a href="http://tom.lokhorst.eu/media/presentation-awesomeprelude-dhug-feb-2010.pdf">slides</a> are online, and the code <a href="http://github.com/tomlokhorst/AwesomePrelude">is available</a> on github. <br />
The <a href="http://sp2o.org/~sebas/frp-js/www/main.html">reactive javascript example</a> Sebastiaan mentions in the talk doesn&#8217;t use the AwesomePrelude per se, but is based on the same ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://tom.lokhorst.eu/2010/02/awesomeprelude-presentation-video/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deeply embedded DSLs in Haskell: overloading everything under the sun</title>
		<link>http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls</link>
		<comments>http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls#comments</comments>
		<pubDate>Fri, 04 Sep 2009 01:44:19 +0000</pubDate>
		<dc:creator>Tom Lokhorst</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://tom.lokhorst.eu/?p=229</guid>
		<description><![CDATA[Haskell is a great language for building embedded domain specific languages. Using algebraic data types and higher order functions, it&#8217;s very easy to work with and reason about an embedded language. This week at the ICFP, several people commented on how Haskell allowed them to build custom control structures and express ideas more clearly. However, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/media/haskell-symposium-2009.jpg" alt="Haskell Symposium 2009" title="Haskell Symposium 2009" width="225" height="258" class="alignright size-full">Haskell is a great language for building embedded domain specific languages. Using algebraic data types and higher order functions, it&#8217;s very easy to work with and reason about an embedded language. This week at the ICFP, several people commented on how Haskell allowed them to build custom control structures and express ideas more clearly. However, while powerful, these embedded languages usually aren&#8217;t as expressive as their host language Haskell. The concrete syntax usually isn&#8217;t as succinct as the Haskell equivalent of a certain expression.</p>
<p>This article explores the very early stages of an idea by Sebastiaan Visser and myself to more deeply embedding DSLs in Haskell. It has been written up rather quickly, so if (when?) you find errors, please do let me know. Also, when I say &#8220;early stages&#8221;, I really mean just that, there are <em>lots</em> of open questions and unknowns left. To start off, here&#8217;s a little teaser:</p>
<pre><code>*Main&gt; -- A simple function that operates on a generalized boolean
*Main&gt; let f x = not x &amp;&amp; false || true

*Main&gt; -- Coerce the function to work on normal Prelude Bools
*Main&gt; let g = f :: Prelude.Bool -&gt; Prelude.Bool

*Main&gt; -- Call the function
*Main&gt; g false
True

*Main&gt; -- Coerce the exact same function to work on JavaScript booleans
*Main let h = f :: Js JsBool -&gt; Js JsBool

*Main&gt; -- Call the function again
*Main&gt; h false
(((false ? false : true) ? false : (false ? false : true))
 ? ((false ? false : true) ? false : (false ? false : true))
 : true)
</code></pre>
<p>The JavaScript string above is the JavaScript equivalent of the <code>f</code> function applied to <code>false</code>. If we <a href="http://tom.lokhorst.eu/awesome-prelude-javascript-evaluator.html">evaluate</a> that string, we get a JavaScript boolean <code>true</code>!</p>
<pre><code>&gt; {-# LANGUAGE GADTs, FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies #-}
</code></pre>
<h2>Background</h2>
<p>At its most basic level, a DSL is usually expressed as an ADT. Take for example this simple definition of a mathematical expression language:</p>
<pre><code>&gt; data ArithExpr
&gt;   = Lit Integer
&gt;   | Add ArithExpr ArithExpr
&gt;   | Mul ArithExpr ArithExpr
&gt;   | Sub ArithExpr ArithExpr
&gt;  deriving (Show, Eq)
</code></pre>
<p>With this evaluator:</p>
<pre><code>&gt; evalArith :: ArithExpr -&gt; Integer
&gt; evalArith (Lit x)   = x
&gt; evalArith (Add x y) = evalArith x + evalArith y
&gt; evalArith (Mul x y) = evalArith x * evalArith y
&gt; evalArith (Sub x y) = evalArith x - evalArith y
</code></pre>
<p>This is a <em>very</em> simple language supporting literal values (<code>Lit</code>), addition (<code>Add</code>), multiplication (<code>Mul</code>) and subtraction (<code>Sub</code>). Given this language, we can now write a mathematical expressions like &#8220;6 + 12 * 3&#8243; as such:</p>
<pre><code>&gt; a1 :: ArithExpr
&gt; a1 = Add (Lit 6) (Mul (Lit 12) (Lit 3))
</code></pre>
<p>This is a bit verbose for something that can be expressed in Haskell with a lot less characters. But luckily, we can fix that in this case, by using the <code>Num</code> type class:</p>
<pre><code>&gt; instance Num ArithExpr where
&gt;   x + y         = Add x y
&gt;   x * y         = Mul x y
&gt;   x - y         = Sub x y
&gt;   abs x         = error "Not implemented"
&gt;   signum x      = error "Not implemented"
&gt;   fromInteger x = Lit x
</code></pre>
<p>Now that we have made <code>ArithExpr</code> an instance of the <code>Num</code> type class, we can use Haskell syntax to write down <code>ArithExpr</code> expressions:</p>
<pre><code>&gt; a2 :: ArithExpr
&gt; a2 = 6 + 12 * 3
</code></pre>
<p>Of course, while this looks like a Haskell epression, it still generates an <code>ArithExpr</code> expression tree:</p>
<pre><code>ghci&gt; a2
Add (Lit 6) (Mul (Lit 12) (Lit 3))
</code></pre>
<h2>Getting functions for free</h2>
<p>Now that our <code>ArithExpr</code> is an instance of <code>Num</code> we can use functions like <code>+</code>, but we can do even more. We can also reuse all existing functions that are defined in terms of the <code>+</code> operator and other functions in the <code>Num</code> type class. For example the <code>sum</code> function:</p>
<pre><code>sum :: (Num a) =&gt; [a] -&gt; a
sum []     = fromInteger 0
sum (x:xs) = x + sum xs
</code></pre>
<p>Since this function is defined entirely using functions from the <code>Num</code> type class, it also works on our <code>ArithExpr</code>!</p>
<pre><code>&gt; a3 :: ArithExpr
&gt; a3 = sum [3, 4 * 3, 2 - 7]
</code></pre>
<p>When we inspect this in GHCi we get the following:</p>
<pre><code>ghci&gt; a3
Add (Add (Add (Lit 0) (Lit 3)) (Mul (Lit 4) (Lit 3)))
    (Sub (Lit 2) (Lit 7))
ghci&gt; evalArith a3
10
</code></pre>
<p>Obviously we didn&#8217;t just add lists to our simple expression language. The lists here are Haskell lists, containing <code>ArithExpr</code> values. Haskell can be used as a meta programming language for the <code>ArithExpr</code> language.</p>
<h2>Scaling up: using more Haskell syntax</h2>
<p>A few months ago Sebastiaan send a message to the Haskell Cafe mailing list titled &#8220;<a href="http://www.mail-archive.com/haskell-cafe@haskell.org/msg60078.html">Bool as type class to serve EDSLs</a>&#8221; where he asked about using the <code>Eq</code> type class for DSLs. In the same way that we make <code>ArithExpr</code> an instance of <code>Num</code>. Lets take this ADT:</p>
<pre><code>&gt; data Expr a where
&gt;   Num      :: ArithExpr -&gt; Expr Integer
&gt;   LitFalse :: Expr Bool
&gt;   LitTrue  :: Expr Bool
&gt;   Equal    :: Expr Integer -&gt; Expr Integer -&gt; Expr Bool
&gt;   If       :: Expr Bool -&gt; Expr a -&gt; Expr a -&gt; Expr a
</code></pre>
<p>The <code>Eq</code> and <code>Show</code> instances for this GADT are provided at the end of this article. The evaluator is defined as such:</p>
<pre><code>&gt; eval :: Expr a -&gt; a
&gt; eval (Num ae)      = evalArith ae
&gt; eval (LitFalse)    = False
&gt; eval (LitTrue)     = True
&gt; eval (Equal e1 e2) = eval e1 == eval e2
&gt; eval (If p e1 e2)  = if eval p then eval e1 else eval e2
</code></pre>
<p>We can now write down an expression like this:</p>
<pre><code>&gt; a4 :: Expr Integer
&gt; a4 = If (Equal ((2 + 3)) (5))
&gt;         (Num 1)
&gt;         (Num 0)
</code></pre>
<p>And here&#8217;s where the <code>Eq</code> type class comes in, we&#8217;d like to use the <code>(==)</code> operator instead of the <code>Equal</code> constructor. Unfortunately, this is not possible since the type of <code>(==)</code> is this:</p>
<pre><code>(==) :: Eq a =&gt; a -&gt; a -&gt; Bool
</code></pre>
<p>This shows us that <code>(==)</code> is polymorphic in both its arguments, but not in its return type. We could make our <code>Expr</code> data type an instance of <code>Eq</code>, but won&#8217;t be not able to use <code>Equal</code> as an implementation of <code>(==)</code>. <code>Equal</code> is of type <code>Expr Integer -&gt; Expr Integer -&gt; Expr Bool</code>, it returns a <code>Expr Bool</code> instead of <code>Bool</code>.</p>
<p>What we would like to have is a more polymorphic <code>(==)</code> of the following type:</p>
<pre><code>(==) :: (Eq a, BooleanLike b) =&gt; a -&gt; a -&gt; b
</code></pre>
<p>We have replaced the type <code>Bool</code> with a polymorphic <code>b</code> that is an instance of the <code>BooleanLike</code> type clas. This way <code>(==)</code> is polymorphic in both its arguments (a long as they implement <code>Eq</code>) and its result type (as long as its &#8220;<code>BooleanLike</code>&#8220;).</p>
<p>In other words: we replace the <strong>concrete data type</strong> <code>Bool</code> with a polymorphic type <code>b</code> in a <strong>type class</strong>. Can we take this further?</p>
<h2>Going large: no more data types!</h2>
<p>With that idea in mind (<em>replace data types with type classes</em>), Sebastiaan and I have been working on <em>systematically</em> rewriting the Haskell Prelude to a more general version. We have dubbed this alternative Prelude the <code>AwesomePrelude</code>, and it&#8217;s currently <a href="http://github.com/tomlokhorst/AwesomePrelude">available on github</a>.</p>
<p>Lets have a look at how the <code>AwesomePrelude</code> works. First the normal Prelude <code>Bool</code> data type:</p>
<pre><code>data Bool = False | True
</code></pre>
<p>This is rewritten in the <code>AwesomePrelude</code> to the following type class (called <code>Bool</code> in the <code>AwesomePrelude</code>):</p>
<pre><code>&gt; class Boolean f r | f -&gt; r where
&gt;   bool  :: r -&gt; r -&gt; f -&gt; r
&gt;   false :: f
&gt;   true  :: f
</code></pre>
<p>This type class consists of these parts:</p>
<ul>
<li>The <code>false</code> and <code>true</code> members. These represent the original two constructors, but they now are more polymorphic, since they are of type <code>f</code>, the first argument to the type class.</li>
<li>The <code>bool</code> function. Because we no longer have real constructors (we use <code>true</code> and <code>false</code>), we can&#8217;t do pattern matching anymore. With <code>bool</code> we can &#8220;fold&#8221; over a <code>Bool</code> value. The <code>bool</code> function <em>destructs</em> a <code>f</code> value and it returns either the first (false) argument or the second (true) argument.</li>
<li>The <code>f</code> type argument. This is the concrete type we use to represent a boolean. In the case of our <code>Expr</code> data type, this is <code>(Expr Bool)</code>, see below.</li>
<li>The <code>r</code> type argument and <code>f -&gt; r</code> functional dependency. This is the somewhat strange part of the type class. We&#8217;ll discuss this after we&#8217;ve looked at the <code>Expr</code> instance.</li>
</ul>
<p>Giving the type class defined above, we make our <code>Expr</code> data type an instance of the type class like so:</p>
<pre><code>&gt; instance Boolean (Expr Bool) (Expr Integer) where
&gt;   bool f t p = If p f t
&gt;   false      = LitFalse
&gt;   true       = LitTrue
</code></pre>
<p>This instance demonstrates the need for the <code>r</code> type argument in the <code>Boolean</code> type class. The <code>r</code> variable is the type to which a boolean is &#8220;destructed&#8221;, when it is &#8220;pattern matched&#8221; (that is, it is used). We need a way to ensure that the result of using a boolean in a DSL doesn&#8217;t fall outside that DSL.</p>
<p>In the case of our <code>Expr</code> data type, the result of <code>If</code> can only be a <code>Expr Integer</code>. Other languages might be a bit more liberal, but they usually do at restrictions. For example, we provide a JavaScript instance of the <code>AwesomePrelude</code> called <code>JsPrelude</code>. In this <code>JsPrelude</code>, we have this: <code>instance Boolean (Js Bool) (Js a)</code>. That means that using a <code>Js Bool</code> will always result in a <code>Js a</code>.</p>
<p>To show how this pattern generalizes, lets look at the <code>Maybe</code> type class.</p>
<pre><code>class Maybe f a r | f -&gt; a, f -&gt; r where
  maybe   :: r -&gt; (a -&gt; r) -&gt; f a -&gt; r
  nothing :: f a
  just    :: a -&gt; f a
</code></pre>
<p>This type class again has two &#8220;constructors&#8221;, <code>nothing</code> and <code>just</code>. Also there is a &#8220;destructor&#8221; function called <code>maybe</code>, which happens to be the same thing as the <code>maybe</code> function in the normal Haskell Prelude.</p>
<p>The real difference between the <code>Maybe</code> type class and the <code>Bool</code> type class is the extra type variable <code>a</code>. This is introduced because the normal <code>Maybe</code> data type has a type variable <code>a</code>. Indeed, the number of type variables in the &#8220;type classification&#8221; of a data type is equal to its original number of arguments plus two for the <code>f</code> and the <code>r</code></p>
<h2>Conclusion? Free functions and future work!</h2>
<p>We are currently working on the <a href="http://github.com/tomlokhorst/AwesomePrelude"><code>AwesomePrelude</code></a> on github. The <a href="http://github.com/tomlokhorst/AwesomePrelude/blob/c2bd5156e7a4367b1d6304ec31d1eaabac90ac14/src/AwesomePrelude.hs"><code>AwesomePrelude</code> module</a> exposes type classes for each data type in the normal <code>Prelude</code>. Note that by &#8220;each&#8221;, I really mean &#8220;the five that we felt like writing&#8221;. The <code>AwesomePrelude</code> module also exports common functions like <code>(&amp;&amp;)</code>, <code>sum</code> and <code>uncurry</code>, that are entirely defined in terms of the &#8220;constructors&#8221; and &#8220;destructors&#8221; in the module.</p>
<p>We have written a <code>JsPrelude</code> that provides instances for the type classes defined in the <code>AwesomePrelude</code>. While this module is far from complete (we&#8217;re doing some weird type hackery, that isn&#8217;t really working), it can already do some cool things, like the example at the top of this article.</p>
<blockquote>
<p>&#8230; define a function on &#8220;type class values&#8221; in the <strong>exact same way</strong> as on normal &#8220;data type values&#8221; &#8230; there is <strong>no difference</strong> between using the generalized functions and the original functions.</p>
</blockquote>
<p>The <a href="http://github.com/tomlokhorst/AwesomePrelude/blob/c2bd5156e7a4367b1d6304ec31d1eaabac90ac14/src/Main.hs"><code>Main</code></a> module shows some examples of uses of the generalized Prelude. The nice point about this examples is this though: We can define a function on &#8220;type class values&#8221; in the <strong>exact same way</strong> as we would on normal &#8220;data type values&#8221;. Except for constructors and pattern matching, there is <strong>no difference</strong> between using the generalized functions and the original functions.</p>
<p>To sum up: Our understanding of this concept is still currently rather limited, but I have the feeling we can do a lot with this, particularly in the field of embedded domain specific languages. After seeing a couple of interesting talks this week at the ICFP and the Haskell Symposium, I think I need to start reading lots of papers (any pointers would be appreciated). More on this to follow!</p>
<hr />
<p><small><br />
A few remaining instance declarations:</p>
<pre><code>&gt; instance (Show a) =&gt; Show (Expr a) where
&gt;   show (Num ae)      = "Num (" ++ show ae ++ ")"
&gt;   show (LitFalse)    = "LitFalse"
&gt;   show (LitTrue)     = "LitTrue"
&gt;   show (Equal e1 e2) = "Equal (" ++ show e1 ++ ") (" ++ show e2 ++ ")"
&gt;   show (If p e1 e2)  = "If (" ++ show p  ++ ") (" ++ show e1 ++ ")"
&gt;                                           ++ " (" ++ show e2 ++ ")"

&gt; instance (Eq a) =&gt; Eq (Expr a) where
&gt;   (Num ae)      == (Num ae')       = ae == ae'
&gt;   (LitFalse)    == (LitFalse)      = True
&gt;   (LitTrue)     == (LitTrue)       = True
&gt;   (Equal e1 e2) == (Equal e1' e2') = e1 == e1' &amp;&amp; e2 == e2'
&gt;   (If p e1 e2)  == (If p' e1' e2') = p  == p'  &amp;&amp; e1 == e1'
&gt;                                                &amp;&amp; e2 == e2'
&gt;   _             == _               = False

&gt; instance Num (Expr Integer) where
&gt;   (Num x) + (Num y) = Num (x + y)
&gt;   (Num x) * (Num y) = Num (x * y)
&gt;   abs (Num x)       = Num (abs x)
&gt;   signum (Num x)    = Num (signum x)
&gt;   fromInteger x     = Num (Lit x)
</code></pre>
<p></small></p>
]]></content:encoded>
			<wfw:commentRss>http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
