Advertisement
Coriic

Untitled

Jun 13th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Result a = Success a | Error String
  2.  
  3. instance (Show a) => Show (Result a) where
  4.     show (Success a) = show a
  5.     show (Error string) = string
  6.  
  7. instance (Eq a) => Eq (Result a) where
  8.     (==) (Success a) (Success b) = (a==b)
  9.     (==) (Error a) (Error b) = (a==b)
  10.     (==) _ _ = False
  11.  
  12. instance Monad Result where
  13.     return a = Success a
  14.     (>>=) (Success a) f = f a
  15.     (>>=) (Error a) _ = Error a
  16.  
  17. instance Functor Result where
  18.     fmap = map'
  19.  
  20. instance Applicative Result where
  21.    pure a = Success a
  22.    (<*>) (Success f) (Success a) = Success (f a)
  23.    (<*>) (Error a) (Error b) = Error (a++b)
  24.    (<*>) (Error f) _ = Error f
  25.    (<*>) _ (Error a) = Error a
  26.  
  27. map':: (a->b)->Result a->Result b
  28. map' f (Success a) = Success (f a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement