<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>.</title>
    
    
    <link rel="alternate" type="text/html" href="http://work.tinou.com/" />
    <id>tag:typepad.com,2003:weblog-59125</id>
    <updated>2009-10-31T21:54:38-07:00</updated>
    
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/typepad/tinou/work" /><feedburner:info uri="typepad/tinou/work" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
        <title>Tries, John Mitchell, Memcache and Read-Only PHP Sessions</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/aofFEhvF3nA/tries-john-mitchell-memcache-and-readonly-php-sessions.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/10/tries-john-mitchell-memcache-and-readonly-php-sessions.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20120a64613a5970b</id>
        <published>2009-10-31T21:54:38-07:00</published>
        <updated>2009-10-31T21:54:38-07:00</updated>
        <summary>Too busy with work to blog lately, but here are some random thoughts I've been meaning to write about. Hopefully I'll get a chance to "expound" (thanks SAT prep) on these topics at a later time. Burst Tries Several prominent...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Too busy with work to blog lately, but here are some random thoughts I've been meaning to write about.  Hopefully I'll get a chance to "expound" (thanks SAT prep) on these topics at a later time.</p><p><strong>Burst Tries</strong></p><p>Several prominent (Google ranking) articles on implementing autocomplete suggest using a <a href="http://en.wikipedia.org/wiki/Trie">trie data structure</a>.  Unfortunately, none of those articles point out how horribly space consuming tries are.  A naive, non-compacting trie structure is exponential in space.  With an a-z alphabet you have 26 exponent word length nodes.  That's a lot of nodes.  If you want the best of both worlds, the memory requirement of a binary search tree and the speed of a trie, try the burst trie.  Burst tries: A Fast, Efficient Data Structure for String Keys by Steffen Heinz, Justin Zobel and Hugh E. Williams.</p><p><strong>John Mitchell</strong></p><p>In my <a href="http://work.tinou.com/2009/07/wtf-is-fbounded-polymorphism.html">blog on F-Bounded Polymorphism</a> I forgot to credit <a href="http://theory.stanford.edu/people/jcm/">John Mitchell</a> as one of the authors of F-Bounded Polymorphism for Object-Oriented Programming.  This was purely accidental and has nothing to do with the fact that he's a Stanford guy.</p><p><strong>Memcached</strong></p><p>Memcached is awesome.  Locks, CAS, is there anything it can't do?  PHP's support for memcached makes PHP just barely tolerable as a language.  The "M" in LAMP needs to be officially changed to Memcache, not MySQL.</p><p><strong>PHP</strong></p><p>What a mess of a language.  It has deviated too far from its original goals.  Really miss type safety, because, "after all, run time is a little late to find out whether you have a landing gear." <a href="http://archive.eiffel.com/doc/manuals/technology/typing/paper/page.html">(Eiffel dude</a>)  Really miss all of Java's built in libraries.  I find it incredible that no one using PHP has had the need for a red-black tree, because I can't seem to find an implementation of one.  I do like PHP's request processing model better than Java's, though.  Adios threads, synchronized, volatile.    </p><p><strong>Read Only Sessions</strong></p><p>I find it really puzzling that PHP doesn't have a notion of read-only sessions.  Seems like this would be a no-brainer considering that PHP web apps are typically read dominant.  And you can't write your own quickly because there's no function to decode a session string, except to decode it and <em>overwrite</em>  the current session.  Someone did provide a function to decode the session string <a href="http://php.oregonstate.edu/manual/en/function.session-decode.php">here</a>, but I'm not sure if I fully trust it to decode everything correctly.</p><p>Think that's about it for now.</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/10/tries-john-mitchell-memcache-and-readonly-php-sessions.html</feedburner:origLink></entry>
    <entry>
        <title>My First GNOME Application</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/GxzeL344rik/my-first-gnome-application.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/09/my-first-gnome-application.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20120a599f0d7970c</id>
        <published>2009-09-02T21:01:45-07:00</published>
        <updated>2009-09-02T21:01:45-07:00</updated>
        <summary>I've worked at a couple of companies now where the hosts file is heavily leveraged to support multiple environments. Through /etc/hosts file manipulation www.mycompany.com could be pointing to either dev, QA, staging or prod. I prefer a more explicit approach...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Linux" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>I've worked at a couple of companies now where the hosts file is heavily leveraged to support multiple environments.  Through /etc/hosts file manipulation www.mycompany.com could be pointing to either dev, QA, staging or prod.  I prefer a more explicit approach but we'll leave that debate for another day.  </p><p>It's easy enough to write a little script to change hosts file, but I wanted something on the desktop that could quickly tell me my current environment.  Figured someone must have already written one but no luck.  So I decided to write one.  Problem is I'm pretty clueless about GTK, GTK+, GTK2 and GUI programming in general.</p><p>Then I stumbled upon the SystemTray class in java.awt.  It was perfect for what I wanted.  With just about 10 lines of code I was able to add this little system tray applet to the GNOME panel.</p><p style="text-align: center;"><a href="http://tinou.blogs.com/.a/6a00d83451ce5a69e20120a599ed04970c-pi" style="display: inline;"><img alt="System_tray" border="0" class="at-xid-6a00d83451ce5a69e20120a599ed04970c " src="http://tinou.blogs.com/.a/6a00d83451ce5a69e20120a599ed04970c-800wi" title="System_tray" /></a> </p><p>All it does is read in /etc/hosts and display the first line.  Now I'll never be left wondering which environment I'm pointing to.</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/09/my-first-gnome-application.html</feedburner:origLink></entry>
    <entry>
        <title>Pure Inefficiencies</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/D32EyDuEd14/pure-inefficiencies.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/08/pure-inefficiencies.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20120a4ddb4ea970b</id>
        <published>2009-08-09T20:08:13-07:00</published>
        <updated>2009-08-09T20:08:13-07:00</updated>
        <summary>Skimmed a paper titled "Pure versus Impure Lisp" by Nicholas Pippenger the other day. Impure Lisp dialects allow mutations. Pippenger wanted to know if pure Lisp, without mutation, was less "powerful". Under two conditions he showed the following, A computation...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Functional Programming" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Skimmed a paper titled "Pure versus Impure Lisp" by Nicholas Pippenger the other day.  Impure Lisp dialects allow mutations.  Pippenger wanted to know if pure Lisp, without mutation, was less "powerful".  Under two conditions he showed the following,   </p><div style="text-align: center;"><a href="http://tinou.blogs.com/.a/6a00d83451ce5a69e20120a4dd709e970b-pi" style="display: inline;"><img alt="Picture 1" border="0" class="at-xid-6a00d83451ce5a69e20120a4dd709e970b " src="http://tinou.blogs.com/.a/6a00d83451ce5a69e20120a4dd709e970b-800wi" title="Picture 1" /></a> <br /></div><p>A computation in impure Lisp upper bounded by O(n) is lower bounded in pure Lisp by big-omega n log n.  Basically, given the same problem there's a theoretical lower limit to pure Lisp's efficiency compared to impure Lisp.  </p><p>But then the guys at <a href="http://sneezy.cs.nott.ac.uk/fplunch/">FP Lunch</a> lead me to a follow-up paper titled "More Haste, Less Speed: Lazy Versus Eager Evaluation".  In it, Bird, Jone and de Moor showed that if you changed the pure Lisp evaluator to pure <strong><em>and</em></strong> lazy as oppose to Pippenger's original pure and eager then you can get the same linear time as the impure program.  Interestingly enough, the authors noted that lazy evaluation is often implemented with mutations behind the scenes (via memoization), so it's really a discussion of restricted mutation.  This is along the lines of an earlier posting I made about the <a href="http://work.tinou.com/2009/07/the-problem-with-immutability.html">problem with immutability</a> and how compilers can, behind the scenes, solve some of the inefficiencies of immutability, but then it becomes a philosophical debate on what it really means to be immutable (or pure).</p><p>And that concludes another installment of interesting topics I'll never need to know.</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/08/pure-inefficiencies.html</feedburner:origLink></entry>
    <entry>
        <title>Fail Fast, Debugging and Systems that Never Stop</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/frgYHGk11_w/fail-fast-debugging-and-systems-that-never-stop.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/08/fail-fast-debugging-and-systems-that-never-stop.html" thr:count="1" thr:updated="2009-11-13T11:30:42-08:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20120a52bb5ff970c</id>
        <published>2009-08-07T18:43:27-07:00</published>
        <updated>2009-08-07T18:43:27-07:00</updated>
        <summary>If you're a Java guy you're probably familiar with the phrase "fail fucking fast" in the context of Iterators (I added the fucking part). But fail fast is much more than just iterators, it's a general design principle--and a very...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Concurrency" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Design" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Distributed Computing" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Programming" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Software" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>If you're a Java guy you're probably familiar with the phrase "fail fucking fast" in the context of Iterators (I added the fucking part).  But fail fast is much more than just iterators, it's a general design principle--and a very important one at that.  </p><p><a href="http://en.wikipedia.org/wiki/Jim_Gray_%28computer_scientist%29">Jim Gray</a> on fail fast: "it [the process] should either function correctly or it should detect the fault, signal failure and stop operating."  You can replace "the process" with component, class, etc.  Whatever you designate as your logical unit, the fail fast principle remains.  What you absolutely don't want to do is detect the fault, <strong>don't</strong> signal failure and <strong>continue</strong>.  This is why <a href="http://work.tinou.com/2009/05/pardon-the-interruption-please-dont-swallow-1.html">swallowing exceptions is one of my all time pet peeves</a>.  Fail fast is often described in terms of fault detection latency, the time between initial error detection and the fault.  You want your latency to be small.  </p><p>There are two advantages to failing fast. First, easier debugging and analysis.  It complicates things greatly if your code detects errors but continues.  Eventually something will go wrong but now you can't easily track down the root cause.  I recently worked with a component (one that shall remain nameless) that doesn't follow the fail fast principle.  During initialization the component knew it was missing a configuration file but instead of halting it continued on.  When the component was finally invoked things obviously did not work, but the root cause was not obvious.  Ended up wasting two days tracking down the root cause, when it really should have taken 30 seconds.  </p><p>Second, ability to build "systems that never stop".  Failing fast is so important to building highly available systems that <a href="http://www.sics.se/%7Ejoe/">Joe Armstrong</a> talks about it at great lengths in his presentation on "systems that never stop (and Erlang)."  In this presentation he presents 6 laws,</p><ol>
