module Data.Adaptive.Maybe where
import Prelude hiding (Maybe(..), maybe)
import Data.Word
import Data.Int
class AdaptMaybe a where
data Maybe a
just :: a -> Maybe a
nothing :: Maybe a
isJust :: Maybe a -> Bool
maybe :: b -> (a -> b) -> Maybe a -> b
isNothing :: AdaptMaybe a => Maybe a -> Bool
isNothing = not . isJust
fromJust :: AdaptMaybe a => Maybe a -> a
fromJust = maybe (error "Data.Adaptive.Maybe.fromJust: Nothing") id
fromMaybe :: AdaptMaybe a => a -> Maybe a -> a
fromMaybe d = maybe d id
maybeToList :: AdaptMaybe a => Maybe a -> [a]
maybeToList = maybe [] (:[])
listToMaybe :: AdaptMaybe a => [a] -> Maybe a
listToMaybe [] = nothing
listToMaybe (a:_) = just a
catMaybes :: AdaptMaybe a => [Maybe a] -> [a]
catMaybes ls = [ fromJust x | x <- ls, isJust x ]
mapMaybe :: AdaptMaybe b => (a -> Maybe b) -> [a] -> [b]
mapMaybe _ [] = []
mapMaybe f (x:xs) = maybe rs (:rs) (f x)
where
rs = mapMaybe f xs
instance AdaptMaybe Int where
data Maybe Int = NothingInt | JustInt !Int
just = JustInt
nothing = NothingInt
isJust (JustInt _) = True
isJust _ = False
maybe n _ NothingInt = n
maybe _ f (JustInt x) = f x
instance AdaptMaybe Integer where
data Maybe Integer = NothingInteger | JustInteger !Integer
just = JustInteger
nothing = NothingInteger
isJust (JustInteger _) = True
isJust _ = False
maybe n _ NothingInteger = n
maybe _ f (JustInteger x) = f x
instance AdaptMaybe Int8 where
data Maybe Int8 = NothingInt8 | JustInt8 !Int8
just = JustInt8
nothing = NothingInt8
isJust (JustInt8 _) = True
isJust _ = False
maybe n _ NothingInt8 = n
maybe _ f (JustInt8 x) = f x
instance AdaptMaybe Int16 where
data Maybe Int16 = NothingInt16 | JustInt16 !Int16
just = JustInt16
nothing = NothingInt16
isJust (JustInt16 _) = True
isJust _ = False
maybe n _ NothingInt16 = n
maybe _ f (JustInt16 x) = f x
instance AdaptMaybe Int32 where
data Maybe Int32 = NothingInt32 | JustInt32 !Int32
just = JustInt32
nothing = NothingInt32
isJust (JustInt32 _) = True
isJust _ = False
maybe n _ NothingInt32 = n
maybe _ f (JustInt32 x) = f x
instance AdaptMaybe Int64 where
data Maybe Int64 = NothingInt64 | JustInt64 !Int64
just = JustInt64
nothing = NothingInt64
isJust (JustInt64 _) = True
isJust _ = False
maybe n _ NothingInt64 = n
maybe _ f (JustInt64 x) = f x
instance AdaptMaybe Word where
data Maybe Word = NothingWord | JustWord !Word
just = JustWord
nothing = NothingWord
isJust (JustWord _) = True
isJust _ = False
maybe n _ NothingWord = n
maybe _ f (JustWord x) = f x
instance AdaptMaybe Word8 where
data Maybe Word8 = NothingWord8 | JustWord8 !Word8
just = JustWord8
nothing = NothingWord8
isJust (JustWord8 _) = True
isJust _ = False
maybe n _ NothingWord8 = n
maybe _ f (JustWord8 x) = f x
instance AdaptMaybe Word16 where
data Maybe Word16 = NothingWord16 | JustWord16 !Word16
just = JustWord16
nothing = NothingWord16
isJust (JustWord16 _) = True
isJust _ = False
maybe n _ NothingWord16 = n
maybe _ f (JustWord16 x) = f x
instance AdaptMaybe Word32 where
data Maybe Word32 = NothingWord32 | JustWord32 !Word32
just = JustWord32
nothing = NothingWord32
isJust (JustWord32 _) = True
isJust _ = False
maybe n _ NothingWord32 = n
maybe _ f (JustWord32 x) = f x
instance AdaptMaybe Word64 where
data Maybe Word64 = NothingWord64 | JustWord64 !Word64
just = JustWord64
nothing = NothingWord64
isJust (JustWord64 _) = True
isJust _ = False
maybe n _ NothingWord64 = n
maybe _ f (JustWord64 x) = f x
instance AdaptMaybe Double where
data Maybe Double = NothingDouble | JustDouble !Double
just = JustDouble
nothing = NothingDouble
isJust (JustDouble _) = True
isJust _ = False
maybe n _ NothingDouble = n
maybe _ f (JustDouble x) = f x
instance AdaptMaybe Float where
data Maybe Float = NothingFloat | JustFloat !Float
just = JustFloat
nothing = NothingFloat
isJust (JustFloat _) = True
isJust _ = False
maybe n _ NothingFloat = n
maybe _ f (JustFloat x) = f x
instance AdaptMaybe Char where
data Maybe Char = NothingChar | JustChar !Char
just = JustChar
nothing = NothingChar
isJust (JustChar _) = True
isJust _ = False
maybe n _ NothingChar = n
maybe _ f (JustChar x) = f x