プログラマのためのモナド(圏論) / #cat4pg

プログラマのためのモナド(圏論) / #cat4pg

プログラマのための圏論勉強会 - connpass #cat4pg (Oct 16, 2018)
https://cat4pg.connpass.com/event/100728/

YouTube:
https://www.youtube.com/watch?v=fhuAlDPgi2Q

----------------------------------------------------------------------------------

■ 前回

圏論とSwiftへの応用 / iOSDC Japan 2018 - Speaker Deck
https://speakerdeck.com/inamiy/iosdc-japan-1

iOSDC Japan 2018 「圏論とSwiftへの応用」発表スライドメモ - Qiita
https://qiita.com/inamiy/items/3e0c10d5eaf234b41c3d

iOSDC Japan 2018 で発表した「圏論とSwiftへの応用」の補足 - Qiita
https://qiita.com/inamiy/items/c4e85f22273e98b8db26

Eac0bf787b5279aca5e699ece096956e?s=128

Yasuhiro Inami

October 16, 2018
Tweet

Transcript

  1. !ϓϩάϥϚͷͨΊͷ" Ϟφυ ݍ࿦ ϓϩάϥϚͷͨΊͷݍ࿦ษڧձDBUQH Ҵݟହ޺!JOBNJZ

  2. Ҵݟହ޺!JOBNJZ "CFNB57
 J04ΤϯδχΞ 4XJGU)BTLFMM&MN ݍ࿦ྺ೥ J04%$ొஃ

  3. None
  4. None
  5. ϒϩάهࣄ w ݍ࿦ͷΦεεϝษڧ๏ʢϓϩάϥϚ޲͚ʣ2JJUB w J04%$+BQBOʮݍ࿦ͱ4XJGU΁ͷԠ༻ʯൃදεϥΠυϝϞ 2JJUB w J04%$+BQBOͰൃදͨ͠ʮݍ࿦ͱ4XJGU΁ͷԠ༻ʯͷิ଍ 2JJUB

  6. !ϓϩάϥϚͷͨΊͷ" Ϟφυೖ໳

  7. ϓϩάϥϚͷͨΊͷϞφυೖ໳ ശͰߟ͑Δ'VODUPSɺ"QQMJDBUJWFͦͯ͠.POBE2JJUB

  8. !ϓϩάϥϚͷͨΊͷ" Ϟφυ ݍ࿦

  9. ͳͥϓϩάϥϚ͸ Ϟφυʹऒ͔ΕΔͷ͔ʁ

  10. None
  11. ؔ਺ܕϓϩάϥϛϯά αʔόʔαΠυ։ൃ

  12. None
  13. ΫϥΠΞϯτ։ൃ ΦϒδΣΫτࢤ޲ʴؔ਺ܕ

  14. ΫϥΠΞϯτ։ൃݴޠͷॆ࣮ʢྫɿ4XJGUʣ w0QUJPOBMDIBJOJOH •view?.superview?.frame ==
 view.flatMap { $0.superview }.map { $0.frame

    } w1SPNJTF'VUVSF •sendRequest(req).then { nextRequest($0) } wؔ਺ܕϦΞΫςΟϒϓϩάϥϛϯά wObservable.flatMap(merge, concat, first, latest, race, …)
  15. // RxSwiftΛ࢖ͬͨɺΠϯΫϦϝϯλϧαʔνͷྫ searchBar.rx.text .debounce(0.1, scheduler: backgroundScheduler) .flatMapLatest { API.fetchData(query: $0)

    } .map { $0.results.map(SectionModel.init) } .observeOn(mainScheduler) .bind(to: tableView.rx.items( cellIdentifier: "Cell", configureCell: { index, model, cell in cell.textLabel?.text = model } ) .disposed(by: disposeBag)
  16. // RxSwiftΛ࢖ͬͨɺΠϯΫϦϝϯλϧαʔνͷྫ searchBar.rx.text .debounce(0.1, scheduler: backgroundScheduler) .flatMapLatest { API.fetchData(query: $0)

    } .map { $0.results.map(SectionModel.init) } .observeOn(mainScheduler) .bind(to: tableView.rx.items( cellIdentifier: "Cell", configureCell: { index, model, cell in cell.textLabel?.text = model } ) .disposed(by: disposeBag)
  17. Ϟφυ flatMap / then / >>=

  18. ͦͷଞͷϞφυ׆༻ࣄྫ w)BTLFMMEPߏจ4DBMBGPS಺แදه %4-  w&JUIFSʢΤϥʔॲཧʣ w454UBUF ঢ়ଶૢ࡞  w*0ʢ෭࡞༻ʣ w$PODVSSFOUʢฒྻॲཧʣ

    w1BSTFDʢߏจղੳʣ
  19. Ϟφυߏ଄Խఆཧͷͭͷجຊ੍ޚߏ଄Λ΋ͭ  ॱ࣍ ˺"QQMJDBUJWF 
 ϓϩάϥϜʹه͞Εͨॱʹɺஞ࣍ॲཧΛߦͳ͍ͬͯ͘  ൓෮ ˺'VODUPS 


    Ұఆͷ৚͕݅ຬͨ͞Ε͍ͯΔؒॲཧΛ܁Γฦ͢  ෼ذ ˺.POBE 
 ͋Δ৚͕݅੒ཱ͢ΔͳΒॲཧ"Λɺͦ͏Ͱͳ͚Ε͹ॲཧ#Λߦ͏
  20. None
  21. A monad is just a monoid in the category of

    endofunctors, 
 what's the problem? ෆ׬શʹ͓ͯ͠Αͦਖ਼͘͠ͳ͍ϓϩάϥϛϯάݴޠখ࢙ ݩωλ͸ɺ.BD-BOFʮݍ࿦ͷجૅʯ
  22. Ϟφυ͸୯ͳΔࣗݾؔख ͷݍʹ͓͚ΔϞϊΠυର ৅ͩΑɻԿ͔໰୊Ͱ΋?

  23. None
  24. Ϟφυ͸୯ͳΔࣗݾؔख ͷݍʹ͓͚ΔϞϊΠυର ৅ͩΑɻԿ͔໰୊Ͱ΋?

  25. ࣗݾؔखͷݍ ϞϊΠυର৅

  26. ؔख Functor

  27. ݍ ݍ

  28. ݍ ݍ

  29. ݍ ݍ

  30. ܕͷݍ String [String] Int [Int] Bool [Bool] Array ܕͷݍ f

    g map(f ) map(g) g ∘ f map(g ∘ f ) map(g) ∘ map(f ) =
  31. A B C ݍ F f g g ∘ f

    ݍ F(f) F(g) F(A) F(B) F(C) F(g ∘ f) F(g) ∘ F(f) =
  32. ؔखͷ߹੒ Functor Composition

  33. A B C ݍ f g g ∘ f ݍ

    F(f) F(g) F(A) F(B) F(C) F(g ∘ f) F(g) ∘ F(f) = F
  34. ݍ ݍ F A B C f g g ∘

    f F( f ) F(g) F(A) F(B) F(C) F(g ∘ f ) F(g) ∘ F( f ) =
  35. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) F A B C f g g

    ∘ f F( f ) F(g) F(A) F(B) F(C) F(g ∘ f ) F(g) ∘ F( f ) = Cat
  36. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) F A B C f g g

    ∘ f F( f ) F(g) F(A) F(B) F(C) F(g ∘ f ) F(g) ∘ F( f ) = Cat খ͍͞ݍ G G(F( f )) G(F(g)) G(F(A)) G(F(B)) G(F(C)) G(F(g ∘ f )) G(F(g)) ∘ G(F( f )) =
  37. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) F A B C f g g

    ∘ f F( f ) F(g) F(A) F(B) F(C) F(g ∘ f ) F(g) ∘ F( f ) = Cat খ͍͞ݍ G G(F( f )) G(F(g)) G(F(A)) G(F(B)) G(F(C)) G(F(g ∘ f )) G(F(g)) ∘ G(F( f )) = G ∘ F
  38. A B C ݍ G ∘ F f g g

    ∘ f ݍ G(F(f)) G(F(g)) G(F(A)) G(F(B)) G(F(g ∘ f)) G(F(g)) ∘ G(F(f)) = G(F(C))
  39. (খ͍͞ݍͷݍ) F C D Cat

  40. (খ͍͞ݍͷݍ) F G C D E Cat G ∘ F

    ؔखͷ߹੒
  41. (খ͍͞ݍͷݍ) F G C D E Cat G ∘ F

    I I I ߃౳ؔख ؔखͷ߹੒
  42. (খ͍͞ݍͷݍ) Cat F G C D E G ∘ F

    E′ H H ∘ G (H ∘ G) ∘ F H ∘ (G ∘ F) ྆ऀ͸౳͍͠ (݁߹཯Λຬͨ͢)
  43. ؔखݍ Functor Category

  44. (খ͍͞ݍͷݍ) Cat F C D

  45. (খ͍͞ݍͷݍ) Cat C D F H G

  46. (খ͍͞ݍͷݍ) Cat C D F H G α β

  47. A B F f H F(A) F(B) F(f) α C

    D G(A) G(B) G(f) H(A) H(B) H(f) β G αA αB βA βB
  48. A B F f H F(A) F(B) F(f) α C

    D G(A) G(B) G(f) H(A) H(B) H(f) β G αA αB βA βB β ∘ α βA ∘ αA βB ∘ αB ࣗવม׵ͷ ਨ௚߹੒
 (݁߹཯Λຬͨ͢)
  49. (খ͍͞ݍͷݍ) Cat C D F H G α β ؔखݍ

    DC
  50. (ؔखݍ) α β F G H DC

  51. α β F G H β ∘ α id id

    id ߃౳ࣗવม׵ ࣗવม׵ͷ(ਨ௚)߹੒ (݁߹཯Λຬͨ͢) (ؔखݍ) DC
  52. ࣗݾؔखͷݍ Category of Endofunctors

  53. (খ͍͞ݍͷݍ) Cat C D F H G

  54. (খ͍͞ݍͷݍ) Cat C C F H G

  55. (খ͍͞ݍͷݍ) Cat C F G H

  56. (খ͍͞ݍͷݍ) Cat C G ∘ F F G H I

  57. (খ͍͞ݍͷݍ) Cat C G ∘ F F G H I

    ࣗݾؔखͷݍ CC α β
  58. α β F G H (ؔखݍ) DC

  59. α β F G H (ࣗݾؔखͷݍ) CC I G ∘

    F ߃౳ؔख (୯Ґݩ) ؔखͷ߹੒ (ೋ߲ԋࢉ)
  60. ϞϊΠυ Monoid

  61. ϞϊΠυ ೋ߲ԋࢉ ୯Ґݩͷଘࡏ

  62. (Int, +, 0) a + 0 = a 0 +

    a = a (a + b) + c = a + (b + c) ϞϊΠυͷྫ
  63. (Int, *, 1) a * 1 = a 1 *

    a = a (a * b) * c = a * (b * c) ϞϊΠυͷྫ
  64. (String, +, ””) a + ”” = a ”” +

    a = a (a + b) + c = a + (b + c) ϞϊΠυͷྫ
  65. (Array, +, []) a + [] = a [] +

    a = a (a + b) + c = a + (b + c) ϞϊΠυͷྫ
  66. (Bool, &&, true) a && true = a true &&

    a = a (a && b) && c = a && (b && c) ϞϊΠυͷྫ
  67. (Bool, ||, false) a || false = a false ||

    a = a (a || b) || c = a || (b || c) ϞϊΠυͷྫ
  68. (Int, max, minBound) max(a, minBound) = a max(minBound, a) =

    a max(max(a, b), c)
 = max(a, max(b, c)) ϞϊΠυͷྫ
  69. (Int, min, maxBound) min(a, maxBound) = a min(maxBound, a) =

    a min(min(a, b), c)
 = min(a, min(b, c)) ϞϊΠυͷྫ
  70. ϞϊΠυ w ू߹ɺೋ߲ԋࢉ υοτ ɺ୯Ґݩͷͭ૊
 ͰɺҎԼͷ৚݅Λຬͨ͢ɿ w ݁߹཯೚ҙͷʹ͍ͭͯ w ୯Ґݩ೚ҙͷʹ͍ͭͯ

    (a ⋅ b) ⋅ c = a ⋅ (b ⋅ c) e ⋅ a = a ⋅ e = e a, b, c ∈ M a ∈ M M ⋅ e ∈ M (M, ⋅ ,e) (M, ⋅ ,e)
  71. // Swift ʹ͓͚ΔϞϊΠυ protocol Monoid { static func <> (lhs:

    Self, rhs: Self) -> Self static var empty: Self } -- Haskell ʹ͓͚ΔϞϊΠυ class Monoid a where (<>) :: a -> a -> a mempty :: a
  72. ݍ࿦ʹ͓͚ΔϞϊΠυ Monoid in Category Theory

  73. Int 1 … 3 (+): (Int,Int) -> Int ΛࣹͰදݱ͍ͨ͠l 0

    2 ୯Ґݩ
  74. Int 0 1 2 … 3 ୯Ґݩ ϖΞΛ࡞ͬͯɾɾɾ ೋ߲ԋࢉʁ ͜ͷॻ͖ํ͸ݍ࿦Ͱ͸ͳ͍

    ΛࣹͰදݱ͍ͨ͠l (+): (Int,Int) -> Int
  75. σΧϧτੵ ੵू߹ (0,0) (1,0) (0,2) … (1,2) Int 0 1

    2 … 3 (Int,Int) ͱ΋ॻ͘ Int × Int Int 0 1 2 … 3 Int 0 1 2 … 3 ϖΞ λϓϧ (+)
  76. (0,0) (1,0) (0,2) … (1,2) Int 0 1 2 …

    3 (+) ೋ߲ԋࢉΛIntͷ ʮ֎ʯͰఆٛ͢Δ ಺෦Ͱࣹ(ߏ଄)Λ ߟ͑ͳ͍ (Int,Int) Int 0 1 2 … 3 Int 0 1 2 … 3 ϖΞ λϓϧ σΧϧτੵ ੵू߹ σΧϧτੵ Int × Int
  77. (+) : (Int, Int) -> Int ΧϦʔԽ͢Δͱɺ (+) : Int

    -> (Int -> Int) ϖΞΛ࡞ͬͯɺIntʹม׵ ϖΞΛ࡞Βͣɺ IntΛʮࣗݾؔ਺ʯʹม׵ ͱ͜ΖͰɾɾɾ
  78. ୯Ұର৅ݍ Int (0+) (1+) (2+) Int (0+) Int -> Int

    0 1 2 … 3 (1+) (2+) … (3+) (+)
  79. ୯Ұର৅ݍ Int (0+) (1+) (2+) Int (0+) Int -> Int

    0 1 2 … 3 (1+) (2+) … (3+) (+) ߃౳ม׵ ࣹͳͷͰɺ ߹੒Մೳ
  80. 0 + 1 + 2 (0+) ∘ (1+) ∘ (2+)

    (+)ͷ෦෼ద༻ ࣗݾؔ਺Խ Int (0+) Int -> Int 0 1 2 … 3 (1+) (2+) … (3+) (+)
  81. Int (0+) Int -> Int 0 1 2 … 3

    (1+) (2+) … (3+) { f in f(0) } \f -> f 0 0 + 1 + 2 (0+) ∘ (1+) ∘ (2+) ୯ҐݩΛద༻ ஋ʹ໭͢ PS
  82. Int (0+) Int -> Int 0 1 2 … 3

    (1+) (2+) … (3+) 0 + 1 + 2 (0+) ∘ (1+) ∘ (2+) ͷ෦෼ద༻ ࣗݾؔ਺Խ ୯ҐݩΛద༻ ஋ʹ໭͢ ୯Ґݩ ೋ߲ԋࢉ ୯Ґݩ ೋ߲ԋࢉ ࣗݾؔ਺͸ ϞϊΠυʂ
  83. 0 + 1 + 2 ͷ෦෼ద༻ ؔ਺Խ ୯ҐݩΛద༻ ஋ʹ໭͢ (0+)

    ∘ (1+) ∘ (2+) ୯Ґݩ ೋ߲ԋࢉ έΠϦʔදݱ ೚ҙͷϞϊΠυ͸ɺͦͷࣗݾ४ಉܕ ˺ࣗݾؔ਺ ͷϞϊΠυͷ෦෼ϞϊΠυ Int (0+) Int -> Int 0 1 2 … 3 (1+) (2+) … (3+)
  84. ϞϊΠυର৅ Monoid Object

  85. σΧϧτੵ ੵू߹ (0,0) (1,0) (0,2) … (1,2) Int 0 1

    2 … 3 Int 0 1 2 … 3 Int 0 1 2 … 3 ೋ߲ԋࢉΛIntͷ ʮ֎ʯͰఆٛ͢Δ ୯Ґݩ (Int,Int) ϖΞ ಺෦Ͱࣹ(ߏ଄)Λ ߟ͑ͳ͍ λϓϧ (+)
  86. (Int,Int) Int 0 1 2 … 3 ೋ߲ԋࢉΛIntͷ ʮ֎ʯͰఆٛ͢Δ ୯Ґݩ

    (+)
  87. Int 0 1 2 … 3 ೋ߲ԋࢉΛIntͷ ʮ֎ʯͰఆٛ͢Δ ୯Ґݩ ()

    ୯ҐݩΛܾΊΔࣹΛ ʮ֎ʯͰఆٛ͢Δ(ҰൠԽݩ) Ұݩू߹ (Int,Int) 0 (+) Unit
  88. ϞϊΠυର৅ (M, μ, η) μ η 1 ୯Ґࣹ ೋ߲ԋࢉ M

    × M M
  89. Int 0 1 2 … 3 Unit () (Int,Int) ⟨

    Int, Int ⟩ ϖΞ 0 (+) = Int ⊗ Int ⊗ = (, ) (Int × IntͷҰൠԽ)
  90. (Int × IntͷҰൠԽ) Int 0 1 2 … 3 ()

    ()  ϖΞ(ܕͰ͸ͳ͍) λϓϧܕ ʮϖΞΛ࡞Δʯ͜ͱͱʮϖΞΛܕͱ Έͳ͢(λϓϧ)ʯ͜ͱΛ෼཭͢Δ ςϯιϧੵ (ޙड़) (Int,Int) Int ⊗ Int = ⟨ Int, Int ⟩ ϖΞ ⊗ = (, )
  91. ϞϊΠυର৅ (M, μ, η) μ η I M ⊗ M

    M ୯Ґࣹ ೋ߲ԋࢉ
  92. (σΧϧτੵͷҰൠԽ) ⊗ ୯Ґର৅ (Ұݩू߹ͷҰൠԽ) μ η I M ⊗ M

    M ⊗ ςϯιϧੵ
  93. ϞϊΠμϧݍ Monoidal Category

  94. ςϯιϧੵ ⊗ : C × C → C M I

    M ⊗ M ⊗ ୯Ґର৅ (ϞϊΠμϧݍ) C (C, ⊗ ,I, α, λ, ρ)
  95. M I M ⊗ M M ⊗ I M ⊗

    M′ (M ⊗ M′) ⊗ M′′ طଘͷର৅ͷϖΞ͔Β ৽ͨͳର৅Λ࡞Γग़ͤΔ ςϯιϧੵ ⊗ : C × C → C (ϞϊΠμϧݍ) C (C, ⊗ ,I, α, λ, ρ) ⊗
  96. ςϯιϧੵ ⟨M ⊗ M′, M′′⟩ ੵͷݍ I M M ⊗

    M ⟨M, M⟩ C × C M ⊗ M′ ⟨M, M′⟩ ⋯ ⊗ (M ⊗ M′) ⊗ M′′ ݍͷϖΞ ⊗ ⋯ (ϞϊΠμϧݍ) C (C, ⊗ ,I, α, λ, ρ) ૒ؔख
  97. ϞϊΠμϧݍ wςϯιϧੵ w୯Ґର৅ w݁߹ࢠ wࠨ୯Ґࢠ wӈ୯Ґࢠ ⊗ : C ×

    C → C I ∈ Ob(C) αA,B,C : (A ⊗ B) ⊗ C ≅ ⟶ A ⊗ (B ⊗ C) λA : I ⊗ A ≅ ⟶ A ρA : A ⊗ I ≅ ⟶ A ౳߸ͷͱ͖ɺ ετϦΫτϞϊΠμϧݍ (C, ⊗ ,I, α, λ, ρ)
  98. ϞϊΠμϧݍ (C, ⊗ ,I, α, λ, ρ) ݁߹ࢠ ݁߹ॱংͷೖΕସ͑Մೳ ୯Ґࢠ

    ୯Ґݩ Λ௥ՃɾআڈՄೳ I
  99. μ η I M ⊗ M M ϞϊΠυʹʮϞϊΠμϧݍʯʹ͓͚ΔʮϞϊΠυର৅ʯ (ϞϊΠμϧݍ) (C,

    ⊗ ,I, α, λ, ρ) C
  100. ࠶ߟɿࣗݾؔखͷݍ Revisiting Category of Endofunctors

  101. α β F G H I G ∘ F ߃౳ؔख

    (୯Ґݩ) ؔखͷ߹੒ (ೋ߲ԋࢉ) (ࣗݾؔखͷݍ) CC
  102. ؔखͷ߹੒ (ೋ߲ԋࢉ) (ࣗݾؔखͷݍ) CC ߃౳ؔख (୯Ґݩ) F I F ∘

    F ∘
  103. M ⊗ M ςϯιϧੵ ⊗ : C × C →

    C ୯Ґର৅ M I M ⊗ M (ϞϊΠμϧݍ) C (C, ⊗ ,I, α, λ, ρ) ⊗
  104. ؔखͷ߹੒ˠςϯιϧੵ ߃౳ؔखˠ୯Ґର৅ ؔखͷ߹੒ʹ͍ͭͯ ݁߹཯ɾ୯Ґ཯͕੒Γཱͭ

  105. ࣗݾؔखͷݍ ʹετϦΫτϞϊΠμϧݍ

  106. Ϟφυ Monad

  107. F(C) D ݍ F C ݍ U(D) ࣹͷू߹ U ≅

  108. F(C) D ݍ F U C U(D) ݍ F(U(D)) U(F(C))

    f f F(f) U(f) counitD ηC ϵD unitC = =
  109. F(C) D ݍ F U C ݍ F(U(D)) U(F(C)) ηC

    ϵD
  110. F(C) D ݍ F U C ݍ F(U(D)) U(F(C)) ηC

    ϵD U(F(U(F(C)))) F(U(F(C)))
  111. F(C) D ݍ F U C ݍ F(U(D)) U(F(C)) ηC

    U(F(U(F(C)))) F(U(F(C))) ϵD ϵF(C)
  112. F(C) D ݍ F U C ݍ F(U(D)) U(F(C)) ηC

    U(F(U(F(C)))) F(U(F(C))) ϵD ϵF(C) UϵF(C)
  113. C ݍ T(C) ηC T2(C) T = UF UϵF(C)

  114. C ݍ ηC T T ∘ T I T(C) T2(C)

    UϵF(C)
  115. C ݍ ηC T T ∘ T μ η I

    T(C) T2(C) UϵF = ࣗݾؔखͷݍ UϵF(C)
  116. μ η I M ⊗ M M ϞϊΠυʹʮϞϊΠμϧݍʯʹ͓͚ΔʮϞϊΠυର৅ʯ (ϞϊΠμϧݍ) (C,

    ⊗ ,I, α, λ, ρ) C
  117. T T ∘ T μ η Ϟφυʹʮࣗݾؔखͷݍ(ετϦΫτϞϊΠμϧݍ)ʯ ʹ͓͚ΔʮϞϊΠυର৅ʯ (ࣗݾؔखͷݍ) CC

    (CC, ∘ ,I) I
  118. Ϟφυ wࣗݾؔख wࣗવม׵ wࣗવม׵ T : C → C μ

    : T ⊗ T → T η : I → T μ ∘ Tμ = μ ∘ μT μ ∘ Tη = μ ∘ ηT = 1T Ϟφυଇ ݁߹཯ ୯Ґ཯ (T, μ, η)
  119. Ϟφυ (T, μ, η) μ ∘ Tμ = μ ∘

    μT μ ∘ Tη = μ ∘ ηT = 1T ݁߹཯ ୯Ґ཯
  120. ʹࣗݾؔखͷݍʹ͓͚ΔϞϊΠυର৅ (T, μ, η) Ϟφυ

  121. )BTLFMMʹ͓͚ΔϞφυ class Functor f where fmap :: (a -> b)

    -> f a -> f b class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a return = pure (ࣗݾ)ؔख ΞϓϦΧςΟϒؔख Ϟφυ
  122. join :: (Monad m) => m (m a) -> m

    a join x = x >>= id )BTLFMMʹ͓͚ΔϞφυ class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a return = pure Ϟφυ
  123. join :: (Monad m) => m (m a) -> m

    a join x = x >>= id )BTLFMMʹ͓͚ΔϞφυ class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a return = pure ࣗવม׵ η : I → T
  124. join :: (Monad m) => m (m a) -> m

    a join x = x >>= id )BTLFMMʹ͓͚ΔϞφυ class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a return = pure μ : T ⊗ T → T ࣗવม׵
  125. join :: (Monad m) => m (m a) -> m

    a join x = x >>= id )BTLFMMʹ͓͚ΔϞφυ class Applicative m => Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a return = pure flatMap
  126. ϓϩάϥϛϯάͷੈքͰ͸ɺ joinΑΓ΋ flatMap(>>=)ͷํ͕ॏཁ

  127. )BTLFMMʹ͓͚ΔflatMap == (>>=) (>>=) :: m a -> (a ->

    m b) -> m b ͜ͷܕͷҙຯ͸Կʁ
  128. ΫϥΠεϦ߹੒ͱ ΫϥΠεϦɾτϦϓϧ Kleisli Composition & Kleisli Triple

  129. C B A C ࣹͷ߹੒͕Մೳ

  130. T(B) C B A T(C) C Ϟφυ͕ίυϝΠϯ(ऴҬ)ͷ ࣹ͸߹੒Ͱ͖ͳ͍

  131. C B A C B A T(B) T(C) ⋯ C

    CT ΫϥΠεϦݍ ⋯ ⋯ f g gT fT ΫϥΠεϦࣹ (A -> T(B)ͷܗͷࣹ)
  132. C B A C B A T(B) T(C) ⋯ C

    CT ⋯ ⋯ f g gT fT ྆ऀ ͷࣹͷू߹ Λ ରԠ͍ͤͨ͞
  133. f g C B A C B A T(B) T(C)

    ⋯ ⋯ C CT ⋯ gT fT gT ∘ fT ରԠͰ͖Δͱɺ ݍͷࣹͷ߹੒A -> C͕ ݍ ͷΫϥΠεϦࣹͷ߹੒ A -> T(C)ʹରԠ͢Δ CT C
  134. F G C B A C B A T(B) T(C)

    ⋯ C CT ⋯ ⋯ f g gT fT ͱͷ ਵ൐ؔखΛߟ͑Δ CT C
  135. C B A C B A T(B) T(C) ⋯ C

    CT ⋯ ⋯ f g gT fT F G ຒΊࠐΈؔख ର৅ͱࣹΛ ͦͷ··ࣸ͢ fT ≠ F(f) ஫ҙɿ
  136. f g C B A F G C B A

    T(B) T(C) ⋯ ⋯ C CT T(A) ⋯ gT fT G(fT ) G(gT ) ֦ுؔख ͷର৅Λ ͷର৅ʹࣸ͢ A CT C T(A) G(A) = T(A) G(fT ) ≠ T(f) ஫ҙɿ
  137. f g C B A F G C B A

    T(B) T(C) ⋯ C CT T(A) F gT fT G(fT ) G(gT ) T2(C) T2(B) T(f ) T(g) μB ηB ηC μA ηA ͸ϞφυͳͷͰ ͜ΕΒͷࣹ͕ଘࡏ͢Δ T μC
  138. g C B A F G C B ⋯ C

    CT T(A) F gT fT G(fT ) G(gT ) T2(B) gT ∘ fT T(f ) μB ηB μA ηA f A T(B) T(C) T2(C) T(g) μC μC ∘ T(g) ∘ f ηC ͱͷΫϥΠεϦ߹੒ f g
  139. ΫϥΠεϦ߹੒ C A gT ∘ fT f g B A

    T(B) T(C) μC ∘ T(g) ∘ f g < = < f = def < = < /PUFΫϥΠεϦ߹੒͸ ݁߹཯ɾ୯Ґ཯Λຬͨ͢
  140. wΫϥΠεϦ߹੒ ࢀߟ ؔ਺߹੒ ࠨ୯Ґ཯ return <=< f ≡ f ӈ୯Ґ཯

    f <=< return ≡ f ݁߹཯ (h <=< g) <=< f ≡ h <=< (g <=< f) Ϟφυଇ ΫϥΠεϦ߹੒ (<=<) :: (b -> m c) -> (a -> m b) -> (a -> m c) (.) :: (b -> c) -> (a -> b) -> (a -> c) ࠨ୯Ґ཯ id . f ≡ f ӈ୯Ґ཯ f . id ≡ f ݁߹཯ (h . g) . f ≡ h . (g . f)
  141. )BTLFMMʹ͓͚ΔΫϥΠεϦ߹੒ (<=<) :: Monad m => (a -> m b)

    -> m a -> m b g <=< f = join . fmap g . f μC ∘ T(g) ∘ f g < = < f = def
  142. )BTLFMMʹ͓͚ΔΫϥΠεϦ߹੒ ࣮ࡍͷίʔυ -- | Left-to-right composition of Kleisli arrows. (>=>)

    :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) f >=> g = \x -> f x >>= g -- | Right-to-left composition of Kleisli arrows. -- > (.) :: (b -> c) -> (a -> b) -> a -> c -- > (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) g <=< f = \x -> g =<< f x —- (<=<) = flip (>=>)
  143. -- | Left-to-right composition of Kleisli arrows. (>=>) :: Monad

    m => (a -> m b) -> (b -> m c) -> (a -> m c) f >=> g = \x -> f x >>= g -- | Right-to-left composition of Kleisli arrows. -- > (.) :: (b -> c) -> (a -> b) -> a -> c -- > (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) g <=< f = \x -> g =<< f x —- (<=<) = flip (>=>) (=<<) :: Monad m => (a -> m b) -> m a -> m b f =<< x = x >>= f —- (=<<) = flip (>>=) )BTLFMMʹ͓͚ΔΫϥΠεϦ߹੒ ࣮ࡍͷίʔυ flip(flatMap)
  144. (=<<) :: (a -> m b) -> m a ->

    m b ͜ͷܕͷҙຯ͸Կʁ )BTLFMMʹ͓͚Δflip(flatMap) == (=<<)
  145. (=<<) :: (a -> m b) -> m a ->

    m b ͜ͷܕͷҙຯ͸Կʁ ʮa -> m bʯͷࣹ͔Β ʮm a -> m bʯͷࣹʹࣸؔ͢਺ )BTLFMMʹ͓͚Δflip(flatMap) == (=<<)
  146. A T(B) C T(A) ηA CT B A g C

    B T(C) ⋯ G(gT ) T2(C) T(g) ηB ηC μA μC C gT fT T2(B) T(f ) μB F G G(fT ) f
  147. A T(B) C T(A) ηA CT B A g C

    B T(C) ⋯ G(gT ) T2(C) T(g) ηB ηC μA μC C gT fT T2(B) T(f ) μB F G = < < ֦ுԋࢉ
  148. A T(B) C T(A) ηA CT B A g C

    B T(C) ⋯ G(gT ) T2(C) T(g) ηB ηC μA μC C gT fT T2(B) T(f ) μB = < < ֦ுԋࢉ F G ֦ுؔख
  149. F G f A T(B) C T(A) ηA CT B

    A g C B T(C) ⋯ G(gT ) T2(C) T(g) ηB ηC μA μC C gT fT G(fT ) T2(B) T(f ) μB G(fT ) = μB ∘ T(f) ӈล͸Λ Ҿ਺ʹऔΔؔ਺ f
  150. Ϟφυɾ֦ுԋࢉ = < < A T(B) T(A) B A fT

    T2(B) T(f ) μB μB ∘ T(f) f = < < = def f
  151. Ϟφυɾ֦ுԋࢉ f A T(B) T(A) B A fT T2(B) T(f

    ) μB f = < < Λ࢖Θͣ Λ࢖ͬͯϞφυΛදݱ͢Δ μ = < < = < < ֦ுԋࢉ = < <
  152. ΫϥΠεϦɾτϦϓϧ wࣗݾؔख wࣗવม׵ w֦ுԋࢉ (T, η, = < < )

    T : C → C η : I → T = < <: (A → T(B)) → T(A) → T(B) (g = < < ) ∘ (f = < < ) = (((g = < < ) ∘ f) = < < ) (η = < < ) = 1T ݁߹཯ ୯Ґ཯ (f = < < ) ∘ η = f Ϟφυଇ ,MFJTMJ4UBSͱ΋ݴ͏ f* def = (f = < < )
  153. ΫϥΠεϦɾτϦϓϧ ʹϞφυͷjoinΛ࢖Θͣ flip(flatMap)Λ࢖ͬͨผͷܗ (T, η, = < < )

  154. )BTLFMMʹ͓͚Δ֦ுԋࢉ (=<<) :: Monad m => (a -> m b)

    -> m a -> m b (=<<) f = join . fmap f μB ∘ T(f) f = < < = def = < <
  155. ࢀߟɿjoin, <=<, =<< ͷؔ܎ -- from `join` join :: (Monad

    m) => m (m a) -> m a join x = id =<< x join x = (id <=< id) x -- from `<=<` (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) g <=< f = \a -> g =<< f a g <=< f = join . fmap g . f -- from `=<<` (=<<) :: Monad m => (a -> m b) -> m a -> m b f =<< m = (f <=< id) m f =<< m = (join . fmap f) m
  156. wΫϥΠεϦ߹੒ wϞφυflatMap ࠨ୯Ґ཯ return >=> g ≡ g ӈ୯Ґ཯ f

    >=> return ≡ f ݁߹཯ (f >=> g) >=> h ≡ f >=> (g >=> h) ࠨ୯Ґ཯ return a >>= f ≡ f a ӈ୯Ґ཯ m>>= return ≡ m ݁߹཯ (m >>= f) >>= g ≡ m >>= (\x -> f x >>= g) Ϟφυଇ(flatMap / >>=) (>=>) :: (a -> m b) -> (b -> m c) -> (a -> m c) (>>=) :: m a -> (a -> m b) -> m b (<=<)ͷٯ޲͖ ࠨ͔Βӈ΁ͷΫϥΠεϦ߹੒ f >=> g = \a -> f a >>= g
  157. ·ͱΊ

  158. ·ͱΊ wϞφυ wؔखݍɺࣗݾؔखͷݍ wϞϊΠυɺϞϊΠυର৅ɺϞϊΠμϧݍ wࣗݾؔखͷݍʹ͓͚ΔϞϊΠυର৅ wΫϥΠεϦɾτϦϓϧ wjoinͷ୅ΘΓʹflip(flatMap) /=<<Λ࢖͏

  159. (T, μ, η) (T, η, = < < )

  160. None
  161. ͞ΒʹֶͿͨΊʹ w ετϦϯάਤ 4USJOH%JBHSBN .POBET.BEF%J⒏DVMU
 4USJOHEJBHSBNT BEKVODUJPOTBOENPOBET:PV5VCF

  162. ͞ΒʹֶͿͨΊʹ wϞφυม׵ࢠ .POBE5SBOTGPSNFS  wࣗ༝Ϟφυ 'SFF.POBE  w'SFFS0QFSBUJPOBMϞφυ ࣗ༝ؔख ࣗ༝Ϟφυ

     wࢦඪ෇͖Ϟφυ *OEFYFE.POBE  w&YUFOTJCMF'SFFS&GGFDUT 'SFFS 0QFO6OJPO  w(SBEFEϞφυ ϞφυͷҰൠԽ
  163. ࢀߟจݙ wϞφυ΢ΥʔΫεϧʔ)BTLFMM wϞφυʹ͍ͭͯϞϊΠυɾϞϊΠυݍɾϞϊΠυର৅ɾϞφυٙ೦͸୳ڀͷಈػͰ͋Γɺ୳ڀͷ།Ұͷ໨త͸৴ ೦ͷ֬ఆͰ͋Δɻ wϓϩάϥϛϯάʹ͓͚ΔϞφυͷॳظͷྺ࢙ʹ͍ͭͯ࠶ؼͷ൓෮ wʮϞφυ͸୯ͳΔࣗݾؔखͷݍʹ͓͚ΔϞϊΠυର৅ͩΑɻԿ͔໰୊Ͱ΋ʁʯ2JJUB w)BTLFMMͷ.POBEΛݍ࿦ͷ.POBE,MFJTMJ5SJQMFͱରൺ͢ΔΊ΋Ί΋ wϞφυͷఆٛͱ͔ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه wϞϊΠυ͔ΒϞφυΛ࡞Δᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه w.POBET.BEF%JGpDVMU

    w/PUJPOTPG$PNQVUBUJPOBT.POPJET
  164. Thanks! Yasuhiro Inami @inamiy