module Foundation.Partial
( Partial
, PartialError
, partialError
, partial
, fromPartial
, head
, fromJust
, fromLeft
, fromRight
) where
import Basement.Compat.Base
import Basement.Compat.Identity
newtype Partial a = Partial (Identity a)
deriving (Functor, Applicative, Monad)
data PartialError = PartialError [Char] [Char]
deriving (Show,Eq,Typeable)
instance Exception PartialError
partialError :: [Char] -> [Char] -> a
partialError lbl exp = throw (PartialError lbl exp)
partial :: a -> Partial a
partial = pure
fromPartial :: Partial a -> a
fromPartial (Partial ida) = runIdentity ida
head :: [a] -> Partial a
head l = partial $
case l of
[] -> partialError "head" "empty list"
x:_ -> x
fromJust :: Maybe a -> Partial a
fromJust x = partial $
case x of
Nothing -> partialError "fromJust" "Nothing"
Just y -> y
fromRight :: Either a b -> Partial b
fromRight x = partial $
case x of
Left _ -> partialError "fromRight" "Left"
Right a -> a
fromLeft :: Either a b -> Partial a
fromLeft x = partial $
case x of
Right _ -> partialError "fromLeft" "Right"
Left a -> a