<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Apocalisp</title>
	<atom:link href="http://apocalisp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://apocalisp.wordpress.com</link>
	<description>The end of programming as you know it</description>
	<lastBuildDate>Fri, 11 Sep 2009 22:31:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='apocalisp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/2f9cebefdf40abb34726b98ac36d3dc0?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Apocalisp</title>
		<link>http://apocalisp.wordpress.com</link>
	</image>
			<item>
		<title>More Scala Typehackery</title>
		<link>http://apocalisp.wordpress.com/2009/09/02/more-scala-typehackery/</link>
		<comments>http://apocalisp.wordpress.com/2009/09/02/more-scala-typehackery/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 01:18:51 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=220</guid>
		<description><![CDATA[Inspired by this page on the Haskell Wiki, here&#8217;s a stripped down version of the lambda calculus encoded in the Scala type system:


trait Lambda {
  type subst[U &#60;: Lambda] &#60;: Lambda
  type apply[U &#60;: Lambda] &#60;: Lambda
  type eval &#60;: Lambda
}

trait App[S &#60;: Lambda, T &#60;: Lambda] extends Lambda {
  type [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=220&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Inspired by <a href="http://www.haskell.org/haskellwiki/Type_arithmetic">this page on the Haskell Wiki</a>, here&#8217;s a stripped down version of the lambda calculus encoded in the Scala type system:</p>
<pre>
<pre class="brush: java;">
trait Lambda {
  type subst[U &lt;: Lambda] &lt;: Lambda
  type apply[U &lt;: Lambda] &lt;: Lambda
  type eval &lt;: Lambda
}

trait App[S &lt;: Lambda, T &lt;: Lambda] extends Lambda {
  type subst[U &lt;: Lambda] = App[S#subst[U], T#subst[U]]
  type apply[U] = Nothing
  type eval = S#eval#apply[T]
}

trait Lam[T &lt;: Lambda] extends Lambda {
  type subst[U &lt;: Lambda] = Lam[T]
  type apply[U &lt;: Lambda] = T#subst[U]#eval
  type eval = Lam[T]
}

trait X extends Lambda {
  type subst[U &lt;: Lambda] = U
  type apply[U] = Lambda
  type eval = X
}
</pre>
</pre>
<p>Now to see if it works. Here&#8217;s a simple identity:</p>
<pre>scala&gt; type x = Equal[App[Lam[X], X]#Eval, X]
defined type alias x</pre>
<p>Yay! You might want to know what Equal is. It&#8217;s a trait that doesn&#8217;t typecheck unless both of its type parameters are the same (thanks, Jesper):</p>
<pre>
<pre class="brush: java;">
trait Equal[T1 &gt;: T2 &lt;: T2, T2]
</pre>
</pre>
<p>Let&#8217;s try an expression that diverges (never terminates) in the untyped lambda calculus:</p>
<pre>scala&gt; type x = App[Lam[App[X, X]], Lam[App[X, X]]]#Eval
:8: error: illegal cyclic reference involving type Eval</pre>
<p>That&#8217;s Scala 2.7.4. It figures out that we&#8217;re trying to construct an infinite type. Hurray! But that also means it&#8217;s not Turing-equivalent. There will be other, legitimate types that it can&#8217;t check either.</p>
<p>Try the same thing in 2.8 and you get&#8230;</p>
<pre>
Exception in thread "main" java.lang.StackOverflowError
        at scala.tools.nsc.symtab.Symbols$Symbol.isPackageClass(Symbols.scala:407)
        at scala.tools.nsc.symtab.Types$Type.isGround(Types.scala:640)
        at scala.tools.nsc.symtab.Types$Type.isGround(Types.scala:640)
        at scala.tools.nsc.symtab.Types$Type.isGround(Types.scala:640)
        at scala.tools.nsc.symtab.Types$Type.isGround(Types.scala:640)
        at scala.tools.nsc.symtab.Types$Type.isGround(Types.scala:638)
        at scala.tools.nsc.symtab.Types$Type$$anonfun$isGround$1.apply(Types.scala:638)
        at scala.tools.nsc.symtab.Types$Type$$anonfun$isGround$1.apply(Types.scala:638)
        at scala.collection.generic.LinearSequenceTemplate$class.forall(LinearSequenceTemplate.scala:97)
        at scala.collection.immutable.List.forall(List.scala:27)
</pre>
<p>&#8230;which is what you would expect. So it looks like typechecking in 2.8 has become undecidable in general. But don&#8217;t fret. You really have to try hard to get this kind of result.</p>
<p>Data types such as the Church numerals and Booleans can be encoded in this calculus. I might post those a bit later.</p>
<p>Scala rocks.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/220/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=220&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2009/09/02/more-scala-typehackery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>A Scala Puzzler</title>
		<link>http://apocalisp.wordpress.com/2009/08/28/a-scala-puzzler/</link>
		<comments>http://apocalisp.wordpress.com/2009/08/28/a-scala-puzzler/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 07:19:19 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=211</guid>
		<description><![CDATA[I&#8217;ve been toying around with Scala&#8217;s type system a little bit, and I&#8217;ve come to the conclusion that it&#8217;s completely brilliant. And I&#8217;m very encouraged by how rapidly it has improved.
There is, for example, limited support for type-level computation. But not so much that typing becomes undecidable.
In Scala, types can have type members, and those [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=211&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve been toying around with Scala&#8217;s type system a little bit, and I&#8217;ve come to the conclusion that it&#8217;s completely brilliant. And I&#8217;m very encouraged by how rapidly it has improved.</p>
<p>There is, for example, limited support for type-level computation. But not so much that typing becomes undecidable.</p>
<p>In Scala, types can have type members, and those members can take type parameters. Type members can be bounded, and so can their parameters.</p>
<pre>
<pre class="brush: java;">

trait Lambda { type a[A &lt;: Lambda] &lt;: Lambda }
</pre>
</pre>
<p>This feature gives you quite a lot of expressive power in the type system. Type members are accessed by use of the # symbol. You can nest types, so Scala effectively has closures for types. So you can do crazy stuff like this:</p>
<pre>
<pre class="brush: java;">
  trait S extends Lambda { type a[X &lt;: Lambda] = Lambda { type a[Y &lt;: Lambda] = Lambda { type a[Z] = X#a[Z]#a[Y#a[Z]] }} }
</pre>
</pre>
<p>OK, since we basically have the power of the lambda calculus in the type system, let&#8217;s see if we can do Church encoding of datatypes. It turns out that we can, to an extent. Here are booleans:</p>
<pre>
<pre class="brush: java;">
  trait Boolean { type cond[t, f] }
  trait TFalse extends Boolean { type cond[t, f] = f }
  trait TTrue extends Boolean { type cond[t, f] = t }
</pre>
</pre>
<p>Let&#8217;s test that out.</p>
<pre>scala&gt; val x: TTrue#cond[Int, String] = 10
x: TTrue#cond[Int,String] = 10

scala&gt; val x: TTrue#cond[Int, String] = "10"
:6: error: type mismatch;
 found   : java.lang.String("10")
 required: TTrue#cond[Int,String]
 val x: TTrue#cond[Int, String] = "10"</pre>
<p>It works. We can assign an Int to the value if the condition is TTrue, and a String if it&#8217;s TFalse, but not vice versa.</p>
<p>How about church numerals? Here are those:</p>
<pre>
<pre class="brush: java;">
trait Num { type a[F[_], X] }
trait Zero extends Num { type a[F[_], X] = X }
trait One extends Num { type a[F[_], X] = F[X] }
trait Two extends Num { type a[F[_], X] = F[F[X]] }
trait Three extends Num { type a[F[_], X] = F[F[F[X]]] }
</pre>
</pre>
<p>The way this works is that the numerals take some type constructor F and a unit type X. For example, to get lists of strings three levels deep, Three#a[List, String] is equivalent to the type List[List[List[String]]]</p>
<pre>scala&gt; val x: Three#a[List, String] = List(List(List("Three")), List(List("Levels"), List("Deep")))
x: Three#a[List,String] = List(List(List(Three)), List(List(Levels), List(Deep)))</pre>
<p>We can even do some arithmetic. Here are three type-level functions. Respectively, they add two numbers together, increment a number by one, and multiply one number by another:</p>
<pre>
<pre class="brush: java;">
trait PApp[F[P[_], _], A[_]] {
  type a[X] = F[A, X]
}

trait Plus[M &lt;: Num, N &lt;: Num] extends Num { type a[F[_], X] = M#a[F, N#a[F, X]] }
trait Succ[N &lt;: Num] extends Num { type a[F[_], X] = F[N#a[F, X]] }
trait Mult[M &lt;: Num, N &lt;: Num] extends Num { type a[F[_], X] = PApp[N#a, PApp[M#a, F]#a]#a[X] }
</pre>
</pre>
<p>Pretty sweet. For example, a List of Strings 20 levels deep would be of the type <code>Mult[Succ[Three], Plus[Two, Three]]]#a[List, String]</code></p>
<p>Now for the puzzler.</p>
<p><strong>Exercise:</strong> Write a type-level function <code>Exp[M, N]</code> that yields the Church numeral M lifted to the Nth power. Can it be done? If not, why not?</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=211&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2009/08/28/a-scala-puzzler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Some Notes in Hostility Toward Subtyping</title>
		<link>http://apocalisp.wordpress.com/2009/08/27/hostility-toward-subtyping/</link>
		<comments>http://apocalisp.wordpress.com/2009/08/27/hostility-toward-subtyping/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 00:54:04 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=203</guid>
		<description><![CDATA[What follows is a session of thinking out loud, so it&#8217;s not to be taken too seriously.
The more I think about it, the more I convince myself that the idea of subtyping and class hierarchies is a mistake. &#8220;Inheritance&#8221; is a good way to lock down a design so that it becomes rigid and brittle. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=203&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><em>What follows is a session of thinking out loud, so it&#8217;s not to be taken too seriously.</em></p>
<p>The more I think about it, the more I convince myself that the idea of subtyping and class hierarchies is a mistake. &#8220;Inheritance&#8221; is a good way to lock down a design so that it becomes rigid and brittle. If you remember your (now scorched, I trust) GoF book, its main cargo was &#8220;composition over inheritance&#8221;. So why do we want this mechanism at all? Because we want type substitution. We say that if X is a subtype of Y, then X can be substituted wherever Y is expected. Informally, we want to say that an X <em>is a</em> Y. But what does this mean? It means that the set of all values in X is a subset of all the values in Y. So if you have a value x in X, then x is also in Y. In other words, X implies Y, or (X =&gt; Y). Hang on. This is just the type signature for a function.</p>
<p>When we observe this, we realise that we can substitute functions for subtyping. Moreover, we can make those functions implicit (as in Scala) and then type substitution will work just as if we were using subtypes (hopefully without loss of type inference). As a bonus, our implicit type substitutions have scope, while subtyping is a global declaration.</p>
<p>For example, take a look at the function type (A =&gt; B), recalling that this conceptually the same as saying &#8220;A implies B&#8221;, or even &#8220;A is a subtype of B&#8221;. Using subtyping, we could have types Foo and Bar, and say &#8220;Bar extends Foo&#8221;. But instead of subtyping, in Scala we could have an implicit function:</p>
<pre>implicit def fooBar(b: Bar): Foo = b.foo</pre>
<p>The easiest way to implement this is to have Bar wrap a value of type Foo by accepting it in its constructor. Then the Bar.foo method simply returns this value. But it can really be implemented any way we want. For example, if both Foo and Bar take a parameter of type Int, then we can extract it from one when constructing the other.</p>
<p>There&#8217;s a slight problem with implicit functions like this. Somebody else may have defined a function with the same type already, with a totally different intent. To remedy that, we can create a type for a specific kind of conversion to Foo; one that implies the relation we want to express. Something like&#8230;</p>
<pre>trait IsAFoo[A] {
  def apply(a: A): Foo
}</pre>
<p>This is now unambiguous. This kind of construct is a typeclass. IsAFoo classifies all types that imply type Foo. We can supply an implicit instance of this typeclass for Bar:</p>
<pre>implicit val bar_is_a_foo = new IsAFoo[Bar] {
  def apply(b: Bar) = b.foo
}</pre>
<p>And wherever we want to accept something that &#8220;is a&#8221; Foo, we accept an implicit parameter as evidence that it is indeed a Foo.</p>
<pre>implicit def doSomethingWithFoo[A](implicit foo: IsAFoo[A])(a: A) = foo(a).methodOnFoo</pre>
<p>We can call this method with a value of type Bar, because of the existence of the implicit instance bar_is_a_foo. In fact, Scala has an even nicer syntax for this, using &#8220;view bounds&#8221;. I leave it to you to check that out. What I want to impress on you is how flexible typeclasses are. We&#8217;re not constrained to using this mechanism to substitute for subtyping. We can use it to do the converse, i.e. supertyping an existing type. Or we can have the conversion go both ways to express isomorphisms between types. The pattern here is that we want to be able to state any kind of relation between types.</p>
<p>If Scala had the ability to state <a href="http://www.haskell.org/haskellwiki/Functional_dependencies">functional dependencies</a>, the typeclass mechanism truly could obviate subtyping, with the added bonus that we could state any kind of type-level relation that we want, rather than just type order. I could talk about the downside of this, but&#8230; I could go on forever.</p>
<p><strong>Variance of Functors</strong></p>
<p>So, speaking of type order, there&#8217;s a tie-in here with variance. This is one of the things that trips people up when thinking about class hierarchies. I know it trips me up. But variance is much easier to reason about if we think of the subtype/supertype relation as just a function.</p>
<p>To compare, here&#8217;s what Wikipedia has to say about variance in class hierarchies:</p>
<blockquote><p>If class B is a subtype of class A, then all member functions of B must return the same or narrower set of types as A; the return type is said to be covariant. On the other hand, the member functions of B must take the same or broader set of arguments compared with the member functions of A; the argument type is said to be contravariant. The problem for instances of B is how to be perfectly substitutable for instances of A. The only way to guarantee type safety and substitutability is to be equally or more liberal than A on inputs, and to be equally or more strict than A on outputs.</p></blockquote>
<p>OK, but why is it the case that this is the only way to guarantee substitutability and type-safety? To understand that, it helps to throw away the notion of &#8220;subtype&#8221;, and simply think only in terms of functions, instead of the mixed notions of &#8220;subtype&#8221; and &#8220;member function&#8221;. Try translating that snippet from Wikipedia so that it&#8217;s worded in terms of functions. It gets pretty convoluted.</p>
<p>Proceeding from the premise that functions and subtype relations are interchangeable, we can derive a definition of co- and contravariance simply from the definition of substitutability. We start with this:</p>
<blockquote><p>If B is a subtype of A, then every subtype of B is also a subtype of A, and every supertype of A is also a supertype of B.</p></blockquote>
<p>Now, if we take (A &lt;: B) to mean &#8220;A is a subtype of B&#8221;, and (A =&gt; B) to mean &#8220;function from A to B&#8221;, or equivalently &#8220;A implies B&#8221;, we can write this property as:</p>
<blockquote><p>(A &lt;: B) &lt;==&gt; ((B &lt;: C) =&gt; (A &lt;: C)) and ((C &lt;: A) =&gt; (B &lt;: C))</p></blockquote>
<p>Remember that you can substitute functions for subtyping and vice versa, so any (=&gt;) sign above can be replaced with the (&lt;:) sign, or the other way around, and the meaning stays intact. So let&#8217;s restate this property purely in terms of functions: If there exists a function from B to A, then for every function from C to B there exists a function from C to A, and for every function from A to C there exists a function from B to C. (B =&gt; A) &lt;==&gt; ((C =&gt; B) =&gt; (C =&gt; A)) and ((A =&gt; C) =&gt; (B =&gt; C))</p>
<p>Moving (B =&gt; A) to the left of the &lt;==&gt; sign, we can infer two properties of (B =&gt; A):</p>
<blockquote><p>(B =&gt; A) =&gt; (C =&gt; B) =&gt; (C =&gt; A)<br />
and<br />
(B =&gt; A) =&gt; (A =&gt; C) =&gt; (B =&gt; C)</p></blockquote>
<p>Recalling that function application is logical implication, both of these properties evaluate to true, for all A, B, and C. Let&#8217;s use Wolfram Alpha to confirm this for us.<br />
<a href="http://www.wolframalpha.com/input/?i=%28B+%3D%3E+A%29+%3D%3E+%28%28C+%3D%3E+B%29+%3D%3E+%28C+%3D%3E+A%29%29">See here</a> <a href="http://www.wolframalpha.com/input/?i=%28B+%3D%3E+A%29+%3D%3E+%28%28A+%3D%3E+C%29+%3D%3E+%28B+%3D%3E+C%29%29">and here</a>.</p>
<p>Let&#8217;s now say that C is a fixed type. Remembering what we did above with typeclasses, this gives rise to two such typeclasses, representing (C =&gt; A) and (A =&gt; C), respectively, for all A:</p>
<pre>trait FromC[A] {
  def fromC(c: C): A
}</pre>
<pre>trait ToC[A] {
  def toC(a: A): C
}</pre>
<p>To restate the properties above:</p>
<blockquote><p>(B =&gt; A) =&gt; FromC[B] =&gt; FromC[A]<br />
(B =&gt; A) =&gt; ToC[A] =&gt; ToC[B]</p></blockquote>
<p>In other words, mapping a function (B =&gt; A) over FromC[B] results in FromC[A]. So the implication is preserved across the mapping. Mapping a function (B =&gt; A) over ToC[A] results in ToC[B]. So the implication is reversed across the mapping. This means that FromC is a covariant functor and ToC is a contravariant functor, by the definition of co- and contravariance. So now we have anchored the notion of substitutability to the variance of functors. For reference, here&#8217;s my off-the-cuff definition of co- and contravariance, along with some preliminaries:</p>
<p><strong>Definition of Higher-Order Function<br />
</strong>A higher-order function (HOF) is a function that takes another function as its argument.</p>
<p><strong>Definition of Functor<br />
</strong>For our purposes, a functor is a unary type constructor T[_]. There are some additional properties, but we&#8217;ll gloss over them for now.</p>
<p><strong>Definition of covariance<br />
</strong>A functor T[_] is covariant if and only if there exists a HOF with the following type, for all A and B:<br />
(A =&gt; B) =&gt; T[A] =&gt; T[B].</p>
<p><strong>Definition of contravariance<br />
</strong>A functor T[_] is contravariant if and only if there exists a HOF with the following type, for all A and B:<br />
(B =&gt; A) =&gt; T[A] =&gt; T[B].</p>
<p>Note that these last two are exactly the properties above that we got for FromC and ToC, which we derived directly from the definition of subtype and supertype.</p>
<p><strong>Conclusion</strong></p>
<p>Algebraic data types can be simulated with subtyping in a language that lacks them. Conversely, in a language that has algebraic data types and a mechanism for classifying types, subtyping is obviated. The benefits of subtyping, namely type substitution, can be expressed more naturally with a mechanism for classifying types and functional dependencies between them.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/203/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/203/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/203/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=203&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2009/08/27/hostility-toward-subtyping/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Structural Pattern Matching in Java</title>
		<link>http://apocalisp.wordpress.com/2009/08/21/structural-pattern-matching-in-java/</link>
		<comments>http://apocalisp.wordpress.com/2009/08/21/structural-pattern-matching-in-java/#comments</comments>
		<pubDate>Sat, 22 Aug 2009 01:51:31 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=185</guid>
		<description><![CDATA[One of the great features of modern programming languages is structural pattern matching on algebraic data types. Once you&#8217;ve used this feature, you don&#8217;t ever want to program without it. You will find this in languages like Haskell and Scala.
In Scala, algebraic types are provided by case classes. For example:


sealed trait Tree
case object Empty extends [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=185&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>One of the great features of modern programming languages is structural pattern matching on algebraic data types. Once you&#8217;ve used this feature, you don&#8217;t ever want to program without it. You will find this in languages like Haskell and Scala.</p>
<p>In Scala, algebraic types are provided by case classes. For example:</p>
<pre>
<pre class="brush: java;">
sealed trait Tree
case object Empty extends Tree
case class Leaf(n: Int) extends Tree
case class Node(l: Tree, r: Tree) extends Tree
</pre>
</pre>
<p>To define operations over this algebraic data type, we use pattern matching on its structure:</p>
<pre>
<pre class="brush: java;">
def depth(t: Tree): Int = t match {
  case Empty =&gt; 0
  case Leaf(n) =&gt; 1
  case Node(l, r) =&gt; 1 + max(depth(l), depth(r))
}
</pre>
</pre>
<p>When I go back to a programming language like, say, Java, I find myself wanting this feature. Unfortunately, algebraic data types aren&#8217;t provided in Java. However, a great many hacks have been invented over the years to emulate it, knowingly or not.</p>
<h3>The Ugly: Interpreter and Visitor</h3>
<p>What I have used most throughout my career to emulate pattern matching in languages that lack it are a couple of hoary old hacks. These venerable and well respected practises are a pair of design patterns from the GoF book: Interpreter and Visitor.</p>
<p>The Interpreter pattern really does describe an algebraic structure, and it provides a method of reducing (interpreting) the structure. However, there are a couple of problems with it. The interpretation is coupled to the structure, with a &#8220;context&#8221; passed from term to term, and each term must know how to mutate the context appropriately. That&#8217;s minus one point for tight coupling, and minus one for relying on mutation.</p>
<p>The Visitor pattern addresses the former of these concerns. Given an algebraic structure, we can define an interface with one &#8220;visit&#8221; method per type of term, and have each term accept a visitor object that implements this interface, passing it along to the subterms. This decouples the interpretation from the structure, but still relies on mutation. Minus one point for mutation, and minus one for the fact that Visitor is incredibly crufty. For example, to get the depth of our tree structure above, we have to implement a TreeDepthVisitor. A good IDE that generates boilerplate for you is definitely recommended if you take this approach.</p>
<p>On the plus side, both of these patterns provide some enforcement of the exhaustiveness of the pattern match. For example, if you add a new term type, the Interpreter pattern will enforce that you implement the interpretation method. For Visitor, as long as you remember to add a visitation method for the new term type to the visitor interface, you will be forced to update your implementations accordingly.</p>
<h3>The Bad: Instanceof</h3>
<p>An obvious approach that&#8217;s often sneered at is runtime type discovery. A quick and dirty way to match on types is to simply check for the type at runtime and cast:</p>
<pre>
<pre class="brush: java;">
public static int depth(Tree t) {
  if (t instanceof Empty)
    return 0;
  if (t instanceof Leaf)
    return 1;
  if (t instanceof Node)
    return 1 + max(depth(((Node) t).left), depth(((Node) t).right));
  throw new RuntimeException(&quot;Inexhaustive pattern match on Tree.&quot;);
}
</pre>
</pre>
<p>There are some obvious problems with this approach. For one thing, it bypasses the type system, so you lose any static guarantees that it&#8217;s correct. And there&#8217;s no enforcement of the exhaustiveness of the matching. But on the plus side, it&#8217;s both fast and terse.</p>
<h3>The Good: Functional Style</h3>
<p>There are at least two approaches that we can take to approximate pattern matching in Java more closely than the above methods. Both involve utilising parametric polymorphism and functional style. Let&#8217;s consider them in order of increasing preference, i.e. less preferred method first.</p>
<h4>Safe and Terse &#8211; Disjoint Union Types</h4>
<p>The first approach is based on the insight that algebraic data types represent a disjoint union of types. Now, if you&#8217;ve done any amount of programming in Java with generics, you will have come across (or invented) the simple pair type, which is a conjunction of two types:</p>
<pre>
<pre class="brush: java;">
public abstract class P2&lt;A, B&gt; {
  public A _1();
  public B _2();
}
</pre>
</pre>
<p>A value of this type can only be created if you have both a value of type A and a value of type B. So (conceptually, at least) it has a single constructor that takes two values. The disjunction of two types is a similar idea, except that a value of type <code>Either&lt;A, B&gt;</code> can be constructed with either a value of type A or a value of type B:</p>
<pre>
<pre class="brush: java;">
public final class Either&lt;A, B&gt; {
  ...
  public static &lt;A, B&gt; Either&lt;A, B&gt; left(A a) { ... }
  public static &lt;A, B&gt; Either&lt;A, B&gt; right(B a) { ... }
  ...
}
</pre>
</pre>
<p>Encoded as a disjoint union type, then, our Tree data type above is: <code>Either&lt;Empty, Either&lt;Leaf, Node&gt;&gt;</code></p>
<p>Let&#8217;s see that in context. Here&#8217;s the code.</p>
<pre>
<pre class="brush: java;">
public abstract class Tree {
  // Constructor private so the type is sealed.
  private Tree() {}

  public abstract Either&lt;Empty, Either&lt;Leaf, Node&gt;&gt; toEither();

  public static final class Empty extends Tree {
    public &lt;T&gt; T toEither() {
      return left(this);
    }

    public Empty() {}
  }

  public static final class Leaf extends Tree {
    public final int n;

    public Either&lt;Empty, Either&lt;Leaf, Node&gt;&gt; toEither() {
      return right(Either.&lt;Leaf, Node&gt;left(this));
    }

    public Leaf(int n) { this.n = n; }
  }

  public static final class Node extends Tree {
    public final Tree left;
    public final Tree right;    

    public Either&lt;Empty, Either&lt;Leaf, Node&gt;&gt; toEither() {
      return right(Either.&lt;Leaf, Node&gt;right(this));
    }

    public Node(Tree left, Tree right) {
      this.left = left; this.right = right;
    }
  }
}
</pre>
</pre>
<p>The neat thing is that <code>Either&lt;A, B&gt;</code> can be made to return both <code>Iterable&lt;A&gt;</code> and <code>Iterable&lt;B&gt;</code> in methods right() and left(), respectively. One of them will be empty and the other will have exactly one element. So our pattern matching function will look like this:</p>
<pre>
<pre class="brush: java;">
public int depth(Tree t) {
  Either&lt;Empty, Either&lt;Leaf, Node&gt;&gt; eln = t.toEither();
  for (Empty e: eln.left())
    return 0;
  for (Either&lt;Leaf, Node&gt; ln: t.toEither().right()) {
    for (leaf: ln.left())
      return 1;
    for (node: ln.right())
      return 1 + max(depth(node.left), depth(node.right));
  }
  throw new RuntimeException(&quot;Inexhaustive pattern match on Tree.&quot;);
}
</pre>
</pre>
<p>That&#8217;s terse and readable, as well as type-safe. The only issue with this is that the exhaustiveness of the patterns is not enforced, so we&#8217;re still only discovering that error at runtime. So it&#8217;s not all that much of an improvement over the instanceof approach.</p>
<h4>Safe and Exhaustive: Church Encoding</h4>
<p>Alonzo Church was a pretty cool guy. Having invented the lambda calculus, he discovered that you could encode data in it. We&#8217;ve all heard that every data type can be defined in terms of the kinds of operations that it supports. Well, what Church discovered is much more profound than that. A data type IS a function. In other words, an algebraic data type is not just a structure together with an algebra that collapses the structure. The algebra IS the structure.</p>
<p>Consider the boolean type. It is a disjoint union of True and False. What kinds of operations does this support? Well, you might want to do one thing if it&#8217;s True, and another if it&#8217;s False. Just like with our Tree, where we wanted to do one thing if it&#8217;s a Leaf, and another thing if it&#8217;s a Node, etc.</p>
<p>But it turns out that the boolean type IS the condition function. Consider the Church encoding of booleans:</p>
<div style="font-size:larger;">
<pre>
true  = λa.λb.a
false = λa.λb.b
</pre>
</div>
<p>So a boolean is actually a binary function. Given two terms, a boolean will yield the former term if it&#8217;s true, and the latter term if it&#8217;s false. What does this mean for our Tree type? It too is a function:</p>
<div style="font-size:larger;">
<pre>
empty = λa.λb.λc.a
leaf  = λa.λb.λc.λx.b x
node  = λa.λb.λc.λl.λr.c l r
</pre>
</div>
<p>You can see that this gives you pattern matching for free. The Tree type is a function that takes three arguments:</p>
<ol>
<li>A value to yield if the tree is empty.</li>
<li>A unary function to apply to an integer if it&#8217;s a leaf.</li>
<li>A binary function to apply to the left and right subtrees if it&#8217;s a node.</li>
</ol>
<p>The type of such a function looks like this (Scala notation):</p>
<p><code>T =&gt; (Int =&gt; T) =&gt; (Tree =&gt; Tree =&gt; T) =&gt; T</code></p>
<p>Or equivalently:</p>
<p><code>(Empty =&gt; T) =&gt; (Leaf =&gt; T) =&gt; (Node =&gt; T) =&gt; T</code></p>
<p>Translated to Java, we need this method on Tree:</p>
<pre>
<pre class="brush: java;">
public abstract &lt;T&gt; T match(F&lt;Empty, T&gt; a, F&lt;Leaf, T&gt; b, F&lt;Node, T&gt; c);
</pre>
</pre>
<p>The F interface is a first-class function from Functional Java. If you haven&#8217;t seen that before, here it is:</p>
<pre>
<pre class="brush: java;">
public interface F&lt;A, B&gt; { public B f(A a); }
</pre>
</pre>
<p>Now our Tree code looks like this:</p>
<pre>
<pre class="brush: java;">
public abstract class Tree {
  // Constructor private so the type is sealed.
  private Tree() {}

  public abstract &lt;T&gt; T match(F&lt;Empty, T&gt; a, F&lt;Leaf, T&gt; b, F&lt;Node, T&gt; c);

  public static final class Empty extends Tree {
    public &lt;T&gt; T match(F&lt;Empty, T&gt; a, F&lt;Leaf, T&gt; b, F&lt;Node, T&gt; c) {
      return a.f(this);
    }

    public Empty() {}
  }

  public static final class Leaf extends Tree {
    public final int n;

    public &lt;T&gt; T match(F&lt;Empty, T&gt; a, F&lt;Leaf, T&gt; b, F&lt;Node, T&gt; c) {
      return b.f(this);
    }

    public Leaf(int n) { this.n = n; }
  }

  public static final class Node extends Tree {
    public final Tree left;
    public final Tree right;    

    public &lt;T&gt; T match(F&lt;Empty, T&gt; a, F&lt;Leaf, T&gt; b, F&lt;Node, T&gt; c) {
      return c.f(this);
    }

    public Node(Tree left, Tree right) {
      this.left = left; this.right = right;
    }
  }
}
</pre>
</pre>
<p>And we can do our pattern matching on the calling side:</p>
<pre>
<pre class="brush: java;">
public int depth(Tree t) {
  return t.match(constant(0), constant(1), new F&lt;Node, Integer&gt;(){
    public Integer f(Node n) {
      return 1 + max(depth(n.left), depth(n.right));
    }
  });
}
</pre>
</pre>
<p>This is almost as terse as the Scala code, and very easy to understand. Everything is checked by the type system, and we are guaranteed that our patterns are exhaustive. This is an ideal solution.</p>
<p>By the way, if you&#8217;re wondering what constant(0) means, it&#8217;s a method that returns a function <code>F&lt;A, Integer&gt;</code> that always returns 0, ignoring the argument.</p>
<h3>Conclusion</h3>
<p>With some slightly clever use of generics and a little help from our friends Church and Curry, we can indeed emulate structural pattern matching over algebraic data types in Java, to the point where it&#8217;s almost as nice as a built-in language feature.</p>
<p>So throw away your Visitors and set fire to your GoF book.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/185/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/185/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/185/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=185&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2009/08/21/structural-pattern-matching-in-java/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>A Critique of Impure Reason</title>
		<link>http://apocalisp.wordpress.com/2009/04/27/a-critique-of-impure-reason/</link>
		<comments>http://apocalisp.wordpress.com/2009/04/27/a-critique-of-impure-reason/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 03:01:56 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Epistemology]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[metaphysics]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[purity]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=155</guid>
		<description><![CDATA[Much has been made of the difference between &#8220;object-oriented&#8221; and &#8220;functional&#8221; programming. As much as I&#8217;ve seen people talk about this difference, or some kind of imperative/functional dichotomy, I don&#8217;t think it&#8217;s a meaningful distinction. I find that the question of OO vs FP, or imperative vs functional, is not interesting or relevant. The relevant [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=155&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Much has been made of the difference between &#8220;object-oriented&#8221; and &#8220;functional&#8221; programming. As much as I&#8217;ve seen people talk about this difference, or some kind of imperative/functional dichotomy, I don&#8217;t think it&#8217;s a meaningful distinction. I find that the question of OO vs FP, or imperative vs functional, is not interesting or relevant. The relevant question is one of purity, or the distinction between pure and impure programming. That is, respectively, the kind of programming that eschews data mutation and side-effects as against the kind that embraces them.</p>
<p>Looking at pure (referentially transparent, purely functional) programming from a perspective of having accepted the premise of impurity, it&#8217;s easy to be intimidated, and to see it as something foreign. Dijkstra once said that some programmers are &#8220;mentally mutilated beyond hope of regeneration&#8221;. I think that on one account, he was right, and that he was talking about this mental barrier of making the leap, from thinking about programming in terms of data mutation and effects, to the &#8220;pure&#8221; perspective. But I believe he was wrong on the other account. There is hope.</p>
<p>Contrary to what the last post indicated, this will not be a post about problems in &#8220;object-oriented&#8221; programming in particular, but about problems in impure programming in general. I will illustrate the issue of data mutation by way of two difficulties with it:</p>
<ol>
<li>It blurs the distinction between values and variables, such that a term can be both a value and a variable.</li>
<li>It introduces an artificial distinction between equality and identity.</li>
</ol>
<p>I will talk briefly about what the implications of each are, and what the alternative view is. But first I will discuss what I think motivates the impure perspective: an aversion to abstraction.<strong></strong></p>
<h3><strong>The Primacy of the Machine</strong></h3>
<p>In the early days of programming, there were no computers. The first programs were written, and executed, on paper. It wasn&#8217;t until later that machines were first built that could execute programs automatically.</p>
<p>During the ascent of computers, an industry of professional computer programmers emerged. Perhaps because early computers were awkward and difficult to use, the focus of these professionals became less thinking about programs and more <em>manipulating the machine</em>.</p>
<p>Indeed, if you read the <a href="http://en.wikipedia.org/wiki/Computer_program" target="_blank">Wikipedia entry on &#8220;Computer Program&#8221;</a>, it tells you that computer programs are &#8220;instructions for a computer&#8221;, and that &#8220;a computer requires programs to function&#8221;. This is a curious position, since it&#8217;s completely backwards. It implies that programming is done in order to make computers do things, as a primary. I&#8217;ll warrant that the article was probably written by a professional programmer.</p>
<p>But why does a computer need to function? Why does a computer even exist? The reality is that computers exist solely for the purpose of executing programs. The machine is not a metaphysical primary. Reality has primacy, a program is a description, an abstraction, a proof of some hypothesis about an aspect of reality, and the computer exists to deduce the implications of that fact for the pursuit of human values.</p>
<h3>Shaping Programs in the Machine&#8217;s Image</h3>
<p>There&#8217;s a certain kind of programming that lends itself well to manipulating a computer at a low level. You must think in terms of copying data between registers and memory, and executing instructions based on the machine&#8217;s current state. In this kind of activity, the programmer serves as the interpreter between the abstract algorithm and the physical machine. But this in itself is a mechanical task. We can instead write a program, once and for all, that performs the interpretation for us, and direct that program using a general-purpose programming language.</p>
<p>But what kind of structure will that language have? Ideally, we would be able to express programs in a natural and concise notation without regard to the machine. Such was the motivation behind LISP. It was designed as an implementation of the lambda calculus, a minimal formal system for expressing algorithms.</p>
<p>This is not what has happened with languages like Java, C++, and some other of today&#8217;s most popular languages. The abstractions in those languages largely simulate the machine. You have mutable records, or objects, into which you copy data and execute instructions based on their current state. The identity of objects is based on their location in the computer&#8217;s memory. You have constructs like &#8220;factories&#8221;, &#8220;builders&#8221;, &#8220;generators&#8221;&#8230; machines.</p>
<p>What we have is a generation of programmers accustomed to writing programs as if they were constructing a machine. We hear about &#8220;engineering&#8221; and &#8220;architecture&#8221; when talking about software. Indeed, as users, we interact with software using pictures of buttons, knobs, and switches. More machinery.</p>
<h3>Equality and Identity</h3>
<p>Programmers are generally used to thinking of equality and identity as distinct. For example, in Java, <code>(x == y)</code> means that the variable <code>x</code> holds a pointer to the same memory address as the variable <code>y</code>, i.e. that the variable <code>x</code> is identical with <code>y</code>. However, by convention, <code>x.equals(y)</code> means that the object at the memory address pointed to by <code>x</code> is equivalent in some sense to the object at the memory address pointed to by <code>y</code>, but <code>x</code> and <code>y</code> are not necessarily identical.</p>
<p>Note that the difference between the two can only be explained in terms of the underlying machine. We have to invoke the notion of memory addresses to understand what&#8217;s going on, or else resort to an appeal to intuition with terms like &#8220;same object&#8221;, &#8220;same instance&#8221;, etc.</p>
<p>In a pure program, side-effects are disallowed. So a function is not allowed to mutate arguments or make calls to functions that have side-effects. So the result of every function is purely determined by the value of its arguments, and a function does nothing except compute a result. Think about the implications of that for a moment. Given that constraint, is there any reason to maintain a distinction between equality and identity? Consider this example:</p>
<pre>
<pre class="brush: java;">

String x = new String(&quot;&quot;);
String y = new String(&quot;&quot;);
</pre>
</pre>
<p>If the values referenced by<code> x</code> and <code>y</code> are forever immutable, what could it possibly mean for them to not be identical? In what meaningful sense are they not the &#8220;same object&#8221;?</p>
<p>Note that there&#8217;s a philosophical connection here. The notion that every object carries an <em>extradimensional</em> identity that is orthogonal to its attributes is a very Platonic idea. The idea that an object has, in addition to its measurable attributes, a distinguished, eternal, <a href="http://apocalisp.wordpress.com/2008/05/03/null-vs-pure-reason/"><em>noumenal </em></a>identity. Contrast this with the Aristotelian view, in which an object&#8217;s identity is precisely the sum of its attributes.</p>
<h3>Variables and Pseudovariables</h3>
<p>So we find that, given the mutability premise, a term in the language may be not be equal to itself even as it remains identical with itself. In a sense, this is because it&#8217;s not entirely clear whether we are comparing values or variables when we compare a term. If that sounds a bit strange, consider the following snippet:</p>
<pre>
<pre class="brush: java;">
public class Foo {
  public int v;
  public Foo(int i) {
    v = i;
  }
}
...
public Foo f(Foo x) {
  x.v = x.v + 1;
  return x;
}
...
Foo x = new Foo(0);
boolean a = f(x) == f(x); // true
boolean b = f(x).v &gt; f(x).v; // ???
</pre>
</pre>
<p>Even that short and ostensibly simple code snippet is difficult to comprehend properly, because the f method mutates its argument. It seems that since the first boolean is true, the same object appears on either side of the <code>== </code>operator, so<code> f(x)</code> is identical with<code> f(x)</code>. They&#8217;re the &#8220;same object&#8221;. Even so, it is not clear that this &#8220;same object&#8221; is equal to itself. To figure out the value of b, you have to know some things about <em>how</em> the machine executes the program, not just <em>what </em>the program actually says. You can argue about pass-by-reference and pass-by-value, but these are terms that describe the machine, not the program.</p>
<p>You will note that the <code>Foo </code>type above is mutable in the sense that it has accessible components that can be assigned new values. But what does that mean? Is an object of type <code>Foo </code>a value or a variable? It seems to <em>have </em>a kind of pseudo-variable <code>v</code>. To clarify this, we can imagine that a variable holding a value of type <code>Foo </code>also holds a further value of type <code>int</code>. Assignment to a variable <code>x</code> of type <code>Foo </code>also assigns the variable <code>x.v</code> of type <code>int</code>, but the variable<code> x.v</code> can be assigned independently of <code>x</code>.</p>
<p>So data mutation is, ultimately, variable assignment. Above, <code>Foo </code>is mutated by assigning a value to the pseudo-variable <code>v</code>.</p>
<p>Now, I say pseudo-variable, because it&#8217;s not obvious whether this is a value or a variable. A function that receives a value of type <code>Foo</code>, also receives the variable <code>Foo.v</code>, to which it can assign new values. So the variable is passed as if it were a value. Stated in terms of the machine, a pointer to a pointer is being passed, and the memory at that address can be mutated by writing another pointer to it. In terms of the program, the called function is able to bind a value to a variable in the calling scope.</p>
<p>This is why you have bugs, why programs are hard to debug, and software is difficult to extend. It&#8217;s because programs are difficult to comprehend to the extent that they are impure. And the reason for that is that they are not sufficiently abstract. A writer of impure code is not operating on the level of abstraction of programming. He&#8217;s operating at the level of abstraction of the machine.</p>
<h3>Thinking Pure Thoughts</h3>
<p>In a pure program, there are no side-effects at all. Every function is just that, a function from its arguments to its result. Values are immutable and eternal. <strong>A</strong> is <strong>A</strong>, and will always remain <strong>A</strong>. The only way to get from <strong>A</strong> to <strong>B</strong> is for there to be a function that takes <strong>A</strong> and returns <strong>B</strong>.</p>
<p>To a lot of programmers used to thinking in terms of mutable state, this perspective is completely baffling. And yet, most of them use immutable values all the time, without finding them particularly profound. For example:</p>
<pre>
<pre class="brush: java;">
int x = 1;
int y = x;
x = x + 1;
</pre>
</pre>
<p>In that example, <code>1</code> is an immutable value. The <code>+</code> operator doesn&#8217;t modify the value of <code>1</code>, nor of <code>x</code>, nor of <code>y</code>. It doesn&#8217;t take the value <code>1</code> and turn it into a <code>2</code>. Every integer is equal to and identical with itself, always and forever.</p>
<p>But what&#8217;s special about integers? When adding a value to a list, a Java programmer will ordinarily do something like this:</p>
<pre>
<pre class="brush: java;">list.add(one);</pre>
</pre>
<p>This modifies the list in place, so that any variable referencing it will change values. But why shouldn&#8217;t lists be as immutable as integers? I&#8217;m sure some readers just mumbled something about primitives and passing by reference versus passing by value. But why the distinction? If everything were immutable, you would never know if it were passed by value or reference under the covers. Nor would you care what&#8217;s primitive and what isn&#8217;t. For an immutable list, for example, the add function would not be able to modify the list in place, and so would return a new one:</p>
<pre>
<pre class="brush: java;">list = list.add(one);</pre>
</pre>
<p>From this perspective, you can write programs that perform arithmetic with complex values as if they were primitive. But why stop with lists? When functions are pure, we can understand programs themselves as objects that are subject to calculation just like numbers in arithmetic, using simple and composable operators.</p>
<h3>Assignment in a Pure World</h3>
<p>Let&#8217;s take a simple example of just such an operator. We&#8217;ve talked about how mutation is really assignment. Now let&#8217;s take the leap fully into the pure perspective, in which assignment is really just closure (pure functions with free variables). To illustrate, here is an example that all Java and C programmers are familiar with. Let&#8217;s state the previous example again:</p>
<pre>
<pre class="brush: java;">
int x = 1;
int y = x;
x = x + 1;
</pre>
</pre>
<p>The familiar semicolon represents a combinator called bind. In Haskell, this operator is denoted <code>(&gt;&gt;=)</code>. In Scala, it&#8217;s called <code>flatMap</code>. To better see how this works, here&#8217;s a rough transliteration into Haskell:</p>
<pre>
<pre class="brush: java;">
\t -&gt; (return 1) &gt;&gt;= \x -&gt; (return x) &gt;&gt;= \y -&gt; (return (x + 1)) &gt;&gt;= \x -&gt; t x
</pre>
</pre>
<p>Of course, this isn&#8217;t at all the kind of code you would normally write, but it serves to demonstrate the idea that assignment can be understood as binding to the free variable of a closure. Haskell is a purely functional language, and purity is enforced by its type system. If you haven&#8217;t taken it for a spin, I challenge you to do so. For more on the benefits of purely functional programming, see <a href="http://haskell.org/haskellwiki/Why_Haskell_matters#Purity" target="_blank">&#8220;Why Haskell Matters&#8221;</a>.</p>
<p>Of course, purity is not purely the prerogative of Haskell. Sure, Haskell enforces purity, but you can write pure code in any language. That Java code above is pure, you know. If this is all new to you, I challenge you to give purity a shot. In your favourite language, be it Java or something else, start using immutable datastructure in preference to mutable ones. Try thinking in terms of functions from arguments to results rather than in terms of mutation and effects. For Java, you might consider using the immutable datastructures provided by <a href="http://code.google.com/p/functionaljava">Functional Java</a> or Google Collections. Scala also has immutable datastructures as part of its standard libraries.</p>
<p>It&#8217;s time that we graduate from punching buttons and pulling levers — manipulating the machines. Here&#8217;s to programming as it could be and ought to be.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=155&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2009/04/27/a-critique-of-impure-reason/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Objects, Identity, and Concept-Formation</title>
		<link>http://apocalisp.wordpress.com/2008/12/04/no-such-thing/</link>
		<comments>http://apocalisp.wordpress.com/2008/12/04/no-such-thing/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 20:18:58 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Epistemology]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[foundational]]></category>
		<category><![CDATA[object-oriented]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=126</guid>
		<description><![CDATA[Coming from a background in Pascal and C, during the 1990s, like most others, I became infatuated with Object-Oriented programming. I thought they were really on to something. It seemed intuitive. I read and re-read the GoF book. I became fluent in UML. I read Chris Alexander. I wrote factories and metafactories, and more class [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=126&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Coming from a background in Pascal and C, during the 1990s, like most others, I became infatuated with Object-Oriented programming. I thought they were really on to something. It seemed intuitive. I read and re-read the <a title="Design Patterns" href="http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612" target="_blank">GoF book</a>. I became fluent in UML. I read Chris Alexander. I wrote factories and metafactories, and more class hierarchies than I&#8217;ll ever count. I embraced Java eagerly, and XML moreso. But with experience came the realisation that I was solving the same problems over and over again. There was <a href="http://en.wikipedia.org/wiki/Combinatory_logic" target="_blank">some governing principle</a> that I was missing but knew I had to grasp if I were to progress in my chosen profession. I went back to the fundamentals. I learned about LISP, lamdba calculi, type theory, relational programming, <a href="http://en.wikipedia.org/wiki/Category_theory" target="_blank">category theory</a>, and <a href="http://en.wikipedia.org/wiki/Introduction_to_Objectivist_Epistemology" target="_blank">epistemology</a>. And I&#8217;m still learning. But in integrating all of that into my existing knowledge, I discovered something that I found liberating:</p>
<p><em>There is no such thing as Object-Oriented programming.</em></p>
<p>I realise that I might risk starting a religious war here, but I don&#8217;t intend to. If you think I&#8217;m attacking you personally in some way, please stop reading and come back later with fresh eyes. Note that <strong>I&#8217;m not saying that OO is a bad thing</strong>. I&#8217;m saying that there isn&#8217;t any special kind of programming that is object-oriented as against programming that isn&#8217;t. It is a useless distinction, in exactly the same way that <a href="http://freedomkeys.com/ar-racism.htm" target="_blank">&#8220;race&#8221;</a> is a useless distinction. And holding on to a useless concept isn&#8217;t good for anybody. The term &#8220;object-oriented&#8221; is at least honest in that it says what it implies, which is a frame of mind, an orientation of the programmer. However, the practical effect of this orientation is anti-conceptual, in that it displaces actual concepts like algebra, calculus, closure, function, identity, type, value, variable, etc. It fosters an aversion to abstraction. So you could say that there are object-orientated programmers.</p>
<h3><strong>&#8220;Object-Oriented&#8221; as a non-concept</strong></h3>
<p>Remember, you cannot be called upon to prove a negative. The burden of proof falls on whomever is claiming that there is such a thing as OO. But try as you might, <a href="http://www.paulgraham.com/reesoo.html" target="_blank">there&#8217;s no objective definition of what &#8220;object-oriented&#8221; refers to</a>. It means anything you want, which is to say that it&#8217;s rather meaningless. Peddlers of &#8220;object methodology&#8221; readily admit that &#8220;object-oriented&#8221; means different things to different people. To quote Aristotle: &#8220;Not   to have one meaning is to have no meaning, and if   words have no meaning, our reasoning with one another, and indeed   with ourselves, has been annihilated.&#8221;</p>
<p>When I say that there&#8217;s no such thing as OO, I mean, more precisely, that there exists some abstraction (or several) that is referred to as &#8220;object-oriented&#8221;, but that this abstraction has no actual referent in reality. It is an abstraction that is made in error. It is not necessary, and serves no cognitive purpose. In the words of <a href="http://blog.tmorris.net/" target="_blank">Tony Morris</a>, it is &#8220;not even false&#8221;.</p>
<p>This is to say that it&#8217;s not like the concept of Santa Claus, which is a fiction, a falsehood. OO is not a fiction, but a misintegration. It is a term that sounds like a concept, but denotes nothing more than a vague collection of disparate elements, any of which can be omitted, and none of which are essential.</p>
<h3><strong>A Proper Method of Concept-Formation</strong></h3>
<p>How is Object-Oriented a non-concept? Let&#8217;s demonstrate that very cursorily. First, we have to explicitly identify what it means for a concept to be valid.</p>
<p><em>Valid concepts are arrived at by induction. </em>Induction is the process of integrating facts of reality by observing similarities and omitting particulars. Or, if you will, the process of applying logic to experience. Our induction must adhere to the corollary laws of <a href="http://en.wikipedia.org/wiki/Law_of_identity" target="_blank">identity</a>, <a href="http://en.wikipedia.org/wiki/Law_of_noncontradiction" target="_blank">noncontradiction</a>, and <a href="http://en.wikipedia.org/wiki/Law_of_excluded_middle" target="_blank">the excluded middle</a>, and it must observe <a href="http://aynrandlexicon.com/lexicon/causality.html" target="_blank">causality</a>.</p>
<p>Note that a correct concept of concepts is the vehicle and not the cargo of this post. In brief, a valid concept or theory must meet the following criteria (hat tip: <a href="http://www.theobjectivestandard.com/issues/2007-spring/induction-experimental-method.asp" target="_blank">David Harriman</a>):</p>
<ol>
<li>It must be derived from observations by a valid method (by logical induction from experience or experiment as described above). A valid concept can have no arbitrary elements and no relations that are unsupported by the facts being integrated.</li>
<li>It must form an integrated whole in which every part supports every other. It cannot be a conglomeration of independent parts that are freely adjusted to fit a given situation.</li>
<li>It must be no more and no less general than required to meet criteria 1 and 2.</li>
</ol>
<p><strong> OO doesn&#8217;t meet criterion #1</strong>. There&#8217;s nothing about the nature of programming that gives rise to a concept of object-orientation. Rather, the notion is sparked by the desire to make an analogy between manipulating physical objects in the real world and manipulating abstract entities in the computer. The motivation is understandable, to help people think about programs in the same way that they are used to reasoning about physical objects. But the analogy isn&#8217;t warranted by the facts, and therefore it is arbitrary. The concepts simply don&#8217;t transfer, because they are abstractions over entities of a different kind. The mistake is to use the concepts anyway, choosing the illusion that software is like physical systems rather than understanding the actual nature of programs.</p>
<p><strong>OO doesn&#8217;t meet criterion #2</strong>: There&#8217;s no consistent definition of object-orientation. It&#8217;s a loose grab-bag in which nothing is essential. Even if you start removing things, it&#8217;s not at all clear at what point your programming stops being object-oriented. One would think that it&#8217;s at the point where you stop programming with objects, but that&#8217;s merely begging the question since it&#8217;s not clear what kind of entity is an object and what kind of entity isn&#8217;t. We are to rely on intuition alone in discovering that. What&#8217;s more, some of the contents of the &#8220;object-oriented&#8221; bag contradict one another (both inheritance and mutation contradict encapsulation, for example).</p>
<h3><strong>Repairing the Disorientation</strong></h3>
<p>Of course, it does no good to tear down the cognitive package-deal that is &#8220;object-oriented&#8221; if you don&#8217;t replace it with something. If you entered a village of <a href="http://www.smurf.com/" target="_blank">little blue creatures</a> and convinced them that the term &#8220;smurf&#8221; had no cognitive utility, you would have to teach them a whole host of concepts to use in its stead. There are two main ideas that the anti-concept &#8220;object-oriented&#8221; annihilates:</p>
<ol>
<li>It introduces the false dichotomy of identity as apart from equivalence.</li>
<li>It blurs the distinction between values and variables.</li>
</ol>
<p>I will elaborate on each of these points in two follow-up posts. Stay tuned.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=126&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2008/12/04/no-such-thing/feed/</wfw:commentRss>
		<slash:comments>72</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Heterogeneous Lists and the Limits of the Java Type System</title>
		<link>http://apocalisp.wordpress.com/2008/10/23/heterogeneous-lists-and-the-limits-of-the-java-type-system/</link>
		<comments>http://apocalisp.wordpress.com/2008/10/23/heterogeneous-lists-and-the-limits-of-the-java-type-system/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 09:05:04 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[hlist]]></category>
		<category><![CDATA[java abuse]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=108</guid>
		<description><![CDATA[Today, we&#8217;re going on a journey. It is a sojourn to the outer limits of the expressiveness of the Java type system, and to the edge of what can be considered sane programming. This is definitely one for the power users. You will need a firm grasp of the Java language, and an iron constitution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=108&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today, we&#8217;re going on a journey. It is a sojourn to the outer limits of the expressiveness of the Java type system, and to the edge of what can be considered sane programming. This is definitely one for the power users. You will need a firm grasp of the Java language, and an iron constitution for type annotations. But the reward will be something far greater than any treasure: understanding, entertainment, and perhaps even enlightenment. Remember that we choose to do these things in Java, not because they are easy, but because they are hard. Now then, to the ships.</p>
<p><strong>A Most Versatile Vessel</strong></p>
<p><strong><span style="font-weight:normal;">In Java, we can create a list that contains values of type <em><code>A</code></em>, by constructing a value of type <em><code>List&lt;A&gt;</code></em>. The type system will enforce that each element in the list is in fact of type <em><code>A</code></em>. But sometimes we want lists of values that aren&#8217;t necessarily of the same type. Normally, for such a purpose, we would use a heterogeneous list, which in Java is just the raw list type <em><code>List&lt;?&gt;</code></em> or <em><code>List&lt;Object&gt;</code></em>. Since every class in Java is a subclass of Object (and now that we have autoboxing), such a list can contain any Java value. There are many kinds of situation where this would be necessary. For example, a row of database results will comprise values that are not all of the same type.</span></strong></p>
<p><strong><span style="font-weight:normal;">However, there&#8217;s a problem with the raw list approach. In using the <em><code>List&lt;?&gt;</code></em> type, we are dispensing with the type system. When you get a value from the list, how do you know what it is? How do you know which operations it supports? Well, you will have to defer that discovery until runtime, and use explicit type casting. Most will shrug at this and say: &#8220;So what?&#8221; After all, this is what we did anyway, before generics. Ah, but what if we don&#8217;t have to? Can we create generic heterogeneous collections that are type-safe? Yes, we can. Sort of.</span></strong></p>
<p><strong>Products of Types</strong></p>
<p>What we would like to see is if it&#8217;s possible to declare some constraints on the types of a heterogeneous collection, to achieve essential type-safety while maintaining the extensibility of a list. Of course, it&#8217;s easy to create types that are the product of two or more types:</p>
<pre class="brush: java;">
public abstract class P2&lt;A, B&gt; {
  public abstract A _1();
  public abstract B _2();
}
</pre>
<p>But the length of this kind of product is as fixed as the length of a string in Pascal. It isn&#8217;t extensible, so it&#8217;s more like a type-safe heterogeneous array than a list. If you want products of different lengths, you will need to declare separate classes for <em><code>P3&lt;A, B, C&gt;</code></em>, <em><code>P4&lt;A, B, C, D&gt;</code></em>, etc. What we&#8217;re trying to achieve is a product of arbitrary length, whose length might even vary at runtime. There&#8217;s no reason we couldn&#8217;t create products of products in a chain, like <em><code>P2&lt;A, P2&lt;B, P2&lt;C, D&gt;&gt;&gt;</code></em>, and this is more or less the approach that we will take.</p>
<p><strong>Introducing HList</strong></p>
<p><strong><span style="font-weight:normal;">To achieve our goal, we&#8217;re going to implement linked lists in the type system. Let&#8217;s remind ourselves what a linked list looks like. A <em><code>List&lt;T&gt;</code></em> is essentially either the empty list or a value of type <em><code>T</code></em> paired with a <em><code>List&lt;T&gt;</code></em>. In Java, using the <em><code>List&lt;A&gt;</code></em> type from Functional Java, an unsafe heterogeneous list might be constructed in a manner like the following:</span></strong></p>
<pre class="brush: java;">
List&lt;?&gt; x = cons(&quot;One&quot;, cons(2, cons(false, nil()));
</pre>
<p>The <em><code>cons </code></em>method constructs a list, and the <em><code>nil </code></em>method returns the empty list. With just these two methods, we can create any homogeneous list. A list has two methods to access its members, <em><code>head()</code></em> which returns the first element, and <em><code>tail() </code></em>which returns the rest of the list. Getting the head or tail of the empty list is an error at runtime.</p>
<p>Let&#8217;s now take a step up into the type system, and say that a list of types is either the empty list or a type paired with a list of types. This gives rise to our heterogeneous list type:</p>
<pre class="brush: java;">
public abstract class HList&lt;A extends HList&lt;A&gt;&gt; {
 private HList() {}

 private static final HNil nil = new HNil();

 public static HNil nil() {
   return nil;
 }

 public static &lt;E, L extends HList&lt;L&gt;&gt; HCons&lt;E, L&gt; cons(final E e, final L l) {
   return new HCons&lt;E, L&gt;(e, l);
 }

 public static final class HNil extends HList&lt;HNil&gt; {
   private HNil() {}
 }

 public static final class HCons&lt;E, L extends HList&lt;L&gt;&gt; extends HList&lt;HCons&lt;E, L&gt;&gt; {
   private E e;
   private L l;

   private HCons(final E e, final L l) {
     this.e = e;
     this.l = l;
   }

   public E head() {
     return e;
   }

   public L tail() {
     return l;
   }
 }
}
</pre>
<p>That&#8217;s not a lot of code, and it&#8217;s all relatively straightforward Java. The <em><code>HList </code></em>class is parameterised with a parameterised subclass of itself. There are only two concrete subclasses of <em><code>HList </code></em>that can possibly occupy that slot: the type <em><code>HNil </code></em>and the type constructor <em><code>HCons</code></em>. These represent the empty list and the list constructor, respectively. <em><code>HCons </code></em>takes two type parameters, the first representing the first element of the list, and the second being another <em><code>HList</code></em>, allowing us to form a chain of them. <em><code>HNil </code></em>does not take type parameters, so it terminates the chain.</p>
<p>As with regular old lists, you can access the <em><code>head() </code></em>and <em><code>tail() </code></em>of the list. Note, however, that the fact that you cannot get the head or tail of the empty list is now enforced by the type system. There&#8217;s a <em><code>nil </code></em>method to get the empty list, and a <em><code>cons </code></em>method to construct a nonempty list, just like with regular lists.</p>
<p>Here&#8217;s an example of how we would construct a heterogeneous list using this new type:</p>
<pre class="brush: java;">
HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;&gt; x = cons(&quot;One&quot;, cons(2, cons(false, nil()));
</pre>
<p>This is more verbose than the unsafe version before, but not by much. Obviously, the <em><code>HList </code></em>example assumes a static import of <em><code>HList.cons</code></em> and the <em><code>List&lt;?&gt;</code></em> example assumes a static import of <em><code>List.cons</code></em>. Using the type-safe version is, however, much nicer. Compare these two contrived examples:</p>
<pre class="brush: java;">
if (x.tail().tail().head()) {
  return x.head().length() == x.tail().head();
}

if ((boolean) x.index(3)) {
  return ((String) x.head()).length() == (int) x.index(2);
}
</pre>
<p>The latter, of course, offers no static guarantees and may throw <em><code>ClassCastExceptions</code></em>, or we might inadvertently get the head or tail of the empty list at runtime. The former will always work as long as it compiles, guaranteed.</p>
<p><strong>Concatenating HLists</strong></p>
<p><strong><span style="font-weight:normal;">Now let&#8217;s do something more interesting with these lists. Notice that the cons methods for both type-safe and unsafe lists prepend an element to a list rather than appending. Sometimes we want to append a list to the end of another. This is unsurprisingly uncomplicated for unsafe lists:</span></strong></p>
<pre class="brush: java;">
List&lt;?&gt; c = a.append(b);
</pre>
<p>Behind the scenes, we can think of append as reversing the first list and consing each element to the second list in reverse order. Doing that for <em><code>HList </code></em>is a little more involved. We have to construct a chain of types in exactly the right way, at compile-time.</p>
<p>Appending an <em><code>HList </code></em>to another is a function that takes two <em><code>HList</code></em>-valued arguments and returns an <em><code>HList</code></em>. Using first-class functions from <a href="http://functionaljava.org/" target="_blank">Functional Java</a>, the append operation for <em><code>HLists </code></em>of specific types <em><code>L</code></em> and <em><code>R</code></em>, would be a function of the following type:</p>
<pre class="brush: java;">
F2&lt;L extends HList&lt;R&gt;, L extends HList&lt;L&gt;, LR extends HList&lt;LR&gt;&gt;
</pre>
<p>Where <em><code>LR </code></em>is the type of the concatenated <em><code>HList</code></em>. Now, since we necessarily have the two arguments, we know the specific types of <em><code>L</code></em> and <em><code>R</code></em>. Since Java doesn&#8217;t have type inference, it cannot automatically figure out the specific type of <em><code>LR</code></em>. We will have to supply it as type annotation. Not to worry. Even though Java doesn&#8217;t infer types, it can be coerced into doing some type arithmetic. All we have to do is a little inductive reasoning.</p>
<p><strong>Types as Formulae</strong></p>
<p><strong><span style="font-weight:normal;">According to the Curry-Howard isomorphism, a program is a proof, and the hypothesis that it proves is a type for the program. In this sense, Java&#8217;s type system is a kind of crude theorem prover. Put another way, a type is a predicate, and values of that type represent the terms for which the predicate holds. The function type above therefore asserts that for any two <em><code>HLists</code></em>, <em><code>L</code></em> and <em><code>R</code></em>, there exists some program to derive the <em><code>HList LR</code></em>. The function type by itself does not put any constraints on <em><code>LR</code></em>, however. It can be derived by any function, not just the concatenation function. We will remedy that presently. We need a formula that states that the two types <em><code>L</code></em> and <em><code>R</code></em> imply a third type <em><code>LR </code></em>which is the <em><code>HList </code></em>concatenation of<em><code> L</code></em> and<em><code> R</code></em>, given some concatenation function. Here is the type that represents that formula:</span></strong></p>
<pre class="brush: java;">
public static final class HAppend&lt;L, R, LR&gt; {
    private final F2&lt;L, R, LR&gt; append;

    private HAppend(final F2&lt;L, R, LR&gt; f) {
      append = f;
    }

    public LR append(final L l, final R r) {
      return append.f(l, r);
    }
}
</pre>
<p>At this point, <em><code>HAppend </code></em>is still just a hypothesis without evidence. Remember that a value of a type is proof of the formula that it represents. So we will need to supply two proofs in the form of constructors for values of this type; one for the base case of appending to the empty list <em><code>HNil</code></em>, and another for the case of appending to an <em><code>HCons</code></em>. The base case is easy. Appending anything to the empty list should result in that same thing. So the <em><code>HAppend </code></em>constructor for appending to the empty list looks like this:</p>
<pre class="brush: java;">
    public static &lt;L extends HList&lt;L&gt;&gt; HAppend&lt;HNil, L, L&gt; append() {
      return new HAppend&lt;HNil, L, L&gt;(new F2&lt;HNil, L, L&gt;() {
        public L f(final HNil hNil, final L l) {
          return l;
        }
      });
    }
</pre>
<p>The case for the nonempty list is not quite as easy. Consider its type:</p>
<pre class="brush: java;">
    public static &lt;X, A extends HList&lt;A&gt;, B, C extends HList&lt;C&gt;, H extends HAppend&lt;A, B, C&gt;&gt;
                         HAppend&lt;HCons&lt;X, A&gt;, B, HCons&lt;X, C&gt;&gt; append(final H h)
</pre>
<p>Read the return type first. This returns an <em><code>HAppend </code></em>that appends some <em><code>B</code></em> to an <em><code>HCons&lt;X, A&gt;</code></em>. The type of the head of the first list (<em><code>X</code></em>) becomes the type of the head of the concatenated list. The tail of the concatenated list is <em><code>C</code></em>. The type constraints state that <em><code>C</code></em> must be an <em><code>HList</code></em>, and that there must exist some way to append <em><code>B</code></em> (the second list) to <em><code>A</code></em> (the tail of the first list) so that they make <em><code>C</code></em>. We must supply proof that this last constraint holds, and you&#8217;ll see that such a proof is in fact supplied as an argument (in the form of the value <em><code>h</code></em>).<br />
What this is saying is that, given the premise that <em><code>A</code></em> and <em><code>B </code></em>can be concatenated, the concatenation of <em><code>HCons&lt;X, A&gt;</code></em> and<em><code> B</code></em> can be inferred. A value of type <em><code>HAppend&lt;A, B, C&gt;</code></em> is precisely proof of the hypothesis that <em><code>A</code></em> and <em><code>B</code></em> can be concatenated, since there are only these two cases and we&#8217;ve supplied a proof for both. In other words, if we can append to the empty list, then that&#8217;s proof enough that we can append to a list of one element, which proves that we can append to a list of two elements, and so on. Given this, we can construct a chain of proofs. This concatenated proof, then, is a function that concatenates lists of the corresponding types.</p>
<p>OK, so how do we use this? Well, here&#8217;s an example program that appends one list to another:</p>
<pre class="brush: java;">
public class HList_append {
  public static void main(final String[] args) {
    // The two lists
    final HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;&gt; a =
      cons(&quot;Foo&quot;, cons(3, cons(true, nil())));
    final HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt; b =
      cons(4.0, cons(&quot;Bar&quot;, cons(new Integer[]{1, 2}, nil())));

    // A lot of type annotation
    final HAppend&lt;HNil, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt; zero = append();
    final HAppend&lt;HCons&lt;Boolean, HNil&gt;, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt; one = append(zero);
    final HAppend&lt;HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt; two = append(one);
    final HAppend&lt;HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HNil&gt;&gt;&gt;,
      HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;,
      HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt;&gt;
      three = append(two);

    // And all of that lets us append one list to the other.
    final HCons&lt;String, HCons&lt;Integer, HCons&lt;Boolean, HCons&lt;Double, HCons&lt;String, HCons&lt;Integer[], HNil&gt;&gt;&gt;&gt;&gt;&gt;
      x = three.append(a, b);

    // And we can access the components of the concatenated list in a type-safe manner
    if (x.tail().tail().head())
      System.out.println(x.tail().tail().tail().tail().tail()[1] * 2); // 4
  }
}
</pre>
<p>Holy pointy brackets, Batman! Do we really need all of that? Well, look at what it&#8217;s doing. It&#8217;s constructing a concatenation function of the appropriate type, by supplying the premise at each step. If this seems mechanical, then that&#8217;s because it is. There is only one possible implementation for the <em><code>HAppend </code></em>we need, but Java does not have any mechanism for figuring this out, nor does it provide a facility for the programmer to tell it how.<br />
Contrast that to Scala. The above is a clear example of where Scala&#8217;s implicit arguments come in handy. If we import this to Scala, we can make both of the <em><code>append </code></em>functions implicit, and we can further make the <em><code>H</code></em> argument to the <em><code>append </code></em>function for nonempty lists implicit as well. There can be only one possible implementation of each function, so it can be declared once and used implicitly wherever proofs of the corresponding types are required. <a href="http://trac.assembla.com/metascala/browser/src/metascala/HLists.scala" target="_blank">Jesper Nordenberg has implemented an HList library for Scala that demonstrates this well</a>. With implicits and Scala, the whole middle section of our program is condensed from 12 lines of type annotations to just:</p>
<pre class="brush: java;">
 val x = a.append(b)
</pre>
<p>Now, if you&#8217;re really into this Java stuff, you&#8217;re probably thinking: &#8220;implicits are just dependency injection&#8221;. Well, in a sense, you would be right. Both dependency injection and inheritance are degenerate forms of implicits. However, there is currently no dependency injection framework for Java that can abstract over type constructors such that it provides injection of parameterised types with injection of type parameters also. If you can prove me wrong, by all means send me evidence in the form of working code.</p>
<p><strong>Conclusion</strong></p>
<p>Clearly, Java is not very useful for this kind of type-safe programming. I was actually quite surprised that you can do this in Java <strong>at all</strong>, but we&#8217;ve definitely hit the outer boundary of what can be considered reasonably expressible.</p>
<p>The code you&#8217;ve seen in this article uses the new <em><code>HList </code></em>package that was released with <a href="http://functionaljava.org/download" target="_blank">Functional Java 2.16</a>. And is based on the Haskell HS library by<em> </em><em><span style="font-style:normal;"><a href="http://okmij.org/ftp/" target="_blank">Oleg</a></span></em><a href="http://okmij.org/ftp/" target="_blank"> Kiselyov</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=108&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2008/10/23/heterogeneous-lists-and-the-limits-of-the-java-type-system/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Higher-Order Java Parallelism, Part 4: A Better Future</title>
		<link>http://apocalisp.wordpress.com/2008/09/02/a-better-future/</link>
		<comments>http://apocalisp.wordpress.com/2008/09/02/a-better-future/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 07:37:29 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[java abuse]]></category>
		<category><![CDATA[java7]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=80</guid>
		<description><![CDATA[This is the fourth installment in a series of posts about making highly concurrent software easier to write in Java. Previous entries are available here: part 1, part 2, part 3. However, I aim to make it possible to follow along even if you haven&#8217;t read the previous posts.
I Have Seen the Future&#8230;
If you have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=80&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This is the fourth installment in a series of posts about making highly concurrent software easier to write in Java. Previous entries are available here: <a href="http://apocalisp.wordpress.com/2008/06/18/parallel-strategies-and-the-callable-monad/" target="_blank">part 1</a>, <a href="http://apocalisp.wordpress.com/2008/06/30/parallel-list-transformations/" target="_blank">part 2</a>, <a href="http://apocalisp.wordpress.com/2008/07/28/threadless-concurrency-with-actors/" target="_blank">part 3</a>. However, I aim to make it possible to follow along even if you haven&#8217;t read the previous posts.</p>
<p><strong>I Have Seen the Future&#8230;</strong></p>
<p>If you have used the <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html" target="_blank">Java 5 concurrency API</a> at all, you will have come across the <em><code><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html" target="_blank">Future</a></code></em> class. For example, when you submit a <em><code>Callable&lt;Integer&gt;</code></em> to an <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html" target="_blank"><em><code>ExecutorService</code></em></a>, what you get back is a <em><code>Future&lt;Integer&gt;</code></em> which represents a computation, running concurrently, that will (hopefully) result in an integer at some time in the future. Once you have the <em><code>Future&lt;Integer&gt; fi</code></em>, you can later get the integer out of it by calling <em><code>fi.get().</code></em></p>
<p>That&#8217;s all fine and dandy, but let&#8217;s say you want do do something like add two future integers. You could do something like this:</p>
<pre class="brush: java;">
int sum = x.get() + y.get();
</pre>
<p>This will block the current thread until both of those integers are available, then add them together. But why wait for that? If you have an <em><code>ExecutorService</code></em>, you can create a new <em><code>Future </code></em>that computes the sum:</p>
<pre class="brush: java;">

Future&lt;Integer&gt; sum = executorService.submit(new Callable&lt;Integer&gt;() {
  public Integer call() {
    return x.get() + y.get();
  }
});
</pre>
<p>Now the current thread can continue, but we&#8217;ve started a new thread that does nothing until the values of <em><code>x</code></em> and <em><code>y</code></em> have both been calculated by yet another thread.</p>
<p>We&#8217;re beginning to see a problem here. We want to be able to compose <em><code>Futures </code></em>together to form new <em><code>Futures</code></em>, but find that the number of threads required to compose n <em><code>Future </code></em>values is on the order of O(n). If we have a fixed-size thread pool, we&#8217;ll run into starvation. If we have an unbounded thread pool, then we might start more threads than the operating system can handle, most of which will be doing nothing at all but wait for other threads.</p>
<p>This should all sound very familiar. Threads are a space resource. What kind of processes are O(n) in their space requirement? If you said &#8220;linearly recursive processes&#8221;, go to the head of the class. Intuitively, for the same reason that we can find iterative versions of any recursive algorithm, it seems that we should be able to find an algorithm to accomplish the same thing with O(1) threads.</p>
<p><strong>&#8230;and it is a Monad</strong></p>
<p>In the above example, it&#8217;s like we&#8217;re giving seperate instructions, waiting for the results of each in between. Imagine if we were working in an office with Bob and Alice, and we needed work on something from both of them. We might go to Bob and say: &#8220;Bob, process this and give me the result&#8221;. Then we&#8217;d take the result to Alice and say: &#8220;Alice, here&#8217;s a result from Bob.&#8221; It would be much better, if we could just go to Bob and say: &#8220;Bob, process this and give the result to Alice.&#8221; This is the essential difference between recursive and iterative processes.</p>
<p>But wait! We say that kind of thing all the time, in Java:</p>
<pre class="brush: java;">

public Work bob(Work w) { ... }
public Work alice(Work w) { ... }

public Work bobThenAlice(Work w) {
  Work b = bob(w);
  return alice(b);
}
</pre>
<p>Here, we&#8217;re instructing a single thread to do some work, then use the result of that work to do more work. What&#8217;s really sneaky here is the meaning of the semicolon. In this context, what the former semicolon means is &#8220;take the stored value <em><code>b</code></em> from the previous statement and bind it to the free variable <em><code>b</code></em> in the next statement&#8221;. You can think of the second semicolon as binding a blank statement over the result of the preceding statement.</p>
<p>Using <a href="http://projects.workingmouse.com/public/functionaljava/artifacts/2.13/javadoc/fj/F.html" target="_blank">first-class functions</a> from <a href="http://functionaljava.org/" target="_blank">Functional Java</a>, and using the <a href="http://projects.workingmouse.com/public/functionaljava/artifacts/2.13/javadoc/fj/control/parallel/Callables.html" target="_blank">Callables monad</a> from the first part of this series, you could implement that same behaviour using something like this:</p>
<pre class="brush: java;">
F&lt;Work, Callable&lt;Work&gt;&gt; bob = new F&lt;Work, Callable&lt;Work&gt;&gt;() {
  public Callable&lt;Work&gt; f(final Work w) {
    return new Callable&lt;Work&gt;() {
      public Work call() { ... }
    };
  }
};
F&lt;Work, Callable&lt;Work&gt;&gt; alice = new F&lt;Work, Callable&lt;Work&gt;&gt;() { ... };

public Callable&lt;Work&gt; bobThenAlice(Work w) {
  return Callables.bind(bob.f(w), alice);
}
</pre>
<p>That&#8217;s pretty neat. Now we have a single <em><code>Callable </code></em>that we can run concurrently in a new thread, turning it into a <em><code>Future</code></em>. But wouldn&#8217;t it be cool if we could bind <em><code>Futures?</code></em> That would let us take already running computations and combine them in exactly this way. We want a <em><code>Future </code></em>monad.</p>
<p>The problem with combining <em><code>Futures </code></em>is in the nature of the future. This is a deliberate pun on &#8220;future&#8221;. Think about time for a second. What does it mean to get a value that&#8217;s in the future? By the very fact that causality is sequential, it&#8217;s a violation of the nature of reality to have something that doesn&#8217;t yet exist. It&#8217;s the future; you&#8217;re not supposed to get stuff out. But, we can put stuff in, can&#8217;t we? Yes we can. You know those corny time-capsule things where people put their mountain bikes and Nintendo games for future generations to enjoy later? We can do that with data values. And not just values, but computations.</p>
<p><strong>Here&#8217;s One I Made Earlier</strong></p>
<p>The <em><code>Future </code></em>class in the standard Java libraries doesn&#8217;t come with any methods for projecting computations into the future. But Functional Java comes with a class called <a href="http://projects.workingmouse.com/public/functionaljava/artifacts/2.13/javadoc/fj/control/parallel/Promise.html" target="_blank">Promise&lt;A&gt;</a> which does have that feature. It makes use of light-weight concurrent processes (actors), and parallel strategies, as described in the previous post, to implement the ability to combine concurrent computations into larger (concurrently executing) structures.</p>
<p>Since it is implemented as a <a href="http://en.wikipedia.org/wiki/Monads_in_functional_programming" target="_blank">monad</a>, the methods it provides are all the usual suspects: unit, bind, fmap, join, etc. Here&#8217;s a quick overview of what they do and why they&#8217;re useful. Grasping them doesn&#8217;t just help you understand the <em><code>Promise </code></em>class, but any monad you may come across in the (ahem) future.</p>
<p>The <strong>unit function</strong>, the constructor of <em><code>Promises</code></em>, is just called <em><code>promise</code></em>. It has a few overloaded forms, but here is the simplest one.</p>
<pre class="brush: java;">
public static &lt;A&gt; Promise&lt;A&gt; promise(Strategy&lt;A&gt; s, P1&lt;A&gt; p);
</pre>
<p>The <em><code>P1</code></em> class is just a simple closure with no arguments, provided by the Functional Java library. <em><code>P1&lt;A&gt;</code></em> consists of one abstract method: <em><code>A _1(). Strategy</code></em> represents a method of evaluating <em><code>P1s</code></em> concurrently. I also talk about <em><code>Strategies </code></em>in the previous post, but the long and the short of it is that it has methods to evaluate the <em><code>P1</code></em> value according to some parallelisation strategy, like with a thread pool for instance.</p>
<p>Calling the <em><code>promise </code></em>method starts a concurrent computation, in a manner according to the given strategy, that evaluates <em><code>p</code></em>. The resulting <em><code>Promise </code></em>value is a handle on the running computation, and can be used to retrieve the value later. <em><code>Promise.claim()</code></em> will block the current thread until the value is available, exactly like <em><code>Future.get()</code></em>, but this is generally not what you want to do. Instead, you want to bind.</p>
<p>The essence of the monad pattern is the <strong>binding function</strong>. If you don&#8217;t think you already know what a monad is, but understand this method, then you know more than you think:</p>
<pre class="brush: java;">
public Promise&lt;B&gt; bind(F&lt;A, Promise&lt;B&gt;&gt; f);
</pre>
<p>This method means that if you have a <em><code>Promise </code></em>of an <em><code>A</code></em>, and a function from an <em><code>A</code></em> to a <em><code>Promise </code></em>of a <em><code>B</code></em>, you can get a <em><code>Promise </code></em>of a <em><code>B</code></em>. I.e. if somebody promises you an <em><code>A</code></em>, and I can promise you a <em><code>B</code></em> for every <em><code>A</code></em>, it&#8217;s the same thing as being promised a <em><code>B</code></em> in the first place.</p>
<p>The <strong>mapping function</strong>:</p>
<pre class="brush: java;">
public Promise&lt;B&gt; fmap(F&lt;A, B&gt; f);
</pre>
<p>This method means that if you have an <em><code>Promise </code></em>of an <em><code>A</code></em>, and a function from <em><code>A</code></em> to <em><code>B</code></em>, you can get a <em><code>Promise </code></em>of a <em><code>B</code></em>. In other words, you can map any function over a <em><code>Promise</code></em>, and <em><code>fmap </code></em>will return you a <em><code>Promise </code></em>of the result. Behind the scenes, <em><code>fmap </code></em>is implemented by calling the <em><code>bind </code></em>and <em><code>promise </code></em>methods. The difference between this method and the <em><code>bind </code></em>method is subtle but important. Calling <em><code>p.bind(f)</code></em> is exactly equivalent to calling <em><code>Promise.join(p.fmap(f)).</code></em></p>
<p>The <strong>join function</strong>:</p>
<pre class="brush: java;">
public static &lt;A&gt; Promise&lt;A&gt; join(Promise&lt;Promise&lt;A&gt;&gt; a);
</pre>
<p><em><code>Join </code></em>is a lot more useful than it looks. If you have a promised <em><code>Promise,</code></em> it&#8217;s the same as just having a <em><code>Promise</code></em>. In practise, that means that if you can start a concurrent task that starts a concurrent task, you can combine those into one concurrent task. You can think of it as the semantic equivalent of <em><code>Thread.join()</code></em>, except that our method returns the joined <em><code>Promise </code></em>immediately.</p>
<p>Coming back to Bob and Alice for a second, we can implement <em><code>bob</code></em> and <em><code>alice</code></em> from the <em><code>Callables </code></em>example above, using <em><code>Promise</code></em> instead of <em><code>Callable</code></em> . Both <em><code>bob</code></em> and <em><code>alice</code></em> will construct <em><code>Promises</code></em> using the <em><code>promise</code></em> method, putting whatever work they do inside a <em><code>P1</code></em>. That way, when you call <em><code>bob</code></em>, he&#8217;s already doing his work by the time you mention Alice&#8217;s name:</p>
<pre class="brush: java;">
final Strategy&lt;Work&gt; s = Strategy.simpleThreadStrategy();
F&lt;Work, Promise&lt;Work&gt;&gt; bob = new F&lt;Work, Promise&lt;Work&gt;&gt;() {
  public Promise&lt;Work&gt; f(final Work w) {
    return promise(s, new P1() {
      public Work _1() { ... }
    });
  }
};
F&lt;Work, Promise&lt;Work&gt;&gt; alice = new F&lt;Work, Promise&lt;Work&gt;&gt;() { ... };

public Promise&lt;Work&gt; bobThenAlice(Work w) {
  return bob.f(w).bind(alice);
}
</pre>
<p>So now that we can build arbitrarily complex concurrent processes from already-running processes, how do we get the final promised value out? Again, you could call <em><code>Promise.claim()</code></em>, but that blocks the current thread as we know. Instead, <em><code>Promise </code></em>comes equipped with a method <em><code>to(Actor&lt;A&gt;) </code></em>which promises to send the value to the given <em><code>Actor </code></em>as soon as it&#8217;s ready. Control is returned to the current thread immediately, and the whole computation continues in the background, including the action to take on the final result. <em><code>Actors </code></em>were discussed in the <a href="http://apocalisp.wordpress.com/2008/07/28/threadless-concurrency-with-actors/" target="_blank">previous post.</a></p>
<p><strong>A Fully Functional Example</strong></p>
<p>I think an example is in order. The following program calculates Fibonacci numbers using a naive recursive algorithm. This is an algorithm that benefits particularly well from parallelisation (barring any other kind of optimisation). If we were just using plain old <em><code>Future </code></em>instead of <em><code>Promise</code></em>, the number of <em><code>Threads </code></em>required to calculate the nth Fibonacci number is O(fib(n)). But since we&#8217;re using <em><code>Promise</code></em>, we can use a fixed number of actual Java threads.</p>
<pre class="brush: java;">

package concurrent;

import static fj.Bottom.error;
import fj.Effect;
import fj.F;
import fj.F2;
import fj.Function;
import fj.P;
import fj.P1;
import fj.P2;
import fj.Unit;
import fj.data.List;
import fj.control.parallel.Actor;
import fj.control.parallel.Promise;
import fj.control.parallel.Strategy;
import static fj.data.List.range;
import static fj.function.Integers.add;
import static fj.control.parallel.Promise.join;
import static fj.control.parallel.Promise.promise;
import static fj.control.parallel.Actor.actor;

import java.text.MessageFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Fibs {

  private static final int CUTOFF = 35;

  public static void main(final String[] args) throws Exception {
    if (args.length &lt; 1)
      throw error(&quot;This program takes an argument: number_of_threads&quot;);

    final int threads = Integer.parseInt(args[0]);
    final ExecutorService pool = Executors.newFixedThreadPool(threads);
    final Strategy&lt;Unit&gt; su = Strategy.executorStrategy(pool);
    final Strategy&lt;Promise&lt;Integer&gt;&gt; spi = Strategy.executorStrategy(pool);

    // This actor performs output and detects the termination condition.
    final Actor&lt;List&lt;Integer&gt;&gt; out = actor(su, new Effect&lt;List&lt;Integer&gt;&gt;() {
      public void e(final List&lt;Integer&gt; fs) {
        for (P2&lt;Integer, Integer&gt; p : fs.zipIndex()) {
          System.out.println(MessageFormat.format(&quot;n={0} =&gt; {1}&quot;, p._2(), p._1()));
        }
        pool.shutdown();
      }
    });

    // A parallel recursive Fibonacci function
    final F&lt;Integer, Promise&lt;Integer&gt;&gt; fib = new F&lt;Integer, Promise&lt;Integer&gt;&gt;() {
      public Promise&lt;Integer&gt; f(final Integer n) {
        return n &lt; CUTOFF ?
                promise(su, P.p(seqFib(n))) :
                f(n - 1).bind(f(n - 2), add);
      }
    };

    System.out.println(&quot;Calculating Fibonacci sequence in parallel...&quot;);

    join(su, spi.parMap(fib, range(0, 46)).map(Promise.&lt;Integer&gt;sequence(su))).to(out);
  }

  // The sequential version of the recursive Fibonacci function
  public static int seqFib(final int n) {
    return n &lt; 2 ? n : seqFib(n - 1) + seqFib(n - 2);
  }

}
</pre>
<p>For all you Scala fans out there, the Functional Java library comes with <a href="http://projects.workingmouse.com/public/functionaljava/artifacts/2.13/scaladoc/index.html" target="_blank">convenient bindings for Scala</a> as well. Here&#8217;s the same thing written in Scala. Note that this does not use the Actor library from the standard Scala libraries, but the same lighter weight Java implementation that the Java example above uses.</p>
<pre class="brush: java;">

package concurrent

import fj.control.parallel.{Actor, Promise}
import fj.Function.curry
import fj.control.parallel.Strategy.executorStrategy
import fjs.control.parallel.Strategy.parMap
import fjs.control.parallel.Promise._
import fjs.control.parallel.Actor._
import Integer.parseInt
import List.range
import java.util.concurrent.Executors.newFixedThreadPool
import fjs.F._
import fjs.F2._
import fjs.P1._
import fjs.P2._
import fjs.data.List._
import fjs.control.parallel.Strategy.ListPar

object Fibs {
  val CUTOFF = 35;

  def main(args: Array[String]) = {
    if (args.length &lt; 1)
      error(&quot;This program takes an argument: number_of_threads&quot;)

    val threads = parseInt(args(0))
    val pool = newFixedThreadPool(threads)
    implicit def s[A] = executorStrategy[A](pool)

    // This actor performs output and detects the termination condition.
    val out: Actor[List[Int]] = actor{
      ns =&gt;
        for ((n, i) &lt;- ns.zipWithIndex) printf(&quot;n=%d =&gt; %d\n&quot;, i, n)
        pool.shutdown()
    }

    // A parallel recursive Fibonacci function
    def fib(n: Int): Promise[Int] = {
      if (n &lt; CUTOFF) promise(() =&gt; seqFib(n))
      else fib(n - 1).bind(fib(n - 2), curry((_: Int) + (_: Int)))
    }

    println(&quot;Calculating Fibonacci sequence in parallel...&quot;)
    out ! sequence(parMap[Int, Promise[Int], List](fib, range(0, 46)));
  }

  // The sequential version of the recursive Fibonacci function
  def seqFib(n: Int): Int = if (n &lt; 2) n else seqFib(n - 1) + seqFib(n - 2);
}
</pre>
<p>Here&#8217;s an example run of this program using a pool of 10 threads. It runs about 7 times faster that way than with just 1 thread on my 8-way machine. The Scala version is also very slightly faster for some reason.</p>
<pre style="background:black;color:white;">$ scala -classpath .:../../../build/classes/src concurrent.Fibs 10
Calculating Fibonacci sequence in parallel...
n=0 =&gt; 0
n=1 =&gt; 1
n=2 =&gt; 1
n=3 =&gt; 2
n=4 =&gt; 3
n=5 =&gt; 5
n=6 =&gt; 8
n=7 =&gt; 13
n=8 =&gt; 21
n=9 =&gt; 34
n=10 =&gt; 55
n=11 =&gt; 89
n=12 =&gt; 144
n=13 =&gt; 233
n=14 =&gt; 377
n=15 =&gt; 610
n=16 =&gt; 987
n=17 =&gt; 1597
n=18 =&gt; 2584
n=19 =&gt; 4181
n=20 =&gt; 6765
n=21 =&gt; 10946
n=22 =&gt; 17711
n=23 =&gt; 28657
n=24 =&gt; 46368
n=25 =&gt; 75025
n=26 =&gt; 121393
n=27 =&gt; 196418
n=28 =&gt; 317811
n=29 =&gt; 514229
n=30 =&gt; 832040
n=31 =&gt; 1346269
n=32 =&gt; 2178309
n=33 =&gt; 3524578
n=34 =&gt; 5702887
n=35 =&gt; 9227465
n=36 =&gt; 14930352
n=37 =&gt; 24157817
n=38 =&gt; 39088169
n=39 =&gt; 63245986
n=40 =&gt; 102334155
n=41 =&gt; 165580141
n=42 =&gt; 267914296
n=43 =&gt; 433494437
n=44 =&gt; 701408733
n=45 =&gt; 1134903170</pre>
<div><span><br />
</span></div>
<p>Massive win! If we had been using <em><code>Future </code></em>instead of <code><em>Promise</em></code>, we would have needed at least 55 threads (since we&#8217;re using a cutoff at 35 and 45 &#8211; 35 = 10 and fib(10) = 55). Heck, we could even remove the threshold value altogether and calculate all 45 parallel fibs, in parallel. That would require <strong>1,134,903,170 threads</strong> in the absence of non-blocking concurrency abstractions like <code><em>Promise</em></code> and <code><em>Actor</em></code>. We can run that in just one thread if we&#8217;d like.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/apocalisp.wordpress.com/80/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/apocalisp.wordpress.com/80/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=80&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2008/09/02/a-better-future/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Higher-Order Java Parallelism, Part 3: Threadless Concurrency With Actors</title>
		<link>http://apocalisp.wordpress.com/2008/07/28/threadless-concurrency-with-actors/</link>
		<comments>http://apocalisp.wordpress.com/2008/07/28/threadless-concurrency-with-actors/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 18:08:57 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[actors]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[java7]]></category>
		<category><![CDATA[multicore]]></category>
		<category><![CDATA[nonblocking]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[threadless]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=52</guid>
		<description><![CDATA[Multi-core processing is changing the way we write programs. Modern computers come with multi-core or multiple processors, and modern applications no longer do just one thing at a time. But all of this has left Java a little bit behind. While languages like Erlang and Haskell, even other JVM languages like Scala, have powerful concurrency [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=52&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Multi-core processing is changing the way we write programs. Modern computers come with multi-core or multiple processors, and modern applications no longer do just one thing at a time. But all of this has left Java a little bit behind. While languages like Erlang and Haskell, even other JVM languages like Scala, have powerful concurrency abstractions, concurrent programming in Java is still mostly based around the threading practices of way back in the time of applets.</p>
<p>Threading in Java has long been the domain of the brave or foolish, weaving a brittle and intricate web of manual synchronization between threads, or threads waiting on other threads to feed them work. <a href="http://oreilly.com/catalog/expjava/excerpt/index.html" target="_blank">We all know the examples from old Java books</a> where there might be a thread running in a loop, sleeping for a second, and waking up to check if work needs to be done. This kind of implementation is still being taught to newcomers to the Java language. And when they start writing their own applications in Java, they find that this approach does not scale. Not along the axis of program complexity, nor the axis of the number of threads. In a complex application or one with many threads, you may end up with a program that stops doing anything at all for long periods of time, or worse, hangs forever, while still consuming all the operating system resources that go with those threads.</p>
<p><strong>Blocking vs. Nonblocking Concurrency</strong></p>
<p>The new concurrency library in Java 5 help this situation a great deal. At least some advancement has been made towards better concurrency abstractions. But there are still pitfalls. For instance, <a href="http://apocalisp.wordpress.com/2008/06/18/parallel-strategies-and-the-callable-monad/" target="_blank">as we&#8217;ve seen</a>, the <code><em><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html" target="_blank">Future</a> </em></code>and <code><em><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Callable.html" target="_blank">Callable</a> </em></code>interfaces are really rather jolly useful, and they can take us a long way towards the kinds of things possible in those other languages. But at the end of the day, something has to call <code><em>Future.get()</em></code>, and that something will block unless the value is already available, until such time that it is. This can result in deadlocks or starvation, as you may end up in a situation where all threads are blocking on <code><em>Futures </em></code>and none are available to advance your program forward. This would be bad. In fact, we could say that <strong>in a highly concurrent system, blocking on shared state is a catastrophic event.</strong></p>
<p>The Java libraries are a veritable minefield of methods that, ultimately, block on shared memory monitors. There&#8217;s <code><em>Thread.join(), Future.get(), <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html" target="_blank">BlockingQueue</a>.take(),</em></code> and the list goes on. But what if we could solve this problem with a simple abstraction? Could we solve the blocking problem once and re-use our solution for different situations? Let&#8217;s see.</p>
<p>Instead of sleeping or blocking, what we really want is the ability for our code to say: &#8220;When X happens, wake up a thread to execute me.&#8221; That is, in some sense we want threads to go on doing useful work, and have them be notified when an event happens that requires more work to be done. For example, when Bob comes into my office and asks me for this month&#8217;s TPS reports, he doesn&#8217;t stand around and wait for them, nor does he check periodically to see if I have them done, and he certainly doesn&#8217;t go to sleep to periodically wake up and poll me for them. He will continue with his work (or whatever it is he does all day), since I have instructions to send him those TPS reports as soon as they&#8217;re ready. We&#8217;re independent and concurrent actors, so we don&#8217;t wait on each other.</p>
<p>There is a model of computation, called the <a href="http://en.wikipedia.org/wiki/Actor_model" target="_blank">Actor Model</a>, that works very much the same way. This is the model employed in the design of Erlang, and it was a major motivator for languages like Simula and Scheme. An implementation of Actors comes with the standard Scala library, and as it happens an implementation of it for Java has just been released as part of the <a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/control/parallel/package-frame.html" target="_blank">2.8 version of the Functional Java library</a>. You can <a href="http://www.google.com/search?q=actor+model" target="_blank">read more about the actor model</a> on your own, but I will explain in some detail how it works in <a href="http://functionaljava.org" target="_blank">Functional Java</a>.</p>
<p><strong>Algorithm + Strategy = Parallelism</strong></p>
<p>The first thing to explain is <a href="http://www.macs.hw.ac.uk/~dsg/gph/papers/html/Strategies/strategies.html" target="_blank">Parallel Strategies</a>. I&#8217;ve talked about them before, but I&#8217;ll do so again here since they&#8217;re important to what I&#8217;m trying to demonstrate. The idea of the Parallel Strategy is that we can capture a threading pattern, or some method of concurrency in general, in a separate abstraction called a <code><em>Strategy</em></code>, and write our concurrent programs independently of the particular threading pattern.</p>
<p>In Java terms, a <a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/control/parallel/Strategy.html" target="_blank"><code><em>Strategy&lt;A&gt;</em></code></a> is a way of turning any Java expression, whose evaluated type is <code><em>A</em></code>, into a value of type <code><em>A</em></code>. How this happens is implementation-specific, but we&#8217;d like it to be concurrent. First, we need a way of capturing the idea of an unevaluated <code><em>A</em></code>. It&#8217;s a bit like the idea behind <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runnable.html" target="_self"><code><em>Runnable</em></code></a>, only that we can get a value out of it. Java 5 has the <code><em>Callable </em></code>interface, which is really close to representing &#8220;any expression&#8221;, since we can create <code><em>Callables</em></code> anonymously, but its<code><em> call()</em></code> method throws <code><em>Exceptions</em></code>, creating a kind of barrier for the programmer. Instead, we&#8217;ll use <a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/P1.html"><code><em>fj.P1&lt;A&gt;</em></code></a>. It&#8217;s an abstract class with one abstract method: <code><em>A _1()</em></code></p>
<p>The <code><em>Strategy</em></code> class has an instance method called <code><em>par(P1&lt;A&gt;)</em></code>. This will evaluate the given <code><em>P1</em></code> concurrently, immediately returning another <code><em>P1</em></code> that can get the value once it&#8217;s ready. Behind the scenes, it actually creates a <code><em>Callable</em></code>, turns it into a <code><em>Future</em></code>, then returns a <code><em>P1</em></code> that calls <code><em>Future.get()</em></code>, but all of that is implementation detail. Strategy comes with some static construction methods that implement basic threading patterns, and you can implement your own by passing a Future-valued function to the <code><em>strategy(F&lt;P1&lt;A&gt;, Future&lt;A&gt;&gt;)</em></code> method.</p>
<p>You might be concerned that <code><em>P1</em></code> can&#8217;t throw any checked exceptions. This is by design. You&#8217;ll find that once you start working with concurrent effects, the value of checked exceptions goes out the window. Besides, if our <code><em>P1</em></code> did need to throw errors, we would use <a href="http://apocalisp.wordpress.com/2008/06/18/parallel-strategies-and-the-callable-monad/" target="_blank">Lazy Error Handling</a> and declare this in the <code><em>P1's</em></code> type.</p>
<p><strong>Strategy + Effect = Actor</strong></p>
<p>So now we have concurrent evaluation and parallel transformations. But we&#8217;re still ending up with objects that require us to call a blocking method to get their value. The only way around that is to not return any values at all. Instead, we&#8217;re going to let our <code><em>P1s </em></code>have side-effects. So instead of returning a <code><em>P1 </em></code>that lets us wait for the value, our <code><em>P1s </em></code>are now going to update a variable or send the value somewhere.</p>
<p>To model a <code><em>P1 </em></code>that doesn&#8217;t return a useful value, I&#8217;m going to use the <code><em><a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/Unit.html" target="_blank">Unit</a> </em></code>type. It&#8217;s another one of those trivial but useful classes. The <code><em>Unit </em></code>type is part of Functional Java, and it&#8217;s simply a type that only has one possible value. It&#8217;s a lot like the standard library&#8217;s <code><em><a href="http://java.sun.com/javase/6/docs/api/java/lang/Void.html" target="_blank">Void</a> </em></code>type, except <code><em>Void </em></code>is even further impoverished. When you use this type, you&#8217;re saying that the actual value is immaterial. So to describe a <code><em>P1 </em></code>that doesn&#8217;t return anything useful, we use the type <code><em>P1&lt;Unit&gt;</em></code>. We can also describe a transformation from some type <code><em>T</em></code> to <code><em>Unit</em></code>, with <code><em>F&lt;T, Unit&gt;</em></code>.</p>
<p>Functional Java comes with a more honest version of <code><em>F&lt;A, Unit&gt;</em></code>, which is<code><em> Effect&lt;A&gt;</em></code>. It&#8217;s more honest in the sense that it doesn&#8217;t pretend to be a transformation, which properly should have no side-effects. <code><em>Effect&lt;A&gt;</em></code> is explicit about having side-effects, which is what we intend for Actors. It has one method that doesn&#8217;t return a value at all: <code><em>void e(A a).</em></code></p>
<p>We now have what we need to instantiate the <a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/control/parallel/Actor.html" target="_blank"><code><em>Actor</em></code></a> class. Here&#8217;s an example of how to create and use a simple actor:</p>
<pre style="background:#242424;color:#f6f3e8;"><span>
  Strategy&lt;Unit&gt; s = Strategy.simpleThreadStrategy();

  Actor&lt;<span style="color:#e5786d;">String</span>&gt; a = Actor.actor(s, <span style="color:#8ac6f2;">new</span> Effect&lt;<span style="color:#e5786d;">String</span>&gt;()
    <span style="color:#cae682;">{</span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> e(</span><span style="color:#e5786d;">String</span><span style="color:#cae682;"> s)</span>
      <span style="color:#cae682;">{</span><span style="color:#e5786d;">System</span>.out.println(s);<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>);

  a.act(<span style="color:#95e454;"><code><em>"Hello, actors!"</em></code></span>);

</span>
</pre>
<p>The actor receives &#8220;messages&#8221; on its <code><em>act</em></code> method, and the <code><em>Strategy </em></code>serves as a kind of mailbox for it. You will note that there&#8217;s no dependency at all on any particular threading strategy or any part of the concurrency library by the <code><em>Actor</em></code> class. The strategy could be sending our <code><em>Effects </em></code>to be evaluated by <code><em>Threads</em></code>, remote server farms, by <a href="http://citeseer.ist.psu.edu/lea00java.html" target="_blank">ForkJoin</a> tasks, or even by a <a href="http://www.mturk.com/mturk/welcome" target="_blank">Mechanical Turk</a>.</p>
<p><strong>An Actor With its Own Mailbox</strong></p>
<p>The <code><em>Actor </em></code>class by itself doesn&#8217;t yet quite achieve the kind of actors model you see implemented in Erlang or Scala. The important difference is that the <code><em>Actor </em></code>above can process multiple &#8220;messages&#8221; simultaneously. This solution, then, is more general, although if an actor such as the above mutates some state, we&#8217;re likely to run into race conditions. Not to worry, it&#8217;s easy to construct the more specific case. We just add a queue and ensure that only one message is processed at a time. This construct is available as part of Functional Java, and it&#8217;s called <a href="http://functionaljava.googlecode.com/svn/artifacts/2.19/javadoc/fj/control/parallel/QueueActor.html" target="_blank">QueueActor</a>.</p>
<p>Of course, the &#8220;one thread at a time&#8221; requirement is not implemented using any blocking or synchronization. Instead, The <code><em>QueueActor </em></code>has two possible states&#8211;&#8221;suspended&#8221; or &#8220;running&#8221;&#8211;and, behind the scenes, this is enforced with an <code><em><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicBoolean.html" target="_blank">AtomicBoolean</a> </em></code>to keep it consistent in the face of concurrency. If the actor is suspended when it receives a message, it becomes running and its <code><em>Effect </em></code>is immediately handed off to its <code><em>Strategy</em></code>. If it&#8217;s already running, then callers will leave a message on its queue. The <code><em>QueueActor's</em></code> <code><em>Effect</em></code> is a concurrent, threadless recursion (i.e. it uses a <code><em>Strategy </em></code>rather than a <code><em>Thread</em></code>) that completely empties the queue, then puts the <code><em>QueueActor's</em></code> state back to &#8220;suspended&#8221;.</p>
<p><code><em>QueueActor</em></code> puts some sanity into managing locally mutable state within an actor&#8217;s Effect, since it&#8217;s ensured that the state can only be mutated by one thread at a time. It is guaranteed to act on its messages in some unspecified order, but is otherwise semantically equivalent to <code><em>Actor</em></code>.</p>
<p><strong>The Obligatory Example<br />
</strong></p>
<p>We now have a light-weight  implementation of the actor model, in Java. Don&#8217;t believe it? Have a look at this implementation of the canonical Ping-Pong example (imports omitted), and compare it to similar examples for <a href="http://www.erlang.org/doc/getting_started/conc_prog.html" target="_blank">Erlang</a> and <a href="http://lamp.epfl.ch/~phaller/doc/ActorsTutorial.html" target="_blank">Scala</a>.</p>
<p>First, a <code><em>Ping</em></code> actor that sends a number of pings to a given <code><em>Pong</em></code> actor, waiting for a pong reply each time before sending the next ping.</p>
<pre style="background:#242424;color:#f6f3e8;"><span>
  <span style="color:#cae682;">public</span> <span style="color:#cae682;">class</span> Ping
    {<span style="color:#cae682;">private</span> <span style="color:#cae682;">final</span> Pong pong;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">final</span> Actor&lt;Pong&gt; ping;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">final</span> Actor&lt;<span style="color:#e5786d;">Integer</span>&gt; cb;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">volatile</span> <span style="color:#cae682;">int</span> n;

<span style="color:#cae682;">     </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> Ping(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Strategy&lt;Unit&gt; s, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#cae682;">int</span><span style="color:#cae682;"> i, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Pong pong, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#cae682;">int</span><span style="color:#cae682;"> id, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Actor&lt;</span><span style="color:#e5786d;">Integer</span><span style="color:#cae682;">&gt; callback)</span>
       <span style="color:#cae682;">{</span>n = i;
      <span style="color:#cae682;">  this</span>.pong = pong;
        cb = callback;
        ping = actor
          (s, <span style="color:#8ac6f2;">new</span> Effect&lt;Pong&gt;() <span style="color:#cae682;">{</span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> e(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Pong pong)</span>
            <span style="color:#cae682;">{</span>n--;
          <span style="color:#8ac6f2;">   if</span> (n &gt; <span style="color:#e5786d;">0</span>)
                pong.act(Ping.<span style="color:#cae682;">this</span>);
             <span style="color:#8ac6f2;">else</span> <span style="color:#99968b;"><em>// Done. Notify caller.</em></span>
                cb.act(id);<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>);<span style="color:#cae682;">}</span>

    <span style="color:#99968b;"><em>// Commence pinging</em></span>
<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> P1&lt;Unit&gt; start()</span>
      <span style="color:#cae682;">{</span><span style="color:#8ac6f2;">return</span> pong.act(<span style="color:#cae682;">this</span>);<span style="color:#cae682;">}</span>

    <span style="color:#99968b;"><em>// Receive a pong</em></span>
<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> P1&lt;Unit&gt; act(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Pong p)</span>
      <span style="color:#cae682;">{</span><span style="color:#8ac6f2;">return</span> ping.act(p);<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>
</span>
</pre>
<p>The Pong actor simply receives ping messages and responds.</p>
<pre style="background:#242424;color:#f6f3e8;"><span>
  <span style="color:#cae682;">public</span> <span style="color:#cae682;">class</span> Pong
    <span style="color:#cae682;">{</span><span style="color:#cae682;">private</span> <span style="color:#cae682;">final</span> Actor&lt;Ping&gt; p;

<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> Pong(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Strategy&lt;Unit&gt; s)</span>
      <span style="color:#cae682;">{</span>p = actor(s, <span style="color:#8ac6f2;">new</span> Effect&lt;Ping&gt;()
        <span style="color:#cae682;">{</span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> e(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Ping m)</span>
          <span style="color:#cae682;">{</span>m.act(Pong.<span style="color:#cae682;">this</span>);<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>);<span style="color:#cae682;">}</span>

    <span style="color:#99968b;"><em>// Receive a ping</em></span>
<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> P1&lt;Unit&gt; act(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> Ping ping)</span>
      <span style="color:#cae682;">{</span><span style="color:#8ac6f2;">return</span> p.act(ping);<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>
</span>
</pre>
<p>And here&#8217;s the main program that uses the <code><em>Ping </em></code>and <code><em>Pong </em></code>actors. There&#8217;s only one <code><em>Pong </em></code>actor that responds to any number of <code><em>Ping</em></code> actors pinging it concurrently. There&#8217;s also a further <code><em>QueueActor </em></code>that is contacted by each <code><em>Ping </em></code>actor once that <code><em>Ping </em></code>actor has done its work. The example uses a thread pool to back its <code><em>Strategy</em></code>. When all the <code><em>Ping</em></code> actors have sent all their pings and received all their pongs, the program is terminated by shutting down the thread pool.</p>
<pre style="background:#242424;color:#f6f3e8;"><span>
  <span style="color:#cae682;">public</span> <span style="color:#cae682;">class</span> PingPong
    <span style="color:#cae682;">{</span><span style="color:#cae682;">private</span> <span style="color:#cae682;">final</span> <span style="color:#cae682;">int</span> actors;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">final</span> <span style="color:#cae682;">int</span> pings;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">final</span> Strategy&lt;Unit&gt; s;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">final</span> Actor&lt;<span style="color:#e5786d;">Integer</span>&gt; callback;
    <span style="color:#cae682;"> private</span> <span style="color:#cae682;">volatile</span> <span style="color:#cae682;">int</span> done;

<span style="color:#cae682;">    </span><span style="color:#cae682;"> public</span><span style="color:#cae682;"> PingPong(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> ExecutorService pool, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#cae682;">int</span><span style="color:#cae682;"> actors, </span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#cae682;">int</span><span style="color:#cae682;"> pings)</span>
       <span style="color:#cae682;">{</span><span style="color:#cae682;">this</span>.actors = actors;
      <span style="color:#cae682;">  this</span>.pings = pings;
        s = Strategy.executorStrategy(pool);

      <span style="color:#99968b;"><em>  // This actor gives feedback to the user that work is being done</em></span>
      <span style="color:#99968b;"><em>  // and also terminates the program when all work has been completed.</em></span>
        callback = QueueActor.queueActor
          (s, <span style="color:#8ac6f2;">new</span> Effect&lt;<span style="color:#e5786d;">Integer</span>&gt;() <span style="color:#cae682;">{</span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> e(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#e5786d;">Integer</span><span style="color:#cae682;"> i)</span>
            <span style="color:#cae682;">{</span>done++;
          <span style="color:#8ac6f2;">   if</span> (done &gt;= actors)
               <span style="color:#cae682;">{</span><span style="color:#e5786d;">System</span>.out.println(<span style="color:#95e454;"><em>"All done."</em></span>);
                pool.shutdown();<span style="color:#cae682;">}</span>
<span style="color:#8ac6f2;">             else</span> <span style="color:#8ac6f2;">if</span> (actors &lt; <span style="color:#e5786d;">10</span> || done % (actors / <span style="color:#e5786d;">10</span>) == <span style="color:#e5786d;">0</span>)
            <span style="color:#e5786d;">         System</span>.out.println(<span style="color:#e5786d;">MessageFormat</span>.format
                       (<span style="color:#95e454;"><em>"{0} actors done ({1} total pongs)."</em></span>, done, pings * done));<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>)
          .asActor();<span style="color:#cae682;">}</span>

<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">static</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> main(</span><span style="color:#cae682;">final</span><span style="color:#cae682;"> </span><span style="color:#e5786d;">String</span><span style="color:#cae682;">[] args)</span>
      <span style="color:#cae682;">{</span><span style="color:#cae682;">final</span> <span style="color:#cae682;">int</span> actors  = <span style="color:#e5786d;">Integer</span>.parseInt(args[<span style="color:#e5786d;">0</span>]);
      <span style="color:#cae682;"> final</span> <span style="color:#cae682;">int</span> pings   = <span style="color:#e5786d;">Integer</span>.parseInt(args[<span style="color:#e5786d;">1</span>]);
      <span style="color:#cae682;"> final</span> <span style="color:#cae682;">int</span> threads = <span style="color:#e5786d;">Integer</span>.parseInt(args[<span style="color:#e5786d;">2</span>]);
      <span style="color:#8ac6f2;"> new</span> PingPong(<span style="color:#e5786d;">Executors</span>.newFixedThreadPool(threads), actors, pings).start();<span style="color:#cae682;">}</span>

<span style="color:#cae682;">    </span><span style="color:#cae682;">public</span><span style="color:#cae682;"> </span><span style="color:#cae682;">void</span><span style="color:#cae682;"> start()</span>
      <span style="color:#cae682;">{</span><span style="color:#99968b;"><em>// We will use one Pong actor...</em></span>
      <span style="color:#cae682;"> final</span> Pong pong = <span style="color:#8ac6f2;">new</span> Pong(s);
       <span style="color:#99968b;"><em>// ...and an awful lot of Ping actors.</em></span>
      <span style="color:#8ac6f2;"> for</span> (<span style="color:#cae682;">int</span> i = <span style="color:#e5786d;">1</span>; i &lt;= actors; i++)
         <span style="color:#cae682;">  {</span><span style="color:#8ac6f2;">new</span> Ping(s, pings, pong, i, callback).start();
        <span style="color:#8ac6f2;">    if</span> (actors &lt; <span style="color:#e5786d;">10</span> || i % (actors / <span style="color:#e5786d;">10</span>) == <span style="color:#e5786d;">0</span>)
              <span style="color:#e5786d;"> System</span>.out.println(<span style="color:#e5786d;">MessageFormat</span>.format(<span style="color:#95e454;"><em>"{0} actors started."</em></span>, i));<span style="color:#cae682;">}</span><span style="color:#cae682;">}</span><span style="color:#cae682;">}</span>
</span>
</pre>
<p>What follows is an example run of this Java program, with a million concurrent <code><em>Ping</em></code> actors pinging 7 times each. Each actor takes about 300 bytes of memory, so we need a sizable heap for one million of them, but 19 real Java Threads handle this quite nicely on my 8-core machine.</p>
<pre style="background:#242424;color:#f6f3e8;">$ time java -Xmx600m -cp ../../../.build/classes/src:. concurrent.PingPong 1000000 7 19
100,000 actors started.
200,000 actors started.
300,000 actors started.
400,000 actors started.
500,000 actors started.
600,000 actors started.
700,000 actors started.
800,000 actors started.
900,000 actors started.
1,000,000 actors started.
100,000 actors done (700,000 total pongs).
200,000 actors done (1,400,000 total pongs).
300,000 actors done (2,100,000 total pongs).
400,000 actors done (2,800,000 total pongs).
500,000 actors done (3,500,000 total pongs).
600,000 actors done (4,200,000 total pongs).
700,000 actors done (4,900,000 total pongs).
800,000 actors done (5,600,000 total pongs).
900,000 actors done (6,300,000 total pongs).
All done.

real    1m16.376s
user    3m53.612s
sys     0m10.924s
</pre>
<p>As you see, these simple tools, built on basic components of the Java 5 concurrency library, paired with powerful abstractions from programming in functional style, makes it seem like we have millions of tasks running concurrently. We get a virtually unbounded degree of concurrency while never seeing any locks nor performing any blocking calls. The number of concurrent tasks is limited only by the size of your heap.</p>
<p>Again, everything you see here, and more, has been released just recently as part of the Functional Java library. So head over to their <a href="http://functionaljava.org/download">download page</a> and put those extra cores to use with Java, today!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/apocalisp.wordpress.com/52/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/apocalisp.wordpress.com/52/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=52&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2008/07/28/threadless-concurrency-with-actors/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>
	</item>
		<item>
		<title>Happy Independence Day</title>
		<link>http://apocalisp.wordpress.com/2008/07/03/happy-independence-day/</link>
		<comments>http://apocalisp.wordpress.com/2008/07/03/happy-independence-day/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 16:08:20 +0000</pubDate>
		<dc:creator>Rúnar</dc:creator>
				<category><![CDATA[Philosophy]]></category>

		<guid isPermaLink="false">http://apocalisp.wordpress.com/?p=43</guid>
		<description><![CDATA[Celebrate independence day by thinking for yourself.

       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=43&subd=apocalisp&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Celebrate independence day by thinking for yourself.</p>
<p><span style="text-align:center; display: block;"><a href="http://apocalisp.wordpress.com/2008/07/03/happy-independence-day/"><img src="http://img.youtube.com/vi/mWv5VZWlwRQ/2.jpg" alt="" /></a></span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/apocalisp.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/apocalisp.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/apocalisp.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/apocalisp.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/apocalisp.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/apocalisp.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/apocalisp.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/apocalisp.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/apocalisp.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/apocalisp.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/apocalisp.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/apocalisp.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=apocalisp.wordpress.com&blog=1150675&post=43&subd=apocalisp&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://apocalisp.wordpress.com/2008/07/03/happy-independence-day/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fe3ec04d1300b66c6afd32bf56979e54?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Rúnar</media:title>
		</media:content>

		<media:content url="http://img.youtube.com/vi/mWv5VZWlwRQ/2.jpg" medium="image" />
	</item>
	</channel>
</rss>