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

<channel>
	<title>Do Loop Until You Die</title>
	<atom:link href="http://blog.matthewdoig.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.matthewdoig.com</link>
	<description>Matthew Doig's Weblog</description>
	<lastBuildDate>Sun, 06 Sep 2009 18:24:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Source Code into Your Blog</title>
		<link>http://blog.matthewdoig.com/?p=181</link>
		<comments>http://blog.matthewdoig.com/?p=181#comments</comments>
		<pubDate>Sun, 06 Sep 2009 17:57:34 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Blogging Tools]]></category>
		<category><![CDATA[copyashtml]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=181</guid>
		<description><![CDATA[<p>I have always written my blogs using Microsoft Word.&#160; A quick look around the internet and the vitriolic hatred of Word as a blog writing tool becomes apparent.&#160; But I try not to let popular opinion affect my technology choices, and instead focus on what works for me.</p>
<p>The blogging workflow for Word was easy.&#160; Write [...]]]></description>
			<content:encoded><![CDATA[<p>I have always written my blogs using Microsoft Word.&#160; A quick look around the internet and the vitriolic hatred of Word as a blog writing tool becomes apparent.&#160; But I try not to let popular opinion affect my technology choices, and instead focus on what works for me.</p>
<p>The blogging workflow for Word was easy.&#160; Write the entry, pasting source code straight from visual studio as I went along.&#160; Save as hmtl filtered. Copy the html output into wordpress, and voila, shiny new blog entry.</p>
<p>I don’t know if it was the upgrade to latest version of wordpress, maybe an upgrade to the theme i am using, but my old workflow no longer worked.&#160; The problem involved additional spacing in all the paragraph tags generated by word.&#160; I fiddled, I faddled, but no matter how i massaged the html i just couldn’t get the source code to look right.&#160; So instead of blasting the evil empires worthless text editor on slashdot, i simply started looking for a new workflow that works for me.</p>
<p>I had tried live writer before and had found it a very capable blog editor, however, the ease of copying source code straight from visual studio was no longer an option.&#160; A quick look at what others were doing turned up <a href="http://code.google.com/p/syntaxhighlighter/">the wordpress option</a>, <a href="http://gallery.live.com/liveItemDetail.aspx?li=1f57bd9b-a692-4593-9e9e-e2962d9c0eee&amp;bt=9&amp;pl=8">the live writer option</a>, and <a href="http://copysourceashtml.codeplex.com/">the visual studio option</a>.</p>
<p>The wordpress option uses javascript to format your code when the page is loaded in the browser.&#160; I would call this delayed syntax highlighting, since it waits until the last possible moment to actually format your code.&#160; The downside of this is the highlighting does not follow your visual studio settings, and only supports languages that have javascipt provider written for them.&#160; Since i write a decent amount of f#, which is not supported, this route was a non starter.</p>
<p>I tried the live writer option in my last blog post.&#160; It works reasonably well, but i got continually frustrated with having to resize my code blocks because of a large space between the code block and the next line of text.&#160; And because it uses pre tags there is not direct access to the code from the editor.&#160; The nail in the coffin though was when i checked the feed in ie, firefox, chrome, and google reader, every client rendered the spacing different with google reader adding a huge number of spaces after the code block.</p>
<p>So for this blog entry i’m using the visual studio option.&#160; The only thing that stopped me from using&#160; CopySourceAsHtml last time was it did not support Visual Studio 2010.&#160; Luckily, adding support is rather a trivial with a few hickups along the way.</p>
<p>First we add an xml addin description for Visual Studio 2010. Next we update the installer to build an addin directory for Visual Studio 2010.&#160; Tweak the user interface of the installer and we’ve got a 2010 installer except</p>
<p><a href="http://blog.matthewdoig.com/wp-content/uploads/2009/09/AddInError.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AddInError" border="0" alt="AddInError" src="http://blog.matthewdoig.com/wp-content/uploads/2009/09/AddInError_thumb.jpg" width="515" height="176" /></a></p>
<p>The <a href="http://msmvps.com/blogs/carlosq/archive/2009/06/05/known-bugs-in-the-new-commandbars-of-vs-2010-beta-1.aspx">command bars api is busted</a> in Beta 1.&#160; Which means you will have to assign shortcut keys to the commands.</p>
<p><a href="http://blog.matthewdoig.com/wp-content/uploads/2009/09/CommandKeyboard.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="CommandKeyboard" border="0" alt="CommandKeyboard" src="http://blog.matthewdoig.com/wp-content/uploads/2009/09/CommandKeyboard_thumb.jpg" width="509" height="319" /></a> </p>
<p>And to get rid of the error when visual studio 2010 loads, we simply comment out the code that adds the menu to the command bars. And for all our hard work we can now post the following to our blog form visual studio 2010.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">var</span> rect = <span style="color: #2b91af">RectangleFluent</span>.Create()</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue)</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetHeight(16)</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetLength(16);</p>
</p></div>
<p style="margin: 0px">&#160;</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">Dim</span> rect = <span style="color: #2b91af">RectangleFluent</span>.Create() _</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue) _</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetHeight(16) _</p>
<p style="margin: 0px">&#160;&#160;&#160; .SetLength(16)</p>
</p></div>
<p style="margin: 0px">&#160;</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">let</span> rect = RectangleFluent.Create().SetColor(Color.AliceBlue).SetHeight(16).SetLength(16)</p>
</p></div>
<p>I’ll be interested to see how the code renders in the various clients.&#160; You can download a modified installer <a href="http://blog.matthewdoig.com/wp-content/uploads/2009/09/CopySourceAsHtml.msi">here</a> that supports Visual Studio 2010.&#160; Hopefully, the comand bar api will be fixed for beta 2.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=181</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Consuming Fluent Interfaces</title>
		<link>http://blog.matthewdoig.com/?p=174</link>
		<comments>http://blog.matthewdoig.com/?p=174#comments</comments>
		<pubDate>Fri, 04 Sep 2009 02:18:18 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=174</guid>
		<description><![CDATA[<p>I guess I’ve been living under a rock for the last year, but I’ve only recently started using a couple of fluent APIs.  A quick search of the internet and fluent APIs are popping up for everything from twitter to orm mapping to dependency injection.</p>
<p>So far I buy into the advantages of fluent interfaces over [...]]]></description>
			<content:encoded><![CDATA[<p>I guess I’ve been living under a rock for the last year, but I’ve only recently started using a couple of fluent APIs.  A quick search of the internet and fluent APIs are popping up for everything from <a href="http://tweetsharp.com/">twitter</a> to <a href="http://wiki.fluentnhibernate.org/Main_Page">orm mapping</a> to <a href="http://using.castleproject.org/display/IoC/Fluent+Registration+API">dependency injection</a>.</p>
<p>So far I buy into the advantages of fluent interfaces over traditional property setting and long lists of function parameters.  I find the consuming code easier to read, more intentful, easier to mix and match behavior, and yes, more fluid.</p>
<p>So let’s throw together a trivial fluent interface and examine how we consume it from a couple of different .Net languages.</p>
<pre class="code" style="width: 83%; height: 523px;">
<span style="color: blue">public interface </span><span style="color: #2b91af">IRectangleFluent
</span>{
    <span style="color: #2b91af">IRectangleFluent </span>SetHeight(<span style="color: blue">int </span>height);
    <span style="color: #2b91af">IRectangleFluent </span>SetLength(<span style="color: blue">int </span>length);
    <span style="color: #2b91af">IRectangleFluent </span>SetColor(<span style="color: #2b91af">Color </span>color);
}

<span style="color: blue">public class </span><span style="color: #2b91af">RectangleFluent </span>: <span style="color: #2b91af">IRectangleFluent
</span>{
    <span style="color: #2b91af">Color </span>color;
    <span style="color: blue">int </span>height;
    <span style="color: blue">int </span>length;

    <span style="color: blue">public </span>RectangleFluent() { }

    <span style="color: blue">public </span><span style="color: #2b91af">IRectangleFluent </span>SetColor(<span style="color: #2b91af">Color </span>color)
    {
        <span style="color: blue">this</span>.color = color;
        <span style="color: blue">return this</span>;
    }

    <span style="color: blue">public </span><span style="color: #2b91af">IRectangleFluent </span>SetHeight(<span style="color: blue">int </span>height)
    {
        <span style="color: blue">this</span>.height = height;
        <span style="color: blue">return this</span>;
    }

    <span style="color: blue">public </span><span style="color: #2b91af">IRectangleFluent </span>SetLength(<span style="color: blue">int </span>length)
    {
        <span style="color: blue">this</span>.length = length;
        <span style="color: blue">return this</span>;
    }
}</pre>
<p>Basically, instead of properties, we’re using methods to return the current context for the next call in the method chain. To consume our fluent interface from c# we simply write</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code" style="width: 99%; height: 46px;"><span style="color: blue">var </span>rect = <span style="color: blue">new </span><span style="color: #2b91af">RectangleFluent</span>()
    .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue)
    .SetHeight(16)
    .SetLength(16);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>From VB we write</p>
<pre class="code" style="width: 99%; height: 45px;"><span style="color: blue">Dim </span>rect = (<span style="color: blue">New </span><span style="color: #2b91af">RectangleFluent</span>()) _
            .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue) _
            .SetHeight(16) _
            .SetLength(16)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Not quite as pretty, as we have to wrap the construction of our instance in an additional set of parenthesis, and of course we have the infamous line continuations.  I could have sworn i read something about line continuations in VB being smarter, i guess not smart enough to realize we want to call our method on the next line.</p>
<p>The additional set of parenthesis are needed in f# as well</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code" style="width: 99%; height: 1px;"><span style="color: blue">let </span>rect = (<span style="color: blue">new </span>RectangleFluent()).SetColor(Color.AliceBlue).SetHeight(16).SetLength(16)</pre>
<p>although we can write</p>
<pre class="code" style="width: 99%; height: 1px;"><span style="color: blue">let </span>rect = RectangleFluent().SetColor(Color.AliceBlue).SetHeight(16).SetLength(16)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>which I really don’t like as it’s very difficult to tell whether an object is being created or a static method is being called.</p>
<p>Luckily, a common practice in fluent libraries is to use a static factory method for creation of object instances, which standardizes the construction of the instance across all three languages.  Adding a factory method to our definition and changing the constructor to private</p>
<pre class="code" style="width: 64.61%; height: 77px;"><span style="color: blue">private </span>RectangleFluent() { }

<span style="color: blue">public static </span><span style="color: #2b91af">IRectangleFluent </span>Create()
{
    <span style="color: blue">return new </span><span style="color: #2b91af">RectangleFluent</span>();
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>allows us to write</p>
<pre class="code" style="width: 95.69%; height: 168px;"><span style="color: blue">var </span>rect = <span style="color: #2b91af">RectangleFluent</span>.Create()
    .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue)
    .SetHeight(16)
    .SetLength(16);

<span style="color: blue">Dim </span>rect = <span style="color: #2b91af">RectangleFluent</span>.Create() _
    .SetColor(<span style="color: #2b91af">Color</span>.AliceBlue) _
    .SetHeight(16) _
    .SetLength(16)

<span style="color: blue">let </span>rect = RectangleFluent.Create().SetColor(Color.AliceBlue).SetHeight(16).SetLength(16)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The c# and vb allow for very readable consuming code, but I’m embarrassed to admit I can’t figure out how to format the f# code for readability.  I’ve looked everywhere and I can’t find an example of calling a method in a method chain on the next line, at least VB’s unsmart line continuation characters let us do it. With the abundance of fluent APIs on the horizon, and f# being pitched as the hot new language for VS 2010, I’m sure I’m overlooking something obvious.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=174</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Hide your Design Decisions with Abstract Data Types</title>
		<link>http://blog.matthewdoig.com/?p=169</link>
		<comments>http://blog.matthewdoig.com/?p=169#comments</comments>
		<pubDate>Mon, 15 Dec 2008 01:30:53 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Pattern Matching]]></category>
		<category><![CDATA[Abstract Data Types]]></category>
		<category><![CDATA[ML]]></category>
		<category><![CDATA[Sets]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=169</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">We all know the importance of protecting consumers of our libraries from our design decisions.  In object oriented programming we refer to this protection mechanism as encapsulation or black box design.  Functional programming allows us to achieve the same goal under a slightly different guise, abstract [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We all know the importance of protecting consumers of our libraries from our design decisions.<span style="mso-spacerun: yes;">  </span>In object oriented programming we refer to this protection mechanism as encapsulation or black box design.<span style="mso-spacerun: yes;">  </span>Functional programming allows us to achieve the same goal under a slightly different guise, abstract data types.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Abstract Data Types in ML </span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The first way to build an abstract data type in ML is to define the data type using abstype instead of datatype.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">abstype</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> &#8216;a Set = set <span style="color: blue;">of</span> &#8216;a list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">with </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Here we’ve chosen to use a list as the underlying store for our Set data type.<span style="mso-spacerun: yes;">  </span>As long as we keep the “interface” the same, we are free to choose a different design for the underlying store.<span style="mso-spacerun: yes;">  </span>The abstype keyword hides the set data constructor from the consumer, preventing sets being constructed from lists.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The abstype keyword may seem convenient, however, modern ML programs rarely use it. <span style="mso-spacerun: yes;"> </span>Abstype has been deprecated in favor of opaque signature ascription.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">signature</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">sig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> empty : &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> singleton : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">end</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">structure</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Set :&gt; SetSig =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">struct</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set = &#8216;a list;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> empty = [];</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">fun</span> singleton a = [a]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">end</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The Set structure is similar to a typeclass where we’ve provided an implementation for SetSig using lists.<span style="mso-spacerun: yes;">  </span>We are free to provide an implementation using binary trees and change the implementation Set will use under the covers.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">signature</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">structure</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ListSet :&gt; SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">structure</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> TreeSet :&gt; SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">Set = TreeSet</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The consumer remains unaware that we’re now using a binary tree instead of a list to represent sets.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Abstract Data Types in F#</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">There is no abstype keyword in F# so we’ll use opaque signature ascription to achieve our data abstraction.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> <span style="color: blue;">internal</span> toList : &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a list<span style="color: green;"></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> empty : &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> singleton : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a set<span style="color: green;"></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set = <span style="color: blue;">internal</span> Set <span style="color: blue;">of</span> &#8216;a list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> toList s = <span style="color: blue;">match</span> s <span style="color: blue;">with</span> Set lst <span style="color: blue;">-&gt;</span> lst </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> empty = Set[]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> is_empty s = List.length(toList s) = 0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> singleton x = Set[x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">There are a couple of problems with the F# implementation.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The fist is just annoying.<span style="mso-spacerun: yes;">  </span>We use the module keyword for both the signature and the implementation in F#.<span style="mso-spacerun: yes;">  </span>The signature must go into a file with the fsi extension and the implementation in a file with an fs extension.<span style="mso-spacerun: yes;">  </span>The system is similar to header files in c and went out of favor around the time Java was introduced.<span style="mso-spacerun: yes;">  </span>Hopefully, the day will come when the signature and the many implementations of the signature can be declared in the same file.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The second is limiting.<span style="mso-spacerun: yes;">  </span>Notice how we’ve had to declare a toList method to convert our set to a list? <span style="mso-spacerun: yes;"> </span>This limits our signature to lists.<span style="mso-spacerun: yes;">  </span>Without the concept of functors we have no way to vary our implementation without changing our signature. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The third is explicitness. Even with functors in the language we would constantly have to convert from a set to a functor and back again.<span style="mso-spacerun: yes;">  </span>There is no implicit conversion mechanism.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The fourth is the module system. Notice how without a different keyword for signature and structure we have no way to provide additional implementations of our signature.<span style="mso-spacerun: yes;">  </span>ML allows the following all in one file</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">signature</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">structure</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ListSet :&gt; SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">structure</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> TreeSet :&gt; SetSig = &#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">Set = TreeSet</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">How we achieve this in F# I do not know.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Expanding Our Set Signature</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s add a bit more functionality to our Set signature to get an idea of how cumbersome problem number two is.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> <span style="color: blue;">internal</span> toList : &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a list <span style="color: green;">(* list all set elts, sorted low to high *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> empty : &#8216;a set <span style="color: green;">(* the empty set *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> is_empty : &#8216;a set <span style="color: blue;">-&gt;</span> bool <span style="color: green;">(* is a set the empty set *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> singleton : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: green;">(* a set with one element *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> union: &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: green;">(* union of two sets *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> is_member : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: blue;">-&gt;</span> bool <span style="color: green;">(* is elt a member of given set? *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> remove : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a set <span style="color: green;">(* remove elt from given set *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">type</span> &#8216;a set = <span style="color: blue;">internal</span> Set <span style="color: blue;">of</span> &#8216;a list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> toList s = <span style="color: blue;">match</span> s <span style="color: blue;">with</span> Set lst <span style="color: blue;">-&gt;</span> lst </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> empty = Set[]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> is_empty s = List.length(toList s) = 0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> singleton x = Set[x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> union s1 s2 = Set(ListSetUtils.union (toList s1) (toList s2))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> is_member x s = ListSetUtils.is_member x (toList s)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> remove x s = Set(ListSetUtils.remove x (toList s))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Two patterns should immediately jump out at you.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">1.<span style="font: 7pt &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We constantly have to use toList to convert our set to a list</span></p>
<p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">2.<span style="font: 7pt &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We constantly have to use the Set data constructor to wrap our list as a set</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The constant wrapping and unwrapping obfuscates the purpose of our code and makes it harder to vary the implementation without rewriting everything.<span style="mso-spacerun: yes;">  </span>You wish there was a way to tell the compiler to implicitly convert from list to set and vice versa instead of always having to explicitly make the conversion.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Pattern Matching To the Rescue</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">All is not doom and gloom, however, F# has very cool pattern matching capabilities.<span style="mso-spacerun: yes;">  </span>Let’s say a consumer of our library wants to add a function to calculate the cardinality of the set.<span style="mso-spacerun: yes;">  </span>In ML we would provide a destructor function and force the user into the following pattern.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">rec</span> cardinality s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">if</span> is_empty s <span style="color: blue;">then</span> 0 <span style="color: blue;">else</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> (x,s&#8217;) = singleton_split s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>1 + cardinality s&#8217;<span style="mso-spacerun: yes;">  </span></span><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">With F# we can provide pattern matching over our abstract data types. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">   </span><span style="mso-spacerun: yes;"> </span>&#8230;<span style="color: green;"></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> <span style="color: blue;">internal</span> singleton_split : &#8216;a set <span style="color: blue;">-&gt;</span> &#8216;a * &#8216;a set <span style="color: green;">(* remove elt from given set and return singleton *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">val</span> (|Nil|Cons|) : &#8216;a set <span style="color: blue;">-&gt;</span> Choice&lt;&#8217;a set,(&#8217;a * &#8216;a set)&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">module</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> SetSig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>&#8230;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> singleton_split s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">match</span> toList s <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>| [] <span style="color: blue;">-&gt;</span> invalid_arg <span style="color: maroon;">&#8220;s&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>| x::s <span style="color: blue;">-&gt;</span> <span style="color: blue;">let</span> lst = ListSetUtils.remove x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                  </span>(x, Set(lst))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> (|Nil|Cons|) s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">if</span> is_empty s <span style="color: blue;">then</span> Nil(s) </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">else</span> Cons(singleton_split s)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Notice the return type of our pattern matching function, choice basically returns all possibilities and allows us to match on them.<span style="mso-spacerun: yes;">  </span>The consumer of our library can now do the following.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">rec</span> cardinality s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">match</span> s <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Nil s <span style="color: blue;">-&gt;</span> 0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Cons(x,s) <span style="color: blue;">-&gt;</span> 1 + cardinality s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Handy, and an F# exclusive feature.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">A Mixed Bag of Functionality</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The Set class provided in Microsoft.FSharp actually implements a Set as an object oriented class.<span style="mso-spacerun: yes;">  </span>Unfortunately, swapping this Set out for your own implementation is a non starter as it does not implement an interface.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">With Haskell we get higher kinded polymorphism and type classes, ML solves many of the same problems with a sophisticated module system, F# uses the object oriented type system of .Net to solve problems the way you would in VB or C#.<span style="mso-spacerun: yes;">  </span>Hopefully, at some point F# will be free to forge its own road on the .Net platform, making it easier to solve problems the other functional languages have licked.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=169</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Abstracting the Map Function</title>
		<link>http://blog.matthewdoig.com/?p=164</link>
		<comments>http://blog.matthewdoig.com/?p=164#comments</comments>
		<pubDate>Sun, 07 Dec 2008 00:47:27 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Functors]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Map]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=164</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">When pitching functional programming to an imperative friend, the “map” function is the canonical example of the obvious advantages of the functional approach.  And once our backwoods imperative friend has begrudgingly accepted “map” as a neat party trick, we can really amaze them by abstracting “map” [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">When <a href="http://blog.matthewdoig.com/?p=25">pitching functional programming to an imperative friend</a>, the “map” function is the canonical example of the obvious advantages of the functional approach. <span style="mso-spacerun: yes;"> </span>And once our backwoods imperative friend has begrudgingly accepted “map” as a neat party trick, we can really amaze them by abstracting “map” over any mappable structure.<span style="mso-spacerun: yes;">  </span>That is as long as brand of functional magic is something besides F#.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">A Map over Trees </span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s start with an example from <a href="http://www.realworldhaskell.org/blog/">Real World Haskell</a>.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Tree&lt;&#8217;a&gt; = </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Node <span style="color: #0000ff;">of</span> (Tree&lt;&#8217;a&gt;) * (Tree&lt;&#8217;a&gt;)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Leaf <span style="color: #0000ff;">of</span> &#8216;a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> tree = Node ((Leaf <span style="color: #800000;">&#8220;foo&#8221;</span>),(Node((Leaf <span style="color: #800000;">&#8220;x&#8221;</span>),(Leaf <span style="color: #800000;">&#8220;quux&#8221;</span>))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And we can write a function to calculate the length of the string at each node.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> tree_lengths = <span style="color: #0000ff;">function</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Leaf s <span style="color: #0000ff;">-&gt;</span> Leaf (String.length s)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Node (l,r) <span style="color: #0000ff;">-&gt;</span> Node (tree_lengths l,tree_lengths r)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; tree_lengths tree;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : Tree&lt;int&gt; = Node ((Leaf 3),(Node((Leaf 1),(Leaf 4))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">But we prefer to use our party trick wherever we can and so we write.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> tree_map f tree =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> tree <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Leaf a <span style="color: #0000ff;">-&gt;</span> Leaf (f a)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Node (l,r) <span style="color: #0000ff;">-&gt;</span> Node (tree_map f l,tree_map f r)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; tree_map String.length tree;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : Tree&lt;int&gt; = Node ((Leaf 3),(Node((Leaf 1),(Leaf 4))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Then impress them with our combinations.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> odd = (<span style="color: #0000ff;">fun</span> x <span style="color: #0000ff;">-&gt;</span> x % 2 &lt;&gt; 0)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; tree_map (odd &lt;&lt; String.length) tree;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : Tree&lt;bool&gt; = Node ((Leaf true),(Node((Leaf true),(Leaf false))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And finally define an operator for our tree_map mapping function.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> (&lt;$&gt;) f tree = tree_map f tree </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; String.length &lt;$&gt; tree;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : Tree&lt;int&gt; = Node ((Leaf 3),(Node((Leaf 1),(Leaf 4))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; (odd &lt;&lt; String.length) &lt;$&gt; tree;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : Tree&lt;bool&gt; = Node ((Leaf true),(Node((Leaf true),(Leaf false))))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">A Map over Type&lt;_&gt;</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Now that we’ve defined a map function for Tree types let’s compare it to our familiar map function over lists.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// val tree_map : (&#8217;a -&gt; &#8216;b) -&gt; Tree&lt;&#8217;a&gt; -&gt; Tree&lt;&#8217;b&gt;<span style="mso-spacerun: yes;">   </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> tree_map f tree =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> tree <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Leaf a <span style="color: #0000ff;">-&gt;</span> Leaf (f a)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Node (l,r) <span style="color: #0000ff;">-&gt;</span> Node (tree_map f l,tree_map f r)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// val list_map : (&#8217;a -&gt; &#8216;b) -&gt; List&lt;&#8217;a&gt; -&gt; List&lt;&#8217;b&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> list_map f list =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> list <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| [] <span style="color: #0000ff;">-&gt;</span> []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| h::t <span style="color: #0000ff;">-&gt;</span> (f h) :: list_map f t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">In both cases we’re taking a function over ordinary types and lifting it into a function over container types. If a is a Type then our Tree and List types are related in that they are both Type&lt;_&gt;. <span style="mso-spacerun: yes;"> </span>And because we prefer to codify the relationship between apparently disparate structures, we write the following map function <span style="mso-spacerun: yes;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// val list_map : (&#8217;a -&gt; &#8216;b) -&gt; Type&lt;&#8217;a&gt; -&gt; Type&lt;&#8217;b&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> type_map f t = </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| :? List&lt;&#8217;a&gt; <span style="color: #0000ff;">-&gt;</span> list_map f t.a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| :? Tree&lt;&#8217;a&gt; <span style="color: #0000ff;">-&gt;</span> tree_map f t.a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> (&lt;$&gt;) f t = type_map f t </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Of course the compiler screams about our attempt to relate trees and lists.<span style="mso-spacerun: yes;">  </span>Our type_map function requires 4 concepts the F# compiler does not support.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">1.<span style="font-family: &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Higher kinded polymorphism</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">2.<span style="font-family: &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Pattern matching over higher kinds</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">3.<span style="font-family: &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Static type tests</span></p>
<p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; line-height: normal; mso-layout-grid-align: none; mso-add-space: auto; mso-list: l0 level1 lfo1;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Lucida Sans Unicode';"><span style="mso-list: Ignore;">4.<span style="font-family: &quot;Times New Roman&quot;;">    </span></span></span><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Polymorphic return types</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">In plain English this means we want the compiler to differentiate between ordinary types and parameterized types, pattern match over parameterized types, provide a standard way to extract the underlying type, catch invalid type tests at compile time instead of runtime, and allow us to return specific instances of Type&lt;_&gt; from a function.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Because F# has to use the type system of .Net, I wouldn’t expect any of these any time soon, if ever.<span style="mso-spacerun: yes;">  </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">A Connection between Trees and Lists</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Unfortunately, F# brains will never make the connection between trees and lists. Trees and lists will forever remain separate unrelated concepts.<span style="mso-spacerun: yes;">  </span>How important you regard this connection depends on your tolerance for redundant code.<span style="mso-spacerun: yes;">  </span>I guess we’ll have to learn to have a high tolerance indeed.<span style="mso-spacerun: yes;">  </span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=164</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Untying the Recursive Knot</title>
		<link>http://blog.matthewdoig.com/?p=160</link>
		<comments>http://blog.matthewdoig.com/?p=160#comments</comments>
		<pubDate>Mon, 01 Dec 2008 22:24:28 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Combinators]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Recursion]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=160</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">When a book is packed with concepts and information it’s very easy to gloss over something that in retrospect is very interesting.  I often skim back over things I’ve already read hoping some of the new information I’ve accumulated will lead to deeper insights and interesting [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">When a book is packed with concepts and information it’s very easy to gloss over something that in retrospect is very interesting.<span style="mso-spacerun: yes;">  </span>I often skim back over things I’ve already read hoping some of the new information I’ve accumulated will lead to deeper insights and interesting discoveries.<span style="mso-spacerun: yes;">  </span>“Untying the recursive knot” from <a href="http://www.amazon.com/F-Scientists-Jon-Harrop/dp/0470242116">F# for Scientists</a> is one such concept I don’t believe I was sufficiently fluent enough in functional programming to glean its power.<span style="mso-spacerun: yes;">  </span>Let’s have a look.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Recursive Functions </span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The factorial function is a pretty standard function for introducing the concept of recursion to beginning functional programmers.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> factorial = <span style="color: #0000ff;">function</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| 0 <span style="color: #0000ff;">-&gt;</span> 1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| n <span style="color: #0000ff;">-&gt;</span> n * factorial (n &#8211; 1)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The rec keyword allows us to recursively call our factorial function passing in a number 1 less than the current call.<span style="mso-spacerun: yes;">  </span>The factorial function is a perfectly good function that accomplishes its assigned task, hence</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; factorial 5;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : int = 120</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">but as functional programmers we’re not so interested in standalone functionality as the ability to compose simple functions into more complex ones.<span style="mso-spacerun: yes;">  </span>We have no way to intercept the recursion and plug in a piece of custom functionality.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So how can we untie this recursive knot?</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Higher Order Continuations</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We can rewrite our factorial function to accept the function it should call next.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">//val factorial : (int -&gt; int) -&gt; int -&gt; int</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> factorial factorial = <span style="color: #0000ff;">function</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none; tab-stops: 602.25pt;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| 0 <span style="color: #0000ff;">-&gt;</span> 1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none; tab-stops: 602.25pt;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| n <span style="color: #0000ff;">-&gt;</span> n * factorial(n &#8211; 1)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And now we need a way to jumpstart the recursion.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">//val y : ((&#8217;a -&gt; &#8216;b) -&gt; &#8216;a -&gt; &#8216;b) -&gt; &#8216;a -&gt; &#8216;b</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> y f x =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>f (y f) x</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The y combinator will continue to generate a continuation for us as long as we ask for one.<span style="mso-spacerun: yes;">  </span>Applying y to our factorial function gives us the answer we expect</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; y factorial 5;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : int = 120</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"><span style="mso-spacerun: yes;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">except now we’re able to insert some custom functionality right into the middle of the recursion.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&gt; y (factorial &gt;&gt; <span style="color: #0000ff;">fun</span> f n <span style="color: #0000ff;">-&gt;</span> printf <span style="color: #800000;">&#8220;%d\n&#8221;</span> n; f n) 5;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">5</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">4</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">3</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">val</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> it : int = 120</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"><span style="mso-spacerun: yes;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Pretty neat!<span style="mso-spacerun: yes;">  </span>Instead of each function having its own proprietary recursion, we’ve made y our recursive combinator, and all other functions are responsible for playing nicely with it or risk being excluded from the fun.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=160</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Launch the Missiles</title>
		<link>http://blog.matthewdoig.com/?p=157</link>
		<comments>http://blog.matthewdoig.com/?p=157#comments</comments>
		<pubDate>Fri, 28 Nov 2008 05:08:45 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Lazy Evaluation]]></category>
		<category><![CDATA[call by name]]></category>
		<category><![CDATA[call by value]]></category>
		<category><![CDATA[eager evalutation]]></category>
		<category><![CDATA[lazy evalutation]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=157</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">If you’ve been programming in F# for any length of time then undoubtedly you’ve run into the term “call by value”.  Another name for “call by value” is eager evaluation as opposed to lazy evaluation and the corresponding “call by name”.  Let’s throw together a fun [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">If you’ve been programming in F# for any length of time then undoubtedly you’ve run into the term “call by value”.<span style="mso-spacerun: yes;">  </span>Another name for “call by value” is eager evaluation as opposed to lazy evaluation and the corresponding “call by name”.<span style="mso-spacerun: yes;">  </span>Let’s throw together a fun little program to demonstrate the difference between the two.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Call By Value</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> MissileCommand = </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Launch</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Disable</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> a1 = printfn <span style="color: #800000;">&#8220;%A&#8221;</span> <span style="color: #800000;">&#8220;KABOOM!&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> a2 = printfn <span style="color: #800000;">&#8220;%A&#8221;</span> <span style="color: #800000;">&#8220;WE LIVE!&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> run cmd a1 a2 =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> cmd <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Launch <span style="color: #0000ff;">-&gt;</span> a1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Disable <span style="color: #0000ff;">-&gt;</span> a2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">run Disable a1 a2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"><span style="mso-spacerun: yes;">  </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So what do you think gets printed to the screen when we run our Disable command?<span style="mso-spacerun: yes;">  </span>You may be surprised to see the following written to the console.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">“KABOOM!”</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">“WE LIVE!”</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So how did the human race get obliterated when we sent the Disable command?<span style="mso-spacerun: yes;">  </span>In a “call by value” language functions arguments are evaluated eagerly, so both a1 and a2 are evaluated before the run function is applied to them.<span style="mso-spacerun: yes;">  </span>Scary!</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Call By Name</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">To simulate “call by name” semantics in F# we are forced to explicitly wrap our functions in a lazy wrapper.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> lazy_a1 = <span style="color: #0000ff;">lazy</span> (printfn <span style="color: #800000;">&#8220;%A&#8221;</span> <span style="color: #800000;">&#8220;KABOOM!&#8221;</span>)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> lazy_a2 = <span style="color: #0000ff;">lazy</span> (printfn <span style="color: #800000;">&#8220;%A&#8221;</span> <span style="color: #800000;">&#8220;WE LIVE!&#8221;</span>)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> safe_run cmd (a1:Lazy&lt;_&gt;) (a2:Lazy&lt;_&gt;) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> cmd <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Launch <span style="color: #0000ff;">-&gt;</span> a1.Force()</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Disable <span style="color: #0000ff;">-&gt;</span> a2.Force()</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">safe_run Disable lazy_a1 lazy_a2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And now the human race is saved from nuclear annihilation as we get what we expect printed to the console.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">“WE LIVE!”</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The lazy wrapper acts as a thunk for the actual computation which only gets computed once we force it. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Call By Value With Side Effects</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Now I hope you don’t lie await at night worrying about a computer bug accidently leading to Armageddon.<span style="mso-spacerun: yes;">  </span>I’m sure our nuclear missile system is written in a lazy language. But I do hope you get a feeling for what a noxious combination “call by value” and undocumented side effects are.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">What if instead of printing to the console our action updated a global counter? <span style="mso-spacerun: yes;"> </span>Or deleted records from a database?<span style="mso-spacerun: yes;">  </span>Functional languages allow us to pass functions around as if they were data, but do we really want them evaluating them behind the scenes when they do? </span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=157</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>OCaml Functors as Interfaces Take 2</title>
		<link>http://blog.matthewdoig.com/?p=155</link>
		<comments>http://blog.matthewdoig.com/?p=155#comments</comments>
		<pubDate>Sun, 23 Nov 2008 20:08:51 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Functors]]></category>
		<category><![CDATA[OCaml]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Polymporphism]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=155</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">We left off in a rather unsatisfying predicament, we can simulate OCaml functors with interfaces, but at the expense of passing in two type parameters and guarding all our functions with type constraints.  Yuck!</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"> </p>
