<?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>Integer Overflow</title>
	<atom:link href="http://intoverflow.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://intoverflow.wordpress.com</link>
	<description>Not quite insightful</description>
	<lastBuildDate>Fri, 06 Jan 2012 02:08:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='intoverflow.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Integer Overflow</title>
		<link>http://intoverflow.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://intoverflow.wordpress.com/osd.xml" title="Integer Overflow" />
	<atom:link rel='hub' href='http://intoverflow.wordpress.com/?pushpress=hub'/>
		<item>
		<title>I come from Java and want to know what monads are in Haskell</title>
		<link>http://intoverflow.wordpress.com/2010/07/20/i-come-from-java-and-want-to-know-what-monads-are-in-haskell/</link>
		<comments>http://intoverflow.wordpress.com/2010/07/20/i-come-from-java-and-want-to-know-what-monads-are-in-haskell/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 01:37:53 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=546</guid>
		<description><![CDATA[This is an introduction to monads. There are many of these. My goal today is to show how a simple class written in Java could be translated into equivalent functionality in Haskell using some monads, without getting into any of the theory stuff. Hopefully some people coming from a non-Haskell background will get something out [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=546&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is an introduction to monads.  There are <a href="http://www.haskell.org/haskellwiki/Monad_tutorials_timeline">many of these</a>.  My goal today is to show how a simple class written in Java could be translated into equivalent functionality in Haskell using some monads, without getting into any of the theory stuff.</p>
<p>Hopefully some people coming from a non-Haskell background will get something out of this, though the Haskell syntax is likely to be very WTF-inducing for those who haven&#8217;t seen it before.</p>
<p>I should begin with a few things that this guide is <i>not</i> about:</p>
<ul>
<li>Categories.  The etymology of the word &#8220;monad&#8221; is a red herring.  Trust me.  Knowing a lot about category theory will make you a better programmer in the same way that playing a lot of checkers will make you better at chess: there&#8217;s probably <i>some</i> benefit, but it&#8217;s not a good way to get up and running with the basics.
<p>We will be treating monads as a <b>design pattern</b> instead of <b>monoids in the category of endofunctors</b>.  The latter perspective is interesting to the people who like to design languages; the former perspective is interesting to people who like to write code.</li>
<li>The full generality of monads in programming.  Lots of things are monads, and I will be ignoring most of them.  I&#8217;m going to focus on how monads can be used to translate a particular Java class into Haskell, and what it looks like as we add functionality to both versions of the code.</li>
<li>Haskell evangelism.  I&#8217;ve already written about the <a href="http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/">things in Haskell that I love</a>.  I love Haskell, and I use it to make my dreams come true.  On the other hand, I&#8217;m not at all invested in whether or not you like Haskell.
<p>Though if you want to learn more about this language, I&#8217;d like to help you along your journey.</li>
</ul>
<p>What I <i>am</i> going to talk about is how to use monads to do something in Haskell that is easy to do in Java.</p>
<p><span id="more-546"></span></p>
<h2>Some idiomatic Java code and some non-idiomatic Haskell code</h2>
<p>Let&#8217;s start by taking a look at a class in Java:<br />
<code>
<pre>
public class IntWrapper {
  private int m_i;

  public IntWrapper(int i) { m_i = i; }

  public void print() { System.out.println(m_i); }

  public void inc() { m_i++; }

  public void nextPrime() {
    while (!isPrime()) inc();
  }

  public boolean isPrime() {
    for (int t = 2; t &lt; m_i; t++)
      if (m_i % t == 0)
        return false;
    return true;
  }
}
</pre>
<p></code><br />
We&#8217;re going to re-implement this in Haskell using monads.  To do this, we&#8217;re going to need to look at some facets of this code that are so obvious we usually wouldn&#8217;t even <i>think</i> to mention them:</p>
<ul>
<li>(<b>Valid by construction</b>) This class has a constructor.  The constructor is there to make sure that the class methods have valid data to work with.</li>
<li>(<b>Data hiding</b>) Once we&#8217;ve created a new <code>IntWrapper</code> object there is no way to get its private member <code>m_i</code> out: the only code that can access this member variable are the member methods.</li>
<li>(<b>Implicit scoping</b>) Member methods can refer to <code>m_i</code> without specifying an object instance.  This is because the variable <code>m_i</code> is implicitly <code>this.m_i</code>.</li>
<li>(<b>Encapsulation</b>) Member methods can refer to <code>m_i</code> belonging to other instances, but methods in other classes (that is, methods not in the <code>IntWrapper</code> class) cannot do this.</li>
</ul>
<p>How might a translation of this class look in Haskell?  Here&#8217;s a &#8220;day one&#8221; approach:<br />
<code>
<pre>
module IntWrapper (intWrapper, printiw, inc, isPrime, nextPrime) where

data IntWrapper = IntWrapper Integer

-- We don't export the IntWrapper constructor because we want to
-- preserve encapsulation.  Because of this, we need to export a
-- function that wraps the constructor.  That's what intWrapper does.
intWrapper i = IntWrapper i

printiw (IntWrapper i) = putStrLn (show i)

inc (IntWrapper i) = IntWrapper (i+1)

isPrime (IntWrapper i) = isPrime' 2 i
  where isPrime' t i | t &gt;= i         = True
                     | i `mod` t == 0 = False
                     | otherwise      = isPrime' (t+1) i

nextPrime iw = until isPrime inc iw
</pre>
<p></code><br />
What makes this a &#8220;day one&#8221; approach?  It&#8217;s all about the interface.  Let&#8217;s do a compare and contrast between using the <code>IntWrapper</code> class in Java versus the <code>IntWrapper</code> data type in Haskell.</p>
<p>Java:<br />
<code>
<pre>
public class UseIntWrapper {
  public static void main(String[] argv) {
    IntWrapper myInt = new IntWrapper(20);
    myInt.print();
    for (int i = 0; i &lt; 4; i++)
      myInt.inc();
    myInt.print();
    myInt.nextPrime();
    myInt.print();
  }
}
</pre>
<p></code></p>
<p>Here&#8217;s what this might look like in Haskell (again, a &#8220;day one&#8221; translation):<br />
<code>
<pre>
main =
     do let iw1 = intWrapper 20
        printiw iw1
        let iw2 = (iterate inc iw1) !! 4
        printiw iw2
        let iw3 = nextPrime iw2
        printiw iw3
</pre>
<p></code></p>
<p>This is utterly terrible.  Because we aren&#8217;t mutating any of our variables, we&#8217;ve been forced to define three variables (<code>iw1, iw2, iw3</code>) to try and describe a succession of modified values.  It&#8217;s obviously error-prone.  In fact, when I did this translation, I typo&#8217;d my way into <code>print iw2</code> for the last line instead of <code>print iw3</code> (seriously).  If you like this style, that&#8217;s cool, but if you want to tell me that I should like it as well, be prepared to get punched in the face.</p>
<p>The essential weakness of this style is that it requires us to manually thread the <code>IntWrapper</code> data through each of the functions.  The Java code also does this (we need to refer to <code>myInt</code> throughout) but clearly Java&#8217;s support for mutable state means it isn&#8217;t as ugly as our Haskell code.</p>
<h2>The difference between the two</h2>
<p>Isn&#8217;t this an example of why Java has better syntax than Haskell?  Well, no: our Java code looks better than our Haskell code because we have used <i>good</i> Java practices and <i>bad</i> Haskell practices.</p>
<p>It&#8217;s quite easy to write equally crappy code in Java.  In fact, let&#8217;s do just that.  When we&#8217;re done, we&#8217;ll compare the bad Java to the good Java, make a few observations, and apply those lessons to our Haskell code.</p>
<p>Here is the <code>IntWrapper</code> class, written so that</p>
<ol>
<li>The <code>m_i</code> variable is final, thereby simulating the way in which Haskell prevents us from mutating variables, and</li>
<li>each of the methods will be static, thereby simulating the way in which our Haskell functions led us to have <code>iw1, iw2, iw3</code> in our code.</li>
</ol>
<p>Here is the resulting mess:<br />
<code>
<pre>
// IntWrapper2.java
public class IntWrapper2 {
  private final int m_i;

  public IntWrapper2(int i) { m_i = i; }

  public static void print(IntWrapper2 iw) { System.out.println(iw.m_i); }

  public static IntWrapper2 inc(IntWrapper2 iw) {
    return new IntWrapper2( iw.m_i + 1 );
  }

  public static IntWrapper2 nextPrime(IntWrapper2 iw) {
    int i = iw.m_i;
    while (!isPrimeHelper(i))
      i++;
    return new IntWrapper2(i);
  }

  public static boolean isPrime(IntWrapper2 iw) {
    return isPrimeHelper(iw.m_i);
  }

  private static boolean isPrimeHelper(int i) {
    for (int t = 2; t &lt; i; t++)
      if (i % t == 0)
        return false;
    return true;
  }
}

// UseIntWrapper2.java
public class UseIntWrapper2 {
  public static void main(String[] argv) {
    IntWrapper2 iw1 = new IntWrapper2(20);
    IntWrapper2.print( iw1 );
    IntWrapper2 iw2 = iw1;
    for (int i = 0; i &lt; 4; i++)
      iw2 = IntWrapper2.inc(iw2);
    IntWrapper2.print( iw2 );
    IntWrapper2 iw3 = IntWrapper2.nextPrime(iw2);
    IntWrapper2.print(iw3);
  }
}
</pre>
<p></code></p>
<p>Where we had originally made use of objects to pass state between function, in this crap code we are not doing any such thing.  It looks strikingly similar to our Haskell code, but with extra verbosity (obvious fact: this isn&#8217;t an argument against Java, because in practice no one codes like this).  Let&#8217;s analyze the differences between this crappy implementation and our original Java code.</p>
<h2>An analogy and a lesson</h2>
<p>In our original Java code, the <code>IntWrapper</code> class has a mutable member variable <code>m_i</code>.  Our client code could then create a new <code>IntWrapper</code>, call it <code>myInt</code>, and repeatedly use the same variable as it did its calculations.</p>
<p>I want you to think of the line <code>IntWrapper myInt = new IntWrapper(20)</code> as a <i>declaration of a context</i>.  It is saying &#8220;hey, make me a new context for carrying around an integer.  Call it <code>myInt</code>.&#8221;  Then I want you to read lines like <code>myInt.print()</code> as saying &#8220;go into the <code>myInt</code> context and <code>print</code>.&#8221;  This is <i>precisely</i> what this code means.  In other words, to some approximation, <i>classes in Java provide us a way to define a stateful context</i>.</p>
<p>This mode of thinking is important enough in what follows that I want to expand on it a bit.  Let&#8217;s leave the world of programming for a moment and enter into an analogy.  You and Sue are both talking about a mutual friend named Bob.  You&#8217;ve both known Bob for years.  Your conversation might look something like this:</p>
<blockquote><p>
Sue: Did you see the postcard Bob sent me from Alaska?<br />
You: Yeah.  I couldn&#8217;t believe what he wrote on the back!<br />
Sue: Oh, the thing about the reindeer?  That&#8217;s an old inside joke we had from when we were dating.<br />
You: I didn&#8217;t know you guys had dated.<br />
Sue: It was before we met.  We broke up because he wanted kids and I didn&#8217;t.<br />
You: Really?  I never figured him to be the fatherhood type.
</p></blockquote>
<p>In this exchange there are a few things to observe:</p>
<ul>
<li>After Sue mentions Bob in the first line, for the rest of the conversation he is only mentioned by pronoun.  The conversation implicitly carries the information that you&#8217;re talking about Bob, and indeed, it appears to carry the information about <i>which</i> Bob you&#8217;re talking about.</li>
<li>Sue and Bob have their own contextual information (the inside joke) that wasn&#8217;t known to you until Sue alluded to it.</li>
<li>For that matter, the information that the two of them had dated wasn&#8217;t known to you until Sue mentioned it.</li>
<li>Before the end of the conversation, your mental model of Bob has shifted: you now know that he wants to be a father, whereas before you did not.</li>
</ul>
<p>Structurally, when the conversation started, it was established that you and Sue are working in a context where &#8220;Bob&#8221; is a known entity.  This is analogous to the Java statement <code>IntWrapper myInt = new IntWrapper(20);</code> which establishes <code>myInt</code> as a known entity.  As Sue reveals information about Bob, changing your mental model of who he is (his <i>state</i>), she is speaking statements similar to <code>myInt.nextPrime()</code>.</p>
<p>In our crappy Java code, we&#8217;ve basically abandoned this whole &#8220;context&#8221; idea.</p>
<p>So to fix our Haskell code, we&#8217;re going to do what we already were trained to do in Java: introduce a notion of <i>context</i>.  To some approximation, this is what monads are about.  A subtle point is that there are different notions of context that arise in programming, and different monads for modeling each.  Following our conversation example from above, this is already something you are familiar with: some conversations are friendly, some are professional, some are adversarial, etc, and each carries with it a unique set of rules for what&#8217;s appropriate and what is not.</p>
<p>This is what different <i>monads</i> do: each comes with its own set of operations that are legal within the context that the monad is modeling.  In some cases &#8212; as with some conversations &#8212; it becomes necessary to nest contexts.  This is what <i>monad transformers</i> do: they give us a way to define some nested contexts and to move information back and forth through the nesting.  Again, an example conversation to illustrate the point:</p>
<blockquote><p>
You: Say, have you heard any news about Bob&#8217;s mother?<br />
Sue: Yeah, I heard she&#8217;s doing alright.<br />
You: Has Bob gotten over the fact that she&#8217;s dating so soon?<br />
Sue: Last time I brought it up he changed the subject.<br />
You: Did she mention anything about whether or not she can make it to Bob&#8217;s surprise party?<br />
Sue: She said she could if we schedule it for Saturday, but not if we are shooting for Sunday.
</p></blockquote>
<p>Here the conversation enters a sub-context dealing with Bob&#8217;s mother.  Information about Bob&#8217;s relationship with his mother is moved around, based on the fact that there are two related contexts now in play in the conversation.  Monad transformers provide a tool for moving information between nested contexts as well.</p>
<p>In Java, the analogous situation is where you have an object which references members that are also objects.  This is obviously an extremely common situation.  Later, when we add logging to our examples, we&#8217;ll see this explicitly.</p>
<h2>Enough analogies, time to be concrete</h2>
<p>We will add some context to our Haskell code via the <a href="http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-State-Lazy.html#2"><code>StateT</code></a> monad transformer.  You can think of <code>StateT</code> as a design pattern for when you think your code would be simplified if you had some mutable state.  (Unsurprisingly, this is a common thing.)  I frequently see people claim that Haskell doesn&#8217;t support mutable state; we&#8217;re about to see that this is false.</p>
<p>The <code>StateT</code> monad gives us a single mutable variable.  It gives us <a href="http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-State-Class.html">three functions</a> for working with this: <code>put</code>, <code>get</code>, and <code>modify</code> (the latter is a convenience function built as a combination of <code>put</code> and <code>get</code>).  Refactoring our Haskell code to use <code>StateT</code> will complicate it a bit, but ultimately boils down to two types of changes:</p>
<ul>
<li>Whereas our earlier Haskell code didn&#8217;t have any type annotations, once we start using <code>StateT</code> it will be better if we include some.  The reason is that, while Haskell is clever enough to infer that we are using <i>some</i> monad, it isn&#8217;t always clever enough to infer <i>which</i> monad we&#8217;re using.  (This is often related to the <a href="http://www.haskell.org/haskellwiki/Monomorphism_restriction">monomorphism restriction</a>, but that&#8217;s a topic for another day.)  The type annotations that we&#8217;ll be putting on will be pretty bland.</li>
<li>We&#8217;ll use some <code>do</code> notation, which will make our code look more imperative.  We&#8217;ll also use <code>get</code> and <code>modify</code>.</li>
</ul>
<p>There&#8217;s one other change that we&#8217;ll make: we&#8217;re going to replace our <code>intWrapper</code> constructor with a different style of constructor that will wind up exposing a better interface to our client code.</p>
<p>When you start using <code>StateT</code>, you can think of the functions you write as being very similar to methods in Java: the function implicitly has access to some data (in Java it was <code>m_i</code>, here it is the data accessed by <code>get</code> and <code>put</code>).  The type signatures on these functions documents this fact.  It&#8217;s important to know, however, that <code>StateT</code> isn&#8217;t the same thing as a Java class, due to some differences I&#8217;ll mention at the end.</p>
<p>Here&#8217;s what the conversion looks like:<br />
<code>
<pre>
module IntWrapper2
        (runIntWrapper, printiw, inc, isPrime, nextPrime) where

import Control.Monad.State

data IntWrapper = IntWrapper Integer

runIntWrapper i f =
     do (a, i) &lt;- runStateT f (IntWrapper i)
        return a

printiw :: MonadIO m =&gt; StateT IntWrapper m ()
printiw =
     do (IntWrapper i) &lt;- get
        liftIO $ putStrLn (show i)

inc :: Monad m =&gt; StateT IntWrapper m ()
inc = modify (\(IntWrapper i) -&gt; IntWrapper (i+1))

isPrime :: Monad m =&gt; StateT IntWrapper m Bool
isPrime =
     do (IntWrapper i) &lt;- get
        return (isPrime&#039; 2 i)
  where isPrime&#039; t i | t &gt;= i         = True
                     | i `mod` t == 0 = False
                     | otherwise      = isPrime&#039; (t+1) i

nextPrime :: Monad m =&gt; StateT IntWrapper m ()
nextPrime =
     do isItPrime &lt;- isPrime
        if isItPrime then return ()
                     else do inc
                             nextPrime
</pre>
<p></code></p>
<p>This code has definitely gotten more complex in appearance, but if you pick any given function, the differences between this version and the original are small.</p>
<p>Before we analyze this code, let&#8217;s take a look at what effect these changes have had on our client code:<br />
<code>
<pre>
import IntWrapper2

main :: IO ()
main = runIntWrapper 20 $
             do printiw
                sequence (replicate 4 inc)
                printiw
                nextPrime
                printiw
</pre>
<p></code></p>
<p>This is <i>substantially</i> better than our &#8220;day one&#8221; implementation, and (in my opinion) is even better than our original Java implementation as well.  The <code>runIntWrapper</code> function takes two arguments: the initial integer (<code>20</code>) and a body of code to execute.  The body of code (the stuff following the <code>do</code> keyword) is just a sequence of instructions that we want to perform on our <code>IntWrapper</code>.  Behind the scenes, <code>runIntWrapper</code> is implicitly threading our <code>IntWrapper</code> to each of these instructions.  This really obviates the whole &#8220;think of this as declaring a context&#8221; idea.</p>
<p>In our conversation analogy, this might be something like sending Bob an email:</p>
<blockquote><p>
To: Bob<br />
From: You<br />
Subject: Directions to my house</p>
<p>From I-90, take exit 18 up to the Highlands.  From there, take the first right turn, passing by Cafe Ladro.  Drive until the road ends.  My house is at the very end; park anywhere on the street.  The doorbell is broken, so you&#8217;ll need to call Sue when you arrive.
</p></blockquote>
<p>The directions don&#8217;t say Bob&#8217;s name anywhere except for the <code>To</code> field of the email, which establishes that the instructions are implicitly for him.</p>
<h2>Some analysis of the transformation</h2>
<p>I already mentioned that <code>StateT</code> is a <i>monad transformer</i>.  Let&#8217;s dig into what this means.</p>
<p>All along I&#8217;ve been trying to push the idea that monads provide us with a way of describing &#8220;contexts&#8221; for our code.  When you sit down and try to get some work done with this idea, you&#8217;ll quickly run into the following question: how can I nest contexts?</p>
<p>I&#8217;ve already mentioned that this is what monad transformers do.  Let&#8217;s dig into that a bit.</p>
<p>Most monads come in two flavors: a standard flavor and a <i>transformer</i> flavor.  The former provides a notion of context; the latter provides a notion of context <i>and</i> is compatible with nesting.</p>
<p>Our <code>printiw</code> function actually demonstrates this idea rather nicely.  As a refresher, here&#8217;s the code for <code>printiw</code>, exactly as it appears above in our <code>StateT</code> version:<br />
<code>
<pre>
printiw :: MonadIO m =&gt; StateT IntWrapper m ()
printiw =
     do (IntWrapper i) &lt;- get
        liftIO $ putStrLn (show i)
</pre>
<p></code><br />
Notice that this function does exactly two things:</p>
<ol>
<li>It uses the <code>get</code> accessor to grab a copy of our integer.</li>
<li>It uses <code>putStrLn</code> to print this integer.</li>
</ol>
<p>What&#8217;s so significant about this?  In Haskell, all things related to the environment (writing to files, opening sockets, printing to the console, etc) are modeled in the <code>IO</code> monad (read: context).  So <code>printiw</code> must be able to work with two contexts: our <code>StateT</code> and <code>IO</code>.  The type signature<br />
<code>
<pre>
printiw :: MonadIO m =&gt; StateT IntWrapper m ()
</pre>
<p></code><br />
documents this: it says that <code>printiw</code> foremost resides in a <code>StateT</code> context, but it must also have access to an <code>IO</code>-capable context <code>m</code> (that&#8217;s what the <code>MonadIO m</code> assumptions means).  The line <code>liftIO $ putStrLn (show i)</code> does two things:</p>
<ol>
<li>It indicates that we want to print to the console.  Trouble is, printing to the console is an action in the <code>IO</code> context, and our code resides within the <code>StateT</code> context.  So to fix this&#8230;</li>
<li>it then takes this action and &#8220;lifts&#8221; it to the broader (<code>StateT</code>) context (via <code>liftIO</code>).</li>
</ol>
<p><b>Note to the reader.</b>  If this seems like boilerplate, it&#8217;s because it is.  Lifting is a facet of the monad design pattern that sometimes introduces boilerplate, but is sometimes done automatically (the distinction depends on which monad transformers you&#8217;re using, and sadly, it seems to take some experience and trial-and-error to get a sense of when you need to <code>lift</code> explicitly and when it&#8217;ll be done for you).  In a moment, when we add logging to our example, lifting will be done automatically for us, and our code won&#8217;t have any additional <code>lift</code>s in it.</p>
<p>You&#8217;ll notice that most of our functions have a signature like <code>Monad m =&gt; StateT IntWrapper m ()</code> instead of using <code>MonadIO</code>.  That&#8217;s because <code>printiw</code> is the only function that needs to do an <code>IO</code> action; the others have no such need, so we are able to give them a more general-purpose signature.</p>
<h2>Isn&#8217;t this a giant hassle?</h2>
<p>Experienced programmers know that there are many philosophies when it comes to writing code.  Usually these philosophies come in opposing pairs: static versus dynamic typing, functional versus imperative, compiled versus interpreted, etc.</p>
<p>What we&#8217;ve just seen is an example of a currently not-so-mainstream philosophy: that types should be used to annotate the ways in which code interacts with contexts.  For instance, the type signature for <code>printiw</code> <i>explicitly documents</i> that this function requires access to the <code>IO</code> monad, while the type signature for <code>nextPrime</code> is sufficiently general that we can conclude that this function <i>does not use</i> the <code>IO</code> monad.</p>
<p>There is no related idea in Java or .Net (though the latter is introducing the <code>pure</code> keyword, at present time support is extremely limited).  In Java, a function may or may not write to the console; the only way to know is to analyze the code, as the type system isn&#8217;t strong enough to make claims one way or the other.</p>
<p>Haskell has a popular reputation for making IO difficult.  My experience &#8212; and I&#8217;d suspect that other experienced Haskell programmers would agree &#8212; is that working with IO in Haskell is no hassle at all; the difficulty is in shifting one&#8217;s thinking away from Java&#8217;s fast-and-loose approach to Haskell&#8217;s explicit-by-default approach.</p>
<p>Whether or not this is a good thing is decidedly a matter of one&#8217;s programming philosophy.</p>
<h2>Lessons in this code that can be reused elsewhere</h2>
<p>When I&#8217;m programming, I usually start <i>without</i> monads, and add them as it becomes clear that they will simplify my design.  Over time, intuition develops that will allow you to decide from the beginning if you want to be using a monad (and which monad to use).</p>
<p>If you want to have some notion of mutated state, <code>StateT</code> is a good way to do it.  To take code that is <i>not</i> written for <code>StateT</code> and refactor it so that it is, the basic steps are:</p>
<ol>
<li>Write a &#8220;constructor&#8221; function.  In our example, that&#8217;s <code>runIntWrapper</code>.  The constructor function takes the initial value of the internal value (we supplied <code>20</code> in our example) <i>and</i> some code to execute within this new context.  In terms of implementation, it&#8217;s just a wrapper for <code>runStateT</code>, which returns two things: the value of your internal state variable after the given code has executed (in our example we discarded this value) as well as any data returned by the given code.</li>
<li>Use <code>get</code>, <code>put</code>, and <code>modify</code> in your functions.</li>
<li>Provide some type signatures to help Haskell along.</li>
</ol>
<p>Obviously this is just a quick and dirty to-do list, and while the steps sometimes vary based on the application, the complexity involved is usually at about this level.</p>
<h2>Nesting contexts</h2>
<p>Let&#8217;s beef up this example a bit, both in Java and in Haskell.  We&#8217;ll add a logging feature to our wrapper: every time a function is called to act on our wrapper, it&#8217;ll make an entry in the log.</p>
<p>In Java the easiest way to introduce logging is to just have a list of strings.  Here&#8217;s what the code looks like:<br />
<code>
<pre>
public class IntWrapper3 {
  private int m_i;
  private List&lt;String&gt; m_log;

  public IntWrapper3(int i) {
    m_i = i;
    m_log = new ArrayList&lt;String&gt;();
  }

  public void print() {
    m_log.add("print");
    System.out.println(m_i);
  }

  public void inc() {
    m_log.add("inc");
    m_i++;
  }

  public void nextPrime() {
    m_log.add("nextPrime start");
    while (!isPrime()) inc();
    m_log.add("nextPrime done");
  }

  public boolean isPrime() {
    m_log.add("isPrime");
    for (int t = 2; t &lt; m_i; t++)
      if (m_i % t == 0)
        return false;
    return true;
  }

  public void printLog() {
    for (Iterator it = m_log.iterator(); it.hasNext(); )
      System.out.println( it.next() );
  }
}
</pre>
<p></code></p>
<p>Pretty simple.  Here&#8217;s how I&#8217;d do the same job in Haskell.  While I <i>could</i> use my current <code>StateT</code> to carry log data, it would be a bit of a mess; after all, &#8220;logging&#8221; and &#8220;working with <code>IntWrapper</code>&#8221; are orthogonal concerns.  Let&#8217;s reflect that fact in our code by using another monad transformer <i>in addition</i> to <code>StateT</code>.  Folks who have read other monad tutorials will not be surprised when I say that we&#8217;re going to use <a href="http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Writer-Lazy.html#v:WriterT"><code>WriterT</code></a>.</p>
<p>Here&#8217;s what it looks like:<br />
<code>
<pre>
module IntWrapper3
        (runIntWrapper, printiw, inc, isPrime, nextPrime) where

import Control.Monad.State
import Control.Monad.Writer

data IntWrapper = IntWrapper Integer

runIntWrapper i f =
     do ((a, i), l) &lt;- runWriterT (runStateT f (IntWrapper i))
        return (a, l)

printiw :: MonadIO m =&gt; StateT IntWrapper (WriterT [String] m) ()
printiw =
     do tell ["printiw"]
        (IntWrapper i) &lt;- get
        liftIO $ putStrLn (show i)

inc :: Monad m =&gt; StateT IntWrapper (WriterT [String] m) ()
inc =
     do tell ["inc"]
        modify (\(IntWrapper i) -&gt; IntWrapper (i+1))

isPrime :: Monad m =&gt; StateT IntWrapper (WriterT [String] m) Bool
isPrime =
     do tell ["isPrime"]
        (IntWrapper i) &lt;- get
        return (isPrime' 2 i)
  where isPrime' t i | t &gt;= i         = True
                     | i `mod` t == 0 = False
                     | otherwise      = isPrime' (t+1) i

nextPrime :: Monad m =&gt; StateT IntWrapper (WriterT [String] m) ()
nextPrime =
     do tell ["nextPrime start"]
        nextPrime'
        tell ["nextPrime done"]
  where nextPrime' =
             do isItPrime &lt;- isPrime
                if isItPrime then return ()
                             else do inc
                                     nextPrime'
</pre>
<p></code></p>
<p>There&#8217;s a few basic things to notice.  First, signatures like <code>Monad m =&gt; StateT IntWrapper m ()</code> have become <code>Monad m =&gt; StateT IntWrapper (WriterT ["String"] m) ()</code>.  This indicates that we have a <i>nested context</i>, or in the Haskell terminology, a <i>monad stack</i>.  The inner monad (read: context) (<code>WriterT</code>) provides us with our logging interface (granted by the <code>tell</code> function).  The <code>["String"]</code> specifies the implementation of our log: we&#8217;re just using a list of strings.</p>
<p>Second, we&#8217;ve changed <code>runIntWrapper</code> so that it is able to accommodate the logging.  In addition to using <code>runStateT</code>, it now also uses <code>runWriterT</code> to get the contents of the log.  It is now returning these contents to its caller.</p>
<p>Let&#8217;s look at our client code:<br />
<code>
<pre>
main :: IO ()
main =
     do (_, l) &lt;- runIntWrapper 20 $
                     do printiw
                        sequence (replicate 4 inc)
                        printiw
                        nextPrime
                        printiw
        mapM_ putStrLn l
</pre>
<p></code><br />
Not surprisingly, only a little has changed.  We are now capturing the data returned by <code>runIntWrapper</code>.  The <code>runIntWrapper</code> function yields both the output of our given code (which we ignore with a little <code>_</code>, as the code we&#8217;re giving doesn&#8217;t have anything to return) as well as the contents of the log at the end of execution (which we capture as <code>l</code>).  We then print the contents of the log using <code>mapM_ putStrLn l</code>.</p>
<h2>A qualitative analysis</h2>
<p>There are a lot of important differences between our Haskell and Java code, some we&#8217;ve already discussed, some we have not.</p>
<p>One difference is the way in which we <i>separated concerns</i>.  In our Haskell code, we wound up using two separate monads (<code>StateT</code> and <code>WriterT</code>) to implement our wrapped integer with logging.  In our Java code, we used a single class with a member variable (a <code>List&lt;String&gt;</code>).</p>
<p>In Haskell, using a stack of monad transformers is an idiomatic way to separate concerns.  The <code>WriterT</code> transformer is an expert in providing a write-only record; the <code>StateT</code> transformer is an expect in providing a mutable variable.  If we were being more serious with our coding, we would have defined a new type like<br />
<code>
<pre>
type IntWrapperT m a = StateT IntWrapper (WriterT [String] m) a
</pre>
<p></code><br />
to ease up our type signatures.  (This practice also makes it easier to later come along and factor in additional monad transformers.  Indeed, defining this type synonym is definitely the preferred way to do things: by leaving it out of the samples I&#8217;ve unfortunately demonstrated bad behavior, but I didn&#8217;t want to have a sidebar discussion of type synonyms in the middle of the example.)</p>
<p>If we had been more serious on the Java side of things, we could have created a class that was purpose-built for logging.  We may have then simply had <code>IntWrapper</code> inherit from this class.</p>
<p>In my view, adding logging in Java and Haskell had about the same amount of overhead (in terms of the work it took as a programmer).  Then again, I&#8217;ve been working in both languages for years, so adding a member variable or another monad transformer is a pretty routine thing for me.  I&#8217;d presume that someone who is new to Haskell (or Java for that matter) would have a different view on the issue.</p>
<p>There&#8217;s an important topic I&#8217;ve thus far ignored: should all Java classes be translated into monads?  My experience has been that this is usually a fine way to implement the class&#8217; functionality in Haskell, in situations where the class is used to carry and mutate state.</p>
<p>It&#8217;s important to realize, however, that these two approaches to design are not interchangeable.  In Java it makes sense to have several instances of a class interacting with one another; in Haskell one usually would not have several blocks of monadic code interacting, unless they were all running within the same monad (read: context).</p>
<p>So while we could implement a method in Java for adding two <code>IntWrapper</code> instances together, a similar function in Haskell would probably require us to refactor a bit (we&#8217;d need to look at the <code>MonadPlus</code> typeclass).  This would be simple to do, but would have taken us down a different path.  This difference does illustrate that Java and Haskell are sufficiently different that a direct translation is not always a natural thing to do.</p>
<p>Of course, there are examples that work the other way as well (that is, monad idioms that are commonplace in Haskell but hard to translate naturally into Java).  I wouldn&#8217;t say that this is a good way to go about deciding which language is &#8220;better,&#8221; but it is definitely an example of why Haskell monads and Java classes aren&#8217;t really solving the same problems (though clearly their problem domains overlap).</p>
<p>Is the approach I demonstrated in Haskell easy or hard?  I believe this to be a matter of one&#8217;s experience.  I can attest that it took me some time to really grok this approach, but then again, I can also attest that it took me some time before I was able to write good object-oriented code as well.</p>
<p>I think that part of the problem is that, while it&#8217;s pretty simple to motivate how one should think about object oriented programming, similar models for how to think about monads have taken some time to develop.  I suspect this is a pedagogical problem more than an intrinsic difficulty with the monad design pattern, but ultimately only time will tell.</p>
<h2>Using monads in your own code</h2>
<p>We&#8217;ve just looked at an example using the <code>StateT</code> and <code>WriterT</code> monad transformers.  Moving forward, here some important questions:</p>
<ol>
<li><b>Will I always use these two monad transformers?</b>  Yes and no.  When you are writing your own applications and libraries, you&#8217;ll probably wind up using these transformers (perhaps as well as <code>ReaderT</code>, which provides a read-only variable) as the foundation for your work.  As suggested above, you&#8217;ll probably define a <code>type</code> synonym to hide this fact.  You&#8217;ll frequently have the <code>IO</code> monad at the base (so that you can have IO), but will sometimes use the <code>Identity</code> moand at the base (when you are writing functions that don&#8217;t need IO).  (The <code>Identity</code> monad literally does nothing.  If you have a stack of monad transformers, and need a monad at the base that won&#8217;t change the semantics of your program, you can use the <code>Identity</code> monad.)
<p>When you use libraries written by others (such as <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/parsec">parsec</a> or Template Haskell) you will find that these libraries provide you with their own monads that they want you to use.  It will be convenient and natural.  You&#8217;ll use these monads in a manner similar the client code we were writing earlier.</p>
<p>(Parsec, a library for writing parsers, is so natural and intuitive that it&#8217;s completely changed my outlook on writing parsers.)</li>
<li><b>Should I always use the transformer versions of the monads, or should I use the &#8220;regular&#8221; versions?  Why use <code>StateT</code> instead of <code>State</code>?</b>  I tend to always use the transformer versions, simply because I find it makes refactoring easier, and it makes the code slightly more general purpose in some situations.  If I think I want to use the regular <code>State</code> monad, I often use <code>StateT</code> transformer on top of the <code>Identity</code> monad, yielding the same behavior.
<p>Of course, that&#8217;s just how I do things.  It&#8217;s certainly not the law.  I&#8217;d presume that someone out there will have a very good list of reasons why I&#8217;m wrong to do what I just described, and in the interest of being a better programmer, I&#8217;d appreciate hearing from them.</li>
<li><b>When should I write my own monad?</b>  Aside from creating a <code>type</code> synonym, I rarely find myself writing my own monads (and with a type synonym, you don&#8217;t need to define a monad instance anyway).  Monads are most frequently used to carry state around, and the <code>StateT</code>, <code>WriterT</code>, and <code>ReaderT</code> monad transformers do that handsomely.
<p>Sometimes a need for <a href="http://stackoverflow.com/questions/3236442/iteration-of-a-randomized-algorithm-in-fixed-space-and-linear-time/3239837#3239837">extra high performance</a> can drive the need to write your own monad.  Sometimes you need to write your own monad to interact with some weird Haskell extension (like the rank-2 types example I mention <a href="http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/">here</a>).</p>
<p>Usually, though, you can go pretty far just using the standard monads and transformers.  I&#8217;ve talked with many multi-year Haskell programmers who have said that they&#8217;ve <i>never</i> written their own monad.  Your mileage may vary.</li>
<li><b>What are the most important monads for a beginner to know about?</b> This would be <code>StateT</code>, <code>WriterT</code>, <code>ReaderT</code>, and <code>ErrorT</code>.  Here&#8217;s what they are used for:
<ul>
<li><b><code>StateT</code></b> is used to pass around mutable state, like our <code>IntWrapper</code> example.</li>
<li><b><code>WriterT</code></b> provides an interface for writing to a collection.  It is very often used for introducing simple logging to an application.</li>
<li><b><code>ReaderT</code></b> provides a read-only variable.  It is most often used to pass around application configuration data in such a way that consumers of this data can&#8217;t modify it (which is a useful design invariant in many situations).</li>
<li><a href="http://hackage.haskell.org/packages/archive/mtl/1.1.0.2/doc/html/Control-Monad-Error.html"><b><code>ErrorT</code></b></a> provides a way to perform operations which might fail, and to manage failure gracefully.  You can think of it as a way to perform computations which might throw exceptions.</li>
</ul>
</li>
</ol>
<p>From here, if you want to know more about the practice of using monads to get work done in Haskell, I&#8217;d suggest checking out <a href="http://book.realworldhaskell.org/">Real World Haskell</a>, which has some good discussion about using monads to engineer solutions to real problems, and the <a href="http://www.haskell.org/haskellwiki/Typeclassopedia">Typeclassopedia</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/546/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/546/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=546&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/07/20/i-come-from-java-and-want-to-know-what-monads-are-in-haskell/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell features I&#8217;d like to see in other languages</title>
		<link>http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/</link>
		<comments>http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 07:27:40 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=502</guid>
		<description><![CDATA[When I read Ben Hutchison&#8216;s OO/Imperative programmers: ‘Study Functional Programming or Be Ignorant’ I knew I had too much to say for the comments, so I figured I&#8217;d put in my 2 cents here. Haskell is my go-to language, both for scripting, and for getting work done. This is not because of any particular allegiance [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=502&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When I read <a href="http://benhutchison.wordpress.com/">Ben Hutchison</a>&#8216;s <a href="http://benhutchison.wordpress.com/2009/06/02/study-functional-programming-or-be-ignorant/">OO/Imperative programmers: ‘Study Functional Programming or Be Ignorant’</a> I knew I had too much to say for the comments, so I figured I&#8217;d put in my 2 cents here.</p>
<p>Haskell is my go-to language, both for scripting, and for <a href="http://potential-lang.org">getting work done</a>.  This is not because of any particular allegiance to the language.  Haskell and I have an open relationship, and the moment I find a language that out-Haskells Haskell, you can be sure I&#8217;ll move on.</p>
<p>Here I want to describe my favorite things about Haskell.  You&#8217;ll note that they are all about the type-system.  I don&#8217;t feel too strongly one way or the other about laziness, or about monads (though I won&#8217;t give them up without first finding something to take their place).  I don&#8217;t even particularly care that it&#8217;s a functional language, in as much as I can have these features in a non-functional environment.</p>
<p>Some of these features are already available elsewhere.  This is wonderful!  If you know of any examples of this, <i>please</i> tell me in the comments.</p>
<p>This is a list of my favorite things:</p>
<p><span id="more-502"></span></p>
<h2>Separation of class and data definitions.</h2>
<p>Haskell&#8217;s notion of classes is more like Java&#8217;s notion of interfaces.  A <i>class</i> is a list of function prototypes, and any data type for which such functions can be defined is an instance of that class.  One does not <i>inheret</i> a parent class, but rather, one <i>implements</i> a class.  It&#8217;s a weird distinction if you haven&#8217;t seen it before, but after I learned how to use it, I must say I prefer it.</p>
<p>The first example most people see is the <code>Show</code> class.  Here is how it&#8217;s defined (to get this listing, I just asked <code>ghci</code> &#8212; the interactive <a href="http://www.haskell.org/ghc">GHC</a> prompt &#8212; to give me the definition):<br />
<code>
<pre>
Prelude&gt; :info Show
class Show a where
  showsPrec :: Int -&gt; a -&gt; ShowS
  show :: a -&gt; String
  showList :: [a] -&gt; ShowS
  	-- Defined in GHC.Show
</pre>
<p></code><br />
This says that any data type <code>a</code> which is an instance of <code>Show</code> provides functions with these signatures.  (<i>Edited:</i> The first of these functions are used for implementing a Haskell idiom for fast string construction, while the last is related to a restriction in the unmodified Haskell 98 standard.)</p>
<p>When I define a new datatype, I can either ask Haskell to derive a <code>Show</code> instance for me automatically, or I can specify one myself:<br />
<code>
<pre>
data Car = Person { make :: String, year :: Int } deriving Show
data Pet = Pet { name :: String, animal :: String, age :: Int }
instance Show Pet where
  show p = "My pet is named " ++ name p ++
           " and he is a " ++ animal p ++
           " and he is " ++ show (age p) ++ " years old."
</pre>
<p></code><br />
I understand that Google&#8217;s <a href="http://golang.org/">Go</a> language has this notion of class (they call it <i>interface</i>), and that <a href="http://www.scala-lang.org/">Scala</a> provides this as well (they call it <i>trait</i>).</p>
<h2>Typed side-effects.</h2>
<p>In Haskell, if a function wants to communicate with the environment, then the function&#8217;s type signature will document this fact.  Want to print to the console?  Open a socket?  Read a file?  Any of these actions will put your function into the <code>IO</code> monad, which is a red-flag to other programmers that the function communicates with the environment.  When your application works with library code (and whose doesn&#8217;t?) this is a handy feature.</p>
<p>Haskell uses the monad design pattern as the underpinning of how it types side-effects.  I don&#8217;t particularly care that it&#8217;s monads <i>per se</i>, I just like that there is <i>something</i> which <i>statically</i> documents which functions communicate with the environment.</p>
<p>Why is this useful?  One huge answer is <b>concurrency</b>.  If your function has side-effects, it is not obviously thread-safe.  If it has no side-effects, it is thread-safe.  The monad design pattern provides a way to define application-specific notions of side-effects, which allows you to dial in the granularity on this as much as is appropriate for your application.</p>
<p>With respect to typed side-effects, a common Haskell idiom is to break up your program into different layers of state.  For instance, in a web framework, you might have a &#8220;user-input&#8221; layer which is read-only, and on top of that a &#8220;logging&#8221; layer, and on top of that your application-specific stuff.  (Each of these layers is a monad, or more precisely, a monad transformer.)  Haskell allows you to statically track which functions rely on which layers, which is a useful thing if you want to call a function and be certain that it won&#8217;t modify some data out from under you.</p>
<p>If you&#8217;re new to Haskell and monads, in my humble opinion <i>this idiom</i> is the real reason to give a damn about monads.  But that&#8217;s just my perspective.</p>
<p>(And it&#8217;s certainly not obvious from the beginning, but a lot of bugs can be eliminated this way.)</p>
<h2>Type safe macros.</h2>
<p>No language is completely free from the occasional boilerplate.  One way around this is to use macros.</p>
<p>In C, macros can be very tricky.  The preprocessor takes all instances of a macro, replaces it with the corresponding text, then passes off to the compiler.  If it turns out that you used the macro incorrectly, the compiler isn&#8217;t really there to help you out: after all C macros are all about find-and-replace.</p>
<p>Haskell&#8217;s macro system is called <a href="http://www.haskell.org/haskellwiki/Template_Haskell"><i>Template Haskell</i></a>.  Macros written in Template Haskell are actually written in Haskell syntax.  The compiler then takes this code, compiles it like it would any other Haskell, and then uses it to expand your usage of the macros.  Everything is typed the whole way through, and if there are errors, the compiler can tell you where they are and why (with its usual level of precision, for better or worse).</p>
<p>When I recently ran into a scenario where (for some very long-winded reason) I had to define 20 essentially-identical datatypes, then give them all essentially the same class instances, I was able to quickly whip up some Template Haskell to do all the lifting for me.  When I realized I needed to modify those class instances, it was as simple as modifying the Template Haskell that was generating them.</p>
<p>This is how macro-ing should be.  Instead of a deal with the devil, it should be safe enough to be accepted practice.</p>
<h2>Quasi-quoting.</h2>
<p>This is one of the many fine ways to embed a language in Haskell.  Here&#8217;s a typical use case: you&#8217;re writing a library and the most natural way for a developer to specify some options is in a simple configuration language.  You could implement a function <code>String -&gt; MyLibOptions</code>, but if they have any typos in their configuration string, you won&#8217;t be able to catch them until run-time.  If the configuration isn&#8217;t known until run-time that&#8217;s fine, but if the configuration is known at <i>compile-time</i>, you&#8217;d like the error to be caught at compile-time.  (I need to mention that quasi-quoting is able to mix run-time and compile-time data &#8212; I&#8217;m just simplifying things to describe this use case.)</p>
<p><a href="http://www.haskell.org/haskellwiki/Quasiquotation">Quasi-quoting</a> to the rescue.  I recently gave an example of Haskell&#8217;s quasi-quoting abilities in a post about how it can be used to provide an <a href="http://intoverflow.wordpress.com/2010/06/23/syntactic-support-for-kaminskys-interpolique-in-haskell/">injection-proof form of string interpolation</a> (via <a href="http://recursion.com/interpolique.html">Interpolique</a>).  One of my favorite applications is <a href="http://www.snoyman.com">Michael Snoyman</a>&#8216;s <a href="http://docs.yesodweb.com/hamlet/">Hamlet</a>, a type-safe HTML generation library.</p>
<p>(If you&#8217;d like to see what it looks like to implement a quasi-quoter in Haskell, I&#8217;ve got some code up on <a href="http://github.com/intoverflow/InterpoliqueQQ/blob/initial-blog-announce/InterpoliqueQQ.hs">github</a> that demonstrates this in the case of string interpolation, as mentioned above.)</p>
<p>Quasi-quoting is basically syntactic sugar for Template Haskell.  Consequently your quasi-quoters are able to reach into the environment and interact with the rest of the code (all in a type-safe, purely functional way, of course).  In the string interpolation example above, for instance, the code<br />
<code>
<pre>
author = "broker"
content = "' or 1=1;"

query = [$interpolique| insert into posts values(^^author , ^^content ); |]
</pre>
<p></code><br />
set <code>query</code> equal to the following<br />
<code>
<pre>
*Test&gt; query
InterpoliquedString
    " insert into posts values(b64d(\"YnJva2Vy\"), b64d(\"JyBvciAxPTE7\")); "
</pre>
<p></code><br />
which was generated by inspecting the values of <code>author</code> and <code>content</code> <i>at run-time</i>, encoding them in base64, and then interpolating them into the result you see here.  The fact that <code>author</code> and <code>content</code> were strings was determined at <i>compile-time</i>, so there wasn&#8217;t any chance of any shenanigans when the code actually executed.</p>
<p>For instance, if I instead had the code<br />
<code>
<pre>
author = 2 :: Int
content = "' or 1=1;"

query = [$interpolique| insert into posts values(^^author , ^^content ); |]
</pre>
<p></code><br />
I&#8217;d get a compile-time error:<br />
<code>
<pre>
Test.hs:9:23:
    Couldn't match expected type `String' against inferred type `Int'
    In the first argument of `InterpoliqueQQ.b64enc', namely `author'
</pre>
<p></code><br />
which I think is pretty cool.</p>
<h2>Type families and associated types.</h2>
<p>I must admit that I only use the &#8220;associated types&#8221; half of this, although the feature is slightly more general.  Anyway, I&#8217;ll describe the part that I use.</p>
<p><a href="http://www.haskell.org/haskellwiki/GHC/Type_families">Type families</a> give you a way to compute which type you want to use.  Yes, sounds weird, but it&#8217;s amazing.</p>
<p>A typical first example of this is the associated list.  Every modern language has these: it is just an array where the lookup doesn&#8217;t need to be an <code>Int</code> (think <code>HashMap</code> and the like).</p>
<p>In Haskell this can be described like so:<br />
<code>
<pre>
class GenericMap a where
  type Key a
  type Value a
  get :: a -&gt; Key a -&gt; Value a
  set :: a -&gt; Key a -&gt; Value a -&gt; a
</pre>
<p></code><br />
The first two parts of this class definition are the so-called &#8220;associated types.&#8221;  The easiest way to see this in use is with an example of what an instance might look like.  Here I&#8217;ll do something crazy and define the <i>function type</i> <code>String -&gt; Int</code> as an instance of this class (the Haskell Wiki article on <a href="http://www.haskell.org/haskellwiki/GHC/Type_families">type families</a> has other examples, some of which you might find more conventional):<br />
<code>
<pre>
instance GenericMap (String -&gt; Int) where
  type Key (String -&gt; Int) = String
  type Value (String -&gt; Int) = Int
  get f k = f k
  set f k v' = \k' -&gt; if k == k' then v' else f k'
</pre>
<p></code><br />
This instance works:<br />
<code>
<pre>
sampleMap :: String -&gt; Int
sampleMap s = length s

sampleMap' = set sampleMap "foo" 4

...

*Main&gt; get sampleMap "monkey"
6
*Main&gt; get sampleMap "foo"
3
*Main&gt; get sampleMap' "foo"
4
*Main&gt; get sampleMap' "bar"
3
*Main&gt; get sampleMap' "monkey"
6
</pre>
<p></code><br />
which is all well and good.</p>
<p>Now, I haven&#8217;t yet given any reasons why this type families business is any good.  The answer has to do with polymorphism: sometimes you want to write a function whose type signature is <i>so damned flexible</i> you just can&#8217;t figure out how to write it.  You try a few examples, but each is too restrictive.  But there&#8217;s a pattern to it.  If you&#8217;re in this boat, type families can help.</p>
<p>I&#8217;d give an example of this, except I already did in <a href="http://intoverflow.wordpress.com/2010/05/09/polymorphic-first-class-labels/">polymorphic first class labels</a>.  (Which, by the way, is another feature I&#8217;d like to see in other languages.)</p>
<p>Another application of type families is <i>type-level programming</i> (<a href="http://www.haskell.org/haskellwiki/Functional_dependencies">functional dependencies</a> can also be used for this, but as type families get better, my interest in seeing functional dependencies in other languages will dwindle).  Type-level programming is an insane idea where you do computation <i>in the type system at compile-time</i>.</p>
<p>This actually can be helpful in situations where you have really complicated properties you want to express about your program statically.  For instance, I had a situation where certain types had a &#8220;size&#8221; associated to them.  I had functions that were polymorphic over arguments of a given size.  Some of these functions would yield a new type that was twice the size as the input.</p>
<p>How do you express that statically, if the goal is to still be polymorphic?  Type families can do it.  I basically wrote a class whose sole job was to use type families to do arithmetic <i>in the freaking type system</i>.  This basically looked like<br />
<code>
<pre>
class HasSize a where
  type Size a

class Doubler a where
  type Double a

...

-- The ~ operator asserts type equality, so this next
-- line basically reads "the size of b is `Double' the
-- size of a."
someFunction :: ( Size b ~ Double (Size a) ) =&gt; a -&gt; b
someFunction = ...
</pre>
<p></code></p>
<p>I would not describe it as pretty, but it solved my problem, and it gave me a compile-time guarantee that an important design invariant was being met.  The syntax is easy to read as well.  And if it looks like I&#8217;m applying functions to types, it&#8217;s because I am.</p>
<h2>Rank-2 types.</h2>
<p>You don&#8217;t often see this on the list of great things about Haskell, but I love them.  To say that a type is &#8220;rank-2&#8243; is basically a statement about just how polymorphic it is.  I use this feature in two different ways: the first is to solve a polymorphism problem, the second is to <b>prevent tainted data from leaking into places it doesn&#8217;t belong</b> (I&#8217;m in love with this second application and I have <i>no clue</i> how to statically do it in <i>any other language</i> &#8212; tell me in the comments if you do!).</p>
<p>Here is an example of how I use it to get some extra polymorphism:<br />
<code>
<pre>
useFoo :: (forall a . a -&gt; [a]) -&gt; b -&gt; ([b], [String])
useFoo f b = ( f b, f "bar" )
</code></pre>
<p>This function takes two arguments (another function and some other type) and uses them to build a tuple (by applying that function twice).  The <code>forall</code> asserts that the function we give must work for <i>any</i> type <code>a</code>, hence why we can apply it to the mystical input of type <code>b</code> or to an ordinary <code>String</code>.</p>
<p>If I were to rewrite this function without the <code>forall</code> I'd get a type error (two type errors, actually):<br />
<code>
<pre>
useFoo1 :: (a -&gt; [a]) -&gt; b -&gt; ([b], [String])
useFoo1 f a = ( f a, f "bar" )
</pre>
<p></code><br />
gives me<br />
<code>
<pre>
temp.hs:14:18:
    Couldn't match expected type `[Char]' against inferred type `b'
      `b' is a rigid type variable bound by
          the type signature for `useFoo1' at temp.hs:13:25
    In the first argument of `f', namely `a'
    In the expression: f a
    In the expression: (f a, f "bar")

temp.hs:14:23:
    Couldn't match expected type `a' against inferred type `[Char]'
      `a' is a rigid type variable bound by
          the type signature for `useFoo1' at temp.hs:13:12
    In the first argument of `f', namely `"bar"'
    In the expression: f "bar"
    In the expression: (f a, f "bar")
</pre>
<p></code></p>
<p>Absent the <code>forall</code>, the type checker assumes that the function I'm providing works for <i>some</i> type <code>a</code>, and attempts to determine <i>just which type</i> that happens to be.  That is, the compiler is allowing me to be a little ambiguous with my type signature, figuring that there is <i>a particular type</i> I have in mind and that it will use type inference to determine what that would be.  But then I try to use the function on two different types -- <code>b</code> and <code>String</code> -- and therefore is quite upset.  (In fact, it is already upset because, the way I've written the signature for <code>useFoo1</code>, Haskell assumes that <code>a</code> and <code>b</code> must be distinct, and in fact this is what those errors above are telling me: <code>a</code> is not the same as <code>b</code>, nor is it the same as <code>String</code>.)</p>
<p>While this application is nice, as I alluded above, in my mind the killer application is tracking tainted data.  Here are two common scenarios where this is something you want to do:</p>
<ul>
<li>You have some function which accepts untrusted user input, and you want to be certain that whatever value it returns has been scrubbed clean.  This is handy for a function like, say, <code>useUserInputToBuildSQLQuery</code>.  (There are many other ways to solve this problem, of course.)</li>
<li>You have a function which allocates some resources, uses them, then frees them, and you want to make sure it doesn't return a dangling handle.  (I'm not aware of another way of solving this problem, and again would appreciate any comments with other ideas.)</li>
</ul>
<p>The best example of that second scenario is Haskell's <code>ST</code> monad.  Code that executes with the <code>ST</code> monad is able to create mutable variables.  If you have a function that is written in the <code>ST</code> monad, you can execute it using the <code>runST</code> function, whose signature is<br />
<code>
<pre>
Prelude&gt; :m +Control.Monad.ST
Prelude Control.Monad.ST&gt; :t runST
runST :: (forall s. ST s a) -&gt; a
</pre>
<p></code><br />
The key to how this works is the <code>forall</code> in the signature of <code>runST</code>.  In essence, it is preventing code in the <code>ST</code> monad from returning one of these mutable variables.  So the following code works:<br />
<code>
<pre>
{-# LANGUAGE Rank2Types #-}

import Control.Monad.ST
import Data.STRef

exampleST :: ST s Int
exampleST =
     do myMutableVar &lt;- newSTRef 0
        modifySTRef myMutableVar (\n -&gt; n+1)
        n &lt;- readSTRef myMutableVar
        return n

...

*Main&gt; runST exampleST
1
</pre>
<p></code><br />
but the following code does not:<br />
<code>
<pre>
{-# LANGUAGE Rank2Types #-}

import Control.Monad.ST
import Data.STRef

exampleST1 :: ST s (STRef s Int)
exampleST1 =
     do myMutableVar &lt;- newSTRef 0
        modifySTRef myMutableVar (\n -&gt; n+1)
        return myMutableVar

...

*Main&gt; runST exampleST1

:1:0:
    Inferred type is less polymorphic than expected
      Quantified type variable `s' escapes
    In the first argument of `runST', namely `exampleST1'
    In the expression: runST exampleST1
    In the definition of `it': it = runST exampleST1
</pre>
<p></code></p>
<p>People who read my blog will not be surprised when I mention that Oleg Kiselyov and Chung-chieh Shan have shown that this approach can be used to implement <a href="http://lambda-the-ultimate.org/node/2926">region based resource management</a> with good granularity.  (This is a paper I've been bringing up a lot recently, as it is the underpinning of memory management in <a href="http://potential-lang.org">Potential</a>.)</p>
<h2>Conclusion.</h2>
<p>Haskell has a reputation for being hard to learn, though I feel this reputation is a bit dated now that we have good resources like <a href="http://learnyouahaskell.com/">Learn You a Haskell for Great Good</a> and <a href="http://book.realworldhaskell.org/">Real World Haskell</a>.  Certainly one of the hardest parts about learning Haskell is that so many of the examples of "good Haskell" that we hold up rely on many of the features I mentioned above, and most of them seem foreign to new Haskellers.  That's hard to avoid: Haskell is, after all, a research testbed.</p>
<p>I don't know if you'll feel the same way as I do, but after gaining some experience with using these tools in my own code, it is frustrating to leave them behind when working in other languages.  Every language has its seed of grace, elegance, and brilliance that, if it gets into you and grows, will make you into a zealot.  I feel that these are Haskell's seeds.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/502/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=502&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/06/30/haskell-features-id-like-to-see-in-other-languages/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Crypto in the classroom: digital signatures for homework</title>
		<link>http://intoverflow.wordpress.com/2010/06/27/crypo-in-the-classroom-digital-signatures-for-homework/</link>
		<comments>http://intoverflow.wordpress.com/2010/06/27/crypo-in-the-classroom-digital-signatures-for-homework/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 20:10:23 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=365</guid>
		<description><![CDATA[If you don&#8217;t know, I&#8217;m a graduate student at the University of Utah, which means I make a living my teaching classes. Recently a student charged that I lost a good deal of her homework. We wound up in a &#8220;he-said/she-said&#8221; situation where ultimately the dean concluded that we need to raise her grade by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=365&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t know, I&#8217;m a graduate student at the <a href="http://math.utah.edu">University of Utah</a>, which means I make a living my teaching classes.  Recently a student charged that I lost a good deal of her homework.  We wound up in a &#8220;he-said/she-said&#8221; situation where ultimately the dean concluded that we need to raise her grade by a letter under the assumption that I really was up to shenanigans (we ultimately gave her 100% credit in the &#8220;homework&#8221; column in the grade book, raising her grade from F to D).  Not a pleasant situation: aside from a track record of strong teaching evaluations, there was nothing to defend my reputation.</p>
<p>Experienced teachers know that claims of &#8220;lost&#8221; work are frequent.  If we want to be objective about this (and we do), the claims need to be taken seriously, since lost things rarely leave a trail.  All we have when analyzing such claims is the following:</p>
<ul>
<li>The missing work never seems to turn up.  Not after a week, a month, a semester, a year, or ever.</li>
<li>If a person rarely finds that they misplace his own belongings, it&#8217;s hard to accept that he is misplacing student work (assuming they treat student work with a reasonable amount of care, as we typically do, given how terrible it would be to lose it!)</li>
<li>These claims never seem to come from students who are doing well on exams; they tend to come from students who are backed into a corner, grade-wise.</li>
</ul>
<p>Of course, it is entirely conceivable that these claims are occasionally correct, and it would be <b>terrible</b> to allow such mistakes &#8212; <i>our</i> mistakes &#8212; to adversely effect our students.</p>
<p>Last Spring was the only time a student has accused me of losing their work.  It was a lousy situation that I have no intention of ever repeating.  So when I was assigned to teach a half-term class this summer, I decided it was time to try something new.  I&#8217;ve recently finished teaching that class; here&#8217;s what I did.</p>
<p><span id="more-365"></span></p>
<p><b>The idea</b></p>
<p>A <a href="http://en.wikipedia.org/wiki/Digital_signature">digital signature</a> is a cryptographic technique to verify that a document was authored by a particular person.  They are frequently used in situations where someone might later claim to have not authored a document in order to weasel out of a precarious situation.  It is usually the recipient of the document who insists that a digital signature be used.</p>
<p>Digital signatures can be used in other situations where authenticity is important.  For instance, concert tickets can be printed with a digital signature that validates they were printed by the ticketing agent.  The signature on each ticket will need to be different, but this isn&#8217;t hard to arrange.  If the cryptography is sound, no one will be able to forge the signature, hence no one should be able to print a phony ticket.</p>
<p>This summer I experimented with using digital signatures to provide students with a way to <i>prove</i> that they&#8217;ve turned an assignment in.  Every time I made an assignment I produced a corresponding batch of digitally-signed receipts.  One receipt per student per assignment.  I wrote a program to automate this: it takes a list of students and the name of the assignment and produces a PDF file of receipts.  I&#8217;d then print the PDF, take it to the cutting board, and produce an alphabetized stack of receipts for each assignment (the program was clever enough to sort the receipts on the printed page in such a way that the alphabetizing didn&#8217;t require me to sort through the cut up pages).</p>
<p>I then instituted a policy: I won&#8217;t take your homework unless you take your receipt.  Because the receipts were sorted this didn&#8217;t take long to do (though to save class time, I only accept homework before and after class, or during problem sessions).</p>
<p>I&#8217;d then periodically create printed grade reports for each individual (again I had a program that automated this, taking data from my spreadsheet and turning it into a PDF with one page per student).  This allowed students to check whether or not I was giving them credit for their work.  At the end of the term, I gave them one last report that showed the grade I was going to submit for them.  No one can claim that I was withholding information.</p>
<p>The wonderful thing about this system is that it gives the students proper recourse: if it looks like I lost an assignment, the receipt proves they are right, and I&#8217;ll give them full credit.  Because the receipts are digitally-signed, they cannot be forged, so there&#8217;s no funny business to be had by any bad apples.</p>
<p><b>Reception</b></p>
<p>Naturally I described my plan to the bosses in the math department.  My department chair conjectured that no one on earth had tried this before.  The associate chair laughed at the lengths I was going through.  My peers suspected that the students would find this system confusing, annoying, and unnecessary.</p>
<p>When I implemented this system the results were fantastic.  I don&#8217;t believe anyone in class understood how the digital signatures worked, but they didn&#8217;t need to: all they had to know is that the receipts give them recourse if I lose their work.  And they loved it.  Why wouldn&#8217;t they?  It was apparent that I was doing extra work to expose myself for their sake (nevermind that I was originally motivated to try this to avoid getting in hot water myself).</p>
<p>Contrary to initial predictions that I would be mired down in extra paperwork, this process did not take much time on my end.  Once I had the programs written to automate the work (which took an afternoon) the time investment was nearly zero.  The most time consuming part was passing out the receipts as students turned in their work, but since this was taking place during problem sessions, it didn&#8217;t actually increase my working time.</p>
<p>For me the most interesting thing is that I <i>did</i> apparently lose someone&#8217;s assignment!  I have no clue how, and part of me still hasn&#8217;t accepted this, but the evidence suggests that it happened.  I gave someone a grade report, it said they were missing an assignment, but sure enough they had a receipt.  They called me on it in front of the class, and when I reaffirmed my promise to give them credit, a couple people in the class <i>praised</i> me.  The receipt system actually took <i>my mistake</i> and turned it into an <i>asset</i>.  Incredible.</p>
<p><b>Edit</b>: this is what a sample of my receipt-PDF looks like (here shown with just a small number of hypothetical students).  Notice how it&#8217;s sorted &#8212; makes it very easy to just go to the chopping board, stack the pages, make 4 cuts, and concatenate the little stacks.</p>
<a href="http://intoverflow.files.wordpress.com/2010/06/homework-15.png"><img src="http://intoverflow.files.wordpress.com/2010/06/homework-15.png?w=231&#038;h=300" alt="" title="Homework 15 Sample Receipts, Page 1" width="231" height="300" class="size-medium wp-image-499" /></a>
<a href="http://intoverflow.files.wordpress.com/2010/06/homework-15-2.png"><img src="http://intoverflow.files.wordpress.com/2010/06/homework-15-2.png?w=231&#038;h=300" alt="" title="Homework 15 Sample Receipts, Page 2" width="231" height="300" class="size-medium wp-image-498" /></a>
<p><b>How I did it</b></p>
<p>There&#8217;s a considerable about of machinery that goes into such a scheme.  If you&#8217;re interested in implementing something like this, and have a good familiarity with computing, you can follow these steps to get rolling.  For all of this I&#8217;m using <a href="http://www.openssl.org/">openssl</a>, which is installed by default on my Mac, and also on every Linux distribution I&#8217;ve ever used.  I&#8217;ll show how this is done using a test receipt.</p>
<p><b>Step 1:</b> Create the key files.  Create a directory to do your work in.  Within this directory, issue the following two commands:</p>
<p><code><br />
$ openssl genrsa -out private.pem 1024<br />
$ openssl rsa -in private.pem -out public.pem -outform PEM -pubout<br />
</code></p>
<p>This will create a <i>public</i> and a <i>private</i> file, so-named by <code>openssl</code>.  The public file can be put on your website, or included in your syllabus, allowing a 3rd party to arbitrate any disputes that may arise.  The private file must be kept private, as it is the key to signing the receipts.</p>
<p><b>Step 2:</b> Create a test receipt to work with.  Later you&#8217;ll do this step for each student, for each assignment (consider using a script to do this for you!).  Here&#8217;s my test receipt:</p>
<p><code><br />
$ cat Test-receipt.txt<br />
FAKE, STUDENT ASSIGNMENT 0001<br />
</code></p>
<p><b>Step 3:</b> Sign the receipt, putting the output into base64 (this will allow you to print the signed receipt so you can give it to your student in writing)</p>
<p><code><br />
$ openssl rsautl -sign -inkey private.pem -in Test-receipt.txt |<br />
openssl enc -base64 -out Test-receipt.sig<br />
</code><br />
You can examine the output:</p>
<p><code><br />
$ cat Test-receipt.sig<br />
hRqaY5LAns3CrzueaMXirehihYCn6TI6K4Luwo9T6F4JVMXiBb10wSN4fDLnM12m<br />
NICQihiAt5prlqDxjwqpr2J4tPMmQZpXr8dpFKdyQgxn6IesLiEm9HIVjYUELRMW<br />
kzxv86+8oVl6qQny+kMVWo3w7pI/JTTnHP3yLl1NJJw=<br />
</code><br />
When you go to print your student&#8217;s receipt, include both the contents of the receipt (in my case, <code>Test-receipt.txt</code>) as well as this gibberish.</p>
<p><b>Step 4:</b> Make sure you know how to verify a receipt!  This isn&#8217;t so bad.  If someone gives you their receipt, you enter the gibberish into a file (in this case, <code>Test-receipt.sig</code>) and execute the following:</p>
<p><code><br />
$ cat Test-receipt.sig | openssl enc -base64 -d | openssl rsautl -verify<br />
 -pubin -inkey public.pem<br />
FAKE, STUDENT ASSIGNMENT 0001<br />
</code></p>
<p>If the signature is correct (that is, not entered wrongly, nor an invalid forgery) you should see the receipt in plain text, as demonstrated here.</p>
<p>As an example, if I modify even a single letter in <code>Test-receipt.sig</code>, I&#8217;ll wind up with something that makes no sense, or more likely, I&#8217;ll get an error.  For instance, if I replace the first letter (a lower-case <code>h</code>) with an upper case <code>H</code>, I get the following:</p>
<p><code><br />
$ cat Test-receipt.sig-error | openssl enc -base64 -d | openssl rsautl -verify -pubin -inkey public.pem<br />
RSA operation error<br />
286:error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01:/SourceCache/OpenSSL098/OpenSSL098-27/src/crypto/rsa/rsa_pk1.c:100:<br />
286:error:04067072:rsa routines:RSA_EAY_PUBLIC_DECRYPT:padding check failed:/SourceCache/OpenSSL098/OpenSSL098-27/src/crypto/rsa/rsa_eay.c:697:<br />
</code></p>
<p>This will allow you to detect cheap forgeries.</p>
<p><b>Conclusions</b></p>
<p>This summer I ran this experiment with a class of 55 students, and in this setting, homework collection took about 15 minutes.  It worked fantastically.  In the Fall I&#8217;m teaching a group of 180.  The turn-in time might make this plan infeasible for such a large group unless I can have my TA&#8217;s come to class to help collect work.  I&#8217;ll definitely post my experience with this group in the comments to this post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/365/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/365/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/365/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=365&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/06/27/crypo-in-the-classroom-digital-signatures-for-homework/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>

		<media:content url="http://intoverflow.files.wordpress.com/2010/06/homework-15.png?w=231" medium="image">
			<media:title type="html">Homework 15 Sample Receipts, Page 1</media:title>
		</media:content>

		<media:content url="http://intoverflow.files.wordpress.com/2010/06/homework-15-2.png?w=231" medium="image">
			<media:title type="html">Homework 15 Sample Receipts, Page 2</media:title>
		</media:content>
	</item>
		<item>
		<title>Syntactic support for Kaminsky&#8217;s Interpolique in Haskell</title>
		<link>http://intoverflow.wordpress.com/2010/06/23/syntactic-support-for-kaminskys-interpolique-in-haskell/</link>
		<comments>http://intoverflow.wordpress.com/2010/06/23/syntactic-support-for-kaminskys-interpolique-in-haskell/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 00:22:58 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=474</guid>
		<description><![CDATA[When I recently wrote about my first impressions of Kaminsky&#8217;s Interpolique, I mentioned that the only thing I didn&#8217;t like is that PHP doesn&#8217;t offer any way to protect against syntactic mistakes, such as where the programmer mistakenly uses a $ instead of a ^^. Today we&#8217;ll look at how Interpolique can be implemented in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=474&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When I recently wrote about my <a href="http://intoverflow.wordpress.com/2010/06/17/still-solving-string-injection/">first impressions</a> of Kaminsky&#8217;s <a href="http://recursion.com/interpolique.html">Interpolique</a>, I mentioned that the only thing I <i>didn&#8217;t</i> like is that PHP doesn&#8217;t offer any way to protect against syntactic mistakes, such as where the programmer mistakenly uses a <code>$</code> instead of a <code>^^</code>.</p>
<p>Today we&#8217;ll look at how Interpolique can be implemented in Haskell in such a way that we <b>force</b> the developer to use Interpolique when creating a SQL query, precluding the possibility of the <code>$</code>/<code>^^</code> mixup bug.  In doing so we&#8217;ll see that we <i>don&#8217;t</i> need anything like PHP&#8217;s <code>eval</code> to get the job done.</p>
<p>All of the code for this post is on <a href="http://github.com/intoverflow/InterpoliqueQQ/tree/initial-blog-announce">github: InterpoliqueQQ</a>.</p>
<p><span id="more-474"></span></p>
<p>Since version 6.10 of the <a href="http://www.haskell.org/ghc/">Glorious Haskell Compiler</a>, we have had the ability to essentially <i>define new language syntax</i>.  This functionality &#8212; called <a href="http://www.haskell.org/haskellwiki/Quasiquotation">quasi-quotation</a> &#8212; is useful for embedding mini-languages into Haskell in a type-safe way.  Since Interpolique is basically a mini-language (it&#8217;s only operator is the <code>^^</code> interpolator), it is natural to use quasi-quotation when implementing Interpolique in Haskell.</p>
<p>Let&#8217;s look at an example of what this looks like when it&#8217;s being used.  On the Interpolique site, an example of an attempted <a href="http://recursion.com/interpolique_sql.html">SQL injection</a> is given.  The crux of the example is the following code:<br />
<code>
<pre>
$conn-&gt;query(eval(b('insert into posts values(^^_POST[author] , ^^_POST[content] );')));
</pre>
<p></code><br />
In InterpoliqueQQ (see <a href="http://github.com/intoverflow/InterpoliqueQQ/blob/initial-blog-announce/Test.hs"><code>Test.hs</code></a> in the InterpoliqueQQ code), the same code can be written as<br />
<code>
<pre>
query = [$interpolique| insert into posts values(^^author , ^^content ); |]
</pre>
<p></code><br />
If we hop into interactive-mode with GHC, we can see the value of <code>query</code>:<br />
<code>
<pre>
*Test&gt; query
InterpoliquedString " insert into posts values(b64d(\"Zm9v\"), b64d(\"JyBvciAxPTE7\")); "
</pre>
<p></code><br />
Thus the run-time value of <code>query</code> is, in fact, an Interpolique&#8217;d SQL query.</p>
<p><b>Why this is interesting</b></p>
<p>The important feature of InterpoliqueQQ is that this syntax offers protection in the form of <b>static typing</b>.  If we inspect the type of <code>query</code> we get<br />
<code>
<pre>
*Test&gt; :t query
query :: InterpoliqueQQ.InterpoliquedString
</pre>
<p></code><br />
That is, this syntax creates a query whose type is <code>InterpoliquedString</code>.  In this implementation of Interpolique, the <b>only way</b> to obtain an instance of <code>InterpoliquedString</code> is <b>via this syntax</b>.  In other words, if a function is given a query of type <code>InterpoliquedString</code>, it can be <b>completely cetain</b> that the query was generated using Interpolique.  Since this syntax does not allow PHP-style string interpolation (that is, there is no analogue of <code>'insert into posts values($author, $content)</code>), there is no way for a developer to introduce a SQL injection bug due to a misused interpolation operator.</p>
<p>(We can also note that InterpoliqueQQ does not use anything similar to PHP&#8217;s <code>eval</code>, thereby rendering any objection to the presence of <code>eval</code> moot.)</p>
<p><b>The implementation</b></p>
<p>InterpoliqueQQ is <a href="http://github.com/intoverflow/InterpoliqueQQ/blob/initial-blog-announce/InterpoliqueQQ.hs">implemented</a> in Haskell in less than 75 lines of code, relying on the powerful <a href="http://www.cs.uu.nl/~daan/parsec.html">parsec</a> parser combinator library, <a href="http://www.haskell.org/haskellwiki/Quasiquotation">GHC&#8217;s quasi-quotation support</a>, and the base64 encoder of the <a href="http://www.haskell.org/haskellwiki/Library/Data_encoding">dataenc</a> library.</p>
<p>This implementation is entirely proof-of-concept.  In particular, it&#8217;s missing two things:</p>
<ol>
<li>Field-testing.  Interpolique hasn&#8217;t (yet) been out long enough for peer review to have run its course, so certainly nothing can be said about whether or not this particular implementation is secure.</li>
<li>Library support.  This implementation is built on the <code>InterpoliquedString</code> type.  In order for this to be useful, there needs to be a SQL library which is ready to act on this type.  For the time being, I&#8217;ve included the <code>runQuery</code> function in <code>InterpoliqueQQ</code> which just takes an <code>InterpoliquedString</code> and prints (to <code>stdout</code>) the corresponding SQL code, as in<br />
<code>
<pre>
*Test&gt; runQuery query
 insert into posts values(b64d("Zm9v"), b64d("JyBvciAxPTE7"));
</pre>
<p></code></li>
</ol>
<p>At present time Haskell is a decidedly <b>non-standard</b> language to use for web development.  Examples like this, however, suggest that it could be a powerful tool in this domain in the future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/474/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/474/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/474/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=474&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/06/23/syntactic-support-for-kaminskys-interpolique-in-haskell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>First impressions: Serving statically with Snap</title>
		<link>http://intoverflow.wordpress.com/2010/06/20/first-impressions-serving-statically-with-snap/</link>
		<comments>http://intoverflow.wordpress.com/2010/06/20/first-impressions-serving-statically-with-snap/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 22:47:57 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=467</guid>
		<description><![CDATA[(This post refers to Snap 0.2.6.) There&#8217;s been a lot of buzz about the Snap framework, so I thought I&#8217;d give it a look. My personal website doesn&#8217;t have anything dynamic going on, so arguably Snap is &#8220;overkill,&#8221; but then again so is Apache, so what the heck. Snap is entirely experimental at this time: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=467&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(This post refers to Snap 0.2.6.)</p>
<p>There&#8217;s been a lot of buzz about the <a href="http://snapframework.com/">Snap framework</a>, so I thought I&#8217;d give it a look.  My <a href="http://www.ninj4.net">personal website</a> doesn&#8217;t have anything dynamic going on, so arguably Snap is &#8220;overkill,&#8221; but then again so is <a href="http://www.apache.org">Apache</a>, so what the heck.  Snap is entirely experimental at this time: in their own words, &#8220;it is <b>early-stage software</b>,&#8221; so every single critique given here should be read with an implied expiration date.</p>
<p>So the question is: how does one host a static site on Snap?  At present time there&#8217;s no tutorial for this, so I fumbled around until I got something working.  Here&#8217;s my code:</p>
<p><code>
<pre>
main = do
    putStrLn "ninj4net online"
    quickServer config site

site :: Snap ()
site =
    route [ ("kinetic", static "kinetic")
          , ("math1010", static "math1010")
          , ("math1030", static "math1030")
          , ("math1100", static "math1100")
          , ("math1210", static "math1210")
          , ("", static "")
          ]
    (writeBS "general error")

static d = do
    let html_file = "static/" ++ d ++ "/index.html"
        xml_file  = "static/" ++ d ++ "/index.xml"
    html &lt;- liftIO $ doesFileExist html_file
    xml  &lt;- liftIO $ doesFileExist xml_file
    ( (ifTop (fileServeSingle $ if html then html_file else xml_file))
      (fileServe $ "static/" ++ d) )
</pre>
<p></code></p>
<p><b>Discussion</b></p>
<p>Some of my directories use an <code>index.html</code> file, while others use an <code>index.xml</code> file.  I need to use <code>System.Directory.doesFileExist</code> function to determine whether or not these files exist &#8212; trying <code>ifTop (fileServeSingle "something that doesn't exist")</code> will <b>not</b> switch to the alternative using <code>&lt;|&gt;</code>, so an explicit check is needed (it will whine about an exception being thrown, completely undermining the choice operator!).  This is presumably a bug (I submitted it to their <a href="http://github.com/snapframework/snap-core/issues/issue/5">github</a>).</p>
<p>I&#8217;m sure there is a much more elegant approach, but this was the best I could muster during lunch.</p>
<p><b>A thing Snap is missing</b></p>
<p>I came across a design decision that makes me nervous.  As with any web framework, there are facilities for getting strings from the user.  Unfortunately, Snap does <i>not</i> use types to distinguish between user-provided strings (dirty) and programmer-provided strings (clean).</p>
<p>Why does this matter?  Segregating user input into its own type is a formidable defense against (say) SQL injection, since it obviates that <code>"select * from myData where foo='" ++ userInput ++ "'"</code> isn&#8217;t well-typed (presumably SQL code should be its own type, say, <code>SQLString</code>, and <b>the</b> function <code>UserString -&gt; SQLString</code> should be some kind of escape routine).  It would be nice to see framework support for this types-based defense.</p>
<p>The most obvious example of this is in <a href="http://snapframework.com/docs/latest/snap-core/Snap-Types.html#v%3AgetParam">getParam</a>, which simply returns a <code>Maybe ByteString</code>.</p>
<p>Another example is provided by <a href="http://snapframework.com/docs/latest/snap-core/Snap-Util-FileServe.html#v%3AgetSafePath"><code>getSafePath</code></a> and <a href="http://snapframework.com/docs/latest/snap-core/Snap-Util-FileServe.html#v%3AfileServeSingle"><code>fileServeSingle</code></a>.  The former returns a <code>FilePath</code> provided by the user (a &#8220;safe&#8221; path, which &#8212; looking at the source &#8212; means that the &#8220;/../&#8221;&#8216;s get removed), and the latter takes a <code>FilePath</code> and opens the corresponding local file.  I suppose the idea is that the code<br />
<code>
<pre>
do p &lt;- getSafePath
   fileServeSingle p
</pre>
<p></code><br />
shouldn&#8217;t escape the implied sandbox of the file system.  Of course, if the application has tighter requirements than this, the type system isn&#8217;t there to help out.  (For instance, perhaps a path is considered &#8220;safe&#8221; if it excludes certain keywords <i>in addition</i> to the constraints imposed by <code>getSafePath</code>).</p>
<p>A natural work-around is to build an application-specific wrapper around Snap, and perhaps this is the better approach; I&#8217;m not yet sure.</p>
<p><b>Conclusions</b></p>
<p>I&#8217;m glad that Snap has been announced, as it has proven interesting to look at.  Of course, Haskell already has (at least) two other web frameworks (<a href="http://docs.yesodweb.com/yesod/">yesod</a> and <a href="http://happstack.com/index.html">happstack</a>) and it&#8217;s not clear which will win-out in mindshare, nor is it obvious (to me, anyway) which one would be the best choice for someone wanting to sit down and make a site.  Of course, it&#8217;s possible that some mix-and-match might be the best approach: the web server of one project, the HTML generation of another, and the persistent storage of the third.  (This possibility deserves some consideration, especially as projects like <a href="http://jaspervdj.be/posts/2010-04-28-blazehtml-initial-results.html">BlazeHTML</a> really take off.)  Hopefully in the coming months we&#8217;ll see more high-powered applications of these frameworks, giving us a fountain of lessons we can capitalize on, and providing some compelling show cases of Haskell&#8217;s power as a web development language.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=467&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/06/20/first-impressions-serving-statically-with-snap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Still solving string injection: first impressions of Kaminsky&#8217;s Interpolique</title>
		<link>http://intoverflow.wordpress.com/2010/06/17/still-solving-string-injection/</link>
		<comments>http://intoverflow.wordpress.com/2010/06/17/still-solving-string-injection/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 04:15:54 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=451</guid>
		<description><![CDATA[This past week Dan Kaminsky announced Interpolique, a technology for dealing with string injection problems in web applications. The basic idea is pretty sharp: instead of writing (say) PHP code like $conn-&#62;query('insert into posts values($_POST[author] , $_POST[content] );'); we write $conn-&#62;query(eval(b('insert into posts values(^^_POST[author] , ^^_POST[content] );'))); The b function is provided by interpolique. It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=451&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This past week Dan Kaminsky announced <a href="http://recursion.com/interpolique.html">Interpolique</a>, a technology for dealing with string injection problems in web applications.  The basic idea is pretty sharp: instead of writing (say) PHP code like<br />
<code>
<pre>
$conn-&gt;query('insert into posts values($_POST[author] , $_POST[content] );');
</pre>
<p></code><br />
we write<br />
<code>
<pre>
$conn-&gt;query(eval(b('insert into posts values(^^_POST[author] , ^^_POST[content] );')));
</pre>
<p></code><br />
The <code>b</code> function is provided by interpolique.  It essentially translates the input string into some PHP code (which is then reified using <code>eval</code>) that base64 encodes the user-input and wraps that encoding up in a call to the MySQL function for base64 decoding.</p>
<p>The idea is that the resulting query is given to MySQL in a format where the user input is base64 encoded.  As Dan <a href="http://www.scribd.com/doc/33001026/Interpolique">points out</a>, there aren&#8217;t any known injection techniques that can escape the MySQL base64 decoder, and the decoder won&#8217;t try to evaluate the resulting string as a SQL expression, so no injection is possible.</p>
<p>I have mixed feelings about this approach.  On the one hand, it&#8217;s really just another form of escaping (instead of inserting a bunch of <code>\</code>&#8216;s into the string, we&#8217;re base64 encoding it), and escaping is an error-prone thing.  After all, there&#8217;s nothing preventing a tired developer from accidentally mixing some <code>$</code> in with their <code>^^</code>, nor could there be &#8212; if the developer writes <code>$</code> instead of <code>^^</code>, PHP will interpolate the string before passing it off to the <code>b</code> function, so no run-time check will be able to save the day.</p>
<p>(I&#8217;m not elated about the use of <code>eval</code>, but (a) I see no way around it if the plan is to use a syntactic approach, as is currently the case, and (b) the only vector I can see for attacking it requires a programmer to leave out the call to <code>b</code>, which is something they&#8217;d likely catch during development unless they also used <code>$</code> instead of <code>^^</code>, and that double-accident seems unlikely, barring a stupid refactoring snafu.)</p>
<p>On the other hand, if this technique is applied correctly, it seems likely to be robust (peer review should weigh in on this pretty quickly).</p>
<p>When I look at interpolique, I see the next generation of escaping: if you forget to do it you&#8217;re screwed, but if you do it correctly you&#8217;re safe.  interpolique&#8217;s contribution is that its style of escaping is much simpler than trying to scan strings for dangerous characters, hence less likely to contain silly errors and edge cases, and that it is cross-language ready, in that base64 encoding isn&#8217;t target-language-specific (unlike escaping, which certainly is).</p>
<p>interpolique does not improve upon escaping biggest failure, though: if you&#8217;ve got 50,000 lines of PHP, the only way to know that interpolique (or escaping) is being used throughout is to look through the code.  This is a PHP shortcoming, of course.  We could certainly produce some static analysis tool to check for this design pattern, but then again, if writing tools that understand strings in PHP were easy we wouldn&#8217;t have the code injection mess in the first place.</p>
<p><b>Future direction</b></p>
<p>interpolique does provide a novel improvement for how to move data across the language barrier.  This makes the core idea useful even in situations where programmers aren&#8217;t using PHP (or other half-brained-but-common-anyway languages).</p>
<p>In the long term, however, we still need to address the fact that we&#8217;re abusing the <code>String</code> type.  <b>User-input should be its own distinct type.</b>  The fact that this isn&#8217;t the case in .Net and Java completely explains why those type-safe languages don&#8217;t fare any better than PHP in terms of code injection.</p>
<p>Following the interpolique idea, the only function from <code>UserString</code> to <code>String</code> could be a base64 encoder.  Languages could provide syntactic sugar to allow things like<br />
<code>
<pre>
$conn-&gt;query('insert into posts values($_POST[author] , $_POST[content] );');
</pre>
<p></code><br />
to implicitly denote the interpolique style, thereby preserving type-safety (in this case, separation of user-input from SQL code) without compromising string interpolation style.</p>
<p>(Of course, both of these ideas are already possible in Haskell using algebraic data types and Template Haskell, but this is of little comfort to the vast majority of programmers since (a) most haven&#8217;t heard of Haskell and (b) Haskell is still in its web-development-language infancy.)</p>
<p><b>Moving forward</b> I am interested in seeing whether interpolique passes peer review (probably will), becomes a common practice and reduces the incidence of code injection.  Regardless of how these questions fare, the core idea is elegant, doesn&#8217;t seem to have a performance penalty, and can likely be carried forward fruitfully in future technologies.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/451/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/451/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/451/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=451&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/06/17/still-solving-string-injection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Announcing Potential: x86-64 assembler as a Haskell EDSL</title>
		<link>http://intoverflow.wordpress.com/2010/05/21/announcing-potential-x86-64-assembler-as-a-haskell-edsl/</link>
		<comments>http://intoverflow.wordpress.com/2010/05/21/announcing-potential-x86-64-assembler-as-a-haskell-edsl/#comments</comments>
		<pubDate>Fri, 21 May 2010 20:17:31 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Potential]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=385</guid>
		<description><![CDATA[Over the years there have been many projects which seek to use advanced types to provide better static-guarantees in low level languages. There are many examples of this in the literature; here are just a few: Cyclone, perhaps the most-cited example of using types to protect memory in low-level settings. Habit, a proposed Haskell dialect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=385&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Over the years there have been many projects which seek to use advanced types to provide better static-guarantees in low level languages.  There are many examples of this in the literature; here are just a few:</p>
<ul>
<li><a href="http://cyclone.thelanguage.org/">Cyclone</a>, perhaps the most-cited example of using types to protect memory in low-level settings.</li>
<li><a href="http://hasp.cs.pdx.edu/">Habit</a>, a proposed Haskell dialect which uses a viable form of dependent types to model low-level data structures and their memory management which I recently learned about on reddit, coincidentally in a <a href="http://www.reddit.com/r/haskell/comments/c5oby/optional_class_constraints_in_haskell_integer/c0qbqft">comment</a> on an earlier post of mine.</li>
<li>Some work due to <a href="http://okmij.org/ftp/Haskell/types.html#ls-resources">Oleg Kiselyov and Chung-chieh Shan</a> showing that Haskell is a viable setting for embedding a low-level language.</li>
</ul>
<p>(If you know of others, put them in the comments!)</p>
<p>In December of 2009 I became interested in typed assembly languages and began working on my own, quickly deciding to embed the language into Haskell.  At present there are many facets of the language which work well, but there is still a good deal of work to be done.</p>
<p>When I started this, I was unaware of much of the work that had already been done in this direction (I especially wish I had been aware of the Kiselyov-Shan proof of concept).  In the course of the project, I&#8217;ve learned a bunch of great tricks, and also have learned of a good deal of excellent work done by others that I hadn&#8217;t seen promoted elsewhere.  Over the next few posts, my goals are threefold:</p>
<ul>
<li>To describe my own project: where it is, what the challenges have been, where I hope for it to go.  And to release some source (on <a href="http://github.com/intoverflow/Potential/tree/initial-blog-announce">github</a>), such as it is.</li>
<li>To describe some of the general lessons I&#8217;ve learned working on an EDSL in Haskell, to evangelize this approach to problem solving, and to describe some caveats to conventional Haskell wisdom in this setting.</li>
<li>To promote some of the work others have done in this area, in the aim of showing just how far along this idea is.</li>
</ul>
<p><span id="more-385"></span></p>
<p><b>The <i>Potential</i> programming language</b></p>
<p><i>Potential</i> is an adaptation of x86-64 assembly language as a domain specific language, embedded in Haskell.  It&#8217;s ultimate purpose is to provide a setting in which to implement a practical operating system kernel (the next iteration of the <a href="http://intoverflow.wordpress.com/kinetic/">Kinetic</a> project).  The basic idea is pretty simple:</p>
<ul>
<li>Functions are written in Haskell which more or less correspond to many of the mnemonics in assembly language.</li>
<li>The functions carry type signatures which encode the effect that the mnemonic has on machine state, in the form of an <a href="http://blog.sigfpe.com/2009/02/beyond-monads.html">indexed (or parametrized) monad</a>.</li>
<li>These functions can be composed to form blocks of code.  Haskell type inference can be used to determine the assumptions the code blocks make, as well as the condition they leave the machine in upon return.</li>
<li>When these functions are executed, they output abstract syntax for x86-64 assembly, which can then be pretty-printed to a <code>.asm</code> file, which is then passed onto a toolchain for actually compiling the code.</li>
</ul>
<p>Potential provides the following:</p>
<ul>
<li>An ability to safely handle bit-fields.  Bit fields can be defined using <a href="http://www.haskell.org/haskellwiki/Template_Haskell">Template Haskell</a>, which will automatically generate code for getting and setting elements of the bit field.  Potential uses type-level integers to keep track of the sizes of fields, thereby providing a static guarantee that data isn&#8217;t getting truncated along the way.  (This idea has also been proposed independently in <a href="http://hasp.cs.pdx.edu/">Habit</a>).  Potential is able to use this static data to compute the shifts and bit masks necessary for updating and using bit fields.</li>
<li>Higher data structures (those built out of bit-fields) are commonplace in the x86-64 standard, and Potential provides a syntax for assembling these as well.  Syntax is also provided for defining arrays of such structures, as is needed (for instance) in defining the interrupt descriptor table.</li>
<li>A form of linear types, inspired directly by Kiselyov and Shan&#8217;s <a href="http://okmij.org/ftp/Haskell/regions.html#light-weight">Lightweight monadic regions</a>, for managing pointers.</li>
<li>Overloaded <code>&gt;&gt;</code>, <code>&gt;&gt;=</code>, and <code>return</code> operators for use with <code>do</code> notation, allowing programmers to write their assembler in Haskell&#8217;s imperative syntax.</li>
<li>Predefined versions of the important x86-64 data structures, as well as instructions for making type-level assertions about the state of the hardware.  (For instance: are interrupts disabled?  Is paging enabled?  Is the current privilege level Kernel mode?)</li>
<li>Type-level, static tracking of many assembly language subtleties.  For instance, the assembly comparison mnemonic (<code>cmp</code>) releases a variable to the programmer which is then passed to the conditional mnemonics (such as <code>je</code>) as a way of demonstrating that the correct comparison is being examined.  As an example, the code sequence<br />
<blockquote><p><code>
<pre>
compare1 &lt;- cmp rax rbx
compare2 &lt;- cmp r09 r10
sje &lt;some function&gt; compare1
</pre>
<p></code></p></blockquote>
<p>fails to type check because Potential knows that the result of the comparison labeled by <code>compare1</code> is no longer stored in the CPU&#8217;s flags register.  As more of assembly gets introduced into Potential, there will be more examples of this type of subtlety being brought directly to the programmer&#8217;s eyes, all modeled by types.</li>
<li>Every good assembly language needs a good macro language to scrap boiler plate.  In the case of Potential, all of Haskell is available as the macro language.</li>
<li>Since the language is embedded in Haskell, the result is an assembly language which is essentially as polymorphic as Haskell is, including support for Haskell type class machinery.</li>
</ul>
<p>All of the above is (at least partially) implemented today.  The next major step (according to the plan) is to expand the the memory system so that it can deal with the complexities of paged memory.</p>
<p>Here are just a few of the simple things that are (in some cases, should be) possible in Potential:</p>
<ul>
<li>Code which must execute with interrupts disabled can assert this requirement on the level of types; Haskell will verify that no function invokes such code without first disabling interrupts.</li>
<li>The processor flag which enables paged memory cannot be enabled unless the current interrupt descriptor table has a present vector for handling page faults.</li>
<li>The task-switching interrupt handler can assert that processor state is preserved between task changes.</li>
<li>If two registers carry pointers to the same address, and one of the pointers is used to modify a data structure in a way which modifies its type, the other pointer is implicitly invalidated, and subsequent attempts to use it will result in an error detected by the Haskell type system.</li>
</ul>
<p>The language is very much still a work in progress with large amounts of rework taking place every few days (owing to the complexity of getting the types to line up with expectations <i>and</i> getting them to be inferred with the appropriate balance of generality and specificity).</p>
<p><b>Some examples of code written in <i>Potential</i></b></p>
<p>Let&#8217;s look at some code which is used to modify an interrupt gate.  Interrupt gates are used in x86-64 to describe interrupt handlers to the CPU.  Structurally, they consist of a small number of bitfields which are used to indicate things like the privilege level that the interrupt handler should execute as (called the &#8220;descriptor privilege level,&#8221; or DPL).  We&#8217;ll look at this particular field in these examples.</p>
<p>In Potential, the interrupt gate data structure is defined like so:</p>
<blockquote><p><code>
<pre>
intGate = mkStruct "InterruptGate"
                   [ (field 16 "offset_lo")
                   , (field 16 "segsel")
                   , (field 3 "ist")
                   , ($(constField 2) CB0 CB0)
                   , ($(constField 3) CB0 CB0 CB0)
                   , ($(constField 4) CB1 CB1 CB1 CB0)  -- defines Interrupt Gate type
                   , ($(constField 1) CB0)
                   , (field 2  "dpl")
                   , (field 1  "p")
                   , (field 16 "offset_mid")
                   , (field 32 "offset_hi")
                   , ($(reservedField 32))
                   ]
</pre>
<p></code></p></blockquote>
<p>We see the use of some Template Haskell as a matter of convenience (to allow for the <code>constField</code> variadic function).  This defines various named fields that can be modified at run-time, as well as some constant fields, and some areas which are reserved for future changes to the x86-64 platform.  As is the rule with Template Haskell, this code resides in a module (<code>IntGateStruct</code>) which is, in turn, loaded by another module (<code>IntGate</code>) that reifies it.</p>
<p>Once the <code>IntGate</code> module reifies the structure, Template Haskell is used to define a sequence of new types and top level definitions that can be used to modify <code>InterruptGate</code> structures.  It introduces a new <code>InterruptGate</code> type:</p>
<blockquote><p><code>
<pre>
&gt; :info InterruptGate
newtype InterruptGate offset_lo segsel ist dpl p offset_mid offset_hi
  = InterruptGate (offset_lo,
                   segsel,
                   ist,
                   dpl,
                   p,
                   offset_mid,
                   offset_hi)
  	-- Defined at Potential/Machine/IntGate.hs:26:0-19
</pre>
<p></code></p></blockquote>
<p>and provides us with some tools we can use to modify such types.</p>
<p>The <code>setField</code> instruction, for instance, is an example of a Haskell-built macro in Potential that can be used to update fields within such structures.  Using it, we&#8217;re able to write a function that can be used to update the DPL of an interrupt gate:</p>
<blockquote><p><code>
<pre>
-- rax contains Ptr64 to interrupt gate
-- rbx contains the new DPL
setDPL = asCode "setDPL" $
     do push rcx
        push rbx
        setField dpl rax rbx rcx
        pop rbx
        pop rcx
        ret
</pre>
<p></code></p></blockquote>
<p>Here some (Haskell) code comments document that <code>rax</code> is used to pass in pointer to the interrupt gate we want to update, and <code>rbx</code> contains the new value for the DPL.  The <code>setField</code> macro also needs a register to temporarily use: in this case, we&#8217;ve chosen <code>rcx</code>, whose value we are preserving by using the stack (the macro will also modify <code>rbx</code>, so we&#8217;re using the stack to preserve that value as well).</p>
<p>We can then ask Potential to translate this code into AT&amp;T-syntax assembler (of course, we could modify the pretty-printing to use a different syntax).  Here&#8217;s the result of that output (presuming I haven&#8217;t many any silly errors in the printer, this should be the right sequence of instructions for performing this operation &#8212; if you notice anything to the contrary, let me know!)</p>
<blockquote><p><code>
<pre>
&gt; renderFn setDPL
setDPL:
    push %rcx
    push %rbx
    // updating field "dpl" at (%rax) using value in %rbx, with %rcx as a temp register
    mov 0(%rax) %rcx
    shl 45 %rbx
    and 0xffff9fffffffffff %rcx
    or %rbx %rcx
    mov %rcx 0(%rax)
    // update complete
    pop %rbx
    pop %rcx
    ret
</pre>
<p></code></p></blockquote>
<p>But what about types?  Even though we haven&#8217;t given a type signature for <code>setDPL</code>, Haskell is (of course) able to infer one for us.  We can inspect the type using the Potential function <code>getType</code>.</p>
<p>We can inspect the type of <code>setDPL</code>, as shown below.  <b>To make things manageable, I&#8217;ve elided much of the class-related output.</b>  You can see how type-level integers are used to encode assumptions about the side of arguments, in this case verifying that the arguments are (1) appropriately sized to be pushed onto the stack, and (2) appropriately sized to fill in the DPL field in the interrupt gate:</p>
<blockquote><p><code>
<pre>
&gt; :t getType setDPL
getType setDPL
  :: (SZ rcx
      :&lt;= S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))),
      SZ rbx
      :&lt;= S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))),
      SZ rbx :== S (S Z)) =&gt;
     Function
       ConstraintsOn
       (MS
          (Ptr64
             h1
             (InterruptGate offset_lo segsel ist base p offset_mid offset_hi))
          rbx
          rcx
          rdx
          rsi
          rdi
          rbp
          (Ptr64 h (Stack (Ptr64 h11 b11) (Stack a' b')))
          rflags
          rip
          r08
          r09
          r10
          r11
          r12
          r13
          r14
          r15
          (Allocator hn hs cs)
          cmp)
       (MS
          (Ptr64
             (HS (HS hn))
             (InterruptGate offset_lo segsel ist rbx p offset_mid offset_hi))
          rbx
          rcx
          rdx
          rsi
          rdi
          rbp
          (Ptr64 b3 (Stack a' b'))
          rflags
          rip
          r08
          r09
          r10
          r11
          r12
          r13
          r14
          r15
          (Allocator
             (HS (HS (HS (HS (HS (HS hn))))))
             (C (HS (HS (HS (HS (HS hn))))) hs'5)
             (C b2 (C b1 (C h2 (C h1 (C b (C h cs)))))))
          cmp)
</pre>
<p></code></p></blockquote>
<p>Notice that the <code>Ptr64</code> type carries two things: the first is a handle, in the style of Kiselyov and Shan&#8217;s <a href="http://okmij.org/ftp/Haskell/regions.html#light-weight">Lightweight monadic regions</a>, and the second is the type of the data being pointed to.</p>
<p>As pointers are manipulated, the handles are closed and new ones issued, in order to statically prevent pointer aliasing from undermining type safety.  As we see in this example, the handle for our interrupt gate pointer has changed from <code>h1</code> to <code>(HS (HS hn))</code>.  We can also see that, in the process, the <code>h1</code> handle is removed from the list of open handles (the third field in the <code>Allocator</code> type records handles which have been closed).</p>
<p>Here is a typical example of why this is necessary: <code>rax</code> points to an interrupt gate whose DPL is set to User.  After <code>mov rax rbx</code>, <code>rbx</code> now aliases this pointer.  If we then <i>update</i> the DPL field via the <code>rax</code> pointer, we run into a situation where <code>rax</code> and <code>rbx</code> carry the same address but give it different types!</p>
<p>To get around this, whenever a structure manipulated in a way that changes its type, the pointer is modified to keep up: its handle (just prior to update) is closed and replaced with a new handle, reflecting the change.  Any aliases of the pointer (prior to update) are now invalid, as tracked by the Haskell type system.</p>
<p>In fact, such trouble is indeed caught and detected by <code>getType</code>.  Here we see the code for a function which does exactly what I described in this example: it aliases a pointer to an interrupt gate, then attempts to modify both pointers to wind up with an inconsistent DPL in the type.  The code for this function:</p>
<blockquote><p><code>
<pre>
testSetDPL2 = asCode "testSetDPL2" $
     do assumeType rbx (undefined :: PrivLevelUser)
        mov rax r10
        scall setDPL
        comment "Now rax has PrivLevelUser, r10 has unknown dpl"
        swap rax r10
        comment "Now rax has unknown dpl, r10 has PrivLevelUser"
        pop rbx
        assumeType rbx (undefined :: PrivLevelKernel)
        scall setDPL
        comment "Now rax has PrivLevelKernel, r10 has PrivLevelUser, but it's the same ptr"
        ret
</pre>
<p></code></p></blockquote>
<p>The code will render (at present, the <code>render</code> function doesn&#8217;t verify types &#8212; only <code>getType</code> does this), and if we render it, we get the following (AT&amp;T) assembly:</p>
<blockquote><p><code>
<pre>
&gt; renderFn testSetDPL2
testSetDPL2:
    mov %rax %r10
    call setDPL
    // Now rax has PrivLevelUser, r10 has unknown dpl
    // swapping %rax with %r10
    push %rax
    mov %r10 %rax
    pop %r10
    // swap complete
    // Now rax has unknown dpl, r10 has PrivLevelUser
    pop %rbx
    call setDPL
    // Now rax has PrivLevelKernel, r10 has PrivLevelUser, but it's the same ptr
    ret
</pre>
<p></code></p></blockquote>
<p>However, once we try to inspect the type, we get the following:</p>
<blockquote><p><code>
<pre>
&gt; :t getType testSetDPL2

Top level:
    Couldn't match expected type `Potential.Handles.False'
           against inferred type `Potential.Handles.True'
    When using functional dependencies to combine
      Potential.Handles.LOr
        n1 Potential.Handles.True Potential.Handles.True,
        arising from the dependency `n1 n2 -&gt; t'
        in the instance declaration at Potential/Handles.hs:101:9
      Potential.Handles.LOr
        t Potential.Handles.True Potential.Handles.False,
        arising from a use of `testSetDPL2' at :1:8-18
</pre>
<p></code></p></blockquote>
<p>(Of course, <code>ghci</code> isn&#8217;t giving us very useful information about why the failure happened; more work is definitely needed in this area.  I expect that type families could be used to replace my functional dependencies, and would give much more meaningful errors, but I believe this will require <a href="http://hackage.haskell.org/trac/ghc/ticket/2715">equality constraints in superclasses</a>, which is why I haven&#8217;t yet gone down that road.)</p>
<p>Although it is not currently implemented, it should be possible (once a memory allocator is written) to extend this system to also ensure that memory is freed at appropriate times.  For instance, when writing the task manager for an operating system kernel, one will need to allocate memory as tasks are spawned.  It should be possible to extend the handle system to track when memory is allocated <i>for a particular task</i>, so that when that task exits, it can be ensured that <i>all associated memory is freed</i>.  I&#8217;m unaware of any trick in C or C++ that would allow such a guarantee to be made.</p>
<p>Here is another example.  In this example, <code>test2</code> is just a simple function whose behavior is immaterial &#8212; it just provides a place that we can conditionally jump to.</p>
<p>Consider the code</p>
<blockquote><p><code>
<pre>
test11 = asCode "test11" $
     do pop rax
        pop rbx
        pop rcx
        rabxCmp &lt;- cmp rax rbx
        racxCmp &lt;- cmp rax rcx
        sje test2 racxCmp
        ret
</pre>
<p></code></p></blockquote>
<p>We see two comparisons being performed.  The latter comparison is used to conditionally jump to <code>test2</code>.  If we inspect the type of <code>test11</code>, we get what we&#8217;d expect (again <b>we elide much of the class constraint output</b> for the sake of brevity):</p>
<blockquote><p><code>
<pre>
&gt; :t getType test11
getType test11
  :: Function
       ConstraintsOn
       (MS
          rax
          rbx
          rcx
          rdx
          rsi
          rdi
          rbp
          (Ptr64
             h
             (Stack
                Int64
                (Stack
                   Int64
                   (Stack
                      Int64
                      (Stack (Ptr64 h11 b11) (Stack (Ptr64 h12 b12) (Stack a' b')))))))
          (FlagsRegister cf pf af zf sf tf if df of' iopl rf ac vif vip id)
          rip
          r08
          r09
          r10
          r11
          r12
          r13
          r14
          r15
          (Allocator hn hs cs)
          cmp)
       (MS
          Int64
          Int64
          Int64
          rdx
          rsi
          rdi
          rbp
          (Ptr64 b5 (Stack a' b'))
          (FlagsRegister
             (CF (CS (CS cmp)))
             (PF (CS (CS cmp)))
             (AF (CS (CS cmp)))
             (ZF (CS (CS cmp)))
             (SF (CS (CS cmp)))
             tf
             if
             df
             (OF (CS (CS cmp)))
             iopl
             rf
             ac
             vif
             vip
             id)
          rip
          r08
          r09
          r10
          r11
          r12
          r13
          r14
          r15
          (Allocator
             (HS (HS (HS (HS (HS (HS (HS (HS hn))))))))
             (C (HS (HS (HS (HS (HS (HS (HS hn))))))) hs'6)
             (C b4 (C b3 (C b2 (C h1 (C b1 (C b (C h cs))))))))
          (CS (CS cmp)))
</pre>
<p></code></p></blockquote>
<p>The very last line indicates that two comparisons have been performed in this block.</p>
<p>Now suppose we modify the function to introduce a bug:</p>
<blockquote><p><code>
<pre>
test11 = asCode "test11" $
     do pop rax
        pop rbx
        pop rcx
        rabxCmp &lt;- cmp rax rbx
        racxCmp &lt;- cmp rax rcx
        sje test2 rabxCmp
        ret
</pre>
<p></code></p></blockquote>
<p>The change is subtle &#8212; here we are now attempting to conditionally jump on <code>rabxCmp</code>, which is a comparison whose results should no longer be held in the flags register.  Indeed, Potential catches this bug when we attempt to load the code in <code>ghci</code>:</p>
<blockquote><p><code>
<pre>
TestCode.hs:54:1:
    Occurs check: cannot construct the infinite type: cmp1 = CS cmp1
      Expected type: PState
                       Instr
                       c
                       (Potential.MachineState.Set
                          Potential.MachineState.RRflags
                          (FlagsRegister
                             (CF (CS (CS cmp1)))
                             (PF (CS (CS cmp1)))
                             (AF (CS (CS cmp1)))
                             (ZF (CS (CS cmp1)))
                             (SF (CS (CS cmp1)))
                             tf
                             if'
                             df
                             (OF (CS (CS cmp1)))
                             iopl
                             rf
                             ac
                             vif
                             vip
                             id)
                          rax
                          rbx
                          rcx
                          rdx
                          rsi
                          rdi
                          rbp
                          (Ptr64 h (Stack (Ptr64 h1 b1) (Stack a' b')))
                          (FlagsRegister
                             (CF (CS cmp1))
                             (PF (CS cmp1))
                             (AF (CS cmp1))
                             (ZF (CS cmp1))
                             (SF (CS cmp1))
                             tf
                             if'
                             df
                             (OF (CS cmp1))
                             iopl
                             rf
                             ac
                             vif
                             vip
                             id)
                          rip1
                          r081
                          r091
                          r101
                          r111
                          r121
                          r131
                          r141
                          r151
                          alloc
                          (CS (CS cmp1)))
                       y
                       a
      Inferred type: PState
                       Instr
                       c
                       (MS
                          rax
                          rbx
                          rcx
                          rdx
                          rsi
                          rdi
                          rbp
                          (Ptr64 h (Stack (Ptr64 h1 b1) (Stack a' b')))
                          (FlagsRegister
                             (CF (CS (CS cmp1)))
                             (PF (CS (CS cmp1)))
                             (AF (CS (CS cmp1)))
                             (ZF (CS cmp1))
                             sf
                             tf1
                             if
                             df1
                             of
                             iopl1
                             rf1
                             ac1
                             vif1
                             vip1
                             id1)
                          rip
                          r08
                          r09
                          r10
                          r11
                          r12
                          r13
                          r14
                          r15
                          (Allocator hn hs cs)
                          cmp)
                       (MS
                          rax
                          rbx
                          rcx
                          rdx
                          rsi
                          rdi
                          rbp
                          (Ptr64 b (Stack a' b'))
                          (FlagsRegister
                             (CF (CS (CS cmp1)))
                             (PF (CS (CS cmp1)))
                             (AF (CS (CS cmp1)))
                             (ZF (CS cmp1))
                             sf
                             tf1
                             if
                             df1
                             of
                             iopl1
                             rf1
                             ac1
                             vif1
                             vip1
                             id1)
                          rip
                          r08
                          r09
                          r10
                          r11
                          r12
                          r13
                          r14
                          r15
                          (Allocator (HS (HS (HS (HS hn)))) hs'1 cs')
                          cmp)
                       ()
    In a stmt of a 'do' expression: sje test2 rabxCmp
    In the second argument of `($)', namely
        `do { pop rax;
              pop rbx;
              pop rcx;
              rabxCmp &lt;- cmp rax rbx;
              .... }&#039;
</pre>
<p></code></p></blockquote>
<p>There&#8217;s a lot of output, but towards the bottom we see that <code>ghci</code> correctly points to <code>sje test2 rabxCmp</code> as the source of the trouble.</p>
<p><b>Source preview</b></p>
<p>At present time, nothing in Potential is being taken as &#8220;stable,&#8221; so the code is not available on Hackage.  However, the project is on github at <a href="http://github.com/intoverflow/Potential">Potential</a>.  The code as of this blog post is in the <a href="http://github.com/intoverflow/Potential/tree/initial-blog-announce">initial-blog-announce</a> branch.</p>
<p>Basic usage is pretty simple.  To play around a bit, get started like so:</p>
<blockquote><p><code>
<pre>
$ ghci -fcontext-stack=160 TestCode.hs
*TestCode&gt; renderFn test1
test1:
    pop %rax
    pop %rbx
    pop %rbx
    // swapping %rax with %rbx
    push %rax
    mov %rbx %rax
    pop %rbx
    // swap complete
    ret
</pre>
<p></code></p></blockquote>
<p>(The large context stack is there for the type-level integers.)</p>
<p>Since Potential makes heavy use of <a href="http://intoverflow.wordpress.com/2010/05/18/optional-class-constraints-in-haskell/">optional class constraints</a>, the best way to inspect the type of a function is with <code>:t getType &lt;function name goes here&gt;</code>.</p>
<p><b>What&#8217;s next (Potential-wise)</b></p>
<p>The system that tracks pointers is still being adjusted.  It seems to be doing its job, but it can undoubtedly be improved in terms of producing good human-readable output via <code>getType</code>.  Ultimately I&#8217;d like to move the system away from functional dependencies, but without <a href="http://hackage.haskell.org/trac/ghc/ticket/2715">equality constraints in superclasses</a>, the illegality of incoherent instances for associated types seems to make this infeasible.</p>
<p>There&#8217;s also a good deal of basic assembly that isn&#8217;t present in the language right now.  <code>jmp</code> can be invoked on top-level defined functions (it&#8217;s name in Potential is <code>sjmp</code> for &#8220;static jump&#8221;), but is not currently implemented for non-literals (that is, <code>jmp rax</code> isn&#8217;t implemented).</p>
<p>The only conditional jump instruction in the current source is <code>sje</code>, which permits conditional jumps on equality to a top-level-defined function.  At present it is just a hack &#8212; type inference with this instruction isn&#8217;t quite correct.  (This problem relates to verifying that the instructions following the <code>sje</code> mnemonic are typed the same as the code which might be jumped to.  This condition, currently missing, is essentially the same as the condition in Haskell that the <code>then</code> and <code>else</code> blocks in an <code>if</code> statement must have the same type.)</p>
<p>For me, the biggest question is one of scale: while Potential seems capable at managing my small examples, will it be practical to write an operating system in this language?  Will Potential&#8217;s promises of static checking via types deliver?  I believe it might, but this issue is far from settled.</p>
<p><b>What&#8217;s next (blog-wise)</b></p>
<p>In the days to come, I will give some more examples of code written in Potential, as well as some downloads that can be played with.  I&#8217;ll also dig into some of the issues that have come up during the design and implementation of the language, much in the style of my posts on <a href="http://intoverflow.wordpress.com/2010/05/18/optional-class-constraints-in-haskell/">optional class constraints</a> and <a href="http://intoverflow.wordpress.com/2010/05/09/polymorphic-first-class-labels/">polymorphic first-class labels</a>, which both arose during this project.  I&#8217;ll also describe some related work that has really come in to save my skin design-wise.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/385/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=385&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/05/21/announcing-potential-x86-64-assembler-as-a-haskell-edsl/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Optional class constraints in Haskell</title>
		<link>http://intoverflow.wordpress.com/2010/05/18/optional-class-constraints-in-haskell/</link>
		<comments>http://intoverflow.wordpress.com/2010/05/18/optional-class-constraints-in-haskell/#comments</comments>
		<pubDate>Tue, 18 May 2010 20:48:24 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=371</guid>
		<description><![CDATA[Work on my Haskell EDSL is moving ever onward. Today I want to talk about a trick I found while working on it. (Along the way I&#8217;ll make some allusions to the EDSL, but I want to forestall announcing the EDSL for another week or so, in the interest of ensuring it&#8217;s fully baked.) My [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=371&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Work on my Haskell EDSL is moving ever onward.  Today I want to talk about a trick I found while working on it.  (Along the way I&#8217;ll make some allusions to the EDSL, but I want to forestall announcing the EDSL for another week or so, in the interest of ensuring it&#8217;s fully baked.)</p>
<p><span id="more-371"></span></p>
<p>My EDSL is essentially an attempt at grafting the Haskell static type system onto a dynamically typed language.  The idea is that a programmer writes their code in my EDSL &#8212; that is, he&#8217;s really written his code in an <a href="http://blog.sigfpe.com/2009/02/beyond-monads.html">indexed monad</a> I&#8217;ve set up in Haskell &#8212; and when he executes his EDSL code, the output is a bunch of code written in the target language, which can then be compiled using whatever toolchain he likes.  We&#8217;ve basically embedded the target language into Haskell, which means that the Haskell type system can now be used to model concepts in the target language.  The target language has a very weak dynamic type system which, if I&#8217;ve done my modeling correctly, allows a proper superset of the programs that my EDSL will allow.</p>
<p>Anyway, with this design, there are two operations that the programmer needs to be able to perform with their code:</p>
<ul>
<li>Make sure that the code type-checks.  Obviously the Haskell compiler will do this on its own.</li>
<li>If the code type-checks, render the code from the EDSL into the target language.</li>
</ul>
<p>As alluded to above, the EDSL resides within an indexed monad, say <code>PState assumes returns a</code>, which encodes the Hoare types of code blocks.  Consequently, there is a render function, appropriately typed <code>render :: PState assumes returns () -&gt; TargetAST</code>, where <code>TargetAST</code> is some algebraic data type that I can pretty print into the target language&#8217;s syntax.</p>
<p>Obviously I&#8217;m leaving out a good deal of detail about the EDSL, which I&#8217;ll talk about in a later post; today I want to talk about a problem that arises with this style.</p>
<p>It is a common theme that the true zen of Haskell code is modeling your program&#8217;s behavior in the type system.  As a basic example, a program that <a href="http://neilmitchell.blogspot.com/2008/02/hoogle-3-security-bug.html">takes user input and puts it into a syntax-sensitive</a> structure can use types to ensure that non-escaped strings never make it from the user to the structure; as a more complex example, configuration flags can be managed at the type level, if one is sufficiently clever.  These are all ideas that would be wonderful to have in other languages, but which are typically absent due to the limitations of the type systems in question.</p>
<p>Of course, when one wishes to encode program behavior into types, one often works into a box where type classes need to be introduced.  I recently found myself: my target language has a notion of &#8220;operand size,&#8221; and some functions in the language only work with certain sizes of data; it was thus necessary to have a representation for this in the EDSL.  Since operand size is a <i>property</i> of a type, rather than a type itself, this introduced type classes.</p>
<p>All of this is fine, of course, when it comes to type checking: I was able to employ <a href="http://www.haskell.org/haskellwiki/Type_arithmetic">a common trick to encode sizes in types</a>, then have a two-parameter class<br />
<code>
<pre>
class HasSZ d size
</pre>
<p></code><br />
whose only job is to describe a size predicate on a type.  But now I have EDSL code with a type signature predicated on a class constraint, as in<br />
<code>
<pre>
someEDSLFunction :: HasSZ d (S (S Z))
        =&gt; PState (some complex expression using d) returns ()
</pre>
<p></code><br />
Since the implementation of <code>render</code> is essentially (using <code>ScopedTypeVariables</code>) just<br />
<code>
<pre>
render :: PState assumes returns () -&gt; TargetAST
render f = getTargetAST $ runPState f (undefined :: assumes)
</pre>
<p></code><br />
we now have a problem: if <code>f</code> has a class constraint like that found in <code>someEDSLFunction</code>, we get a type error:</p>
<blockquote><p><code>
<pre>
&gt; render someEDSLFunction 

:1:7:
    No instance for (HasSZ d (S (S Z)))
      arising from a use of `someEDSLFunction' at :1:7-22
    Possible fix: add an instance declaration for (HasSZ d (S (S Z)))
    In the first argument of `render', namely `someEDSLFunction'
    In the expression: render someEDSLFunction
    In the definition of `it': it = render someEDSLFunction
</pre>
<p></code></p></blockquote>
<p>This is particularly frustrating because (though I haven&#8217;t yet explained this) we&#8217;re using <code>undefined</code> for the sole reason that we <i>don&#8217;t want</i> the code which is generated to depend on the input &#8212; that is, the class constraints on the input are only a formality, there to make sure that function calls within the EDSL are legal in the context of the target language!</p>
<p>So that&#8217;s the problem.  Here&#8217;s the fix: <i>optional</i> class constraints.  The central issue is that, during the type-checking phase, we want the class constraints to be there &#8212; but during the rendering phase, we wish they weren&#8217;t.  We can achieve this by introducing a new type class, along with some suspicious looking instances.</p>
<p>In addition to the <code>HasSZ</code> class, we&#8217;ll introduce the following class (and instances):<br />
<code>
<pre>
data ClassConstraintsOn
data ClassConstraintsOff

class MaybeHasSZ d size c
instance (HasSZ d size) =&gt; MaybeHasSZ d size ConstraintsOn
instance MaybeHasSZ d size ConstraintsOff
</pre>
<p></code><br />
We then take our indexed monad, <code>PState</code>, and add an extra type parameter to track whether or not we want class constraints enabled or disabled.  Functions in our EDSL now have type <code>PState c assumes returns a</code>.</p>
<p>We now give the function <code>someEDSLFunction</code> a signature like<br />
<code>
<pre>
someEDSLFunction :: MaybeHasSZ d (S (S Z)) c =&gt;
        PState c (some complex expression using d) returns ()
</pre>
<p></code><br />
By coupling the class constraint with the <code>c</code> variable in our <code>PState</code>, we&#8217;ve given ourselves a way to &#8220;flip a switch&#8221; to turn the <code>HasSZ</code> constraint on and off: during type checking, the EDSL binds <code>c</code> to <code>ConstraintsOn</code>, so that the constraint <code>MaybeHasSZ d (S (S Z)) c</code> reduces to the constraint <code>HasSZ d (S (S Z))</code>.  To fix rendering, we just give <code>render</code> the signature<br />
<code>
<pre>
render :: PState ConstraintsOff assumes returns () -&gt; TargetAST
</pre>
<p></code><br />
so that the expression <code>render someEDSLFunction</code> collapses the constraint <code>MaybeHasSZ d (S (S Z)) c</code> down to nothing, allowing the <code>render</code> function to do its job.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/371/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/371/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/371/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=371&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/05/18/optional-class-constraints-in-haskell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Polymorphic first class labels</title>
		<link>http://intoverflow.wordpress.com/2010/05/09/polymorphic-first-class-labels/</link>
		<comments>http://intoverflow.wordpress.com/2010/05/09/polymorphic-first-class-labels/#comments</comments>
		<pubDate>Mon, 10 May 2010 02:49:32 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=359</guid>
		<description><![CDATA[(This post uses GHC 6.12.1) The expression &#8220;first class labels&#8221; refers to the idea that, for record data types, one should be able to pass around the labels just as they would any other type. For instance, if I have a record like data Foo a b = { biz :: a, baz :: b [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=359&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(This post uses GHC 6.12.1)</p>
<p>The expression &#8220;first class labels&#8221; refers to the idea that, for record data types, one should be able to pass around the labels just as they would any other type.  For instance, if I have a record like<br />
<code>
<pre>
data Foo a b = { biz :: a, baz :: b }
</pre>
<p></code><br />
the value <code>biz</code> shouldn&#8217;t just denote the function <code>biz :: Foo a b -&gt; a</code>, but should also be usable as a way of updating records, that is, a function like <code>biz' :: Foo a b -&gt; a' -&gt; Foo a' b</code>.</p>
<p>The Mythical Haskell&#8217; includes some <a href="http://hackage.haskell.org/trac/haskell-prime/wiki/FirstClassLabels">proposals</a> for updating the records system with features aimed at supporting this idea, but for the time being, many people prefer to use <a href="http://hackage.haskell.org/package/fclabels">fclabels</a>, which achieves much of this magic using <a href="http://www.haskell.org/haskellwiki/Template_Haskell">Template Haskell</a>.</p>
<p>Recently, while working on an EDSL, I found myself wishing I had first class labels.  I ran into a problem, though, which (along with solution) I&#8217;ll now describe.</p>
<p>Consider the following code:</p>
<blockquote><p>
<code>
<pre>
module Label where

data Foo a b = Foo a b deriving Show

updatea :: a' -&gt; Foo a b -&gt; Foo a' b
updatea a (Foo _ b) = Foo a b

updateb :: b' -&gt; Foo a b -&gt; Foo a b'
updateb b (Foo a _) = Foo a b

worksFine foo0 = let foo1 = updatea 'a' foo0
                     foo2 = updatea "a" foo1
                 in foo2
</pre>
<p></code>
</p></blockquote>
<p>Here I&#8217;ve defined a data structure <code>Foo</code> with two fields, along with a pair of functions for updating these fields.  Then I defined a function <code>worksFine</code> which uses <code>updatea</code> to modify a <code>Foo</code>.</p>
<p>Obviously I could also write the following function:<br />
<code>
<pre>
worksFine' foo0 = let foo1 = updateb 'a' foo0
                      foo2 = updateb "a" foo1
                  in foo2
</pre>
<p></code><br />
which is exactly the same, except that it uses <code>updateb</code> instead, thereby modifying the <i>other</i> field in <code>Foo</code>.</p>
<p>So now we have an obvious place to generalize: instead of having both <code>worksFine</code> and <code>worksFine'</code>, why not have a single function which takes the updater as a parameter?</p>
<p>If we try it out, the first attempt looks like this:<br />
<code>
<pre>
trouble u foo0 = let foo1 = u 'a' foo0
                     foo2 = u "a" foo1
                 in foo2
</pre>
<p></code><br />
Only trouble is that this fails to type check:<br />
<code>
<pre>
    Couldn't match expected type `[Char]' against inferred type `Char'
      Expected type: [Char] -&gt; t1 -&gt; t
      Inferred type: Char -&gt; t2 -&gt; t1
    In the expression: u "a" foo1
    In the definition of `foo2': foo2 = u "a" foo1
</pre>
<p></code></p>
<p>The problem is that <code>trouble</code> doesn&#8217;t believe that the argument <code>u</code> is polymorphic enough.  This is a typical rank-2 issue: we don&#8217;t want <code>trouble</code> to bind the type variables in the signature for <code>u</code>.</p>
<p>Using rank-2 types, we can get very close to a solution.  We can write the functions</p>
<blockquote><p>
<code>
<pre>
alsoWorksFinea :: (forall a a' . a' -&gt; Foo a b -&gt; Foo a' b)
               -&gt; Foo a b -&gt; Foo [Char] b
alsoWorksFinea u foo0 = let foo1 = u 'a' foo0
                            foo2 = u "a" foo1
                        in foo2

alsoWorksFineb :: (forall b b' . b' -&gt; Foo a b -&gt; Foo a b')
               -&gt; Foo a b -&gt; Foo a [Char]
alsoWorksFineb u foo0 = let foo1 = u 'a' foo0
                            foo2 = u "a" foo1
                        in foo2
</pre>
<p></code>
</p></blockquote>
<p>but neither is able to accept <i>both</i> of our update functions, even though each function has exactly the same body.  Worse, I wasn&#8217;t able to find a sufficiently general type signature that would allow me to have <i>one</i> function which would be able to accept <i>both</i> update functions.</p>
<p>Luckily, where rank-2 types have failed me, type families have saved me.  Any time you need more flexibility in your type signatures than the syntax will allow, you might be in a box where type families are the way to go.  Here&#8217;s what it looked like in my case.</p>
<p>First I defined some typed to represent the two fields of my <code>Foo</code> structure:<br />
<code>
<pre>
data A = A
data B = B
</pre>
<p></code><br />
Obviously I can pass around these values in a first-class manner, no trouble at all.</p>
<p>Then I defined a class for describing updating and getting:<br />
<code>
<pre>
class Field f x y where
  type Updated f x y
  update :: f -&gt; x -&gt; y -&gt; Updated f x y
  type Gotten f y
  get :: f -&gt; y -&gt; Gotten f y
</pre>
<p></code></p>
<p>Now there are two instances to give, one for each field in <code>Foo</code>:</p>
<blockquote><p>
<code>
<pre>
instance Field A a' (Foo a b) where
  type Updated A a' (Foo a b) = Foo a' b
  update A a' (Foo a b) = Foo a' b
  type Gotten A (Foo a b) = a
  get A (Foo a b) = a

instance Field B b' (Foo a b) where
  type Updated B b' (Foo a b) = Foo a b'
  update B b' (Foo a b) = Foo a b'
  type Gotten B (Foo a b) = b
  get B (Foo a b) = b
</pre>
<p></code>
</p></blockquote>
<p>And we&#8217;re basically done.  We can now write the function that started this whole mess:</p>
<blockquote><p>
<code>
<pre>
shouldWorkFine f foo0 = let foo1 = update f 'a' foo0
                            foo2 = update f "a" foo1
                        in foo2
</pre>
<p></code>
</p></blockquote>
<p><code>GHCi</code> is able to give us a very promising type signature for it:<br />
<code>
<pre>
&gt; :t shouldWorkFine
shouldWorkFine
  :: (Field f [Char] (Updated f Char y), Field f Char y) =&gt;
     f -&gt; y -&gt; Updated f [Char] (Updated f Char y)
</pre>
<p></code></p>
<p>While this technique introduces type classes and type families into our program (something which can make typing troublesome in other areas), it delivers something I don&#8217;t know how to otherwise get: <i>polymorphic</i> first class labels.</p>
<p>Clearly the next step is to implement a library like fclabels which uses Template Haskell to define instances of the <code>Field</code> class.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/359/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=359&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/05/09/polymorphic-first-class-labels/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Some weird interactions between Monomorphism Restriction and Template Haskell</title>
		<link>http://intoverflow.wordpress.com/2010/05/04/some-weird-interactions-between-monomorphism-restriction-and-template-haskell/</link>
		<comments>http://intoverflow.wordpress.com/2010/05/04/some-weird-interactions-between-monomorphism-restriction-and-template-haskell/#comments</comments>
		<pubDate>Tue, 04 May 2010 18:00:38 +0000</pubDate>
		<dc:creator>intoverflow</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://intoverflow.wordpress.com/?p=349</guid>
		<description><![CDATA[Today I&#8217;m going to look at a weird issue I encountered this past weekend while working on a DSL in Haskell. I&#8217;ll start with the code. As this example uses Template Haskell, we need to have the source broken up into two files: Testa.hs: {-# LANGUAGE TemplateHaskell #-} module Testa where import Language.Haskell.TH someth = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=349&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m going to look at a weird issue I encountered this past weekend while working on a <a href="http://paulspontifications.blogspot.com/2008/01/why-haskell-is-good-for-embedded-domain.html">DSL</a> in Haskell.</p>
<p>I&#8217;ll start with the code.  As this example uses <a href="http://www.haskell.org/haskellwiki/Template_Haskell">Template Haskell</a>, we need to have the source broken up into two files:</p>
<p><b>Testa.hs</b>:</p>
<blockquote><p><code>
<pre>
{-# LANGUAGE
        TemplateHaskell #-}
module Testa where

import Language.Haskell.TH

someth = [| () |]

unit = ()

mrIssue :: (Monad m) =&gt; b -&gt; m b
mrIssue = return
</pre>
<p></code></p></blockquote>
<p><b>Testb.hs</b>:</p>
<blockquote><p><code>
<pre>
{-# LANGUAGE
        NoMonomorphismRestriction,
        TemplateHaskell #-}
module Testb where

import Testa

g = $(someth)
foo1 = mrIssue $(someth)

f = ()
foo2 = mrIssue ()

h = unit
foo3 = mrIssue unit
</pre>
<p></code></p></blockquote>
<p>Now, if we fire up <code>ghci Testb.hs</code>, we get the following:</p>
<blockquote><p>
<code>
<pre>
$ ghci Testb.hs
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 2] Compiling Testa            ( Testa.hs, interpreted )
[2 of 2] Compiling Testb            ( Testb.hs, interpreted )
Loading package syb ... linking ... done.
Loading package array-0.2.0.0 ... linking ... done.
Loading package packedstring-0.1.0.1 ... linking ... done.
Loading package containers-0.2.0.1 ... linking ... done.
Loading package pretty-1.0.1.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Ok, modules loaded: Testb, Testa.
*Testb&gt; :t foo1
foo1 :: (Monad m) =&gt; m ()
*Testb&gt; :t g

:1:0:
    Ambiguous type variable `m' in the constraint:
      `Monad m' arising from a use of `g' at :1:0
    Probable fix: add a type signature that fixes these type variable(s)
*Testb&gt;
</pre>
<p></code>
</p></blockquote>
<p>Notice that, while we&#8217;re able to get the type for <code>foo1</code>, for some reason <code>g</code> is ill-typed with an ambiguous type variable in the constraint <code>Monad m</code>.  The only problem, of course, is that when we look at our source we <i>don&#8217;t see</i> any way in which the type for <code>g</code> should have this constraint!</p>
<p>So maybe the problem is that <code>g</code> needs a type signature.  But if we go in and modify Testb.hs, giving it<br />
<code>
<pre>
g :: ()
g = $(someth)
</code></pre>
<p>then we get the following from ghci:</p>
<blockquote><p>
<code>
<pre>
Testb.hs:10:7:
    Could not deduce (Monad m) from the context ()
      arising from a use of `mrIssue' at Testb.hs:10:7-23
    Possible fix:
      add (Monad m) to the context of the type signature for `g'
    In the expression: mrIssue ($someth)
    In the definition of `foo1': foo1 = mrIssue ($someth)
Failed, modules loaded: Testa.
*Testa&gt;
</pre>
<p></code>
</p></blockquote>
<p>So now it's upset about the type for <code>foo1</code>.  Fine.  Let's give it a signature as well:<br />
<code>
<pre>
g :: ()
g = $(someth)
foo1 :: (Monad m) =&gt; m ()
foo1 = mrIssue $(someth)
</pre>
<p></code></p>
<p>Now we get a new error from <code>ghci</code>:</p>
<blockquote><p>
<code>
<pre>
Testb.hs:11:0:
    Contexts differ in length
      (Use -XRelaxedPolyRec to allow this)
    When matching the contexts of the signatures for
      g :: ()
      foo1 :: forall (m :: * -&gt; *). (Monad m) =&gt; m ()
    The signature contexts in a mutually recursive group should all be identical
    When generalising the type(s) for g, foo1
Failed, modules loaded: Testa.
</pre>
<p></code>
</p></blockquote>
<p>If we add <code>RelaxedPolyRec</code> to our list of <code>LANGUAGE</code> extensions, the problem does, indeed, go away.  In this case, we can even remove our type signature for <code>g</code> <i>or</i> for <code>foo1</code>, but not both -- we need to have at least one of them present.</p>
<p>Lastly, if we go back to our original source given above, but replace the signature for <code>mrIssue</code> with<br />
<code>
<pre>
mrIssue :: b -&gt; IO b
mrIssue = return
</pre>
<p></code><br />
then we can remove the <code>NoMonomorphismRestriction</code>, and everything works just fine (we can <code>:t g</code> and <code>:t foo1</code> without any problems).</p>
<p>I'm entirely unsure of what's going on here.  Any theories?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/intoverflow.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/intoverflow.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/intoverflow.wordpress.com/349/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=intoverflow.wordpress.com&amp;blog=1061596&amp;post=349&amp;subd=intoverflow&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://intoverflow.wordpress.com/2010/05/04/some-weird-interactions-between-monomorphism-restriction-and-template-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">intoverflow</media:title>
		</media:content>
	</item>
	</channel>
</rss>
