git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Added rules to the Functor, Apply, Applicative, Bind, and Monad classes, partially addressing #852
88f031eJP Smith
prelude/prelude.purs+57
@@ -170,6 +170,17 @@ module Prelude
170 infixl 4 <$>
170 infixl 4 <$>
171 infixl 1 <#>
171 infixl 1 <#>
172
172
173+ -- | A Functor is intuitively a type which can be mapped over, and more formally a mapping
174+ -- | between categories that preserves structure.
175+ -- |
176+ -- | Functors should obey the following rules.
177+ -- |
178+ -- | Identity
179+ -- | (<$>) id = id
180+ -- |
181+ -- | Composition
182+ -- | forall f g. (<$>) (f . g) = ((<$>) f) . ((<$>) g)
183+ -- |
173 class Functor f where
184 class Functor f where
174 (<$>) :: forall a b. (a -> b) -> f a -> f b
185 (<$>) :: forall a b. (a -> b) -> f a -> f b
175
186
@@ -181,9 +192,34 @@ module Prelude
181
192
182 infixl 4 <*>
193 infixl 4 <*>
183
194
195+ -- | Applys are intuitively Applicatives less `pure`, and more formally a strong lax semi-
196+ -- | -monoidal endofunctor.
197+ -- |
198+ -- | Applys should obey the following rules.
199+ -- |
200+ -- | Associative Composition
201+ -- | forall f g h. (.) <$> f <*> g <*> h = f <*> (g <*> h)
202+ -- |
184 class (Functor f) <= Apply f where
203 class (Functor f) <= Apply f where
185 (<*>) :: forall a b. f (a -> b) -> f a -> f b
204 (<*>) :: forall a b. f (a -> b) -> f a -> f b
186
205
206+ -- | Applicatives are Functors which can be "applied" by sequencing composition (<*>) or embedding
207+ -- | pure expressions (pure).
208+ -- |
209+ -- | Applicatives should obey the following rules.
210+ -- |
211+ -- | Identity
212+ -- | forall v. (pure id) <*> v = v
213+ -- |
214+ -- | Composition
215+ -- | forall f g h. (pure (.)) <*> f <*> g <*> h = f <*> (g <*> h)
216+ -- |
217+ -- | Homomorphism
218+ -- | forall f x. (pure f) <*> (pure x) = pure (f x)
219+ -- |
220+ -- | Interchange
221+ -- | forall u y. u <*> (pure y) = (pure (($) y)) <*> u
222+ -- |
187 class (Apply f) <= Applicative f where
223 class (Apply f) <= Applicative f where
188 pure :: forall a. a -> f a
224 pure :: forall a. a -> f a
189
225
@@ -192,9 +228,30 @@ module Prelude
192
228
193 infixl 1 >>=
229 infixl 1 >>=
194
230
231+ -- | A Bind is an Apply with a bind operation which sequentially composes actions.
232+ -- |
233+ -- | A Bind should obey the following rule.
234+ -- |
235+ -- | Associative (as Bind)
236+ -- | forall f g x. (x >>= f) >>= g = x >>= (\k => f k >>= g)
237+ -- |
195 class (Apply m) <= Bind m where
238 class (Apply m) <= Bind m where
196 (>>=) :: forall a b. m a -> (a -> m b) -> m b
239 (>>=) :: forall a b. m a -> (a -> m b) -> m b
197
240
241+ -- | Monad is a class which can be intuitively thought of as an abstract datatype of actions or
242+ -- | more formally though of as a monoid in the category of endofunctors.
243+ -- |
244+ -- | A Monad should obey the following rules.
245+ -- |
246+ -- | Associative (as Bind)
247+ -- | forall f g x. (x >>= f) >>= g = x >>= (\k => f k >>= g)
248+ -- |
249+ -- | Left Identity
250+ -- | forall f x. pure x >>= f = f x
251+ -- |
252+ -- | Right Identity
253+ -- | forall x. x >>= pure = x
254+ -- |
198 class (Applicative m, Bind m) <= Monad m
255 class (Applicative m, Bind m) <= Monad m
199
256
200 return :: forall m a. (Monad m) => a -> m a
257 return :: forall m a. (Monad m) => a -> m a