Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module contains examples of simple advices.
BEWARE! These are provided for illustrative purposes only, they strive for simplicity and not robustness or efficiency.
Synopsis
- returnMempty :: forall ca cem. Advice ca cem Monoid
- printArgs :: forall cr. Handle -> String -> Advice Show (MonadConstraint MonadIO) cr
- data AnyEq where
- doCachingBadly :: forall m r. (AnyEq -> m (Maybe r)) -> (AnyEq -> r -> m ()) -> Advice (Eq `And` Typeable) (MonadConstraint (MustBe m)) (MustBe r)
- doAsyncBadly :: Advice ca (MonadConstraint (MustBe IO)) (MustBe ())
Basic advices
returnMempty :: forall ca cem. Advice ca cem Monoid Source #
printArgs :: forall cr. Handle -> String -> Advice Show (MonadConstraint MonadIO) cr Source #
Given a Handle
and a prefix string, makes functions print their
arguments to the Handle
.
This advice uses MonadConstraint
to lift the MonadIO
constraint that
applies only to the monad.
Because it doesn't touch the return value of the advised function, this
Advice
is polymorphic on cr
.
A helper datatype for universal equality comparisons of existentialized values, used by doCachingBadly
.
For a more complete elaboration of this idea, see the the "exinst" package.
doCachingBadly :: forall m r. (AnyEq -> m (Maybe r)) -> (AnyEq -> r -> m ()) -> Advice (Eq `And` Typeable) (MonadConstraint (MustBe m)) (MustBe r) Source #
Given the means for looking up and storing values in the underlying monad
m
, makes functions (inefficiently) cache their results.
Notice the equality constraints on the Advice
. This means that the monad
m
and the result type r
are known and fixed before building the advice.
Once built, the Advice
won't be polymorphic over them.
The implementation of this function makes use of the existential type
parameter u
of makeAdvice
, because the phase that processes the function
arguments needs to communicate the calculated AnyEq
cache key to the phase
that processes the function result.
A better implementation of this advice would likely use an AnyHashable
helper datatype for the keys.
doAsyncBadly :: Advice ca (MonadConstraint (MustBe IO)) (MustBe ()) Source #
Makes functions that return ()
launch asynchronously.
A better implementation of this advice would likely use the "async"
package instead of bare forkIO
.
And the MustBe IO
constraint could be relaxed to MonadUnliftIO
.