<li>Isolation</li>
<li>Concurrency</li>
<li>Failure Detection</li>
<li>Fault Identification</li>
<li>Live Code Upgrade</li>
<li>Stable Storage</li>
</ol>
<p>Without failure detection you can't fail fast, and if you can't fail fast you can't achieve isolation.  Real world example: Amazon S3's massive outage last year.  A huge embarrassment to Amazon's otherwise great cloud stack.  Their code didn't have adequate failure detection, allowing a single corrupt bit to propagate throughout the system.  The result was hours of downtime while engineers scrambled.  If Amazon had been able to detect the single corrupt bit then they could have isolated the failing component instead of letting the error bring down the entire system.</p><p>Few of us will be coding S3 like systems, but it doesn't mean we can't follow the fail fast approach.  It will make debugging easier and just might prevent similar embarrassments.  I think some programmers don't like to fail fast because they worry they'll be blamed for the failure, so just pretend things are good and pray nothing bad happens downstream.  I suppose it depends on how much you believe in God.  </p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/08/fail-fast-debugging-and-systems-that-never-stop.html</feedburner:origLink></entry>
    <entry>
        <title>WTF is F-Bounded Polymorphism</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/2zJCFtz21j8/wtf-is-fbounded-polymorphism.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/wtf-is-fbounded-polymorphism.html" thr:count="3" thr:updated="2009-11-15T13:35:46-08:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e201157139317b970c</id>
        <published>2009-07-23T23:39:09-07:00</published>
        <updated>2009-10-26T09:47:35-07:00</updated>
        <summary>Here's an example of where I read random articles just because the title is very catchy: F-Bounded Polymorphism for Object-Oriented Programming by Peter Canning, William Cook, Walter Hill, Walter Olthoff and John Mitchell. Polymorphism is already confusing enough. Just look...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Languages" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Here's an example of where I read random articles just because the title is very catchy: F-Bounded Polymorphism for Object-Oriented Programming by Peter Canning, William Cook, Walter Hill, Walter Olthoff and John Mitchell.  Polymorphism is already confusing enough.  Just look at the Wikipedia entry on <a href="http://en.wikipedia.org/wiki/Type_polymorphism">type polymorphism</a>: "this article may be confusing or unclear to readers".</p><p><a href="http://tinou.blogs.com/.a/6a00d83451ce5a69e20115713906bb970c-pi" style="display: inline;"><img alt="Picture 1" border="0" class="at-xid-6a00d83451ce5a69e20115713906bb970c " src="http://tinou.blogs.com/.a/6a00d83451ce5a69e20115713906bb970c-500pi" title="Picture 1" /><br /></a></p><p><span style="text-decoration: underline;" />There's parametric polymorphism, let-polymorphism, impredicative polymorphism, ad-hoc polymorphism, intensional polymorphism and subtype polymorphism.  So WTF is F-bounded polymorphism then.  If there's no Wikipedia entry does it really exist?  Despite no mention in Wikipedia F-bounded polymorphism is pretty important and I'm using it everyday without knowing it.</p><p>Lets start with "bounded quantification".  With bounded quantification you can do something like this</p><p>
