<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag testing</title>
    <link>http://blog.moertel.com/articles/tag/testing?tag=testing</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Property checking with Python's nose testing framework</title>
      <description>&lt;p&gt;At work recently I was writing some tests with Python&amp;#8217;s out-of-the-box
unit-testing framework
&lt;a href="http://docs.python.org/lib/module-unittest.html"&gt;unittest&lt;/a&gt;.  I&amp;#8217;m new
to Python and accustomed to Perl and Haskell&amp;#8217;s testing frameworks,
which are lightweight and let you write tests without much
hoop-jumping.  In particular,
&lt;a href="http://www.cs.chalmers.se/~rjmh/QuickCheck/"&gt;QuickCheck&lt;/a&gt; and
&lt;a href="http://search.cpan.org/dist/Test-LectroTest/"&gt;LectroTest&lt;/a&gt; make it easy
to test at the property level instead of the test-case level.
With unittest, I was having to write a lot of code
to get the same level of abstraction.&lt;/p&gt;


	&lt;p&gt;By &amp;#8220;property level,&amp;#8221; here&amp;#8217;s what I mean.  Say I&amp;#8217;m testing this thing,
let&amp;#8217;s call it a &lt;em&gt;subscriber pool&lt;/em&gt;.  It has two fundamental properties:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;strong&gt;Subscribe.&lt;/strong&gt; For all initial states of the pool, if you call &lt;em&gt;subscribe&lt;/em&gt;(&lt;em&gt;user&lt;/em&gt;), then, assuming there have been no other operations on the pool, &lt;em&gt;user&lt;/em&gt; must be in the pool.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Unsubscribe.&lt;/strong&gt; For all initial states of the pool, if you call &lt;em&gt;unsubscribe&lt;/em&gt;(&lt;em&gt;user&lt;/em&gt;), then, assuming there have been no other operations on the pool, &lt;em&gt;user&lt;/em&gt; must not be in the pool.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;That&amp;#8217;s it.  If my implementation satisfies both properties, it&amp;#8217;s
correct.  (This is a simplified version of my real testing problem,
which required additional property checks.)&lt;/p&gt;


	&lt;p&gt;To test whether my implementation satisfies each property, I must
write individual test cases that together &amp;#8220;cover&amp;#8221; the property.  For
example, to test whether the Subscribe property holds, I might write
four test cases:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class SubscribeProperty(unittest.TestCase):

    def setUp(self):
        initialize_pool()

    def tearDown(self):
        destroy_pool()

    def testEmpty(self):
        load_pool_with_members([])
        subscribe("1")
        self.assert_("1" in pool_members())

    def testOtherGuyAlreadyInPool(self):
        load_pool_with_members(["2"])
        subscribe("1")
        self.assert_("1" in pool_members())

    def testSubscriberAlreadyInPool(self):
        load_pool_with_members(["1"])
        subscribe("1")
        self.assert_("1" in pool_members())

    def testSubscriberAndOtherGuyAlreadyInPool(self):
        load_pool_with_members(["1", "2"])
        subscribe("1")
        self.assert_("1" in pool_members())
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Every one of the test cases has the same form.  The repetition
makes me want to refactor the whole thing.&lt;/p&gt;


	&lt;p&gt;Okay, let&amp;#8217;s do it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;INITIAL_POOL_STATES = [[], ["2"], ["1"], ["1", "2"]]

class SubscribeProperty(unittest.TestCase):

    def setUp(self):
        initialize_pool()

    def tearDown(self):
        destroy_pool()

    def testSubscribe(self):
        for case in INITIAL_POOL_STATES:
            self.setUp()
            try:
                load_pool_with_members(case)
                subscribe("1")
                self.assert_("1" in pool_members(), case)
            finally:
                self.tearDown()
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;We&amp;#8217;re fighting a bit with the testing framework because our notion of
when set-up and tear-down should occur doesn&amp;#8217;t match its own, but
otherwise our code is looking much more manageable.  In particular, if
we want to extend our property-check coverage with additional initial pool
states, we don&amp;#8217;t need to write additional tests; instead, we can just
extend a single list.&lt;/p&gt;


	&lt;p&gt;But we&amp;#8217;re only halfway done.  We must also check the Unsubscribe
property.  The code for it is virtually the same as for Subscribe, but
with &lt;em&gt;subscribe&lt;/em&gt; becoming &lt;em&gt;unsubscribe&lt;/em&gt; and &lt;em&gt;in&lt;/em&gt; becoming &lt;em&gt;not in&lt;/em&gt;.
Let&amp;#8217;s add it to our class:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class SubscriberPoolProperties(unittest.TestCase):

    def setUp(self):
        initialize_pool()

    def tearDown(self):
        destroy_pool()

    def testSubscribe(self):
        for case in INITIAL_POOL_STATES:
            self.setUp()
            try:
                load_pool_with_members(case)
                subscribe("1")
                self.assert_("1" in pool_members(), case)
            finally:
                self.tearDown()

    def testUnsubscribe(self):
        for case in INITIAL_POOL_STATES:
            self.setUp()
            try:
                load_pool_with_members(case)
                unsubscribe("1")
                self.assert_("1" not in pool_members(), case)
            finally:
                self.tearDown()
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And now let&amp;#8217;s factor out the new redundancy:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class SubscriberPoolProperties(unittest.TestCase):

    def setUp(self):
        initialize_pool()

    def tearDown(self):
        destroy_pool()

    def testSubscribe(self):
        def testfn(case):
            subscribe("1")
            self.assert_("1" in pool_members(), case)
        self._forall_test_cases(testfn)

    def testUnsubscribe(self):
        def testfn(case):
            unsubscribe("1")
            self.assert_("1" not in pool_members(), case)
        self._forall_test_cases(testfn)

    def _forall_test_cases(self, testfn):
        for case in INITIAL_POOL_STATES:
            self.setUp()
            try:
                load_pool_with_members(case)
                testfn(case)
            finally:
                self.tearDown()
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;It&amp;#8217;s not bad, but it&amp;#8217;s not great either.  There&amp;#8217;s still a
lot of noise in that code.&lt;/p&gt;


	&lt;p&gt;After discussing the situation with my more-Pythonic
colleague &lt;a href="http://apipes.blogspot.com/"&gt;Tim Lesher&lt;/a&gt;, I took his
advice to check out the &lt;a href="http://somethingaboutorange.com/mrl/projects/nose/"&gt;nose testing framework&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;One of the things I liked right away about nose was that it supports
&lt;a href="http://somethingaboutorange.com/mrl/projects/nose/#test-generators"&gt;test
generators&lt;/a&gt;,
which would let me represent each property-check as a generator that
yields the test cases needed to check the property.  Also, set-up and
tear-down would automatically occur per &lt;em&gt;generated&lt;/em&gt; test, so I
wouldn&amp;#8217;t have to invoke them manually.&lt;/p&gt;


	&lt;p&gt;Once I got familiar with nose, it was easy to create a decorator to
represent the forall-test-cases idiom:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def forall_cases(cases):
    def decorate(testfn):
        def gen():
            for case in cases:
                yield testfn, case
        gen.__name__ = "test_%s_for_a_case" % testfn.__name__
        return gen
    return decorate
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Note that this decorator is not specific to our subscriber-pool tests.
It can be used in any situation where we need to check a property
across a collection of cases.  In fact, I keep this little gem in a
&amp;#8220;nosehelpers&amp;#8221; library, where I reuse it all the time.  Here&amp;#8217;s an
example of how to use it to check the trivial property that
&lt;em&gt;x&lt;/em&gt;&amp;#160;=&amp;#160;&lt;em&gt;x&lt;/em&gt; for all &lt;em&gt;x&lt;/em&gt; in 0&amp;#8211;99:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;@forall_cases(range(100))
def check_self_equality(x):
    assert x == x
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, back to our testing problem.  Here&amp;#8217;s how we can use the
decorator to check the Subscribe property:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;@forall_cases(INITIAL_POOL_STATES)
@with_setup(initialize_pool, destroy_pool)
def check_subscribe(case):
    load_pool_with_members(case)
    subscribe("1")
    assert "1" in pool_members()
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;(The &lt;em&gt;with_setup&lt;/em&gt; decorator is defined by nose and tells nose to run
the given set-up and tear-down functions before and after each of the
generated test cases.)&lt;/p&gt;


	&lt;p&gt;Not bad.  The only problem I have with that code is that it mixes the
&amp;#8220;For all initial states of the pool&amp;#8221; part of the property definition
into the &amp;#8220;if you call &lt;em&gt;subscribe&lt;/em&gt;(&lt;em&gt;user&lt;/em&gt;), then &amp;#8230;&amp;#8221; part.  I&amp;#8217;d like
the code to be more explicit about which part defines the scope of the
property claim and which part defines the test for whether the claim
holds for any particular test case within that scope.&lt;/p&gt;


	&lt;p&gt;Fortunately, we can build upon our existing decorator to create
exactly what we need:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def forall_initial_pools(testfn):
    @forall_cases(INITIAL_POOL_STATES)
    @with_setup(initialize_pool, destroy_pool)
    def setup_case_and_test_it(case):
        load_pool_with_members(case)
        testfn(case)
    setup_case_and_test_it.__name__ = \
        "test_%s_for_a_subscriber_case" % testfn.__name__
    return setup_case_and_test_it
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here&amp;#8217;s what the decorator does.  When you apply it to a test function
&lt;em&gt;testfn&lt;/em&gt;, it returns a test generator that yields a property-check
test for each of the initial pool states.  For each, it sets up a new
pool, loads it with the initial subscribers (as given by the
corresponding test case), runs the given check function &lt;em&gt;testfn&lt;/em&gt;, and
then cleans up after itself.&lt;/p&gt;


	&lt;p&gt;With this decorator, our Pythonic property definitions now mirror
the human-language definitions from the start of the article:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;@forall_initial_pools
def check_subscribe(case):
    subscribe("1")
    assert "1" in pool_members()

