<?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>www.hans-eric.com &#187; Functional D</title>
	<atom:link href="http://www.hans-eric.com/category/functional-d/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hans-eric.com</link>
	<description>Hans-Eric Grönlund on software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:32:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functional D: Is Transitive Const Fundamental?</title>
		<link>http://www.hans-eric.com/2008/07/30/functional-d-is-transitive-const-fundamental/</link>
		<comments>http://www.hans-eric.com/2008/07/30/functional-d-is-transitive-const-fundamental/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 11:50:56 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[D Programming Language]]></category>
		<category><![CDATA[Functional D]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/?p=124</guid>
		<description><![CDATA[As I&#8217;ve mentioned before, a pure functional subset is forming in the D Programming Language. According to the creators of D, transitive const is a key feature to make this work.
The future of programming will be multicore, multithreaded. Languages that 	make it easy to program them will supplant languages that don&#8217;t. Transitive const is key [...]]]></description>
			<content:encoded><![CDATA[<p>As <a title="The Functional Subset of D" href="http://www.hans-eric.com/2008/05/20/the-functional-subset-of-d/">I&#8217;ve mentioned before</a>, a pure functional subset is forming in the D Programming Language. According to the creators of D, transitive const is a key feature to make this work.</p>
<blockquote><p>The future of programming will be multicore, multithreaded. Languages that 	make it easy to program them will supplant languages that don&#8217;t. Transitive const is key to bringing D into this paradigm. [...] C++ cannot be retrofitted to supporting multiprogramming in a manner that makes it accessible. D isn&#8217;t there yet, but it will be, and transitive const will be absolutely fundamental to making it work.</p></blockquote>
<p><small>[<a title="Const(FAQ)" href="http://www.digitalmars.com/d/2.0/const-faq.html#transitive-const">Quote from the D website</a>]</small></p>
<h3>What is transitive const?</h3>
<p>Just a quick explanation for those of us who doesn&#8217;t have academic terms in close memory. Transitivity is a property of some binary relations, for example equality:</p>
<p><em>if A = B and B = C, then A = C</em></p>
<p>Applied to the concept of const it means, simply put, that anything reachable from a const type is also a const. So, for a declaration <em>const int **p</em>, <em>p</em> is const, as well as <em>*p</em> and <em>**p</em>.<br />
The same is true in the case of composite types:</p>
<pre class="brush: d;">
class A {
  int f;

  void set_f(int a_value) {
    f = a_value;
  }
}

const A a = new A();
a = new A(); // error
a.a = 2; // error
a.set_a(2); // error
</pre>
<p>All three reassignments above result in compiler errors due to the fact that a is const, and anything reachable from it is also const.</p>
<h3>Why does it matter?</h3>
<p>So, in what way is transitive const fundamental to concurrent programming? Well, it isn&#8217;t. What Walter Bright and his companions refer to is the fact that pure functional programming is thread-safe by design. That is, in a pure functional language the result of a function is solely dependent on its arguments. Thus, in a code like this:</p>
<pre class="brush: d;">
val = some_func( a(), b(), c() );
</pre>
<p>functions a(), b() and c() can be safely executed concurrently in a multi-core architecture; Nothing a(), b() or c() does can affect each others results. This is not the case for imperative languages that builds on the notion of mutable and global state. With mutable state comes hidden side-effects (a(), b() or c() could change common data and cause raise conditions) that complicates multi-thread programming so much.</p>
<p>What the people behind D tries to do is to create a pure functional subset within the language. I like to refer to it as Functional D. Such a subset would allow us to write code that is thread-safe by default, all you have to do is to write Functional D code. The compiler would then be able to chisel out the functional code and fully utilize the advantages of functional programming.</p>
<h3>Immutable data and pure functions <em>are</em> fundamental</h3>
<p>To make this work we need a way to make data immutable and a way to shut down access to the global state. In D you use the <em>invariant</em> keyword to create immutable data. The <em>pure</em> keyword is used to mark functions that may only take invariant arguments, no access to the global state, and that may only invoke other pure functions. (As of this writing, the semantics of the pure keyword is not yet implemented).</p>
<pre class="brush: d;">
int g = 0;

pure int pure_func(invariant int a) {
  a = 0; // error, a is invariant
  g = 1; // error, can't write to global g
  writefln(a); // error, writefln is not pure
  return g + a; // error, can't read global g
}
</pre>
<p>How does transitive const fit into all of this? To use the intuitive definitions from Andrei Alexandrescu&#8217;s <a title="Grafting Functional Support on Top of an Imperative Language" href="http://www.digitalmars.com/d/2.0/accu-functional.pdf">slides on the functional subset of D</a>:</p>
<blockquote>
<ul>
<li>const(T) x: I can’t modify x or anything reachable from it</li>
<li>invariant(T) x: Nobody can modify x or anything reachable from it</li>
</ul>
</blockquote>
<p>Const is not strong enough to be used in the functional subset (which depends on truly immutable data), but it has one application that could be important. From Walter Bright at the D newsgroup:</p>
<blockquote><p>Const allows you to reuse code that works on invariants to work with<br />
mutables, too.</p></blockquote>
<h3>How usable is const to the functional subset?</h3>
<p>Const can be used to write code that works with data from both the imperative (mutable) and the functional (immutable) subsets. For example, the print function below.</p>
<pre class="brush: d;">
void print(const int a) {
  writefln(a);
}

const int a = 1;
print(a); // ok

invariant int b = 2;
print(b); // ok

print(3); // ok
</pre>
<p>The reason this works is that invariants and immutable data is implicitly converted to const when necessary. One can question how useful this would be in practice though, the print function above would not be invokable from the functional subset (which would require it to be pure).</p>
<p>My conclusion is that although it may very well be important, transitive const is not &#8220;absolutely fundamental to making it work.&#8221; Transitive invariant, on the other hand, is.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2008/07/30/functional-d-is-transitive-const-fundamental/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.347 seconds -->
