<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Monomorphic &#187; programming languages</title>
	<atom:link href="http://www.monomorphic.org/wordpress/tag/programming-languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.monomorphic.org/wordpress</link>
	<description>Conceptual meandering</description>
	<lastBuildDate>Wed, 11 Jan 2012 02:07:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Platonism and the dominant decomposition</title>
		<link>http://www.monomorphic.org/wordpress/platonism-and-the-dominant-decomposition/</link>
		<comments>http://www.monomorphic.org/wordpress/platonism-and-the-dominant-decomposition/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 17:33:43 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[epistemology]]></category>
		<category><![CDATA[nietzsche]]></category>
		<category><![CDATA[ontologies]]></category>
		<category><![CDATA[platonism]]></category>
		<category><![CDATA[poplar]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=1020</guid>
		<description><![CDATA[I&#8217;m in Portland, Oregon for the SPLASH conference. There&#8217;s a lot of energy and good ideas going around. I gave a talk about my project, Poplar, at the FREECO workshop. At the same workshop there was a very interesting talk given by Klaus Ostermann, outlining some of the various challenges facing software composition. He linked [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m in Portland, Oregon for the <a href="http://www.splashcon.org">SPLASH</a> conference. There&#8217;s a lot of energy and good ideas going around.</p>
<p>I gave a talk about my project, <a href="http://www.poplar-lang.org">Poplar</a>, at the <a href="http://trese.ewi.utwente.nl/workshops/FREECO/FREECO-Onward2011/home.html">FREECO</a> workshop. At the same workshop there was a very interesting talk given by Klaus Ostermann, outlining some of the various challenges facing software composition. He linked composition of software components to concepts in classical logic, and informally divided composition into a light side and a dark side. On the light side are ideal concepts such as monotonicity (the more axioms we have, the more we can prove), absence of side effects and a single, canonical decomposition of everything. On the dark side are properties such as side effects, the absence of a single decomposition, knowledge that invalidates previously obtained theorems, and so on.</p>
<p>One of the ideas that resonated the most with me is the tyranny of the dominant decomposition. (For instance, a single type hierarchy). Being forced to decompose a system in a single way at all times implies only having a single perspective on it. Is this not platonism coming back to haunt us in programming languages? (Ostermann did indeed say that he suspects that mathematics and the natural sciences have had too much influence on programming). What we might need now is an antiplatonism in programming: we might need subjectivist/perspectivist programming languages. If components can view their peer components in different ways, depending on their domain and their interests (i.e. what kind of stakeholders they are), we might truly obtain flexible, evolvable, organic composition.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=1020&amp;md5=aab39ff709a53e33a585f94992d25b1f" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/platonism-and-the-dominant-decomposition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What makes a good programming language?</title>
		<link>http://www.monomorphic.org/wordpress/what-makes-a-good-programming-language/</link>
		<comments>http://www.monomorphic.org/wordpress/what-makes-a-good-programming-language/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 09:00:09 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=1012</guid>
		<description><![CDATA[New programming languages are released all the time. History is littered with dead ones. There are also many long time survivors in good shape, as well as geriatric languages on life support. What makes a programming language attractive and competitive? How can we evaluate its quality? There are many different aspects of this problem. Ease [...]]]></description>
			<content:encoded><![CDATA[<p>New programming languages are released all the time. History is littered with dead ones. There are also many long time survivors in good shape, as well as geriatric languages on life support.</p>
<p>What makes a programming language attractive and competitive? How can we evaluate its quality? There are many different aspects of this problem.</p>
<p><strong>Ease of reading and writing</strong>, or: directness of the mapping between the problem in your head and the model you are creating on the computer. This can be highly domain dependent, for instance languages such as LaTeX, Matlab and R are designed with specific problems in mind and cater to users from that domain. Their limits show quickly when you try to stretch them beyond their envisioned purpose. Speaking of general programming languages, I think Python deserves to be mentioned as a language that is extremely readable and writable. It has other shortcomings though &#8211; see below. Prolog is also highly read- and writable if it suits your problem.</p>
<p><strong>Runtime performance. </strong>Arguably this is one of the few reasons to bother with using C++. For the majority of programming projects though, performance is much less of a problem than one might think, especially if one considers how close the performance of many JVM languages get to C++. When programmers think about their overall productivity and effectiveness in developing and maintaining a system, C++ is often not the best choice, obviously.</p>
<p><strong>Scalability to large teams. </strong>The key property here is: does the language do anything to help me, as a developer, work with code that other people wrote? Ease of maintenance may be strongly correlated with usability in large teams. An anti-pattern here is languages that allow for solving the same problem in a huge amount of ways with very variable syntax. For instance, Perl and C++ can lead to notoriously unmaintainable code if used carelessly. Some say that Scala also suffers from this problem. Basically, the language helps here if it prevents me from doing things that other developers might not expect, and that I might forget to document or communicate. This is why Gosling famously called Java a blue collar language; it restricts you enough to make teamwork quite practical. It even restricts the layout of your source file hierarchy. (Now we begin to see that some goals are in conflict with each other).</p>
<p><strong>Scalability to large systems. </strong>This is related to the preceding property, but whereas team scalability seems to be mainly about avoiding the creation of code fragments that surprise people other than their creators, system size scalability seems to be about avoiding the creation of code fragments that surprise other code fragments. Here one needs invariants, good type checking, static constraints of all kinds. Scripting languages like Perl and Python, lacking static typing completely, are some of the worst in this regard, since we cannot even be sure at startup time that methods we try to invoke on objects exist at all (Python).</p>
<p><strong>Scalability over time (maintainability).</strong>Â If there is both system size scalability and team scalability, then the system is also likely to be able to live for a long time without great troubles.</p>
<p><strong>Developer efficiency and rapid prototyping. </strong>Depending on the nature of the system being developed, this may depend on several different properties listed above.</p>
<p><strong>Availability of quality tools.</strong>Â Mature runtime environments, such as the JVM, have many more high quality tools and IDEs available than a language than Ruby. Mature languages also have more compilers for more different architectures available.</p>
<p>These points begin to give us an idea of how we can evaluate programming languages. However, I also believe that making a good language and making people use it is largely about luck and factors outside the design itself. Just like there&#8217;s a big step between imagining and specifying an utopian society and making that social order an actuality, there&#8217;s a big step between designing an ideal programming language and achieving widespread adoption for it. We have seen a way forward though: with generalised runtime environments such as the JVM and the CLR, we may develop and deploy languages that take advantage of a lot of existing infrastructure much more easily than before. And what I hope for is in fact that it becomes even easier to deploy new languages, and that new languages are as interoperable as possible (insofar as it doesn&#8217;t constrain their design), so that we could see more competition, more evolution and more risk taking in the PL space.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=1012&amp;md5=fff6eeac12b2b852112ea787fe9ec90f" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/what-makes-a-good-programming-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pointers in programming languages</title>
		<link>http://www.monomorphic.org/wordpress/pointers-in-programming-languages/</link>
		<comments>http://www.monomorphic.org/wordpress/pointers-in-programming-languages/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 15:21:03 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=1006</guid>
		<description><![CDATA[It is likely that few features cause as much problems as pointers and references in statement-oriented languages, such as C, C++ and Java. They are powerful, yes, and they allow us to control quite precisely how a program is to represent something. We can use them to conveniently compose objects and data without the redundancy [...]]]></description>
			<content:encoded><![CDATA[<p>It is likely that few features cause as much problems as pointers and references in statement-oriented languages, such as C, C++ and Java. They are powerful, yes, and they allow us to control quite precisely how a program is to represent something. We can use them to conveniently compose objects and data without the redundancy of replicating information massively. In languages like C they are even more powerful than in Java, since just about any part of memory can be viewed as if it were just about anything through the use of pointer arithmetic, which is indeed frightening.</p>
<p>But they also complicate reasoning about programs enormously. Both human reasoning and automated reasoning. Pointers allow any part of the program to have side effects in any other part of the program (if we have a reference to an object that originated there), and they make it very hard to reason about the properties that an object might have at a given point in time (since we generally have no idea who might hold a reference to it &#8211; it is amazing that programmers are forced to track this in their heads, more or less). In my effort to design my own language, multiple pointers to the same objects &#8211; aliases &#8211; have come back from time to time to bite me and block elegant, attractive designs. I believe that this is a<em> very </em>hard problem to design around. Aliased pointers set up communication channels between arbitrary parts of a program.</p>
<p>Nevertheless attempts have been made, in academia and in research labs, to solve this problem. Fraction-based permissions track how many aliases exist and endow each alias with specific permissions to access the object that is referred to. Ownership analysis forces access to certain objects to go through special, &#8220;owning&#8221; objects. Unique or &#8220;unshared&#8221; pointers in some language extensions restrict whether aliases may be created or not. But so far no solution has been extremely attractive and convenient, and none has made it into mainstream languages. (<del>I know that someone</del> Philipp HallerÂ made a <a href="http://lamp.epfl.ch/~phaller/readme_uniqueness.html">uniqueness plugin for the Scala compiler</a>, but it is not in wide use, I believe.)</p>
<p>If we are to attempt further incremental evolution of the C-family languages, aliased pointers are one of the most important issues we can attack in my opinion.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=1006&amp;md5=45794a6be4c8cfc42a0be706de7eb18b" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/pointers-in-programming-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type theory</title>
		<link>http://www.monomorphic.org/wordpress/type-theory/</link>
		<comments>http://www.monomorphic.org/wordpress/type-theory/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 21:23:32 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=774</guid>
		<description><![CDATA[One of the most interesting things I&#8217;ve been studying in the past year has been type theory. I feel that type theory is an area where a lot of separate fields can come together in a good design. In strongly typed languages, language implementation efficiency, syntax and language semantics all leave essential marks in the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most interesting things I&#8217;ve been studying in the past year has been type theory. I feel that type theory is an area where a lot of separate fields can come together in a good design. In strongly typed languages, language implementation efficiency, syntax and language semantics all leave essential marks in the type system, and conversely, flaws in the type system can impair all of them.</p>
<p>Type theory was invented by <a href="http://en.wikipedia.org/wiki/Bertrand_Russell">Bertrand Russell</a> as a solution to his own <a href="http://en.wikipedia.org/wiki/Russell%27s_paradox">well-known paradox</a>, which cast doubt on the foundations of mathematics upon its discovery.Â  The problem posed by the paradox is essentially: given a set of all sets that do not contain themselves, does that set contain itself? Another interesting problem arising from self-referentialism. Type theory resolves this issue by classifying the members of sets according to some type, and in such a system a set definition like this one is not permissible.</p>
<p>(The drama surrounding Russell&#8217;s paradox and <a href="http://en.wikipedia.org/wiki/Principia_Mathematica">Principia Mathematica</a> can now also be read in the form of a &#8220;logicomix&#8221; <a href="http://www.amazon.com/Logicomix-Search-Truth-Apostolos-Doxiadis/dp/1596914521">graphic novel</a> by Doxiadis et al!)</p>
<p>Type theory did not end its life as a metamathematical hack that solved a set theoretical problem. <a href="http://cacm.acm.org/magazines/2010/2/69367-type-theory-comes-of-age/fulltext">It has now come of age.</a> Type systems are, of course, at the heart of the development of many modern programming languages. Java, C#, Lisp, ML and Haskell are but a few of the ones that depend completely on a nontrivial type system. (Even though C and C++ have type systems, they are kept in a crippled state by the fact that the programmer is allowed to ignore their commandments at will.)</p>
<p>What is the benefit of a type system in a programming language? In the words of <a href="http://www.cis.upenn.edu/~bcpierce/">Benjamin Pierce</a>,</p>
<blockquote><p>A type system is a syntactic method for automatically checking the  absence of certain erroneous behaviors by classifying program phrases  according to the kinds of values they compute.</p></blockquote>
<p>So, generally speaking, a type system helps distinguish correct programs from incorrect programs. The parser also does part of this job, of course, since many programs that cannot compile will not parse. But there are programs that are syntactically correct but at the same time semantically unsound according to the evaluation rules of the language. Consider the following Java statement:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">T x <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> U<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is clearly syntactically acceptable, but is it semantically valid? In order for it to be, either U needs to be the same type as T, or a subtype of T. But Java programmers are free to define new subtypes of existing types in source code. In order for the parser to check the correctness of the assignment, it seems that either the grammar would become enormously large, or it would place awkward restrictions on Java syntax. So we leave more refined aspects of correctness to the type checker, which is applied later, post-parsing.</p>
<p>What does a type system look like? As used in programming languages today, a type system consists of a set of <em>typing judgments</em>. The judgments can be stacked together to form the <em>typing derivation</em> of an expression in the language. Every expression normally has a unique typing derivation, and when a type can be derived, we call the expression well typed. As Benjamin Pierce&#8217;s formulation suggests, each typing rule makes some assumptions about the kinds of values that constitute the various parts of an expression, and then says something about the kind of value that will be computed by the expression as a whole.</p>
<p>Type systems can look prohibitive at first sight, since they use quite a few algebraic symbols. But in general, the structure and interrelationships between the symbols is the most important thing to discern from them, and focussing on this aspect makes reading them much easier.</p>
<p>In the following mini-type system, typing judgments will have the form <img src='http://www.monomorphic.org/wordpress/wp-content/latex/4e7/4e703057592a70077f2894e4f4643f16-ffffff-000000-0.png' alt='\Gamma \vdash t: T' title='\Gamma \vdash t: T' class='latex' />, where <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' /> is a <em>typing context</em>, <img src='http://www.monomorphic.org/wordpress/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-ffffff-000000-0.png' alt='t' title='t' class='latex' /> is a <em>term</em>, and <img src='http://www.monomorphic.org/wordpress/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-ffffff-000000-0.png' alt='T' title='T' class='latex' /> is a type. Terms are essentially fragments of the source code, and from smaller terms we can construct larger terms using syntactic forms. So for example, if <strong>a</strong>, <strong>b</strong> and <strong>c</strong> are terms, then we might use the if-syntax to construct the larger term</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>b<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>c<span style="color: #009900;">&#125;</span></pre></div></div>

<p>in a java-like language. The typing context <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' /> is essentially just a map, mapping already typed terms to the types assigned to them. As we type increasingly large terms, we gradually add more information to the typing context. The symbol <img src='http://www.monomorphic.org/wordpress/wp-content/latex/7f5/7f5788d56bd683ffba59eed8ad6ecbe6-ffffff-000000-0.png' alt='\vdash' title='\vdash' class='latex' /> is just notational convention and has no particular meaning. The judgment <img src='http://www.monomorphic.org/wordpress/wp-content/latex/4e7/4e703057592a70077f2894e4f4643f16-ffffff-000000-0.png' alt='\Gamma \vdash t: T' title='\Gamma \vdash t: T' class='latex' /> is read as &#8220;term t has type T in typing context <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' />&#8220;.</p>
<p>A fragment of a minimal system might look like the following.</p>
<img src='http://www.monomorphic.org/wordpress/wp-content/latex/d7a/d7a8b6c7986eb44d76281142c2a69410-ffffff-000000-0.png' alt='  \inferrule  {  \Gamma \vdash t_1 : T  \and  \text{vartype}(v) = U  \and  T &lt;: U  }  {  \Gamma \vdash v = t_1 : U  }  {  \text{T-ASGN}  }  \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Int}  \and  \Gamma \vdash t_2 : \text{Int}  }  {  \Gamma \vdash t_1~\textsf{intBinop}~t_2 : \text{Int}  }  {  \text{T-BINOP}  }   \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Bool}  \and  \Gamma \vdash t_2 : T_0  \and  \Gamma \vdash t_3 : T_0  }  {  \Gamma \vdash \text{if}~t_1~\text{then}~t_2~\text{else}~t_3 : T_0  }  {  \text{T-IF}  }  ' title='  \inferrule  {  \Gamma \vdash t_1 : T  \and  \text{vartype}(v) = U  \and  T &lt;: U  }  {  \Gamma \vdash v = t_1 : U  }  {  \text{T-ASGN}  }  \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Int}  \and  \Gamma \vdash t_2 : \text{Int}  }  {  \Gamma \vdash t_1~\textsf{intBinop}~t_2 : \text{Int}  }  {  \text{T-BINOP}  }   \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Bool}  \and  \Gamma \vdash t_2 : T_0  \and  \Gamma \vdash t_3 : T_0  }  {  \Gamma \vdash \text{if}~t_1~\text{then}~t_2~\text{else}~t_3 : T_0  }  {  \text{T-IF}  }  ' class='latex' />
<p>Three typing rules have been introduced here, T-ASGN for assignments, T-IF for if-statements, and T-BINOP for binary operations on integers, like + and -.<br />
Every rule has the same structure: above the line, assumptions that must hold true for the rule to be applied; below the line, the conclusion we may draw if the assumptions are true.</p>
<p>Above, the following example was given.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">T x <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> U<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Using T-ASGN, we can type an assignment statement like this one. The assumption T &lt;: U says that T is a subtype of U. We have also assumed a lookup function <strong>vartype</strong> which gives the declared type of the variable we assign to. In English, we can read the rule T-ASGN as saying: &#8220;Assuming that the type of the right hand side is a subtype of, or the same type as, the declared type of the variable, the assignment will evaluate to a value that has the declared type of the variable&#8221;. Of course not all programming languages implement assignment in this way.</p>
<p>In the T-BINOP rule, we must of course substitute an actual operation on integers for the special word <strong>intBinop</strong> for it to be valid. For addition, the rule can be read as &#8220;Assuming that the left hand side and the right hand side are both integers, then the result is also an integer&#8221;.</p>
<p>Finally, the T-IF rule says &#8220;assuming that the truth condition is a boolean, and that the two alternative paths have the same type, then the if-statement evaluates to the same type as that of the two conditional paths&#8221;.</p>
<p>The shape of the type system follows the evaluation rules for the language quite closely, so that a well-typed term is also always possible to evaluate. We can thus avoid accepting programs that might get stuck without any valid evaluation rule at some point. (Consider what happens in Python if you try to invoke a method on an object, and the object doesn&#8217;t have a method with that name.) We are not limited to tracking just the kinds of values being computed in a type system. We can track various kinds of safety properties, such as memory regions being violated, as well, or we may track multiple properties at once. <a href="http://en.wikipedia.org/wiki/Effect_system">Type and effect systems </a>is one way of tracking both values being computed and resource violations.</p>
<p>A good source of more information on type systems applied to programming languages would be Benjamin Pierce&#8217;s <a href="http://www.amazon.com/Types-Programming-Languages-Benjamin-Pierce/dp/0262162091/ref=cm_cr_pr_product_top">Types and Programming Languages</a>.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=774&amp;md5=d3d903e1d4d5f0b5f1bc76ddf9d7f12c" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/type-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doing generality right</title>
		<link>http://www.monomorphic.org/wordpress/doing-generality-right/</link>
		<comments>http://www.monomorphic.org/wordpress/doing-generality-right/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 02:00:02 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[applications]]></category>
		<category><![CDATA[HCI]]></category>
		<category><![CDATA[platforms]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=558</guid>
		<description><![CDATA[Many software developers, while making a tool to solve a specific problem, heed the siren call of generality. By making a few specific changes, they can turn the tool into a general framework for solving a larger class of problems. And then, with a few more changes, an even larger class of problems, and so [...]]]></description>
			<content:encoded><![CDATA[<p>Many software developers, while making a tool to solve a specific problem, heed the siren call of generality. By making a few specific changes, they can turn the tool into a general framework for solving a larger class of problems. And then, with a few more changes, an even larger class of problems, and so on. This often turns into a trap, and there is a risk that the end of the line is an over-generalised tool that isn&#8217;t very good at solving any problem, because the specificity that was present in the first place was part of why it was powerful. In this way, constraints can equal freedom.</p>
<p>Sometimes, though, the generalizers get it right. These are often moments of exceptional and lasting innovation. One example of such a system is the fabulously influential (but today, not that widely used) programming language <a href="http://www.smalltalk.org">Smalltalk</a>. Invented by the former jazz guitarist and subsequent Turing award winner <a href="http://en.wikipedia.org/wiki/Alan_Kay">Alan Kay</a>, Smalltalk was released as one of the first true object-oriented programming languages in 1980. It is probably still ahead of its time. It runs on a virtual machine, it has reflection, everything is an object, and the separation between applications is blurred in favour of a big object box. On running <a href="http://www.squeak.org/">Squeak</a>, a popular Smalltalk implementation, with its default system image today, users discover that all the objects on the screen, including the IDE to develop and debug objects, appear to follow the same rules. No objects seem to have special privileges.</p>
<p>Another such system is an application that used to be shipped on Mac computers in the distant past, <a href="http://en.wikipedia.org/wiki/HyperCard">Hypercard</a>. Hypercard enabled ordinary users to create highly customized software using the idea of filing cards in a drawer as the underlying model, blurring the line between end users and developers through its accessibility. I haven&#8217;t had the privilege to use it myself, but it seems like this was as powerful as it was because it served up a homogenous and familiar model, where everything was a card, and yet the cards had considerable scope for modification and special features. Even though, in some ways, this system appears to be a database, the cards didn&#8217;t need to have the same format, for instance. (Are we seeing this particular idea being recycled in a more enterprisey form in <a href="http://couchdb.apache.org/">CouchDB</a>?)</p>
<p>There are more examples of successful highly general design: the Unix file system, TCP/IP sockets and so on. They all have in common that they are easy to think about as a mental model, since a universal set of rules apply to all objects, they scale well in different directions when used for different purposes, and they give the user a satisfying sense of empowerment, <a href="http://www.monomorphic.org/wordpress/making-playtime-useful-with-color-filling-games/">blurring the line between work and play</a> to draw on the user&#8217;s natural creativity. Successful general systems are the ones that can be easily applied in quite varied situations without tearing in the seams.</p>
<p>While not widely used by industrial programmers today, Smalltalk was incredibly influential. In 1981 Objective-C was created by Brad Cox and Tom Love, directly inspired by what the Smalltalk designers had done. Objective-C was subsequently used as the language of choice for NeXTStep, and later for Apple&#8217;s MacOS X when Apple bought NeXT. Today it&#8217;s seeing a big surge in popularity thanks to devices like the iPhone, on which it is also used. In 1995 Java was introduced, owing a great deal of its design to Objective-C, but also introducing features such as a universal virtual machine and garbage collection, which Objective-C didn&#8217;t have at the time. In some sense, both Objective-C and Java are blends of the C-family languages and Smalltalk. Tongue in cheek, we might say that it seems evolution in industrial programming these days consists of finding blends that contain less of the C model and more of smalltalk or functional programming.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=558&amp;md5=4284f16331bd64cda77d45a7dcf90333" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/doing-generality-right/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overloading words in research and programming</title>
		<link>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/</link>
		<comments>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 06:16:22 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[human condition]]></category>
		<category><![CDATA[Natural language]]></category>
		<category><![CDATA[ontologies]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=540</guid>
		<description><![CDATA[In research and academia, one of the fundamental activities is the invention and subsequent examination of new concepts. For concepts, we need names. One way of making a name is stringing words together until the meaning is sufficiently specific. E.g. &#8220;morphism averse co-dependent functor substitutions in virtual machine transmigration systems&#8221;. Thus the abstruse academic research [...]]]></description>
			<content:encoded><![CDATA[<p>In research and academia, one of the fundamental activities is the invention and subsequent examination of new concepts. For concepts, we need names.</p>
<p>One way of making a name is stringing words together until the meaning is sufficiently specific. E.g. &#8220;morphism averse co-dependent functor substitutions in virtual machine transmigration systems&#8221;. Thus the abstruse academic research paper title is born.</p>
<p>Sciences sometimes give new meanings to existing words. This could be called overloading, following the example of object-oriented programming. E.g. a &#8220;group&#8221; in mathematics is something different from the everyday use of the term. A &#8220;buffer&#8221; in chemistry is something different from a software or hardware buffer, even though a fragment of similarity is there. And so on. This overloading of words gives newcomers to the field a handle on what is meant, but full understanding is still impossible without understanding the actual definitions being employed.</p>
<p>Sometimes new terms can be created using inventors&#8217; names and everyday words. E.g. a &#8220;Lie group&#8221; or the &#8220;Maxwell equations&#8221;, or &#8220;Curry-Howard correspondence&#8221;. This is potentially useful, but perhaps not something you can do freely with your own research without seeming like you&#8217;re trying to inflate your ego excessively. (Even though researchers love inflating their egos, nobody wants to admit it.)</p>
<p>There&#8217;s a similar problem in software development. When we invent names of functions, classes and variables, the lack of words becomes very clear. Intuitively, what is an &#8220;adapter registry&#8221;? An &#8220;observer list&#8221;? Or an &#8220;observer list mediation adapter?&#8221; My feeling is that we often end up compounding abstract words because we have no better choice. And here lies a clue to some of the apparent impermeability of difficult source code. We need better ways of making names. We&#8217;re inventing ideas faster than our language can stretch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A wikipedia of algorithms</title>
		<link>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/</link>
		<comments>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 01:21:52 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[social media]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=429</guid>
		<description><![CDATA[Here&#8217;s something I&#8217;ve wanted to see for some time, but probably don&#8217;t have time to work on myself. It would be nice if there was a wikipedia-like web site for code and algorithms. Just the common ones to start with, but perhaps more specialised ones over time. Of course the algorithms should be available in [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s something I&#8217;ve wanted to see for some time, but probably don&#8217;t have time to work on myself.</p>
<p>It would be nice if there was a wikipedia-like web site for code and algorithms. Just the common ones to start with, but perhaps more specialised ones over time. Of course the algorithms should be available in lots of different languages. This would in fact be one of the main points, so that people could compare good style and see how things should be done for different languages. In addition, there should be an in-browser editor, just like on Wikipedia (but perhaps with syntax highlighting) so people can make changes easily.</p>
<p>Furthermore, there should be unit tests for every algorithm, and these should be user-editable in the same way as the main code. In an ideal world, the web site would automatically run the unit tests every time there&#8217;s a change to some algorithm and check in a new version of the code to a versioned repository. People could then trust with reasonable confidence that the code is valid and safe. However, if the system were to be as open as Wikipedia is, such a system wouldn&#8217;t work, since users could write unit tests with malicious code. So I suspect volunteers would have to download, inspect, and run the unit tests regularly, and perhaps there would be a meta-moderation system of some kind, allowing senior members to promote changes to the official repository. In the meantime, everybody should be allowed to see and edit changes on the wiki immediately, but they would be marked as &#8220;untested&#8221; or &#8220;unsafe&#8221;.</p>
<p>User interface would be very important since this kind of site needs to be fun and easy to use regularly.</p>
<p>Has this kind of project already been carried out by someone? I can find some things by googling. <a href="http://www.thecodewiki.com/">The Code Wiki</a> appears to once have been a wikipedia of code, but it seems defunct, C# only, and now they&#8217;re selling a book with the contents of the site! <a href="http://www.algorithm-code.com/">Algorithm Wiki</a> has many algorithms in different languages, but the user interface is awkward and littered with obstructive advertising, the code is hard to browse, and it doesn&#8217;t make for a usable quick reference. They seem to have gotten off to a good start though. Any others?</p>
<p>Edit: <a href="http://rosettacode.org/wiki/Main_Page">Rosetta Code</a> seems to be the most mature and useful such site out there today.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Where is Java going?</title>
		<link>http://www.monomorphic.org/wordpress/where-is-java-going/</link>
		<comments>http://www.monomorphic.org/wordpress/where-is-java-going/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 05:51:16 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[platforms]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=391</guid>
		<description><![CDATA[Today, Java is one of the most popular programming languages. Introduced in 1995, it rests on a tripod of the language itself, its libraries, and the JVM. In the TIOBE programming language league charts, it has been at the top for as long as the measurements have been made (since 2002), overtaken by C only [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/11/creative.jpg"><img class="aligncenter size-medium wp-image-401" title="creative" src="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/11/creative-225x300.jpg" alt="creative" width="225" height="300" /></a></p>
<p>Today, Java is one of the most popular programming languages. Introduced in 1995, it rests on a tripod of the language itself, its libraries, and the JVM. In the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE programming language league charts</a>, it has been at the top for as long as the measurements have been made (since 2002), overtaken by C only for a brief period due to measurement irregularities.</p>
<p>Yet not all is <a href="http://www.sun.com/">Sun</a>-shine in Java world. Sun Microsystems is about to be taken over by Oracle, pending EU approval. (EU is really dragging its feet in this matter but it seems unlikely they would really reject the merger). Larry Ellison has voiced strong support for Java and for Sun&#8217;s way of developing software, so maybe this is really not a threat by itself. But how far can the language itself go?</p>
<p>The Java language was carefully designed to be relatively easy to understand and work with. James Gosling, its creator, has <a href="http://www.computer.org/portal/web/csdl/doi/10.1109/2.587548">called it a blue collar language</a>, meaning it was designed for industrial, real world use. In a world where C++ was the de facto standard for OO programming, Java was a big step forward in terms of ease of development, with its lack of pointers and strong type system &#8211; to say nothing of its garbage collection. Many classes of common programming errors were removed altogether. However, in the interests of simplicity and clarity, some tradeoffs were made. The language&#8217;s detractors today point to problems such as excessive verbosity, the lack of closures, the limited generics, and the checked exceptions.</p>
<p>For some time there has been a lot of exciting alternative languages available on the JVM. Clojure is a Lisp dialect. Scala, the only non-Java JVM language I have used extensively, mixes the functional and object oriented paradigms. Languages like JPython and JRuby basically exist to allow scripting and interoperability with popular scripting languages on the JVM.</p>
<p>Today it seems as if the JVM and the standardized libraries will be Java&#8217;s most prominent legacy. The language itself will not go away for a long time either &#8211; considering that many companies still maintain or develop in languages like Cobol and Fortran, we will probably be maintaining Java code 30 years from now (what a sad thought!), but newer and more modern JVM languages will probably take turns being number one. The JVM and the libraries guarantee that we will be able to mix them relatively easily anyway, unless they stray too far from the standard with their custom features.</p>
<p>So in hindsight, developing this intermediate layer, this virtual machine &#8211; and disseminating it so widely &#8211; Â was a stroke of genius. Will it be that in future programming models we have even more standardized middle layers, and not just one?</p>
<p>Meanwhile, there&#8217;s a lot of debate about the process being used to shape and define Java. For a long time, Sun employed something called the Java Community Process, JCP, which was supposed to ensure openness. Some people <a href="http://www.jroller.com/scolebourne/entry/no_more_java_7">proclaim</a> that the openness has ended. To take one example, very recently, Sun announced that there will be support for closures in Java 7, after first announcing that there would be no support for closures in Java 7. The process by which this decision has been managed has been <a href="http://blogs.tedneward.com/2009/11/19/Closures+Are+Back+Again.aspx">described as</a> not being a community effort. Some aspects of Java are definitely up in the air these days.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/where-is-java-going/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming languages are about people</title>
		<link>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/</link>
		<comments>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 14:40:43 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programmer psychology]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=337</guid>
		<description><![CDATA[Programming languages are more about people and less about machines. Programming languages are about staying inside the limitations of people&#8217;s minds and their ability to keep track of and work with abstractions. If people had no such limitations, they could code in assembly language all the time. Programming languages and supporting tools and environments are [...]]]></description>
			<content:encoded><![CDATA[<p>Programming languages are more about people and less about machines.</p>
<p>Programming languages are about staying inside the limitations of people&#8217;s minds and their ability to keep track of and work with abstractions. If people had no such limitations, they could code in assembly language all the time.</p>
<p>Programming languages and supporting tools and environments are the interface between people and the raw instruction set of a computer (or a bigger entity, like a network of computers). When we design programming languages, we must take into account not only what the machine environment will do and how it changes, but also how people create the software, how they modify it, how they think about it. Maybe programming languages should even be designed with business processes in mind, in some cases.</p>
<p>But this is also a question of what kind of programmer we want to cultivate. The language shapes the programmer, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala and actors</title>
		<link>http://www.monomorphic.org/wordpress/scala-and-actors/</link>
		<comments>http://www.monomorphic.org/wordpress/scala-and-actors/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:15:03 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=298</guid>
		<description><![CDATA[Programming with actors was a new concept to me until I tried it out in Scala. It&#8217;s appears to be one of Scala&#8217;s most celebrated features, judging by the official blurb. Actors was a daunting word at first but it really ends up being a very simple concept. Actors are a programming model for concurrent [...]]]></description>
			<content:encoded><![CDATA[<p>Programming with <em>actors</em> was a new concept to me until I tried it out in Scala. It&#8217;s appears to be one of Scala&#8217;s most celebrated features, judging by the official blurb. Actors was a daunting word at first but it really ends up being a very simple concept.</p>
<p>Actors are a programming model for concurrent programming. With conventional mutex/monitor based programming in Java, say, programmers hold and release locks (the <code>synchronized</code> keyword) to achieve safe concurrency. Condition variables are used for thread communication (the <code>wait</code> and <code>notify</code> family of functions on java.lang.Object). Communication is synchronous: a typical case would be that you change some condition, invoke <code>notifyAll</code> to wake up threads waiting on that condition, and then they can take over the relevant lock and proceed to do some processing.</p>
<p>An actor is a unit of execution with an asynchronous message queue. Actors can receive messages from other actors or send messages to other actors at any time, however, the messages wait in the receiving actor&#8217;s &#8220;mailbox&#8221; until the actor has time to receive it.</p>
<p>As a simple example, let&#8217;s develop a program that converts text files to upper case using actors. The program will have an &#8220;Input&#8221; actor, an &#8220;Output&#8221; actor, and a number of &#8220;UpperCase&#8221; actors that do the processing. First the Input actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Input<span style="color: #F78811;">&#40;</span>in<span style="color: #000080;">:</span> BufferedReader<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	  <span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	    receive <span style="color: #F78811;">&#123;</span>
	      <span style="color: #0000ff; font-weight: bold;">case</span> Next <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> sender <span style="color: #000080;">!</span> Line<span style="color: #F78811;">&#40;</span>in.<span style="color: #000000;">readLine</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
	    <span style="color: #F78811;">&#125;</span>
	  <span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>It&#8217;s worth noting that the Actor system is implemented completely in the libraries, outside of the core language. Actors are not first class constructs, but sometimes look as if they were. The <code>act</code> method is where actors begin their execution. The receive method causes them to block and wait for a message, which we may pattern match on. The sender variable corresponds to whoever sent the last message received, and the &#8216;!&#8217; operator sends a message. So whenever this actor receives the <code>Next</code> message, it will respond with the next line from a buffered reader.</p>
<p>Then, the UpperCase actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Next
<span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Line<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> UpperCase<span style="color: #F78811;">&#40;</span>input<span style="color: #000080;">:</span> Actor, out<span style="color: #000080;">:</span> Actor<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
		<span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>
		<span style="color: #F78811;">&#123;</span>
			input <span style="color: #000080;">!</span> Next
			receive <span style="color: #F78811;">&#123;</span>
			<span style="color: #0000ff; font-weight: bold;">case</span> Line<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> out <span style="color: #000080;">!</span> x.<span style="color: #000000;">toUpperCase</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
			<span style="color: #F78811;">&#125;</span>
		<span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This actor is created with in- and output actors as its constructor parameters. It continually asks the input actor for a new line, converts it to upper case, and sends it to the output actor. Also note the <em>case classes</em> here, which are for pattern matching only. They are a bit like algebraic data types in Haskell.</p>
<p>Finally, the Output actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Output <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
		<span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>
		<span style="color: #F78811;">&#123;</span>
			receive <span style="color: #F78811;">&#123;</span>
			<span style="color: #0000ff; font-weight: bold;">case</span> x<span style="color: #000080;">:</span>String <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> println<span style="color: #F78811;">&#40;</span>x<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
			<span style="color: #F78811;">&#125;</span>
		<span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>And then we have to tie it all together:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Demonstration <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> reader <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> BufferedReader<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> InputStreamReader<span style="color: #F78811;">&#40;</span>System.<span style="color: #000000;">in</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> in <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Input<span style="color: #F78811;">&#40;</span>reader<span style="color: #F78811;">&#41;</span>
    in.<span style="color: #000000;">start</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> out <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Output<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    out.<span style="color: #000000;">start</span>
&nbsp;
    <span style="color: #F78811;">1</span>.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>x <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> tr <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> UpperCase<span style="color: #F78811;">&#40;</span>in, out<span style="color: #F78811;">&#41;</span>
      tr.<span style="color: #000000;">start</span>
    <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Here I abuse the foreach notation slightly to create 5 parallel text processors. Each actor runs on its own thread (though there are ways to prevent this if one wants very large numbers of actors). Now of course, the lines will probably be output in the wrong order. Another obvious shortcoming is that there is no clean shutdown protocol that terminates all the actors when the input stream is fully read. Solving these problems is outside of the scope of this article.</p>
<p>Some other interesting resources on actors: <a href="http://www.scala-lang.org/node/242">the official tutorial</a>, <a href="http://lamp.epfl.ch/~phaller/actors.html">the papers</a> (slightly more academic but accessible to the <em>monomorphic</em> reader, I imagine). Debasish highlights how actors can be used to get <a href="http://debasishg.blogspot.com/2006/11/threadless-concurrency-on-jvm-aka-scala.html">threadless concurrency</a>, Erlang-style.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/scala-and-actors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

