Silk

Silk Blog


We're Silk. Silk is a new way to create and consume content.
Follow us on Twitter or subscribe to our feed.


Why we use Haskell Erik

development
27 comments

As a newly started company, we have a lot of technical decisions to make. One of the important ones is the choice of a programming language. Since we’re building a web application, this goes for both the client (i.e. the web browser) and the server.

On the client, there wasn’t much discussion. Javascript is the only viable choice, unless you want to use Flash or similar plugin-based models. But on the server, we had more freedom. Popular choices for web applications are dynamically typed languages like Ruby, Python and PHP, and statically typed languages like Java and C#.

However, there was another option. Two of us (me and Sebas) are enrolled in the Software Technology master program at Utrecht University, where we often use the programming language Haskell. Haskell is a functional programming language, which means that a program is composed of expressions instead of statements, as is the case in an imperative programming language.

As you might have guessed from the title of this post, we decided to use Haskell for our server side programming. What are the features that made us choose Haskell?

First, we like the functional programming model. Modeling programs based on what values are, instead of what bits to move where, is much closer to the way we think. We write down what we mean, and let the compiler figure out how to calculate it. This also leads to shorter code: Haskell is often surprisingly concise.

Another important consideration is the strong type system that Haskell has. It has powerful features that prevent you from running incorrect programs, while not getting in the way, as is often the case in traditional statically typed languages like Java or C#. This is due to the fact that Haskell has type inference: it can deduce the types of variables and functions without programmer specifications.

Something that sets Haskell apart from almost all other programming languages, is its purity: an expression in Haskell is not allowed to do anything on evaluation other than return its value (functions are not allowed to have side-effects). This has many implications. For example, data is immutable: if you modify a list, for example, you create a new copy (although smart compilers share duplicate parts for efficiency). This means that you never have to think about other parts of the code changing your data: you can always reason locally. Another implication is that your language can be lazy (and in fact, Haskell is). This means that expressions are not evaluated unless their results are needed. This can lead to performance improvements, and allows you to use infinite data structures.

If we disallow side effects, how do we perform I/O, for example? Haskell uses an abstraction called a monad to do this. A monad shows up in the type, indicating that we are performing side effects. The I/O monad allows us to force an ordering on actions, allowing us to safely perform the actions in a lazy setting. We can also use monads to keep state, have mutable variables, and many other things.

Purity also makes concurrency a lot easier: if data is immutable, there is no risk in sharing it between different threads. If you need to share mutable data between different threads, Haskell offers a few ways of dealing with this, the most interesting of which is software transactional memory (STM), a lock-free way of building composable atomic actions.

Since Haskell is a functional language, functions are first class: you can create them on the fly, pass them as arguments, return them as results, store them in a data structure, etc. This allows you to easily create powerful abstractions. For example, one often-used Haskell function is map. It takes an argument that turns a value of type a into a value of type b, and uses this to transform a list of a’s into a list of b’s by applying it to each element. This function abstracts away looping over a list. In a similar way we can create our own abstractions. This often leads to the creation of lightweight EDSLs (embedded domain specific languages), especially since Haskell allows the creation of (infix) operators as well.

The combination of conciseness, locality of data, strong types and explicit side-effects has a very important consequence: it makes refactoring easy and risk-free. You can change your code, and if it still type checks, you can often be sure that it still works. Combined with the power of abstraction this means we can quickly develop programs that can be easily understood and modified by others.

Finally, another positive aspect of Haskell is its community. There is an active blog community, a large and helpful IRC channel (#haskell) and an online package repository. There is an industrial strength compiler, a dependency tracking package manager and built-in tools like code coverage.

Of course, there are also a few risks involved in choosing a language like Haskell. Since it doesn’t have a user base as large as some other languages, there are less libraries available. This is alleviated in part by a strong C interface that Haskell provides. Because of laziness, performance problems can be harder to diagnose and fix. And of course, there are less programmers who have experience with Haskell, so finding people to help you can be harder.

But all in all, we feel that the benefits Haskell can bring us outweigh the risks. And most of all, Haskell makes programming fun!

dixiecko

good luck, guys! :)

Jordan

I think your last point is largely mitigated by the fact that when you do find a Haskell programmer, you’re almost guaranteed he’s awesome :)

Great to hear. Good luck, I’ll be dropping by every now and then to see how you guys are doing or to steal some coffee ;).

