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