@forall_initial_pools
def check_unsubscribe(case):
    unsubscribe("1")
    assert "1" not in pool_members()
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And that&amp;#8217;s pretty much the solution I ended up using at work.  There,
as opposed to here, I got to reuse my decorators for many more tests,
making them all the more worth their small implementation price.&lt;/p&gt;</description>
      <pubDate>Wed, 19 Mar 2008 22:34:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:48416d65-7ef2-4453-9adc-6ee13f8e85b3</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2008/03/19/property-checking-with-pythons-nose-testing-framework</link>
      <category>testing</category>
      <category>testing</category>
      <category>python</category>
      <category>nose</category>
      <category>unittest</category>
      <category>properties</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/707</trackback:ping>
    </item>
    <item>
      <title>Introductory Haskell: Solving the Sorting-It-Out Kata</title>
      <description>&lt;p&gt;Last Tuesday, my friend &lt;a href="http://caseywest.com/"&gt;Casey&lt;/a&gt; and I were hanging
out at &lt;a href="http://aldocoffee.com/"&gt;Aldo Coffee&lt;/a&gt;.  We planned on enjoying
some espresso, doing some work, and then heading over to the &lt;a href="http://pghcodingdojo.org/"&gt;Pittsburgh Coding
Dojo&lt;/a&gt;, where we could hang out with
other geekly folks.
We ended up
not having enough time to go to the meeting, but we decided to hack
on the challenge problem anyway, using Aldo&amp;#8217;s ever-handy free
wireless to access the Internet.&lt;/p&gt;


	&lt;p&gt;The Dojo problem was PragDave&amp;#8217;s &lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Practices/Kata/KataEleven.rdoc"&gt;Kata Eleven &amp;#8211; Sorting it
Out&lt;/a&gt;.  (It&amp;#8217;s short;
read it now.)  We decided to use Haskell for our implementation
language.&lt;/p&gt;


	&lt;p&gt;In this post, I&amp;#8217;ll walk through our coding session and explain how our
solution evolved.  To better fit the session into a blog post, I
have removed a lot of back-and-forth micro iterations, and I have
edited some of the code for clarity.&lt;/p&gt;


	&lt;h3&gt; The first part of the problem&lt;/h3&gt;


	&lt;p&gt;The first part of the problem was &amp;#8220;Sorting Balls.&amp;#8221;  The story: You
need to implement a &amp;#8220;rack&amp;#8221; to hold the balls drawn at random (without
replacement) from a bin containing sixty balls, numbered 0 to 59.
Regardless of the order in which the balls are added to the rack, you
need to present them in sorted order whenever you&amp;#8217;re asked for them.&lt;/p&gt;


	&lt;p&gt;Upon reading this part of the challenge, a couple of thoughts sprung to mind:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Because the range of balls is so small, the problem was begging for a solution based on a &lt;a href="http://en.wikipedia.org/wiki/Counting_sort"&gt;counting sort&lt;/a&gt;.&lt;/li&gt;
		&lt;li&gt;Because the balls are uniquely numbered and drawn without replacement, we could even use a bit vector to represent counts.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Nevertheless, we decided to ignore these thoughts and implement a
more-general solution that would work for any (orderable) values,
not just small ranges of integers.&lt;/p&gt;


	&lt;h3&gt; Sketching the interface&lt;/h3&gt;


	&lt;p&gt;The first step, then, was to sketch out an interface.  Our
