mirror of
https://github.com/polysemy-research/polysemy.git
synced 2024-09-11 10:36:40 +03:00
parent
e41b73bb55
commit
cd074a4bd4
@ -51,3 +51,8 @@
|
||||
|
||||
## Unreleased changes
|
||||
|
||||
- TODO: Remove deprecated names
|
||||
- NEEDS NEW MAJOR RELEASE: Moved `Random` effect to `polysemy-zoo`
|
||||
- `makeSem` can now be used to create term-level operators (thanks to
|
||||
@TheMatten)
|
||||
|
||||
|
@ -17,13 +17,12 @@ description: Please see the README on GitHub at <https://github.com/isov
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- transformers >= 0.5.5.0 && < 0.6
|
||||
- mtl >= 2.2.2 && <3
|
||||
- template-haskell >= 2.14.0.0 && <2.15
|
||||
- random >= 1.1 && <1.2
|
||||
- th-abstraction >= 0.3 && <= 0.4
|
||||
- containers >= 0.6 && <= 0.7
|
||||
- mtl >= 2.2.2 && <3
|
||||
- syb >= 0.7 && <= 0.8
|
||||
- template-haskell >= 2.14.0.0 && <2.15
|
||||
- th-abstraction >= 0.3 && <= 0.4
|
||||
- transformers >= 0.5.5.0 && < 0.6
|
||||
|
||||
default-extensions:
|
||||
- DataKinds
|
||||
|
@ -4,7 +4,7 @@ cabal-version: 1.12
|
||||
--
|
||||
-- see: https://github.com/sol/hpack
|
||||
--
|
||||
-- hash: c0c48158ef37719851ad6b2bef99ba3236628e92c385792bac924109605febe9
|
||||
-- hash: e0bd1c00115dcbb08343754f06369914411b0aa48c9838c3fa4cd59cf861607b
|
||||
|
||||
name: polysemy
|
||||
version: 0.2.2.0
|
||||
@ -57,7 +57,6 @@ library
|
||||
Polysemy.IO
|
||||
Polysemy.NonDet
|
||||
Polysemy.Output
|
||||
Polysemy.Random
|
||||
Polysemy.Reader
|
||||
Polysemy.Resource
|
||||
Polysemy.State
|
||||
@ -73,7 +72,6 @@ library
|
||||
base >=4.7 && <5
|
||||
, containers >=0.6 && <=0.7
|
||||
, mtl >=2.2.2 && <3
|
||||
, random >=1.1 && <1.2
|
||||
, syb >=0.7 && <=0.8
|
||||
, template-haskell >=2.14.0.0 && <2.15
|
||||
, th-abstraction >=0.3 && <=0.4
|
||||
@ -95,6 +93,7 @@ test-suite polysemy-test
|
||||
HigherOrderSpec
|
||||
InspectorSpec
|
||||
OutputSpec
|
||||
ThEffectSpec
|
||||
Paths_polysemy
|
||||
hs-source-dirs:
|
||||
test
|
||||
@ -107,7 +106,6 @@ test-suite polysemy-test
|
||||
, inspection-testing >=0.4.1.1 && <0.5
|
||||
, mtl >=2.2.2 && <3
|
||||
, polysemy
|
||||
, random >=1.1 && <1.2
|
||||
, syb >=0.7 && <=0.8
|
||||
, template-haskell >=2.14.0.0 && <2.15
|
||||
, th-abstraction >=0.3 && <=0.4
|
||||
@ -131,7 +129,6 @@ benchmark polysemy-bench
|
||||
, freer-simple
|
||||
, mtl
|
||||
, polysemy
|
||||
, random >=1.1 && <1.2
|
||||
, syb >=0.7 && <=0.8
|
||||
, template-haskell >=2.14.0.0 && <2.15
|
||||
, th-abstraction >=0.3 && <=0.4
|
||||
|
@ -1,57 +0,0 @@
|
||||
{-# LANGUAGE BlockArguments #-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
|
||||
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 (Lift IO) r => Sem (Random ': r) a -> Sem r a
|
||||
runRandomIO m = do
|
||||
q <- sendM R.newStdGen
|
||||
snd <$> runRandom q m
|
||||
{-# INLINE runRandomIO #-}
|
||||
|
Loading…
Reference in New Issue
Block a user