<?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; functors</title>
	<atom:link href="http://existentialtype.net/tag/functors/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>Functors in Scala</title>
		<link>http://existentialtype.net/2008/05/26/functors-in-scala/</link>
		<comments>http://existentialtype.net/2008/05/26/functors-in-scala/#comments</comments>
		<pubDate>Mon, 26 May 2008 19:42:19 +0000</pubDate>
		<dc:creator>washburn</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[functors]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[sml]]></category>

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

