Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

モナドについて調べていく(3)

Monads in Scalaというバッチリなタイトルの文章がありました。

これを訳して少し知識をつけたいと思います。

Here's the beginning of a little tutorial on Monads in Scala.

Scalaモナドを実現するちょっとしたチュートリアルを始めます。


Michel Schinz provided encouragement and large parts of code, especially the trick to use bind and unit

Michel Schinzがコードの大部分を提供してくれました。特にbindとunit使うためのトリックの部分です。


In Informatics, there are two (related) meanings of the word "monad":

情報科学において、「モナド」という単語には2つの関連する意味があります。

  • A triple (T,eta,mu) following some laws in category theory
  • A way of structuring functional programs


圏論(category theory)

圏論(けんろん、category theory)は、数学的構造とその間の関係を抽象的に扱う数学理論の 1 つである。考えている種類の「構造」を持った対象とその構造を反映するような対象間の射の集まりからなる圏が基本的な考察の対象になる。

圏論 - Wikipedia

The first meaning can probably not be described easily in natural language.

1つ目の意味はおそらく自然言語で簡単に記述することはできないでしょう。


Michael Arbib and Ernest Manes' "Arrows, Structure, Functors - The Categorical Imperative". describe them as (generalized monoids) in Section 10.2 and through adjointness to the forgetful functor from algebras to sets.

Michael ArbibとErnest Manesが書いた「Arrows, Structure, Functors - The Categorical Imperative」ではそれらをセクション10.2で(普遍化したモノイド)として、そして代数から集合までの忘却関手への共役性を通じて記述しています。


※Arrows, Structures, and Functors: The Categorical Imperative

1975年の著作のようです。


※Monoid(モノイド)とは

モノイドは「集合Gが関数fに関して半群であり、さらに単位元をもつもの」

[http://d.hatena.ne.jp/Otter_O/searchdiary?word=%2a%5bMonoid%5d:title=[Monoid] - 取り急ぎブログです]


※忘却関手とは

関手(かんしゅ、functor)とは、圏論における一つの圏から別の圏への対応でその構造と両立するようなものである。関手によって一つの数学体系から別の体系へのシステマティックな対応が定式化される。

関手 - Wikipedia

C が、D の対象のうちでさらに付加的な構造を持つものの圏として定式化されているとき、C の対象の付加的な構造を無視することで C から D への忘却関手(ぼうきゃくかんしゅ、forgetful functor)を考えることができる。

関手 - Wikipedia

That last connection basically makes everything that we can write down or model using abstract syntax / universal algebra a monad.

基本的に最後の関連によって抽象化したシンタックスや不偏代数を使ってモナドをわかりやすく書いたりモデル化したりします。


In order to make sense of the "triple" jargon above, say T is a polymorphic type List (including the higher-order function map:(A->B)->(List[A]->List[B])).
Then mu is nothing else but the well-known function flatten: List[List[A]] -> List[A], and eta:A -> List[A] is a constructor of singleton lists.

上記の「3重の」専門用語の意味を理解できるように、Tをポリモーフィックなタイプのリストとしましょう(map:(A->B)->(List[A]->List[B])のような高階関数を含みます。)。
それからmuは有名なフラット化する関数(List[List[A]] -> List[A])にほかなりません。
eta(A -> List[A])はシングルトンのリストのコンストラクタです。


The second view is described in a dozen of Phil Wadler's papers and shall be considered from the Scala programmer's point of view.

2番目の観点はPhil Wadlerのたくさんの論文に記述され、Scalaプログラマの観点から考察されるでしょう。


I mention category theory here because one should keep in mind that we can describe things that are not computations as monads.

ここで圏論について触れます。なぜならモナドのような算定法ではないことを記述する、ということを気に留めておくべきだからです。


It also reminds us why a monad is a collection of things taken together.

それはまたなぜモナドはひとまとめにして考えたことの集まりであるのかを私たちに思い出させてくれます。


With the List example in mind, a potentially more revealing account on monads (aka triples) can be found in the free book Barr, Wells (2000) Toposes, Triples and Theories, p. 104.

心の中のリストの例とともに、モナド(別名triples)での潜在的により意味のある記述が今は無料となっているBarrとWellsの2000年の書籍『Toposes, Triples and Theories』の104ページで見つかるでしょう。


※Toposes, Triples and Theories (Grundlehren Der Mathematischen Wissenschaften)

Toposes, Triples and Theories (Grundlehren Der Mathematischen Wissenschaften)

Toposes, Triples and Theories (Grundlehren Der Mathematischen Wissenschaften)


続きます。難しい。。。

*1:ギリシャ語のタウ、イータ、ミューと思われる