{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.Haskell.Brittany.Internal.PreludeUtils
where



import Prelude
import qualified Data.Strict.Maybe as Strict
import Debug.Trace
import Control.Monad
import System.IO

import Control.DeepSeq ( NFData, force )
import Control.Exception.Base ( evaluate )

import Control.Applicative



instance Applicative Strict.Maybe where
  pure :: a -> Maybe a
pure = a -> Maybe a
forall a. a -> Maybe a
Strict.Just
  Strict.Just a -> b
f <*> :: Maybe (a -> b) -> Maybe a -> Maybe b
<*> Strict.Just a
x = b -> Maybe b
forall a. a -> Maybe a
Strict.Just (a -> b
f a
x)
  Maybe (a -> b)
_ <*> Maybe a
_ = Maybe b
forall a. Maybe a
Strict.Nothing

instance Monad Strict.Maybe where
  return :: a -> Maybe a
return = a -> Maybe a
forall a. a -> Maybe a
Strict.Just
  Maybe a
Strict.Nothing >>= :: Maybe a -> (a -> Maybe b) -> Maybe b
>>= a -> Maybe b
_ = Maybe b
forall a. Maybe a
Strict.Nothing
  Strict.Just a
x >>= a -> Maybe b
f = a -> Maybe b
f a
x

instance Alternative Strict.Maybe where
  empty :: Maybe a
empty = Maybe a
forall a. Maybe a
Strict.Nothing
  Maybe a
x <|> :: Maybe a -> Maybe a -> Maybe a
<|> Maybe a
Strict.Nothing = Maybe a
x
  Maybe a
_ <|> Maybe a
x = Maybe a
x

traceFunctionWith :: String -> (a -> String) -> (b -> String) -> (a -> b) -> (a -> b)
traceFunctionWith :: String -> (a -> String) -> (b -> String) -> (a -> b) -> a -> b
traceFunctionWith String
name a -> String
s1 b -> String
s2 a -> b
f a
x = String -> b -> b
forall a. String -> a -> a
trace String
traceStr b
y
 where
  y :: b
y = a -> b
f a
x
  traceStr :: String
traceStr =
    String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nBEFORE:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
s1 a
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nAFTER:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ b -> String
s2 b
y

(<&!>) :: Monad m => m a -> (a -> b) -> m b
<&!> :: m a -> (a -> b) -> m b
(<&!>) = ((a -> b) -> m a -> m b) -> m a -> (a -> b) -> m b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> m a -> m b
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
(<$!>)

putStrErrLn :: String -> IO ()
putStrErrLn :: String -> IO ()
putStrErrLn String
s = Handle -> String -> IO ()
hPutStrLn Handle
stderr String
s

putStrErr :: String -> IO ()
putStrErr :: String -> IO ()
putStrErr String
s = Handle -> String -> IO ()
hPutStr Handle
stderr String
s

printErr :: Show a => a -> IO ()
printErr :: a -> IO ()
printErr = String -> IO ()
putStrErrLn (String -> IO ()) -> (a -> String) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show

errorIf :: Bool -> a -> a
errorIf :: Bool -> a -> a
errorIf Bool
False = a -> a
forall a. a -> a
id
errorIf Bool
True  = String -> a -> a
forall a. HasCallStack => String -> a
error String
"errorIf"

errorIfNote :: Maybe String -> a -> a
errorIfNote :: Maybe String -> a -> a
errorIfNote Maybe String
Nothing  = a -> a
forall a. a -> a
id
errorIfNote (Just String
x) = String -> a -> a
forall a. HasCallStack => String -> a
error String
x

(<&>) :: Functor f => f a -> (a -> b) -> f b
<&> :: f a -> (a -> b) -> f b
(<&>) = ((a -> b) -> f a -> f b) -> f a -> (a -> b) -> f b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
infixl 4 <&>

(.>) :: (a -> b) -> (b -> c) -> (a -> c)
a -> b
f .> :: (a -> b) -> (b -> c) -> a -> c
.> b -> c
g = b -> c
g (b -> c) -> (a -> b) -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
infixl 9 .>

evaluateDeep :: NFData a => a -> IO a
evaluateDeep :: a -> IO a
evaluateDeep = a -> IO a
forall a. a -> IO a
evaluate (a -> IO a) -> (a -> a) -> a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. NFData a => a -> a
force