@@ -10,6 +10,7 @@ import Prelude.List
1010import Prelude.Maybe
1111import Prelude.Monad
1212import Prelude.Applicative
13+import Prelude.Functor
1314import Prelude.Either
1415import Prelude.Vect
1516import Prelude.Strings
@@ -67,41 +68,11 @@ instance Show a => Show (Maybe a) where
6768 show Nothing = "Nothing"
6869 show (Just x) = "Just " ++ show x
6970
70−---- Monad instances
71−
72−instance Monad IO where
73− return t = io_return t
74− b >>= k = io_bind b k
75−
76−instance Monad Maybe where
77− return t = Just t
78−
79− Nothing >>= k = Nothing
80− (Just x) >>= k = k x
81−
82−instance MonadPlus Maybe where
83− mzero = Nothing
84−
85− mplus (Just x) _ = Just x
86− mplus Nothing (Just y) = Just y
87− mplus Nothing Nothing = Nothing
88−
89−instance Monad (Either e) where
90− return = Right
91−
92− (Left n) >>= _ = Left n
93− (Right r) >>= f = f r
94−
95−instance Monad List where
96− return x = [x]
97− m >>= f = concatMap f m
98−
99−instance MonadPlus List where
100− mzero = []
101− mplus = (++)
102−
10371---- Functor instances
10472
73+instance Functor IO where
74+ fmap f io = io_bind io (io_return . f)
75+
10576instance Functor Maybe where
10677 fmap f (Just x) = Just (f x)
10778 fmap f Nothing = Nothing
@@ -115,6 +86,11 @@ instance Functor List where
11586
11687---- Applicative instances
11788
89+instance Applicative IO where
90+ pure = io_return
91+
92+ am <$> bm = io_bind am (\f => io_bind bm (io_return . f))
93+
11894instance Applicative Maybe where
11995 pure = Just
12096
@@ -133,6 +109,39 @@ instance Applicative List where
133109
134110 fs <$> vs = concatMap (\f => map f vs) fs
135111
112+---- Monad instances
113+
114+instance Monad IO where
115+ return t = io_return t
116+ b >>= k = io_bind b k
117+
118+instance Monad Maybe where
119+ return t = Just t
120+
121+ Nothing >>= k = Nothing
122+ (Just x) >>= k = k x
123+
124+instance MonadPlus Maybe where
125+ mzero = Nothing
126+
127+ mplus (Just x) _ = Just x
128+ mplus Nothing (Just y) = Just y
129+ mplus Nothing Nothing = Nothing
130+
131+instance Monad (Either e) where
132+ return = Right
133+
134+ (Left n) >>= _ = Left n
135+ (Right r) >>= f = f r
136+
137+instance Monad List where
138+ return x = [x]
139+ m >>= f = concatMap f m
140+
141+instance MonadPlus List where
142+ mzero = []
143+ mplus = (++)
144+
136145---- some mathematical operations
137146
138147%include "math.h"