{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Bluefin.Internal.Examples where
import Bluefin.Internal hiding (w)
import Control.Monad (forever, when)
import Control.Monad.IO.Class (liftIO)
import Data.Foldable (for_)
import Data.Monoid (Any (Any, getAny))
import Prelude hiding (break, drop, head, read, return)
monadIOExample :: IO ()
monadIOExample :: IO ()
monadIOExample = (forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ()
forall a.
(forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) a)
-> IO a
runEff ((forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ())
-> (forall (e :: Effects) (es :: Effects).
IOE e -> Eff (e :& es) ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \IOE e
io -> IOE e
-> (forall {m :: * -> *}. MonadIO m => m ()) -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects) r.
(e :> es) =>
IOE e -> (forall (m :: * -> *). MonadIO m => m r) -> Eff es r
withMonadIO IOE e
io ((forall {m :: * -> *}. MonadIO m => m ()) -> Eff (e :& es) ())
-> (forall {m :: * -> *}. MonadIO m => m ()) -> Eff (e :& es) ()
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
[Char]
name <- IO [Char]
forall a. Read a => IO a
readLn
[Char] -> IO ()
putStrLn ([Char]
"Hello " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
name)
monadFailExample :: Either String ()
monadFailExample :: Either [Char] ()
monadFailExample = (forall (es :: Effects). Eff es (Either [Char] ()))
-> Either [Char] ()
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es (Either [Char] ()))
-> Either [Char] ())
-> (forall (es :: Effects). Eff es (Either [Char] ()))
-> Either [Char] ()
forall a b. (a -> b) -> a -> b
$ (forall (ex :: Effects). Exception [Char] ex -> Eff (ex :& es) ())
-> Eff es (Either [Char] ())
forall e (es :: Effects) a.
(forall (ex :: Effects). Exception e ex -> Eff (ex :& es) a)
-> Eff es (Either e a)
try ((forall (ex :: Effects). Exception [Char] ex -> Eff (ex :& es) ())
-> Eff es (Either [Char] ()))
-> (forall (ex :: Effects).
Exception [Char] ex -> Eff (ex :& es) ())
-> Eff es (Either [Char] ())
forall a b. (a -> b) -> a -> b
$ \Exception [Char] ex
e ->
Bool -> Eff (ex :& es) () -> Eff (ex :& es) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Int
2 :: Int) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1) (Eff (ex :& es) () -> Eff (ex :& es) ())
-> Eff (ex :& es) () -> Eff (ex :& es) ()
forall a b. (a -> b) -> a -> b
$
Exception [Char] ex
-> (forall (m :: * -> *). MonadFail m => m ()) -> Eff (ex :& es) ()
forall (e :: Effects) (es :: Effects) r.
(e :> es) =>
Exception [Char] e
-> (forall (m :: * -> *). MonadFail m => m r) -> Eff es r
withMonadFail Exception [Char] ex
e ([Char] -> m ()
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"2 was bigger than 1")
throwExample :: Either Int String
throwExample :: Either Int [Char]
throwExample = (forall (es :: Effects). Eff es (Either Int [Char]))
-> Either Int [Char]
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es (Either Int [Char]))
-> Either Int [Char])
-> (forall (es :: Effects). Eff es (Either Int [Char]))
-> Either Int [Char]
forall a b. (a -> b) -> a -> b
$ (forall (ex :: Effects). Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es (Either Int [Char])
forall e (es :: Effects) a.
(forall (ex :: Effects). Exception e ex -> Eff (ex :& es) a)
-> Eff es (Either e a)
try ((forall (ex :: Effects).
Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es (Either Int [Char]))
-> (forall (ex :: Effects).
Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es (Either Int [Char])
forall a b. (a -> b) -> a -> b
$ \Exception Int ex
e -> do
Any
_ <- Exception Int ex -> Int -> Eff (ex :& es) Any
forall (ex :: Effects) (es :: Effects) e a.
(ex :> es) =>
Exception e ex -> e -> Eff es a
throw Exception Int ex
e Int
42
[Char] -> Eff (ex :& es) [Char]
forall a. a -> Eff (ex :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
"No exception thrown"
handleExample :: String
handleExample :: [Char]
handleExample = (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es [Char]) -> [Char])
-> (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a b. (a -> b) -> a -> b
$ (Int -> Eff es [Char])
-> (forall {ex :: Effects}.
Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es [Char]
forall e (es :: Effects) a.
(e -> Eff es a)
-> (forall (ex :: Effects). Exception e ex -> Eff (ex :& es) a)
-> Eff es a
handle ([Char] -> Eff es [Char]
forall a. a -> Eff es a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> Eff es [Char])
-> (Int -> [Char]) -> Int -> Eff es [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Char]
forall a. Show a => a -> [Char]
show) ((forall {ex :: Effects}.
Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es [Char])
-> (forall {ex :: Effects}.
Exception Int ex -> Eff (ex :& es) [Char])
-> Eff es [Char]
forall a b. (a -> b) -> a -> b
$ \Exception Int ex
e -> do
Any
_ <- Exception Int ex -> Int -> Eff (ex :& es) Any
forall (ex :: Effects) (es :: Effects) e a.
(ex :> es) =>
Exception e ex -> e -> Eff es a
throw Exception Int ex
e (Int
42 :: Int)
[Char] -> Eff (ex :& es) [Char]
forall a. a -> Eff (ex :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
"No exception thrown"
exampleGet :: (Int, Int)
exampleGet :: (Int, Int)
exampleGet = (forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int)
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int))
-> (forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int)
forall a b. (a -> b) -> a -> b
$ Int
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es (Int, Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es (a, s)
runState Int
10 ((forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es (Int, Int))
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es (Int, Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
st -> do
Int
n <- State Int st -> Eff (st :& es) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
st
Int -> Eff (st :& es) Int
forall a. a -> Eff (st :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n)
examplePut :: ((), Int)
examplePut :: ((), Int)
examplePut = (forall (es :: Effects). Eff es ((), Int)) -> ((), Int)
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ((), Int)) -> ((), Int))
-> (forall (es :: Effects). Eff es ((), Int)) -> ((), Int)
forall a b. (a -> b) -> a -> b
$ Int
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es (a, s)
runState Int
10 ((forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int))
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
st -> do
State Int st -> Int -> Eff (st :& es) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> s -> Eff es ()
put State Int st
st Int
30
exampleModify :: ((), Int)
exampleModify :: ((), Int)
exampleModify = (forall (es :: Effects). Eff es ((), Int)) -> ((), Int)
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ((), Int)) -> ((), Int))
-> (forall (es :: Effects). Eff es ((), Int)) -> ((), Int)
forall a b. (a -> b) -> a -> b
$ Int
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es (a, s)
runState Int
10 ((forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int))
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) ())
-> Eff es ((), Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
st -> do
State Int st -> (Int -> Int) -> Eff (st :& es) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> (s -> s) -> Eff es ()
modify State Int st
st (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
yieldExample :: ([Int], ())
yieldExample :: ([Int], ())
yieldExample = (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ()))
-> (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a b. (a -> b) -> a -> b
$ (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a (es :: Effects) r.
(forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Eff es ([a], r)
yieldToList ((forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ()))
-> (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a b. (a -> b) -> a -> b
$ \Stream Int e1
y -> do
Stream Int e1 -> Int -> Eff (e1 :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Stream Int e1
y Int
1
Stream Int e1 -> Int -> Eff (e1 :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Stream Int e1
y Int
2
Stream Int e1 -> Int -> Eff (e1 :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Stream Int e1
y Int
100
forEachExample :: ([Int], ())
forEachExample :: ([Int], ())
forEachExample = (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ()))
-> (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a b. (a -> b) -> a -> b
$ (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a (es :: Effects) r.
(forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Eff es ([a], r)
yieldToList ((forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ()))
-> (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a b. (a -> b) -> a -> b
$ \Stream Int e1
y -> do
(forall (e1 :: Effects).
Coroutine Int () e1 -> Eff (e1 :& (e1 :& es)) ())
-> (Int -> Eff (e1 :& es) ()) -> Eff (e1 :& es) ()
forall a b (es :: Effects) r.
(forall (e1 :: Effects). Coroutine a b e1 -> Eff (e1 :& es) r)
-> (a -> Eff es b) -> Eff es r
forEach ([Int] -> Coroutine Int () e1 -> Eff (e1 :& (e1 :& es)) ()
forall (t :: * -> *) (e1 :: Effects) (es :: Effects) a.
(Foldable t, e1 :> es) =>
t a -> Stream a e1 -> Eff es ()
inFoldable [Int
0 .. Int
4]) ((Int -> Eff (e1 :& es) ()) -> Eff (e1 :& es) ())
-> (Int -> Eff (e1 :& es) ()) -> Eff (e1 :& es) ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Stream Int e1 -> Int -> Eff (e1 :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Stream Int e1
y Int
i
Stream Int e1 -> Int -> Eff (e1 :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Stream Int e1
y (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10)
inFoldableExample :: ([Int], ())
inFoldableExample :: ([Int], ())
inFoldableExample = (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ()))
-> (forall (es :: Effects). Eff es ([Int], ())) -> ([Int], ())
forall a b. (a -> b) -> a -> b
$ (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a (es :: Effects) r.
(forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Eff es ([a], r)
yieldToList ((forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ()))
-> (forall (e1 :: Effects). Stream Int e1 -> Eff (e1 :& es) ())
-> Eff es ([Int], ())
forall a b. (a -> b) -> a -> b
$ [Int] -> Stream Int e1 -> Eff (e1 :& es) ()
forall (t :: * -> *) (e1 :: Effects) (es :: Effects) a.
(Foldable t, e1 :> es) =>
t a -> Stream a e1 -> Eff es ()
inFoldable [Int
1, Int
2, Int
100]
enumerateExample :: ([(Int, String)], ())
enumerateExample :: ([(Int, [Char])], ())
enumerateExample = (forall (es :: Effects). Eff es ([(Int, [Char])], ()))
-> ([(Int, [Char])], ())
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es ([(Int, [Char])], ()))
-> ([(Int, [Char])], ()))
-> (forall (es :: Effects). Eff es ([(Int, [Char])], ()))
-> ([(Int, [Char])], ())
forall a b. (a -> b) -> a -> b
$ (forall (e1 :: Effects).
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ())
-> Eff es ([(Int, [Char])], ())
forall a (es :: Effects) r.
(forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Eff es ([a], r)
yieldToList ((forall (e1 :: Effects).
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ())
-> Eff es ([(Int, [Char])], ()))
-> (forall (e1 :: Effects).
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ())
-> Eff es ([(Int, [Char])], ())
forall a b. (a -> b) -> a -> b
$ (forall (e1 :: Effects).
Stream [Char] e1 -> Eff (e1 :& (e1 :& es)) ())
-> Stream (Int, [Char]) e1 -> Eff (e1 :& es) ()
forall (e2 :: Effects) (es :: Effects) a r.
(e2 :> es) =>
(forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Stream (Int, a) e2 -> Eff es r
enumerate ([[Char]] -> Stream [Char] e1 -> Eff (e1 :& (e1 :& es)) ()
forall (t :: * -> *) (e1 :: Effects) (es :: Effects) a.
(Foldable t, e1 :> es) =>
t a -> Stream a e1 -> Eff es ()
inFoldable [[Char]
"A", [Char]
"B", [Char]
"C"])
returnEarlyExample :: String
returnEarlyExample :: [Char]
returnEarlyExample = (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es [Char]) -> [Char])
-> (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a b. (a -> b) -> a -> b
$ (forall (er :: Effects).
EarlyReturn [Char] er -> Eff (er :& es) [Char])
-> Eff es [Char]
forall r (es :: Effects).
(forall (er :: Effects). EarlyReturn r er -> Eff (er :& es) r)
-> Eff es r
withEarlyReturn ((forall (er :: Effects).
EarlyReturn [Char] er -> Eff (er :& es) [Char])
-> Eff es [Char])
-> (forall (er :: Effects).
EarlyReturn [Char] er -> Eff (er :& es) [Char])
-> Eff es [Char]
forall a b. (a -> b) -> a -> b
$ \EarlyReturn [Char] er
e -> do
[Int] -> (Int -> Eff (er :& es) ()) -> Eff (er :& es) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 :: Int .. Int
10] ((Int -> Eff (er :& es) ()) -> Eff (er :& es) ())
-> (Int -> Eff (er :& es) ()) -> Eff (er :& es) ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Bool -> Eff (er :& es) () -> Eff (er :& es) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
5) (Eff (er :& es) () -> Eff (er :& es) ())
-> Eff (er :& es) () -> Eff (er :& es) ()
forall a b. (a -> b) -> a -> b
$
EarlyReturn [Char] er -> [Char] -> Eff (er :& es) ()
forall (ex :: Effects) (es :: Effects) e a.
(ex :> es) =>
Exception e ex -> e -> Eff es a
returnEarly EarlyReturn [Char] er
e ([Char]
"Returned early with " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i)
[Char] -> Eff (er :& es) [Char]
forall a. a -> Eff (er :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
"End of loop"
effIOExample :: IO ()
effIOExample :: IO ()
effIOExample = (forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ()
forall a.
(forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) a)
-> IO a
runEff ((forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ())
-> (forall (e :: Effects) (es :: Effects).
IOE e -> Eff (e :& es) ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \IOE e
io -> do
IOE e -> IO () -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects) a.
(e :> es) =>
IOE e -> IO a -> Eff es a
effIO IOE e
io ([Char] -> IO ()
putStrLn [Char]
"Hello world!")
example1_ :: (Int, Int)
example1_ :: (Int, Int)
example1_ =
let example1 :: Int -> Int
example1 :: Int -> Int
example1 Int
n = (forall (es :: Effects). Eff es Int) -> Int
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es Int) -> Int)
-> (forall (es :: Effects). Eff es Int) -> Int
forall a b. (a -> b) -> a -> b
$ Int
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es Int
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState Int
n ((forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es Int)
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) Int)
-> Eff es Int
forall a b. (a -> b) -> a -> b
$ \State Int st
st -> do
Int
n' <- State Int st -> Eff (st :& es) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
st
Bool -> Eff (st :& es) () -> Eff (st :& es) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
10) (Eff (st :& es) () -> Eff (st :& es) ())
-> Eff (st :& es) () -> Eff (st :& es) ()
forall a b. (a -> b) -> a -> b
$
State Int st -> Int -> Eff (st :& es) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> s -> Eff es ()
put State Int st
st (Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10)
State Int st -> Eff (st :& es) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
st
in (Int -> Int
example1 Int
5, Int -> Int
example1 Int
12)
example2_ :: ((Int, Int), (Int, Int))
example2_ :: ((Int, Int), (Int, Int))
example2_ =
let example2 :: (Int, Int) -> (Int, Int)
example2 :: (Int, Int) -> (Int, Int)
example2 (Int
m, Int
n) = (forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int)
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int))
-> (forall (es :: Effects). Eff es (Int, Int)) -> (Int, Int)
forall a b. (a -> b) -> a -> b
$
Int
-> (forall {st :: Effects}.
State Int st -> Eff (st :& es) (Int, Int))
-> Eff es (Int, Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState Int
m ((forall {st :: Effects}.
State Int st -> Eff (st :& es) (Int, Int))
-> Eff es (Int, Int))
-> (forall {st :: Effects}.
State Int st -> Eff (st :& es) (Int, Int))
-> Eff es (Int, Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
sm -> do
Int
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (st :& es)) (Int, Int))
-> Eff (st :& es) (Int, Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState Int
n ((forall {st :: Effects}.
State Int st -> Eff (st :& (st :& es)) (Int, Int))
-> Eff (st :& es) (Int, Int))
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (st :& es)) (Int, Int))
-> Eff (st :& es) (Int, Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
sn -> do
do
Int
n' <- State Int st -> Eff (st :& (st :& es)) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
sn
Int
m' <- State Int st -> Eff (st :& (st :& es)) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
sm
if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
m'
then State Int st -> Int -> Eff (st :& (st :& es)) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> s -> Eff es ()
put State Int st
sn (Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10)
else State Int st -> Int -> Eff (st :& (st :& es)) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> s -> Eff es ()
put State Int st
sm (Int
m' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10)
Int
n' <- State Int st -> Eff (st :& (st :& es)) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
sn
Int
m' <- State Int st -> Eff (st :& (st :& es)) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
sm
(Int, Int) -> Eff (st :& (st :& es)) (Int, Int)
forall a. a -> Eff (st :& (st :& es)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
n', Int
m')
in ((Int, Int) -> (Int, Int)
example2 (Int
5, Int
10), (Int, Int) -> (Int, Int)
example2 (Int
12, Int
5))
example3_ :: IO ()
example3_ :: IO ()
example3_ = (forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ()
forall a.
(forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) a)
-> IO a
runEff ((forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ())
-> (forall (e :: Effects) (es :: Effects).
IOE e -> Eff (e :& es) ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \IOE e
io -> do
let getLineUntilStop :: Coroutine [Char] () e1 -> Eff es ()
getLineUntilStop Coroutine [Char] () e1
y = (forall (j :: Effects). Jump j -> Eff (j :& es) ()) -> Eff es ()
forall (es :: Effects).
(forall (j :: Effects). Jump j -> Eff (j :& es) ()) -> Eff es ()
withJump ((forall (j :: Effects). Jump j -> Eff (j :& es) ()) -> Eff es ())
-> (forall (j :: Effects). Jump j -> Eff (j :& es) ()) -> Eff es ()
forall a b. (a -> b) -> a -> b
$ \Jump j
stop -> Eff (j :& es) () -> Eff (j :& es) ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (Eff (j :& es) () -> Eff (j :& es) ())
-> Eff (j :& es) () -> Eff (j :& es) ()
forall a b. (a -> b) -> a -> b
$ do
[Char]
line <- IOE e -> IO [Char] -> Eff (j :& es) [Char]
forall (e :: Effects) (es :: Effects) a.
(e :> es) =>
IOE e -> IO a -> Eff es a
effIO IOE e
io IO [Char]
getLine
Bool -> Eff (j :& es) () -> Eff (j :& es) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Char]
line [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"STOP") (Eff (j :& es) () -> Eff (j :& es) ())
-> Eff (j :& es) () -> Eff (j :& es) ()
forall a b. (a -> b) -> a -> b
$
Jump j -> Eff (j :& es) ()
forall (j :: Effects) (es :: Effects) a.
(j :> es) =>
Jump j -> Eff es a
jumpTo Jump j
stop
Coroutine [Char] () e1 -> [Char] -> Eff (j :& es) ()
forall (e1 :: Effects) (es :: Effects) a.
(e1 :> es) =>
Stream a e1 -> a -> Eff es ()
yield Coroutine [Char] () e1
y [Char]
line
nonEmptyLines :: Stream [Char] e2 -> Eff es ()
nonEmptyLines =
([Char] -> Maybe [Char])
-> (forall (e1 :: Effects). Stream [Char] e1 -> Eff (e1 :& es) ())
-> Stream [Char] e2
-> Eff es ()
forall (e2 :: Effects) (es :: Effects) a b r.
(e2 :> es) =>
(a -> Maybe b)
-> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Stream b e2
-> Eff es r
mapMaybe
( \case
[Char]
"" -> Maybe [Char]
forall a. Maybe a
Nothing
[Char]
line -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
line
)
Coroutine [Char] () e1 -> Eff (e1 :& es) ()
forall (e1 :: Effects). Stream [Char] e1 -> Eff (e1 :& es) ()
forall {es :: Effects} {e1 :: Effects}.
(e :> es, e1 :> es) =>
Coroutine [Char] () e1 -> Eff es ()
getLineUntilStop
enumeratedLines :: Stream (Int, [Char]) e2 -> Eff es ()
enumeratedLines = Int
-> (forall (e1 :: Effects). Stream [Char] e1 -> Eff (e1 :& es) ())
-> Stream (Int, [Char]) e2
-> Eff es ()
forall (e2 :: Effects) (es :: Effects) a r.
(e2 :> es) =>
Int
-> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Stream (Int, a) e2
-> Eff es r
enumerateFrom Int
1 Stream [Char] e1 -> Eff (e1 :& es) ()
forall (e1 :: Effects). Stream [Char] e1 -> Eff (e1 :& es) ()
forall {e2 :: Effects} {es :: Effects}.
(e2 :> es, e :> es) =>
Stream [Char] e2 -> Eff es ()
nonEmptyLines
formattedLines :: Stream [Char] e2 -> Eff es ()
formattedLines =
((Int, [Char]) -> [Char])
-> (forall (e1 :: Effects).
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ())
-> Stream [Char] e2
-> Eff es ()
forall (e2 :: Effects) (es :: Effects) a b r.
(e2 :> es) =>
(a -> b)
-> (forall (e1 :: Effects). Stream a e1 -> Eff (e1 :& es) r)
-> Stream b e2
-> Eff es r
mapStream
(\(Int
i, [Char]
line) -> Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
". Hello! You said " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
line)
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ()
forall (e1 :: Effects).
Stream (Int, [Char]) e1 -> Eff (e1 :& es) ()
forall {e2 :: Effects} {es :: Effects}.
(e2 :> es, e :> es) =>
Stream (Int, [Char]) e2 -> Eff es ()
enumeratedLines
(forall (e1 :: Effects).
Coroutine [Char] () e1 -> Eff (e1 :& (e :& es)) ())
-> ([Char] -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall a b (es :: Effects) r.
(forall (e1 :: Effects). Coroutine a b e1 -> Eff (e1 :& es) r)
-> (a -> Eff es b) -> Eff es r
forEach Stream [Char] e1 -> Eff (e1 :& (e :& es)) ()
forall (e1 :: Effects).
Coroutine [Char] () e1 -> Eff (e1 :& (e :& es)) ()
forall {e2 :: Effects} {es :: Effects}.
(e2 :> es, e :> es) =>
Stream [Char] e2 -> Eff es ()
formattedLines (([Char] -> Eff (e :& es) ()) -> Eff (e :& es) ())
-> ([Char] -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall a b. (a -> b) -> a -> b
$ \[Char]
line -> IOE e -> IO () -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects) a.
(e :> es) =>
IOE e -> IO a -> Eff es a
effIO IOE e
io ([Char] -> IO ()
putStrLn [Char]
line)
countPositivesNegatives :: [Int] -> String
countPositivesNegatives :: [Int] -> [Char]
countPositivesNegatives [Int]
is = (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es [Char]) -> [Char])
-> (forall (es :: Effects). Eff es [Char]) -> [Char]
forall a b. (a -> b) -> a -> b
$
Int
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) [Char])
-> Eff es [Char]
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState (Int
0 :: Int) ((forall {st :: Effects}. State Int st -> Eff (st :& es) [Char])
-> Eff es [Char])
-> (forall {st :: Effects}. State Int st -> Eff (st :& es) [Char])
-> Eff es [Char]
forall a b. (a -> b) -> a -> b
$ \State Int st
positives -> do
Either () [Char]
r <- (forall (ex :: Effects).
Exception () ex -> Eff (ex :& (st :& es)) [Char])
-> Eff (st :& es) (Either () [Char])
forall e (es :: Effects) a.
(forall (ex :: Effects). Exception e ex -> Eff (ex :& es) a)
-> Eff es (Either e a)
try ((forall (ex :: Effects).
Exception () ex -> Eff (ex :& (st :& es)) [Char])
-> Eff (st :& es) (Either () [Char]))
-> (forall (ex :: Effects).
Exception () ex -> Eff (ex :& (st :& es)) [Char])
-> Eff (st :& es) (Either () [Char])
forall a b. (a -> b) -> a -> b
$ \Exception () ex
ex ->
Int
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (ex :& (st :& es))) [Char])
-> Eff (ex :& (st :& es)) [Char]
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState (Int
0 :: Int) ((forall {st :: Effects}.
State Int st -> Eff (st :& (ex :& (st :& es))) [Char])
-> Eff (ex :& (st :& es)) [Char])
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (ex :& (st :& es))) [Char])
-> Eff (ex :& (st :& es)) [Char]
forall a b. (a -> b) -> a -> b
$ \State Int st
negatives -> do
[Int]
-> (Int -> Eff (st :& (ex :& (st :& es))) ())
-> Eff (st :& (ex :& (st :& es))) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int]
is ((Int -> Eff (st :& (ex :& (st :& es))) ())
-> Eff (st :& (ex :& (st :& es))) ())
-> (Int -> Eff (st :& (ex :& (st :& es))) ())
-> Eff (st :& (ex :& (st :& es))) ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
0 of
Ordering
GT -> State Int st -> (Int -> Int) -> Eff (st :& (ex :& (st :& es))) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> (s -> s) -> Eff es ()
modify State Int st
positives (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Ordering
EQ -> Exception () ex -> () -> Eff (st :& (ex :& (st :& es))) ()
forall (ex :: Effects) (es :: Effects) e a.
(ex :> es) =>
Exception e ex -> e -> Eff es a
throw Exception () ex
ex ()
Ordering
LT -> State Int st -> (Int -> Int) -> Eff (st :& (ex :& (st :& es))) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> (s -> s) -> Eff es ()
modify State Int st
negatives (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Int
p <- State Int st -> Eff (st :& (ex :& (st :& es))) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
positives
Int
n <- State Int st -> Eff (st :& (ex :& (st :& es))) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
negatives
[Char] -> Eff (st :& (ex :& (st :& es))) [Char]
forall a. a -> Eff (st :& (ex :& (st :& es))) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> Eff (st :& (ex :& (st :& es))) [Char])
-> [Char] -> Eff (st :& (ex :& (st :& es))) [Char]
forall a b. (a -> b) -> a -> b
$
[Char]
"Positives: "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
p
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
", negatives "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
case Either () [Char]
r of
Right [Char]
r' -> [Char] -> Eff (st :& es) [Char]
forall a. a -> Eff (st :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
r'
Left () -> do
Int
p <- State Int st -> Eff (st :& es) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
positives
[Char] -> Eff (st :& es) [Char]
forall a. a -> Eff (st :& es) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Char] -> Eff (st :& es) [Char])
-> [Char] -> Eff (st :& es) [Char]
forall a b. (a -> b) -> a -> b
$
[Char]
"We saw a zero, but before that there were "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
p
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" positives"
type MyHandle = Compound (State Int) (Exception String)
myInc :: (e :> es) => MyHandle e -> Eff es ()
myInc :: forall (e :: Effects) (es :: Effects).
(e :> es) =>
MyHandle e -> Eff es ()
myInc MyHandle e
h = MyHandle e
-> (forall (e1 :: Effects) (e2 :: Effects).
(e1 :> es, e2 :> es) =>
State Int e1 -> Exception [Char] e2 -> Eff es ())
-> Eff es ()
forall (h1 :: Effects -> *) (h2 :: Effects -> *) (e :: Effects)
(es :: Effects) r.
(e :> es) =>
Compound h1 h2 e
-> (forall (e1 :: Effects) (e2 :: Effects).
(e1 :> es, e2 :> es) =>
h1 e1 -> h2 e2 -> Eff es r)
-> Eff es r
withCompound MyHandle e
h (\State Int e1
s Exception [Char] e2
_ -> State Int e1 -> (Int -> Int) -> Eff es ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> (s -> s) -> Eff es ()
modify State Int e1
s (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
myBail :: (e :> es) => MyHandle e -> Eff es r
myBail :: forall (e :: Effects) (es :: Effects) r.
(e :> es) =>
MyHandle e -> Eff es r
myBail MyHandle e
h = MyHandle e
-> (forall {e1 :: Effects} {e2 :: Effects}.
(e1 :> es, e2 :> es) =>
State Int e1 -> Exception [Char] e2 -> Eff es r)
-> Eff es r
forall (h1 :: Effects -> *) (h2 :: Effects -> *) (e :: Effects)
(es :: Effects) r.
(e :> es) =>
Compound h1 h2 e
-> (forall (e1 :: Effects) (e2 :: Effects).
(e1 :> es, e2 :> es) =>
h1 e1 -> h2 e2 -> Eff es r)
-> Eff es r
withCompound MyHandle e
h ((forall {e1 :: Effects} {e2 :: Effects}.
(e1 :> es, e2 :> es) =>
State Int e1 -> Exception [Char] e2 -> Eff es r)
-> Eff es r)
-> (forall {e1 :: Effects} {e2 :: Effects}.
(e1 :> es, e2 :> es) =>
State Int e1 -> Exception [Char] e2 -> Eff es r)
-> Eff es r
forall a b. (a -> b) -> a -> b
$ \State Int e1
s Exception [Char] e2
e -> do
Int
i <- State Int e1 -> Eff es Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int e1
s
Exception [Char] e2 -> [Char] -> Eff es r
forall (ex :: Effects) (es :: Effects) e a.
(ex :> es) =>
Exception e ex -> e -> Eff es a
throw Exception [Char] e2
e ([Char]
"Current state was: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i)
runMyHandle ::
(forall e. MyHandle e -> Eff (e :& es) a) ->
Eff es (Either String (a, Int))
runMyHandle :: forall (es :: Effects) a.
(forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff es (Either [Char] (a, Int))
runMyHandle forall (e :: Effects). MyHandle e -> Eff (e :& es) a
f =
(forall (ex :: Effects).
Exception [Char] ex -> Eff (ex :& es) (a, Int))
-> Eff es (Either [Char] (a, Int))
forall e (es :: Effects) a.
(forall (ex :: Effects). Exception e ex -> Eff (ex :& es) a)
-> Eff es (Either e a)
try ((forall (ex :: Effects).
Exception [Char] ex -> Eff (ex :& es) (a, Int))
-> Eff es (Either [Char] (a, Int)))
-> (forall (ex :: Effects).
Exception [Char] ex -> Eff (ex :& es) (a, Int))
-> Eff es (Either [Char] (a, Int))
forall a b. (a -> b) -> a -> b
$ \Exception [Char] ex
e -> do
Int
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (ex :& es)) a)
-> Eff (ex :& es) (a, Int)
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es (a, s)
runState Int
0 ((forall {st :: Effects}. State Int st -> Eff (st :& (ex :& es)) a)
-> Eff (ex :& es) (a, Int))
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (ex :& es)) a)
-> Eff (ex :& es) (a, Int)
forall a b. (a -> b) -> a -> b
$ \State Int st
s -> do
State Int st
-> Exception [Char] ex
-> (forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff (st :& (ex :& es)) a
forall (e1 :: Effects -> *) (s1 :: Effects) (e2 :: Effects -> *)
(s2 :: Effects) (es :: Effects) r.
e1 s1
-> e2 s2
-> (forall (es' :: Effects).
Compound e1 e2 es' -> Eff (es' :& es) r)
-> Eff (s1 :& (s2 :& es)) r
runCompound State Int st
s Exception [Char] ex
e MyHandle es' -> Eff (es' :& es) a
forall (e :: Effects). MyHandle e -> Eff (e :& es) a
f
compoundExample :: Either String (a, Int)
compoundExample :: forall a. Either [Char] (a, Int)
compoundExample = (forall (es :: Effects). Eff es (Either [Char] (a, Int)))
-> Either [Char] (a, Int)
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es (Either [Char] (a, Int)))
-> Either [Char] (a, Int))
-> (forall (es :: Effects). Eff es (Either [Char] (a, Int)))
-> Either [Char] (a, Int)
forall a b. (a -> b) -> a -> b
$ (forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff es (Either [Char] (a, Int))
forall (es :: Effects) a.
(forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff es (Either [Char] (a, Int))
runMyHandle ((forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff es (Either [Char] (a, Int)))
-> (forall (e :: Effects). MyHandle e -> Eff (e :& es) a)
-> Eff es (Either [Char] (a, Int))
forall a b. (a -> b) -> a -> b
$ \MyHandle e
h -> do
MyHandle e -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects).
(e :> es) =>
MyHandle e -> Eff es ()
myInc MyHandle e
h
MyHandle e -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects).
(e :> es) =>
MyHandle e -> Eff es ()
myInc MyHandle e
h
MyHandle e -> Eff (e :& es) a
forall (e :: Effects) (es :: Effects) r.
(e :> es) =>
MyHandle e -> Eff es r
myBail MyHandle e
h
countExample :: IO ()
countExample :: IO ()
countExample = (forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ()
forall a.
(forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) a)
-> IO a
runEff ((forall (e :: Effects) (es :: Effects). IOE e -> Eff (e :& es) ())
-> IO ())
-> (forall (e :: Effects) (es :: Effects).
IOE e -> Eff (e :& es) ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \IOE e
io -> do
forall s (es :: Effects) a.
s
-> (forall (st :: Effects). State s st -> Eff (st :& es) a)
-> Eff es a
evalState @Int Int
0 ((forall {st :: Effects}. State Int st -> Eff (st :& (e :& es)) ())
-> Eff (e :& es) ())
-> (forall {st :: Effects}.
State Int st -> Eff (st :& (e :& es)) ())
-> Eff (e :& es) ()
forall a b. (a -> b) -> a -> b
$ \State Int st
sn -> do
(forall (j :: Effects). Jump j -> Eff (j :& (st :& (e :& es))) ())
-> Eff (st :& (e :& es)) ()
forall (es :: Effects).
(forall (j :: Effects). Jump j -> Eff (j :& es) ()) -> Eff es ()
withJump ((forall (j :: Effects). Jump j -> Eff (j :& (st :& (e :& es))) ())
-> Eff (st :& (e :& es)) ())
-> (forall (j :: Effects).
Jump j -> Eff (j :& (st :& (e :& es))) ())
-> Eff (st :& (e :& es)) ()
forall a b. (a -> b) -> a -> b
$ \Jump j
break -> Eff (j :& (st :& (e :& es))) () -> Eff (j :& (st :& (e :& es))) ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (Eff (j :& (st :& (e :& es))) ()
-> Eff (j :& (st :& (e :& es))) ())
-> Eff (j :& (st :& (e :& es))) ()
-> Eff (j :& (st :& (e :& es))) ()
forall a b. (a -> b) -> a -> b
$ do
Int
n <- State Int st -> Eff (j :& (st :& (e :& es))) Int
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> Eff es s
get State Int st
sn
Bool
-> Eff (j :& (st :& (e :& es))) ()
-> Eff (j :& (st :& (e :& es))) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
10) (Jump j -> Eff (j :& (st :& (e :& es))) ()
forall (j :: Effects) (es :: Effects) a.
(j :> es) =>
Jump j -> Eff es a
jumpTo Jump j
break)
IOE e -> IO () -> Eff (j :& (st :& (e :& es))) ()
forall (e :: Effects) (es :: Effects) a.
(e :> es) =>
IOE e -> IO a -> Eff es a
effIO IOE e
io (Int -> IO ()
forall a. Show a => a -> IO ()
print Int
n)
State Int st -> (Int -> Int) -> Eff (j :& (st :& (e :& es))) ()
forall (st :: Effects) (es :: Effects) s.
(st :> es) =>
State s st -> (s -> s) -> Eff es ()
modify State Int st
sn (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
writerExample1 :: Bool
writerExample1 :: Bool
writerExample1 = Any -> Bool
getAny (Any -> Bool) -> Any -> Bool
forall a b. (a -> b) -> a -> b
$ (forall (es :: Effects). Eff es Any) -> Any
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es Any) -> Any)
-> (forall (es :: Effects). Eff es Any) -> Any
forall a b. (a -> b) -> a -> b
$ (forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any
forall w (es :: Effects) r.
Monoid w =>
(forall (e :: Effects). Writer w e -> Eff (e :& es) r) -> Eff es w
execWriter ((forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any)
-> (forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any
forall a b. (a -> b) -> a -> b
$ \Writer Any e
w -> do
[Any] -> (Any -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [] ((Any -> Eff (e :& es) ()) -> Eff (e :& es) ())
-> (Any -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall a b. (a -> b) -> a -> b
$ \Any
_ -> Writer Any e -> Any -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects) w.
(e :> es) =>
Writer w e -> w -> Eff es ()
tell Writer Any e
w (Bool -> Any
Any Bool
True)
writerExample2 :: Bool
writerExample2 :: Bool
writerExample2 = Any -> Bool
getAny (Any -> Bool) -> Any -> Bool
forall a b. (a -> b) -> a -> b
$ (forall (es :: Effects). Eff es Any) -> Any
forall a. (forall (es :: Effects). Eff es a) -> a
runPureEff ((forall (es :: Effects). Eff es Any) -> Any)
-> (forall (es :: Effects). Eff es Any) -> Any
forall a b. (a -> b) -> a -> b
$ (forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any
forall w (es :: Effects) r.
Monoid w =>
(forall (e :: Effects). Writer w e -> Eff (e :& es) r) -> Eff es w
execWriter ((forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any)
-> (forall (e :: Effects). Writer Any e -> Eff (e :& es) ())
-> Eff es Any
forall a b. (a -> b) -> a -> b
$ \Writer Any e
w -> do
[Integer] -> (Integer -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Integer
1 .. Integer
10] ((Integer -> Eff (e :& es) ()) -> Eff (e :& es) ())
-> (Integer -> Eff (e :& es) ()) -> Eff (e :& es) ()
forall a b. (a -> b) -> a -> b
$ \Integer
_ -> Writer Any e -> Any -> Eff (e :& es) ()
forall (e :: Effects) (es :: Effects) w.
(e :> es) =>
Writer w e -> w -> Eff es ()
tell Writer Any e
w (Bool -> Any
Any Bool
True)