{-# LANGUAGE GeneralizedNewtypeDeriving #-}
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 ((forall a b. (a -> b) -> Partial a -> Partial b)
-> (forall a b. a -> Partial b -> Partial a) -> Functor Partial
forall a b. a -> Partial b -> Partial a
forall a b. (a -> b) -> Partial a -> Partial b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Partial a -> Partial b
fmap :: forall a b. (a -> b) -> Partial a -> Partial b
$c<$ :: forall a b. a -> Partial b -> Partial a
<$ :: forall a b. a -> Partial b -> Partial a
Functor, Functor Partial
Functor Partial =>
(forall a. a -> Partial a)
-> (forall a b. Partial (a -> b) -> Partial a -> Partial b)
-> (forall a b c.
(a -> b -> c) -> Partial a -> Partial b -> Partial c)
-> (forall a b. Partial a -> Partial b -> Partial b)
-> (forall a b. Partial a -> Partial b -> Partial a)
-> Applicative Partial
forall a. a -> Partial a
forall a b. Partial a -> Partial b -> Partial a
forall a b. Partial a -> Partial b -> Partial b
forall a b. Partial (a -> b) -> Partial a -> Partial b
forall a b c. (a -> b -> c) -> Partial a -> Partial b -> Partial c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> Partial a
pure :: forall a. a -> Partial a
$c<*> :: forall a b. Partial (a -> b) -> Partial a -> Partial b
<*> :: forall a b. Partial (a -> b) -> Partial a -> Partial b
$cliftA2 :: forall a b c. (a -> b -> c) -> Partial a -> Partial b -> Partial c
liftA2 :: forall a b c. (a -> b -> c) -> Partial a -> Partial b -> Partial c
$c*> :: forall a b. Partial a -> Partial b -> Partial b
*> :: forall a b. Partial a -> Partial b -> Partial b
$c<* :: forall a b. Partial a -> Partial b -> Partial a
<* :: forall a b. Partial a -> Partial b -> Partial a
Applicative, Applicative Partial
Applicative Partial =>
(forall a b. Partial a -> (a -> Partial b) -> Partial b)
-> (forall a b. Partial a -> Partial b -> Partial b)
-> (forall a. a -> Partial a)
-> Monad Partial
forall a. a -> Partial a
forall a b. Partial a -> Partial b -> Partial b
forall a b. Partial a -> (a -> Partial b) -> Partial b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b. Partial a -> (a -> Partial b) -> Partial b
>>= :: forall a b. Partial a -> (a -> Partial b) -> Partial b
$c>> :: forall a b. Partial a -> Partial b -> Partial b
>> :: forall a b. Partial a -> Partial b -> Partial b
$creturn :: forall a. a -> Partial a
return :: forall a. a -> Partial a
Monad)
data PartialError = PartialError [Char] [Char]
deriving (Int -> PartialError -> ShowS
[PartialError] -> ShowS
PartialError -> String
(Int -> PartialError -> ShowS)
-> (PartialError -> String)
-> ([PartialError] -> ShowS)
-> Show PartialError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PartialError -> ShowS
showsPrec :: Int -> PartialError -> ShowS
$cshow :: PartialError -> String
show :: PartialError -> String
$cshowList :: [PartialError] -> ShowS
showList :: [PartialError] -> ShowS
Show,PartialError -> PartialError -> Bool
(PartialError -> PartialError -> Bool)
-> (PartialError -> PartialError -> Bool) -> Eq PartialError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PartialError -> PartialError -> Bool
== :: PartialError -> PartialError -> Bool
$c/= :: PartialError -> PartialError -> Bool
/= :: PartialError -> PartialError -> Bool
Eq,Typeable)
instance Exception PartialError
partialError :: [Char] -> [Char] -> a
partialError :: forall a. String -> String -> a
partialError String
lbl String
exp = PartialError -> a
forall a e. Exception e => e -> a
throw (String -> String -> PartialError
PartialError String
lbl String
exp)
partial :: a -> Partial a
partial :: forall a. a -> Partial a
partial = a -> Partial a
forall a. a -> Partial a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
fromPartial :: Partial a -> a
fromPartial :: forall a. Partial a -> a
fromPartial (Partial Identity a
ida) = Identity a -> a
forall a. Identity a -> a
runIdentity Identity a
ida
head :: [a] -> Partial a
head :: forall a. [a] -> Partial a
head [a]
l = a -> Partial a
forall a. a -> Partial a
partial (a -> Partial a) -> a -> Partial a
forall a b. (a -> b) -> a -> b
$
case [a]
l of
[] -> String -> String -> a
forall a. String -> String -> a
partialError String
"head" String
"empty list"
a
x:[a]
_ -> a
x
fromJust :: Maybe a -> Partial a
fromJust :: forall a. Maybe a -> Partial a
fromJust Maybe a
x = a -> Partial a
forall a. a -> Partial a
partial (a -> Partial a) -> a -> Partial a
forall a b. (a -> b) -> a -> b
$
case Maybe a
x of
Maybe a
Nothing -> String -> String -> a
forall a. String -> String -> a
partialError String
"fromJust" String
"Nothing"
Just a
y -> a
y
fromRight :: Either a b -> Partial b
fromRight :: forall a b. Either a b -> Partial b
fromRight Either a b
x = b -> Partial b
forall a. a -> Partial a
partial (b -> Partial b) -> b -> Partial b
forall a b. (a -> b) -> a -> b
$
case Either a b
x of
Left a
_ -> String -> String -> b
forall a. String -> String -> a
partialError String
"fromRight" String
"Left"
Right b
a -> b
a
fromLeft :: Either a b -> Partial a
fromLeft :: forall a b. Either a b -> Partial a
fromLeft Either a b
x = a -> Partial a
forall a. a -> Partial a
partial (a -> Partial a) -> a -> Partial a
forall a b. (a -> b) -> a -> b
$
case Either a b
x of
Right b
_ -> String -> String -> a
forall a. String -> String -> a
partialError String
"fromLeft" String
"Right"
Left a
a -> a
a