Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. {-# LANGUAGE InstanceSigs #-}
  2. {-# LANGUAGE NoImplicitPrelude #-}
  3. {-# LANGUAGE RebindableSyntax #-}
  4. {-# LANGUAGE ScopedTypeVariables #-}
  5.  
  6. module Course.Monad(
  7. Monad(..)
  8. , join
  9. , (>>=)
  10. , (<=<)
  11. ) where
  12.  
  13. import Course.Applicative hiding ((<*>))
  14. import Course.Core
  15. import Course.Functor
  16. import Course.Id
  17. import Course.List
  18. import Course.Optional
  19. import qualified Prelude as P ((=<<))
  20.  
  21. -- | All instances of the `Monad` type-class must satisfy one law. This law
  22. -- is not checked by the compiler. This law is given as:
  23. --
  24. -- * The law of associativity
  25. -- `∀f g x. g =<< (f =<< x) ≅ ((g =<<) . f) =<< x`
  26. class Applicative f => Monad f where
  27. -- Pronounced, bind.
  28. (=<<) ::
  29. (a -> f b)
  30. -> f a
  31. -> f b
  32.  
  33. infixr 1 =<<
  34.  
  35. -- | Witness that all things with (=<<) and (<$>) also have (<*>).
  36. --
  37. -- >>> Id (+10) <*> Id 8
  38. -- Id 18
  39. --
  40. -- >>> (+1) :. (*2) :. Nil <*> 1 :. 2 :. 3 :. Nil
  41. -- [2,3,4,2,4,6]
  42. --
  43. -- >>> Full (+8) <*> Full 7
  44. -- Full 15
  45. --
  46. -- >>> Empty <*> Full 7
  47. -- Empty
  48. --
  49. -- >>> Full (+8) <*> Empty
  50. -- Empty
  51. --
  52. -- >>> ((+) <*> (+10)) 3
  53. -- 16
  54. --
  55. -- >>> ((+) <*> (+5)) 3
  56. -- 11
  57. --
  58. -- >>> ((+) <*> (+5)) 1
  59. -- 7
  60. --
  61. -- >>> ((*) <*> (+10)) 3
  62. -- 39
  63. --
  64. -- >>> ((*) <*> (+2)) 3
  65. -- 15
  66. (<*>) ::
  67. Monad f =>
  68. f (a -> b)
  69. -> f a
  70. -> f b
  71. (<*>) ff fa = do
  72. f <- ff
  73. (<$>) f fa
  74.  
  75. infixl 4 <*>
  76.  
  77. -- | Binds a function on the Id monad.
  78. --
  79. -- >>> (\x -> Id(x+1)) =<< Id 2
  80. -- Id 3
  81. instance Monad Id where
  82. (=<<) ::
  83. (a -> Id b)
  84. -> Id a
  85. -> Id b
  86. (=<<) f (Id a) = f a
  87.  
  88. -- | Binds a function on a List.
  89. --
  90. -- >>> (\n -> n :. n :. Nil) =<< (1 :. 2 :. 3 :. Nil)
  91. -- [1,1,2,2,3,3]
  92. instance Monad List where
  93. (=<<) ::
  94. (a -> List b)
  95. -> List a
  96. -> List b
  97. (=<<) = flatMap
  98.  
  99. -- | Binds a function on an Optional.
  100. --
  101. -- >>> (\n -> Full (n + n)) =<< Full 7
  102. -- Full 14
  103. instance Monad Optional where
  104. (=<<) ::
  105. (a -> Optional b)
  106. -> Optional a
  107. -> Optional b
  108. (=<<) _ Empty = Empty
  109. (=<<) f (Full a) = f a
  110.  
  111. -- | Binds a function on the reader ((->) t).
  112. --
  113. -- >>> ((*) =<< (+10)) 7
  114. -- 119
  115. instance Monad ((->) t) where
  116. (=<<) :: (a -> (->) t b) -> (->) t a -> (->) t b
  117. (=<<) ff f t = ff (f t) t
  118.  
  119. -- | Flattens a combined structure to a single structure.
  120. --
  121. -- >>> join ((1 :. 2 :. 3 :. Nil) :. (1 :. 2 :. Nil) :. Nil)
  122. -- [1,2,3,1,2]
  123. --
  124. -- >>> join (Full Empty)
  125. -- Empty
  126. --
  127. -- >>> join (Full (Full 7))
  128. -- Full 7
  129. --
  130. -- >>> join (+) 7
  131. -- 14
  132. join ::
  133. Monad f =>
  134. f (f a)
  135. -> f a
  136. join mma = do
  137. ma <- mma
  138. ma
  139.  
  140. -- | Implement a flipped version of @(=<<)@, however, use only
  141. -- @join@ and @(<$>)@.
  142. -- Pronounced, bind flipped.
  143. --
  144. -- >>> ((+10) >>= (*)) 7
  145. -- 119
  146. (>>=) ::
  147. Monad f =>
  148. f a
  149. -> (a -> f b)
  150. -> f b
  151. (>>=) ma f = join $ f <$> ma
  152.  
  153. infixl 1 >>=
  154.  
  155. -- | Implement composition within the @Monad@ environment.
  156. -- Pronounced, kleisli composition.
  157. --
  158. -- >>> ((\n -> n :. n :. Nil) <=< (\n -> n+1 :. n+2 :. Nil)) 1
  159. -- [2,2,3,3]
  160. (<=<) ::
  161. Monad f =>
  162. (b -> f c)
  163. -> (a -> f b)
  164. -> a
  165. -> f c
  166. (<=<) f1 f2 a = do
  167. b <- f2 a
  168. f1 b
  169.  
  170. infixr 1 <=<
  171.  
  172. -----------------------
  173. -- SUPPORT LIBRARIES --
  174. -----------------------
  175.  
  176. instance Monad IO where
  177. (=<<) =
  178. (P.=<<)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement