/
Applicative.hs
171 lines (156 loc) · 3.45 KB
/
Applicative.hs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Course.Applicative(
Applicative(..)
, sequence
, replicateA
, filtering
) where
import Course.Core
import Course.Apply
import Course.Id
import Course.List
import Course.Optional
import qualified Prelude as P
class Apply f => Applicative f where
pure ::
a -> f a
-- | Witness that all things with (<*>) and pure also have (<$>).
--
-- >>> (+1) <$> (Id 2)
-- Id 3
--
-- >>> (+1) <$> Nil
-- []
--
-- >>> (+1) <$> (1 :. 2 :. 3 :. Nil)
-- [2,3,4]
(<$>) ::
Applicative f =>
(a -> b)
-> f a
-> f b
f <$> a =
-- pure :: x -> f x
-- pure f :: f (a -> b)
pure f <*> a
-- | Insert into Id.
--
-- prop> pure x == Id x
instance Applicative Id where
pure =
error "todo"
-- | Insert into a List.
--
-- prop> pure x == x :. Nil
instance Applicative List where
pure a =
a :. Nil
-- | Insert into an Optional.
--
-- prop> pure x == Full x
instance Applicative Optional where
-- pure :: a -> f a
-- pure :: a -> Optional a
pure a =
Full a
-- | Insert into a constant function.
--
-- prop> pure x y == x
instance Applicative ((->) t) where
-- pure :: a -> f a
-- pure :: a -> ((->) t a)
-- pure :: a -> (t -> a)
-- pure :: a -> t -> a
pure =
const
-- | Sequences a list of structures to a structure of list.
--
-- >>> sequence (Id 7 :. Id 8 :. Id 9 :. Nil)
-- Id [7,8,9]
--
-- >>> sequence ((1 :. 2 :. 3 :. Nil) :. (1 :. 2 :. Nil) :. Nil)
-- [[1,1],[1,2],[2,1],[2,2],[3,1],[3,2]]
--
-- >>> sequence (Full 7 :. Empty :. Nil)
-- Empty
--
-- >>> sequence (Full 7 :. Full 8 :. Nil)
-- Full [7,8]
--
-- >>> sequence ((*10) :. (+2) :. Nil) 6
-- [60,8]
sequence ::
Applicative f =>
List (f a)
-> f (List a)
sequence Nil =
pure Nil
-- pure, (<$>), (<*>), lift2, lift*...
-- h :: f a
-- t :: List (f a)
-- sequence t :: f (List a)
-- (:.) :: a -> List a -> List a
-- lift2 (:.) :: f a -> f (List a) -> f (List a)
-- lift2 (:.) h :: f (List a) -> f (List a)
-- lift2 (:.) h (sequence t) :: f (List a)
sequence (h:.t) =
lift2 (:.) h (sequence t)
-- | Replicate an effect a given number of times.
--
-- >>> replicateA 4 (Id "hi")
-- Id ["hi","hi","hi","hi"]
--
-- >>> replicateA 4 (Full "hi")
-- Full ["hi","hi","hi","hi"]
--
-- >>> replicateA 4 Empty
-- Empty
--
-- >>> replicateA 4 (*2) 5
-- [10,10,10,10]
--
-- >>> replicateA 3 ['a', 'b', 'c']
-- ["aaa","aab","aac","aba","abb","abc","aca","acb","acc","baa","bab","bac","bba","bbb","bbc","bca","bcb","bcc","caa","cab","cac","cba","cbb","cbc","cca","ccb","ccc"]
replicateA ::
Applicative f =>
Int
-> f a
-> f (List a)
replicateA =
error "todo"
-- | Filter a list with a predicate that produces an effect.
--
-- >>> filtering (Id . even) (4 :. 5 :. 6 :. Nil)
-- Id [4,6]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. Nil)
-- Full [4,5,6]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. 7 :. 8 :. 9 :. Nil)
-- Full [4,5,6,7]
--
-- >>> filtering (\a -> if a > 13 then Empty else Full (a <= 7)) (4 :. 5 :. 6 :. 13 :. 14 :. Nil)
-- Empty
--
-- >>> filtering (>) (4 :. 5 :. 6 :. 7 :. 8 :. 9 :. 10 :. 11 :. 12 :. Nil) 8
-- [9,10,11,12]
filtering ::
Applicative f =>
(a -> f Bool)
-> List a
-> f (List a)
filtering =
error "todo"
-----------------------
-- SUPPORT LIBRARIES --
-----------------------
instance Applicative IO where
pure =
P.return
instance Applicative [] where
pure =
P.return
instance Applicative P.Maybe where
pure =
P.return