$30 off During Our Annual Pro Sale. View Details »

プログラマのためのモナド(圏論) / #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

Yasuhiro Inami

October 16, 2018
Tweet

More Decks by Yasuhiro Inami

Other Decks in Programming

Transcript

  1. ΫϥΠΞϯτ։ൃݴޠͷॆ࣮ʢྫɿ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, …)
  2. // 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)
  3. // 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)
  4. Ϟφυߏ଄Խఆཧͷͭͷجຊ੍ޚߏ଄Λ΋ͭ  ॱ࣍ ˺"QQMJDBUJWF 
 ϓϩάϥϜʹه͞Εͨॱʹɺஞ࣍ॲཧΛߦͳ͍ͬͯ͘  ൓෮ ˺'VODUPS 


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

    endofunctors, 
 what's the problem? ෆ׬શʹ͓ͯ͠Αͦਖ਼͘͠ͳ͍ϓϩάϥϛϯάݴޠখ࢙ ݩωλ͸ɺ.BD-BOFʮݍ࿦ͷجૅʯ
  6. ܕͷݍ String [String] Int [Int] Bool [Bool] Array ܕͷݍ f

    g map(f ) map(g) g ∘ f map(g ∘ f ) map(g) ∘ map(f ) =
  7. 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) =
  8. 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
  9. ݍ ݍ 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 ) =
  10. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) 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
  11. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) 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 )) =
  12. খ͍͞ݍ খ͍͞ݍ (খ͍͞ݍͷݍ) 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
  13. 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))
  14. (খ͍͞ݍͷݍ) F G C D E Cat G ∘ F

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

    E′ H H ∘ G (H ∘ G) ∘ F H ∘ (G ∘ F) ྆ऀ͸౳͍͠ (݁߹཯Λຬͨ͢)
  16. 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
  17. 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 ࣗવม׵ͷ ਨ௚߹੒
 (݁߹཯Λຬͨ͢)
  18. α β F G H β ∘ α id id

    id ߃౳ࣗવม׵ ࣗવม׵ͷ(ਨ௚)߹੒ (݁߹཯Λຬͨ͢) (ؔखݍ) DC
  19. (খ͍͞ݍͷݍ) Cat C G ∘ F F G H I

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

    F ߃౳ؔख (୯Ґݩ) ؔखͷ߹੒ (ೋ߲ԋࢉ)
  21. (Int, +, 0) a + 0 = a 0 +

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

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

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

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

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

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

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

    a min(min(a, b), c)
 = min(a, min(b, c)) ϞϊΠυͷྫ
  29. // Swift ʹ͓͚ΔϞϊΠυ protocol Monoid { static func <> (lhs:

    Self, rhs: Self) -> Self static var empty: Self } -- Haskell ʹ͓͚ΔϞϊΠυ class Monoid a where (<>) :: a -> a -> a mempty :: a
  30. σΧϧτੵ ੵू߹ (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 ϖΞ λϓϧ (+)
  31. (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
  32. (+) : (Int, Int) -> Int ΧϦʔԽ͢Δͱɺ (+) : Int

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

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

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

    (+)ͷ෦෼ద༻ ࣗݾؔ਺Խ Int (0+) Int -> Int 0 1 2 … 3 (1+) (2+) … (3+) (+)
  36. 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
  37. Int (0+) Int -> Int 0 1 2 … 3

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

    ∘ (1+) ∘ (2+) ୯Ґݩ ೋ߲ԋࢉ έΠϦʔදݱ ೚ҙͷϞϊΠυ͸ɺͦͷࣗݾ४ಉܕ ˺ࣗݾؔ਺ ͷϞϊΠυͷ෦෼ϞϊΠυ Int (0+) Int -> Int 0 1 2 … 3 (1+) (2+) … (3+)
  39. σΧϧτੵ ੵू߹ (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) ϖΞ ಺෦Ͱࣹ(ߏ଄)Λ ߟ͑ͳ͍ λϓϧ (+)
  40. Int 0 1 2 … 3 ೋ߲ԋࢉΛIntͷ ʮ֎ʯͰఆٛ͢Δ ୯Ґݩ ()

    ୯ҐݩΛܾΊΔࣹΛ ʮ֎ʯͰఆٛ͢Δ(ҰൠԽݩ) Ұݩू߹ (Int,Int) 0 (+) Unit
  41. Int 0 1 2 … 3 Unit () (Int,Int) ⟨

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

    ()  ϖΞ(ܕͰ͸ͳ͍) λϓϧܕ ʮϖΞΛ࡞Δʯ͜ͱͱʮϖΞΛܕͱ Έͳ͢(λϓϧ)ʯ͜ͱΛ෼཭͢Δ ςϯιϧੵ (ޙड़) (Int,Int) Int ⊗ Int = ⟨ Int, Int ⟩ ϖΞ ⊗ = (, )
  43. ςϯιϧੵ ⊗ : C × C → C M I

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

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

    M ⟨M, M⟩ C × C M ⊗ M′ ⟨M, M′⟩ ⋯ ⊗ (M ⊗ M′) ⊗ M′′ ݍͷϖΞ ⊗ ⋯ (ϞϊΠμϧݍ) C (C, ⊗ ,I, α, λ, ρ) ૒ؔख
  46. ϞϊΠμϧݍ 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, α, λ, ρ)
  47. α β F G H I G ∘ F ߃౳ؔख

    (୯Ґݩ) ؔखͷ߹੒ (ೋ߲ԋࢉ) (ࣗݾؔखͷݍ) CC
  48. M ⊗ M ςϯιϧੵ ⊗ : C × C →

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

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

    ϵD U(F(U(F(C)))) F(U(F(C)))
  51. 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)
  52. 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)
  53. C ݍ ηC T T ∘ T μ η I

    T(C) T2(C) UϵF = ࣗݾؔखͷݍ UϵF(C)
  54. Ϟφυ wࣗݾؔख wࣗવม׵ wࣗવม׵ T : C → C μ

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

    μT μ ∘ Tη = μ ∘ ηT = 1T ݁߹཯ ୯Ґ཯
  56. )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 (ࣗݾ)ؔख ΞϓϦΧςΟϒؔख Ϟφυ
  57. 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 Ϟφυ
  58. 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
  59. 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 ࣗવม׵
  60. 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
  61. C B A C B A T(B) T(C) ⋯ C

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

    CT ⋯ ⋯ f g gT fT ྆ऀ ͷࣹͷू߹ Λ ରԠ͍ͤͨ͞
  63. 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
  64. F G C B A C B A T(B) T(C)

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

    CT ⋯ ⋯ f g gT fT F G ຒΊࠐΈؔख ର৅ͱࣹΛ ͦͷ··ࣸ͢ fT ≠ F(f) ஫ҙɿ
  66. 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) ஫ҙɿ
  67. 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
  68. 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
  69. ΫϥΠεϦ߹੒ C A gT ∘ fT f g B A

    T(B) T(C) μC ∘ T(g) ∘ f g < = < f = def < = < /PUFΫϥΠεϦ߹੒͸ ݁߹཯ɾ୯Ґ཯Λຬͨ͢
  70. 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)
  71. )BTLFMMʹ͓͚ΔΫϥΠεϦ߹੒ (<=<) :: Monad m => (a -> m b)

    -> m a -> m b g <=< f = join . fmap g . f μC ∘ T(g) ∘ f g < = < f = def
  72. )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 (>=>)
  73. -- | 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)
  74. (=<<) :: (a -> m b) -> m a ->

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

    m b ͜ͷܕͷҙຯ͸Կʁ ʮa -> m bʯͷࣹ͔Β ʮm a -> m bʯͷࣹʹࣸؔ͢਺ )BTLFMMʹ͓͚Δflip(flatMap) == (=<<)
  76. 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
  77. 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 = < < ֦ுԋࢉ
  78. 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 ֦ுؔख
  79. 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
  80. Ϟφυɾ֦ுԋࢉ = < < A T(B) T(A) B A fT

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

    ) μB f = < < Λ࢖Θͣ Λ࢖ͬͯϞφυΛදݱ͢Δ μ = < < = < < ֦ுԋࢉ = < <
  82. ΫϥΠεϦɾτϦϓϧ 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 = < < )
  83. )BTLFMMʹ͓͚Δ֦ுԋࢉ (=<<) :: Monad m => (a -> m b)

    -> m a -> m b (=<<) f = join . fmap f μB ∘ T(f) f = < < = def = < <
  84. ࢀߟɿ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
  85. 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
  86. ͞ΒʹֶͿͨΊʹ wϞφυม׵ࢠ .POBE5SBOTGPSNFS  wࣗ༝Ϟφυ 'SFF.POBE  w'SFFS0QFSBUJPOBMϞφυ ࣗ༝ؔख ࣗ༝Ϟφυ

     wࢦඪ෇͖Ϟφυ *OEFYFE.POBE  w&YUFOTJCMF'SFFS&GGFDUT 'SFFS 0QFO6OJPO  w(SBEFEϞφυ ϞφυͷҰൠԽ