</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">Moveable {<br /> Moveable move(int x, int y)<br />}<br /><br />Moveable moveMeOneInch(Moveable m) {...}<br /></pre><p>
The method moveMeOneInch takes in some a Moveable, moves it one inch, and returns a Moveable.  It is polymorphic and will work on Cars, Trains, Points--anything that's Moveable.  All good, except we'll have to do some unsafe type casting.</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">Car c = ...<br />Car movedCar = (Car) moveMeOneInch(c);<br /></pre><p>

But with Java 5 we can do something like this</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">&lt;T extends Moveable&gt; T moveMeOneInch(T m) {...}<br /><br />Car c = ...<br />Airplane p = ...<br /><br />Car movedCar = moveMeOneInch(c);<br />Airplane movedAirplane = moveMeOneInch(p);<br /></pre><p>and get the type safety we want, because "run time is a little late to find out whether you have a landing gear".  What we've done is created an "F-bound". </p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">F-Moveable[t] = { move : int, int -&gt; t }<br />F-Moveable[Car] = { move : int, int -&gt; Car }<br />F-Moveable[Airplane] = { move : int, int -&gt; Airplane }<br /></pre>
<p>Note the type appears on both sides.  </p><p>Here's a real world example.  The Collections utility class has a max method that takes in a Collection of Comparables and return the max of the collection.</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">public static &lt;T extends Object &amp; Comparable&lt;? super T&gt;&gt; <br /> T max(Collection&lt;? extends T&gt; coll) {...}<br /><br />//Integer implements Comparable&lt;Integer&gt;<br />Collection&lt;Integer&gt; ints = ...<br />Integer i = Collections.max(ints);<br /></pre>
<p>Here we bound the type of the collection and the type is part of the bound for Comparable.  A collection of integers, and integers are comparable to integers.</p><p>That's F-bounded polymorphism in a nutshell.  Now you can say, "yea, I like Java, it supports F-bounded polymorphism."</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/wtf-is-fbounded-polymorphism.html</feedburner:origLink></entry>
    <entry>
        <title>Functional Dependency Injection for the Presentation Layer</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/le1_Dz1Gb00/functional-dependency-injection-for-the-presentation-layer.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/functional-dependency-injection-for-the-presentation-layer.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20115722d3477970b</id>
        <published>2009-07-23T20:12:43-07:00</published>
        <updated>2009-07-23T23:52:10-07:00</updated>
        <summary>A couple of years ago I wrote a now deleted blog post about how every web application should use a template engine for the presentation layer and how much I hated JSPs. Here's my second attempt at convincing the developer...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Design" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>A couple of years ago I wrote a now deleted blog post about how every web application should use a template engine for the presentation layer and how much I hated JSPs.  Here's my second attempt at convincing the developer community to stop using JSPs or similar technologies for view rendering.  I will approach things this time by piggy backing off dependency injection and functional programming.  Both dependency injection and functional programming are very popular these days and the community has largely accepted their benefits.  If I can convince you that template engines is dependency injection and functional then it's a no-brainer to start using template engines on your projects.</p><p>Think about how a typical JSP is rendered.  It'll make calls to each of the various scopes to get some information, maybe even a call to the database--or worse.  You'll have a hard time tracking down all the JSP's required data (dependencies).  Moreover, you'll have a hard time tracking down how the data got there in the first place.  This is especially true of session scope data.  Random flows in your application adds random crap to the session--good luck hunting things down.        </p><div style="text-align: center;"><img alt="JSP" border="0" src="http://tinou.blogs.com/.a/6a00d83451ce5a69e20115722cfc54970b-800wi" title="JSP" /><br /></div><p>JSPs are just too powerful, flexible a mechanism for rendering the view.  With great powers comes great responsibility.  I'm sorry to say, we programmers are not capable of such great responsibility.</p><p>Contrast the above diagram with how a template engine works.  Dependencies are "injected" into the template.  The template is very limited.  It can't make random calls to the outside world, there are no complicated scopes--there are no scopes period.  Templates can only do one thing, take some input, render the output.  The input should be immutable base types: strings, integers, etc.</p><div style="text-align: center;"><img alt="Template" border="0" src="http://tinou.blogs.com/.a/6a00d83451ce5a69e2011571387630970c-800wi" title="Template" />  <br /></div><p>Template engines <strong><em>is</em></strong> dependency injection without the cool sounding name.  Just like how Guice or Spring inject dependencies into your Java classes, template engines inject dependencies into your templates. There are no "hard links" with templates. All the benefits of dependency injection applies to template engines.  Think about how much easier it is to test, debug and maintain templates than JSPs.  </p><p>There would be virtually no objections to introducing Guice or Spring into any solution stack.  DI is a no-brainer (except that we really should have the language provide us DI, but that's <a href="http://work.tinou.com/2008/08/fixing-dependency-injection-with-interface-reification.html">another topic</a>).  Yet it amazes me that in 2009 template engines haven't reached the same level of awareness and acceptance.  </p><p>OK, fine, you're not really that into dependency injection.  How about thinking about template engines as functional and JSPs are imperative.</p><p>
