-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathApplicative.purs
More file actions
81 lines (70 loc) · 2.8 KB
/
Applicative.purs
File metadata and controls
81 lines (70 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Control.Applicative
( class Applicative
, pure
, liftA1
, unless
, unless'
, when
, when'
, module Control.Apply
, module Data.Functor
) where
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Category ((<<<))
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.HeytingAlgebra (not)
import Data.Unit (Unit, unit)
import Type.Proxy (Proxy(..))
-- | The `Applicative` type class extends the [`Apply`](#apply) type class
-- | with a `pure` function, which can be used to create values of type `f a`
-- | from values of type `a`.
-- |
-- | Where [`Apply`](#apply) provides the ability to lift functions of two or
-- | more arguments to functions whose arguments are wrapped using `f`, and
-- | [`Functor`](#functor) provides the ability to lift functions of one
-- | argument, `pure` can be seen as the function which lifts functions of
-- | _zero_ arguments. That is, `Applicative` functors support a lifting
-- | operation for any number of function arguments.
-- |
-- | Instances must satisfy the following laws in addition to the `Apply`
-- | laws:
-- |
-- | - Identity: `(pure identity) <*> v = v`
-- | - Composition: `pure (<<<) <*> f <*> g <*> h = f <*> (g <*> h)`
-- | - Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
-- | - Interchange: `u <*> (pure y) = (pure (_ $ y)) <*> u`
class Apply f <= Applicative f where
pure :: forall a. a -> f a
instance applicativeFn :: Applicative ((->) r) where
pure x _ = x
instance applicativeArray :: Applicative Array where
pure x = [ x ]
instance applicativeProxy :: Applicative Proxy where
pure _ = Proxy
-- | `liftA1` provides a default implementation of `(<$>)` for any
-- | [`Applicative`](#applicative) functor, without using `(<$>)` as provided
-- | by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
-- | relationship.
-- |
-- | `liftA1` can therefore be used to write [`Functor`](#functor) instances
-- | as follows:
-- |
-- | ```purescript
-- | instance functorF :: Functor F where
-- | map = liftA1
-- | ```
liftA1 :: forall f a b. Applicative f => (a -> b) -> f a -> f b
liftA1 f a = pure f <*> a
-- | Perform an applicative action when a condition is true.
when :: forall m. Applicative m => Boolean -> m Unit -> m Unit
when true m = m
when false _ = pure unit
-- | Construct an applicative action when a condition is true.
when' :: forall m a. Applicative m => (a -> Boolean) -> (a -> m Unit) -> a -> m Unit
when' f m a = if f a then m a else pure unit
-- | Perform an applicative action unless a condition is true.
unless :: forall m. Applicative m => Boolean -> m Unit -> m Unit
unless = when <<< not
-- | Construct an applicative action unless a condition is true.
unless' :: forall m a. Applicative m => (a -> Boolean) -> (a -> m Unit) -> a -> m Unit
unless' = when' <<< not