<?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; dependent types</title>
	<atom:link href="http://existentialtype.net/tag/dependent-types/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>Literally dependent types</title>
		<link>http://existentialtype.net/2008/07/21/literally-dependent-types/</link>
		<comments>http://existentialtype.net/2008/07/21/literally-dependent-types/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 17:26:29 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[dependent types]]></category>
		<category><![CDATA[implicit conversions]]></category>
		<category><![CDATA[literals]]></category>
		<category><![CDATA[scala]]></category>

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