</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">// imperative, full of side effects<br />String html = myJSP.render();<br /><br />// functional, referential transparency, composable<br />String myTemplate = ...<br />String html = render("Tinou", "tinou.com", <br />   "you're broke", myTemplate);<br /></pre><p>
JSP rendering is very imperative.  It's potentially full of side effects.  Calling myJSP.render() twice might give you back different HTML each time because JSPs can use gloabl state.  Basically, you have no idea what's really going to happen when you invoke render.  </p><p>Contrast this with the functional template approach.  There's a render function that renders the given template with the given arguments.  You call render a million times and it'll produce the same HTML. You can compose templates together to produce bigger, more complex output.  You can't compose JSPs--good luck with trying to "include" JSPs.  Templates give you all the advantages of the functional programming style.</p><p>I probably can continue and make an analogy of how JSPs are like GOTO statements, but if you're not convinced by now that templates are the way to go, another comparison probably won't persuade you.</p><p /><p /><p /><p /></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/functional-dependency-injection-for-the-presentation-layer.html</feedburner:origLink></entry>
    <entry>
        <title>Scala, FUD and the PCP Drug</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/Kev-FJXOU8U/scala-fud-and-the-pcp-drug.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/scala-fud-and-the-pcp-drug.html" thr:count="3" thr:updated="2009-07-16T09:03:53-07:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e201157206ce89970b</id>
        <published>2009-07-14T19:28:51-07:00</published>
        <updated>2009-07-14T21:32:34-07:00</updated>
        <summary>When something new comes along the established players often spread fear, uncertainty and doubt (FUD) in an attempt to remain dominant. But what's the opposite of FUD? I propose praise, certainty and perfection: PCP. Every now and then something new...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Languages" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Opinion" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>When something new comes along the established players often spread fear, uncertainty and doubt (FUD) in an attempt to remain dominant.  But what's the opposite of FUD?  I propose <em><strong>praise, certainty and perfection</strong></em>: <strong>PCP</strong>.  Every now and then something new comes along that so enamors early adopters they uncontrollably praise its merits, the certainty of its value and its utter perfection.  Like a drug, PCP is dangerous unless carefully supervised.
