<?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; higher-rank</title>
	<atom:link href="http://existentialtype.net/tag/higher-rank/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>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>
	</channel>
</rss>

