<?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; languages</title>
	<atom:link href="http://existentialtype.net/category/languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://existentialtype.net</link>
	<description>For People Who Like Type and Types</description>
	<lastBuildDate>Sat, 12 May 2012 16:35:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</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>Out with the cheese, in with the pastry</title>
		<link>http://existentialtype.net/2008/12/19/out-with-the-cheese-in-with-the-pastry/</link>
		<comments>http://existentialtype.net/2008/12/19/out-with-the-cheese-in-with-the-pastry/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 17:14:06 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[delphin]]></category>
		<category><![CDATA[itu]]></category>
		<category><![CDATA[lf]]></category>
		<category><![CDATA[scala classic]]></category>
		<category><![CDATA[twelf]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=269</guid>
		<description><![CDATA[I have unfortunately been quite busy the past several months, and have not had as much time to write about what I have been doing as I would have liked.  For the most part, I have been splitting my time between teaching and research.  I will hopefully go into more detail about the latter in [...]]]></description>
			<content:encoded><![CDATA[<p>I have unfortunately been quite busy the past several months, and have not had as much time to write about what I have been doing as I would have liked.  For the most part, I have been splitting my time between teaching and research.  I will hopefully go into more detail about the latter in the near future.</p>
<p>However, I figured I should take some time now, before I leave for my first proper vacation in a year, to announce that I have accepted a postdoc position at <a href="http://www.itu.dk/">ITU</a> in <a href="http://en.wikipedia.org/wiki/Copenhagen">Copenhagen</a> with Carsten Schürmann.  This has been in the works for a while now, but this week I finally was able to make it official.  My plan is to start at ITU at the beginning of March, where I will be working on things relating to the <a href="http://en.wikipedia.org/wiki/LF_%28logical_framework%29">LF family of logical frameworks</a>, <a href="http://www.twelf.org/">Twelf</a>, and <a href="http://www.cs.yale.edu/~delphin/">Delphin</a>.</p>
<p>There are probably a vanishingly small number of you that care about what this means for Scala Classic, but I hope to write something much more detailed about it when I get back from my vacation.  I'll leave you in suspense with the short answer: despite all my efforts, it simply is not possible to prove it sound without making it a very different language.  Which is rather unfortunate.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/12/19/out-with-the-cheese-in-with-the-pastry/feed/</wfw:commentRss>
		<slash:comments>4</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>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>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>4</slash:comments>
		</item>
		<item>
		<title>A new species</title>
		<link>http://existentialtype.net/2008/04/30/a-new-species/</link>
		<comments>http://existentialtype.net/2008/04/30/a-new-species/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 06:55:50 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[papers]]></category>
		<category><![CDATA[theory]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[girard]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[LtU]]></category>
		<category><![CDATA[ludics]]></category>
		<category><![CDATA[species]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=239</guid>
		<description><![CDATA[I spent some time last night looking more closely at Carette and Uszkey's new paper on species, as mentioned recently on Lambda the Ultimate. I mostly concentrated on the first few sections and the Haskell bit at the end. They say many tantalizing things, and I had kind of hoped the punchline would be something [...]]]></description>
			<content:encoded><![CDATA[<p>I spent some time last night looking more closely at Carette and Uszkey's new paper on <a href="http://www.cas.mcmaster.ca/~carette/species/">species</a>, as mentioned recently on <a href="http://lambda-the-ultimate.org/node/2785">Lambda the Ultimate</a>.  I mostly concentrated on the first few sections and the Haskell bit at the end.  They say many tantalizing things, and I had kind of hoped the punchline would be something like "here is how you can write code in a beautifully functional style over graphs and cyclic structures" (assuming I understood the potential of species correctly).  Perhaps in a follow up paper.</p>
<p>I also found it interesting that their dot product operator sounds more like the traditional Cartesian product type (one of each type), and their Cartesian product type sounds much more like an intersection type (the value is a structure having both types).  Perhaps it is a notational holdover from previous work on species.  It also seem to make for some nice concrete examples of the "locative" aspects of Girard's <a href="http://en.wikipedia.org/wiki/Ludics">Ludics</a>.</p>
<p>Now back to reviewing journal and ICFP papers...</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/04/30/a-new-species/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The problem with being a PL researcher</title>
		<link>http://existentialtype.net/2008/04/09/the-problem-with-being-a-pl-researcher/</link>
		<comments>http://existentialtype.net/2008/04/09/the-problem-with-being-a-pl-researcher/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 13:56:21 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://existentialtype.net/?p=236</guid>
		<description><![CDATA[The problem with being a programming languages researcher is that you can wind up like a blacksmith who spends all of his time tweaking his hammer and anvil, instead of producing less esoteric artifacts.]]></description>
			<content:encoded><![CDATA[<p>The problem with being a programming languages researcher is that you can wind up like a blacksmith who spends all of his time tweaking his hammer and anvil, instead of producing less esoteric artifacts.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/04/09/the-problem-with-being-a-pl-researcher/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Best quote from a paper this week</title>
		<link>http://existentialtype.net/2008/03/21/best-quote-from-a-paper-this-week/</link>
		<comments>http://existentialtype.net/2008/03/21/best-quote-from-a-paper-this-week/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 17:50:49 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[humour]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[papers]]></category>

		<guid isPermaLink="false">http://existentialtype.net/2008/03/21/best-quote-from-a-paper-this-week/</guid>
		<description><![CDATA[Programming in OMeta would be very frustrating if all productions were defined in the same namespace: two grammars might unknowingly use the same name for two productions that have different purposes, and one of them would certainly stop working! (Picture one sword-wielding grammar decapitating another, Highlander-style: “There can be only one!”) From Warth and Piumarta's [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Programming in OMeta would be very frustrating if all productions were defined in the same namespace: two grammars might unknowingly use the same name for two productions that have different purposes, and one of them would certainly stop working! (Picture one sword-wielding grammar decapitating another, Highlander-style: “There can be only one!”)</p></blockquote>
<p>From Warth and Piumarta's <a href="http://www.cs.ucla.edu/~awarth/papers/dls07.pdf">OMeta: an Object-Oriented Language for Pattern Matching</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/03/21/best-quote-from-a-paper-this-week/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Correction on existential unpacking</title>
		<link>http://existentialtype.net/2008/03/09/correction-on-existential-unpacking/</link>
		<comments>http://existentialtype.net/2008/03/09/correction-on-existential-unpacking/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 20:53:41 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[correction]]></category>
		<category><![CDATA[existentials]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://existentialtype.net/2008/03/09/correction-on-existential-unpacking/</guid>
		<description><![CDATA[(WordPress ate my first draft, grrr.) So after thinking about it further, I was incorrect, and it is possible to explicitly unpack existentials in Scala. As with some other languages, it is done via pattern matching. The following example illustrates how: val x : List&#91;T&#93; forSome &#123; type T &#125; = List&#40;42&#41; val w = [...]]]></description>
			<content:encoded><![CDATA[<p>(WordPress ate my first draft, grrr.)</p>
<p>So after thinking about it further, <a href="http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/">I was incorrect</a>, and it is possible to explicitly unpack existentials in Scala.  As with some other languages, it is done via pattern matching.  The following example illustrates how:</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> x <span style="color: #a00000;">:</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>T<span style="color: #e77801;">&#93;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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: #e77801;">&#125;</span> <span style="color: #a00000;">=</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: #f78811;">42</span><span style="color: #e77801;">&#41;</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> w <span style="color: #a00000;">=</span> x <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">match</span></a> <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">case</span></a> y <span style="color: #a00000;">:</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>u<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>z <span style="color: #a00000;">:</span> u<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> z<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#40;</span>y.<span style="color: #006600;">head</span><span style="color: #e77801;">&#41;</span> <span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>However, in practice this functionality seems to be rather fragile.  For example, the following two variations are rejected:</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> x <span style="color: #a00000;">:</span> T <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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: #e77801;">&#125;</span> <span style="color: #a00000;">=</span> <span style="color: #f78811;">42</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> w <span style="color: #a00000;">=</span> x <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">match</span></a> <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">case</span></a> y <span style="color: #a00000;">:</span> u <span style="color: #a00000;">=&gt;</span> <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>z <span style="color: #a00000;">:</span> u<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> z<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#40;</span>y<span style="color: #e77801;">&#41;</span> <span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>and</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> x <span style="color: #a00000;">:</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;">List</span></a><span style="color: #e77801;">&#91;</span>T<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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: #e77801;">&#125;</span> <span style="color: #a00000;">=</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><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: #f78811;">42</span><span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> w <span style="color: #a00000;">=</span> x <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">match</span></a> <span style="color: #e77801;">&#123;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">case</span></a> y <span style="color: #a00000;">:</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;">List</span></a><span style="color: #e77801;">&#91;</span>u<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>z <span style="color: #a00000;">:</span> u<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> z<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#40;</span>y.<span style="color: #006600;">head</span>.<span style="color: #006600;">head</span><span style="color: #e77801;">&#41;</span> <span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>In both cases it reports that it cannot find the type <code>u</code>.  In the second case, it could be attributed to erasure, as there is no way dynamically to guarantee that the contents of <code>List</code> are in turn also a <code>List</code>.  However, the first seems reasonable, so it should probably be reported as a bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/03/09/correction-on-existential-unpacking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Higher-rank, impredicative polymorphism in Scala</title>
		<link>http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/</link>
		<comments>http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 19:42:22 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[boxes go bananas]]></category>
		<category><![CDATA[classical equivalences]]></category>
		<category><![CDATA[cps conversion]]></category>
		<category><![CDATA[double negation]]></category>
		<category><![CDATA[existentials]]></category>
		<category><![CDATA[ghc]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[twelf]]></category>
		<category><![CDATA[universals]]></category>

		<guid isPermaLink="false">http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/</guid>
		<description><![CDATA[One project that I spent some time thinking about prior arriving in Lausanne was whether I might be able to put together a Scala library for manipulating data-structures with binding based around the use of parametricity to help enforce the adequacy of function spaces (ala Boxes Go Bananas). However, one problem is that Scala does [...]]]></description>
			<content:encoded><![CDATA[<p>One project that I spent some time thinking about prior arriving in Lausanne was whether I might be able to put together a <a href="http://www.scala-lang.org/">Scala</a> library for manipulating data-structures with binding based around the use of parametricity to help enforce the adequacy of function spaces (ala <a href="http://journals.cambridge.org/action/displayAbstract?fromPage=online&amp;aid=1457888&amp;fulltextType=RA&amp;fileId=S0956796807006557">Boxes Go Bananas</a>).  However, one problem is that Scala does not have higher-rank polymorphism like <a href="http://www.haskell.org/ghc/">GHC</a>.  The initial thought was, "well, Scala has higher-rank existential types, so adding the dual case should be pretty straightforward to add to the typechecker".  However, it turns out it is not so straightforward to add universal quantification to the implementation of the subtyping algorithm and ensure that constraints can be solved unambiguously.  It still may make sense to try and add them in some fashion.</p>
<p>Later, while working on Featherweight Scala, it occurred to me that subtyping is very likely to be undecidable in Scala because its language of types is essentially as expressive as full F<sub>&lt;:</sub>.  Benjamin agreed that there existed a straightforward reduction to languages with only bounded existential quantification rather than bounded universal quantification, but did not know offhand if there is a paper that gives it.</p>
<p>So I spent a bit of time thinking about encoding universal quantification using existential quantification, and as alluded to in TAPL (the answer to exercise 24.3.3), it can be done, but there is no completely local encoding of universals into existentials.  To obtain the classical equivalences between the negation of universal quantification and existential quantification (∀α.τ implies ¬∃α.¬τ) in a constructive setting you have to use the <a href="http://en.wikipedia.org/wiki/G%C3%B6del%E2%80%93Gentzen_negative_translation">double-negation encoding</a>, which essentially means <a href="http://en.wikipedia.org/wiki/Continuation-passing_style">CPS converting</a> your program. The other classical equivalence under negation, ∃α.τ implies ¬∀α.¬τ, is constructively valid which is why it is existential quantification is <a href="http://www.ccs.neu.edu/scheme/pubs/scp91-felleisen.ps.gz">macro-expressible</a> in languages with universal quantification.</p>
<p>Since I am not aware of any papers that illustrate the encoding of universals into existentials, I figured I would explain it here. The encoding of the universal type pretty much follows from the equivalence:</p>
<p align="center">|∀α.τ| =  (∃α.|τ|→0)→0</p>
<p>Here I am using 0 for the void or empty type and using the standard constructive encoding of negation:</p>
<p align="center">|¬τ| = |τ| → 0</p>
<p>The term level encodings are not so difficult, but does require a little thought:</p>
<p align="center">|Λα.e : ∀α.τ|<sub>k</sub> = k (λx:∃α.|τ|→0.let (α,k') = unpack x in |e : τ|<sub>k'</sub>)</p>
<p>The term encoding has the form |e : τ|<sub>k</sub>, where τ is the type of the term and k is a continuation threaded through the encoding. The first thing to note is that because type abstractions are values, we always immediately pass them to the continuation.  Given that,<br />
if we look at the type we need to give the encoding of type abstraction, it is a function that that takes an existentially quantified continuation, and never returns.  The only way to never return is to use the continuation.  This tells us just about everything else we need to know about the encoding.</p>
<p>Because the continuation is existentially quantified, it is not possible to invoke it as is, so we must unpack it first.  This is how we simulate the introduction of a type variable without using type abstraction.  The next problem is that in order to call the continuation, we need to apply it to something with the correct type.  The continuation takes values of type |τ|, possibly referencing a free type variable α, which is fortunately what the encoding of the type abstraction's body gives us.  Therefore, we can use the unpacked continuation k' as the continuation argument of the recursive call to the encoding.</p>
<p>Given that we know how to encode type abstractions, encoding type applications is straightforward:</p>
<p align="center">|e[τ] : τ'|<sub>k</sub> = |e : ∀α.τ'{α/τ}|<sub>(λv:(∃α.τ'{α/τ}→0)→0.v (pack(τ,k) as ∃α.(τ'{α/τ}→0)))</sub></p>
<p>We encode the term to be applied and pass it a new continuation, that packs up the original continuation and applies it to function to which the term evaluates.  I am writing {α/τ} to mean replace τ with α.  I am using curly braces rather than the usual square brackets to avoid confusion with the square brackets that is commonly used for type application.</p>
<p>While I am almost certain that the encoding above is correct, it would be good at some point to prove it.  This is the sort of proof for which <a href="http://twelf.plparty.org/wiki/Main_Page">Twelf</a> is very well suited, so that is probably the route I would take.</p>
<p>Okay, so that is the theoretical half out of the way.  Now, given the above, can we implement higher-rank impredicative polymoprhism in Scala?  Yes, with a minor caveat.</p>
<p>First, I'll define an abstract class that defines the type of bounded universally quantified values, and methods for creating and destructing them.</p>
<blockquote>
<pre class="scala"><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;">class</span></a> Universal <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> Univ<span style="color: #e77801;">&#91;</span>Bound<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>
&nbsp;
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> createUniv<span style="color: #e77801;">&#91;</span>Bound<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;">&#40;</span>impl <span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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> A <span style="color: #a00000;">&lt;:</span> Bound <span style="color: #e77801;">&#125;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span>
        Univ<span style="color: #e77801;">&#91;</span>Bound<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#93;</span>
&nbsp;
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> applyUniv<span style="color: #e77801;">&#91;</span>Bound<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: #a00000;">,</span>A<span style="color: #a00000;">&lt;:</span>Bound<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> Univ<span style="color: #e77801;">&#91;</span>Bound<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span>
<span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>This should be fairly straightforward given the encoding described above.  The only tricky bit is the use of a higher-kinded type parameter (<code lang="scala">Body[_]</code>) to give the body of the universal type.  The other important point to note is that it is not necessary to completely CPS convert the implementation because Scala provides primitives for non-local control transfers.</p>
<p>One possible implementation of the above abstract class is the following:</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">class</span></a> UniversalImpl <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">extends</span></a> Universal <span style="color: #e77801;">&#123;</span>
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">type</span></a> Univ<span style="color: #e77801;">&#91;</span>Bound<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: #a00000;">=</span>
     <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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> A <span style="color: #a00000;">&lt;:</span> Bound <span style="color: #e77801;">&#125;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a>
&nbsp;
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> createUniv<span style="color: #e77801;">&#91;</span>Bound<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;">&#40;</span>impl <span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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> A <span style="color: #a00000;">&lt;:</span> Bound <span style="color: #e77801;">&#125;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span>
        Univ<span style="color: #e77801;">&#91;</span>Bound<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=</span> impl
&nbsp;
  <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">def</span></a> applyUniv <span style="color: #e77801;">&#91;</span>Bound<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: #a00000;">,</span>A<span style="color: #a00000;">&lt;:</span>Bound<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#40;</span>univ <span style="color: #a00000;">:</span> Univ<span style="color: #e77801;">&#91;</span>Bound<span style="color: #a00000;">,</span>Body<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">:</span> Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <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: #000099;">case</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">class</span></a> Control<span style="color: #e77801;">&#40;</span>arg <span style="color: #a00000;">:</span> Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">extends</span></a> Throwable
    <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> res <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">try</span></a> <span style="color: #e77801;">&#123;</span>
      univ<span style="color: #e77801;">&#40;</span><span style="color: #e77801;">&#40;</span>arg<span style="color: #a00000;">:</span>Body<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">throw</span></a> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> Control<span style="color: #e77801;">&#40;</span>arg<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
    <span style="color: #e77801;">&#125;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">catch</span></a> <span style="color: #e77801;">&#123;</span>
      <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">case</span></a> Control<span style="color: #e77801;">&#40;</span>arg<span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span> arg
      <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">case</span></a> e <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #000099;">throw</span></a> e
    <span style="color: #e77801;">&#125;</span>
    res
  <span style="color: #e77801;">&#125;</span>
<span style="color: #e77801;">&#125;</span></pre>
</blockquote>
<p>The implementation of the abstract type and the code for <code lang="scala">createUniv</code> are trivial.  The implementation of <code lang="scala">applyUniv</code> is a little more interesting.  Here, we create a local case class <code lang="scala">Control</code>, extending <code lang="scala">Throwable</code> so that it may be thrown as an exception, that will hold a value of the desired result type.  We then just pass the  representation of the type abstraction a continuation that throws a new instance of <code lang="scala">Control</code>.  We immediately catch it and return the value stored within.  If we happen to catch some other sort of <code lang="scala">Throwable</code> we just re-throw it.</p>
<p>And that's it.  It is worth looking at a few examples of how this might be used.  The first thing that is necessary is to create a concrete implementation of <code>Universal</code>:</p>
<blockquote>
<pre class="scala"><a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> u <span style="color: #a00000;">:</span> Universal <span style="color: #a00000;">=</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">new</span></a> UniversalImpl</pre>
</blockquote>
<p>Given <code lang="scala">u</code>, we can create an instance of the polymorphic identity function:</p>
<blockquote>
<pre class="scala"><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>
    u.<span style="color: #006600;">createUniv</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">AnyRef</span></a><span style="color: #a00000;">,</span>Id<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#40;</span>
      <span style="color: #e77801;">&#40;</span>k<span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span>Id<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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> A <span style="color: #e77801;">&#125;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span>
         k<span style="color: #e77801;">&#40;</span>x <span style="color: #a00000;">=&gt;</span> x<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span>
<a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">val</span></a> idString <span style="color: #a00000;">=</span> u.<span style="color: #006600;">applyUniv</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">AnyRef</span></a><span style="color: #a00000;">,</span>Id<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;">&#93;</span><span style="color: #e77801;">&#40;</span>id<span style="color: #e77801;">&#41;</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> u.<span style="color: #006600;">applyUniv</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">AnyRef</span></a><span style="color: #a00000;">,</span>Id<span style="color: #a00000;">,</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><span style="color: #e77801;">&#40;</span>id<span style="color: #e77801;">&#41;</span>
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></pre>
</blockquote>
<p>The first thing that needs to be done is to define a type function for representation the body of the universally quantified type.  For the polymorphic identity function we call it <code lang="scala">Id[T]</code>.  The rest is pretty straightforward.  It ought to be possible to make the above code a little less verbose if Scala allowed type parameters to methods to be curried: it should be possible for it to infer that the first two arguments to <code lang="scala">u.applyUniv</code> are <code lang="scala">AnyRef</code> and <code lang="scala">Id</code> from the argument <code lang="scala">id</code>.  However, because it will not be able to infer the last type parameter, we have to give all three.  It might also be desirable in some case to not have to give a type definition to define the body of the a universal type; this could be achieve by extending Scala with support for anonymous type functions.</p>
<p>Now for that caveat I mentioned.    When we defined the type abstraction,</p>
<blockquote>
<pre class="scala">u.<span style="color: #006600;">createUniv</span><span style="color: #e77801;">&#91;</span><a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #99cc99;">AnyRef</span></a><span style="color: #a00000;">,</span>Id<span style="color: #e77801;">&#93;</span><span style="color: #e77801;">&#40;</span>
  <span style="color: #e77801;">&#40;</span>k<span style="color: #a00000;">:</span> <span style="color: #e77801;">&#40;</span>Id<span style="color: #e77801;">&#91;</span>A<span style="color: #e77801;">&#93;</span> <span style="color: #a00000;">=&gt;</span> <a href="http://www.scala-lang.org/docu/files/api/index.html"><span style="color: #5555cc;">Nothing</span></a><span style="color: #e77801;">&#41;</span> <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"><span style="color: #009900;">forSome</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> A <span style="color: #e77801;">&#125;</span><span style="color: #e77801;">&#41;</span> <span style="color: #a00000;">=&gt;</span>
    k<span style="color: #e77801;">&#40;</span>x <span style="color: #a00000;">=&gt;</span> x<span style="color: #e77801;">&#41;</span><span style="color: #e77801;">&#41;</span></pre>
</blockquote>
<p>a significant difference between how this code is written, and how it would have been written in terms of my original explanation of the encoding, is that there is no explicit unpacking of the existential.  All existential unpacking in Scala is implicit.  This is nice in some ways, but it means that we have no way to give a name to type that the existential is hiding.  Therefore, when constructing a function to pass to the continuation,</p>
<blockquote>
<pre class="scala">k<span style="color: #e77801;">&#40;</span>x <span style="color: #a00000;">=&gt;</span> x<span style="color: #e77801;">&#41;</span></pre>
</blockquote>
<p>it must be the case that Scala can infer the domain of this function's type, because it is not possible to write a type annotation for <code lang="scala">x</code> without having a name for the existentially hidden type.  I think in most cases this should not be possible, as Scala knows the type that <code lang="scala">k</code> is expecting as an argument, but there may be some corner cases I have not considered.</p>
<p>(Does anyone know how to prevent WordPress from completely screwing up less than and greater than inside of &lt;pre&gt; tags?)</p>
]]></content:encoded>
			<wfw:commentRss>http://existentialtype.net/2008/03/09/higher-rank-impredicative-polymorphism-in-scala/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