</p><p>
Take Scala, for example. There's so much PCP surround Scala I'm about to overdose.<a href="http://www.dzone.com/links/index.html">  DZone</a>'s top link for the last two days has been a blog titled <a href="http://www.khelll.com/blog/scala/scala-is-my-next-choice/">Scala is my next choice</a>.  It's a nice entry highlighting some of Scala's strengths but the author is too generous with some of the claims.  A quick read might well lead a programmer to conclude Scala will solve all of our programming problems.  Lets look at two claims.
</p>
<p><strong>Efficient</strong>
</p>
<blockquote><p>
Scala is an efficient language, actually due to the latest benchmarks Scala is almost as fast as Java. The JVM implementation of Scala compiles down to bytecode, but during so, the code passes through optimization phaze. An optimization example is the tail recursion optimization, to help you stick to the functional paradigm without sacrificing the performance.
</p></blockquote>
<p>
While the Scala compiler (which I praised <a href="http://work.tinou.com/2009/06/scalable-component-abstractions.html">here</a>) does do tail recursion optimization it's very limited, a performance hack if you will.  The tail call has to be within a final method or local function (and has to be to itself, hence the recursive qualifier).  Rich Dougherty has a nice post on <a href="http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html">tail calls in Scala</a> that goes into this further.  The fundamental issue is that unlike Scheme or Haskell there's no guarantee of tail calls in Scala because the JVM doesn't support tail calls. See John Rose's post <a href="http://blogs.sun.com/jrose/entry/tail_calls_in_the_vm">here</a> for further information on tail call support in the JVM and the difference between soft tail calls (the Scala hack) and hard tail calls. Bottom line: recursion in Scala will affect your performance.
</p>
<p><strong>Concurrency</strong>
</p>
<blockquote><p>
Thus Scala suggests another concurrency model called Actors, where an actor sends and receives asynchronous messages in it’s inbox instead of sharing data. This is called: shared nothing model. Once you stop thinking about shared data, you relief yourself from the headache of thinking of code synchronization and deadlocks problems.
</p></blockquote>
<p>
I'm not going to argue which concurrency model is better, but it's incorrect to say that you won't get deadlocks using Actors. See James Iry's post on <a href="http://james-iry.blogspot.com/2009/04/erlang-style-actors-are-all-about_16.html">deadlocks in Erlang</a>, or see <a href="http://people.csail.mit.edu/cel/">Charles Leiserson</a>'s <a href="http://www.cilk.com/multicore-e-book/">discussion of deadlocks</a> with MPI, another message based model used by the super computer guys.  Anytime there's a cycle in the dependencies you can get deadlocks, including when you're just passing messages around. Bottom line: Scala won't stop your deadlock problems. 
</p><p>
I don't have anything against Scala but lets not make Scala out to be this magically language that will solve all our performance, <a href="http://work.tinou.com/2009/06/scalas-dirty-little-secret-to-scalability.html">scalability</a> and concurrency problems.</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/scala-fud-and-the-pcp-drug.html</feedburner:origLink></entry>
    <entry>
        <title>Everything is a Function</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/thiUeRiwBuI/everything-is-a-function.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/everything-is-a-function.html" thr:count="4" thr:updated="2010-01-03T09:33:45-08:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e2011571f86041970b</id>
        <published>2009-07-12T02:21:37-07:00</published>
        <updated>2009-07-12T02:36:33-07:00</updated>
        <summary>I picked up Real World Haskell last week when Gilad Bracha warned me that I might become unemployable if I don't brush up on my functional programming skills. I found his comment amusing because I have Lisp on my resume...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Languages" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>I picked up <a href="http://www.realworldhaskell.org/blog/">Real World Haskell</a> last week when Gilad Bracha warned me that I might become <a href="http://gbracha.blogspot.com/2009/06/ban-on-imports.html">unemployable</a> if I don't brush up on my functional programming skills.  I found his comment amusing because I have Lisp on my resume and have always wondered if anybody cared that I know Lisp.  Know is an exaggeration;  all I remember is cons, car, cdr and thinking why the f*** are there so many parentheses.  Yet here I am, a decade older, reading up on lambda-calculus and folding to remain marketable.</p><p>One thing that might confuse the imperative programmer when reading Haskell code for the first time is the unfamiliar function definitions.  In Java if you have an "add" method that adds two ints and returns an int you might write it as</p>
<pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">add (int, int) : int<br /></pre><p>
or</p>
<pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">int add (int a, int b)<br /></pre>
<p>or</p>
<pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">(int, int) -&gt; int<br /></pre>
<p>In any case it's clear that add takes two ints and returns an int.  But in Haskell your add function will be </p>
<pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">add :: Int -&gt; Int -&gt; Int<br /></pre>
<p>You might guess that the first int is the first argument, the second int is the second argument, and the final int the return type.  But not really.  In Haskell all functions are single argument functions.  Add is a function that takes an int, returns a function that takes an int, returns a function that evaluates to an int.  Here's an example with the map function.  </p><p>
</p><pre style="border-style: dotted; border-width: 1px; margin: 10px 15px; padding: 5px;">map abs [-1,-3,4,-12] <br />[1,3,4,12]<br /><br />:type map<br />map :: (a -&gt; b) -&gt; [a] -&gt; [b]<br /></pre>

<p>It appears that the map function takes two arguments, a mapping function (in this case the abs function that returns the absolute value of a number) and a list, and applies the mapping function to each element of the list.  But as you can see in the type definition map is a function that takes a mapping function (a-&gt;b), returns a function that takes a list [a], returns a function that evaluates to a list [b].  </p><p>This is because the lambda-calculus which Haskell is based upon doesn't have built-in support for multi-argument functions.  It'd be pretty easy to support multi-argument functions according to Benjamin Pierce, but to confuse us imperative programmers they decided it would be easier and better to use higher-order functions (functions that accept and/or return functions). Instead of f = λ(x,y).s we have f = λx.λy.s.  Then we would just write f v w and do two "reductions" instead of writing something like f(v,w).  </p><p>Converting multi-argument functions into higher-order functions is called "currying" after Haskell Curry, for whom the Haskell language is named.  Haskell was a contemporary of Alonzo Church, who developed the lambda-calculus during the 1920s.  While at Princeton Church supervised the doctoral thesis of Alan Turing, the tape machine guy, who committed suicide in 1954 after being prosecuted for his homosexuality.   (That was for all the history buffs out there.)</p><p>Once you see it a few times the notation becomes "normal".  It's actually very elegant (among other things).  Everything is a single argument function.  I mean everything.  You know how Smalltalk guys are always saying everything is an object, well the lambda ultimate guys say everything is a function, including <a href="http://en.wikipedia.org/wiki/Church_encoding">numbers</a> like 23 and 17.  Maybe that's what Java's missing, a pithy tag line.  Java, where everything is a....</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/everything-is-a-function.html</feedburner:origLink></entry>
    <entry>
        <title>One Big Ambient Monad</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/LUHxNeRr3DE/one-big-ambient-monad.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/one-big-ambient-monad.html" thr:count="2" thr:updated="2009-07-14T09:00:58-07:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e2011571c74fdb970b</id>
        <published>2009-07-06T01:19:52-07:00</published>
        <updated>2009-07-06T17:11:19-07:00</updated>
        <summary>In my last two blogs on The von Neumann Bottleneck and The Problem with Immutability I was flushing out some thoughts on immutability, side effects and the difference between functional and imperative programming. One thing I continue wrestling with is...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Languages" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>In my last two blogs on <a href="http://work.tinou.com/2009/06/the-von-neumann-bottleneck.html">The von Neumann Bottleneck</a> and <a href="http://work.tinou.com/2009/07/the-problem-with-immutability.html">The Problem with Immutability</a> I was flushing out some thoughts on immutability, side effects and the difference between functional and imperative programming.  One thing I continue wrestling with is reconciling "no side effects" with the real world need for side effects.  We know that side effects lead to all sorts of problems but how can we eliminate side effects and still be left with useful programs?  The answer seems to lie in these magic monads that everyone keeps talking about.  So I spent some time skimming some papers on monads.  It seems as though I'm late to the party.  2007-2008 was the big year of the monads, when people were comparing them to burritos, elephants and sexual conquests.  But I'm always late to the party.  </p><p>After a day of casual reading I kind of get monads.  There's left unit, right unit, type constructor, a bind function and a couple of other goodies.  Still a bit abstract since when was the last time a Java programmer like myself ran across an explicit monad.  But something still felt off.  Philip Wadler writes in "Monads for functional programming"      </p>
