-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop dependency on
polysemy-zoo
(#380)
Remove polysemy-zoo, and copy over needed code.
- Loading branch information
Showing
9 changed files
with
290 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE ConstraintKinds #-} | ||
|
||
-- This module was copied from polysemy-zoo: | ||
-- https://hackage.haskell.org/package/polysemy-zoo-0.8.2.0/docs/src/Polysemy.ConstraintAbsorber.html | ||
|
||
module Polysemy.ConstraintAbsorber ( | ||
-- * Absorb builder | ||
absorbWithSem, | ||
|
||
-- * Re-exports | ||
Reifies, | ||
(:-) (Sub), | ||
Dict (Dict), | ||
reflect, | ||
Proxy (Proxy), | ||
) where | ||
|
||
import Data.Constraint (Dict (Dict), (:-) (Sub), (\\)) | ||
import qualified Data.Constraint as C | ||
import qualified Data.Constraint.Unsafe as C | ||
import Data.Kind (Constraint, Type) | ||
import Data.Proxy (Proxy (..)) | ||
import Data.Reflection (Reifies, reflect) | ||
import qualified Data.Reflection as R | ||
import Polysemy | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | This function can be used to locally introduce typeclass instances for | ||
-- 'Sem'. See 'Polysemy.ConstraintAbsorber.MonadState' for an example of how to | ||
-- use it. | ||
absorbWithSem :: | ||
forall | ||
-- Constraint to be absorbed | ||
(p :: (Type -> Type) -> Constraint) | ||
-- Wrapper to avoid orphan instances | ||
(x :: (Type -> Type) -> Type -> Type -> Type) | ||
d | ||
r | ||
a. | ||
-- | Reified dictionary | ||
d -> | ||
-- | This parameter should always be @'Sub' 'Dict'@ | ||
(forall s. R.Reifies s d :- p (x (Sem r) s)) -> | ||
(p (Sem r) => Sem r a) -> | ||
Sem r a | ||
absorbWithSem d i m = R.reify d $ \(_ :: Proxy (s :: Type)) -> | ||
m | ||
\\ C.trans | ||
(C.unsafeCoerceConstraint :: (p (x m s) :- p m)) | ||
i | ||
{-# INLINEABLE absorbWithSem #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
-- This module was copied from polysemy-zoo: | ||
-- https://hackage.haskell.org/package/polysemy-zoo-0.8.2.0/docs/src/Polysemy.ConstraintAbsorber.MonadCatch.html | ||
|
||
module Polysemy.ConstraintAbsorber.MonadCatch ( | ||
-- * Constraint Absorbers | ||
absorbMonadThrow, | ||
absorbMonadCatch, | ||
|
||
-- * run helper | ||
runMonadCatch, | ||
runMonadCatchAsText, | ||
|
||
-- * Re-exports | ||
Exception (..), | ||
SomeException, | ||
) | ||
where | ||
|
||
import Control.Monad.Catch ( | ||
Exception (..), | ||
SomeException, | ||
toException, | ||
) | ||
import qualified Control.Monad.Catch as C | ||
|
||
import qualified Data.Text as T | ||
import Polysemy | ||
import Polysemy.ConstraintAbsorber | ||
import qualified Polysemy.Error as E | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | Like 'E.runError' but applies a given function from 'SomeException' | ||
-- to some other type, typically something less opaque. | ||
-- e.g.: | ||
-- @runMonadCatch C.displayException@ | ||
runMonadCatch :: | ||
Exception e => | ||
(Maybe e -> e') -> | ||
Sem (E.Error C.SomeException : E.Error e' : r) a -> | ||
Sem r (Either e' a) | ||
runMonadCatch f = E.runError . E.mapError (f . C.fromException) | ||
|
||
runMonadCatchAsText :: | ||
Sem (E.Error C.SomeException : E.Error T.Text : r) a -> | ||
Sem r (Either T.Text a) | ||
runMonadCatchAsText = E.runError . E.mapError (T.pack . C.displayException) | ||
|
||
-- | Introduce a local 'S.MonadCatch' constraint on 'Sem' --- allowing it to | ||
-- interop nicely with exceptions | ||
absorbMonadCatch :: | ||
Member (E.Error C.SomeException) r => | ||
-- | A computation that requires an instance of 'C.MonadCatch' | ||
-- or 'C.MonadThrow' for | ||
-- 'Sem'. This might be something with type @'C.MonadCatch' e m => m a@. | ||
(C.MonadCatch (Sem r) => Sem r a) -> | ||
Sem r a | ||
absorbMonadCatch = | ||
absorbWithSem @C.MonadCatch @Action (CatchDict E.throw E.catch) (Sub Dict) | ||
{-# INLINEABLE absorbMonadCatch #-} | ||
|
||
-- | Introduce a local 'S.MonadThrow' constraint on 'Sem' --- allowing it to | ||
-- interop nicely with exceptions | ||
absorbMonadThrow :: | ||
Member (E.Error C.SomeException) r => | ||
-- | A computation that requires an instance of 'C.MonadCatch' | ||
-- or 'C.MonadThrow' for | ||
-- 'Sem'. This might be something with type @'C.MonadCatch' e m => m a@. | ||
(C.MonadThrow (Sem r) => Sem r a) -> | ||
Sem r a | ||
absorbMonadThrow = absorbMonadCatch | ||
{-# INLINEABLE absorbMonadThrow #-} | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | A dictionary of the functions we need to supply | ||
-- to make an instance of Error | ||
data CatchDict m = CatchDict | ||
{ throwM_ :: forall a. C.SomeException -> m a | ||
, catch_ :: forall a. m a -> (C.SomeException -> m a) -> m a | ||
} | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | Wrapper for a monadic action with phantom | ||
-- type parameter for reflection. | ||
-- Locally defined so that the instance we are going | ||
-- to build with reflection must be coherent, that is | ||
-- there cannot be orphans. | ||
newtype Action m s' a = Action {action :: m a} | ||
deriving (Functor, Applicative, Monad) | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | Given a reifiable mtl Error dictionary, | ||
-- we can make an instance of @MonadError@ for the action | ||
-- wrapped in @Action@. | ||
instance | ||
( Monad m | ||
, Reifies s' (CatchDict m) | ||
) => | ||
C.MonadThrow (Action m s') | ||
where | ||
throwM e = Action $ throwM_ (reflect $ Proxy @s') (C.toException e) | ||
{-# INLINEABLE throwM #-} | ||
|
||
instance | ||
( Monad m | ||
, Reifies s' (CatchDict m) | ||
) => | ||
C.MonadCatch (Action m s') | ||
where | ||
catch x f = | ||
let catchF = catch_ (reflect $ Proxy @s') | ||
in Action $ | ||
action x `catchF` \e -> case C.fromException e of | ||
Just e' -> action $ f e' | ||
_ -> throwM_ (reflect $ Proxy @s') (C.toException e) | ||
{-# INLINEABLE catch #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
-- This module was copied from polysemy-zoo: | ||
-- https://hackage.haskell.org/package/polysemy-zoo-0.8.2.0/docs/src/Polysemy.Random.html | ||
|
||
module Polysemy.Random ( | ||
-- * Effect | ||
Random (..), | ||
|
||
-- * Actions | ||
random, | ||
randomR, | ||
|
||
-- * Interpretations | ||
runRandom, | ||
runRandomIO, | ||
) where | ||
|
||
import Polysemy | ||
import Polysemy.State | ||
import qualified System.Random as R | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | An effect capable of providing 'R.Random' values. | ||
data Random m a where | ||
Random :: R.Random x => Random m x | ||
RandomR :: R.Random x => (x, x) -> Random m x | ||
|
||
makeSem ''Random | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | Run a 'Random' effect with an explicit 'R.RandomGen'. | ||
runRandom :: | ||
forall q r a. | ||
R.RandomGen q => | ||
q -> | ||
Sem (Random ': r) a -> | ||
Sem r (q, a) | ||
runRandom q = | ||
runState q | ||
. reinterpret | ||
( \case | ||
Random -> do | ||
~(a, q') <- gets @q R.random | ||
put q' | ||
pure a | ||
RandomR r -> do | ||
~(a, q') <- gets @q $ R.randomR r | ||
put q' | ||
pure a | ||
) | ||
{-# INLINE runRandom #-} | ||
|
||
------------------------------------------------------------------------------ | ||
|
||
-- | Run a 'Random' effect by using the 'IO' random generator. | ||
runRandomIO :: Member (Embed IO) r => Sem (Random ': r) a -> Sem r a | ||
runRandomIO m = do | ||
q <- embed @IO R.newStdGen | ||
snd <$> runRandom q m | ||
{-# INLINE runRandomIO #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters