{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
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.DataSource
import Haxl.Core.Exception
import Haxl.Core.Memo
import Haxl.Core.Monad
import Haxl.Core.Fetch
import Haxl.Core.Parallel
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 w Bool) (GenHaxl u2 w a) where
ifThenElse fb t e = do
b <- fb
if b then t else e
instance Num a => Num (GenHaxl u w a) where
(+) = liftA2 (+)
(-) = liftA2 (-)
(*) = liftA2 (*)
fromInteger = pure . fromInteger
abs = liftA abs
signum = liftA signum
negate = liftA negate
instance Fractional a => Fractional (GenHaxl u w a) where
(/) = liftA2 (/)
recip = liftA recip
fromRational = return . fromRational
(.>) :: Ord a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(.>) = liftA2 (Prelude.>)
(.<) :: Ord a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(.<) = liftA2 (Prelude.<)
(.>=) :: Ord a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(.>=) = liftA2 (Prelude.>=)
(.<=) :: Ord a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(.<=) = liftA2 (Prelude.<=)
(.==) :: Eq a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(.==) = liftA2 (Prelude.==)
(./=) :: Eq a => GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w Bool
(./=) = liftA2 (Prelude./=)
(.++) :: GenHaxl u w [a] -> GenHaxl u w [a] -> GenHaxl u w [a]
(.++) = liftA2 (Prelude.++)
(.&&):: GenHaxl u w Bool -> GenHaxl u w Bool -> GenHaxl u w Bool
fa .&& fb = do a <- fa; if a then fb else return False
(.||):: GenHaxl u w Bool -> GenHaxl u w Bool -> GenHaxl u w Bool
fa .|| fb = do a <- fa; if a then return True else fb
pair :: GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w (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 w a -> GenHaxl u w a
withDefault d a = catchAny a (return d)
catchAny
:: GenHaxl u w a
-> GenHaxl u w a
-> GenHaxl u w a
catchAny haxl handler =
haxl `catch` \e ->
if isJust (fromException e :: Maybe LogicError) ||
isJust (fromException e :: Maybe TransientError)
then
handler
else
throw e