<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Caleb Case</title>
	<atom:link href="http://calebcase.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://calebcase.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Dec 2010 00:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='calebcase.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Caleb Case</title>
		<link>http://calebcase.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://calebcase.com/osd.xml" title="Caleb Case" />
	<atom:link rel='hub' href='http://calebcase.com/?pushpress=hub'/>
		<item>
		<title>Ornery Types and Their Associated Grammars</title>
		<link>http://calebcase.com/2010/12/02/ornery-types-and-their-associated-grammars/</link>
		<comments>http://calebcase.com/2010/12/02/ornery-types-and-their-associated-grammars/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 00:40:42 +0000</pubDate>
		<dc:creator>Caleb</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://calebcase.com/?p=40</guid>
		<description><![CDATA[My goals are simple: Represent an AST in Haskell and have the type system accurately describe what is permitted in the language. Almost immediately I was foiled. Consider this deceptively simple language: L ::= A &#124; B &#124; C &#124; D A ::= 'a' B ::= 'b' C ::= 'c' '[' {B} ']' D ::= <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=40&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My goals are simple: Represent an AST in Haskell and have the type system accurately describe what is permitted in the language. Almost immediately I was foiled.</p>
<p>Consider this deceptively simple language:</p>
<pre><code>L ::= A | B | C | D
A ::= 'a'
B ::= 'b'
C ::= 'c' '[' {B} ']'
D ::= 'd' '[' {L} ']'</code></pre>
<p>Strings in this language:</p>
<pre><code>a
b
c [ b ]
c [ b b b ]
d [ b a b ]
d [ a b c [ b b ] ]</code></pre>
<p>Strings NOT in this language:</p>
<pre><code>c [ a b a ]
a b</code></pre>
<p>Now that I know what language I&#8217;m dealing with I can postulate a few Haskell types that might fit the AST bill. I can start by trying the most direct and obvious approach using an algebraic data type:</p>
<pre><code>type L</code></pre>
<p>To which I will add a constructor for A:</p>
<pre><code>    = A</code></pre>
<p>And a constructor for B:</p>
<pre><code>    | B</code></pre>
<p>I&#8217;ll skip C for a moment and add D:</p>
<pre><code>    | D [L]</code></pre>
<p>So far I have:</p>
<pre><code>type L
    = A
    | B
    | D [L]</code></pre>
<p>Which leaves me with C and as you have probably surmised this is where the trouble begins. I could add a constructor for C similar to D:</p>
<pre><code>    | C [L]</code></pre>
<p>But this violates the stated goal of having the AST types tightly match what is actually permitted in the language. In this case the AST would permit the forbidden string &#8220;c [ a b a ]&#8220;. It would be lovely if I could say:</p>
<pre><code>    | C [B]</code></pre>
<p>But B isn&#8217;t a type it&#8217;s a constructor&#8230; Well I could fix that by pulling B out of L and adding in C:</p>
<pre><code>type B = B

type L
    = A
    | D [L]
    | C [B]</code></pre>
<p>Except that I&#8217;ve lost the ability to represent strings such as &#8220;d [ b a b ]&#8220;. Another option would be to parameterize our type L like so:</p>
<pre><code>type B = B
type L b
    = A
    | B' b
    | D [L]
    | C [b]</code></pre>
<p>Which when using B&#8217; B and C [B] permits me to represent all the strings I&#8217;m looking for. It introduces an oddity however. The elements of C&#8217;s list won&#8217;t be directly comparable to those of B&#8217;. While I&#8217;ve no proof of this, I suspect this will lead to some painful acrobatics later on when trying to write functions that manipulate the AST. Similarly, I could &#8216;wrap&#8217; all the constructors of L:</p>
<pre><code>type A = A
type B = B
type C = C [B]
type D = D [L]
type L = LA A | LB B | LC C | LD D</code></pre>
<p>Like before I cannot directly compare B with LB, but I can always count on unpacking an L when doing comparisons and so should be easier. Overall I rank these solutions &#8216;so so&#8217;. Can I do better? Maybe if I use existential types and type classes?</p>
<pre><code>{-# LANGUAGE ExistentialQuantification #-}

class Show a =&gt; L a

data A = A deriving Show
instance L A

data B = B deriving Show
instance L B

data C = C [B] deriving Show
instance L C

data D = D [AnyL] deriving Show
instance L D

data AnyL = forall a. L a =&gt; AnyL a
instance L AnyL
instance Show AnyL where
    show (AnyL a) = show a</code></pre>
<p>Not much better really since I can&#8217;t compare AnyL to B directly. Actually, its worse since I can&#8217;t unpack AnyL. Not all that surprising as that&#8217;s sort of the whole premise of existential types, but very very inconvenient. This solution has the dubious honor of being viewed by some as an <a title="anti-pattern" href="http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/">anti-pattern</a>. Using GADTs I end up with a similar problem:</p>
<pre><code>{-# LANGUAGE GADTs #-}

data A = A deriving Show
data B = B deriving Show
data C = C deriving Show
data D = D deriving Show

data L a where
    LA :: A -&gt; L A
    LB :: B -&gt; L B
    LC :: C -&gt; [L B] -&gt; L C
    LD :: D -&gt; [L b] -&gt; L D

instance Show (L a) where
    show (LA a) = show a
    show (LB a) = show a
    show (LC a b) = show (a, b)
    show (LD a b) = show (a, b)</code></pre>
<p>I have successfully restricted C to type L B (which is different from all previous examples actually), but now D is restricted to only one type of L. It won&#8217;t accept the string &#8220;d [ a b a ]&#8220;. Without reintroducing AnyL I also can&#8217;t see a way to make D general to all L. Which leaves me with two mediocre solutions and a headache.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/calebcase.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/calebcase.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=40&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://calebcase.com/2010/12/02/ornery-types-and-their-associated-grammars/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3cd96274221d5c418ea8334b2fe3acb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">calebcase</media:title>
		</media:content>
	</item>
		<item>
		<title>Monad. Monad.</title>
		<link>http://calebcase.com/2010/07/15/monad-monad/</link>
		<comments>http://calebcase.com/2010/07/15/monad-monad/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 07:23:53 +0000</pubDate>
		<dc:creator>Caleb</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://calebcase.com/?p=33</guid>
		<description><![CDATA[&#62; import Control.Monad &#62; cartesian xs ys = &#62; liftM2 pair xs ys &#62; where pair a b = (a, b) $ cartesian [1,2,3] [4,5,6] [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] Consider my imperative brain just eaten by a monad. I had come to understand monads to be something like a way to string computations together so that they would <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=33&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><!-- table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode, table.sourceCode pre { margin: 0; padding: 0; border: 0; vertical-align: baseline; border: none; } td.lineNumbers { border-right: 1px solid #AAAAAA; text-align: right; color: #AAAAAA; padding-right: 5px; padding-left: 5px; } td.sourceCode { padding-left: 5px; } pre.sourceCode { } pre.sourceCode span.Normal { } pre.sourceCode span.Keyword { color: #007020; font-weight: bold; } pre.sourceCode span.DataType { color: #902000; } pre.sourceCode span.DecVal { color: #40a070; } pre.sourceCode span.BaseN { color: #40a070; } pre.sourceCode span.Float { color: #40a070; } pre.sourceCode span.Char { color: #4070a0; } pre.sourceCode span.String { color: #4070a0; } pre.sourceCode span.Comment { color: #60a0b0; font-style: italic; } pre.sourceCode span.Others { color: #007020; } pre.sourceCode span.Alert { color: red; font-weight: bold; } pre.sourceCode span.Function { color: #06287e; } pre.sourceCode span.RegionMarker { } pre.sourceCode span.Error { color: red; font-weight: bold; } --></p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Keyword">import</span><span class="Normal NormalText"> </span><span class="Normal ModuleName">Control.Monad</span>
<span class="Special">&gt; </span><span class="Normal NormalText">cartesian xs ys =</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	liftM2 pair xs ys</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Keyword">where</span><span class="Normal NormalText"> pair a b = (a, b)</span>
</code></pre>
<pre><code>$ cartesian [1,2,3] [4,5,6]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
</code></pre>
<p>Consider my imperative brain just eaten by a monad. I had come to understand monads to be something like a way to string computations together so that they would occur in order. For example:</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText">printme z = </span><span class="Keyword">do</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">print</span><span class="Normal NormalText"> </span><span class="String">"One"</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">print</span><span class="Normal NormalText"> </span><span class="String">"Two"</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">print</span><span class="Normal NormalText"> $ </span><span class="String">"Many "</span><span class="Normal NormalText"> ++ z</span>
</code></pre>
<p>May the Count and XKCD forgive me, this does just what you&#8217;d expect:</p>
<pre><code>$ printme "ah ah ah"
"One"
"Two"
"Many ah ah ah"
</code></pre>
<p>Now consider this doozy from the Monad wiki article [<a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#do-notation">1</a>]:</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText"> a = </span><span class="Keyword">do</span><span class="Normal NormalText"> x &lt;- [</span><span class="DecVal Decimal">3</span><span class="Normal NormalText">..</span><span class="DecVal Decimal">4</span><span class="Normal NormalText">]</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	[</span><span class="DecVal Decimal">1</span><span class="Normal NormalText">..</span><span class="DecVal Decimal">2</span><span class="Normal NormalText">]</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">return</span><span class="Normal NormalText"> (x, </span><span class="DecVal Decimal">42</span><span class="Normal NormalText">)</span>
</code></pre>
<p>What do you suppose this does? Straight away it appears that the [1..2] does nothing right? Regardless of the meaning of the x &lt;- [3..4] &#8230; right? It&#8217;s not so:</p>
<pre><code>$ a
[(3,42),(3,42),(4,42),(4,42)]
</code></pre>
<p>Cue confusion.</p>
<p>Let&#8217;s simplify that example a bit:</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText">b = </span><span class="Keyword">do</span><span class="Normal NormalText"> x &lt;- [</span><span class="DecVal Decimal">3</span><span class="Normal NormalText">..</span><span class="DecVal Decimal">4</span><span class="Normal NormalText">]</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">return</span><span class="Normal NormalText"> (x, </span><span class="DecVal Decimal">42</span><span class="Normal NormalText">)</span>
</code></pre>
<pre><code>$ b
[(3,42),(4,42)]
</code></pre>
<p>Mmm hmm, yeah. <code>x &lt;- [3..4]</code> is really doing something weird here. According to the wiki article [<a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#do-notation">1</a>] the do notation is really just sugar for this:</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText">a2 = [</span><span class="DecVal Decimal">3</span><span class="Normal NormalText">..</span><span class="DecVal Decimal">4</span><span class="Normal NormalText">] &gt;&gt;= (\x -&gt; [</span><span class="DecVal Decimal">1</span><span class="Normal NormalText">..</span><span class="DecVal Decimal">2</span><span class="Normal NormalText">] &gt;&gt;= (\_ -&gt; </span><span class="Function">return</span><span class="Normal NormalText"> (x, </span><span class="DecVal Decimal">42</span><span class="Normal NormalText">)))</span>
</code></pre>
<p>Ok, combine that with the definition:</p>
<pre><code>instance Monad [] where
	m &gt;&gt;= f  = concatMap f m
	return x = [x]
	fail s   = []
</code></pre>
<p>(Wait, List is a monad? Yup.)</p>
<p>The article then helpfully goes on to show that replacing <code>&gt;&gt;=</code> with concatMap produces just the list we saw for <code>a</code>. Which is to say, when you use lists in monads you are mapping the values over the remainer of the monad: ala foreach.</p>
<p>To illustrate we have a cartesian product:</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText">cartesian0 xs ys = </span><span class="Keyword">do</span><span class="Normal NormalText">		</span><span class="Comment">-- given a list of xs and ys</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	x &lt;- xs			</span><span class="Comment">-- for each x</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	y &lt;- ys			</span><span class="Comment">--   for each y</span>
<span class="Special">&gt; </span><span class="Normal NormalText">	</span><span class="Function">return</span><span class="Normal NormalText"> $ (x, y)		</span><span class="Comment">--     append the tuple (x, y)</span>
</code></pre>
<p>On that same page though we have the definition for liftM2 which looks an awful lot like what we just wrote for cartesian0:</p>
<pre><code>liftM2 :: Monad m =&gt; (a -&gt; b -&gt; c) -&gt; m a -&gt; m b -&gt; m c
liftM2 op mx my = do
	x &lt;- mx
	y &lt;- my
	return (op x y)
</code></pre>
<p>Thus the definition from the top:</p>
<pre><code>cartesian xs ys =
	liftM2 pair xs ys
	where pair a b = (a, b)
</code></pre>
<p>But the fun doesn&#8217;t stop there! Looks like a pretty slick way to perform a computation that typically would require an accumulator eh? Tada! I give you list comprehensions in disguise. Or rather list comprehensions are monads in disguise [<a href="http://www.haskell.org/haskellwiki/List_comprehension">2</a>].</p>
<pre class="sourceCode haskell"><code><span class="Special">&gt; </span><span class="Normal NormalText">cartesian1 xs ys = [(x, y) | x &lt;- xs, y &lt;- ys]</span>
</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/calebcase.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/calebcase.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=33&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://calebcase.com/2010/07/15/monad-monad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3cd96274221d5c418ea8334b2fe3acb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">calebcase</media:title>
		</media:content>
	</item>
		<item>
		<title>Under Construction</title>
		<link>http://calebcase.com/2010/05/17/under-construction/</link>
		<comments>http://calebcase.com/2010/05/17/under-construction/#comments</comments>
		<pubDate>Mon, 17 May 2010 01:00:03 +0000</pubDate>
		<dc:creator>Caleb</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://calebcase.wordpress.com/?p=1</guid>
		<description><![CDATA[Site rebirth take-N. The short of it: Google Sites fails as a blog<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=1&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Site rebirth take-N. The short of it: Google Sites fails as a blog.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/calebcase.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/calebcase.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=calebcase.com&#038;blog=13712583&#038;post=1&#038;subd=calebcase&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://calebcase.com/2010/05/17/under-construction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3cd96274221d5c418ea8334b2fe3acb1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">calebcase</media:title>
		</media:content>
	</item>
	</channel>
</rss>