<blockquote><p>
Monads provide a convenient framework for simulating effect found in other languages, such as global state, exception handling, out-put, or non-determinism
</p></blockquote><p>
So aren't monads just a neat math trick for keeping things pure when things are in fact full of side effects?  That's like me saying my apartment is clean after I shove all my junk in a closet out of sight.  No, it's still dirty, you just can't see it anymore.  </p><p>Then I saw this video of Brian Beckman of Microsoft Research explaining the state monad and I think I'm one step closer to my "ah ha" moment.  Sequential programmers (Java, C, etc.) live in a huge ambient monad.  In this ambient monad side effects (updates) are allowed.  FP forces you to live in smaller monads.  Brian Beckman:</p>
<blockquote><p>
In fact there's nothing wrong about side effects at all, it just how disciplined, how precise are you going to be in your use of side effects.  If your programming language allows you to update any variable then you can never tell by looking at a variable whether it has been side effected. All Haskell does is discipline the side effects; so if you're doing side effects you have to do them in an explicitly written monad.  So now you can tell.  The stuff in the monad may have been side effected by whatever function was ahead of you and may be side effected by whatever function is behind you...It's precise in the text of the program. In a programming environment [like c# or vb] you are in am ambient monad so everything is updatable.</p>
</blockquote><p>OK, this makes a lot more sense now.  Side effect is not this horrible, terrible, bad thing you absolutely cannot have (duh), you just need to be precise with it.  There's no way to be precise with it in Java or the imperative languages (enforce preciseness), but there is a way to be precise with it in functional languages.  Monads make this precision more convenient than passing the world around as was done in the past.</p><p>It should be noted that Beckman goes on to say that monads are not a panacea.  While reading up on Haskell one question in the back of my mind was how does Haskell do logging?  In Java I can do something like System.out.println("the value of x is " + x), but does this mean I have to use the IO monad now to do basic logging?  And Beckman points this out as a shortcoming, that to just do a printf you have to rewrite your functions to use monad.  I guess that's why there's some "unsafe" IO stuff you can do in Haskell, but as the name implies it's unsafe and the compiler can't guarantee things, as it can with monads.</p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/one-big-ambient-monad.html</feedburner:origLink></entry>
    <entry>
        <title>The Problem with Immutability</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/tinou/work/~3/326ttSoD5A0/the-problem-with-immutability.html" />
        <link rel="replies" type="text/html" href="http://work.tinou.com/2009/07/the-problem-with-immutability.html" thr:count="2" thr:updated="2009-07-02T19:06:09-07:00" />
        <id>tag:typepad.com,2003:post-6a00d83451ce5a69e20115719baad0970b</id>
        <published>2009-07-01T16:47:29-07:00</published>
        <updated>2009-07-01T16:47:30-07:00</updated>
        <summary>There's been so many articles touting the benefits of functional programming lately. Read just a few and one theme immediately emerges: no side effects is good. With immutability there's no side effects, and with no side effects life becomes a...</summary>
        <author>
            <name>Tinou Bao</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Data Structures" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Languages" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://work.tinou.com/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>There's been so many articles touting the benefits of functional programming lately.  Read just a few and one theme immediately emerges: no side effects is good.  With immutability there's no side effects, and with no side effects life becomes a lot easier.  Programs are easier to write, compose, debug and you get concurrency for "free".  While all this may be true, I feel there needs to be a little balance on the subject lest some people start to think functional programming is a silver bullet.  </p><p>We live in an imperative world where there's state and things are mutable.    This fact alone hints as to why functional programming cannot be the end all solution.  I/O is the classic example of where imperative operations are required.  For example, Haskell, a functional language, has these things called monads to do I/O.  Michael Scott writes in Programming Language Pragmatics,</p><p>
