module Haxl.Prelude (
module Prelude,
GenHaxl, dataFetch, DataSource, memo,
memoize, memoize1, memoize2,
Applicative(..),
#if __GLASGOW_HASKELL__ < 710
(<$>),
#endif
mapM, mapM_, sequence, sequence_, filterM, foldM,
forM, forM_,
foldl', sort,
Monoid(..),
join,
IfThenElse(..),
(.>), (.<), (.>=), (.<=),
(.==), (./=), (.&&), (.||),
(.++),
pair,
pAnd, pOr,
Text,
IsString(..),
throw, catch, try, withDefault, catchAny,
HaxlException(..), TransientError(..), LogicError(..),
NotFound(..), UnexpectedType(..), FetchError(..),
EmptyList(..), InvalidParameter(..)
) where
import Haxl.Core.Types
import Haxl.Core.Exception
import Haxl.Core.Memo
import Haxl.Core.Monad
import Control.Applicative
import Control.Monad (foldM, join, void)
import Data.List (foldl', sort)
import Data.Text (Text)
import Data.Traversable hiding (forM, mapM, sequence)
import GHC.Exts (IsString(..))
import Prelude hiding (mapM, mapM_, sequence, sequence_)
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid
#endif
import Data.Maybe
import Control.Exception (fromException)
infixr 3 .&&
infixr 2 .||
infix 4 .>, .<, .>=, .<=, .==, ./=
class IfThenElse a b where
ifThenElse :: a -> b -> b -> b
instance IfThenElse Bool a where
ifThenElse b t e = if b then t else e
instance (u1 ~ u2) => IfThenElse (GenHaxl u1 Bool) (GenHaxl u2 a) where
ifThenElse fb t e = do
b <- fb
if b then t else e
instance Num a => Num (GenHaxl u a) where
(+) = liftA2 (+)
() = liftA2 ()
(*) = liftA2 (*)
fromInteger = pure . fromInteger
abs = liftA abs
signum = liftA signum
negate = liftA negate
instance Fractional a => Fractional (GenHaxl u a) where
(/) = liftA2 (/)
recip = liftA recip
fromRational = return . fromRational
(.>) :: Ord a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(.>) = liftA2 (Prelude.>)
(.<) :: Ord a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(.<) = liftA2 (Prelude.<)
(.>=) :: Ord a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(.>=) = liftA2 (Prelude.>=)
(.<=) :: Ord a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(.<=) = liftA2 (Prelude.<=)
(.==) :: Eq a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(.==) = liftA2 (Prelude.==)
(./=) :: Eq a => GenHaxl u a -> GenHaxl u a -> GenHaxl u Bool
(./=) = liftA2 (Prelude./=)
(.++) :: GenHaxl u [a] -> GenHaxl u [a] -> GenHaxl u [a]
(.++) = liftA2 (Prelude.++)
(.&&):: GenHaxl u Bool -> GenHaxl u Bool -> GenHaxl u Bool
fa .&& fb = do a <- fa; if a then fb else return False
(.||):: GenHaxl u Bool -> GenHaxl u Bool -> GenHaxl u Bool
fa .|| fb = do a <- fa; if a then return True else fb
pair :: GenHaxl u a -> GenHaxl u b -> GenHaxl u (a, b)
pair = liftA2 (,)
mapM :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
mapM = traverse
forM :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
forM = flip mapM
mapM_ :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f ()
mapM_ f t = void $ traverse f t
forM_ :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f ()
forM_ = flip mapM_
sequence :: (Traversable t, Applicative f) => t (f a) -> f (t a)
sequence = sequenceA
sequence_ :: (Traversable t, Applicative f) => t (f a) -> f ()
sequence_ t = void $ sequenceA t
filterM :: (Applicative f) => (a -> f Bool) -> [a] -> f [a]
filterM predicate xs =
filt <$> mapM predicate xs
where
filt bools = [ x | (x,True) <- zip xs bools ]
withDefault :: a -> GenHaxl u a -> GenHaxl u a
withDefault d a = catchAny a (return d)
catchAny
:: GenHaxl u a
-> GenHaxl u a
-> GenHaxl u a
catchAny haxl handler =
haxl `catch` \e ->
if isJust (fromException e :: Maybe LogicError) ||
isJust (fromException e :: Maybe TransientError)
then
handler
else
throw e