Upgrade to Pro — share decks privately, control downloads, hide ads and more …

関数型プログラムと圏論-01

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for Ranks Ranks
April 16, 2019

 関数型プログラムと圏論-01

発表用スライド

Avatar for Ranks

Ranks

April 16, 2019

More Decks by Ranks

Other Decks in Technology

Transcript

  1. category is simple concept • 構成 ◦ object ▪ 集合、対象:

    A, B ◦ arrow ▪ 射: f, g, h ◦ composition ▪ 合成: ◦ • 描画することで表現可能 • また次の定義をすべて満たすこと 2 A C B
  2. category is simple concept Category 定義 • Object と Arrow

    からなる • Arrow は Domain と Codomain を有する • Domain と Codomain が一致する Arrow は合成できる ◦ 結合律: Arrow はどこから合成してもよい ◦ 単位律: Identity Arrow と Arrow の合成は Arrow になる • Identity Arrow がある ➔ composite, associative law, identity law 章ごとに説明 3
  3. Arrows as Functions • arrow ◦ morphisms ◦ 射、矢印 ◦

    写像や関数の概念を抽象化 • 合成が可能 ◦ A → B ◦ B → C が存在する時 ◦ A → C と合成 • identity morphisms ◦ A → A 4
  4. Arrows as Functions • arrow ◦ domain: 始域 dom(f) ◦

    codomain: 終域 cod(f) • A → B ◦ dom(f) = A, cod(f) = B • 部分写像は arrow でない ◦ domain のいずれも codomain に属する必要 5 B A f
  5. Arrows as Functions composition f: A → B, g: B

    → C が存在するとき composite arrow g◦f: A → C も存在 6 B A f C g g◦f
  6. Arrows as Functions composition • 結合律(Associative law): Arrow はどこから合成してもよい (

    h ◦ g ) ◦ f = h ◦ ( g ◦ f ) 7 B A f C g g◦f D h h◦(g◦f) (h◦g)◦f h◦g
  7. Arrows as Functions Identity Arrow • 同一の Object へ移す arrow

    のこと • 単位律: Identity Arrow と Arrow の合成は Arrow になる f ◦ 1A = 1B ◦ f = f 8 A 1A B A f 1A 1B A A B B 1A f f f 1B
  8. Arrows as Functions Unix, as in: lsof | grep Chrome

    • lsof ◦ An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file. • grep ◦ searches the named input FILEs for lines containing a match to the given PATTERN. 10
  9. Arrows as Functions C言語 • B f(A, a): ◦ 引数:

    型 A 値 a ◦ 戻り値: 型 B • C g(B b) • C g_after_f(A a) { return g(f(a)); } ◦ 合成 11 Haskell • f :: A -> B ◦ 型 A から 型 B ◦ :: is “has the type of…” • g :: B -> C ◦ 型 B から 型 C • g . f ◦ 合成
  10. Properties of Composition Properties • Category は満たす必要がある • 合成が associative

    であること • 全てのオブジェクトに対して identity arrow がある 12
  11. Properties of Composition Properties • 合成が associative であること ℎ ∘

    ( ∘ ) = (ℎ ∘ ) ∘ = ℎ ∘ ∘ 13 B A f C g D h h◦g h◦(g◦f) (h◦g)◦f g◦f
  12. Properties of Composition 14 Haskell(疑似) • f :: A ->

    B • g :: B -> C • h :: C -> D • h . (g . f) == (h . g) . f == h . g . f 疑似: (同等性は関数で定義されていない)
  13. Properties of Composition Properties • 全てのオブジェクトは identity arrow がある ∘

    id = id ∘ = 関数にする際、identity arrow は恒等関数となる 15
  14. Properties of Composition C++ • template T id(T x) {

    return x; } 16 Haskell • id :: a -> a ◦ a: 型変数 • id x = x ◦ x: 仮引数 Haskell • 型を型変数に置き換え ◦ 大文字は型名、小文字は型変数 • 関数の本体は式 ◦ ここでは x
  15. Properties of Composition なぜ id が必要なのか • 自分を返す関数は一見必要なさそう ➔ シンボリック変数を扱うとき役立つ

    • シンボリック変数 ◦ 数値だけでなく式を指定 ex: sin(x^2 + 1) • 高階関数 ◦ 引数や戻り値を関数にする関数 高階関数を使用する際に使う 17
  16. Properties of Composition まとめ • Object と Arrow からなる •

    Arrow は Domain と Codomain を有する • Domain と Codomain が一致する Arrow は合成できる ◦ 結合律: Arrow はどこから合成してもよい ◦ 単位律: Identity Arrow と Arrow の合成は Arrow になる • Identity Arrow がある 18
  17. Composition is the Essence of Programming プログラミングとは何か? ➔ 筆者曰く ◆

    コンピュータに何をすべきか伝えること ◆ 問題を解決するの手段 どのように問題を解決するか? ➔ 筆者曰く ◆ 問題を小さく分解して解決 ➔ 小さい問題を解決できるコードの組み合わせ 元に戻すことができる 19
  18. Composition is the Essence of Programming 美しいコードとは • 人間の限られた頭脳で簡単に処理できること •

    ちょうどいいサイズで分解されている • 構造化することが必要 オブジェクトとオブジェクトの作用を考慮 • オブジェクト指向: インターフェース • 関数型: 関数の宣言 20
  19. Composition is the Essence of Programming 圏論 • Object の中を見せなくする

    • Object は不明瞭な存在 • Arrow によって Object 同士の結びつきのみ 21
  20. Challenges Implement, as best as you can, the identity function

    in your favorite language (or the second favorite, if your favorite language happens to be Haskell). id :: a -> a id x = x 22
  21. Challenges Implement the composition function in your favorite language. It

    takes two functions as arguments and returns a function that is their composition. foo x = f ( g ( h x ) ) foo = f . g . h 23
  22. Challenges Write a program that tries to test that your

    composition function respects identity 合成関数のテストコード 24
  23. Challenges Is the world-wide web a category in any sense?

    Are links morphisms? www はカテゴリである。 • サイト: Object • リンク: Arrow 25
  24. Challenges Is Facebook a category, with people as objects and

    friendships as morphisms? • People: Object • Friendships: Arrows 26