</p><blockquote><p>
Monads...acknowledge that the physical world is imperative, and that a language that needs to interact with the physical world in nontrivial ways must include imperative features.
</p></blockquote>
<p>I also recall a recent article with Joe Armstrong on state and mutability in Erlang that came to the same obvious conclusion: state and side effects are necessary, we just need to confine them so the rest of the program is not polluted.</p><p>Beyond this inherent issue of modeling the imperative physical world, there are just some things that are more imperative in nature, like counting and updating.  Destructive updating assignments may clog the von Neumann tube, but immutability creates a lot of garbage.  The need for destructive updates leads to the so-called trivial update problem.  How can we efficiently handle updates without creating new copies of the entire data structure.  In imperative languages it's very easy to do map.remove("myKey") but in a functional language you have to call a function passing in the current immutable map and getting back a new immutable map with one entry removed.  </p><p>There are two ways to deal with this.  One is to hope your compiler can optimize.  If the compiler can verify that the old map is never used after the function returns instead of creating a new copy of the map the compiler can just do the update imperatively, in-place.  Apparently there's this Sisal compiler that was shown to eliminate 99-100% of all copy operations.  And that was back in 1992.  Then again, that was very geared toward numerical calculations, so I'm not sure how things would translate to your run of the mill web app.</p><p>Another approach is to make your data structures smarter.  Smarter is probably the wrong word.  The key is to recognize the difference between imperative and functional data structures.  In imperative languages data structures are ephemeral, existing as-is, without a history.  Think of all the data structures we use everyday in Java: HashSet, HashMap, ArrayList.  They're all ephemeral.  Functional languages, because of immutability, have automatically persistent data structures.  Adding two elements to an initially empty list means you have three versions of the list: (), (e1), (e1 e2).  In the imperative world you just have a single list of three elements.</p><p>The problem is how to achieve persistence with O(1) additional space and O(1) slowdown.  Good news is really smart guys like Sleator, Tarjan and Okasaki have laid the foundation for efficient persistent data structures.  I know Rich Hickey's Clojure has persistent data structures base on Okasaki's work with some modifications to get around just the amortized guarantees.  A lot of the work around persistent data structures is around amortized running times, but Clojure has these "persistent vectors" so the time is guaranteed for certain data structures.  While much progress has been made in this area ephemeral data structures still have quite a head start.</p><p>As someone who's basically programmed in Java all his career I find all this very functional stuff very fascinating.  No doubt about functional programming's many pros, but it's also good to be aware of the cons.  I remember when Java faced much criticism because creating objects was expensive, but those criticism have largely subsided.  The Java language hasn't really changed, the bad programs we (I) write definitely haven't changed--it's just that the JVM has gotten a lot better at resource management.  So is functional programming at a similar stage, where all that's needed is improvements in compiler optimization and better functional data structures?  Or are we already there and I'm not just aware of it?  </p></div>
</content>


    <feedburner:origLink>http://work.tinou.com/2009/07/the-problem-with-immutability.html</feedburner:origLink></entry>
 
</feed><!-- ph=1 --><!-- nhm:dynamic-ssi -->
