+-- ---------------------------------------------------------------------------
+-- The Maybe type, and instances
+
+data Maybe a = Nothing | Just a deriving (Eq, Ord)
+
+instance Functor Maybe where
+ fmap _ Nothing = Nothing
+ fmap f (Just a) = Just (f a)
+
+instance Monad Maybe where
+ (Just x) >>= k = k x
+ Nothing >>= _ = Nothing
+
+ (Just _) >> k = k
+ Nothing >> _ = Nothing
+
+ return = Just
+ fail _ = Nothing
+
+-- ---------------------------------------------------------------------------
+-- Functions over Maybe
+
+maybe :: b -> (a -> b) -> Maybe a -> b
+maybe n _ Nothing = n
+maybe _ f (Just x) = f x
+