@Jordan: Good point, all of the ones I know are great! ;)

@Gracjan: No, I don’t mind at all. Thanks for doing this!

After all of this I think it is time to request a namechange Erik, Heskelink seems to be the right option

iAloha! It’s really nice to read such motivating essays about functional programming, so rare to be used for commercial development today. Thank you.

tl

Goodluck, guys!
Hope to see more posts about your experience on running Haskell as a company’s primary language.
-tl

Mark

Congratulations on your choice of Haskell!

For me, your last statement may well be the most important one: “Haskell makes programming fun!”

Loz

You can refactor “You can often be sure” into “You can’t be sure”

i think that “polish translation” is BS. the guy copied your content, and put himself down as the author. lame.

Chad

Are you guys using one of the established web frameworks (snap, yesod, etc.) or did you roll your own?

Mike

I second Chad’s question about the web framework you’re using…

Great post. It’s nice to see a description of functional programming that is understandable by people who are not already functional programmers.

Daniel

I’ll add my +1 to the “what framework are you using”? Best of luck on your project!

I’m a FP fan but lack Haskell experience. Would you also care to comment on your choice of Haskell over other statically-compiled FP languages like OCaml, Scala etc? OCaml is what I know best, am keen to hear more about others’ FP experiences…thanks for the insightful post!

Carsten

I really like Haskell and I wish I could make my living programming in big H but to be honest: it just lacks most things that make my work managable.
I get money for producing .net code, so I’m used to have really good editors and tooling – great debugers and tracers, etc.
I would miss those things a lot.

But it’s not that bad really – I still can use F#. While it’s lacking some cool stuff like type classes and real purity it comes with the full .net-stack and most of the great tooling VS offers.

Closure might be another option (for Java or .net) and to be honest – I would have settled with one of these two.

But good luck for you guys anyway – will follow you on twitter ;)

xyzzy

jorge: your post is BS. The very first line of the translation references the original article and gives the original author’s name.

Clint Moore

From one startup who has committed to going with haskell to another, we wish you the best of luck.

Andrew Bettison

You are early adopters, and you will suffer for it, so you have my best wishes and condolences at the same time. I think that in the long run, pure functional languages like Haskell will be our way out of the software complexity “crisis” (and into the next one :-), so your decision is progressive and deserves respect.

@Matt: We were already familiar with Haskell, I think that influenced our choice the most. But another reason is that OCaml, F# and Scala are impure by default, which removes one of the advantages of Haskell and prohibits some techniques (e.g. STM). I also think Haskell’s community is larger at the moment, though with Scala and F# you have the benefit of existing Java and .NET libraries.

Just came back from a tech talk on Scala by the inventor himself, Martin Odersky. He proudly explained how Twitter, LinkedIn and many other large-on-the-net companies use Scala for their backend and how for Twitter this meant saying goodbye to the fail whale that Ruby had brought them. Just saying: It’s proven technology.

Porting code from Haskell to Scala should be easy, since you are working functionally already.

But anyhow, happy coding ;-)

Did you ever consider Scala? If yes why did you choose Haskel, was it because you were already familiar with it or there are some advantages? Thanks for the insightful post and good luck!

@Mac: We never seriously considered Scala. We do think it’s an interesting and worthwhile language. Like F#, a functional language on a platform with lots of libraries has lots of potential. They’re not pure, though, which gives us a lot. As an example, Haskell is the only language so far where STM works.

Eklerks

@Andrew Bettison

At our company we use Snap -haskell web framework- and it is actually quite easy to create very complex software. We wrote our own connection pool -for the database and the application servers-, (realtime) monitoring software, a proxy server/load-balancer, a special monad in which database transactions are enforced, all kind of concurrent data structures etc. Especially concurrency (with STM) is a breeze.

Before that we rolled our own webserver, but Snap does a better job, so we abandoned it.

The proxy server is really efficient, it adds, under normal load, a lag of 5 ms to the response of the backend servers. -Same network- Thanks to enumerators, the body of the request is send to the backend servers before we received to whole request from the client.

All things you wouldn’t dare to do in most of the script languages, for e.g. sake of stability and maintainability, are actually easy in haskell.

For web development haskell is a very sound choice. Finding programmers is difficult, but most people with a mathematical background pick it up quickly.

Post a comment