<?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>∃xistential Type &#187; scala</title>
	<atom:link href="http://existentialtype.net/tag/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://existentialtype.net</link>
	<description>For People Who Like Type and Types</description>
	<lastBuildDate>Fri, 29 Oct 2010 15:00:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Resumption</title>
		<link>http://existentialtype.net/2009/04/04/resumption/</link>
		<comments>http://existentialtype.net/2009/04/04/resumption/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 16:15:21 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[dot]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scala classic]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=277</guid>
		<description><![CDATA[That was a longer hiatus than I had intended. Partly because not everything went according to plan. My original plan was that, upon returning from my vacation, I would spend my remaining time at EPFL writing a technical report explaining everything I knew about the problems with Scala Classic. Instead, on my first day back [...]]]></description>
			<content:encoded><![CDATA[<p>That was a longer hiatus than I had intended.  Partly because not everything went according to plan.</p>
<p>My original plan was that, upon returning from my vacation, I would spend my remaining time at <a href="http://lamp.epfl.ch/">EPFL</a> writing a technical report explaining everything I knew about the problems with Scala Classic.  Instead, on my first day back in the office, Martin came by with a draft of a new formal system, DOT (for Dependent Object Types), that he came up with while he was on vacation.  After about four weeks I managed to root out pretty much all of the obvious problems with DOT, but another four weeks was not enough to get the proofs and the metatheory into a state that we were happy with.  I am not really sure what will happen with DOT in the long term.</p>
<p>There are a few clever things about DOT that avoid some of the problems I encountered with Scala Classic.  For example, Scala Classic only had intersection types while DOT has both intersection and union types, which solves some problems with computing the members of types.  However, with Scala Classic I was trying to model a minimal subset <a href="http://www.scala-lang.org/">Scala</a> language as it exists, without adding any new features that many never be supported by the full language.  I have no idea if we will see union types in the actual Scala implementation anytime soon.</p>
<p>The other thing DOT does is give up the goal of trying to be a minimal subset of Scala and throws out quite a few important things from a user perspective.  For example, there are no methods (only λ-abstractions), no existentials, no built-in inheritance or mix-in composition (though you can encode it by hand), and object fields must be syntactically values.</p>
<p>This last restriction is particularly important because it solves by fiat the problems that arose in Scala Classic from using paths that have an empty type in dependent type projections.  However, it also means that you may not be able to directly encode some Scala programs into DOT without an effect or termination analysis.  Therefore, while DOT has the potential to be a useful intermediary step, there will still be more work to be done to provide a semantics for a more realistic subset of the Scala language.</p>
<p>I have been in Copenhagen for a little over a month now, but I am not really ready to comment on the research I have been doing here yet.  So in the meantime I'll probably focus more on fonts and typography.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2009/04/04/resumption/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Algorithmic puzzle</title>
		<link>http://existentialtype.net/2008/09/08/algorithmic-puzzle/</link>
		<comments>http://existentialtype.net/2008/09/08/algorithmic-puzzle/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 08:38:54 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[boyer-moore]]></category>
		<category><![CDATA[infinite]]></category>
		<category><![CDATA[knuth-morris-pratt]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[searching]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[time complexity]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=266</guid>
		<description><![CDATA[I was assigned the task of fixing a bug in the Scala standard library involving the indexOf, which given a receiver object that is a sequence and another sequence of the correct type, checks whether the latter is contained within the latter and returns the index. The current version does not correctly handle the case [...]]]></description>
			<content:encoded><![CDATA[<p>I was assigned the task of fixing a bug in the Scala standard library involving the <code>indexOf</code>, which given a receiver object that is a sequence and another sequence of the correct type, checks whether the latter is contained within the latter and returns the index.  The current version does not correctly handle the case when a suffix of the receiver object matches a strict prefix of the argument (for example, <code>List(1,2,3).indexOf(List(3,4))</code> will report a match at the index of <code>2</code>).  This should be fixed for the upcoming 2.7.2-RC2 release.</p>
<p>As soon as I started rewriting the code, I wondered why the original author hadn't just used an off the shelf searching algorithm.  However, a quick search reminded me why: algorithms like <a href="http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm">Knuth-Morris-Pratt</a> and <a href="http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm">Boyer-Moor</a> construct a table based upon the sequence to search for.  However, Scala sequences may be infinite so it is not possible to blindly go ahead and attempt to construct a table, because doing so may diverge.  </p>
<p>Furthermore, there is no way to test whether a sequence is finite without potentially diverging.  So it is not possible to first test whether the argument is finite, because if the receiver object is finite then <code>indexOf</code> should return that there is no match.  Alternately, testing whether the receiver object is finite would be incorrect because it is possible the argument is finite an could potentially match.</p>
<p>However, it seems like it should still be possible to do better than O(<i>nm</i>), where <i>n</i> is the length of the receiver and <i>m</i> the length of the argument.  For example if you start out with the sequence 1, 2, 3, 1 ...  and the pattern  1, 3, 4 ... it seems like it should be possible to exploit the fact that you've looked ahead and know that there is no point and comparing 2 with 1.  Alternately it seems like it might be possible to lazily build a table from the argument, but I would need to think longer to see whether it is always possible, in Knuth-Morris-Pratt for example, to fill in a complete prefix of the table without having processed the entire pattern.  </p>
<p>In any event, searching with combinations of keywords like "string", "searching", "lazy", "infinite", etc. did not really turn anything up.  One possible direction might be to look at "incremental" search algorithms like those used in text editors, etc.  However, I expect that because they are geared to interactive use that the pattern will usually be quite small and therefore much thought has not been put into optimizing them.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/09/08/algorithmic-puzzle/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Literally dependent types</title>
		<link>http://existentialtype.net/2008/07/21/literally-dependent-types/</link>
		<comments>http://existentialtype.net/2008/07/21/literally-dependent-types/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 17:26:29 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[dependent types]]></category>
		<category><![CDATA[implicit conversions]]></category>
		<category><![CDATA[literals]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=264</guid>
		<description><![CDATA[Given that the formalization of Scala Classic has ground to a halt, for reasons I may go into later, I spent part of today hacking on the Scala compiler itself to add support for singleton literals. Currently, Scala allows singleton types for stable identifiers. My modification allows literals to be used in singleton types. I [...]]]></description>
			<content:encoded><![CDATA[<p>Given that <a href="http://existentialtype.net/2008/07/13/even-better-than-the-real-thing/">the formalization of Scala Classic</a> has ground to a halt, for reasons I may go into later, I spent part of today hacking on the Scala compiler itself to add support for singleton literals. Currently, Scala allows singleton types for stable identifiers.  My modification allows literals to be used in singleton types.  I can't promise that it will be in the forthcoming Scala 2.7.2 release, but I would like it to be.</p>
<p>Overall it was far less work than I was expecting.  Scala already internally supports what it calls "constant types", there is just no way to write them in Scala source code presently.  Consequently, most of the effort was in extending the parser.</p>
<p>Given my modifications, it is now possible to write code like the following:</p>
<pre class="scala">&nbsp;
scala<span style="color: #a00000;">&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> x <span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;foo&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> <span style="color: #6666ff;">&quot;foo&quot;</span>
x<span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;foo&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> foo
&nbsp;</pre>
<p>What I was not expecting was that out-of-the-box things like the following would work:</p>
<pre class="scala">&nbsp;
scala<span style="color: #a00000;">&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> x <span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;foobar&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> <span style="color: #6666ff;">&quot;foo&quot;</span> + <span style="color: #6666ff;">&quot;bar&quot;</span>
x<span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;foobar&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> foobar
scala<span style="color: #a00000;">&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> y <span style="color: #a00000;">:</span> <span style="color: #f78811;">10</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> <span style="color: #f78811;">2</span> <span style="color: #a00000;">*</span> <span style="color: #f78811;">5</span>
y<span style="color: #a00000;">:</span> <span style="color: #f78811;">10</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> <span style="color: #f78811;">10</span>
scala<span style="color: #a00000;">&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> frob<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> <span style="color: #f78811;">10</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> <span style="color: #f78811;">6</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> arg - <span style="color: #f78811;">4</span>
frob<span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span><span style="color: #f78811;">10</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a><span style="color: #e77801;">&#41;</span><span style="color: #f78811;">6</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a>
&nbsp;</pre>
<p>Unfortunately the excitement soon passes when you realize all the things you can't do with singleton literals (yet).  Even if we turn on the experimental dependent method support, you can't write things like</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> add<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span>arg + <span style="color: #f78811;">5</span><span style="color: #e77801;">&#41;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <span style="color: #a00000;">=</span> arg + <span style="color: #f78811;">5</span>
&nbsp;</pre>
<p>because these are exactly what they are called, singleton literals, not full-blown dependent types.</p>
<p>One cute example, based on a use suggested by Sean McDirmid, would be that some people might do something like the following with implicits:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">implicit</span></a> stringToColor<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">String</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span> <span style="color: #a00000;">=</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span>.<span style="color: #006600;">getColor</span><span style="color: #e77801;">&#40;</span>arg<span style="color: #e77801;">&#41;</span>
&nbsp;</pre>
<p>However, with singleton literals you can tighten it up by ensuring that for some strings it will never fail:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">implicit</span></a> redToColor<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;red&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span> <span style="color: #a00000;">=</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span>.<span style="color: #006600;">RED</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">implicit</span></a> blueToColor<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> <span style="color: #6666ff;">&quot;blue&quot;</span>.<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span> <span style="color: #a00000;">=</span> java.<span style="color: #006600;">awt</span>.<span style="color: #006600;">Color</span>.<span style="color: #006600;">BLUE</span>
&nbsp;</pre>
<p>Happily, the type inferencer already chooses the most specific implicit conversions.</p>
<p>In any event, they will hopefully become more useful as the type system continues to grow.  I am also sure someone will probably come up with a clever use for them that hasn't occurred to me yet.  If so, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/07/21/literally-dependent-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Even better than the real thing</title>
		<link>http://existentialtype.net/2008/07/13/even-better-than-the-real-thing/</link>
		<comments>http://existentialtype.net/2008/07/13/even-better-than-the-real-thing/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 15:23:45 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[research]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[explicit contexts]]></category>
		<category><![CDATA[featherweight scala]]></category>
		<category><![CDATA[pldi]]></category>
		<category><![CDATA[POPL]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scala classic]]></category>
		<category><![CDATA[soundness]]></category>
		<category><![CDATA[substitution]]></category>
		<category><![CDATA[teaching]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=256</guid>
		<description><![CDATA[Several weeks ago I decided that instead of calling the core calculus that I have been working on Featherweight Scala, which could be confusing given that there is already a different calculus with that name, that I would call it Scala Classic. I had hoped to submit a paper on Scala Classic to POPL 2009, [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align:center;"><a href="http://existentialtype.net/wp-content/uploads/2008/07/scalaclassic.png"><img class="aligncenter size-full wp-image-257" title="scalaclassic" src="http://existentialtype.net/wp-content/uploads/2008/07/scalaclassic.png" alt="Scala Classic" width="488" height="427" /></a></div>
<p>Several weeks ago I decided that instead of calling the core calculus that I have been working on Featherweight Scala, which could be confusing given that there is already a <a href="http://lampwww.epfl.ch/~odersky/papers/mfcs06.pdf">different calculus with that name</a>, that I would call it Scala Classic.</p>
<p>I had hoped to submit a paper on Scala Classic to <a href="http://www.cs.ucsd.edu/popl/09/">POPL 2009</a>, but it has been taking too long to work out the metatheory. Partly because I am developing a mechanized proof of type soundness in <a href="http://twelf.plparty.org/">Twelf</a>, and on my first attempt at representing heaps I think tried to be too clever.  However, a more traditional treatment of heaps leads to the need to <a href="http://www.cs.cmu.edu/~crary/papers/2008/excon.pdf">explicitly represent typing contexts</a>, rather than the implicit treatment of contexts more commonly used in Twelf.</p>
<p>This afternoon I finally finished the proof of the theorem that is traditionally called "substitution".  However, the proof of type preservation will also require another theorem formalizing a substitution-like property for singleton types.  I am hoping that I can prove that theorem more quickly now that I've built up all the explicit context theory.  I have not thought much about whether there are any unusual theorems needed to prove progress.</p>
<p>In any event, I would ideally have the proofs finished by the beginning of August.  My current plan is to try submitting the paper to <a href="http://www-plan.cs.colorado.edu/~pldi09/">PLDI</a> (which is even in Europe this year), unless some obviously better venue comes to my attention.</p>
<p>One of the reasons I would really like to have the proofs done by August is that I will be teaching a PhD level course here at <a href="http://www.epfl.ch/">EPFL</a> this fall on mechanized sepcifications and proofs, with a strong emphasis on deductive systems. It also looks like I may fill in some for a masters level course based on <a href="http://www.cis.upenn.edu/~bcpierce/tapl/">TAPL</a>.  So between those two things, I am not going to have nearly as much time for research as would be ideal.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/07/13/even-better-than-the-real-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The other Scala</title>
		<link>http://existentialtype.net/2008/06/09/the-other-scala/</link>
		<comments>http://existentialtype.net/2008/06/09/the-other-scala/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 18:51:11 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[typography]]></category>
		<category><![CDATA[ff scala]]></category>
		<category><![CDATA[ff scala sans]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scala's bistro]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=248</guid>
		<description><![CDATA[A post by James Iry on the Scala mailing list today reminded me of the existence of FF Scala (along with its companion FF Scala Sans). I haven't received confirmation, but I suspect that Programming in Scala does not, unfortunately,  use any members of the FF Scala family. I was sorely tempted to purchase the [...]]]></description>
			<content:encoded><![CDATA[<p>A post by <a href="http://james-iry.blogspot.com/">James Iry</a> on the <a href="http://www.nabble.com/-scala--Scala-is-so-cool-it%27s-fashionable-td17735295.html">Scala mailing list</a> today reminded me of the existence of <a href="http://en.wikipedia.org/wiki/FF_Scala">FF Scala</a> (along with its companion <a href="http://en.wikipedia.org/wiki/FF_Scala_Sans">FF Scala Sans</a>).  I haven't received confirmation, but I suspect that <a href="http://www.artima.com/shop/forsale">Programming in Scala</a> does not, unfortunately,  use any members of the FF Scala family.  I was sorely tempted to purchase the fonts, <a href="http://www.fontshop.com/search/?q=scala&amp;x=0&amp;y=0">until I saw the prices</a>.  Maybe someday.</p>
<p>And there are plenty of other Scalas about.  In San Francisco:</p>
<p><a href="http://existentialtype.net/wp-content/uploads/2008/06/photo.jpg"><img class="aligncenter size-full wp-image-249" title="Scala\'s Bistro" src="http://existentialtype.net/wp-content/uploads/2008/06/photo.jpg" alt="" width="500" height="375" /></a></p>
<p>Alas, we did not succeed in getting Martin to <a href="http://www.scalasbistro.com/">eat there</a> while he was at <a href="http://java.sun.com/javaone/sf/index.jsp">JavaOne</a>.  Photo courtesy of <a href="http://people.cs.uchicago.edu/~robby/">Robby Findler</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/06/09/the-other-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functors in Scala</title>
		<link>http://existentialtype.net/2008/05/26/functors-in-scala/</link>
		<comments>http://existentialtype.net/2008/05/26/functors-in-scala/#comments</comments>
		<pubDate>Mon, 26 May 2008 19:42:19 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[functors]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=246</guid>
		<description><![CDATA[Following on my earlier entry on modules in Scala, I'll give an encoding of Standard ML style functors here. You can get a pretty close approximation by using class constructor arguments. However, I am going to cheat a little to get the closest encoding I think is possible by using the experimental support for dependent [...]]]></description>
			<content:encoded><![CDATA[<p>Following on my earlier entry on <a href="http://existentialtype.net/2008/05/26/modules-in-scala/">modules in Scala</a>, I'll give an encoding of Standard ML style functors here.  You can get a pretty close approximation by using <code>class</code> constructor arguments.  However, I am going to cheat a little to get the closest encoding I think is possible by using the experimental support for dependent method types.  You can get this by running <code>scala</code> or <code>scalac</code> with the option <code>-Xexperimental</code>.  It works okay at least some of the time, but no one has the time at the moment to commit to getting it in shape for general consumption.</p>
<p>So here is my example of how the encoding works.  First, the SML version:</p>
<pre>&nbsp;
signature Eq = sig
  type t
  val eq: t -&gt; t -&gt; bool
end
&nbsp;
signature RicherEq = sig
  include Eq
  val neq: t -&gt; t -&gt; bool
end
&nbsp;
functor mkRicherEq(arg : Eq) :&gt; RicherEq where type t = arg.t = struct
  type t = arg.t
  val eq = arg.eq
  fun neq x y = not (eq x y)
end
&nbsp;</pre>
<p>We can transliterate this example into Scala as:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> Eq <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> eq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a>
<span style="color: #e77801;">&#125;</span>
&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> RicherEq <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> eq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> neq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a>
<span style="color: #e77801;">&#125;</span>
&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> mkRicherEq<span style="color: #e77801;">&#40;</span>arg<span style="color: #a00000;">:</span> Eq<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> RicherEq <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T <span style="color: #a00000;">=</span> arg.<span style="color: #006600;">T</span> <span style="color: #e77801;">&#125;</span> <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T <span style="color: #a00000;">=</span> arg.<span style="color: #006600;">T</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> eq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=</span> arg.<span style="color: #006600;">eq</span><span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> neq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span>T<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=</span> <span style="color: #a00000;">!</span>eq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;</pre>
<p>The only problem I discovered is that it is not possible to define <code>RicherEq</code> in terms of <code>Eq</code> as we could in SML:</p>
<pre class="scala">&nbsp;
scala<span style="color: #a00000;">&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> RicherEq <span style="color: #a00000;">=</span> Eq <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> neq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #e77801;">&#125;</span>
<span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;:</span><span style="color: #f78811;">5</span><span style="color: #a00000;">:</span> error<span style="color: #a00000;">:</span> Parameter <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> in structural refinement may
not refer to <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">abstract</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> defined outside that same refinement
       <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> RicherEq <span style="color: #a00000;">=</span> Eq <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> neq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #e77801;">&#125;</span>
                                       ^
<span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;:</span><span style="color: #f78811;">5</span><span style="color: #a00000;">:</span> error<span style="color: #a00000;">:</span> Parameter <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> in structural refinement may
not refer to <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">abstract</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> defined outside that same refinement
       <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> RicherEq <span style="color: #a00000;">=</span> Eq <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> neq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #e77801;">&#125;</span>
                                             ^
&nbsp;</pre>
<p>Why this restriction exists I don't know.  In fact, this sort of refinement should work in the current version of Featherweight Scala, so perhaps it can be lifted eventually.</p>
<p>I still need to think about higher-order functors, and probably spend a few minutes researching existing proposals.  I think this is probably something that cannot be easily supported in Scala if it will require allowing method invocations to appear in paths.  However, off hand that only seems like it should be necessary for applicative higher-order functors, but again I definitely need to think it through.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/05/26/functors-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modules in Scala</title>
		<link>http://existentialtype.net/2008/05/26/modules-in-scala/</link>
		<comments>http://existentialtype.net/2008/05/26/modules-in-scala/#comments</comments>
		<pubDate>Mon, 26 May 2008 15:48:18 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=245</guid>
		<description><![CDATA[I just saw a thread on Lambda the Ultimate where I think the expressive power of Scala in comparison to Standard ML's module system was misrepresented. I don't want to go into all of the issues at the moment, but I figured out would point out that you can get the same structural typing, opaque [...]]]></description>
			<content:encoded><![CDATA[<p>I just saw a <a href="http://lambda-the-ultimate.org/node/2808">thread on Lambda the Ultimate</a> where I think the expressive power of Scala in comparison to Standard ML's module system was misrepresented.  I don't want to go into all of the issues at the moment, but I figured out would point out that you can get the same structural typing, opaque sealing, and even the equivalent of SML's <code>where type</code> clause.  </p>
<p>For example, consider the following SML signature:</p>
<pre>&nbsp;
signature Nat = sig
  type t
  val z: t
  val s: t -&gt; t
end
&nbsp;</pre>
<p>This signature can be translated in to Scala as:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> Nat <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> z<span style="color: #a00000;">:</span> T
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> s<span style="color: #e77801;">&#40;</span>arg<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> T
<span style="color: #e77801;">&#125;</span>
&nbsp;</pre>
<p>It is then possible to create an implementation of this type, and opaquely seal it (hiding the definition of <code>T</code>).  In SML:</p>
<pre>&nbsp;
structure nat :&gt; Nat = struct
  type t = int
  val z = 0
  fun s n = n + 1
end
&nbsp;</pre>
<p>In Scala:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> nat <span style="color: #a00000;">:</span> Nat <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> z <span style="color: #a00000;">=</span> <span style="color: #f78811;">0</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> s<span style="color: #e77801;">&#40;</span>arg<span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=</span> arg + <span style="color: #f78811;">1</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;</pre>
<p>In many cases when programming with SML modules it is necessary or convenient to give a module that reveals the definition of an abstract type.  In the above example, this can be done by adding a <code>where type</code> clause to the first line:</p>
<pre>&nbsp;
structure nat :&gt; Nat where type t = int = struct
...
&nbsp;</pre>
<p>We can do the same thing in Scala using refinements:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> nat <span style="color: #a00000;">:</span> Nat <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> T <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a> <span style="color: #e77801;">&#125;</span> <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> <span style="color: #e77801;">&#123;</span>
...
&nbsp;</pre>
<p>Great, right?  Well, almost.  The problem is that structural types are still a bit buggy in Scala compiler at present.  So, while the above typechecks, you can't quite use it yet:</p>
<pre class="scala">&nbsp;
scala<span style="color: #a00000;">&gt;</span> nat.<span style="color: #006600;">s</span><span style="color: #e77801;">&#40;</span>nat.<span style="color: #006600;">z</span><span style="color: #e77801;">&#41;</span>
java.<span style="color: #006600;">lang</span>.<span style="color: #006600;">NoSuchMethodException</span><span style="color: #a00000;">:</span> $anon$<span style="color: #f78811;">1</span>.<span style="color: #006600;">s</span><span style="color: #e77801;">&#40;</span>java.<span style="color: #006600;">lang</span>.<a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">Object</span></a><span style="color: #e77801;">&#41;</span>
        at java.<span style="color: #006600;">lang</span>.<span style="color: #006600;">Class</span>.<span style="color: #006600;">getMethod</span><span style="color: #e77801;">&#40;</span>Class.<span style="color: #006600;">java</span><span style="color: #a00000;">:</span><span style="color: #f78811;">1581</span><span style="color: #e77801;">&#41;</span>
        at .<span style="color: #006600;">reflMethod</span>$Method1<span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;:</span><span style="color: #f78811;">7</span><span style="color: #e77801;">&#41;</span>
        at .<span style="color: #a00000;">&lt;</span>init<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;:</span><span style="color: #f78811;">7</span><span style="color: #e77801;">&#41;</span>
        at .<span style="color: #a00000;">&lt;</span>clinit<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#41;</span>
        at RequestResult$.<span style="color: #a00000;">&lt;</span>init<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;:</span><span style="color: #f78811;">3</span><span style="color: #e77801;">&#41;</span>
        at RequestResult$.<span style="color: #a00000;">&lt;</span>clinit<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#41;</span>
        at RequestResult$result<span style="color: #e77801;">&#40;</span><span style="color: #a00000;">&lt;</span>console<span style="color: #a00000;">&gt;</span><span style="color: #e77801;">&#41;</span>
        at sun.<span style="color: #006600;">reflect</span>.<span style="color: #006600;">NativeMethodAccessorImpl</span>.<span style="color: #006600;">invoke0</span><span style="color: #e77801;">&#40;</span>Native Method<span style="color: #e77801;">&#41;</span>
        at sun.<span style="color: #006600;">reflec</span>...
&nbsp;</pre>
<p>There were some issues raised about how faithful an encoding of SML functors, and well-known extensions for higher-order functors, one can get in Scala.  Indeed, off the top of my head it is not entirely clear.  So I need to think more about that before I write some examples.  </p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/05/26/modules-in-scala/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Featherweight Scala lives</title>
		<link>http://existentialtype.net/2008/05/26/featherweight-scala-lives/</link>
		<comments>http://existentialtype.net/2008/05/26/featherweight-scala-lives/#comments</comments>
		<pubDate>Mon, 26 May 2008 10:54:10 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[research]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[f-bounded quantification]]></category>
		<category><![CDATA[featherweight scala]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[type checking]]></category>
		<category><![CDATA[undecidability]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=244</guid>
		<description><![CDATA[With everything else I've been having to deal with the past couple months, I have not made nearly as much progress on Featherweight Scala as I would have liked. Fortunately, in the past two weeks I have finally started to get back on track. Probably what makes working out the design of Featherweight Scala so [...]]]></description>
			<content:encoded><![CDATA[<p>With everything else I've been having to deal with the past couple months, I have not made nearly as much progress on Featherweight Scala as I would have liked.  Fortunately, in the past two weeks I have finally started to get back on track.</p>
<p>Probably what makes working out the design of Featherweight Scala so difficult is that I am trying my best to commit to it being a valid subset of Scala.  It seems like whenever I try to simplify one part of the language, it just introduces a complication elsewhere.   Still, I think so far that I have been able to ensure that it is a subset with two exceptions.</p>
<p>The first exception is that some programs in Featherweight Scala will diverge in cases where the Scala compiler will generate a terminating program.  This is because in programs that attempt to access an uninitialized field, Scala will return <code>null</code>, while Featherweight Scala diverges.  Since the program many never attempt to use the  <code>null</code> value, it may simple terminate without raising a <code>NullPointerException</code>.</p>
<p>The second exception arises because type checking in Featherweight Scala's type system is most likely undecidable.  Given that the design is in flux, I haven't precisely verified this, but it seems quite plausible given its expressive power is similar to languages where type checking is known to be undecidable.  Because type checking is undecidable, and the Scala language implementation attempts to have its type checking phase be terminating, there should be some programs that can be shown to be well-typed in Featherweight Scala that the Scala compiler will reject.</p>
<p>I think the other thing I've determined is that I do not understand F-bounded quantification as well as I thought I did.  At least, I find myself worried about the cases where the typing rules presently require  templates, that have not yet been verified to be well-formed, to be introduced as hypotheses while verifying their own correctness.  So I need to do some additional reading to see how some other languages deal with this issue.  Certainly I can put my mind at ease by making the type system more restrictive, but I do not want to be so restrictive that it is no longer reasonable to call the language Featherweight Scala.</p>
<p><strong>Update</strong>: Okay, so after looking at Featherweight Generic Java and &nu;Obj again more closely, they both do essentially the same kind of thing.  In FGJ, the class table is well-formed if all its classes are well-formed, but checking a class's well-formedness may require presupposing that that class tale is well-formed. In &nu;Obj, the well-formedness of recursive record types  is checked under the assumption that the record's self-name has the type of the record.  Certainly the same sort of things come up when verifying the well-formedness of recursive functions and recursive types, but at least there what is being assumed is at a higher level of abstraction: when checking a recursive function you assume that the function has some well-formed type, or when checking a recursive type you assume that the type has some well-formed kind.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/05/26/featherweight-scala-lives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Revisiting higher-rank impredicative polymorphism in Scala</title>
		<link>http://existentialtype.net/2008/05/26/revisiting-higher-rank-impredicative-polymorphism-in-scala/</link>
		<comments>http://existentialtype.net/2008/05/26/revisiting-higher-rank-impredicative-polymorphism-in-scala/#comments</comments>
		<pubDate>Mon, 26 May 2008 10:23:36 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[bounded polymorphism]]></category>
		<category><![CDATA[higher-rank]]></category>
		<category><![CDATA[impredicativity]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[type functions]]></category>
		<category><![CDATA[universal quantification]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=243</guid>
		<description><![CDATA[I've been meaning to write this up for a while, but it seems like there has always been something else I really ought to be doing. So I expect this will be a bit more terse than I might like. Anyway, when I wrote about encoding higher-rank universal quantification in Scala back in March, I [...]]]></description>
			<content:encoded><![CDATA[<p>I've been meaning to write this up for a while, but it seems like there has always been something else I really ought to be doing.  So I expect this will be a bit more terse than I might like.  Anyway, when I wrote about <a href="http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/">encoding higher-rank universal quantification in Scala back in March</a>, I used a rather elaborate scheme involving the use of Scala's first-class existential quantifiers.  While this was the point of the exercise, surprisingly, no one called me on the fact that if you just want higher-rank impredicative polymorphism in Scala, there is a much simpler encoding.  Maybe it was obvious to everyone or no one read closely enough to think of raising the issue.  So today, I'll explain the better way to encode them.</p>
<p>First we can define an infinite family of traits to represent <i>n</i>-ary universal quantification, much like Scala represents <i>n</i>-ary functions types:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">trait</span></a> Univ1<span style="color: #e77801;">&#91;</span>Bound1<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#91;</span><span style="color: #a00000;">_</span><span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span> <span style="color: #e77801;">&#123;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> Apply<span style="color: #e77801;">&#91;</span>T1<span style="color: #a00000;">&lt;:</span>Bound1<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">:</span> Body<span style="color: #e77801;">&#91;</span>T1<span style="color: #e77801;">&#93;</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">trait</span></a> Univ2<span style="color: #e77801;">&#91;</span>Bound1<span style="color: #a00000;">,</span>Bound2<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#91;</span><span style="color: #a00000;">_,_</span><span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span> <span style="color: #e77801;">&#123;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> Apply<span style="color: #e77801;">&#91;</span>T1<span style="color: #a00000;">&lt;:</span>Bound1<span style="color: #a00000;">,</span>T2<span style="color: #a00000;">&lt;:</span>Bound2<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">:</span> Body<span style="color: #e77801;">&#91;</span>T1<span style="color: #a00000;">,</span>T2<span style="color: #e77801;">&#93;</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// ... so on for N &gt; 2</span>
&nbsp;</pre>
<p>Really, the key to this encoding is the use of higher-kinded quantification to encode the binding structure of the quantifier.  </p>
<p>Now it is possible to write some examples similar to what I gave previously, but more concisely:</p>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">object</span></a> Test <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">extends</span></a> Application <span style="color: #e77801;">&#123;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> id<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#40;</span>x <span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=</span> x
&nbsp;
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> Id<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=</span> T <span style="color: #a00000;">=&gt;</span> T
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> id <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> Univ1<span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Any</span></a><span style="color: #a00000;">,</span>Id<span style="color: #e77801;">&#93;</span>
     <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> Apply<span style="color: #e77801;">&#91;</span>T <span style="color: #a00000;">&lt;:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Any</span></a><span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">:</span> Id<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=</span> id<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">_</span> <span style="color: #e77801;">&#125;</span>
&nbsp;
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> idString <span style="color: #a00000;">=</span> id.<span style="color: #006600;">Apply</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">String</span></a><span style="color: #e77801;">&#93;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> idStringList <span style="color: #a00000;">=</span> id.<span style="color: #006600;">Apply</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">List</span></a><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">String</span></a><span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span>
&nbsp;
   println<span style="color: #e77801;">&#40;</span>idString<span style="color: #e77801;">&#40;</span><span style="color: #6666ff;">&quot;Foo&quot;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
   println<span style="color: #e77801;">&#40;</span>idStringList<span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">List</span></a><span style="color: #e77801;">&#40;</span><span style="color: #6666ff;">&quot;Foo&quot;</span><span style="color: #a00000;">,</span> <span style="color: #6666ff;">&quot;Bar&quot;</span><span style="color: #a00000;">,</span> <span style="color: #6666ff;">&quot;Baz&quot;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
&nbsp;
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Double</span></a><span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=</span> T <span style="color: #a00000;">=&gt;</span> <span style="color: #e77801;">&#40;</span>T<span style="color: #a00000;">,</span> T<span style="color: #e77801;">&#41;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> double <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> Univ1<span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Any</span></a><span style="color: #a00000;">,</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Double</span></a><span style="color: #e77801;">&#93;</span>
     <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> Apply<span style="color: #e77801;">&#91;</span>T <span style="color: #a00000;">&lt;:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Any</span></a><span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Double</span></a><span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#40;</span>x <span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> <span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> x<span style="color: #e77801;">&#41;</span> <span style="color: #e77801;">&#125;</span>
&nbsp;
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> doubleString <span style="color: #a00000;">=</span> double.<span style="color: #006600;">Apply</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">String</span></a><span style="color: #e77801;">&#93;</span>
   <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> doubleStringList <span style="color: #a00000;">=</span> double.<span style="color: #006600;">Apply</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">List</span></a><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">String</span></a><span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span>
&nbsp;
   println<span style="color: #e77801;">&#40;</span>doubleString<span style="color: #e77801;">&#40;</span><span style="color: #6666ff;">&quot;Foo&quot;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
   println<span style="color: #e77801;">&#40;</span>doubleStringList<span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">List</span></a><span style="color: #e77801;">&#40;</span><span style="color: #6666ff;">&quot;Foo&quot;</span><span style="color: #a00000;">,</span> <span style="color: #6666ff;">&quot;Bar&quot;</span><span style="color: #a00000;">,</span> <span style="color: #6666ff;">&quot;Baz&quot;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;</pre>
<p>As I mentioned previously, this example would be much improved by support for anonymous type functions in Scala.  I am pretty sure Scala will eventually support them, as they would not require any deep changes in the implementation. They could be just implemented by desugaring to a higher-kinded type alias with a fresh name, but depending on when that desugaring is performed, it is possible that it would result in poor error messages.  Supporting curried type functions is also quite desirable, but given my current knowledge of the internals, that seems like adding them will require some more elaborate changes.</p>
<p>I think  <a href="http://lamp.epfl.ch/~cremet/">Vincent Cremet</a> was the first person to suggest this sort of encoding, and I vaguely recall reading about it on one of the Scala mailing lists, but I could not find the message after a little bit of time spent searching.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/05/26/revisiting-higher-rank-impredicative-polymorphism-in-scala/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An unusual definition of total ordering</title>
		<link>http://existentialtype.net/2008/04/09/an-unusual-definition-of-total-ordering/</link>
		<comments>http://existentialtype.net/2008/04/09/an-unusual-definition-of-total-ordering/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 15:16:03 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[equivalence]]></category>
		<category><![CDATA[partial order]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[relations]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[total order]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=237</guid>
		<description><![CDATA[One of the things that will show up in the imminently forthcoming Scala 2.7.1 release candidate, is the addition of traits for representing equivalence relations, partial orderings, and total orderings. Previously, the trait Ordered was used for representing totally ordered things: trait Ordered&#91;A&#93; &#123; def compare&#40;that: A&#41;: Int def &#60; &#40;that: A&#41;: Boolean = &#40;this [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that will show up in the imminently forthcoming Scala 2.7.1 release candidate, is the addition of traits for representing equivalence relations, partial orderings, and total orderings.  Previously,  the trait <code lang="scala">Ordered</code> was used for representing totally ordered things:</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">trait</span></a> Ordered<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #e77801;">&#123;</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> compare<span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> <span style="color: #a00000;">&lt;</span>  <span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">this</span></a> compare that<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&lt;</span>  <span style="color: #f78811;">0</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> <span style="color: #a00000;">&gt;</span>  <span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">this</span></a> compare that<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&gt;</span>  <span style="color: #f78811;">0</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> <span style="color: #a00000;">&lt;=</span> <span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">this</span></a> compare that<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&lt;=</span> <span style="color: #f78811;">0</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> <span style="color: #a00000;">&gt;=</span> <span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> <span style="color: #e77801;">&#40;</span><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">this</span></a> compare that<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&gt;=</span> <span style="color: #f78811;">0</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> compareTo<span style="color: #e77801;">&#40;</span>that<span style="color: #a00000;">:</span> A<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>that<span style="color: #e77801;">&#41;</span>
<span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>However, the <code lang="scala">Ordered</code> trait does not provide a representation of a total ordering.  Therefore, the new trait <code>Ordering</code>:</p>
<blockquote>
<pre class="scala">&nbsp;
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">trait</span></a> Ordering<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">extends</span></a> PartialOrdering<span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span> <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Int</span></a>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">override</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> lteq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&lt;=</span> <span style="color: #f78811;">0</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">override</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> gteq<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&gt;=</span> <span style="color: #f78811;">0</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">override</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> lt<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&lt;</span> <span style="color: #f78811;">0</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">override</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> gt<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">&gt;</span> <span style="color: #f78811;">0</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">override</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> equiv<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">:</span> T<span style="color: #a00000;">,</span> y<span style="color: #a00000;">:</span> T<span style="color: #e77801;">&#41;</span><span style="color: #a00000;">:</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Boolean</span></a> <span style="color: #a00000;">=</span> compare<span style="color: #e77801;">&#40;</span>x<span style="color: #a00000;">,</span> y<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">==</span> <span style="color: #f78811;">0</span>
<span style="color: #e77801;">&#125;</span>
&nbsp;</pre>
</blockquote>
<p>The tricky part however, was writing description of the properties required of something that implements the <code lang="scala">Ordering</code> trait.  When one normally thinks of a <a href="http://en.wikipedia.org/wiki/Total_order">total ordering</a> one thinks of a relation that is</p>
<ul>
<li>anti-symmetric,</li>
<li>transitive,</li>
<li>and total.</li>
</ul>
<p>The problem is that <code lang="scala">Ordering</code> is not defined in terms of a binary relation, but a binary function producing integers (<code lang="scala">compare</code>).  If the first argument is less than the second the function returns a negative integer, if they are equal in the ordering the function returns zero, and if the second argument is less than the first the function returns a positive integer.  Therefore, it is not straightforward to express these same properties.  The best I could come up with was</p>
<ul>
<li><code lang="scala">compare(x, x) == 0</code>, for any <code lang="scala">x</code> of type <code lang="scala">T</code>.</li>
<li><code lang="scala">compare(x, y) == z</code> and <code lang="scala">compare(y, x) == w</code> then <code lang="scala">Math.signum(z) == -Math.signum(w)</code>, for any <code lang="scala">x</code> and <code lang="scala">y</code> of type <code lang="scala">T</code> and <code lang="scala">z</code> and <code lang="scala">w</code> of type <code lang="scala">Int</code>.</li>
<li>if <code lang="scala">compare(x, y) == z</code> and <code lang="scala">lteq(y, w) == v</code> and <code lang="scala">Math.signum(z) &gt;= 0</code> and <code lang="scala">Math.signum(v) &gt;= 0</code> then<br />
 <code lang="scala">compare(x, w) == u</code> and <code lang="scala">Math.signum(z + v) == Math.signum(u)</code>,<br />
 for any <code lang="scala">x</code>, <code lang="scala">y</code>, and <code lang="scala">w</code> of type <code lang="scala">T</code> and <code lang="scala">z</code>, <code lang="scala">v</code>, and <code lang="scala">u</code> of type <code lang="scalal">Int</code>.</li>
</ul>
<p>Where <code lang="scala">Math.signum</code> returns <code lang="scala">-1</code> if its input is negative, <code lang="scala">0</code> if its input is <code lang="scala">0</code>, and <code lang="scala">1</code> if its input is positive.</p>
<p>The first property is clearly reflexivity.  I call the third property transitivity.  I am not sure what to call the second property. I do not think a notion of totality is required because it is assumed you will always get an integer back from <code lang="scala">compare</code> rather than it throwing an exception or going into an infinite loop.  </p>
<p>It would probably be a good exercise to prove that given these properties on <code lang="scala">compare</code> hold if and only if <code lang="scala">lteq</code> (defined above in terms of <code lang="scala">compare</code>) has all the normal properties of a total ordering.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/04/09/an-unusual-definition-of-total-ordering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

