<?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>Lost in Technopolis</title>
	<atom:link href="http://newartisans.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://newartisans.com</link>
	<description></description>
	<lastBuildDate>Wed, 30 Nov 2011 21:57:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Japanese translation of &#8220;Git From the Bottom Up&#8221;</title>
		<link>http://newartisans.com/2011/05/git-from-bottom-up-ja/</link>
		<comments>http://newartisans.com/2011/05/git-from-bottom-up-ja/#comments</comments>
		<pubDate>Thu, 19 May 2011 22:59:57 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Git]]></category>

		<guid isPermaLink="false">http://tech.wp.newartisans.com/?p=238</guid>
		<description><![CDATA[The artice on Git that I wrote a couple years back, <a href="/2008/04/git-from-the-bottom-up">Git From the Bottom Up</a>, has been <a href="http://keijinsonyaban.blogspot.com/2011/05/git.html">translated into Japanese</a> by Noriyuki Komatsuzaki. Thank you so much, Noriyuki!]]></description>
			<content:encoded><![CDATA[<p>The artice on Git that I wrote a couple years back, <a href="/2008/04/git-from-the-bottom-up">Git From the Bottom Up</a>, has been <a href="http://keijinsonyaban.blogspot.com/2011/05/git.html">translated into Japanese</a> by Noriyuki Komatsuzaki.  Thank you so much, Noriyuki!</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2011/05/git-from-bottom-up-ja/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Letter to the Free Software Foundation</title>
		<link>http://newartisans.com/2011/04/letter-to-the-fsf/</link>
		<comments>http://newartisans.com/2011/04/letter-to-the-fsf/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 20:24:14 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.wp.newartisans.com/?p=229</guid>
		<description><![CDATA[The following is an amalgam of several letters I sent to Richard Stallman, founder of the free software movement, expressing my concern about the direction GPL licensing is taking, and why I disagree with some of the objectives of the Free Software Foundation.  The following letter comes to you, Dr. Stallman, after twelve years [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an amalgam of several letters I sent to Richard Stallman,
founder of the free software movement, expressing my concern about the
direction GPL licensing is taking, and why I disagree with some of the
objectives of the Free Software Foundation.</p>

<p><span id="more-229"></span>
The following letter comes to you, Dr. Stallman, after twelve years of
thinking about, and contributing to, free software.  I hope you will
recognize, from this, that I am writing solely out of my great love of
programming and for no other reason.</p>

<p>I have had many years to consider the elements of your software philosophy.  I
think I understand the freedom you see missing in the world, and why you have
chosen this particular path to seek it.  I also think, now, that the means you
have chosen will ultimately hurt the cause you champion, and this letter is my
explanation why.</p>

<p>All productive effort begins with the mind.  Thought is our resource against
unreasoning nature.  The thinker who makes his thoughts come alive in the
world is mankind&#8217;s benefactor.  Anything that would hamper or restrict his
thought is man&#8217;s worst enemy.  A world that does not think is only coasting on
its past achievements; it has no future.</p>

<p>Every profession is but the technical side of making these thoughts a reality.
Everyone, whether farmer or teacher or programmer, has an image in his mind of
what he wants and uses his skills to make that image real.  This trait is
universal and is how mankind stays alive.</p>

<p>The great freedom you champion is the freedom of the mind to create.  It must
not be hampered or restricted &#8212; at the peril of life.  However, there is a
difference between the freedom of the mind and the freedom of the products of
the mind.  The one is alive and must be free to act; the other is inert and
must be controlled to be useful, by the mind.</p>

<p>A pilot must have absolute control of his craft or else.  The pilot is free,
but his plane is not.  If air traffic control, to do their job, thought they
needed to control the pilot&#8217;s plane without his consent, they would quickly
learn what a horrible mess results.  But if each part is free, each in full
control of its own part of the problem, even patterns of magnificent
complexity are possible.</p>

<p>At one point, in your youth, you describe trying to fulfill your ideas in an
atmosphere of incompetence and professional jealousy.  You had good ideas, but
they were hampered by the slowness of those around you and their unwillingness
to make their work and ideas available to you.  This is indeed a problem of
today&#8217;s world: the lack of reasonable co-operation.  I have also felt such
frustrations.</p>

<p>When dealing with unskilled pilots, however, the answer is not to control
their planes and what they do with them &#8212; it is to educate the pilots.
Believing all companies to be unconcerned with co-operation, you have proposed
a means, via the GNU Public License, to remove the software they write from
their hands so that you may use it in yours.  This is an attempt to shortcut
the basic problem, but it will not lead to the world you want.</p>

<p>If you try to govern the fruits of another&#8217;s labor, it will initially increase
the activity of those who know how to use those fruits; but in the end you
will find the producers less and less willing to give you their products with
nothing offered in return.  Free software is in its heyday now, there is a lot
to go around and a lot of people who know how to use it.  Such energy
surrounds every change, until the well its tapping from begins to run dry.</p>

<p>Free software benefits from the labor of a host of volunteers who believe in
your vision: which is to turn the world of programming, and of programs, into
their hands.  That is the promise of all revolutions: To free the work of
others from their owners&#8217; control, in order to grant you the power to do what
you want with it.  It promises possession of the fruits of another&#8217;s mind
without his consent, or else he must not produce at all.</p>

<p>Your mission began with a wish for freedom, but it will end with the minds of
programmers yoked to the will of the majority.  You will ultimately destroy
the very freedom you are working toward.</p>

<p>In a society of free men, the programmer who writes code and the farmer who
grows food must trade on equal terms in order for each to get what he wants
from the other.  Neither has any claim to fruits of the other&#8217;s work without
trading for it.  If the programmer had such a right, the farmer would be his
slave; his love of farming abused by claiming a right its result without
payment.  The farmer, if he wants to go on farming, must do work on the side
to continue providing for his master.</p>

<p>In your vision, a programmer is only paid for the ancillary work he does:
training, customization, documentation.  If eighty percent of his mind&#8217;s labor
is spent making something complex enough that it needs training, why do you
recognize only twenty percent of his work as valuable?  You penalize him for
his best thought.  The more time he spends on his ideas, the less he deserves
from society in return?  There would be no reason for training or
documentation if not for those ideas.  They support the value of the rest; are
you saying they have no value themselves worth paying for?</p>

<p>You encourage a society that feeds on the programmer&#8217;s mind without
compensation, even forcing him to feed on himself &#8212; in the form of not
programming some of the time &#8212; to continue his work.  For the one who loathes
programming, this is not so terrible, he wants to avoid it anyway; but it is
an institutional prison for anyone who loves his work as a programmer and
wants to do nothing but.  You are working toward a society in which the
one-hundred percent programmer cannot support his own life.  The mind you
wanted to free from incompetence will be sacrificed to it; the mind who loves
his work will be unable to do it.</p>

<p>Your movement makes claims about reusability and sharing of ideas.  You have
nothing to reuse if I don&#8217;t write it for you.  They talk about the freedom of
ideas &#8212; at the cost of the mind who made them.  They talk about the joy of
programming in a free community.  But it is a joy that cannot survive on its
own, that must accept joyless things in order to provide that joy for others.</p>

<p>I think when you face a person who will not share his efforts with you on fair
terms you must educate him.  We must endeavor to educate society so that
people realize the benefits of working together, trading effort for effort on
equal terms.  You cannot solve the problem by appropriating the products of
one mind for the use of another.  That is the dream of communism: To take
money from the rich because the poor want to use it and don&#8217;t have any.  For a
programmer, the code he writes is his wealth, which he sells for currency in
order to participate in society.  Take that away from him and you force him
either not to be a programmer or to die as one.</p>

<p>Without educating people, it is true that you will have to do everything by
yourself.  Not wage legal war and conquer others&#8217; territories, but accept the
fact that they will not co-operate with you and carry on without them.  You
may feel this is &#8220;hampering&#8221; your mind, but it is different.  Your options may
be restricted, but only by the limits of your own resources.  Your choice of
what to do with those resources is completely free.  The world you propose,
however, would remove even this choice, since the programmer would literally
have to buy his time from society to do his job.  And if he lacks the money
and the willingness to live off his parents or anyone else?  He will find that
he has lost the choice to program, whatever his personal ability.</p>

<p>So let us do away with the GNU Public License.  Let us realize the mistake,
revert that code to the public domain, and set about teaching companies and
programmers the benefits of co-operation under the terms of fair trade, and
why they should not fear licensing their software at whatever value the market
determines.</p>

<p>Fear of piracy, theft, deadly competition &#8212; these are the bugaboos
responsible for your experiences of long ago.  Companies hold their software
and patents as if a monster were always lurking around the next corner.  That
is the war to fight: the war against ignorance.  Education, not control.  Show
a man he has nothing to fear from you and he will help you; compel him to work
for you without payment and you win only his body, not his mind.  The mind is
what you want freed.  The road to its deliverance has always been through
understanding.</p>

<p>To clarify, I want to program, not because another person wants me to and is
willing to pay me, but because I love to program.  When I use the word
&#8220;programmer&#8221;, I refer to this kind of individual.  And while it is true that I
might accept a business proposal to write software, I am also willing to
program in the absence of anyone&#8217;s personal interest in my idea.  This has
been my history.</p>

<p>So I enact my ideas in code, but now I need to live.  I could go out and sell
the <em>idea</em>, but you force me to do other work &#8212; perhaps related to the idea
&#8211; until I&#8217;ve earned enough to pursue the next one.  I disagree.  If writing
code is what I love, I should not be penalized by society for doing nothing
but that &#8212; if I can find buyers afterwards willing to pay for the code I&#8217;ve
written.  I don&#8217;t want to train, write documentation, etc.; I am a programmer.
The way I sell ideas is by contracting with interested parties via a software
license; not a license on me and my future time, but on the results of the
time I&#8217;ve already spent.  Isn&#8217;t this what copyleft wants to prevent?</p>

<p>As I said before, you deny the 100% programmer &#8212; which is who I am.  I don&#8217;t
want to waste seven weeks of every year doing other things, whether you are
willing to or not.  I have the right to decide exactly how, and on what terms,
to offer my code to the world, even if that means not offering it at all.  Why
is that once I&#8217;ve written it you should become part-owner?  If I wrote it and
deleted it the same night, would that be a crime?  If you say copyleft only
applies once I&#8217;ve published it, why?  Why is there one point at which it
leaves my ownership and becomes the possession of all?  By your philosophy &#8211;
and correct me if this is wrong &#8212; you want part title to my code the moment I
type it out.  Maybe I can charge if someone first asks me to do the typing,
but if I type it <em>for no other reason than wanting to see it done</em>, I cannot
charge for it later?</p>

<p>This is the freedom to create that you seem to want to deny.  It would produce
a world where a full-time programmer could only follow the paths dictated by
his peers, because these are the only paths they&#8217;d be willing to pay him for.
I could not, without turning to part-time work produce work that no one is
presently asking for and then survive on its success &#8212; even if they all want
it once I&#8217;m done and see how it works.  Somehow, it&#8217;s supposed to be theirs
now, and I&#8217;m the one responsible for defaulting on my electric bill?</p>

<p>The farmer analog is of course inaccurate.  His produce can only feed a
limited number of mouths.  A programmer&#8217;s work, however, can potentially help
all people who use computers at all times, far into the future.  His works
&#8220;keeps on giving&#8221; because he has sold the working form of an idea.  How can
you deny him the right to expect remuneration from society for this gift in
equal measure?  Where is the &#8220;poison&#8221; in that?  I see only that you want the
right to use something you did not create without rewarding its creator.
Explain to me how this is not your purpose.</p>

<p>You might, in the end, denounce my argument as &#8220;the individual vs. the
society&#8221;; and that private ownership of what one has done &#8212; whatever it is,
in whatever form &#8212; is destructive to the good of society.  I would say that
society has always been a collection of individuals, and that if you denigrate
the individual in favor of any conception of collective good, it will
ultimately destroy to the very good you had hoped to achieve.</p>

<p>Yes, more and more free software is being written.  That is not what I&#8217;m
addressing.  I refer to the pure programmer.  If you push him out of the
picture, the innovative quality of software &#8212; the range of scope of truly
original ideas &#8212; will deteriorate.  I don&#8217;t mean new ideas that come from
teenagers avoiding their homework in college.  I mean new ideas that come from
experienced minds, deep and far-reaching ideas, that require months and years
of labor without any interest from the public in the meantime.  If you end the
possibility of a financial payoff at the end of such an effort, you will end
all such efforts.</p>

<p>Free software seems bright and rosy, but there are reasons, I believe, why
there are few high-quality programs of an original character that have been
developed at the expense of no one other than the programmer himself.  Your
&#8220;growing&#8221; free society is growing by feeding on the money and time of others:
companies, parents paying for their children&#8217;s time, money earned by the
programmer at another job, etc.  It is parasitic, feeding off society&#8217;s wealth
and unable to sustain itself.  Look at your desktop offerings, the state of
Guile, the lack of any modern build tools &#8212; all of these could be solved,
easily.  But until <em>someone</em> is willing to foot the bill, in advance, without
payoff, they will not be written.  Ever.  Someone has to pay for your movement
to continue; and if they are not willing to pay, the pure programmer cannot do
what he loves to do.</p>

<p>Lastly, it is not copyleft I am arguing with.  That is just another form of
software license.  You <em>should</em> have the freedom to license your software
however you wish.  If you decide not to charge, but instead ask others to make
their changes public and available on the same terms &#8212; that&#8217;s just another
form of payment.  I can decide whether I want to add to your software on those
terms or not &#8212; the terms you set in offering the software to me.  I am still
happy to contribute to Emacs on that basis, for example, because I consider
Emacs enough of a joy that it is, to me, a fair trade.</p>

<p>What I object to is your desire to ultimately deny others the freedom to
choose whatever license they wish.  <strong><em>The GPL could not have existed in the
first place if you yourself had not enjoyed such a freedom</em></strong>.  But you don&#8217;t
just use copyleft for yourself, you campaign that copyright is wrong, evil,
&#8220;poison&#8221;.  Your copyleft license can exist in my world, since you own what you
do and can distribute it on whatever terms you wish; copyright cannot exist in
yours.  My policy is hands off; each to his own work, and let the market
decide what society will pay for and what it won&#8217;t.  I can survive on those
terms.</p>

<p>But you want, if I understand you, to control the terms of licensing available
to programmers.  You said, &#8220;I am in favor of people&#8217;s freedom to control their
own lives and to co-operate with others&#8221;; but your push for copyleft seems to
say the opposite: You want to control their lives by dictating the available
options for earning a livelihood, and by forcing them to co-operate with
others at their own expense.</p>

<p>Can you explain to me how pushing copyleft, and removing my choice of
licensing as a professional, increases rather than decreases my freedom to
control my own life?  You seem to me to be saying:</p>

<blockquote>
  <p>Software may be sold, but without restricting the right of the buyer to
  modify and redistribute the result &#8212; without having to pay his original
  distributor for the copies he makes.</p>
</blockquote>

<p>Is this right?  Or can I allow modification and redistribution, but at a
charge, and it would still be free software?</p>

<p>If this is your basic position, you state that being able to sell software is
evidence that it can be self-sustaining.  From a strictly monetary standpoint,
I cannot prove this to be wrong without making certain assumptions about the
nature of society; just as you cannot prove it right without making the same
kinds of assumptions.</p>

<p>I will continue the argument as though the right to modify and redistribute
software cannot be charged for, since this would constitute a financial
&#8220;restriction&#8221; of that right.</p>

<p>So, if a buyer exists who is willing to make the initial payment for a piece
of software &#8212; or, if you are able to sell it multiple times before free
distribution ends your customer base &#8212; you could, potentially, make as much
money writing code as by proprietary software.  Without buyers, you could not.</p>

<p>In other words, I could develop and sell software exactly as companies do now,
but I would have no right to stop anyone from giving away copies of my
software without paying me.</p>

<p>This means I could not rely on enough buyers, once the software had been
published, to be certain the effort would pay for itself.  Copyleft wipes out
my potential customers by allowing distributors to give away my product &#8212; and
charge for distributing it even &#8212; without paying me and without any
development cost for them.</p>

<p>Thus shrink-wrapped distribution could not be relied on to pay for software
development, as it does now.  Selling licenses will not pay for it either &#8211;
because I could only force payment for the first license.  After the first, no
one would be required to pay me.</p>

<p>The only thing that can reliably fund software development is something other
than software.  In other words:</p>

<blockquote>
  <p>Software cannot be guaranteed to pay for its development unless it is paid
  for up front.</p>
</blockquote>

<p>There are a few implications of this I would like you to consider:</p>

<ol>
<li><p>Full-time programmers who are currently funded by the code they write
will cease to exist.  Once they run out of money in the bank, they will
have to turn to other funding or do other work.  They cannot, as they do
now, rely on software sales to make their living.  This implies that:</p></li>
<li><p>Only software created by part-time programmers, or projects society is
willing to pay for in advance, can exist.  Since the people in category
&#8216;a&#8217; will die out, we fall on category &#8216;b&#8217;. Perhaps you see nothing wrong
with this.  Let&#8217;s carry it further:</p></li>
<li><p>If part-time programmers do not have enough time left over to work on an
idea, and if that idea cannot attract funding in advance, it will never
exist.</p></li>
</ol>

<p>Is this OK with you?  Are you aware how many programs are out there that only
exist because their authors made an investment in the hopes it would pay off?
That, and the existence of a government that requires people to pay them for
their work, and not make copies for everyone else?</p>

<p>Do you really want to restrict the development of complex, new ideas to what
the public is willing to pay for in advance, and what part-time programmers
have enough resources left over to create?  If so, you have far more faith in
humanity&#8217;s fore-sight than I do.  Just look at our history.</p>

<p>The above explains my fear, and the situation I see resulting from copyleft.
It leaves the future of programming at the mercy of altruism, public desire
and free-time.  I believe such a society cannot develop as freely as America
has, on the principle of rewarding inventors for their effort, and their
sacrifice in the face of public disapproval.</p>

<p>You mentioned that I might invent something no one is interested in, and still
not be able to survive as a programmer.  Well, of course.  I have no problem
with that.  I&#8217;m not asking society to guarantee me payment for anything; I
accept this as a risk I&#8217;m willing to take.  What I object to is your desire to
remove all chance of a pay-off that is not guaranteed, or funded by other
efforts.</p>

<p>If I create a piece of software that revolutionizes the world, I deserve to be
made rich.  That would be an appropriate reward for the corresponding benefit
offered to society.  A fair trade.  I wouldn&#8217;t even mind offering people the
restricted right to modify and redistribute my idea &#8212; so long as they pay me
for having such an idea to modify and redistribute!</p>

<p>What you want is that once I release my idea to the public, to becomes public
property.  Anyone can change and distribute it freely.  I may have saved one
hour of every computer user&#8217;s life on the whole planet, but I am not to be
paid in equivalent man-hours to research more ideas.  If I save millions of
hours for other people, it would be right to reward me with the funds to pay
for supporting millions of hours of mine.  In your world, however, the only
thanks I get &#8212; assuming no one paid me to write my software in the first
place, and no one was willing to pay me after I&#8217;d written it to see me publish
it &#8212; is a pat on the back and the necessity now to return to my other job so
I can repeat the performance.  Is this just?</p>

<p>If this is the society you want, I think you are shooting yourself in the
foot.  If one of society&#8217;s members contributes such good, he should be given
every resource to keep doing it.  That&#8217;s what becoming rich is supposed to
mean: Society is rewarding you for the value they recognize.  You may suggest
that now that I&#8217;m famous, others will be willing to fund my future efforts.
That&#8217;s a big assumption.  I think your society wants to use inventors as
mules, driving them in the direction of their preference by controlling the
purse strings.  If one programmer bucks the trend and develops a fantastic new
idea &#8212; well, it sucks to be him.  His thanks is to return to his real job
while everyone else starts copying his idea.</p>

<p>You said my argument is like a gangster justifying the right to take your
purse.  But a gangster gives you no choice.  The whole point of capitalism is
that you get to choose what you pay for &#8212; or don&#8217;t buy it.  I feel more like
the lady with the purse &#8212; which represents the earning potential of selling
my software &#8212; and you want to make that purse disappear.  This works fine for
you, in your position.  But I don&#8217;t think you&#8217;ve considered how it will affect
all parts of society &#8212; and how it will not work for you in the future should
it take hold.  Again, look at the free software community with a close eye.
There are a lot of things missing, and, I believe, good reasons why it still
does not seriously challenge the consumer market after twenty years.  You
could say &#8220;give it time&#8221;, but there is no arguing that; my position would end
with the same remark.  The question is, if we give it time, what really will
be the result?</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2011/04/letter-to-the-fsf/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>A word on Haskell Monads and C++</title>
		<link>http://newartisans.com/2010/07/a-word-on-haskell-monads-and-c/</link>
		<comments>http://newartisans.com/2010/07/a-word-on-haskell-monads-and-c/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 12:20:06 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://newartisans.com/2010/07/a_word_on_haskell_monads_and_c/</guid>
		<description><![CDATA[After spending a good while trying to understand monads in Haskell, and why the Haskell world is so fascinated by them, I finally understand why they aren&#8217;t as exciting to other languages, or why they are completely missing from languages like C++: because they&#8217;re mostly already there. At its simplest, a monad is an abstraction [...]]]></description>
			<content:encoded><![CDATA[<p>After spending a good while trying to understand monads in Haskell, and why the Haskell world is so fascinated by them, I finally understand why they aren&#8217;t as exciting to other languages, or why they are completely missing from languages like C++: because they&#8217;re mostly already there.</p>

<p>At its simplest, <em>a monad is an abstraction of a value which knows how to apply functions to that value, returning a new monad</em>.  In other words, it&#8217;s a way to turn values into little packages that wrap additional functionality around that value.  Sounds a lot like what an object does&#8230;</p>

<p><span id="more-228"></span>
But this doesn&#8217;t tell you what&#8217;s exciting about them, from Haskell&#8217;s point of view.  Another way of looking at them, without going into the wheres and whys, is this: In a lazily-evaluated, expression-based language, monads let you express sequenced, interdependent computation.</p>

<p>Consider the following two code examples.  First, in C++:</p>

<pre><code>#include 
int main() {
  std::cout &lt;&lt; "Hello, world!"
            &lt;&lt; "  This is a sample"
            &lt;&lt; " of using a monad in C++!"
            &lt;&lt; std::endl;
  return 0;
}
</code></pre>

<p>And the same code in Haskell:</p>

<pre><code>module Main where
main :: IO ()
main = do putStr "Hello, world!"
          putStr "  This is a sample"
          putStr " of using a monad in C++!"
          putStr "\n"
</code></pre>

<p>What the IO monad in the second example is doing is making the sequenced evaluation of the print statements possible using a nice, normal looking syntax.  The C++ code doesn&#039;t need monads to do this, because it already embodies the concept of abstracted values (here, the iostream passed between insertion operators) and sequenced computation (because it&#039;s not lazy).</p>

<p>To compare Monads with C++:</p>

<ol>
<li><p>Monads are abstractions of values.  So are most C++ objects.</p></li>
<li><p>Monads permit functions to be applied to the &#8220;contained&#8221; value, returning a a new version of the monad.  C++ objects provide methods, where the mutated object is the new version.</p></li>
<li><p>Monads provide a way to encapsulate values in new monads.  C++ objects have constructors.</p></li>
</ol>

<p>As another example, consider the case where you have to call five functions on an integer, each using the return value of the last:</p>

<pre><code>j(i(h(g(f(10))))
</code></pre>

<p>This is an identical operation in both Haskell and C++.  But what if the return value of each function wasn&#039;t an integer, but an &#8220;object&#8221; that could either be an integer, or an uninitialized value?  In most languages, there&#039;s either a type, or syntax, for this concept:</p>

<pre><code>C++      boost::optional
C#       int?
Java     Integer
Haskell  Maybe Int
</code></pre>

<p>If each function returns one of these, but takes a real integer, it means we have to check the &#8220;null&#8221; status of each return value before calling the next function.  In C++ this leads to a fairly common idiom:</p>

<pre><code>if (boost::optional x1 = f(10))
  if (boost::optional x2 = g(*x1))
    if (boost::optional x3 = h(*x2))
      if (boost::optional x4 = i(*x3))
        j(*x4);
</code></pre>

<p>Note that not only are these calls sequential, but due to the meaning of optionality, they are also inherently short-circuiting.  If <code>f</code> returns <code>none</code>, none of the other functions get called.</p>

<p>Haskell can do this type of thing natively as well, and it looks similar:</p>

<pre><code>case f 10 of
  Nothing -&gt; Nothing
  Just x1 -&gt; 
    case g x1 of
      Nothing -&gt; Nothing
      Just x2 -&gt; 
        case h x2 of
          Nothing -&gt; Nothing
          Just x3 -&gt; 
            case i x3 of
              Nothing -&gt; Nothing
              Just x4 -&gt; j x4
</code></pre>

<p>But it&#8217;s ugly as sin.  In C++, we can be evil and flatten things out using basic features of the language, assuming we pre-declare the variables:</p>

<pre><code>(   (x1 = f(10))
 &amp;&amp; (x2 = g(*x1))
 &amp;&amp; (x3 = h(*x2))
 &amp;&amp; (x4 = i(*x3))
 &amp;&amp; (x5 = j(*x4)), x5)
</code></pre>

<p>Or you can eliminate the use of temporaries altogether by creating a wrapper class:</p>

<pre><code>template  struct Maybe {
  boost::optional value;

  Maybe() {}
  Maybe(const T&amp; t) : value(t) {}
  Maybe(const Maybe&amp; m) : value(m.value) {}

  Maybe operator&gt;&gt;(boost::function,Maybe(const T&amp;)&gt; f) const {
    return value ? f(*value) : *this;
  }
};
</code></pre>

<p>If we change our functions to return <code>Maybe</code> instead of just <code>boost::optional</code>, it allows us to write this:</p>

<pre><code>f(10) &gt;&gt; g &gt;&gt; h &gt;&gt; i &gt;&gt; j
</code></pre>

<p>Which in Haskell is written almost the same way:</p>

<pre><code>f 10 &gt;&gt;= g &gt;&gt;= h &gt;&gt;= i &gt;&gt;= j
</code></pre>

<p>But where Haskell needs Monads to make this type of thing reasonable and concise, C++ doesn&#8217;t.  We get passing around of object state between function calls as part of the core language, and there are many different ways to express it.  However, if you confined C++ to function definitions and return statements only &#8212; where all function arguments were pass-by-value &#8212; then things like Monads would become an essential technique for passing knowledge between calls.</p>

<p>So it&#8217;s not that you can&#8217;t use Monads in C++, it&#8217;s just that they require enough extra machinery, and aren&#8217;t unique enough compared to core features of the language, that there isn&#8217;t the same level of motivation for them as there is in Haskell, where they can really add to the expressiveness of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2010/07/a-word-on-haskell-monads-and-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A C++ gotcha on Snow Leopard</title>
		<link>http://newartisans.com/2009/10/a-c-gotcha-on-snow-leopard/</link>
		<comments>http://newartisans.com/2009/10/a-c-gotcha-on-snow-leopard/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 09:35:32 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/10/a_c_gotcha_on_snow_leopard/</guid>
		<description><![CDATA[I&#8217;ve seen this issue mentioned in some random and hard to reach places on the Net, so I thought I&#8217;d re-express it here for those who find Google sending them this way.

...Whenever a string is deconstructed, the standard library would check whether that string&#8217;s address matches matches the empty string&#8217;s: if so, it does nothing; if not, it calls =free=.

...If a library that  does  have fully dynamic strings enabled (aka the standard library) receives an empty string from code which does not have it enabled (aka, the app you just built), it will try to free it and your application will crash.

...Since my standard library <em>is</em> compiled with fully dynamic strings, the destructor for =basic string= doesn&#8217;t recognize that its the &#8220;special&#8221; empty string, so it tries to free it.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen this issue mentioned in some random and hard to reach places on the Net, so I thought I&#8217;d re-express it here for those who find Google sending them this way.</p>

<p><strong>UPDATE</strong>: According to the discussion at https://trac.macports.org/ticket/27237, the real problem here is not fully dynamic string, but the use of _GLIBCXX_DEBUG.  So I recommend ignoring what follows, as it will help you on Snow Leopard or Lion with gcc 4.6 and above.</p>

<p><span id="more-227"></span>
On Snow Leopard, Apple decided to build g++ and the standard C++ library with &#8220;fully dynamic strings&#8221; enabled.  What this means for you relates to the empty string.</p>

<p>When fully dynamic strings are off (as was true in Leopard), there exists a single global variable representing the empty string.  This variable lives in the data segment of <code>libstdc++</code>, and so it does not exist on the heap.  Whenever a string is deconstructed, the standard library would check whether that string&#8217;s address matches matches the empty string&#8217;s: if so, it does nothing; if not, it calls <code>free</code>.</p>

<p>With fully dynamic strings on, there is no global empty string.  All strings are on the heap, and once their reference count goes to zero, they get deallocated.  Where this creates a problem is if you mix and match code.  If a library that <em>does</em> have fully dynamic strings enabled (aka the standard library) receives an empty string from code which does not have it enabled (aka, the app you just built), it will try to free it and your application will crash.</p>

<p>Here&#8217;s a reproducible case for this issue using Boost:</p>

<pre><code>#include 
#include 
#include 

int main()
{
  std::ostringstream buf;
  boost::variant data;
  data = buf.str();
  data = false;
  return 0;
}
</code></pre>

<p>In this case &#8212; which really happened to me &#8212; I created an empty string by calling <code>ostringstream::str()</code>.  Since I don&#8217;t have fully dynamic string on, its address is in data space, not on the heap.  I pass this string to <code>boost::variant</code>, which makes a copy of that address.  Later, when the variant is reassigned <code>false</code>, it calls <code>~basic_string</code> to deconstruct the string.  Since my standard library <em>is</em> compiled with fully dynamic strings, the destructor for <code>basic_string</code> doesn&#8217;t recognize that its the &#8220;special&#8221; empty string, so it tries to free it.</p>

<p>The solution to this problem is three-fold:</p>

<ol>
<li><p>You must be using the <code>g++</code> that comes with Xcode, or if you build your own (say, via MacPorts), you must configure it using <code>--enable-fully-dynamic-string</code>.  I&#8217;ve already submitted a patch to this effect to the MacPorts crew.</p></li>
<li><p>All libraries must be compiled with <code>-D_GLIBCXX_FULLY_DYNAMIC_STRING</code>.</p></li>
<li><p>Your own code must be compiled with <code>-D_GLIBCXX_FULLY_DYNAMIC_STRING</code>.</p></li>
</ol>

<p>You&#8217;ll know if this issue is biting you by looking at a stack trace in gdb.  You&#8217;ll see a crash somewhere inside basic_string&#8217;s <code>_M_destroy</code> (which calls <code>free</code>).  Move up the trace a bit and check whether the string it&#8217;s trying to free is 0 bytes long.</p>

<p>To recap: what&#8217;s happened is that an empty string constructed by code without fully dynamic strings got deallocated by code that was.  That is, most likely you, or a library you built, handed an empty <code>std::string</code> to the system library.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/10/a-c-gotcha-on-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Branch policies with Git</title>
		<link>http://newartisans.com/2009/10/branch-policies-with-git/</link>
		<comments>http://newartisans.com/2009/10/branch-policies-with-git/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 03:05:57 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/10/branch_policies_with_git/</guid>
		<description><![CDATA[When the master branch is at a state where I want to finally release it, I merge with =&#8212;no-ff=, so the merge gets represented as a single commit on the maint branch.

...Since most development work happens on &#8220;next&#8221;, each time next is stable I merge into master, using =&#8212;no-ff= to keep the merge commits together.

... Note that no commits are ever made directly to master, unless I&#8217;ve seriously broken something that needs to be addressed sooner than the next merge from &#8220;next&#8221;.

... Then there are the various local-only topic branches that live on my machine, in which I develop highly unstable code relating to one feature or another, awaiting the day when it becomes stable enough to be merge into &#8220;next&#8221;.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been managing my <a href="http://wiki.github.com/jwiegley/ledger">Ledger</a> project with Git for some time now, and I&#8217;ve finally settled into a comfortable groove concerning branches and where to commit stuff.</p>

<p><span id="more-226"></span>
Essentially I use four branches, in increasing order of commit frequency.  Each branch has its own policy and purpose, which are described below.</p>

<h2>maint</h2>

<p>Every release of Ledger is made from the maint branch, and every commit on that branch is potentially a release.  This means that no commit is made until some serious vetting takes place.  When the master branch is at a state where I want to finally release it, I merge with =&#8211;no-ff=, so the merge gets represented as a single commit on the maint branch.  Then I tag the release and make a distribution tarball.</p>

<p>It&#8217;s possible after a release that patches need to get applied to maint, and a point release made.  Once this is done, the applicable patches are either merged into master, or if the two diverse too greatly I will begin cherry-picking instead.  Once cherry-picking starts, no more merges into master will occur until after the next release merge happens in maint.</p>

<p>The purpose of maint is to provide the most stable release possible to the public.</p>

<h2>master</h2>

<p>Master is where most people get the latest source code from, so it is kept reasonable stable.  There is a commit hook which guarantees that all commits to this branch build and pass the test suite.  Since most development work happens on &#8220;next&#8221;, each time next is stable I merge into master, using =&#8211;no-ff= to keep the merge commits together.  I also use =&#8211;no-commit=, so the merge must pass the commit hook in order to go in.</p>

<p>Note that no commits are ever made directly to master, unless I&#8217;ve seriously broken something that needs to be addressed sooner than the next merge from &#8220;next&#8221;.  In that case, I&#8217;ll cherry pick this commit into master afterward.  Merges only happen into master from next, and only from master into maint.</p>

<p>The purpose of master is to provide reasonably stable development snapshots to the public.</p>

<h2>next</h2>

<p>The next branch is where I commit most often, and while I try to keep it functional, this is not always the case.  I don&#8217;t run unit tests here for every commit, just before every push (mostly).  Most of my friends follow this branch, because it updates very often.</p>

<p>The purpose of next is to provide potentially unstable, frequent development snapshots to the public.</p>

<h2>test</h2>

<p>The test branch comes in and out of existence, and should only ever be pulled using =pull &#8211;rebase=.  It contains trial commits that I want someone to test out.  It&#8217;s a delivery branch, and after it&#8217;s been used I either delete it or ignore it until the next time it&#8217;s necessary.</p>

<p>The purpose of test is to communicate patch candidates to a particular person at a particular time.</p>

<h2>topic</h2>

<p>Then there are the various local-only topic branches that live on my machine, in which I develop highly unstable code relating to one feature or another, awaiting the day when it becomes stable enough to be merge into &#8220;next&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/10/branch-policies-with-git/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Response to PG&#8217;s &#8220;How to Do Philosophy&#8221;</title>
		<link>http://newartisans.com/2009/05/response-to-pgs-how-to-do-philosophy/</link>
		<comments>http://newartisans.com/2009/05/response-to-pgs-how-to-do-philosophy/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:04:27 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[graham]]></category>
		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/05/response_to_pgs_how_to_do_philosophy/</guid>
		<description><![CDATA[The &#8220;practical man&#8221; knows well the value of practical things and he is an expert at perfecting the animal life; but it takes more than a well-fed stomach to bring true content.

... If a philosopher is anything, I say he is someone who forgoes all else to discover and adventure in  that  world, and to learn what effect immaterial consequences should have on our material life, if all is to be as it ought.

...What Plato used his method for was to approach noesis: to know the &#8220;real real&#8221;, to have a direct apprehension of reality freed from mortal conceptions; to &#8220;remember&#8221; the soul&#8217;s birth and origin; to return our perception of the world to an original, direct perception of Truth itself.

...There are human endeavors which are little more than words or pigments on paper, that come to life only through the eye of an appreciate heart and mind.]]></description>
			<content:encoded><![CDATA[<p>Back in late 2007, Paul Graham put up an essay titled &#8220;How to Do Philosophy&#8221;, in which Mr. Graham hoped to elucidate where Philosophy went wrong and why the field, as now practiced, must be renovated to remain useful.  In fact, he goes so far as to suggest that much of philosophy has no benefit whatsoever:</p>

<blockquote>
  <p>The proof of how useless some of their answers turned out to be is how little effect they have.  No one after reading Aristotle&#8217;s Metaphysics does anything differently as a result.</p>
</blockquote>

<p>If I may, as a student of philosophy, I would like to offer my response to this argument, whose tenets have been repeated many times throughout Philosophy&#8217;s history.</p>

<p><span id="more-225"></span></p>

<h2>The spirit of philosophy</h2>

<p>As far back as Plato&#8217;s Republic (and most likely long before then) there have been debates on the merit of philosophy.  In Plato&#8217;s book it is between Socrates and Glaucon, who fears that men may waste their time in fruitless contemplation:</p>

<blockquote>
  <p>Socrates: I am amused, I said, at your [Glaucon's] fear of the world, which makes you guard
  against the appearance of insisting upon useless studies; and I quite admit
  the difficulty of believing that in every man there is an eye of the soul
  which, when by other pursuits lost and dimmed, is by these purified and
  re-illumined; and is more precious far than ten thousand bodily eyes, for
  by it alone is truth seen&#8230;.</p>
</blockquote>

<p>Earlier Socrates had said something similar, and in briefer terms:</p>

<blockquote>
  <p>Socrates: Then must not a further admission be made?</p>
  
  <p>Glaucon: What admission?</p>
  
  <p>Socrates: That the knowledge at which geometry aims is knowledge of the eternal, and
  not of aught perishing and transient.</p>
  
  <p>Glaucon: That, he replied, may be readily allowed, and is true.</p>
  
  <p>Socrates: Then, my noble friend, geometry will draw the soul towards truth, and
  create the spirit of philosophy, and raise up that which is now unhappily
  allowed to fall down.</p>
</blockquote>

<p>This &#8220;spirit of philosophy&#8221; is held by Socrates over and over again to be precious beyond compare: a light to illumine every aspect of life.  If a lantern is something you can design, hold and weigh, yet this light is its intangible counterpart, granting the lamp its purpose.  It is the &#8220;why&#8221; to the lantern&#8217;s &#8220;what&#8221; and &#8220;how&#8221;.  It can neither be designed, nor held, nor weighed, but must be enkindled.  And only then does the lamp come aglow&#8230;</p>

<h2>The harp on practicality</h2>

<p>I understand the need for practical results in a material world, but results are meaningless deprived of context.  If we boil things down to their material essence, then what we do we do for survival: develop resources to protect and prolong life.  But is surviving enough?  Don&#8217;t people also seek meaning from what they do?  Certainly I don&#8217;t enjoy programming merely to make a paycheck; I have to feel something <em>more</em> to keep me motivated year after year.</p>

<p>The harp on practicality levied against philosophy overstresses the &#8220;what&#8221; against the &#8220;why&#8221;.  Mr. Graham debates how to make philosophy useful again, but I think he has lost the point of it: useful in terms of what?  Does usefulness have a &#8220;why&#8221;?  Who is to define the best &#8220;use&#8221; of anything, so that usefulness may be measured?  Thus, there is a conundrum at this center of his argument: How can any man judge philosophy who has not discovered what it aims to impart?</p>

<p>Anyone can understand the concept of practicality.  Even children connect the ideas of work and output.  It&#8217;s why we hate cleaning our room, because it takes so much work yet we gain so little from it.  But what is pratical is not the same as what is essential.  Happiness, most of us know, is not found in more money, more power, or by more efficient processes.  There is only one outcome in this life which is inevitable, and curiously neither industry nor indolence has any effect on its timing or nature.  But whereas the practical man fears death as the end of opportunity, perhaps the philosopher sees it differently:</p>

<blockquote>
  <p>Socrates: The philosopher desires death &#8212; which the wicked world will insinuate that
  he also deserves:  and perhaps he does, but not in any sense which they are
  capable of understanding.  Enough of them: the real question is, What is
  the nature of that death which he desires?  Death is the separation of soul
  and body &#8212; and the philosopher desires such a separation.  He would like to
  be freed from the dominion of bodily pleasures and of the senses, which are
  always perturbing his mental vision.  He wants to get rid of eyes and ears,
  and with the light of the mind only to behold the light of truth&#8230;.</p>
</blockquote>

<p>So the question is raised: Is there more than just this world?  I don&#8217;t necessarily mean physical death, either.  For there is a world of purely material pursuits and achievement &#8212; a world we share in common with animals &#8212; and there is a world of inspiration, abstraction, and fantasy, which only men participate in.  The &#8220;practical man&#8221; knows well the value of practical things and he is an expert at perfecting the animal life; but it takes more than a well-fed stomach to bring true content.  If not so, then cows should be our kings.</p>

<p>If a philosopher is anything, I say he is someone who forgoes all else to discover and adventure in <em>that</em> world, and to learn what effect immaterial consequences should have on our material life, if all is to be as it ought.</p>

<h2>The bane of method</h2>

<p>Not everyone who reads Plato, of course, comes away with mystical opinions.  Just as there are those who eschew philosophy entirely and ignore its delights, so there are some who accept it but half-way.  They see that philosophy prescribes a method and they fall in love with that method, dedicating the whole of their pursuit to refining it.  Yes, Plato did stress the necessity of dialectic, but his stress had a purpose in mind.  Not a material or pratical goal &#8212; hardly even a &#8220;useful&#8221; one in immediate terms &#8212; but a personal and soulful one.</p>

<p>Philosophy is ever so much more than method.  In fact, the love of method has resulted in a few branches of philosophy which are hardly philosophy at all, but the art of analysis.  What Plato used his method for was to approach noesis: to know the &#8220;real real&#8221;, to have a direct apprehension of reality freed from mortal conceptions; to &#8220;remember&#8221; the soul&#8217;s birth and origin; to return our perception of the world to an original, direct perception of Truth itself.  Through this experience of true perception our breasts and minds would dilate, and every pursuit will become infused with the vibrating principle of Life.</p>

<h2>Missing the point</h2>

<p>This is why, when I read essays like Mr. Graham&#8217;s, I find myself thinking that his own success and momentum have caused him to miss the point.  Philosophy is not meant to be practical.  It is not meant to have a use.  It does not exist to make us more productive girls and boys.  It is a diet of words to feed our soul by way of stimulating our mind.  It is not a roast-beef sandwich, but more the substance of an ethereal longing.</p>

<p>Some will ask, what is this thing that is words and nothing more?  To them I reply: Then what is poetry?  There are human endeavors which are little more than words or pigments on paper, that come to life only through the eye of an appreciate heart and mind.  Does a man read Shakespeare and ask what profit he has gained?  If he does then he cannot see the point.  What he gains is immaterial &#8212; literally and figuratively &#8212; but may in the long run be immensely valuable.  It depends on what he saw, how well he saw, and the breadth of his vision.</p>

<p>It is no different with Philosophy.  Consider it an artform, or a method of tuning the soul through delicate adjustments of the mind.  When one tunes a violin there is no melody played; that comes after.  The fruit of philosphy is the philosopher&#8217;s life itself.  It is how it changes the man that matters, not the changes he can prove to you from day to day.</p>

<p>So if you are accustomed to reading balance sheets and preparing quarterly projections, perhaps you are ill-equipped to judge philosophy.  But if you measure the smile of a happy engineer against the despair of an endless, daily grind, maybe then you will have found the weight of philosophy&#8217;s fruit.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/05/response-to-pgs-how-to-do-philosophy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Journey into Haskell, part 6</title>
		<link>http://newartisans.com/2009/03/journey-into-haskell-part-6/</link>
		<comments>http://newartisans.com/2009/03/journey-into-haskell-part-6/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 13:00:00 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[infinity]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/03/journey_into_haskell_part_6/</guid>
		<description><![CDATA[Create a list of primes &#8220;as you go&#8221;, considering a number prime if it can&#8217;t be divided by any number already considered prime.

... However, although my straightforward solution worked on discrete ranges, it couldn&#8217;t yield a single prime when called on an infinite range &#8212; something I&#8217;m completely unused to from other languages, except for some experience with the SERIES library in Common Lisp.

... But when I suggested this on  #haskell , someone pointed out that you can&#8217;t reverse an infinite list.

...This time when I put  primes [1..]  into GHCi it printed out prime numbers immediately, but visibly slowed as the accumulator grew larger.]]></description>
			<content:encoded><![CDATA[<p>Another thing to be learned down the Haskell rabbit-hole: Thinking in infinites.  Today someone posed a puzzle which I tried to solve in a straight-forward, recursive manner: Building a list of primes.  The requested algorithm was plain enough:</p>

<blockquote>
  <p>Create a list of primes &#8220;as you go&#8221;, considering a number prime if it can&#8217;t be divided by any number already considered prime.</p>
</blockquote>

<p>However, although my straightforward solution worked on discrete ranges, it couldn&#8217;t yield a single prime when called on an infinite range &#8212; something I&#8217;m completely unused to from other languages, except for some experience with the SERIES library in Common Lisp.</p>

<p><span id="more-224"></span></p>

<h2>An incomplete solution</h2>

<p>Looking similar to something I might have written in Lisp, I came up with this answer:</p>

<pre><code>primes = reverse . foldl fn []
    where fn acc n
              | n `dividesBy` acc = acc
              | otherwise         = (n:acc)
          dividesBy x (y:ys)
              | y == 1         = False
              | x `mod` y == 0 = True
              | otherwise      = dividesBy x ys
          dividesBy x [] = False
</code></pre>

<p>But when I suggested this on <a href="irc://irc.freenode.net/haskell">#haskell</a>, someone pointed out that you can&#8217;t reverse an infinite list.  That&#8217;s when a light-bulb turned on: I hadn&#8217;t learned to think in infinites yet.  Although my function worked fine for discrete ranges, like <code>[1..100]</code>, it crashed on <code>[1..]</code>.</p>

<p>So back to the drawing board, later to come up with this infinite-friendly version:</p>

<pre><code>primes :: [Int] -&gt; [Int]
primes = fn []
    where fn _ [] = []
          fn acc (y:ys)
              | y `dividesBy` acc = fn acc ys
              | otherwise         = y : fn (y:acc) ys

          dividesBy _ [] = False
          dividesBy x (y:ys)
              | y == 1         = False
              | x `mod` y == 0 = True
              | otherwise      = dividesBy x ys
</code></pre>

<p>Here the accumulator grows for each prime found, but returns results in order whose value can be calculated as needed.  This time when I put <code>primes [1..]</code> into GHCi it printed out prime numbers immediately, but visibly slowed as the accumulator grew larger.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/03/journey-into-haskell-part-6/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Journey into Haskell, part 5</title>
		<link>http://newartisans.com/2009/03/journey-into-haskell-part-5/</link>
		<comments>http://newartisans.com/2009/03/journey-into-haskell-part-5/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 13:00:00 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cabal]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/03/journey_into_haskell_part_5/</guid>
		<description><![CDATA[I actually imported the full source into HackPorts, ripped out its  List.hs  file, renamed it to my  Main.hs  file, and then began changing it from a function that prints out a list of available packages, to one that writes the data into properly formatted  Portfile  entries.

...  As it does this, it fetches the current version&#8217;s tarball over HTTP, and uses OpenSSL (directly, through FFI) to generate MD5, SHA1 and RIPEMD160 checksums of the tarball image.

...As a stub, I have them all depending on  port:ghc , but I think there&#8217;s sufficient information in the Cabal package info to figure out what the right dependencies should be, both among the Hackage packages themselves and against any external libraries (like OpenSSL).

...Whereas  map  takes a list of values and returns a list of values,  mapM  takes a list of values and returns a list of actions that get invoked in sequence in the current Monad (in this case,  IO ).]]></description>
			<content:encoded><![CDATA[<p>Haskell may be difficult to start out with, but once things start rolling, they roll fast.  Yesterday (real world time, these blog entries are staggered) I had started the first lines of HackPorts, but now things are getting close to done for the first version.  It&#8217;s not that I&#8217;ve written much code, but that it was simple to integrate with other people&#8217;s code.</p>

<p><span id="more-223"></span></p>

<h2>Borrowing all I can</h2>

<p>The first thing I wanted to do was avoid dealing with any of Hackage&#8217;s data formats, so I cribbed everything I could from the <code>cabal-install</code> package.  I actually imported the full source into HackPorts, ripped out its <code>List.hs</code> file, renamed it to my <code>Main.hs</code> file, and then began changing it from a function that prints out a list of available packages, to one that writes the data into properly formatted <code>Portfile</code> entries.</p>

<p>The code does the following bits of work:</p>

<ol>
<li><p>Talks to <code>cabal-install</code> and Cabal to get a list of all known packages on Hackage.</p></li>
<li><p>For every package, creates a directory named <code>haskell/$package</code>, and then writes information about that package into <code>haskell/$package/Portfile</code>.</p></li>
<li><p>As it does this, it fetches the current version&#8217;s tarball over HTTP, and uses OpenSSL (directly, through FFI) to generate MD5, SHA1 and RIPEMD160 checksums of the tarball image.</p></li>
</ol>

<p>And voilá, a directory populated with 1136 Portfile entries.  What&#8217;s missing now is the external dependency mapping.  As a stub, I have them all depending on <code>port:ghc</code>, but I think there&#8217;s sufficient information in the Cabal package info to figure out what the right dependencies should be, both among the Hackage packages themselves and against any external libraries (like OpenSSL).</p>

<h2>What I learned</h2>

<p>As for my Haskell education, I learned about using Haskell&#8217;s very nice FFI mechanism, and had a lot more experience using the IO Monad.  An example of using FFI to call out to OpenSSL:</p>

<pre><code>{-# OPTIONS -#include "openssl/md5.h" #-}

foreign import ccall "openssl/md5.h MD5" c_md5
    :: Ptr CChar -&gt; CULong -&gt; Ptr CChar -&gt; IO (Ptr Word8)
</code></pre>

<p>I now have access to a <code>c_md5</code> function, which go directly over to the C library to do its work.  Not too shabby!</p>

<p>As for the IO Monad, here is the <code>main</code> function for Hackports:</p>

<pre><code>main :: IO ()
main = do
  createDirectoryIfMissing True "haskell"
  pkgs ,- allPackages verbose
  mapM writePortfile pkgs
  putStrLn "Hackage has been exported to MacPorts format in haskell/"
</code></pre>

<p>The trickiest part for me was understanding how <code>mapM</code> differs from <code>map</code>.  Whereas <code>map</code> takes a list of values and returns a list of values, <code>mapM</code> takes a list of values and returns a list of actions that get invoked in sequence in the current Monad (in this case, <code>IO</code>).</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/03/journey-into-haskell-part-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How laziness changes thinking in Haskell</title>
		<link>http://newartisans.com/2009/03/functional-yet-lazy/</link>
		<comments>http://newartisans.com/2009/03/functional-yet-lazy/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 06:06:16 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[laziness]]></category>
		<category><![CDATA[lazy]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/03/functional_yet_lazy/</guid>
		<description><![CDATA[If it were a file I was checksumming, I could memory map the file and pass around a byte pointer, and the OS would take care of lazily reading in the bytes for me as needed.

...In C++ I&#8217;d have to switch from passing a vector to passing an istream iterator, but in Haskell, I don&#8217;t care what algorithm is populating my list, only that it  is  a list, and that I know how to work it.

...Based on the behavior of the program, I&#8217;m led to believe it happened near what the code was actually  doing [^3] &#8212; but in fact the problem may have started long, long before, except that laziness differed the trigger to a later time.

... I still think the benefits can outweight the difficulties &#8212; especially when it comes to parallelism, and avoiding unnecessary computations, and allowing code to safely traverse infinite series &#8212; but it definitely requires a level of algorithmic conciousness on the part of the engineer which seems quite a bit higher than with imperative languages.]]></description>
			<content:encoded><![CDATA[<p>As I explore Haskell, I&#8217;m discovering that one of its trickiest aspects is not structuring things functionally, but the lazy evaluation.  It turns out lazy evaluation comes with both great benefits, and significant difficulties.  I&#8217;d like to point a few of these out, as they&#8217;re becoming clearer to me.</p>

<p><span id="more-222"></span></p>

<h2>Benefits</h2>

<p>One of the great benefits of lazy evaluation is that your code <em>doesn&#8217;t need to account for the scale of an operation</em>.  Let&#8217;s take a simple example: checksumming a large file whose contents are being read, on-demand, over HTTP.</p>

<p>In C++, if I wanted to checksum a large file read over HTTP, I couldn&#8217;t buffer it memory because I don&#8217;t know how large it might get.  Nor do I want my checksumming code to know anything about HTTP, or where the data comes from.  The answer here is to use I/O streams.  By passing a generic <code>istream</code> interface around, I can hide any knowledge of where the data came from.  The checksumming algorithm just reads data from the stream as it needs to, and the HTTP layer downloads more bytes as required (typically caching to avoid constant network access).</p>

<p>However, there&#8217;s a downside to this: the checksumming code now knows something about I/O.  In actuality, a checksumming algorithm only cares about the bytes being checksummed and little else.  It shouldn&#8217;t have to know about I/O, or strings, or any of the details of where data comes from or how it&#8217;s structured.  It should ideally receive a pointer to an arbitrary large sequences of 8-bit bytes, and return a fixed size checksum representing a fingerprint of those bytes.</p>

<p>Yet this naive approach can&#8217;t really be done in C++.  If it were a file I was checksumming, I could memory map the file and pass around a byte pointer, and the OS would take care of lazily reading in the bytes for me as needed.  But for a file being accessed over HTTP this would require first downloading the file and then checksumming it, when I specifically wanted to &#8220;checkum as I go&#8221;.  Who knows, maybe I&#8217;ll discover a reason to stop summing beyond a certain point and I&#8217;d like to stop downloading at that point as well.</p>

<p>Well, just as memory mapping gives me lazy access to the contents of a file, a language with lazy evaluation gives me lazy access to the results of any algorithm &#8212; including downloading data over HTTP.  With Haskell, I can indeed write my checksum algorithm as if it receives a giant byte buffer, and the language takes care of downloading only as much data as I&#8217;ve accessed (plus caching).  This simplifies my checksumming code, and reduces the amount of knowledge that has to be passed around, such as a &#8220;stream&#8221; as opposed to a generic, 8-bit pointer.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup></p>

<p>This simplification lets you design your algorithm as if in an ideal world.  You want to process a bunch of numbers?  Work on a list.  What you say, the numbers are coming in from a socket and you don&#8217;t know when it will end?  Doesn&#8217;t matter, just work on a list.  In C++ I&#8217;d have to switch from passing a vector to passing an istream iterator, but in Haskell, I don&#8217;t care what algorithm is populating my list, only that it <em>is</em> a list, and that I know how to work it.</p>

<h2>Detriments</h2>

<p>For all its beauty, laziness has three costs I&#8217;ve run into so far.  The first is that it lets you very easily write functioning algorithms with horrible performance characteristics.  This happens because laziness causes a promise<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup> to be constructed, which takes memory and time to do.  Sometimes, the cost of the underlying operation is far less than the memory cost of carrying a promise around to do that operation at a later point.  This isn&#8217;t true of a slow operation like reading from a socket, but it&#8217;s certainly true of something trivial like summing two integers.  It means one has to be aware of promises, when they&#8217;re constructed, and when it&#8217;s more beneficial to force evaluation always versus the benefits to be had from deferred a computation whose result may never be needed.</p>

<p>The second is that when a poorly performing algorithm dies, it dies when its value is used, not when the promises are made.  This can make it look like the consumer is to blame, when really it&#8217;s the producer.  Here is a trivial example:</p>

<pre><code>mysum = foldl (+) 0

main = print (mysum [1..1000000])
</code></pre>

<p>Although <code>foldl</code> is tail-recursive, so we aren&#8217;t blowing stack through recursive calls, it still blows stack because it builds up a huge, nested structure of promises that only gets evaluated once print is called to render it as a string.  That is, the return value from mysum itself is no problem, it&#8217;s just a lazy computation against a large list.  But then print needs the result, so it asks mysum to fulfill its promise.  This in turns causes mysum to churn through the large list of integers, building up the return value as it goes.</p>

<p>However, and here is where the surprise comes in: foldl doesn&#8217;t actually compute those values as it walks the input list.  No, even these are done lazily, because it can&#8217;t know how many of those values will actually be needed.  We may know from looking at the code that it will need them all, but it doesn&#8217;t know.  So it constructs something on the stack looking like this:</p>

<pre><code>((((((((0+1)+2)+3)+4)+5)+6)+7)+...)
</code></pre>

<p>And so on, all the way to the last integer.  Only when <code>mysum</code> is done constructing promises across the entire input list, and the promise structure is returned, will it actually get evaluated by summing the integers together and finalizing each promise.  If you pick a input list large enough, there goes available memory.</p>

<p>The trick here is that the stack fault won&#8217;t ocur in foldl, or in mysum.  It will occur in print, where the need to resolve the promise result in the call to mysum actually being made, which then calls foldl, which then starts building thunks until memory is gone.  In this trivial example there&#8217;s very little code or time distance between the problem and its cause, but in real world code there may be enormous gaps between them.</p>

<p>In consequence of this I learned that it&#8217;s <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#tracing">hard to get GHC to produce stack traces</a> for you when there&#8217;s a runtime error.  Your code can be going on its merry way, when suddenly there&#8217;s a stack fault.  But that&#8217;s all you see: a stack fault indicating something went wrong.  Where did it go wrong?  Based on the behavior of the program, I&#8217;m led to believe it happened near what the code was actually <em>doing</em><sup id="fnref:3"><a href="#fn:3" rel="footnote">3</a></sup> &#8212; but in fact the problem may have started long, long before, except that laziness differed the trigger to a later time.</p>

<p>So, even though laziness can delay costs and abstract how data is determined, by the same taken it also delays errors and abstracts blame.  In C++ if I pass in an I/O stream and there&#8217;s a crash reading from it, I know to look at my stream code.  But in Haskell if I get a stack fault simply by processing a list, how am I to know what&#8217;s wrong?  It&#8217;s not going to be in the List code, and probably not in the code walking the list, but in code which promised to produce the list potentially a long time ago.</p>

<p>I still think the benefits can outweight the difficulties &#8212; especially when it comes to parallelism, and avoiding unnecessary computations, and allowing code to safely traverse infinite series &#8212; but it definitely requires a level of algorithmic conciousness on the part of the engineer which seems quite a bit higher than with imperative languages.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>And if I do have to include state with this raw, lazy data, but I don&#8217;t the algorithm to know anything about it?  That&#8217;s where the Monad steps in.  Say instead of checksumming a file, I&#8217;m parsing an expression.  There are a lot of details that go along with parsing that have little to do with interpret the next bit of text, such as token position, error context, backtracking information, etc.  I want to be able to write a routine that parses a number very simply, without knowing about all those details.  It&#8217;s the Monad that manages this extra information.  You can <a href="http://en.wikibooks.org/wiki/Haskell/Practical_monads#Parsing_monads">read more here</a>.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p>Promises are what get turned into real values when data is finally needed.&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:3">
<p>If you use profiling libraries along with <code>-prof -auto-all</code>, you can get a much clearer picture of what was executing at the time of the fault.&#160;<a href="#fnref:3" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/03/functional-yet-lazy/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Journey into Haskell, part 4</title>
		<link>http://newartisans.com/2009/03/journey-into-haskell-part-4/</link>
		<comments>http://newartisans.com/2009/03/journey-into-haskell-part-4/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 08:18:23 +0000</pubDate>
		<dc:creator>johnw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cabal]]></category>
		<category><![CDATA[HackPorts]]></category>
		<category><![CDATA[MacPorts]]></category>

		<guid isPermaLink="false">http://newartisans.com/2009/03/journey_into_haskell_part_4/</guid>
		<description><![CDATA[In the meantime, I&#8217;ve picked a toy project that also has a taste of usefulness: a script to convert the Hackage database into MacPorts Portfiles, respecting inter-package and external library dependencies.

...The impure part takes a command-line argument, interprets it as a  FilePath  (an impure type, since it must concern itself with operating system-dependent naming conventions), and reads the contents of the file at that location.

... This division into pure and impure has an interesting side-effect (no pun intended):  Most of a program&#8217;s code is written in isolation of its context of usage .

... Too many times I&#8217;ve tried to use a utility&#8217;s code as a &#8220;library&#8221;, only to find  it was so caught up in its idea of how it should be used, it had never bothered to abstract its core principles into a set of &#8220;pure&#8221; function, independent from that intent.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading <a href="http://book.realworldhaskell.org/">Real World Haskell</a> now, after having finished the delightful <a href="http://learnyouahaskell.com/">Learn You a Haskell</a> Tutorial.  I&#8217;m up to chapter 6, about to dive into Typeclasses.  In the meantime, I&#8217;ve picked a toy project that also has a taste of usefulness: a script to convert the Hackage database into MacPorts Portfiles, respecting inter-package and external library dependencies.  I call it <a href="http://github.com/jwiegley/hackports">HackPorts</a>, of course.</p>

<p><span id="more-221"></span></p>

<h2>Requirements</h2>

<p>This translation should require two things:</p>

<ol>
<li><p>The Cabal package, for read information about all packages known to it.  This avoids writing a custom parser, or using HTTP to crawl the online Hackage database.</p></li>
<li><p>A mapping file of external dependency names to MacPorts port names.  This is for dependencies on things like <code>libbz2</code>, where the script will need to be taught how MacPorts names that library.  This is likely to be the most labor-intensive step, having nothing to do with Haskell.</p></li>
</ol>

<h2>Initial experiences</h2>

<p>Haskell makes a concerted point about separating &#8220;pure&#8221; from &#8220;impure&#8221; code.  Anything which talks to the outside world, such as reading and writing files, is impure.  Anything which can be expressed in terms of standard data types &#8212; or compositions thereof &#8212; is pure.</p>

<p>Take for example a program to count lines in a file.  The pure part of the code receives a giant string, splits it into lines at line boundaries, counts those lines, and returns an integer.  The impure part takes a command-line argument, interprets it as a <code>FilePath</code> (an impure type, since it must concern itself with operating system-dependent naming conventions), and reads the contents of the file at that location.  The program flows by passed the file contents as a string to the pure code, and receiving an integer to be printed on the output device.</p>

<p>This division into pure and impure has an interesting side-effect (no pun intended): <em>Most of a program&#8217;s code is written in isolation of its context of usage</em>.  Take Cabal, as a case in point here.  Part of Cabal deals with downloading information from the Web, reading and writing package files, and executing external commands, like <code>make</code>.  But another part of Cabal is concerned only with the structure of package files, and determining the total set of dependencies required for building a package.  These latter details can be discussed in complete isolation from what is done with that information.</p>

<p>As a result &#8212; and I&#8217;m not sure whether the Cabal authors designed it this way or not &#8212; Cabal is naturally part &#8220;program&#8221;, and part API.  I was able to start taking apart package files almost instantly, with extremely little code.  Here&#8217;s a toy program to print out a package&#8217;s maintainer, if given the path to a <code>.cabal</code> file:</p>

<pre><code>import System.Environment (getArgs)

import Distribution.Verbosity (verbose)
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse (readPackageDescription)

main = do
  args ,- getArgs
  pkg  ,- readPackageDescription verbose (head args)
  print . maintainer . packageDescription $ pkg
</code></pre>

<p>Now, I do suppose it&#039;s just as easy to do a similar thing in Python&#039;s distutils, for example:</p>

<pre><code>import sys

from distutils.extension import *

exts = read_setup_file(sys.argv[1])
print exts[0].language       # print the ext &amp;#039;language&amp;#039;
</code></pre>

<p>What excites me is that Haskell uniquely encourages the separation of alogrithm and application &#8212; the isolation of context-dependent knowledge into as small a region of a program as possible.</p>

<p>Too many times I&#039;ve tried to use a utility&#039;s code as a &#8220;library&#8221;, only to find  it was so caught up in its idea of how it should be used, it had never bothered to abstract its core principles into a set of &#8220;pure&#8221; function, independent from that intent.  This happens, for example, with the version control system Git.  Although many have wanted a <code>libgit.a</code> for accessing Git&#039;s data structures directly from other languages, yet none exists.  One is forced to either shell out to the <code>git</code> command, or write another implementation to interface with the &#8220;pure&#8221; side of what Git does.</p>
]]></content:encoded>
			<wfw:commentRss>http://newartisans.com/2009/03/journey-into-haskell-part-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

