Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created February 1, 2014 11:20
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanakh/8750952 to your computer and use it in GitHub Desktop.
Save tanakh/8750952 to your computer and use it in GitHub Desktop.
なぜ我々はHaskellを使うのか

スタートアップ企業 Silk が、Haskellを採用した理由。

http://engineering.silk.co/post/31920990633/why-we-use-haskell

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.

新しく始めた会社として、我々はたくさんの技術的決定を行わなければなりません。中でも重要なのは、プログラミング言語の選択です。我々はウェブアプリケーションを作っていたので、この選択がクライアント(Webブラウザなど)とサーバの両方で必要になります。

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#.

クライアントにおいては、議論は多くありませんでした。フラッシュや別のプラグインを使おうとでもしない限り、現実的な選択肢はJavaScriptしかないからです。しかし、サーバにおいては自由があります。Webアプリケーションのサーバに対する一般的な候補は、RubyやPython、PHPなどの動的型付け言語や、静的型付け言語だとJavaや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.

しかし、もっと別の選択肢もあります。我々2人(私とセバス)はUtrecht大学でソフトウェア技術の修士課程を履修しており、そこではHaskellを日常的に用いていました(訳注:Utrecht大学は実験的HaskellコンパイラUHCを有するなど、Haskellを用いた研究で有名)。Haskellは関数型言語で、これはプログラムは式(expression)の組み合わせからなることを意味します。命令型言語が文(statement)からなるというのに対応しています。

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?

この記事のタイトルからうすうす勘付いているかもしれませんが、我々はサーバサイドのプログラミングにHaskellを使うことにしました。我々が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.

まず初めに、我々が関数型のプログラミングモデルが好きだということです。値が何であるかをベースとするプログラムモデルは、どのビットがどこに移動するかというものに比べて、はるかに我々の思考に近いものです。どういうものなのかを書き下せば、それをどうやって計算するかはコンパイラが自動的に見つけてきます。このことはコードを短くすることにもつながります。Haskellのコードは多くの場合、驚くほど簡潔なものになります。

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.

もう一つの重要な理由は、Haskellが強い型システムを持っているということです。これは間違ったプログラムを実行するのを未然に防ぎ、かつJavaやC#などのような伝統的な静的型付け言語とは違って、プログラミングの妨げにならない、強力な機能です。これはHaskellが型推論を持っているおかげです。プログラマが明示することなく、変数と関数の型を推論することができるのです。

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.

Haskellが他のほとんどのプログラミング言語と異なる特徴として、純粋性というのがあります。Haskellの式はその評価の際に、値を返すということ以外何もできないことになっています(関数が副作用を持つことを許されません)。このことはたくさんの意味をもちます。たとえば、データは変更不可能です。リストの一部を変更したいような場合、全体のコピーを作ることになります(もっとも、賢いコンパイラであれば、重複部分をできる限り効率的に共有しようとするでしょう)。これは、コードの別の部分がデータを変更する可能性を考える必要がなくなることを意味します。いつでも局所的な原因のみを考えることができます。もう一つは、言語を遅延評価にすることができるというものです(そして実際に、Haskellはそうなっています)。これは式の結果は、それが必要とされるまで評価されないということを意味します。これはパフォーマンスの改善につながることがありますし、また無限データ構造を扱うことを可能にします。

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.

副作用が許されないのであれば、たとえば、I/Oの実行はどうすればいいのでしょうか?Haskellはモナドという抽象化を用いています。モナドは型に現れ、それが副作用を伴う操作を行うということを示します。I/Oモナドは動作の順序を強制することができるので、遅延評価においても、安全に副作用を行うことができます。また、モナドを用いて状態を保持したり、変更可能な変数を使ったり、その他いろいろなことを行うことができます。

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.

また純粋性は並行性を大幅に簡単にします。もしデータが変更不可能なのであれば、スレッド間でデータを共有するリスクはそもそも存在しないのです。スレッド間で変更可能なデータを共有したいのなら、Haskellはこれを安全に行うためのいくつかの方法を提供しています。最も面白いのは、Software Transactional Memory(STM)と呼ばれるものでしょう。これはロックフリーな処理を、組み合わせ可能なアトミック操作から構築するものです。

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.

Haskellは関数型言語であり、関数は第一級ですので、オンザフライでこれを作ったり、引数として渡したり、結果として返したり、データ構造に格納したり、などを行うことができます。これにより、強力な抽象化を簡単に行うことができます。たとえば、Haskellでよく使う関数の一つにmapというのがあります。これは型aの値を型bの値に変換する関数を引数にとり、これを用いて、aのリストを、各要素にその関数を適用することによって、bのリスト変換します。この関数はリスト上のループを抽象化しています。同様にして自分で抽象化を作ることができます。これは軽量EDSL(embedded domain specific languages、埋め込みドメイン特化言語)の作成に役立ちます。特に、Haskellでは(中置)演算子を自由に作ることができるので、とてもはかどります。

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.

最後に、もう一つのHaskellのよい点は、コミュニティです。活発なブログコミュニティがあり、人数の多く、役立つIRCチャンネル(#haskell)があり、そして、オンラインパッケージレポジトリがあります。業務に耐えうるクオリティーのコンパイラと、依存性を追跡するパッケージマネージャと、コードカバレッジなどのビルトインツールがあります。

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.

もちろん、Haskellのような言語を選択することによるリスクもいくつかあります。ほかの巨大ユーザベースを抱える言語ほど多くのライブラリが利用可能ではありません。しかしHaskellは強力なCへのインターフェースを持っているので、この問題は深刻ではないでしょう。遅延評価により、パフォーマンス問題の原因がわかりづらく、直しづらくなるかもしれません。そしてもちろん、Haskell経験のあるプログラマは少ないですので、協力してくれる人を探すのが難しいかもしれません。

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

しかし全体的に見て、Haskellがもたらす利益はリスクに比べて大きいと感じます。そして何より、Haskellはプログラミングを楽しくしてくれています!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment