on master help sign in
Merge pull request #85 from startling/some-instances
3d74fb2Edwin Brady
lib/Prelude.idr+21
@@ -86,6 +86,12 @@ instance MonadPlus Maybe where
86 mplus Nothing (Just y) = Just y
86 mplus Nothing (Just y) = Just y
87 mplus Nothing Nothing = Nothing
87 mplus Nothing Nothing = Nothing
88
88
89+instance Monad (Either e) where
90+ return = Right
91+
92+ (Left n) >>= _ = Left n
93+ (Right r) >>= f = f r
94+
89instance Monad List where
95instance Monad List where
90 return x = [x]
96 return x = [x]
91 m >>= f = concatMap f m
97 m >>= f = concatMap f m
@@ -100,6 +106,10 @@ instance Functor Maybe where
100 fmap f (Just x) = Just (f x)
106 fmap f (Just x) = Just (f x)
101 fmap f Nothing = Nothing
107 fmap f Nothing = Nothing
102
108
109+instance Functor (Either e) where
110+ fmap f (Left l) = Left l
111+ fmap f (Right r) = Right (f r)
112+
103instance Functor List where
113instance Functor List where
104 fmap = map
114 fmap = map
105
115
@@ -111,6 +121,17 @@ instance Applicative Maybe where
111 (Just f) <$> (Just a) = Just (f a)
121 (Just f) <$> (Just a) = Just (f a)
112 _ <$> _ = Nothing
122 _ <$> _ = Nothing
113
123
124+instance Applicative (Either e) where
125+ pure = Right
126+
127+ (Left a) <$> _ = Left a
128+ (Right f) <$> (Right r) = Right (f r)
129+ (Right _) <$> (Left l) = Left l
130+
131+instance Applicative List where
132+ pure x = [x]
133+
134+ fs <$> vs = concatMap (\f => map f vs) fs
114
135
115---- some mathematical operations
136---- some mathematical operations
116
137