<p class="MsoNormal" style="margin: 0in [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We <a href="http://blog.matthewdoig.com/?p=152">left off in a rather unsatisfying predicament</a>, we can simulate OCaml functors with interfaces, but at the expense of passing in two type parameters and guarding all our functions with type constraints.<span style="mso-spacerun: yes;">  </span>Yuck!</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s solve both these problems</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">OCaml Functors at the Type Level</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s leave the realm of regular F# and pretend we’re able to declare static members on our interfaces.<span style="mso-spacerun: yes;">  </span>This allows us to pass functions around with the type parameters themselves instead of just the instances. <span style="mso-spacerun: yes;"> </span>With this one simple enhancement to interfaces we can now declare the following signatures. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">type comparison = Less | Equal | Greater;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: maroon; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">type comparison = Less | Equal | Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: maroon; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module type ORDERED_TYPE =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>sig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>type t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>val compare: t -&gt; t -&gt; comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>end;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: maroon; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module type ORDERED_TYPE = sig type t val compare : t -&gt; t -&gt; comparison end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module OrderedString =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>struct</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>type t = string</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>let compare x y = if x = y then Equal else if x &lt; y then Less else Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>end;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: maroon; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module OrderedString : sig type t = string val compare : &#8216;a -&gt; &#8216;a -&gt; comparison end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">becomes </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> comparison = Less | Equal | Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ORDERED_TYPE&lt;&#8217;a&gt; =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">static</span> <span style="color: blue;">type</span> t : &#8216;a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">static</span> <span style="color: blue;">abstract</span> compare : t <span style="color: blue;">-&gt;</span> t <span style="color: blue;">-&gt;</span> comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> OrderedString() =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">interface</span> ORDERED_TYPE&lt;string&gt; <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">member</span> compare x y = <span style="color: blue;">if</span> x = y <span style="color: blue;">then</span> Equal <span style="color: blue;">else</span> <span style="color: blue;">if</span> x &lt; y <span style="color: blue;">then</span> Less <span style="color: blue;">else</span> Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">and our set signature with only the add function gets simplified to the following. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Set&lt;&#8217;con&gt; <span style="color: blue;">when</span> &#8216;con :&gt; ORDERED_TYPE&lt;con.t&gt; () =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">member</span> me.add (x : con.t) (s : con.t list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">let</span> <span style="color: blue;">rec</span> add (x : con.t) (s : con.t list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">match</span> s <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| [] <span style="color: blue;">-&gt;</span> [x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| hd::tl <span style="color: blue;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;">               </span><span style="color: blue;">match</span> con.compare x hd <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: blue;">-&gt;</span> s<span style="mso-spacerun: yes;">         </span><span style="color: green;">(* x is already in s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: blue;">-&gt;</span> x :: s<span style="mso-spacerun: yes;">    </span><span style="color: green;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Greater <span style="color: blue;">-&gt;</span> hd :: add x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>add x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">By allowing functions to be passed around with type parameters, we now only have to pass a single type parameter because we can extract the underlying type by a call to t.<span style="mso-spacerun: yes;">  </span>Also, we no longer have to pass constrained types into our add function.<span style="mso-spacerun: yes;">  </span>We can simply call the static compare function on our con type parameter.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Of course, we don’t have “fancy” type level programming in F# so we’re forced to solve the problem at the instance level.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">OCaml Functors at the Instance Level</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Type constraints seem of little use when you can’t perform type level programming.<span style="mso-spacerun: yes;">  </span>So let’s move the type constraint from the parameter list to the constructor. <span style="mso-spacerun: yes;"> </span>Let’s redefine our interfaces and set type.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ORDERED_TYPE&lt;&#8217;a&gt; =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">abstract</span> compare : &#8216;a <span style="color: blue;">-&gt;</span> &#8216;a <span style="color: blue;">-&gt;</span> comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> OrderedString() =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">interface</span> ORDERED_TYPE&lt;string&gt; <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">member</span> me.compare x y = <span style="color: blue;">if</span> x = y <span style="color: blue;">then</span> Equal <span style="color: blue;">else</span> <span style="color: blue;">if</span> x &lt; y <span style="color: blue;">then</span> Less <span style="color: blue;">else</span> Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Set&lt;&#8217;a&gt; (cf : ORDERED_TYPE&lt;&#8217;a&gt;) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">member</span> me.empty : &#8216;a list = []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">member</span> me.add (x : &#8216;a) (s : &#8216;a list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">let</span> <span style="color: blue;">rec</span> add (x : &#8216;a) (s : &#8216;a list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">match</span> s <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| [] <span style="color: blue;">-&gt;</span> [x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| hd::tl <span style="color: blue;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: blue;">match</span> cf.compare x hd <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: blue;">-&gt;</span> s<span style="mso-spacerun: yes;">         </span><span style="color: green;">(* x is already in s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: blue;">-&gt;</span> x :: s<span style="mso-spacerun: yes;">    </span><span style="color: green;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Greater <span style="color: blue;">-&gt;</span> hd :: add x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>add x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Just as with the type level example, we now only need a single type parameter and no longer have to constrain our functions with type constraints.<strong style="mso-bidi-font-weight: normal;"><span style="mso-spacerun: yes;">  </span></strong>The use of the class really isn’t too bad.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> StringSet = Set&lt;string&gt;(OrderedString())</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> r1 = StringSet.add <span style="color: maroon;">&#8220;do&#8221;</span> (StringSet.add <span style="color: maroon;">&#8220;re&#8221;</span> (StringSet.add <span style="color: maroon;">&#8220;mi&#8221;</span> StringSet.empty))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: maroon;">&#8220;%A&#8221;</span> r1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">prints [“do”;”mi”;”re”] to the console we can easily compose a different version of our set.<span style="mso-spacerun: yes;">  </span>For instance,</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> OrderedStringReverse() =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">let</span> reverse (s:string) = <span style="color: blue;">new</span> string(s |&gt; Seq.to_array |&gt; Array.rev)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: blue;">interface</span> ORDERED_TYPE&lt;string&gt; <span style="color: blue;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: blue;">member</span> me.compare x y = </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">let</span> rev_x = reverse x</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">let</span> rev_y = reverse y</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: blue;">if</span> rev_x = rev_y <span style="color: blue;">then</span> Equal <span style="color: blue;">else</span> <span style="color: blue;">if</span> rev_x &lt; rev_y <span style="color: blue;">then</span> Less <span style="color: blue;">else</span> Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> StringSetReverse = Set&lt;string&gt;(OrderedStringReverse())</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> r2 = StringSetReverse.add <span style="color: maroon;">&#8220;do&#8221;</span> (StringSetReverse.add <span style="color: maroon;">&#8220;re&#8221;</span> (StringSetReverse.add <span style="color: maroon;">&#8220;mi&#8221;</span> StringSetReverse.empty))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: maroon;">&#8220;%A&#8221;</span> r2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: maroon; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">prints [“ro”;”mi”;”do”].<span style="mso-spacerun: yes;">  </span>So by passing the constraint to the constructor we can change the structure our “functor” generates.</span><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">What Do We Lose?</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Compossibility.<span style="mso-spacerun: yes;">  </span><span style="mso-spacerun: yes;"> </span>In the second example our StringSetReverse only exists at runtime.<span style="mso-spacerun: yes;">  </span>The following declaration fails.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> StringSetReverse = Set&lt;string&gt;(OrderedStringReverse())</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: black; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We cannot make StringSetReverse part of our type system and use it as a type in other areas of our program.<span style="mso-spacerun: yes;">  </span>We’ve lost the ability to compose complex types from simpler ones.<span style="mso-spacerun: yes;">  </span>How important you consider composing complex types from simple ones will determine how much you feel F# has lost compared to OCaml.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=155</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>OCaml Functors as Interfaces</title>
		<link>http://blog.matthewdoig.com/?p=152</link>
		<comments>http://blog.matthewdoig.com/?p=152#comments</comments>
		<pubDate>Thu, 20 Nov 2008 06:55:16 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Functors]]></category>
		<category><![CDATA[OCaml]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=152</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">The following page describes in detail the differences between the OCaml and F# languages. It’s a good read for anyone familiar with OCaml and an even better read for those who are not.  For the first line of the summary declares</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The <a href="http://research.microsoft.com/fsharp/manual/ml-compat.aspx">following page</a> describes in detail the differences between the OCaml and F# languages. It’s a good read for anyone familiar with OCaml and an even better read for those who are not.<span style="mso-spacerun: yes;">  </span>For the first line of the summary declares</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><em style="mso-bidi-font-style: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Functors, OCaml-style objects, labels and option arguments are not supported.<span style="mso-spacerun: yes;">  </span>Some common functors like Set.Make and Hashtbl.Make are simulated by returning records of functions.</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8.5pt; color: #000000; font-family: &quot;Verdana&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Simulating functors by returning records of functions sounds interesting.<span style="mso-spacerun: yes;">  </span>Let’s take a closer look at what functors in OCaml are. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">OCaml Functors</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"><a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html">Chapter 2</a> of the OCaml manual has a pretty good example of using functors.<span style="mso-spacerun: yes;">  </span>I have to admit that just finding a good example was somewhat of a chore, which suggests either functors aren’t used a whole lot by OCaml programmers or nobody understands them.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The example starts by declaring a signature to represent types that are orderable.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">type comparison = Less | Equal | Greater;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">type comparison = Less | Equal | Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module type ORDERED_TYPE =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>sig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>type t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>val compare: t -&gt; t -&gt; comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>end;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module type ORDERED_TYPE = sig type t val compare : t -&gt; t -&gt; comparison end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We’ll fall back on our object oriented upbringing and use an interface to model the same signature in F#. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> comparison = Less | Equal | Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ORDERED_TYPE&lt;&#8217;a&gt; =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">abstract</span> t : &#8216;a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">abstract</span> compare : &#8216;a <span style="color: #0000ff;">-&gt;</span> &#8216;a <span style="color: #0000ff;">-&gt;</span> comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Now a structure is created for strings. Notice how there is no explicit connection between the structure and the signature. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module OrderedString =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>struct</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>type t = string</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>let compare x y = if x = y then Equal else if x &lt; y then Less else Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>end;;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module OrderedString : sig type t = string val compare : &#8216;a -&gt; &#8216;a -&gt; comparison end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We have no choice but to explicitly implement our interface signature.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> OrderedString(t : string) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">interface</span> ORDERED_TYPE&lt;string&gt; <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">member</span> me.t = t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">member</span> me.compare x y = <span style="color: #0000ff;">if</span> x = y <span style="color: #0000ff;">then</span> Equal <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> x &lt; y <span style="color: #0000ff;">then</span> Less <span style="color: #0000ff;">else</span> Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">For those paying attention you’ll see a subtle difference between the two OrderedString declarations.<span style="mso-spacerun: yes;">  </span>The F# declaration returns an instance of a string for t instead of the type of string.<span style="mso-spacerun: yes;">  </span>In fact, whereas the F# definition appears to be an instance of type the OCaml version appears to be a type of a type.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Implementing Set as a Functor</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The type signature are done, let’s move on to the meat of building our functor.<span style="mso-spacerun: yes;">  </span>We’ll start with the signature.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module Set :</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>functor (Elt : ORDERED_TYPE) -&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>sig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>type element = Elt.t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>type set = element list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>val empty : &#8216;a list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>val add : Elt.t -&gt; Elt.t list -&gt; Elt.t list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>val member : Elt.t -&gt; Elt.t list -&gt; bool</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And examine the implementation of the signature piece by piece.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module Set =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>functor (Elt: ORDERED_TYPE) -&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>struct</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">       </span>type element = Elt.t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">       </span>type set = element list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So a set is a functor that takes an ordered type as a parameter. <span style="mso-spacerun: yes;"> </span>The type is then extracted and stored in the element type.<span style="mso-spacerun: yes;">  </span>I have a feeling our F# definition will have to be quite different.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Set&lt;&#8217;a,&#8217;con&gt; <span style="color: #0000ff;">when</span> &#8216;con :&gt; ORDERED_TYPE&lt;&#8217;a&gt; () =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And so it is.<span style="mso-spacerun: yes;">  </span>With F# we can’t combine the constraint and underlying type as a single parameter.<span style="mso-spacerun: yes;">  </span>Unlike OCaml we have no way of extracting the underlying type from the container type.<span style="mso-spacerun: yes;">  </span>This results in an unfortunate redundant construction.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So the construction of our types are different, let’s analyze the functions.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">let empty = []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">let rec add x s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>match s with</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>[] -&gt; [x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>| hd::tl -&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>match Elt.compare x hd with</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">       </span>Equal<span style="mso-spacerun: yes;">   </span>-&gt; s<span style="mso-spacerun: yes;">        </span><span style="mso-spacerun: yes;"> </span>(* x is already in s *)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>| Less<span style="mso-spacerun: yes;">    </span>-&gt; x :: s<span style="mso-spacerun: yes;">    </span>(* x is smaller than all elements of s *)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">     </span>| Greater -&gt; hd :: add x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">let rec member x s =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>match s with</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>[] -&gt; false</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">   </span>| hd::tl -&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>match Elt.compare x hd with</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">        </span>Equal<span style="mso-spacerun: yes;">   </span>-&gt; true<span style="mso-spacerun: yes;">     </span>(* x belongs to s *)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>| Less<span style="mso-spacerun: yes;">    </span>-&gt; false<span style="mso-spacerun: yes;">    </span>(* x is smaller than all elements of s *)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">      </span>| Greater -&gt; member x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And the corresponding F# implementation.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">member</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> me.empty : &#8216;con list = []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">member</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> me.add (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">   </span><span style="mso-spacerun: yes;"> </span><span style="color: #0000ff;">let</span> <span style="color: #0000ff;">rec</span> add (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> s <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span>| [] <span style="color: #0000ff;">-&gt;</span> [x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span>| hd::tl <span style="color: #0000ff;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">match</span> x.compare x.t hd.t <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: #0000ff;">-&gt;</span> s<span style="mso-spacerun: yes;">         </span><span style="color: #008000;">(* x is already in s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">-&gt;</span> x :: s<span style="mso-spacerun: yes;">    </span><span style="color: #008000;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Greater <span style="color: #0000ff;">-&gt;</span> hd :: add x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>add x s<span style="mso-spacerun: yes;">    </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">member</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> me.is_member (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">let</span> <span style="color: #0000ff;">rec</span> is_member (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> s <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span>| [] <span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">false</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">    </span>| hd::tl <span style="color: #0000ff;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">match</span> x.compare x.t hd.t <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">true</span><span style="mso-spacerun: yes;">     </span><span style="color: #008000;">(* x belongs to s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">false</span><span style="mso-spacerun: yes;">    </span><span style="color: #008000;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="mso-spacerun: yes;">        </span>| Greater <span style="color: #0000ff;">-&gt;</span> is_member x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>is_member x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The fundamental difference with the F# code is we’re forced to work with the constraint instead of the underlying type.<span style="mso-spacerun: yes;">  </span>Because the compare function is only available on instances of ordered types we can only pass those types into our set.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The compare function in the OCaml example however is attached to type itself instead of an instance of the type.<span style="mso-spacerun: yes;">  </span>Remember how our OCaml structure was more analogous to a type of type rather than an instance of a type?<span style="mso-spacerun: yes;">  </span>This allows us to solve problems at the type level instead of always relying on runtime dispatch for polymorphism. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Applying the Set Functor</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So we’ve built a signature for an ORDERED_TYPE.<span style="mso-spacerun: yes;">  </span>We’ve created a structure for a string type that implements the signature.<span style="mso-spacerun: yes;">  </span>We’ve created a functor called Set that can be applied to the structure.<span style="mso-spacerun: yes;">  </span>So let’s finish this off and do the application. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module StringSet = Set(OrderedString);;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">module StringSet :</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>sig</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>type element = OrderedString.t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>type set = element list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>val empty : &#8216;a list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>val add : OrderedString.t -&gt; OrderedString.t list -&gt; OrderedString.t list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">    </span>val member : OrderedString.t -&gt; OrderedString.t list -&gt; bool</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">  </span>end</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The application of the functor creates a new set for ordered strings.<span style="mso-spacerun: yes;">  </span>We can now use this set with functions that only work with strings.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">#</span><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">StringSet.member &#8220;bar&#8221; (StringSet.add &#8220;foo&#8221; StringSet.empty);;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #800000; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman';">- : bool = false</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The application of our F# “functor” is slightly messier because of the inherent redundancy.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> StringSet = Set&lt;string,OrderedString&gt;()</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And of course we have to wrap our strings in OrderedStrings.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> result = StringSet.is_member (OrderedString(<span style="color: #800000;">&#8220;bar&#8221;</span>)) (StringSet.add (OrderedString(<span style="color: #800000;">&#8220;foo&#8221;</span>)) StringSet.empty)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Does F# Compete Against Java or Other Functional Languages</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Some further tricks with interfaces or a record of functions may solve the problem encountered above, but we continue to get the sense that .Net really just isn’t designed with functional languages in mind.<span style="mso-spacerun: yes;">  </span>Here’s another situation where the F# type system doesn’t appear to be up to snuff with a fellow functional friend.<span style="mso-spacerun: yes;">  </span>I think most people would agree .Net was created to compete with Java and it shows.<span style="mso-spacerun: yes;">  </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So will this legacy be the stone around F#’s neck that prevents it from ever really being able to compete with Haskell or OCaml?<span style="mso-spacerun: yes;">  </span>F# may become a great replacement for C# or Java programs, but will the .Net type system ever evolve to the point where F# can solve problems at the type level?<span style="mso-spacerun: yes;">  </span>With the mutable nature of the .Net API’s and the large legacy code base that must be supported, maybe F# would have been better off staying in the lab and forging the way forward for a proper .Net 2.0.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Full Source Code</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">#light</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> comparison = Less | Equal | Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ORDERED_TYPE&lt;&#8217;a&gt; =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">abstract</span> t : &#8216;a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">abstract</span> compare : &#8216;a <span style="color: #0000ff;">-&gt;</span> &#8216;a <span style="color: #0000ff;">-&gt;</span> comparison</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> OrderedString(t : string) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">interface</span> ORDERED_TYPE&lt;string&gt; <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">member</span> me.t = t</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">member</span> me.compare x y = <span style="color: #0000ff;">if</span> x = y <span style="color: #0000ff;">then</span> Equal <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> x &lt; y <span style="color: #0000ff;">then</span> Less <span style="color: #0000ff;">else</span> Greater</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Set&lt;&#8217;a,&#8217;con&gt; <span style="color: #0000ff;">when</span> &#8216;con :&gt; ORDERED_TYPE&lt;&#8217;a&gt; () =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">member</span> me.empty : &#8216;con list = []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">member</span> me.add (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">let</span> <span style="color: #0000ff;">rec</span> add (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">match</span> s <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| [] <span style="color: #0000ff;">-&gt;</span> [x]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| hd::tl <span style="color: #0000ff;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">match</span> x.compare x.t hd.t <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: #0000ff;">-&gt;</span> s<span style="mso-spacerun: yes;">         </span><span style="color: #008000;">(* x is already in s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">-&gt;</span> x :: s<span style="mso-spacerun: yes;">    </span><span style="color: #008000;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Greater <span style="color: #0000ff;">-&gt;</span> hd :: add x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>add x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">member</span> me.is_member (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span><span style="color: #0000ff;">let</span> <span style="color: #0000ff;">rec</span> is_member (x : &#8216;con) (s : &#8216;con list) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span><span style="color: #0000ff;">match</span> s <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| [] <span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">false</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">            </span>| hd::tl <span style="color: #0000ff;">-&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span><span style="color: #0000ff;">match</span> x.compare x.t hd.t <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Equal<span style="mso-spacerun: yes;">   </span><span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">true</span><span style="mso-spacerun: yes;">     </span><span style="color: #008000;">(* x belongs to s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Less<span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">-&gt;</span> <span style="color: #0000ff;">false</span><span style="mso-spacerun: yes;">    </span><span style="color: #008000;">(* x is smaller than all elements of s *)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">                </span>| Greater <span style="color: #0000ff;">-&gt;</span> is_member x tl</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">        </span>is_member x s</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">//By applying the Set functor to a structure implementing an ordered type, we obtain set operations for this type</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> StringSet = Set&lt;string,OrderedString&gt;()</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> result = StringSet.is_member (OrderedString(<span style="color: #800000;">&#8220;bar&#8221;</span>)) (StringSet.add (OrderedString(<span style="color: #800000;">&#8220;foo&#8221;</span>)) StringSet.empty)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: #800000;">&#8220;%A&#8221;</span> result</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">System.Console.ReadLine()</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=152</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using Numerals to Map Functions to Arguments</title>
		<link>http://blog.matthewdoig.com/?p=147</link>
		<comments>http://blog.matthewdoig.com/?p=147#comments</comments>
		<pubDate>Sat, 15 Nov 2008 18:44:25 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Map Function]]></category>
		<category><![CDATA[Parametric Numerals]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=147</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">The map function is one of the most useful functions in all of functional programming.  Pass a function, a list of arguments, apply the function to each argument, and return a new list of the results.</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"> </p>
<p class="MsoNormal" [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The map function is one of the most useful functions in all of functional programming.<span style="mso-spacerun: yes;">  </span>Pass a function, a list of arguments, apply the function to each argument, and return a new list of the results.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> list = [1;2;3;4;5]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> f1 = (<span style="color: #0000ff;">fun</span> a <span style="color: #0000ff;">-&gt;</span> a)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> alist = List.map f1 list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">[1;2;3;4;5]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The map2 function does basically the same thing, except the function takes two arguments, hence we need two lists of arguments.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> list = [1;2;3;4;5]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> f2 = (<span style="color: #0000ff;">fun</span> a b <span style="color: #0000ff;">-&gt;</span> a + b)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> alist2 = List.map2 f2 list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">[2;4;6;8;10]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And I bet you can guess what the map3 function does.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> list = [1;2;3;4;5]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> f3 = (<span style="color: #0000ff;">fun</span> a b c <span style="color: #0000ff;">-&gt;</span> a + b + c)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> alist3 = List.map3 f3 list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">[3;6;9;12;15]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">But now we’ve got a problem for there is no Map4 function.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Extending the Map3 Function</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The basic idea here is to put an end to the continued definitions of Map functions.<span style="mso-spacerun: yes;">  </span>Let’s see if we can “extend” the Map3 function to take one more argument list.<span style="mso-spacerun: yes;">  </span>Something like the following would be great.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> f4 = (<span style="color: #0000ff;">fun</span> a b c d <span style="color: #0000ff;">-&gt;</span> a + b + c + d)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> alist4 = List.map3 f4 list list list &lt;&lt; list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Where the &lt;&lt; operator is defined as</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: #0000ff;">rec</span> (&lt;&lt;) f a =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> f, a <span style="color: #0000ff;">with</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| fh :: ft, ah :: at <span style="color: #0000ff;">-&gt;</span> fh ah :: (ft &lt;&lt; at)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| _ <span style="color: #0000ff;">-&gt;</span> []</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We pass a list of functions, and the next list of arguments to apply the function to.<span style="mso-spacerun: yes;">  </span>For our f4 function we will have already applied f4 for the a b and c arguments by the time we use our &lt;&lt; operator.<span style="mso-spacerun: yes;">  </span>So after the use of our operator we’re left with</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">[4;8;12;16;20]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And if you think about it, there’s not really any reason for the Map2 or Map3 functions.<span style="mso-spacerun: yes;">  </span>Let’s “extend” our Map function for a function requiring 5 bound variables.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> f5 = (<span style="color: #0000ff;">fun</span> a b c d e <span style="color: #0000ff;">-&gt;</span> a + b + c + d + e)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> alist5 = List.map f5 list &lt;&lt; list &lt;&lt; list &lt;&lt; list &lt;&lt; list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">&gt;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">[5;10;15;20;25]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Introducing Parametric Numerals</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The above use of the &lt;&lt; operator is ok.<span style="mso-spacerun: yes;">  </span>It solves our problem of having to continually define Map functions, but it’s not very readable.<span style="mso-spacerun: yes;">  </span>What if a very complex calculation requires 18 bound variables? Do you really want to count up the instances of &lt;&lt; ? And wouldn’t it be great if there was a way to constrain the map function to precisely the number of bound varaibles you require in the first place?</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s define some parametric numerals.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> succ n fl al = n (fl &lt;&lt; al)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> zero al = al</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> one n a = succ zero n a</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> two n a b = succ one n a b</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> three n a b c = succ two n a b c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> four n a b c d = succ three n a b c d</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> five n a b c d e = succ four n a b c d e</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And a new map function that can use them.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> mapWith n f l = n (List.map f l)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Our map function suddenly becomes very readable and constrained to exactly the number of bound variables we require.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist = mapWith zero f1 list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist2 = mapWith one f2 list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist3 = mapWith two f3 list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist4 = mapWith three f4 list list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist5 = mapWith four f5 list list list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The first parameter requires a parametric numeral that states how many additional bound variables our mapWith function requires.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist5 = mapWith four f5 list list list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">can be read as map with four additional bound variables, if we try to write</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> blist5 = mapWith four f5 list list list list list list</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">then the compiler catches the error of our ways.<span style="mso-spacerun: yes;">  </span>Pretty handy!</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"><span style="mso-spacerun: yes;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">An Odd Argument Indeed</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"> </p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The first argument to our mapWith function really is an odd argument.<span style="mso-spacerun: yes;">  </span>Unlike traditional variables, which act as placeholders, it encodes information about how the function should behave.<span style="mso-spacerun: yes;">  </span>We’ll take more in depth look at these “odd arguments” next time.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">The ideas in the blog come from <a href="http://www.brics.dk/RS/01/10/">the following paper</a>, which is a pretty easy read.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=147</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Polymorphic Return Types for F#?</title>
		<link>http://blog.matthewdoig.com/?p=144</link>
		<comments>http://blog.matthewdoig.com/?p=144#comments</comments>
		<pubDate>Wed, 12 Nov 2008 02:59:58 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Choice]]></category>
		<category><![CDATA[Reference Cells]]></category>
		<category><![CDATA[Return Types]]></category>

		<guid isPermaLink="false">http://blog.matthewdoig.com/?p=144</guid>
		<description><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;">We used a good old fashioned subtyping and a dash of parametric polymorphism to write an eval function capable of returning different types.  But surely we gave up too easily on polymorphic return types in F#?  I assure you we gave them the ol’ college try.</p>