interface mirrored the one from the problem statement but
was tweaked for Haskell:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
&lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
&lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The function &lt;em&gt;mkRack&lt;/em&gt; makes a new rack to hold values (&amp;#8220;balls&amp;#8221;) of
type &lt;em&gt;a&lt;/em&gt;.  It&amp;#8217;s equivalent to &lt;code&gt;Rack.new&lt;/code&gt; in Ruby.&lt;/p&gt;


	&lt;p&gt;The &lt;em&gt;add&lt;/em&gt; function adds a ball to a rack.  You give it a ball and a
rack, and it returns a new rack that is the same as the original rack
but also contains the ball.  (If you&amp;#8217;re accustomed to stateful
programming, this may seem weird.  Why return a new rack instead of
modifying the original rack? Because, in Haskell, you can&amp;#8217;t change
values: you can only create new values.  At first, this constraint may
seem limiting, but after you get used to it, you&amp;#8217;ll find it
empowering.)&lt;/p&gt;


	&lt;p&gt;Note: the&lt;code&gt; Ord a &lt;/code&gt;qualification on the type signature of
&lt;em&gt;add&lt;/em&gt; says that it will work for any type &lt;em&gt;a&lt;/em&gt; whose values can be
ordered.  The qualification is necessary because values of some types,
like IO actions, cannot be compared to see which are less than the
others.&lt;/p&gt;


	&lt;p&gt;The &lt;em&gt;balls&lt;/em&gt; function is an &amp;#8220;observer&amp;#8221;: it lets you observe the balls
in a rack by returning them as an ordered list.&lt;/p&gt;


	&lt;p&gt;And that&amp;#8217;s the interface.&lt;/p&gt;


	&lt;p&gt;With the interface sketched, we gave it meaning by defining its
properties.&lt;/p&gt;


	&lt;h3&gt; Giving our interface meaning: defining properties using QuickCheck&lt;/h3&gt;


	&lt;p&gt;&lt;a href="http://www.md.chalmers.se/~rjmh/QuickCheck/"&gt;QuickCheck&lt;/a&gt; is a
powerful, easy-to-use testing tool.  Instead of checking test cases,
it checks &lt;em&gt;properties&lt;/em&gt; &amp;#8211; statements about what your code ought to do
&lt;em&gt;in general&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The great thing about QuickCheck properties is that they are
&lt;em&gt;testable documentation&lt;/em&gt;.  They tell the world what your code
is supposed to do,
and they do so in a concise, formal language that just happens to be
easily readable by humans and automatically testable by computers.&lt;/p&gt;


	&lt;p&gt;To specify the desired properties of our Rack interface, we first had
to import QuickCheck:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Test&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;QuickCheck&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Then, we defined our first property.  It said, simply, that a new rack
must be empty when observed:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_New&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Our second property said that, when you add a ball &lt;em&gt;x&lt;/em&gt; to
a rack, the resulting rack must contain the same
balls as the original rack plus &lt;em&gt;x&lt;/em&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_AddAddsElement&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Both of the properties above rely upon a special, order-insensitive
equality test that we defined for lists of &lt;code&gt;Int&lt;/code&gt; values:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;=~&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt;
&lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Note that under this test, &lt;code&gt;[1,2]&lt;/code&gt; &amp;#8220;equals&amp;#8221; 
both &lt;code&gt;[1,2]&lt;/code&gt; and &lt;code&gt;[2,1]&lt;/code&gt;, but it does not &amp;#8220;equal&amp;#8221; 
any other values.&lt;/p&gt;


	&lt;p&gt;The reason we defined this operator was to help us specify the two
essential properties of &lt;em&gt;add&lt;/em&gt; separately: (1) it must insert a ball
into a rack, and (2) the new ball&amp;#8217;s position, when observed, must
preserve the rack&amp;#8217;s ordering invariant.  The previous property
definition used the &lt;code&gt;=~&lt;/code&gt; operator to specify the first of
these two properties.  The next property we defined specified the
second:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_AddPreservesOrdering&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;==&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This definition specifies that, for all racks &lt;em&gt;rack&lt;/em&gt; and all balls
&lt;em&gt;x&lt;/em&gt;, if the balls in &lt;em&gt;rack&lt;/em&gt; are ordered, the balls in the rack that
results from adding &lt;em&gt;x&lt;/em&gt; to &lt;em&gt;rack&lt;/em&gt; must also be ordered.  If you
are familiar with &lt;a href="http://en.wikipedia.org/wiki/Mathematical_induction"&gt;proof by
induction&lt;/a&gt;, you&amp;#8217;ll
know why we went this route.  In short, if we can prove that this
property holds (and, trivially, that an empty rack is ordered), we can
prove that &lt;em&gt;add&lt;/em&gt; preserves the ordering invariant.&lt;/p&gt;


	&lt;p&gt;To round out the property definition, we needed to define the &lt;em&gt;isOrdered&lt;/em&gt; test:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;Int&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt;
&lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='varid'&gt;sort&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And those are the properties we needed to check the correctness
of our implementation.  Of course, we still needed to &lt;em&gt;write&lt;/em&gt; our
implementation, and we turned to that task next.&lt;/p&gt;


	&lt;h3&gt; A simple, list-based Rack implementation&lt;/h3&gt;


	&lt;p&gt;For our first implementation, we decided upon a drop-dead-simple
list-based representation.  We would keep the elements of the list
in sorted order by inserting them into the correct positions when
&lt;em&gt;add&lt;/em&gt; was called.&lt;/p&gt;


	&lt;p&gt;Here, then, was our code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our list-based implementation of a Rack&lt;/span&gt;

&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;Rack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;mkRack&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;span class='varid'&gt;balls&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;

&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;y&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;ys&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;&amp;lt;&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;         &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;otherwise&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;ys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;That&amp;#8217;s it.&lt;/p&gt;


	&lt;p&gt;We took our new implementation for a spin in GHCi:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; balls mkRack
[]

*Rack&amp;gt; balls (add 3 mkRack)
[3]

*Rack&amp;gt; balls (add 4 (add 3 mkRack))
[3,4]

*Rack&amp;gt; balls (add 1 (add 4 (add 3 mkRack)))
[1,3,4]

*Rack&amp;gt; balls (foldr add mkRack [4,2,6,3,-9,0,33,9])
[-9,0,2,3,4,6,9,33]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To &lt;em&gt;really&lt;/em&gt; test our implementation, we asked QuickCheck to check its
properties:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck prop_New
OK, passed 100 tests.

*Rack&amp;gt; quickCheck prop_AddAddsElement
OK, passed 100 tests.

*Rack&amp;gt; quickCheck prop_AddPreservesOrdering
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I should point out that QuickCheck did not &lt;em&gt;prove&lt;/em&gt; that our properties
held.  Rather, it gathered evidence that we could use to &lt;em&gt;argue&lt;/em&gt; that
our properties held.  The evidence was that each of our properties&amp;#8217;
claims was subjected to 100 randomly generated tests, and none of
the tests was able to disprove a claim.&lt;/p&gt;


	&lt;p&gt;Was this evidence sufficient for us to rest satisfied that our
implementation was correct?  Given how simple our implementation
was, I felt that the evidence was sufficient. Casey agreed, and we moved on.&lt;/p&gt;


	&lt;p&gt;With the first implementation done, we decided to try a more-sophisticated
implementation.&lt;/p&gt;


	&lt;h3&gt; Generalizing the interface&lt;/h3&gt;


	&lt;p&gt;Since we were about to have multiple implementations, it made sense
for us to define a generalized interface that any &amp;#8220;Rack-like&amp;#8221; 
implementation could use.  For that, Haskell&amp;#8217;s type classes were
perfect:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our interface for "Rack-like" data types&lt;/span&gt;

&lt;span class='keyword'&gt;class&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;ra&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The interface was essentially the same as before, except that the data
type behind the rack implementation was not given by a specific type
&lt;em&gt;Rack a&lt;/em&gt; but rather by the type variable &lt;em&gt;ra&lt;/em&gt;, which represents some
type of rack container for balls of type &lt;em&gt;a&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Note that &lt;em&gt;ra&lt;/em&gt; determines &lt;em&gt;a&lt;/em&gt;.  If, for example, you know that
the container type &lt;em&gt;ra&lt;/em&gt; equals &amp;#8220;a list of &lt;code&gt;Int&lt;/code&gt; values,&amp;#8221; 
you know that &lt;em&gt;a&lt;/em&gt; must equal &lt;code&gt;Int&lt;/code&gt;.  (To represent this
relationship, we used &lt;a href="http://haskell.org/haskellwiki/Functional_dependencies"&gt;functional
dependencies&lt;/a&gt;,
a popular extension to the Haskell 98 standard.)&lt;/p&gt;


	&lt;p&gt;With the &lt;em&gt;Racklike&lt;/em&gt; type class in place, we moved our list-based
implementation inside of the interface:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Our list-based implementation of a Rack&lt;/span&gt;

&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;ListRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;ListRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertList&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Next, we modified our QuickCheck property definitions.  Where before
it was fine to assume that we would be testing our single, list-based
implementation, now we needed to allow for testing other
implementation types.  We did this by adding a &lt;em&gt;rackType&lt;/em&gt; parameter to
our property definitions.  We used the type, not the value, of this
parameter to determine the type of rack to test:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;prop_New&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;prop_AddAddsElement&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;=~&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;

&lt;span class='varid'&gt;prop_AddPreservesOrdering&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;==&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;isOrdered&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;rack&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;rack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;ballList&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;rackType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Because we could no longer assume the rack would be represented
as a list of integers, we wrote &lt;em&gt;rackFromList&lt;/em&gt; to convert such
a list into a rack:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;foldr&lt;/span&gt; &lt;span class='varid'&gt;add&lt;/span&gt; &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;With these modifications in place, we re-ran our tests, specifying
(via type annotations) that we wanted to run them for the &lt;em&gt;ListRack&lt;/em&gt;
implementation:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck $ prop_New (undefined :: ListRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddAddsElement (undefined :: ListRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddPreservesOrdering (undefined :: ListRack Int)
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;A tree-based Rack implementation&lt;/h3&gt;


	&lt;p&gt;Now that we were free to add additional implementation types,
we created one based on binary trees.  We started by defining
the tree data type:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;
    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyword'&gt;deriving&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Ord&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Eq&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Show&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This definition says that a tree can be either empty or a root node.
A root node has a single value and left and right sub-trees.&lt;/p&gt;


	&lt;p&gt;Further, root nodes must satisfy an ordering invariant: if a root
node&amp;#8217;s value is &lt;em&gt;x&lt;/em&gt;, all of the values in its left subtree must be
less than &lt;em&gt;x&lt;/em&gt;, and all of the values in its right subtree must be
greater than or equal to &lt;em&gt;x&lt;/em&gt;.  The data type doesn&amp;#8217;t enforce this
invariant, so we would need to enforce it in our implementation.&lt;/p&gt;


	&lt;p&gt;Next, we wrote the basic functions for creating, adding elements to,
and observing our trees.&lt;/p&gt;


	&lt;p&gt;We needed to be able to create empty trees:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;emptyTree&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='conid'&gt;Empty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Inserting an element into a tree requires us to walk the tree and
append the element as a new leaf node in the correct location, being
mindful of our ordering invariant.  Because our data structure is
inherently recursive, a recursive implementation was straightforward
to code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;
&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;&amp;lt;&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt;         &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;otherwise&lt;/span&gt;     &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;y&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;insertTree&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Note that we don&amp;#8217;t try to ensure that the tree is balanced.  The
problem statement says that the balls are randomly selected, and thus
we can expect our trees, on average, to be balanced naturally.&lt;/p&gt;


	&lt;p&gt;Next, we wrote the code to observe the elements of a tree.
We used a functional-programming idiom
for efficiently flattening a tree into a list:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;elemsTree&lt;/span&gt; &lt;span class='varid'&gt;rx&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;rx&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='conid'&gt;Empty&lt;/span&gt;               &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;
&lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Root&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;left&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;elemsTree'&lt;/span&gt; &lt;span class='varid'&gt;right&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Finally, we defined a new tree-based rack type and declared
it to be an instance of the &lt;em&gt;Racklike&lt;/em&gt; type class:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Tree&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;

&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Racklike&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;mkRack&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;emptyTree&lt;/span&gt;
    &lt;span class='varid'&gt;add&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;insertTree&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;elemsTree&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;With the implementation done, we took it for a test drive:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; add 1 mkRack :: TreeRack Int
Root Empty 1 Empty

*Rack&amp;gt; add 3 (add 1 mkRack) :: TreeRack Int
Root Empty 1 (Root Empty 3 Empty)

*Rack&amp;gt; balls (add 3 (add 1 mkRack) :: TreeRack Int)
[1,3]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then, for the real test, we checked that our properties held for
TreeRacks:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; quickCheck $ prop_New (undefined :: TreeRack Int)
OK, passed 100 tests.

*Rack&amp;gt; quickCheck $ prop_AddAddsElement (undefined :: TreeRack Int)
OK, passed 100 tests.

quickCheck $ prop_AddPreservesOrdering (undefined :: TreeRack Int)
OK, passed 100 tests.
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Satisfied with these results,  we moved on to part two of the problem.&lt;/p&gt;


	&lt;h3&gt; The second part of the problem&lt;/h3&gt;


	&lt;p&gt;The second part of the problem was about sorting the letters within a
block of text, ignoring white space and punctuation, and converting
upper case letters into lower case: &amp;#8220;Are there any ways to
perform this sort cheaply, and without using built-in libraries?&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Again, a counting sort seemed like an obvious ideal solution, but
we decided to recycle our existing code since we had to leave soon.
Because our Rack implementations were generic, they would work on
letters just as well as on numbers or other kinds of balls:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; balls (rackFromList "this is a test" :: TreeRack Char)
"   aehiisssttt" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;With our existing code already doing the hard work
for us, it was trivial to code up the letter-sorting function:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;sortLetters&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;balls&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;rackFromList&lt;/span&gt; &lt;span class='varid'&gt;letters&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;TreeRack&lt;/span&gt; &lt;span class='conid'&gt;Char&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;letters&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;toLower&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;isAlpha&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;(Note: Because of the nature of the problem, I interpreted the
question&amp;#8217;s &amp;#8220;without using built-in libraries&amp;#8221; to mean &amp;#8220;without
built-in &lt;em&gt;sorting&lt;/em&gt; libraries.&amp;#8221;)&lt;/p&gt;


	&lt;p&gt;We took the new function for a test drive, and it worked
as expected:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*Rack&amp;gt; sortLetters "This is a test, pal." 
"aaehiilpsssttt" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And that ended our coding session.&lt;/p&gt;


&lt;div class="update"&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Tweaked the revised definition of the AddAddsElement
property for greater parallelism with the original.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update 2007-03-03:&lt;/strong&gt; Minor edits for clarity.&lt;/p&gt;
&lt;/div&gt;</description>
      <pubDate>Tue, 31 Oct 2006 14:44:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8704669e-0d37-4b41-9107-96f72ec20218</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/10/31/introductory-haskell-solving-the-sorting-it-out-kata</link>
      <category>programming</category>
      <category>functional programming</category>
      <category>haskell</category>
      <category>testing</category>
      <category>haskell</category>
      <category>testing</category>
      <category>kata</category>
      <category>sorting</category>
      <category>quickcheck</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/205</trackback:ping>
    </item>
    <item>
      <title>A type-based solution to the &amp;quot;strings problem&amp;quot;: a fitting end to XSS and SQL-injection holes?</title>
      <description>&lt;p&gt;Even skilled programmers have a hard time keeping their web
applications free of &lt;span class="caps"&gt;XSS&lt;/span&gt; and &lt;span class="caps"&gt;SQL&lt;/span&gt;-injection vulnerabilities.  And it
shows:  &lt;a href="http://portal.spidynamics.com/blogs/msutton/archive/2006/09/26/How-Prevalent-Are-SQL-Injection-Vulnerabilities_3F00_.aspx"&gt;a sobering portion of web sites are open to some scary security threats&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Why are so many sites vulnerable to these well-known holes?  Probably
because it&amp;#8217;s insanely hard for programmers to solve the fundamental
&amp;#8220;strings problem&amp;#8221; at the heart of these vulnerabilities. The problem
itself is easy to understand, but we humans aren&amp;#8217;t equipped to carry
out the solution.  Simply put, we just plain suck at keeping a
bazillion different strings straight in our heads, let alone
consistently and reliably rendering their interactions safe whenever they
cross paths in a modern web application.  It&amp;#8217;s easy to say, &amp;#8220;just
escape the little buggers,&amp;#8221; but it&amp;#8217;s hard to get it right, every single time.&lt;/p&gt;


	&lt;p&gt;Computers, on the other hand, are pretty good at keeping track of
details by the bucket-full. Wouldn&amp;#8217;t it be nice, then,
if our programming languages gave us the power to delegate this nasty &amp;#8220;strings
problem&amp;#8221; to our computers, which could then devote their unwavering mechanical precision to grinding the problem out of existence?  &lt;a href="http://weblog.raganwald.com/2006/03/ill-take-static-typing-for-800-alex.html" title="Raganwald: I'll take Static Typing for $800, Alex."&gt;Isn&amp;#8217;t that the kind of thing modern programming languages are supposed to be good at?&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d like to think the answer to that question is a big, &lt;em&gt;you betcha&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;So let&amp;#8217;s grab a modern programming language and solve the strings problem.&lt;/p&gt;


	&lt;h3&gt; Let&amp;#8217;s solve the strings problem in Haskell&lt;/h3&gt;


	&lt;p&gt;In this article, we will look at one way (among many) to solve the strings
problem: by adding Ruby-style string templates to Haskell.  These
templates support &amp;#8220;interpolation&amp;#8221; via the usual, convenient &lt;code&gt;#{var}&lt;/code&gt;
syntax, but here interpolation is type safe. Haskell&amp;#8217;s type system
will prevent us from inadvertently mixing incompatible string types,
and it will detect mistakes at compile time, before they can become
live &lt;span class="caps"&gt;XSS&lt;/span&gt; or &lt;span class="caps"&gt;SQL&lt;/span&gt;-injection holes.  Further, our solution will offer
us these benefits without making us jump through hoops or pay some
onerous syntax penalty.&lt;/p&gt;


	&lt;p&gt;To be more specific, the system offers the following benefits:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;It provides a string-management kernel that lets you create &amp;#8220;safe strings&amp;#8221; by &lt;em&gt;certifying&lt;/em&gt; a regular string as representing either text or a fragment of a known language.&lt;/li&gt;
		&lt;li&gt;It allows you to conveniently define new language types for any string-based language that you can provide an escaping rule for (e.g., &lt;span class="caps"&gt;XML&lt;/span&gt;, URLs, &lt;span class="caps"&gt;SQL&lt;/span&gt;, untrusted user input).&lt;/li&gt;
		&lt;li&gt;It provides compile-time syntactic sugar (via Template Haskell) that makes working with safe strings as convenient as working with string interpolation in languages like Ruby and Perl.&lt;/li&gt;
		&lt;li&gt;It catches and reports (at compile time) the following commonly made programming errors:
	&lt;ul&gt;
	&lt;li&gt;failing to escape a plain-old-text string before mixing it into a string that represents a language fragment&lt;/li&gt;
		&lt;li&gt;mixing strings that represent fragments of incompatible languages&lt;/li&gt;
		&lt;li&gt;mixing strings that represent fragments of compatible languages in an ambiguous way (the system will force you to disambiguate)&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;(This is a long one, so grab an espresso, lean back, and read on in
style.  Also, if you have a smoking jacket, you might want to get it now.)&lt;/p&gt;&lt;p&gt;Before I describe this Haskell-based solution, let&amp;#8217;s take a closer
look at the strings problem and review why a type-based approach makes
sense.  (If you already understand the strings problem and are
convinced that it is both important and tricky to solve, feel free
to skim the first third of this article.)&lt;/p&gt;


	&lt;h3&gt; Examining the &amp;#8220;strings problem&amp;#8221;&lt;/h3&gt;


	&lt;p&gt;Most web applications are just business-logic-driven string processors.  They
take strings from user-submitted forms, database queries, web-service
responses, templates, and myriad other sources, and they combine the
strings to generate yet more strings, which they emit as output and
fling across the Internet, into your web browser.&lt;/p&gt;


	&lt;p&gt;For example, consider this snippet of Ruby (on Rails) code that I used &lt;a href="http://blog.moertel.com/articles/2006/08/09/adding-reddit-and-del-icio-us-buttons-to-articles-in-typo"&gt;to
add submit-to-Reddit and submit-to-del.icio.us
buttons&lt;/a&gt;
to articles on my blog:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def submit_this_article_links(article)
  site_list(article).map do |submit_title, submit_url, image_tag|
    %(&amp;lt;a href="#{h submit_url}" 
         title="#{h submit_title}: &amp;amp;#x201C;#{h article.title}&amp;amp;#x201D;" 
      &amp;gt;#{image_tag}&amp;lt;/a&amp;gt;)
  end.join("&amp;amp;#160;")
end

def site_list(article)
  u_title = u(article.title)
  u_url = u(url_of(article, false))
  [  # I really belong in a database table
    [ "Submit to Reddit.com",
      "http://reddit.com/submit?url=#{u_url}&amp;#38;title=#{u_title}",
      image_tag("reddit.gif", :size =&amp;gt; "18x18", :border =&amp;gt; 0)
    ],
    [ "Save to del.icio.us",
      "http://del.icio.us/post?v=2&amp;#38;url=#{u_url}&amp;#38;title=#{u_title}",
      image_tag("delicious.gif", :size =&amp;gt; "16x16", :border =&amp;gt; 0)
    ]
  ]
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;When writing this code, I had to keep track of at least three
different kinds of strings:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Plain-old text&lt;/strong&gt;, e.g., article titles&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;URLs&lt;/strong&gt;, e.g., article permalinks&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;&lt;span class="caps"&gt;XHTML&lt;/span&gt; fragments&lt;/strong&gt;, e.g., the hypertext link to Reddit&amp;#8217;s submission form&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;In code like this, each type of string must conform to the
requirements of its own little language, and it&amp;#8217;s the programmer&amp;#8217;s job &amp;#8211; your job &amp;#8211; to make sure that differences in these requirements are accounted for
when combining strings.  Getting it right is a
difficult trick to pull off, and getting it right consistently is
&lt;a href="http://blog.moertel.com/articles/2006/10/12/if-unit-testing-cant-keep-rails-safe-from-string-escaping-problems-what-makes-you-think-it-will-keep-your-projects-safe"&gt;something even the best developers have difficulty doing&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;In the tiny snippet of code above, for example, I had to remember to
do all of these things:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;span class="caps"&gt;URL&lt;/span&gt;-escape (using the &lt;code&gt;u&lt;/code&gt; helper method) the article&amp;#8217;s title before inserting it into the submit-URL template&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;URL&lt;/span&gt;-escape the &lt;span class="caps"&gt;URL&lt;/span&gt; for the article&amp;#8217;s permalink before inserting it into the submit-URL template&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt;-escape (using the &lt;code&gt;h&lt;/code&gt; helper method) the final, expanded submit-URL template before inserting it into the hypertext-link template&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt;-escape the submit-title (e.g., &amp;#8220;Submit to Reddit&amp;#8221;) before inserting it into the hypertext-link template&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt;-escape the article&amp;#8217;s title before inserting it into the hypertext-link template&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;That&amp;#8217;s a lot to keep track of when coding.&lt;/p&gt;


	&lt;p&gt;But that&amp;#8217;s not all.  I also had to know &lt;em&gt;not&lt;/em&gt; to escape the result of
calling &lt;code&gt;image_tag&lt;/code&gt;, because that helper method returns
an &lt;span class="caps"&gt;HTML&lt;/span&gt; fragment, which is already in the language of the
hypertext-link template into which it is inserted.  Escaping it would
have turned the image-element markup into embedded text that happens
to look a lot like &lt;span class="caps"&gt;HTML&lt;/span&gt; markup.&lt;/p&gt;


	&lt;p&gt;And that&amp;#8217;s not the worst of it.  If you screw up any one of these
steps for the typical web application, you open
the door to a host of nasty problems.  If you&amp;#8217;re lucky, the damage
will be contained to broken links or a rendering problem that
most people won&amp;#8217;t notice, maybe a weird database error now and again.
In the worst case, however, you&amp;#8217;re screwed: Your application&amp;#8217;s
customers become vulnerable to &lt;a href="http://en.wikipedia.org/wiki/Cross_site_scripting"&gt;cross-site-scripting (XSS)
attacks&lt;/a&gt; and your
database is opened to &lt;a href="http://en.wikipedia.org/wiki/SQL_injection"&gt;injected
&lt;span class="caps"&gt;SQL&lt;/span&gt;&lt;/a&gt;, through which
enterprising crackers might steal your customers&amp;#8217; account data
or do even nastier things.&lt;/p&gt;


	&lt;p&gt;Clearly, the strings problem is common enough and nasty enough to merit
our attention.  Many of our favorite problem-stomping practices,
however, have not proved effective on the ever-tricky strings problem.&lt;/p&gt;


	&lt;h3&gt;Unit testing is an inefficient solution to the strings problem&lt;/h3&gt;


	&lt;p&gt;Unit testing is one of the most efficient programming practices for
increasing the quality of software.  If you write unit tests pervasively
as you code, you are likely to nip many kinds of programming problems
in the bud, saving time and effort, which you can then re-invest in
your code.  Further, unit-testing suites make for swell
regression-detection nets and thus free you to refactor crufty code
without fear of introducing breakage elsewhere.  As a result, you&amp;#8217;re
more likely to keep your code lean and mean.&lt;/p&gt;


	&lt;p&gt;Despite its general effectiveness, unit testing is an inefficient way
to defend against the perils of the strings problem.  That&amp;#8217;s because
the strings problem is caused by knowledge deficits, which you can&amp;#8217;t
test for.  If you don&amp;#8217;t realize that you must escape one &lt;span class="caps"&gt;URL&lt;/span&gt;
before you stuff it into another &lt;span class="caps"&gt;URL&lt;/span&gt;, you probably won&amp;#8217;t think to
write tests for that requirement.&lt;/p&gt;


	&lt;p&gt;Moreover, if you do think to write the tests, it&amp;#8217;s expensive to get
them right.  In most unit testing scenarios, getting the tests right
is usually easier or at least comparable in difficulty to getting the
code that&amp;#8217;s being tested right.  That&amp;#8217;s why unit testing is usually
so efficient.  For the strings problem, however, getting
the tests right is often much more expensive than writing typical
string-handling code.  In my code sample
above, for example, there are at least six ways the strings problem
can cause trouble.  How do you test for them all without making
a mistake?  It&amp;#8217;s not easy.&lt;/p&gt;


	&lt;p&gt;In sum, unit testing probably isn&amp;#8217;t the answer to the strings problem.&lt;/p&gt;


	&lt;h3&gt;Other solutions to the strings problem&lt;/h3&gt;


	&lt;p&gt;If unit testing isn&amp;#8217;t the answer, what is?&lt;/p&gt;


	&lt;p&gt;Joel Spolsky wrote about
the strings problem and &lt;a href="http://www.joelonsoftware.com/articles/Wrong.html"&gt;suggested that using Hungarian notation was
an effective
solution&lt;/a&gt;.
It might work, but it&amp;#8217;s clunky.&lt;/p&gt;


	&lt;p&gt;In the database-programming world, many programmers have adopted the
convention of never inserting a string into a &lt;span class="caps"&gt;SQL&lt;/span&gt; template by hand.
Instead, they insert placeholders, typically question marks,
into a template to indicate where they would like strings to be
inserted.  The template and the strings are then given
to a special function that safely inserts the strings, escaping them
as necessary.  In Ruby on Rails, which has a fairly typical
implementation, template expansion looks like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;Post.find_by_sql \
  [ "SELECT * FROM posts WHERE author = ? AND created &amp;gt; ?",
    author_id, start_date ]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The question-marks-in-the-template solution is effective, but it&amp;#8217;s
also clunky, especially when you&amp;#8217;re trying to insert a lot of strings.
By comparison, Ruby&amp;#8217;s native string-interpolation feature, in which the syntax
&lt;code&gt;#{...}&lt;/code&gt; lets us inject strings into a string template, is
unsafe but much easier to follow:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;chunkiness = "extra chunky" 
"I love #{chunkiness} bacon!" 
# ==&amp;gt; "I love extra chunky bacon!" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In sum, the Hungarian-notation solution and the question-marks
solution are reasonable responses to the strings problem, but both are
clunky, especially when compared to the straightforwardness of
good-old string interpolation.&lt;/p&gt;


	&lt;p&gt;Perhaps we can do better.&lt;/p&gt;


	&lt;h3&gt; Eating and having one&amp;#8217;s cake: a type-based solution&lt;/h3&gt;


	&lt;p&gt;An ideal solution would combine the safety of the question-marks
solution with the straightforward convenience of string interpolation,
and it would work for all kinds of strings, not just &lt;span class="caps"&gt;SQL&lt;/span&gt;, and, because
I&amp;#8217;m implementing it in Haskell, it would lovingly nestle into
Haskell&amp;#8217;s type system and gain the full benefits of type-inferencing
goodness.&lt;/p&gt;


	&lt;p&gt;How would it work?  Well, let&amp;#8217;s back up and think about strings for a
moment.  We can divide strings into two classes: (1) those that
represent text, in which every character represents literally itself;
and (2) those that represent fragments of interpreted languages, such
as &lt;span class="caps"&gt;XML&lt;/span&gt; or &lt;span class="caps"&gt;SQL&lt;/span&gt;, where each character&amp;#8217;s interpretation depends on the
rules of the associated language.  In text, for example, an ampersand
(&amp;#8220;&amp;#38;&amp;#8221;) represents an ampersand, but in &lt;span class="caps"&gt;XML&lt;/span&gt; an ampersand represents the
start of a character-entity reference.&lt;/p&gt;


	&lt;p&gt;It doesn&amp;#8217;t make sense, then, to join text strings directly with
language-fragment strings.  If you did join them, text characters
could be misinterpreted as language characters.  For the same reason,
it doesn&amp;#8217;t make sense to join fragments of different languages
together.  (It does make sense, however, to &lt;em&gt;escape&lt;/em&gt; text strings or
language fragments &amp;#8220;into&amp;#8221; a target language and &lt;em&gt;then&lt;/em&gt; join them with
strings in the target language.)&lt;/p&gt;


	&lt;p&gt;A sound solution, therefore, should enforce the following fundamental,
safe-string-handling rule: &lt;em&gt;Do not allow strings that represent
fragments of one language to be directly joined with strings that
represent either plain text or fragments of another language&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The trick is making the computer enforce this rule for us.  As
it turns out, modern type systems absolutely love to do this kind of thing.&lt;/p&gt;


	&lt;h3&gt; A solution to the strings problem in Haskell&lt;/h3&gt;


	&lt;p&gt;Making the computer enforce our safe-string-handling rule in Haskell
is fairly easy.  All it takes is a little code.
(As we go through the following code, remember that
we&amp;#8217;re writing a library.  Normally, as users of the library, this
code would be invisible to us.)&lt;/p&gt;


	&lt;p&gt;To begin, we create a module for our code and export
the essential types and functions that make up our about-to-be-written
safe-string kernel:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;SafeStrings&lt;/span&gt;
&lt;span class='layout'&gt;(&lt;/span&gt;
  &lt;span class='conid'&gt;Language&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;..&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='comment'&gt;-- we export the data type but not the constructors&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;empty&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;frag&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;cat&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;+++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;render&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;renders&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;lang&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;q&lt;/span&gt;
&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;declareSafeString&lt;/span&gt;
&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='keyword'&gt;where&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In order to create safe strings that correspond to particular
languages, we need to tell the computer what we mean by &lt;em&gt;Language&lt;/em&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;class&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;litfrag&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;   &lt;span class='comment'&gt;-- String is a literal language fragment&lt;/span&gt;
    &lt;span class='varid'&gt;littext&lt;/span&gt;  &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;   &lt;span class='comment'&gt;-- String is literal text&lt;/span&gt;
    &lt;span class='varid'&gt;natrep&lt;/span&gt;   &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt;   &lt;span class='comment'&gt;-- Gets the native-language representation&lt;/span&gt;
    &lt;span class='varid'&gt;language&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt;   &lt;span class='comment'&gt;-- Gets the name of the language&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Here we&amp;#8217;re saying that &lt;em&gt;Language&lt;/em&gt; is the class of languages, i.e., all
data types &lt;em&gt;l&lt;/em&gt; for which we can provide four functions:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;em&gt;litfrag&lt;/em&gt; &amp;#8211; converts a string that represents a language fragment into a language fragment&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;littext&lt;/em&gt; &amp;#8211; converts a string that represents plain text into a language fragment that represents the text (via escaping)&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;natrep&lt;/em&gt; &amp;#8211;  converts a language fragment, verbatim, into a string that represents the language fragment&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;language&lt;/em&gt; &amp;#8211; returns the name of the language associated with a given fragment&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Further, we need to declare a few &amp;#8220;language laws&amp;#8221; that conforming
&lt;em&gt;Language&lt;/em&gt; types must obey.  These laws are for us.  They will keep us
honest when teaching the computer about new languages.  Here are the
two laws we will require language types to satisfy:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;em&gt;natrep&lt;/em&gt; (&lt;em&gt;litfrag&lt;/em&gt; &lt;em&gt;s&lt;/em&gt;) &lt;code&gt;==&lt;/code&gt; &lt;em&gt;s&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;natrep&lt;/em&gt; (&lt;em&gt;littext&lt;/em&gt; &lt;em&gt;s&lt;/em&gt;) &lt;code&gt;==&lt;/code&gt; (&lt;em&gt;escape&lt;sub&gt;L&lt;/sub&gt;&lt;/em&gt; &lt;em&gt;s&lt;/em&gt;)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;The first law requires that (&lt;em&gt;natrep&lt;/em&gt;&amp;#160;.&amp;#160;&lt;em&gt;litfrag&lt;/em&gt;) be
equivalent to the identity function for strings.  The second law
requires that (&lt;em&gt;natrep&lt;/em&gt;&amp;#160;.&amp;#160;&lt;em&gt;littext&lt;/em&gt;) be equivalent to
the text-escaping function for a given language &lt;em&gt;L&lt;/em&gt;.  For example,
for the language &lt;span class="caps"&gt;XML&lt;/span&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;natrep (litfrag "&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;") ==&amp;gt; "&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;" 
natrep (littext "ham &amp;#38; eggs")    ==&amp;gt; "ham &amp;amp;amp; eggs" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, let&amp;#8217;s construct a type-safe container for strings having
a known language:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SSEmpty&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SSFragment&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SSCat&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This data-type definition says that if &lt;em&gt;l&lt;/em&gt; is a language, we
can construct &lt;em&gt;SafeString&lt;/em&gt; values for that language.  Each value can
represent an empty fragment of the language (via &lt;em&gt;SSEmpty&lt;/em&gt;), a
non-empty fragment of the language (via &lt;em&gt;SSFragment&lt;/em&gt;), or the
concatenation of two other &lt;em&gt;SafeString&lt;/em&gt; values for the language
(via &lt;em&gt;SSCat&lt;/em&gt;).&lt;/p&gt;


	&lt;p&gt;Now comes the interesting part.  We are going to use the type
system to enforce the safe-string-handling rule for us.&lt;/p&gt;


	&lt;p&gt;We will do this using the &lt;em&gt;SafeString&lt;/em&gt; data type we just defined.
We have already placed the data type&amp;#8217;s definition into a module that
does &lt;em&gt;not&lt;/em&gt; export the type&amp;#8217;s data constructors.  That means we will not
be able to create &lt;em&gt;SafeString&lt;/em&gt; values for ourselves.  Instead, we must
ask a small set of kernel functions, which &lt;em&gt;are&lt;/em&gt; exported, to create the
values on our behalf.&lt;/p&gt;


	&lt;p&gt;These kernel functions, which we are about to write,
will create &lt;em&gt;SafeString&lt;/em&gt; values only in accordance with our
safe-string-handling rule.  In particular, they will require us
to &lt;em&gt;certify&lt;/em&gt; that an existing string represents either text or a language
fragment before creating a corresponding &lt;em&gt;SafeString&lt;/em&gt; value
for us.  From then on, the type system will know
which language the string is associated with and prevent us from
joining it to regular strings or to &lt;em&gt;SafeString&lt;/em&gt; values associated
with other languages.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s write these constructor functions now:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;empty&lt;/span&gt;      &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
&lt;span class='varid'&gt;empty&lt;/span&gt;       &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SSEmpty&lt;/span&gt;

&lt;span class='varid'&gt;frag&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
&lt;span class='varid'&gt;frag&lt;/span&gt; &lt;span class='varid'&gt;f&lt;/span&gt;      &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SSFragment&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;litfrag&lt;/span&gt; &lt;span class='varid'&gt;f&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;      &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SSFragment&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;littext&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Here&amp;#8217;s what the functions do:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;em&gt;empty&lt;/em&gt; &amp;#8211; creates an empty &lt;em&gt;SafeString&lt;/em&gt; in the &lt;em&gt;Language l&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;frag f&lt;/em&gt; &amp;#8211; takes a string that you certify as representing a fragment in the &lt;em&gt;Language l&lt;/em&gt; and returns a corresponding &lt;em&gt;SafeString&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;text s&lt;/em&gt; &amp;#8211; takes a string that you certify as representing text and returns a corresponding &lt;em&gt;SafeString&lt;/em&gt; in the &lt;em&gt;Language l&lt;/em&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Once the kernel creates &lt;em&gt;SafeString&lt;/em&gt; values for us, we need some way
to combine them safely.  Thus we define the &lt;code&gt;(+++)&lt;/code&gt;
operator and the &lt;em&gt;cat&lt;/em&gt; function:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- join two SafeStrings of the same language&lt;/span&gt;
&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;+++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;+++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SSCat&lt;/span&gt;

&lt;span class='comment'&gt;-- join a list of same-language SafeStrings&lt;/span&gt;
&lt;span class='varid'&gt;cat&lt;/span&gt;   &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;
&lt;span class='varid'&gt;cat&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;foldr&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;+++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varid'&gt;empty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Finally, we need a way to convert &lt;em&gt;SafeString&lt;/em&gt; values into normal
strings so that we can pass them through the boundaries of our
safe-string-protected code and into the outside world.  For this,
we write the &lt;em&gt;render&lt;/em&gt; function:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;render&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='str'&gt;""&lt;/span&gt;

&lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='conid'&gt;SSEmpty&lt;/span&gt;        &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;id&lt;/span&gt;
&lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;SSFragment&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;natrep&lt;/span&gt; &lt;span class='varid'&gt;a&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;SSCat&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='varid'&gt;r&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='varid'&gt;r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;(Don&amp;#8217;t worry about the &lt;em&gt;renders&lt;/em&gt; stuff.  It implements
a Haskell idiom for fast string concatenation.)&lt;/p&gt;


	&lt;p&gt;As a convenience, let&amp;#8217;s round out our kernel with a &lt;em&gt;Show&lt;/em&gt; instance
that tells Haskell how to format
&lt;em&gt;SafeString&lt;/em&gt; values for display.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt; &lt;span class='keyglyph'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Show&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='varid'&gt;l&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;showsPrec&lt;/span&gt; &lt;span class='keyword'&gt;_&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
        &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;lang&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='str'&gt;":\""&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;renders&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='chr'&gt;'"'&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;lang&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='keyword'&gt;let&lt;/span&gt; &lt;span class='conid'&gt;SSFragment&lt;/span&gt; &lt;span class='varid'&gt;e&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;ss&lt;/span&gt; &lt;span class='keyword'&gt;in&lt;/span&gt; &lt;span class='varid'&gt;language&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;undefined&lt;/span&gt; &lt;span class='varop'&gt;`asTypeOf`&lt;/span&gt; &lt;span class='varid'&gt;e&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And that&amp;#8217;s our SafeStrings kernel.&lt;/p&gt;


	&lt;h3&gt; Another look at the SafeStrings kernel&lt;/h3&gt;


	&lt;p&gt;The following illustration, complete with poorly chosen colors, provides a
visual summary of our system:&lt;/p&gt;


&lt;p style="text-align: center"&gt;
&lt;img src="http://community.moertel.com/~thor/pix/20060908/safe-strings.png" title="Stunning visual interpretation of the SafeStrings kernel and its relationship to the evil outside world" alt="Stunning visual interpretation of the SafeStrings kernel and its relationship to the evil outside world" /&gt;
&lt;/p&gt;

	&lt;p&gt;(Don&amp;#8217;t worry about the &lt;code&gt;$(q ...)&lt;/code&gt; stuff for the
moment, we&amp;#8217;ll talk about it later.)&lt;/p&gt;


	&lt;p&gt;Activating our mad art-interpretation skillz, we can
now decipher the illustration:&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Regular strings gain &amp;#8220;admittance&amp;#8221; to the SafeStrings kernel only
via the &lt;/em&gt;text&lt;em&gt; and &lt;/em&gt;frag&lt;em&gt; certification functions, which
we use to create corresponding safe strings for a given language.
Once created, the safe strings live their entire lives in the
fleshy-colored, egg-shaped protective sac that is the kernel, whose
safe-string functions and operators use Haskell&amp;#8217;s type system to
prevent us from accidentally mixing the strings in unsafe
ways. Further, because the kernel does not export its underlying data
structures, we can&amp;#8217;t screw around with the innards of our safe strings to
break the kernel&amp;#8217;s promises.  When our safe strings have finally
reached their ultimate, beautiful state, we can &lt;/em&gt;render&lt;em&gt; them
into regular strings and pass them bravely into the cruel outside
world &amp;#8211; where, most likely, somebody else&amp;#8217;s broken code will screw
them up anyway.  But at least we tried.&lt;/em&gt;&lt;/p&gt;


	&lt;h3&gt;Our first SafeString module: SafeXml&lt;/h3&gt;


	&lt;p&gt;Now that we have written our SafeStrings kernel, let&amp;#8217;s use it to
create a SafeXml module that we can use for working with &lt;span class="caps"&gt;XML&lt;/span&gt;.
Again, we will be writing library code that under normal
circumstances would be hidden from view.&lt;/p&gt;


	&lt;p&gt;First, we will create a new module that uses the SafeStrings kernel:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;SafeXml&lt;/span&gt;
&lt;span class='layout'&gt;(&lt;/span&gt; &lt;span class='conid'&gt;Xml&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;xml&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;renderXml&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;SafeStrings&lt;/span&gt; &lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='keyword'&gt;where&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;SafeStrings&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Next, we will create a wrapper type to testify
that a string represents a fragment of &lt;span class="caps"&gt;XML&lt;/span&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;newtype&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt;
    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt; &lt;span class='layout'&gt;{&lt;/span&gt; &lt;span class='varid'&gt;unXmlString&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='layout'&gt;}&lt;/span&gt;
    &lt;span class='keyword'&gt;deriving&lt;/span&gt; &lt;span class='conid'&gt;Show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;If you go back and look at the export list for the module, you&amp;#8217;ll see
that the &lt;em&gt;XmlString&lt;/em&gt; data type is not exported.  It is internal to the
module, and thus we, as clients of the module, can&amp;#8217;t create values of
that type.  That means we can&amp;#8217;t &amp;#8220;forge&amp;#8221; &lt;span class="caps"&gt;XML&lt;/span&gt; strings into existence.
We can create them only through the safe-string kernel, and even then
only by certifying a regular string as representing text or a language
fragment.  (The kernel, in turn, will create the needed values through
the &lt;em&gt;Language&lt;/em&gt; interface, which we now discuss.)&lt;/p&gt;


	&lt;p&gt;Like all good language types, &lt;em&gt;XmlString&lt;/em&gt; needs to be a member of the
&lt;em&gt;Language&lt;/em&gt; type class, so we provide the necessary instance functions:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;instance&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;litfrag&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt;
    &lt;span class='varid'&gt;littext&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;escapeXml&lt;/span&gt;
    &lt;span class='varid'&gt;natrep&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;unXmlString&lt;/span&gt;
    &lt;span class='varid'&gt;language&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;const&lt;/span&gt; &lt;span class='str'&gt;"xml"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Note that the functions satisfy the language laws
we defined earlier.  (The proof follows immediately from the definitions
of &lt;em&gt;XmlString&lt;/em&gt;, &lt;em&gt;unXmlString&lt;/em&gt;, and &lt;em&gt;escapeXml&lt;/em&gt;.)&lt;/p&gt;


	&lt;p&gt;Next, we need to write a function to implement the escaping
rule for &lt;span class="caps"&gt;XML&lt;/span&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;escapeXml&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;concatMap&lt;/span&gt; &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='chr'&gt;'&amp;lt;'&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;lt;"&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='chr'&gt;'&amp;gt;'&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;gt;"&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='chr'&gt;'&amp;amp;'&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;amp;"&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='chr'&gt;'"'&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;#34;"&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='chr'&gt;'\''&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;#39;"&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Next, because we expect to work with &lt;span class="caps"&gt;XML&lt;/span&gt; frequently, we will create a
convenient type synonym, &lt;em&gt;Xml&lt;/em&gt;, for &lt;em&gt;SafeString&lt;/em&gt; values that represent
&lt;span class="caps"&gt;XML&lt;/span&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;Xml&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SafeString&lt;/span&gt; &lt;span class='conid'&gt;XmlString&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Finally, we will create
a few convenience functions to create and render &lt;span class="caps"&gt;XML&lt;/span&gt; fragments.  These
functions are identical to the SafeString kernel&amp;#8217;s &lt;em&gt;frag&lt;/em&gt; and &lt;em&gt;render&lt;/em&gt;
functions but for the &lt;em&gt;Xml&lt;/em&gt; type exclusively.  When we use these
functions, we won&amp;#8217;t need to provide additional type annotations; the
computer will know we are dealing with &lt;span class="caps"&gt;XML&lt;/span&gt; strings:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;xml&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Xml&lt;/span&gt;
&lt;span class='varid'&gt;xml&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;frag&lt;/span&gt;

&lt;span class='varid'&gt;renderXml&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Xml&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt;
&lt;span class='varid'&gt;renderXml&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;render&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;And we&amp;#8217;re done.&lt;/p&gt;


	&lt;p&gt;Before going on, let me point out two things:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;If you think the code we have written so far is long or perhaps confusing, please remember that it is &lt;em&gt;library code&lt;/em&gt;.  Typically, you would never see it.  All you would do is &lt;code&gt;import SafeXml&lt;/code&gt; and start using the library.&lt;/li&gt;
		&lt;li&gt;The SafeXml implementation is formulaic, and we can replace all of it except for the escaping function&amp;#8217;s definition with a single line of code, something we will do later.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;h3&gt; A quick test drive of our SafeXml module&lt;/h3&gt;


	&lt;p&gt;Let&amp;#8217;s give our SafeXml module a spin in the &lt;span class="caps"&gt;GHC&lt;/span&gt; interactive shell.&lt;/p&gt;


	&lt;p&gt;We can create an &lt;span class="caps"&gt;XML&lt;/span&gt; fragment by certifying that a regular string
represents a language fragment (via the &lt;em&gt;frag&lt;/em&gt; function) and telling
Haskell that we expect a result of type &lt;em&gt;Xml&lt;/em&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;Ok, modules loaded: SafeXml, SafeStrings.
*SafeXml&amp;gt; frag "&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;" :: Xml
xml:"&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Note how the output is prefixed with the label &amp;#8220;xml:&amp;#8221; 
to tell us that our kernel certifies this value to represent an &lt;span class="caps"&gt;XML&lt;/span&gt; fragment.&lt;/p&gt;


	&lt;p&gt;Because entering type annotations can be inconvenient, we can instead
use the &lt;em&gt;xml&lt;/em&gt; function, which certifies a string not just as a
fragment but as an &lt;span class="caps"&gt;XML&lt;/span&gt; fragment:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*SafeXml&amp;gt; xml "&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;" 
xml:"&amp;lt;em&amp;gt;wow!&amp;lt;/em&amp;gt;" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If we want to represent text in &lt;span class="caps"&gt;XML&lt;/span&gt;, the kernel will automatically
escape it for us:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*SafeXml&amp;gt; text "ham &amp;#38; eggs" :: Xml
xml:"ham &amp;amp;amp; eggs" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now let&amp;#8217;s try to do something naughty.  Will the type system
let us?&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*SafeXml&amp;gt; let someXml = xml "&amp;lt;em&amp;gt;Hi!&amp;lt;/em&amp;gt;" 
*SafeXml&amp;gt; let plainOldText = "ham &amp;#38; eggs" 
*SafeXml&amp;gt; someXml ++ plainOldText

&amp;lt;interactive&amp;gt;:1:0:
    Couldn't match `[a]' against `Xml'
      Expected type: [a]
      Inferred type: Xml
    In the first argument of `(++)', namely `someXml'
    In the definition of `it': it = someXml ++ plainOldText
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In Haskell, the &lt;code&gt;(++)&lt;/code&gt; operator is used (among
other things) to join strings.  In the code above, we tried
to use this operator to join an &lt;span class="caps"&gt;XML&lt;/span&gt; fragment to a plain-old
string, which would have violated our safe-string-handling rule.
Fortunately, we were unable to fool the type system into
allowing this ill-conceived union to occur.&lt;/p&gt;


	&lt;p&gt;In fact, the union was never even attempted: our mistake was caught at
compile time, before the code was ever converted into executable form.
This is a big deal. Mistakes like this are &lt;em&gt;programming errors&lt;/em&gt; that
open security holes.  Being able to catch these errors at compile time
means you have the opportunity to track the errors to their source and
fix them there.  If you caught ill-conceived string unions only at run
time, the logical errors that led to the attempted unions could have
been in upstream code that has already executed &amp;#8211; launching the
missiles, perhaps.  By then, it may be too late to undo the
consequences.&lt;/p&gt;


	&lt;p&gt;Returning to our example, if we certify that the plain-old string
represents text, we can make a safe union, so the type system
lets us go ahead:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;*SafeXml&amp;gt; someXml +++ text plainOldText
xml:"&amp;lt;em&amp;gt;Hi!&amp;lt;/em&amp;gt;ham &amp;amp;amp; eggs" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And that&amp;#8217;s basically all there is to it.&lt;/p&gt;


	&lt;h3&gt;Syntactic sugar for safe strings&lt;/h3&gt;


	&lt;p&gt;Not having to worry about the strings problem is fabulous and
all, but having to type in &lt;em&gt;frag&lt;/em&gt;, &lt;em&gt;text&lt;/em&gt;, and &lt;code&gt;+++&lt;/code&gt; is
kind of clunky.  Let&amp;#8217;s get rid of the clunkiness by introducing some
syntactic sugar.&lt;/p&gt;


&lt;p&gt;The common case when dealing with strings in web applications is
templates.  For example, here&amp;#8217;s a simplified version of the
&lt;code&gt;link_to&lt;/code&gt; method from the deservedly popular &lt;a href="http://www.rubyonrails.com/"&gt;Ruby on
Rails&lt;/a&gt;.  The method wraps a hypertext link
around some content by &amp;#8220;interpolating&amp;#8221; the content and a &lt;span class="caps"&gt;URL&lt;/span&gt;
into a link template:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# NOTE: this example is in Ruby

def link_to(content_xhtml, url)
  "&amp;lt;a href=\"#{h url}\"&amp;gt;#{content_xhtml}&amp;lt;/a&amp;gt;" 
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In this code, we need to &lt;span class="caps"&gt;HTML&lt;/span&gt;-escape the &lt;span class="caps"&gt;URL&lt;/span&gt; (via the &lt;code&gt;h&lt;/code&gt;
helper) before interpolating it
into the template.  We do not need to escape the content, however,
because it is already in the template&amp;#8217;s language, &lt;span class="caps"&gt;XHTML&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Now, to introduce our syntactic sugar, here&amp;#8217;s &lt;code&gt;link_to&lt;/code&gt;
rewritten in Haskell and using safe strings:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='comment'&gt;-- Haskell code&lt;/span&gt;

&lt;span class='varid'&gt;link_to&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Xhtml&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Url&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Xhtml&lt;/span&gt;
&lt;span class='varid'&gt;link_to&lt;/span&gt; &lt;span class='varid'&gt;content&lt;/span&gt; &lt;span class='varid'&gt;url&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;q&lt;/span&gt; &lt;span class='str'&gt;"&amp;lt;a href=\"#{r url}\"&amp;gt;#{=content}&amp;lt;/a&amp;gt;"&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The type signature makes clear to everybody that the &lt;em&gt;content&lt;/em&gt;
parameter is &lt;span class="caps"&gt;XHTML&lt;/span&gt;, the &lt;em&gt;url&lt;/em&gt; parameter is a &lt;span class="caps"&gt;URL&lt;/span&gt;, and the result is
&lt;span class="caps"&gt;XHTML&lt;/span&gt;.  The signature isn&amp;#8217;t needed, but &lt;code&gt;link_to&lt;/code&gt; is the
stuff of libraries, and so annotations are good form.&lt;/p&gt;


	&lt;p&gt;The interpolation syntax is like Ruby&amp;#8217;s, but with
slightly different modifiers:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;The template-quoting syntax is &lt;code&gt;$(q "this is a template")&lt;/code&gt;.  (Mnemonic: &lt;code&gt;q&lt;/code&gt; for quote).&lt;/li&gt;
		&lt;li&gt;Within a template, we can interpolate variables using the familiar &lt;code&gt;#{var}&lt;/code&gt; syntax.&lt;/li&gt;
		&lt;li&gt;If an interpolated variable holds a plain string, it will be escaped into the template automatically.&lt;/li&gt;
		&lt;li&gt;If an interpolated variable holds a safe string, we must use an &lt;em&gt;interpolation modifier&lt;/em&gt; to specify how it should be interpolated (to avoid ambiguity):
	&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;#{r var}&lt;/code&gt; renders the safe string in &lt;em&gt;var&lt;/em&gt; into text, and then interpolates the text into the template, escaping as necessary (mnemonic: &lt;code&gt;r&lt;/code&gt; for &lt;em&gt;render&lt;/em&gt;).&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;#{= var}&lt;/code&gt; inserts the safe string in &lt;em&gt;var&lt;/em&gt; directly into the template, which must be of the same language (mnemonic: &lt;code&gt;=&lt;/code&gt; for &lt;em&gt;equal language types&lt;/em&gt;).&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;As a bonus, &lt;code&gt;#{s var}&lt;/code&gt; interpolates any &lt;em&gt;Show&lt;/em&gt;-able value in &lt;em&gt;var&lt;/em&gt; into the template as text, escaping as necessary.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;It&amp;#8217;s pretty easy to tell which interpolation option is right for any
situation, but late-night coding sessions make fools of us all.
That&amp;#8217;s why the type system is there to catch us when we make a dumb mistake.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s try out the sugary &lt;code&gt;link_to&lt;/code&gt; method:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;gt; link_to (text "Tom's Weblog") (url "http://blog.moertel.com/")
xml:"&amp;lt;a href="http://blog.moertel.com/"&amp;gt;Tom's Weblog&amp;lt;/a&amp;gt;" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Let&amp;#8217;s take advantage of type inferencing in the next example:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;gt; link_to $(q "&amp;lt;em&amp;gt;Espresso!&amp;lt;/em&amp;gt;")
          $(q "http://google.com/search?q=espresso&amp;#38;oe=utf-8")

xml:"&amp;lt;a href="http://google.com/search?q=espresso&amp;amp;amp;oe=utf-8"&amp;gt;
     &amp;lt;em&amp;gt;Espresso!&amp;lt;/em&amp;gt;&amp;lt;/a&amp;gt;" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In the above example, we supplied templates as input parameters.
Haskell figured out their types and took care of the escaping (or not
escaping) for us.&lt;/p&gt;


	&lt;p&gt;Now that we know what the syntactic sugar looks like, let&amp;#8217;s
see how to implement it.&lt;/p&gt;


	&lt;h3&gt; Implementing the syntactic sugar using Template Haskell&lt;/h3&gt;


	&lt;p&gt;We implement the SafeString library&amp;#8217;s syntactic sugar using Template
Haskell.  A small function &lt;code&gt;q&lt;/code&gt; (for &amp;#8220;quote&amp;#8221;) parses the
sugared syntax at compile time and emits equivalent code using our
safe-string functions &lt;code&gt;frag&lt;/code&gt;, &lt;code&gt;text&lt;/code&gt;, and so on.
For example, the following sugar:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;q&lt;/span&gt; &lt;span class='str'&gt;"&amp;lt;em&amp;gt;#{mystr}&amp;lt;/em&amp;gt;"&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;becomes the following code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;cat&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;frag&lt;/span&gt; &lt;span class='str'&gt;"&amp;lt;em&amp;gt;"&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='varid'&gt;mystr&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;frag&lt;/span&gt; &lt;span class='str'&gt;"&amp;lt;/em&amp;gt;"&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The code that makes it happen is fairly straightforward if you know
Template Haskell, so I&amp;#8217;ll skip the explanation because this article
is already way too long.  As usual, it&amp;#8217;s library code, so normally we
wouldn&amp;#8217;t see it or care about it.  All we care about is the &lt;code&gt;$(q
"...")&lt;/code&gt; sugar that the code makes available to us.&lt;/p&gt;


	&lt;p&gt;Here it is:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Language&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Haskell&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;TH&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='varid'&gt;qualified&lt;/span&gt; &lt;span class='conid'&gt;Text&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;ParserCombinators&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;ReadP&lt;/span&gt; &lt;span class='keyword'&gt;as&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;

&lt;span class='comment'&gt;-- Convert template sugar into calls to frag, text, cat, etc.&lt;/span&gt;
&lt;span class='comment'&gt;-- This function is exported by the SafeStrings module.&lt;/span&gt;

&lt;span class='varid'&gt;q&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;cat&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;parts&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;parts&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;xparse&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;
        &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;   &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;error&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='str'&gt;"bad template: "&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt; &lt;span class='varid'&gt;show&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
        &lt;span class='varid'&gt;ps&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='keyword'&gt;_&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;foldr&lt;/span&gt; &lt;span class='varid'&gt;gen&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt; &lt;span class='varid'&gt;ps&lt;/span&gt;
    &lt;span class='varid'&gt;gen&lt;/span&gt; &lt;span class='varid'&gt;p&lt;/span&gt; &lt;span class='varid'&gt;ps'&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;\&lt;/span&gt;&lt;span class='varid'&gt;p'&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='varid'&gt;p'&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='varid'&gt;ps'&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;p&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;
        &lt;span class='conid'&gt;SFrag&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;frag&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;litE&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;stringL&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;         &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
        &lt;span class='conid'&gt;SIFrag&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;varE&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkName&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;               &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
        &lt;span class='conid'&gt;SIShow&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;show&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;varE&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkName&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;   &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
        &lt;span class='conid'&gt;SITxt&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;varE&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkName&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;          &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
        &lt;span class='conid'&gt;SIRTxt&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;text&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;render&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;varE&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;mkName&lt;/span&gt; &lt;span class='varid'&gt;s&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='comment'&gt;-- AST for template-specification parts&lt;/span&gt;

&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;SpecPart&lt;/span&gt;
    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;SFrag&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt;  &lt;span class='comment'&gt;-- ^ language fragment&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SIFrag&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='comment'&gt;-- ^ insert fragment by variable reference&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SIShow&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='comment'&gt;-- ^ insert rendered variable via show&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SITxt&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt;  &lt;span class='comment'&gt;-- ^ insert literal text variable&lt;/span&gt;
    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;SIRTxt&lt;/span&gt; &lt;span class='conid'&gt;String&lt;/span&gt; &lt;span class='comment'&gt;-- ^ insert rendered safe string var as text&lt;/span&gt;
  &lt;span class='keyword'&gt;deriving&lt;/span&gt; &lt;span class='conid'&gt;Show&lt;/span&gt;

&lt;span class='comment'&gt;-- Parse a template specification&lt;/span&gt;

&lt;span class='varid'&gt;xparse&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;

    &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;result&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='str'&gt;""&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;readP_to_S&lt;/span&gt; &lt;span class='varid'&gt;templateP&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='varid'&gt;result&lt;/span&gt;
 &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;templateP&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;many&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;liftM&lt;/span&gt; &lt;span class='conid'&gt;SFrag&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;munch1&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;/=&lt;/span&gt; &lt;span class='chr'&gt;'#'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&amp;lt;++&lt;/span&gt;
                &lt;span class='varid'&gt;interpolationP&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&amp;lt;++&lt;/span&gt;
                &lt;span class='varid'&gt;liftM&lt;/span&gt; &lt;span class='conid'&gt;SFrag&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;string&lt;/span&gt; &lt;span class='str'&gt;"#"&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

    &lt;span class='varid'&gt;interpolationP&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
        &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;string&lt;/span&gt; &lt;span class='str'&gt;"#{"&lt;/span&gt;
        &lt;span class='varid'&gt;spec&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;manyTill&lt;/span&gt; &lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;get&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;P&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='varid'&gt;char&lt;/span&gt; &lt;span class='chr'&gt;'}'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
        &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;spec&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;
          &lt;span class='chr'&gt;'r'&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='chr'&gt;' '&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;var&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SIRTxt&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;strip&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
          &lt;span class='chr'&gt;'s'&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='chr'&gt;' '&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;var&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SIShow&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;strip&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
          &lt;span class='chr'&gt;'='&lt;/span&gt;&lt;span class='conop'&gt;:&lt;/span&gt;&lt;span class='varid'&gt;var&lt;/span&gt;     &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SIFrag&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;strip&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
          &lt;span class='varid'&gt;var&lt;/span&gt;         &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;SITxt&lt;/span&gt;  &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;strip&lt;/span&gt; &lt;span class='varid'&gt;var&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;strip&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;frontAndBack&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;dropWhile&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='chr'&gt;' '&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='varid'&gt;frontAndBack&lt;/span&gt; &lt;span class='varid'&gt;f&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;reverse&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;f&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;reverse&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;More sugar: defining additional safe-string types&lt;/h3&gt;


	&lt;p&gt;One additional bit of Template Haskell code, which I won&amp;#8217;t reprint
here, defines &lt;em&gt;declareSafeString&lt;/em&gt;.  This function lets us eliminate
the boilerplate code when defining new safe-string types.  For
example, compare our earlier definition of the SafeXml module with the
following implementation of a module for safe &lt;span class="caps"&gt;URL&lt;/span&gt; strings:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;SafeUrl&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Url&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;url&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;renderUrl&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;SafeStrings&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;SafeStrings&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Text&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Printf&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Data&lt;/span&gt;&lt;span class='varop'&gt;.&lt;/span&gt;&lt;span class='conid'&gt;Char&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;ord&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varid'&gt;escapeUrl&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;concatMap&lt;/span&gt; &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='varid'&gt;xs&lt;/span&gt;
  &lt;span class='keyword'&gt;where&lt;/span&gt;
    &lt;span class='varid'&gt;esc&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;isReserved&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;||&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;&amp;gt;&lt;/span&gt; &lt;span class='chr'&gt;'~'&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;urlEncode&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;
          &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt; &lt;span class='varop'&gt;==&lt;/span&gt; &lt;span class='chr'&gt;' '&lt;/span&gt;                &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='str'&gt;"+"&lt;/span&gt;
          &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;otherwise&lt;/span&gt;               &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;

&lt;span class='varid'&gt;urlEncode&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='chr'&gt;'%'&lt;/span&gt; &lt;span class='conop'&gt;:&lt;/span&gt; &lt;span class='varid'&gt;printf&lt;/span&gt; &lt;span class='str'&gt;"%02x"&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;ord&lt;/span&gt; &lt;span class='varid'&gt;x&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='varid'&gt;isReserved&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varop'&gt;`elem`&lt;/span&gt; &lt;span class='str'&gt;"!#$&amp;amp;'()*+,/:;=?@[]"&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;

&lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;declareSafeString&lt;/span&gt; &lt;span class='str'&gt;"url"&lt;/span&gt; &lt;span class='str'&gt;"Url"&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='varid'&gt;escapeUrl&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The final line generates the boilerplate code for the wrapper type,
the language definition, the &lt;em&gt;Url&lt;/em&gt; type synonym, and the &lt;em&gt;url&lt;/em&gt; and
&lt;em&gt;renderUrl&lt;/em&gt; language-specific convenience functions.&lt;/p&gt;


	&lt;h3&gt;One big example to wrap things up&lt;/h3&gt;


	&lt;p&gt;Because we have been discussing mainly library code, let&amp;#8217;s take a step
back and see some typical user-level code that uses safe strings.
After all, that&amp;#8217;s what counts.&lt;/p&gt;


	&lt;p&gt;Here is a Haskellized, safe-strings version of the Ruby (on Rails)
code that I presented at the beginning of the article to add
submit-to-Reddit and submit-to-del.icio.us buttons to my blog:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;Example&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;List&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;intersperse&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;break&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;SafeXml&lt;/span&gt;
&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;SafeUrl&lt;/span&gt;

&lt;span class='keyword'&gt;type&lt;/span&gt; &lt;span class='conid'&gt;Xhtml&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Xml&lt;/span&gt;

&lt;span class='varid'&gt;submit_this_article_links&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Article&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='conid'&gt;Xhtml&lt;/span&gt;
&lt;span class='varid'&gt;submit_this_article_links&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Article&lt;/span&gt; &lt;span class='varid'&gt;title&lt;/span&gt; &lt;span class='varid'&gt;url&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;
    &lt;span class='varid'&gt;cat&lt;/span&gt; &lt;span class='varop'&gt;.&lt;/span&gt; &lt;span class='varid'&gt;intersperse&lt;/span&gt; &lt;span class='varid'&gt;nbsp&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
    &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;submit_title&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;submit_url&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;Url&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varid'&gt;image_tag&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;site_list&lt;/span&gt;
    &lt;span class='varid'&gt;return&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;q&lt;/span&gt;
      &lt;span class='str'&gt;"&amp;lt;a href=\"#{r submit_url}\" \
         \title=\"#{submit_title}: &amp;amp;#x201C;#{title}&amp;amp;#x201D;\" \
        \&amp;gt;#{=image_tag}&amp;lt;/a&amp;gt;"&lt;/span&gt; &lt;span class='layout'&gt;)&lt;/span&gt;

  &lt;span class='keyword'&gt;where&lt;/span&gt;

    &lt;span class='varid'&gt;nbsp&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='varid'&gt;xml&lt;/span&gt; &lt;span class='str'&gt;"&amp;amp;#160;"&lt;/span&gt;

    &lt;span class='varid'&gt;site_list&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;  &lt;span class='comment'&gt;-- move me into a database table&lt;/span&gt;
      &lt;span class='layout'&gt;(&lt;/span&gt; &lt;span class='str'&gt;"Submit to Reddit.com"&lt;/span&gt;
      &lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='varop'&gt;$&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;q&lt;/span&gt; &lt;span class='str'&gt;"http://reddit.com/submit?url=#{r url}&amp;amp;title=#{title}"&lt;/span&gt;