{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE BlockArguments #-}
module Control.Monad.Dep.SimpleAdvice.Basic
(
returnMempty,
printArgs,
AnyEq (..),
doCachingBadly,
doAsyncBadly
)
where
import Control.Monad.Dep
import Control.Monad.Dep.SimpleAdvice
import Data.Proxy
import Data.SOP
import Data.SOP (hctraverse_)
import Data.SOP.NP
import Data.Type.Equality
import System.IO
import Type.Reflection
import Control.Concurrent
import Control.Monad.IO.Unlift
returnMempty :: forall ca m r. (Monad m, Monoid r) => Advice ca m r
returnMempty :: Advice ca m r
returnMempty =
(AspectT m r -> AspectT m r) -> Advice ca m r
forall (ca :: * -> Constraint) (m :: * -> *) r.
Applicative m =>
(AspectT m r -> AspectT m r) -> Advice ca m r
makeExecutionAdvice
( \AspectT m r
action -> do
r
_ <- AspectT m r
action
r -> AspectT m r
forall (f :: * -> *) a. Applicative f => a -> f a
pure (r
forall a. Monoid a => a
mempty :: r)
)
printArgs :: forall m r. MonadIO m => Handle -> String -> Advice Show m r
printArgs :: Handle -> String -> Advice Show m r
printArgs Handle
h String
prefix =
(forall (as :: [*]). All Show as => NP I as -> AspectT m (NP I as))
-> Advice Show m r
forall (ca :: * -> Constraint) (m :: * -> *) r.
Monad m =>
(forall (as :: [*]). All ca as => NP I as -> AspectT m (NP I as))
-> Advice ca m r
makeArgsAdvice
( \NP I as
args -> do
IO () -> AspectT m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> AspectT m ()) -> IO () -> AspectT m ()
forall a b. (a -> b) -> a -> b
$ Handle -> String -> IO ()
hPutStr Handle
h (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
prefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":"
Proxy Show
-> (forall a. Show a => I a -> AspectT m ())
-> NP I as
-> AspectT m ()
forall k l (h :: (k -> *) -> l -> *) (c :: k -> Constraint)
(xs :: l) (g :: * -> *) (proxy :: (k -> Constraint) -> *)
(f :: k -> *).
(HTraverse_ h, AllN h c xs, Applicative g) =>
proxy c -> (forall (a :: k). c a => f a -> g ()) -> h f xs -> g ()
hctraverse_ (Proxy Show
forall k (t :: k). Proxy t
Proxy @Show) (\(I a) -> IO () -> AspectT m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
hPutStr Handle
h (String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a))) NP I as
args
IO () -> AspectT m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> AspectT m ()) -> IO () -> AspectT m ()
forall a b. (a -> b) -> a -> b
$ Handle -> String -> IO ()
hPutStrLn Handle
h String
"\n"
IO () -> AspectT m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> AspectT m ()) -> IO () -> AspectT m ()
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
h
NP I as -> AspectT m (NP I as)
forall (f :: * -> *) a. Applicative f => a -> f a
pure NP I as
args
)
data AnyEq where
AnyEq :: forall a. (Typeable a, Eq a) => a -> AnyEq
instance Eq AnyEq where
AnyEq a
any1 == :: AnyEq -> AnyEq -> Bool
== AnyEq a
any2 =
case TypeRep a -> TypeRep a -> Maybe (a :~: a)
forall k (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (a -> TypeRep a
forall a. Typeable a => a -> TypeRep a
typeOf a
any1) (a -> TypeRep a
forall a. Typeable a => a -> TypeRep a
typeOf a
any2) of
Maybe (a :~: a)
Nothing -> Bool
False
Just a :~: a
Refl -> a
any1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a
any2
doCachingBadly :: forall m r. Monad m => (AnyEq -> m (Maybe r)) -> (AnyEq -> r -> m ()) -> Advice (Eq `And` Typeable) m r
doCachingBadly :: (AnyEq -> m (Maybe r))
-> (AnyEq -> r -> m ()) -> Advice (And Eq Typeable) m r
doCachingBadly AnyEq -> m (Maybe r)
cacheLookup AnyEq -> r -> m ()
cachePut = (forall (as :: [*]).
All (And Eq Typeable) as =>
NP I as -> AspectT m (AspectT m r -> AspectT m r, NP I as))
-> Advice (And Eq Typeable) m r
forall (ca :: * -> Constraint) (m :: * -> *) r.
(forall (as :: [*]).
All ca as =>
NP I as -> AspectT m (AspectT m r -> AspectT m r, NP I as))
-> Advice ca m r
makeAdvice \NP I as
args ->
let key :: AnyEq
key = [AnyEq] -> AnyEq
forall a. (Typeable a, Eq a) => a -> AnyEq
AnyEq ([AnyEq] -> AnyEq) -> [AnyEq] -> AnyEq
forall a b. (a -> b) -> a -> b
$ Proxy (And Eq Typeable)
-> (forall a. And Eq Typeable a => I a -> [AnyEq])
-> NP I as
-> [AnyEq]
forall k (c :: k -> Constraint) (xs :: [k]) m
(proxy :: (k -> Constraint) -> *) (f :: k -> *).
(All c xs, Monoid m) =>
proxy c -> (forall (a :: k). c a => f a -> m) -> NP f xs -> m
cfoldMap_NP (Proxy (And Eq Typeable)
forall k (t :: k). Proxy t
Proxy @(And Eq Typeable)) (\(I a) -> [a -> AnyEq
forall a. (Typeable a, Eq a) => a -> AnyEq
AnyEq a
a]) (NP I as -> [AnyEq]) -> NP I as -> [AnyEq]
forall a b. (a -> b) -> a -> b
$ NP I as
args
tweakExecution :: AspectT m r -> AspectT m r
tweakExecution AspectT m r
action = do
Maybe r
mr <- m (Maybe r) -> AspectT m (Maybe r)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe r) -> AspectT m (Maybe r))
-> m (Maybe r) -> AspectT m (Maybe r)
forall a b. (a -> b) -> a -> b
$ AnyEq -> m (Maybe r)
cacheLookup AnyEq
key
case Maybe r
mr of
Maybe r
Nothing -> do
r
r <- AspectT m r
action
m () -> AspectT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> AspectT m ()) -> m () -> AspectT m ()
forall a b. (a -> b) -> a -> b
$ AnyEq -> r -> m ()
cachePut AnyEq
key r
r
r -> AspectT m r
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
Just r
r ->
r -> AspectT m r
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
in (AspectT m r -> AspectT m r, NP I as)
-> AspectT m (AspectT m r -> AspectT m r, NP I as)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AspectT m r -> AspectT m r
tweakExecution, NP I as
args)
doAsyncBadly :: forall ca m . MonadUnliftIO m => Advice ca m ()
doAsyncBadly :: Advice ca m ()
doAsyncBadly = (AspectT m () -> AspectT m ()) -> Advice ca m ()
forall (ca :: * -> Constraint) (m :: * -> *) r.
Applicative m =>
(AspectT m r -> AspectT m r) -> Advice ca m r
makeExecutionAdvice \AspectT m ()
action -> do
ThreadId
_ <- ((forall a. AspectT m a -> IO a) -> IO ThreadId)
-> AspectT m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (\forall a. AspectT m a -> IO a
unlift -> IO () -> IO ThreadId
forkIO (AspectT m () -> IO ()
forall a. AspectT m a -> IO a
unlift AspectT m ()
action))
() -> AspectT m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()