<p [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">We used a good old fashioned subtyping and a dash of parametric polymorphism to write an eval function capable of returning different types.<span style="mso-spacerun: yes;">  </span>But surely we gave up too easily on polymorphic return types in F#?<span style="mso-spacerun: yes;">  </span>I assure you we gave them the ol’ college try.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Polymorphic Return Types Take 1</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s start with a pretty simple data constructor that constructs two different types.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">type</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Term =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int <span style="color: #0000ff;">of</span> int</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char <span style="color: #0000ff;">of</span> char</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Now we write a rather straightforward eval function to evaluate our terms.<span style="mso-spacerun: yes;">  </span>For instance,</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; int</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> eval t =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int i <span style="color: #0000ff;">-&gt;</span> i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #008000;">//the expression has type char but here is used with type int</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">But because our first match returns an integer our second must also return a char.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Polymorphic Return Types Take 2</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Just because the compiler doesn’t like what you’re trying to do doesn’t mean it isn’t right.<span style="mso-spacerun: yes;">  </span>Let’s take a different tact.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; obj</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> eval t =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int i <span style="color: #0000ff;">-&gt;</span> i :&gt; obj</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> c :&gt; obj</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> i = eval (Int 1)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> c = eval (Char <span style="color: #800000;">&#8216;c&#8217;</span>)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: #800000;">&#8220;%A&#8221;</span> i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: #800000;">&#8220;%A&#8221;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So we sacrifice our type safety to get a couple of values printed to the console.<span style="mso-spacerun: yes;">  </span>Not what we had in mind.<span style="mso-spacerun: yes;">  </span>We want the type system to guide us instead of forcing us to throw it out the window.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Polymorphic Return Types Take 3</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So if we can’t return a different type and we don’t want to lose our type safety, let’s try returning all possible types.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; Choice&lt;int,char&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> eval t =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;">   </span>| Int i <span style="color: #0000ff;">-&gt;</span> Choice2_1 i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> Choice2_2 c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> i = eval (Int 1)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> c = eval (Char <span style="color: #800000;">&#8216;c&#8217;</span>)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">So our eval function compiles and returns a different choice for each constructor.<span style="mso-spacerun: yes;">  </span>All we have to do now is extract the value out of the choice and were done. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; Choice&lt;&#8217;a,&#8217;a&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> extract choice =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> choice <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Choice2_1 i <span style="color: #0000ff;">-&gt;</span> i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Choice2_2 c <span style="color: #0000ff;">-&gt;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// expecting a Choice&lt;int,int&gt; but given a Choice&lt;int,char&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> i = extract (eval (Int 1))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">And if we annotate the choice function we’ve just reinvented our original problem.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; Choice&lt;int,char&gt; -&gt; int</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> extract_int_char (choice : Choice&lt;int,char&gt;) =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> choice <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Choice2_1 i <span style="color: #0000ff;">-&gt;</span> i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #008000;">//the expression has type char but here is used with type int</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Choice2_2 c <span style="color: #0000ff;">-&gt;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Polymorphic Return Types Take 4</span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Let’s start with a function that does work.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; unit</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> display t =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int i <span style="color: #0000ff;">-&gt;</span> printfn <span style="color: #800000;">&#8220;%A&#8221;</span> i </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> printfn <span style="color: #800000;">&#8220;%A&#8221;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Now can’t we somehow lift the variables out of the function without changing the return type?<span style="mso-spacerun: yes;">  </span>Something along the lines of.<span style="mso-spacerun: yes;">  </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; &#8216;a ref -&gt; unit</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> eval t r =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int i <span style="color: #0000ff;">-&gt;</span> r := i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #008000;">//the expression has type char but here is used with type int</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> r := c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Only to be thwarted again and all we’re left with to get our values out of eval function is the following monstrosity.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #008000; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">// Term -&gt; int ref -&gt; char ref -&gt; unit</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> eval t ri rc =</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span><span style="color: #0000ff;">match</span> t <span style="color: #0000ff;">with</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Int i <span style="color: #0000ff;">-&gt;</span> ri := i </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">    </span>| Char c <span style="color: #0000ff;">-&gt;</span> rc := c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ri = ref 0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> rc = ref <span style="color: #800000;">&#8216;a&#8217;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> ie = eval (Int 1)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> u1 = eval (Int 1) ri rc</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> i = !ri</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> u2 = eval (Char <span style="color: #800000;">&#8216;c&#8217;</span>) ri rc</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">let</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> c = !rc</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: #800000;">&#8220;%A&#8221;</span> i</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">printfn <span style="color: #800000;">&#8220;%A&#8221;</span> c</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #000000; font-family: &quot;Lucida Sans Unicode&quot;,&quot;sans-serif&quot;;">Eat your heart out Perl.<span style="mso-spacerun: yes;">  </span>We’ve got a new write once kid on the block.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.matthewdoig.com/?feed=rss2&amp;p=144</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
