{"id":821,"date":"2009-11-10T13:46:03","date_gmt":"2009-11-10T13:46:03","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2009\/11\/10\/philosophizing-about-programming-or-why-im-learning-to-love-functional-programming\/"},"modified":"2009-11-10T13:46:03","modified_gmt":"2009-11-10T13:46:03","slug":"philosophizing-about-programming-or-why-im-learning-to-love-functional-programming","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2009\/11\/10\/philosophizing-about-programming-or-why-im-learning-to-love-functional-programming\/","title":{"rendered":"Philosophizing about Programming; or &quot;Why I&#039;m learning to love functional programming&quot;"},"content":{"rendered":"<p> Way back, about three years ago, I started writing <a href=\"http:\/\/scienceblogs.com\/goodmath\/goodmath\/programming\/haskell\/\">a Haskell tutorial<\/a> as a series of posts on this blog. After getting to <a href=\"http:\/\/scienceblogs.com\/goodmath\/2007\/01\/haskell_a_first_step_into_mona_1.php\">monads<\/a>, I moved on to other things. But based on some recent philosophizing, I think I&#8217;m going to come back to it. I&#8217;ll start by explaining why, and then over the next few days, I&#8217;ll re-run revised versions of old tutorial posts, and then start new material dealing with the more advanced topics that I didn&#8217;t get to before.<\/p>\n<p> To start with, why am I coming back to Haskell? What changed since the last time I wrote about it?<\/p>\n<p><!--more--><\/p>\n<p> I last wrote about Haskell three years ago, when I was still working for IBM. In the time since then, I&#8217;ve been working for Google. It&#8217;s been a very enlightening couple of years. Instead of working on isolated research code-bases, I&#8217;m working in a truly massive code-base. I&#8217;ve regularly written code that will be read by at least dozens of other engineers, and I regularly read code written by <em>hundreds<\/em> of other people.<\/p>\n<p> At Google, we generally program in three languages: C++, Java, and Python. None of them are functional languages: are all state-heavy, imperative, object-oriented languages. But the more I&#8217;ve read and written code in this code-base, the more I&#8217;ve found that functional code is the best way of building large things. When I look at a piece of code, if the code is basically functional, I&#8217;ve found that it&#8217;s <em>much<\/em> easier to understand, much easier to test, and much <em>less<\/em> likely to produce painful bugs. It&#8217;s gotten to the point where when I see code that isn&#8217;t functional, I cringe a little. Almost everything that I write ends up being at least mostly functional &#8211; the places where I use non-functional code, it&#8217;s because the language and compiler aren&#8217;t up to the task of keeping the code efficient.<\/p>\n<p> Writing functional code in non-functional languages is, obviously, possible. I do it pretty much every day. But it&#8217;s not easy. And it&#8217;s far <em>less<\/em> clear than it would be in a real proper functional language. As I said above, I sometimes need to compromise for efficiency; and sometimes, the language just isn&#8217;t expressive in the right way to let me do things in the way that a functional programmer really would.<\/p>\n<p> Back when I started the original Haskell tutorial, I was rather skeptical about Haskell. Functional languages have not, traditionally, been used for large, complex systems. There were lots of claims about functional systems, but no real strong evidence for those claims.<\/p>\n<p> My experiences over the last few years have convinced me that the functional approach is, really, the correct one. But why Haskell? As I&#8217;ve mentioned before, I&#8217;m an obsessive programming language guy. I know way the hell too many programming languages. In the functional realm, I&#8217;ve learned not just Haskell &#8211; but the strict typed family (like SML and OCaml); the Lisp family (CommonLisp, Scheme), other lazy languages (Clean, Miranda, Hope), and hybrid functional languages (Erlang, Clojure). And in all of those languages, I haven&#8217;t seen any that were both as clear as Haskell, and as good at managing complexity. I&#8217;m really convinced that for an awful lot of complex applications, Haskell is really right.<\/p>\n<p> This is, in many ways, a direct contradiction of what I said when introducing Haskell the first time around. Back then, I said that the fact that Haskell was <em>referentially transparent<\/em> wasn&#8217;t important. Referential transparency is another way of saying that the language is mathematically functional: that every expression in the language is a function from its inputs to its outputs, with no hidden parameters, no hidden mutable state that can change the result of a call.<\/p>\n<p> At the time, I said that I thought that the most common argument in favor of referential transparency was silly. You see, people talk about referential transparency being good because it allows you to do formal reasoning about programs. It&#8217;s close to impossible to reason about programs in languages like C++, where you&#8217;ve got things like mutable pointers to functions that contain implicit, persistent mutable state. But a lazy functional language like Haskell, you can reason about. At the time, my argument was that people don&#8217;t really reason about real, non-trivial programs, and that real complex systems would still be impossible to reason about if they were written in Haskell.<\/p>\n<p> I was wrong. Since then, I&#8217;ve done rather a lot of reasoning about programs.  Sometimes that&#8217;s been in the context of dealing with concurrency, when I&#8217;ve got a strange, intermittent bug which I can&#8217;t reliably reproduce, and so formally reasoning about the possible behaviors of the system was the only way to figure out what was going on. Other times, I&#8217;ve been working on things that are just too expensive to debug &#8211; once they&#8217;ve shown that they can fail, you can&#8217;t deploy test runs on a thousand machines to see if, maybe, you can reproduce the problem and generate a useful stack trace. Even if the cost of deploying a known buggy program weren&#8217;t too expensive, sorting through stacks from a thousand machines to figure out what was going on isn&#8217;t feasible. So I&#8217;ve wound up coming back to formal reasoning.<\/p>\n<p> You <em>can<\/em> do formal reasoning about programs written in non-functional languages. But you&#8217;ve got to start by making assumptions &#8211; and if those assumptions are wrong, you end up wasting a huge amount of time. The style of the program has a huge impact on that: in general, the more functional the programming style, the easier it is to work out a valid set of assumptions to allow you to analyze the program. But no matter what, if the language itself is hostile to that kind of reasoning, you&#8217;re going to have a much harder time of it than if you were using a language that was designed for reasoning.<\/p>\n<p> Languages like Haskell, which have referential transparency, were designed for being analyzable and reasonable. What referential transparency does is buy you the ability to make very strong basic assertions about your system: assertions, not assumptions. In a functional language, you <em>know<\/em> that certain axioms are true. For example, you know that no one could have spawned a thread in the wrong place, because you can only create a thread in a threading monad; code that doesn&#8217;t have access to that monad can&#8217;t acquire locks, send messages, or spawn threads. If you use something like software transactional memory, you know that no one could have accidentally mutated something outside of a transaction &#8211; because it&#8217;s impossible. <\/p>\n<p> I&#8217;ve still got some qualms about Haskell. On one hand, it&#8217;s a very elegant language, and the functional nature of it makes it a beautiful glue language. Some of the most beautiful, clear, elegant code I&#8217;ve ever seen is written in Haskell &#8211; and that&#8217;s not because it was written by exceptional programmers, but because the nature of Haskell as a language can make things clearer than many other programming languages.<\/p>\n<p> But it&#8217;s not all good. Haskell has some serious problems. In particular, it&#8217;s got two issues that worry me enough that I&#8217;m still a bit hesitant to recommend it for a lot of applications. Those two are what I call lazy confusion, and monad complexity.<\/p>\n<p> By lazy confusion, I mean that it&#8217;s often extremely difficult to predict what&#8217;s going to happen in what order in a Haskell program. You can say what the result will be, but you can&#8217;t necessarily say what order the steps will happen in. That&#8217;s because Haskell uses <em>lazy evaluation<\/em>, which means that no computation in Haskell is <em>really<\/em> evaluated until its result is used. You can write Haskell programs that generate infinitely long lists &#8211; but it&#8217;s not a problem, because no element of the list is ever evaluated until you try to use it, and you&#8217;ll never use more that a finite number of elements. But lazy evaluation can be very confusing: even Haskell experts &#8211; even people who&#8217;ve implemented Haskell compilers! &#8211; sometimes have trouble predicting what code will be executed in what order. In order to figure out the computational complexity of algorithms or operations on data structures, people often wind up basically treating the program as if it were going to be evaluated eagerly &#8211; because analyzing the laziness is just too difficult. Laziness is <em>not<\/em> a bad thing; in fact, I&#8217;m pretty convinced that very frequently, it&#8217;s a good thing, which can make code much cleaner and clearer. But the difficulty of analyzing it is a major concern. <\/p>\n<p> Monad complexity is a very different problem. In Haskell, most code is completely stateless. It&#8217;s a pure functional language, so most code can&#8217;t possibly have side effects. There&#8217;s no assignments, no I\/O, nothing but pure functions in most Haskell code. But state is absolutely essential. To quote Simon Peyton-Jones, one of the designers of Haskell: &#8220;In the end, any program must manipulate state. A program that has no side effects whatsoever is a kindd  of black box. All you can tell is that the box gets hotter.&#8221; The way that Haskell gets around that is with a very elegant concept called a <em>monad<\/em>. A monad is a construct in the program that allows you to create an element of state, and transparently pass it through a sequence of computations. This gives you functional semantics for a stateful computation, without having to write tons of code to pass the state around. So, for example, it lets you write code like:<\/p>\n<pre>\n&gt;fancyHello :: IO ()\n&gt;fancyHello =\n&gt;    do\n&gt;       print \"What is your name?\"\n&gt;       x        print (concat [\"Hello \", x])\n<\/pre>\n<p> Great, huh? But there&#8217;s a problem: there is an object that conceptually contains the state being passed between the steps of the &#8220;do&#8221; construct. <\/p>\n<p> The reason that that&#8217;s a problem is that there are multiple different monads, to represent different kinds of state. There are monads for mutable arrays &#8211; so that you can write efficient matrix code. There are monads for parsing, so that you can write beautiful parsers. There are monads for IO, so that you can interact with the outside world. There are monads for interacting with external libraries written in non-functional libraries. There are monads for building graphical UIs. But each of them has a packet of state that needs to be passed between the steps. So if you want to be able to do more than one monadic thing &#8211; like, say, write a program with a GUI that can also read and write files &#8211; you need to be able to combine monads. And the more monads you need to combine, the more complicated and confusing things can get.<\/p>\n<p> I&#8217;ll come back to those two problems in more detail in both the revised tutorial posts, and the new posts that I&#8217;ll be writing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Way back, about three years ago, I started writing a Haskell tutorial as a series of posts on this blog. After getting to monads, I moved on to other things. But based on some recent philosophizing, I think I&#8217;m going to come back to it. I&#8217;ll start by explaining why, and then over the next [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[89],"tags":[],"class_list":["post-821","post","type-post","status-publish","format-standard","hentry","category-haskell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-df","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}