{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Effect.Internal where
import qualified Control.Exception as IO
import qualified Data.Type.Coercion as Coercion
import Control.Applicative
import Control.Exception (Exception)
import Control.Monad
import Control.Monad.IO.Class
import Data.Bool (bool)
import Data.Coerce
import Data.Functor
import Data.IORef
import Data.Kind (Constraint, Type)
import Data.Type.Coercion (Coercion(..), gcoerceWith)
import Data.Type.Equality ((:~:)(..), gcastWith)
import GHC.Exts (Any, Int(..), Int#, RealWorld, RuntimeRep(..), SmallArray#, State#, TYPE, reset#, shift#)
import GHC.Types (IO(..))
import System.IO.Unsafe (unsafeDupablePerformIO)
import Unsafe.Coerce (unsafeCoerce)
import Control.Effect.Internal.Debug
import Control.Effect.Internal.SmallArray
axiom :: a :~: b
axiom :: a :~: b
axiom = (Any :~: Any) -> a :~: b
forall a b. a -> b
unsafeCoerce Any :~: Any
forall {k} (a :: k). a :~: a
Refl
{-# INLINE axiom #-}
pattern Any :: forall a. a -> Any
pattern $bAny :: a -> Any
$mAny :: forall {r} {a}. Any -> (a -> r) -> (Void# -> r) -> r
Any a <- (unsafeCoerce -> a)
where Any = a -> Any
forall a b. a -> b
unsafeCoerce
{-# COMPLETE Any #-}
anyCo :: forall a. Coercion a Any
anyCo :: Coercion a Any
anyCo = Coercion a a -> Coercion a Any
forall a b. a -> b
unsafeCoerce (Coercible a a => Coercion a a
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion @a @a)
{-# INLINE anyCo #-}
null# :: Any
null# :: Any
null# = () -> Any
forall a. a -> Any
Any ()
unIO :: IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO :: IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (IO State# RealWorld -> (# State# RealWorld, a #)
m) = State# RealWorld -> (# State# RealWorld, a #)
m
{-# INLINE unIO #-}
data Dict c = c => Dict
type DictRep :: Constraint -> Type
type family DictRep c
type WithDict :: Constraint -> Type -> Type
newtype WithDict c r = WithDict { WithDict c r -> c => r
unWithDict :: c => r }
reflectDict :: forall c r. DictRep c -> (c => r) -> r
reflectDict :: DictRep c -> (c => r) -> r
reflectDict !DictRep c
d c => r
x = WithDict c r -> DictRep c -> r
forall a b. a -> b
unsafeCoerce ((c => r) -> WithDict c r
forall (c :: Constraint) r. (c => r) -> WithDict c r
WithDict @c @r c => r
x) DictRep c
d
{-# INLINE reflectDict #-}
type Effect = (Type -> Type) -> Type -> Type
type (:<) :: Effect -> [Effect] -> Constraint
class eff :< effs where
reifyIndex :: Int
instance {-# OVERLAPPING #-} eff :< (eff ': effs) where
reifyIndex :: Int
reifyIndex = Int
0
{-# INLINE reifyIndex #-}
instance eff :< effs => eff :< (eff' ': effs) where
reifyIndex :: Int
reifyIndex = (eff :< effs) => Int
forall (eff :: Effect) (effs :: [Effect]). (eff :< effs) => Int
reifyIndex @eff @effs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
{-# INLINE reifyIndex #-}
type (:<<) :: [Effect] -> [Effect] -> Constraint
class effs1 :<< effs2 where
reifySubIndex :: Int
instance {-# OVERLAPPING #-} effs :<< effs where
reifySubIndex :: Int
reifySubIndex = Int
0
{-# INLINE reifySubIndex #-}
instance (effs2 ~ (eff ': effs3), effs1 :<< effs3) => effs1 :<< effs2 where
reifySubIndex :: Int
reifySubIndex = (effs1 :<< effs3) => Int
forall (effs1 :: [Effect]) (effs2 :: [Effect]).
(effs1 :<< effs2) =>
Int
reifySubIndex @effs1 @effs3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
{-# INLINE reifySubIndex #-}
type instance DictRep (eff :< effs) = Int
type instance DictRep (effs1 :<< effs2) = Int
type (:<#) :: Effect -> [Effect] -> TYPE 'IntRep
newtype eff :<# effs = ReflectIndex# { (eff :<# effs) -> Int#
reifyIndex# :: Int# }
pattern IndexDict# :: forall eff effs. () => eff :< effs => eff :<# effs
pattern $bIndexDict# :: eff :<# effs
$mIndexDict# :: forall {r} {eff :: Effect} {effs :: [Effect]}.
(eff :<# effs) -> ((eff :< effs) => r) -> (Void# -> r) -> r
IndexDict# <- ReflectIndex# ((\idx -> reflectDict @(eff :< effs) (I# idx) (Dict @(eff :< effs))) -> Dict)
where IndexDict# = case (eff :< effs) => Int
forall (eff :: Effect) (effs :: [Effect]). (eff :< effs) => Int
reifyIndex @eff @effs of I# Int#
idx -> Int# -> eff :<# effs
forall (eff :: Effect) (effs :: [Effect]). Int# -> eff :<# effs
ReflectIndex# Int#
idx
{-# COMPLETE IndexDict# #-}
type Eff :: [Effect] -> Type -> Type
type role Eff nominal representational
newtype Eff effs a = Eff# { Eff effs a -> EVM a
unEff# :: EVM a }
deriving (a -> Eff effs b -> Eff effs a
(a -> b) -> Eff effs a -> Eff effs b
(forall a b. (a -> b) -> Eff effs a -> Eff effs b)
-> (forall a b. a -> Eff effs b -> Eff effs a)
-> Functor (Eff effs)
forall (effs :: [Effect]) a b. a -> Eff effs b -> Eff effs a
forall (effs :: [Effect]) a b. (a -> b) -> Eff effs a -> Eff effs b
forall a b. a -> Eff effs b -> Eff effs a
forall a b. (a -> b) -> Eff effs a -> Eff effs b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Eff effs b -> Eff effs a
$c<$ :: forall (effs :: [Effect]) a b. a -> Eff effs b -> Eff effs a
fmap :: (a -> b) -> Eff effs a -> Eff effs b
$cfmap :: forall (effs :: [Effect]) a b. (a -> b) -> Eff effs a -> Eff effs b
Functor, Functor (Eff effs)
a -> Eff effs a
Functor (Eff effs)
-> (forall a. a -> Eff effs a)
-> (forall a b. Eff effs (a -> b) -> Eff effs a -> Eff effs b)
-> (forall a b c.
(a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c)
-> (forall a b. Eff effs a -> Eff effs b -> Eff effs b)
-> (forall a b. Eff effs a -> Eff effs b -> Eff effs a)
-> Applicative (Eff effs)
Eff effs a -> Eff effs b -> Eff effs b
Eff effs a -> Eff effs b -> Eff effs a
Eff effs (a -> b) -> Eff effs a -> Eff effs b
(a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c
forall {effs :: [Effect]}. Functor (Eff effs)
forall (effs :: [Effect]) a. a -> Eff effs a
forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs a
forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs b
forall (effs :: [Effect]) a b.
Eff effs (a -> b) -> Eff effs a -> Eff effs b
forall (effs :: [Effect]) a b c.
(a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c
forall a. a -> Eff effs a
forall a b. Eff effs a -> Eff effs b -> Eff effs a
forall a b. Eff effs a -> Eff effs b -> Eff effs b
forall a b. Eff effs (a -> b) -> Eff effs a -> Eff effs b
forall a b c.
(a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: Eff effs a -> Eff effs b -> Eff effs a
$c<* :: forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs a
*> :: Eff effs a -> Eff effs b -> Eff effs b
$c*> :: forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs b
liftA2 :: (a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c
$cliftA2 :: forall (effs :: [Effect]) a b c.
(a -> b -> c) -> Eff effs a -> Eff effs b -> Eff effs c
<*> :: Eff effs (a -> b) -> Eff effs a -> Eff effs b
$c<*> :: forall (effs :: [Effect]) a b.
Eff effs (a -> b) -> Eff effs a -> Eff effs b
pure :: a -> Eff effs a
$cpure :: forall (effs :: [Effect]) a. a -> Eff effs a
$cp1Applicative :: forall {effs :: [Effect]}. Functor (Eff effs)
Applicative, Applicative (Eff effs)
a -> Eff effs a
Applicative (Eff effs)
-> (forall a b. Eff effs a -> (a -> Eff effs b) -> Eff effs b)
-> (forall a b. Eff effs a -> Eff effs b -> Eff effs b)
-> (forall a. a -> Eff effs a)
-> Monad (Eff effs)
Eff effs a -> (a -> Eff effs b) -> Eff effs b
Eff effs a -> Eff effs b -> Eff effs b
forall {effs :: [Effect]}. Applicative (Eff effs)
forall (effs :: [Effect]) a. a -> Eff effs a
forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs b
forall (effs :: [Effect]) a b.
Eff effs a -> (a -> Eff effs b) -> Eff effs b
forall a. a -> Eff effs a
forall a b. Eff effs a -> Eff effs b -> Eff effs b
forall a b. Eff effs a -> (a -> Eff effs b) -> Eff effs b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> Eff effs a
$creturn :: forall (effs :: [Effect]) a. a -> Eff effs a
>> :: Eff effs a -> Eff effs b -> Eff effs b
$c>> :: forall (effs :: [Effect]) a b.
Eff effs a -> Eff effs b -> Eff effs b
>>= :: Eff effs a -> (a -> Eff effs b) -> Eff effs b
$c>>= :: forall (effs :: [Effect]) a b.
Eff effs a -> (a -> Eff effs b) -> Eff effs b
$cp1Monad :: forall {effs :: [Effect]}. Applicative (Eff effs)
Monad)
pattern Eff :: (Registers -> IO (Registers, a)) -> Eff effs a
pattern $bEff :: (Registers -> IO (Registers, a)) -> Eff effs a
$mEff :: forall {r} {a} {effs :: [Effect]}.
Eff effs a
-> ((Registers -> IO (Registers, a)) -> r) -> (Void# -> r) -> r
Eff{Eff effs a -> Registers -> IO (Registers, a)
unEff} = Eff# (EVM unEff)
{-# COMPLETE Eff #-}
newtype EVM a = EVM# { EVM a -> Registers# -> IO (Result a)
unEVM# :: Registers# -> IO (Result a) }
data Result a = Result Registers# ~a
pattern EVM :: (Registers -> IO (Registers, a)) -> EVM a
pattern $bEVM :: (Registers -> IO (Registers, a)) -> EVM a
$mEVM :: forall {r} {a}.
EVM a
-> ((Registers -> IO (Registers, a)) -> r) -> (Void# -> r) -> r
EVM{EVM a -> Registers -> IO (Registers, a)
unEVM} <- EVM# ((\m (BoxRegisters rs1) -> m rs1 <&> \(Result rs2 a) -> (BoxRegisters rs2, a)) -> unEVM)
where EVM Registers -> IO (Registers, a)
m = (Registers# -> IO (Result a)) -> EVM a
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# \Registers#
rs1 -> Registers -> IO (Registers, a)
m (Registers# -> Registers
BoxRegisters Registers#
rs1) IO (Registers, a) -> ((Registers, a) -> Result a) -> IO (Result a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(BoxRegisters Registers#
rs2, a
a) -> Registers# -> a -> Result a
forall a. Registers# -> a -> Result a
Result Registers#
rs2 a
a
{-# COMPLETE EVM #-}
packIOResult :: IO (Registers, a) -> IO (Result a)
packIOResult :: IO (Registers, a) -> IO (Result a)
packIOResult IO (Registers, a)
m = IO (Registers, a)
m IO (Registers, a)
-> ((Registers, a) -> IO (Result a)) -> IO (Result a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(BoxRegisters Registers#
rs, a
a) -> Result a -> IO (Result a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Result a -> IO (Result a)) -> Result a -> IO (Result a)
forall a b. (a -> b) -> a -> b
$! Registers# -> a -> Result a
forall a. Registers# -> a -> Result a
Result Registers#
rs a
a
{-# INLINE packIOResult #-}
newtype Registers# = Registers# (# PromptId, Targets# #)
data Registers = BoxRegisters { Registers -> Registers#
unboxRegisters :: Registers# }
pattern Registers :: PromptId -> Targets -> Registers
pattern $bRegisters :: PromptId -> Targets -> Registers
$mRegisters :: forall {r}.
Registers -> (PromptId -> Targets -> r) -> (Void# -> r) -> r
Registers pid ts <- BoxRegisters (Registers# (# pid, (BoxTargets -> ts) #))
where Registers PromptId
pid (BoxTargets Targets#
ts) = Registers# -> Registers
BoxRegisters ((# PromptId, Targets# #) -> Registers#
Registers# (# PromptId
pid, Targets#
ts #))
{-# COMPLETE Registers #-}
initialRegisters :: Registers
initialRegisters :: Registers
initialRegisters = PromptId -> Targets -> Registers
Registers (Int -> PromptId
PromptId Int
0) Targets
noTargets
newtype PromptId = PromptId# Int#
pattern PromptId :: Int -> PromptId
pattern $bPromptId :: Int -> PromptId
$mPromptId :: forall {r}. PromptId -> (Int -> r) -> (Void# -> r) -> r
PromptId{PromptId -> Int
unPromptId} <- PromptId# (I# -> unPromptId)
where PromptId (I# Int#
n) = Int# -> PromptId
PromptId# Int#
n
{-# COMPLETE PromptId #-}
data Unwind
= UnwindAbort PromptId ~Any
| UnwindControl (Capture Any)
instance Show Unwind where
show :: Unwind -> String
show (UnwindAbort (PromptId Int
pid) Any
_)
= String
"<<Control.Eff.Internal.abort:" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
pid String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">>"
show (UnwindControl (Capture (PromptId Int
pid) CaptureMode
_ (b -> EVM c) -> EVM d
_ b -> EVM Any
_))
= String
"<<Control.Eff.Internal.control:" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
pid String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">>"
instance Exception Unwind
data Capture a where
Capture
:: PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
data CaptureMode
= IncludePrompt
| ExcludePrompt
captureVM :: forall a b. Capture a -> IO b
captureVM :: Capture a -> IO b
captureVM Capture a
a = Coercion Any a -> (Coercible Any a => IO b) -> IO b
forall {k} (a :: k) (b :: k) r.
Coercion a b -> (Coercible a b => r) -> r
gcoerceWith (Coercion a Any -> Coercion Any a
forall {k} (a :: k) (b :: k). Coercion a b -> Coercion b a
Coercion.sym (Coercion a Any -> Coercion Any a)
-> Coercion a Any -> Coercion Any a
forall a b. (a -> b) -> a -> b
$ Coercion a Any
forall {k} (a :: k). Coercion a Any
anyCo @a) ((Coercible Any a => IO b) -> IO b)
-> (Coercible Any a => IO b) -> IO b
forall a b. (a -> b) -> a -> b
$
Unwind -> IO b
forall e a. Exception e => e -> IO a
IO.throwIO (Unwind -> IO b) -> Unwind -> IO b
forall a b. (a -> b) -> a -> b
$! Capture Any -> Unwind
UnwindControl (Capture a -> Capture Any
forall a b. Coercible a b => a -> b
coerce Capture a
a)
{-# INLINE captureVM #-}
promptVM
:: forall a b
. IO (Registers, a)
-> (a -> IO b)
-> (PromptId -> Any -> IO b)
-> (Capture a -> IO b)
-> IO b
promptVM :: IO (Registers, a)
-> (a -> IO b)
-> (PromptId -> Any -> IO b)
-> (Capture a -> IO b)
-> IO b
promptVM IO (Registers, a)
m a -> IO b
onReturn PromptId -> Any -> IO b
onAbort Capture a -> IO b
onControl = (Unwind -> IO b) -> IO b -> IO b
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
IO.handle Unwind -> IO b
handleUnwind do
Result Registers#
_ a
a <- (State# RealWorld -> (# State# RealWorld, Result a #))
-> IO (Result a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result a #)
forall a.
(State# RealWorld -> (# State# RealWorld, a #))
-> State# RealWorld -> (# State# RealWorld, a #)
reset# (IO (Result a)
-> State# RealWorld -> (# State# RealWorld, Result a #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (IO (Registers, a) -> IO (Result a)
forall a. IO (Registers, a) -> IO (Result a)
packIOResult IO (Registers, a)
m)))
a -> IO b
onReturn a
a
where
handleUnwind :: Unwind -> IO b
handleUnwind (UnwindAbort PromptId
pid Any
a) = PromptId -> Any -> IO b
onAbort PromptId
pid Any
a
handleUnwind (UnwindControl Capture Any
cap) = Coercion a Any -> (Coercible a Any => IO b) -> IO b
forall {k} (a :: k) (b :: k) r.
Coercion a b -> (Coercible a b => r) -> r
gcoerceWith (Coercion a Any
forall {k} (a :: k). Coercion a Any
anyCo @a) ((Coercible a Any => IO b) -> IO b)
-> (Coercible a Any => IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ Capture a -> IO b
onControl (Capture Any -> Capture a
forall a b. Coercible a b => a -> b
coerce Capture Any
cap)
{-# INLINE promptVM #-}
promptVM_
:: forall a
. IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ :: IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ IO (Registers, a)
m Registers
rs Capture a -> IO (Registers, a)
onCapture = IO (Registers, a)
-> (a -> IO (Registers, a))
-> (PromptId -> Any -> IO (Registers, a))
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
forall a b.
IO (Registers, a)
-> (a -> IO b)
-> (PromptId -> Any -> IO b)
-> (Capture a -> IO b)
-> IO b
promptVM IO (Registers, a)
m a -> IO (Registers, a)
onReturn PromptId -> Any -> IO (Registers, a)
forall {a}. PromptId -> Any -> IO a
rethrowAbort Capture a -> IO (Registers, a)
onCapture where
onReturn :: a -> IO (Registers, a)
onReturn a
a = (Registers, a) -> IO (Registers, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Registers
rs, a
a)
rethrowAbort :: PromptId -> Any -> IO a
rethrowAbort PromptId
pid Any
a = Unwind -> IO a
forall e a. Exception e => e -> IO a
IO.throwIO (Unwind -> IO a) -> Unwind -> IO a
forall a b. (a -> b) -> a -> b
$! PromptId -> Any -> Unwind
UnwindAbort PromptId
pid Any
a
{-# INLINE promptVM_ #-}
controlVM :: ((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM :: ((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM (a -> EVM b) -> IO (Registers, b)
f = (State# RealWorld -> (# State# RealWorld, Result a #))
-> IO (Result a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((((State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result b #))
-> State# RealWorld -> (# State# RealWorld, Result b #))
-> State# RealWorld -> (# State# RealWorld, Result a #)
forall a b.
(((State# RealWorld -> (# State# RealWorld, a #))
-> State# RealWorld -> (# State# RealWorld, b #))
-> State# RealWorld -> (# State# RealWorld, b #))
-> State# RealWorld -> (# State# RealWorld, a #)
shift# ((State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result b #))
-> State# RealWorld -> (# State# RealWorld, Result b #)
f#) IO (Result a) -> (Result a -> (Registers, a)) -> IO (Registers, a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(Result Registers#
rs a
a) -> (Registers# -> Registers
BoxRegisters Registers#
rs, a
a) where
f# :: ((State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result b #))
-> State# RealWorld -> (# State# RealWorld, Result b #)
f# (State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result b #)
k# = IO (Result b)
-> State# RealWorld -> (# State# RealWorld, Result b #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO ((a -> EVM b) -> IO (Registers, b)
f a -> EVM b
k IO (Registers, b) -> ((Registers, b) -> Result b) -> IO (Result b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(BoxRegisters Registers#
rs, b
a) -> Registers# -> b -> Result b
forall a. Registers# -> a -> Result a
Result Registers#
rs b
a) where
k :: a -> EVM b
k a
a = (Registers# -> IO (Result b)) -> EVM b
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# \Registers#
rs -> (State# RealWorld -> (# State# RealWorld, Result b #))
-> IO (Result b)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Result b #))
-> IO (Result b))
-> (State# RealWorld -> (# State# RealWorld, Result b #))
-> IO (Result b)
forall a b. (a -> b) -> a -> b
$ (State# RealWorld -> (# State# RealWorld, Result a #))
-> State# RealWorld -> (# State# RealWorld, Result b #)
k# \State# RealWorld
s -> (# State# RealWorld
s, Registers# -> a -> Result a
forall a. Registers# -> a -> Result a
Result Registers#
rs a
a #)
{-# INLINE controlVM #-}
parameterizeVM :: (Registers -> Registers) -> EVM a -> EVM a
parameterizeVM :: (Registers -> Registers) -> EVM a -> EVM a
parameterizeVM Registers -> Registers
adjust (EVM Registers -> IO (Registers, a)
m0) = (Registers -> IO (Registers, a)) -> EVM a
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM \Registers
rs -> do
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
forall a.
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ (Registers -> IO (Registers, a)
m0 (Registers -> Registers
adjust Registers
rs)) Registers
rs \(Capture PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
f b -> EVM a
k1) ->
((a -> EVM Any) -> IO (Registers, Any)) -> IO (Registers, a)
forall a b.
((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM \a -> EVM Any
k2 -> Capture Any -> IO (Registers, Any)
forall a b. Capture a -> IO b
captureVM (Capture Any -> IO (Registers, Any))
-> Capture Any -> IO (Registers, Any)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> (a -> EVM Any)
-> Capture Any
forall a d e b c.
PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> Capture c
handleCapture PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
f b -> EVM a
k1 a -> EVM Any
k2
where
handleCapture
:: PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> Capture c
handleCapture :: PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> Capture c
handleCapture PromptId
target1 CaptureMode
mode1 (a -> EVM d) -> EVM e
f1 a -> EVM b
k1 b -> EVM c
k2 =
let k3 :: a -> EVM c
k3 a
a = (Registers -> IO (Registers, c)) -> EVM c
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM \Registers
rs1 -> do
let m :: IO (Registers, b)
m = EVM b -> Registers -> IO (Registers, b)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (a -> EVM b
k1 a
a) (Registers -> Registers
adjust Registers
rs1)
(Registers
rs2, b
b) <- IO (Registers, b)
-> Registers
-> (Capture b -> IO (Registers, b))
-> IO (Registers, b)
forall a.
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ IO (Registers, b)
m Registers
rs1 \(Capture PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
f2 b -> EVM b
k4) ->
Capture c -> IO (Registers, b)
forall a b. Capture a -> IO b
captureVM (Capture c -> IO (Registers, b)) -> Capture c -> IO (Registers, b)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM b)
-> (b -> EVM c)
-> Capture c
forall a d e b c.
PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> Capture c
handleCapture PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
f2 b -> EVM b
k4 b -> EVM c
k2
EVM c -> Registers -> IO (Registers, c)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (b -> EVM c
k2 b
b) Registers
rs2
in PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM c)
-> Capture c
forall b c d a.
PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
Capture PromptId
target1 CaptureMode
mode1 (a -> EVM d) -> EVM e
f1 a -> EVM c
k3
{-# INLINE parameterizeVM #-}
newtype Targets# = Targets# (SmallArray# Any)
newtype Targets = Targets (SmallArray Any)
pattern BoxTargets :: Targets# -> Targets
pattern $bBoxTargets :: Targets# -> Targets
$mBoxTargets :: forall {r}. Targets -> (Targets# -> r) -> (Void# -> r) -> r
BoxTargets ts <- Targets (SmallArray (Targets# -> ts))
where BoxTargets (Targets# SmallArray# Any
ts) = SmallArray Any -> Targets
Targets (SmallArray# Any -> SmallArray Any
forall a. SmallArray# a -> SmallArray a
SmallArray SmallArray# Any
ts)
{-# COMPLETE BoxTargets #-}
noTargets :: Targets
noTargets :: Targets
noTargets = SmallArray Any -> Targets
Targets SmallArray Any
forall a. Monoid a => a
mempty
lookupTarget :: forall effs eff. (DebugCallStack, eff :< effs) => Targets -> Handler eff
lookupTarget :: Targets -> Handler eff
lookupTarget (Targets SmallArray Any
ts) = case SmallArray Any -> Int -> (# Any #)
forall a. DebugCallStack => SmallArray a -> Int -> (# a #)
indexSmallArray SmallArray Any
ts ((eff :< effs) => Int
forall (eff :: Effect) (effs :: [Effect]). (eff :< effs) => Int
reifyIndex @eff @effs) of (# Any h #) -> Handler eff
h
pushTarget :: Handler eff -> Targets -> Targets
pushTarget :: Handler eff -> Targets -> Targets
pushTarget Handler eff
h (Targets SmallArray Any
ts1) = SmallArray Any -> Targets
Targets (SmallArray Any -> Targets) -> SmallArray Any -> Targets
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (SmallMutableArray s Any)) -> SmallArray Any
forall a. (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
runSmallArray do
let len :: Int
len = SmallArray Any -> Int
forall a. DebugCallStack => SmallArray a -> Int
sizeofSmallArray SmallArray Any
ts1
SmallMutableArray s Any
ts2 <- Int -> Any -> ST s (SmallMutableArray (PrimState (ST s)) Any)
forall (m :: * -> *) a.
(DebugCallStack, PrimMonad m) =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Any
null#
SmallMutableArray (PrimState (ST s)) Any -> Int -> Any -> ST s ()
forall (m :: * -> *) a.
(DebugCallStack, PrimMonad m) =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s Any
SmallMutableArray (PrimState (ST s)) Any
ts2 Int
0 (Handler eff -> Any
forall a. a -> Any
Any Handler eff
h)
SmallMutableArray (PrimState (ST s)) Any
-> Int -> SmallArray Any -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
(DebugCallStack, PrimMonad m) =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s Any
SmallMutableArray (PrimState (ST s)) Any
ts2 Int
1 SmallArray Any
ts1 Int
0 Int
len
SmallMutableArray s Any -> ST s (SmallMutableArray s Any)
forall (f :: * -> *) a. Applicative f => a -> f a
pure SmallMutableArray s Any
ts2
dropTargets :: DebugCallStack => Int -> Targets -> Targets
dropTargets :: Int -> Targets -> Targets
dropTargets Int
idx (Targets SmallArray Any
ts) = SmallArray Any -> Targets
Targets (SmallArray Any -> Targets) -> SmallArray Any -> Targets
forall a b. (a -> b) -> a -> b
$ SmallArray Any -> Int -> Int -> SmallArray Any
forall a.
DebugCallStack =>
SmallArray a -> Int -> Int -> SmallArray a
cloneSmallArray SmallArray Any
ts Int
idx (SmallArray Any -> Int
forall a. DebugCallStack => SmallArray a -> Int
sizeofSmallArray SmallArray Any
ts Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
idx)
instance Functor EVM where
fmap :: (a -> b) -> EVM a -> EVM b
fmap a -> b
f EVM a
m = EVM a
m EVM a -> (a -> EVM b) -> EVM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> EVM b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> EVM b) -> (a -> b) -> a -> EVM b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
{-# INLINE fmap #-}
instance Applicative EVM where
pure :: a -> EVM a
pure a
a = (Registers# -> IO (Result a)) -> EVM a
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# \Registers#
rs -> Result a -> IO (Result a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Result a -> IO (Result a)) -> Result a -> IO (Result a)
forall a b. (a -> b) -> a -> b
$ Registers# -> a -> Result a
forall a. Registers# -> a -> Result a
Result Registers#
rs a
a
{-# INLINE pure #-}
<*> :: EVM (a -> b) -> EVM a -> EVM b
(<*>) = EVM (a -> b) -> EVM a -> EVM b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
{-# INLINE (<*>) #-}
instance Monad EVM where
EVM# Registers# -> IO (Result a)
m >>= :: EVM a -> (a -> EVM b) -> EVM b
>>= a -> EVM b
f = (Registers# -> IO (Result b)) -> EVM b
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# \Registers#
rs1 -> Registers# -> IO (Result a)
m Registers#
rs1 IO (Result a) -> (Result a -> IO (Result b)) -> IO (Result b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Result Registers#
rs2 a
a) -> EVM b -> Registers# -> IO (Result b)
forall a. EVM a -> Registers# -> IO (Result a)
unEVM# (a -> EVM b
f a
a) Registers#
rs2
{-# INLINE (>>=) #-}
instance MonadIO EVM where
liftIO :: IO a -> EVM a
liftIO IO a
m = (Registers# -> IO (Result a)) -> EVM a
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# \Registers#
rs -> Registers# -> a -> Result a
forall a. Registers# -> a -> Result a
Result Registers#
rs (a -> Result a) -> IO a -> IO (Result a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
m
{-# INLINE liftIO #-}
run :: Eff '[] a -> a
run :: Eff '[] a -> a
run (Eff Registers -> IO (Registers, a)
m) = IO a -> a
forall a. IO a -> a
unsafeDupablePerformIO ((Registers, a) -> a
forall a b. (a, b) -> b
snd ((Registers, a) -> a) -> IO (Registers, a) -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Registers -> IO (Registers, a)
m Registers
initialRegisters)
type Handle :: Effect -> [Effect] -> Type -> Type -> [Effect] -> Type -> Type
type role Handle nominal nominal representational representational nominal representational
newtype Handle eff effs i r effs' a = Handle# { Handle eff effs i r effs' a -> Registers# -> Eff effs' a
runHandle# :: Registers# -> Eff effs' a }
pattern Handle :: (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
pattern $bHandle :: (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
$mHandle :: forall {r} {effs' :: [Effect]} {a} {eff :: Effect}
{effs :: [Effect]} {i} {r}.
Handle eff effs i r effs' a
-> ((Registers -> Eff effs' a) -> r) -> (Void# -> r) -> r
Handle{Handle eff effs i r effs' a -> Registers -> Eff effs' a
runHandle} <- Handle# ((\f (BoxRegisters rs) -> f rs) -> runHandle)
where Handle Registers -> Eff effs' a
f = (Registers# -> Eff effs' a) -> Handle eff effs i r effs' a
forall (eff :: Effect) (effs :: [Effect]) i r (effs' :: [Effect])
a.
(Registers# -> Eff effs' a) -> Handle eff effs i r effs' a
Handle# \Registers#
rs -> Registers -> Eff effs' a
f (Registers# -> Registers
BoxRegisters Registers#
rs)
{-# COMPLETE Handle #-}
instance Functor (Handle eff effs i r effs') where
fmap :: (a -> b)
-> Handle eff effs i r effs' a -> Handle eff effs i r effs' b
fmap a -> b
f Handle eff effs i r effs' a
m = Handle eff effs i r effs' a
m Handle eff effs i r effs' a
-> (a -> Handle eff effs i r effs' b)
-> Handle eff effs i r effs' b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Handle eff effs i r effs' b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Handle eff effs i r effs' b)
-> (a -> b) -> a -> Handle eff effs i r effs' b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
{-# INLINE fmap #-}
instance Applicative (Handle eff effs i r effs') where
pure :: a -> Handle eff effs i r effs' a
pure a
a = (Registers# -> Eff effs' a) -> Handle eff effs i r effs' a
forall (eff :: Effect) (effs :: [Effect]) i r (effs' :: [Effect])
a.
(Registers# -> Eff effs' a) -> Handle eff effs i r effs' a
Handle# \Registers#
_ -> a -> Eff effs' a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
{-# INLINE pure #-}
<*> :: Handle eff effs i r effs' (a -> b)
-> Handle eff effs i r effs' a -> Handle eff effs i r effs' b
(<*>) = Handle eff effs i r effs' (a -> b)
-> Handle eff effs i r effs' a -> Handle eff effs i r effs' b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
{-# INLINE (<*>) #-}
instance Monad (Handle eff effs i r effs') where
Handle# Registers# -> Eff effs' a
m >>= :: Handle eff effs i r effs' a
-> (a -> Handle eff effs i r effs' b)
-> Handle eff effs i r effs' b
>>= a -> Handle eff effs i r effs' b
f = (Registers# -> Eff effs' b) -> Handle eff effs i r effs' b
forall (eff :: Effect) (effs :: [Effect]) i r (effs' :: [Effect])
a.
(Registers# -> Eff effs' a) -> Handle eff effs i r effs' a
Handle# \Registers#
rs -> Registers# -> Eff effs' a
m Registers#
rs Eff effs' a -> (a -> Eff effs' b) -> Eff effs' b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> Handle eff effs i r effs' b -> Registers# -> Eff effs' b
forall (eff :: Effect) (effs :: [Effect]) i r (effs' :: [Effect])
a.
Handle eff effs i r effs' a -> Registers# -> Eff effs' a
runHandle# (a -> Handle eff effs i r effs' b
f a
a) Registers#
rs
{-# INLINE (>>=) #-}
newtype Handler eff
= Handler# { Handler eff
-> forall (effs :: [Effect]) a.
(eff :<# effs) -> eff (Eff effs) a -> Eff effs a
runHandler# :: forall effs a. eff :<# effs -> eff (Eff effs) a -> Eff effs a }
newtype WrappedHandler eff
= WrapHandler (forall effs a. eff :< effs => eff (Eff effs) a -> Eff effs a)
pattern Handler :: (forall effs a. eff :< effs => eff (Eff effs) a -> Eff effs a) -> Handler eff
pattern $bHandler :: (forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a)
-> Handler eff
$mHandler :: forall {r} {eff :: Effect}.
Handler eff
-> ((forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a)
-> r)
-> (Void# -> r)
-> r
Handler{Handler eff
-> forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
runHandler} <- ((\(Handler# f) -> WrapHandler (f IndexDict#)) -> WrapHandler runHandler)
where Handler forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
f = (forall (effs :: [Effect]) a.
(eff :<# effs) -> eff (Eff effs) a -> Eff effs a)
-> Handler eff
forall (eff :: Effect).
(forall (effs :: [Effect]) a.
(eff :<# effs) -> eff (Eff effs) a -> Eff effs a)
-> Handler eff
Handler# \eff :<# effs
IndexDict# -> eff (Eff effs) a -> Eff effs a
forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
f
{-# COMPLETE Handler #-}
send :: forall eff a effs. eff :< effs => eff (Eff effs) a -> Eff effs a
send :: eff (Eff effs) a -> Eff effs a
send !eff (Eff effs) a
e = (Registers -> IO (Registers, a)) -> Eff effs a
forall a (effs :: [Effect]).
(Registers -> IO (Registers, a)) -> Eff effs a
Eff \rs :: Registers
rs@(Registers PromptId
_ Targets
ts) -> Eff effs a -> Registers -> IO (Registers, a)
forall a (effs :: [Effect]).
Eff effs a -> Registers -> IO (Registers, a)
unEff (Handler eff -> eff (Eff effs) a -> Eff effs a
forall (eff :: Effect).
Handler eff
-> forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
runHandler (Targets -> Handler eff
forall (effs :: [Effect]) (eff :: Effect).
(DebugCallStack, eff :< effs) =>
Targets -> Handler eff
lookupTarget @effs Targets
ts) eff (Eff effs) a
e) Registers
rs
handle
:: forall eff a r effs
. (a -> Eff effs r)
-> (forall effs' b. eff :< effs' => eff (Eff effs') b -> Handle eff effs a r effs' b)
-> Eff (eff ': effs) a
-> Eff effs r
handle :: (a -> Eff effs r)
-> (forall (effs' :: [Effect]) b.
(eff :< effs') =>
eff (Eff effs') b -> Handle eff effs a r effs' b)
-> Eff (eff : effs) a
-> Eff effs r
handle a -> Eff effs r
onReturn forall (effs' :: [Effect]) b.
(eff :< effs') =>
eff (Eff effs') b -> Handle eff effs a r effs' b
f = (a -> Eff effs r)
-> (Registers# -> Handler eff) -> Eff (eff : effs) a -> Eff effs r
forall (eff :: Effect) a r (effs :: [Effect]).
(a -> Eff effs r)
-> (Registers# -> Handler eff) -> Eff (eff : effs) a -> Eff effs r
handleVM a -> Eff effs r
onReturn \Registers#
rs -> (forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a)
-> Handler eff
forall (eff :: Effect).
(forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a)
-> Handler eff
Handler \eff (Eff effs) a
e -> Handle eff effs a r effs a -> Registers# -> Eff effs a
forall (eff :: Effect) (effs :: [Effect]) i r (effs' :: [Effect])
a.
Handle eff effs i r effs' a -> Registers# -> Eff effs' a
runHandle# (eff (Eff effs) a -> Handle eff effs a r effs a
forall (effs' :: [Effect]) b.
(eff :< effs') =>
eff (Eff effs') b -> Handle eff effs a r effs' b
f eff (Eff effs) a
e) Registers#
rs
{-# INLINE handle #-}
handleVM
:: forall eff a r effs
. (a -> Eff effs r)
-> (Registers# -> Handler eff)
-> Eff (eff ': effs) a
-> Eff effs r
handleVM :: (a -> Eff effs r)
-> (Registers# -> Handler eff) -> Eff (eff : effs) a -> Eff effs r
handleVM a -> Eff effs r
onReturn Registers# -> Handler eff
f (Eff Registers -> IO (Registers, a)
m1) = EVM r -> Eff effs r
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# ((Registers -> IO (Registers, a)) -> EVM r
withHandler Registers -> IO (Registers, a)
m1)
where
withHandler :: (Registers -> IO (Registers, a)) -> EVM r
withHandler :: (Registers -> IO (Registers, a)) -> EVM r
withHandler Registers -> IO (Registers, a)
g = (Registers# -> IO (Result a)) -> EVM r
withHandler# (EVM a -> Registers# -> IO (Result a)
forall a. EVM a -> Registers# -> IO (Result a)
unEVM# ((Registers -> IO (Registers, a)) -> EVM a
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM Registers -> IO (Registers, a)
g))
{-# INLINE withHandler #-}
withHandler# :: (Registers# -> IO (Result a)) -> EVM r
withHandler# :: (Registers# -> IO (Result a)) -> EVM r
withHandler# Registers# -> IO (Result a)
m2 = (Registers -> IO (Registers, r)) -> EVM r
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM \Registers
rs -> do
EVM a
-> Registers
-> (Capture a -> IO (Registers, r))
-> IO (Registers, r)
resetPrompt ((Registers# -> IO (Result a)) -> EVM a
forall a. (Registers# -> IO (Result a)) -> EVM a
EVM# Registers# -> IO (Result a)
m2) Registers
rs \(Capture PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
g b -> EVM a
k1) ->
((r -> EVM Any) -> IO (Registers, Any)) -> IO (Registers, r)
forall a b.
((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM \r -> EVM Any
k2 -> Capture Any -> IO (Registers, Any)
forall a b. Capture a -> IO b
captureVM (Capture Any -> IO (Registers, Any))
-> Capture Any -> IO (Registers, Any)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> (r -> EVM Any)
-> Capture Any
forall b d e c.
PromptId
-> CaptureMode
-> ((b -> EVM d) -> EVM e)
-> (b -> EVM a)
-> (r -> EVM c)
-> Capture c
handleCaptureElsewhere PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
g b -> EVM a
k1 r -> EVM Any
k2
pushPrompt :: Registers -> Registers
pushPrompt (Registers PromptId
pid1 Targets
ts1) =
let pid2 :: PromptId
pid2 = Int -> PromptId
PromptId (PromptId -> Int
unPromptId PromptId
pid1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
ts2 :: Targets
ts2 = Handler eff -> Targets -> Targets
forall (eff :: Effect). Handler eff -> Targets -> Targets
pushTarget (Registers# -> Handler eff
f (Registers -> Registers#
unboxRegisters Registers
rs2)) Targets
ts1
rs2 :: Registers
rs2 = PromptId -> Targets -> Registers
Registers PromptId
pid2 Targets
ts2
in Registers
rs2
resetPrompt
:: EVM a
-> Registers
-> (Capture a -> IO (Registers, r))
-> IO (Registers, r)
resetPrompt :: EVM a
-> Registers
-> (Capture a -> IO (Registers, r))
-> IO (Registers, r)
resetPrompt EVM a
m Registers
rs1 Capture a -> IO (Registers, r)
onCaptureElsewhere =
IO (Registers, a)
-> (a -> IO (Registers, r))
-> (PromptId -> Any -> IO (Registers, r))
-> (Capture a -> IO (Registers, r))
-> IO (Registers, r)
forall a b.
IO (Registers, a)
-> (a -> IO b)
-> (PromptId -> Any -> IO b)
-> (Capture a -> IO b)
-> IO b
promptVM (EVM a -> Registers -> IO (Registers, a)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM EVM a
m Registers
rs2) a -> IO (Registers, r)
handleReturn PromptId -> Any -> IO (Registers, r)
handleAbort Capture a -> IO (Registers, r)
handleCapture
where
!rs2 :: Registers
rs2@(Registers PromptId
pid Targets
_) = Registers -> Registers
pushPrompt Registers
rs1
handleReturn :: a -> IO (Registers, r)
handleReturn a
a = Eff effs r -> Registers -> IO (Registers, r)
forall a (effs :: [Effect]).
Eff effs a -> Registers -> IO (Registers, a)
unEff (a -> Eff effs r
onReturn a
a) Registers
rs1
handleAbort :: PromptId -> Any -> IO (Registers, r)
handleAbort PromptId
target Any
a
| PromptId -> Int
unPromptId PromptId
target Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== PromptId -> Int
unPromptId PromptId
pid = case Any
a of { Any b -> (Registers, r) -> IO (Registers, r)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Registers
rs1, r
b) }
| Bool
otherwise = Unwind -> IO (Registers, r)
forall e a. Exception e => e -> IO a
IO.throwIO (Unwind -> IO (Registers, r)) -> Unwind -> IO (Registers, r)
forall a b. (a -> b) -> a -> b
$! PromptId -> Any -> Unwind
UnwindAbort PromptId
target Any
a
handleCapture :: Capture a -> IO (Registers, r)
handleCapture = \case
Capture PromptId
target CaptureMode
mode ((b -> EVM c) -> EVM d
g :: (b -> EVM c) -> EVM d) b -> EVM a
k1
| PromptId -> Int
unPromptId PromptId
target Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== PromptId -> Int
unPromptId PromptId
pid ->
(r :~: d) -> ((r ~ d) => IO (Registers, r)) -> IO (Registers, r)
forall {k} (a :: k) (b :: k) r. (a :~: b) -> ((a ~ b) => r) -> r
gcastWith (r :~: d
forall {k} (a :: k) (b :: k). a :~: b
axiom @r @d) case CaptureMode
mode of
CaptureMode
IncludePrompt -> (r :~: c) -> ((r ~ c) => IO (Registers, r)) -> IO (Registers, r)
forall {k} (a :: k) (b :: k) r. (a :~: b) -> ((a ~ b) => r) -> r
gcastWith (r :~: c
forall {k} (a :: k) (b :: k). a :~: b
axiom @r @c) (((r ~ c) => IO (Registers, r)) -> IO (Registers, r))
-> ((r ~ c) => IO (Registers, r)) -> IO (Registers, r)
forall a b. (a -> b) -> a -> b
$ EVM d -> Registers -> IO (Registers, d)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM ((b -> EVM c) -> EVM d
g ((Registers -> IO (Registers, a)) -> EVM r
withHandler ((Registers -> IO (Registers, a)) -> EVM r)
-> (b -> Registers -> IO (Registers, a)) -> b -> EVM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EVM a -> Registers -> IO (Registers, a)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (EVM a -> Registers -> IO (Registers, a))
-> (b -> EVM a) -> b -> Registers -> IO (Registers, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> EVM a
k1)) Registers
rs1
CaptureMode
ExcludePrompt -> (a :~: c) -> ((a ~ c) => IO (Registers, r)) -> IO (Registers, r)
forall {k} (a :: k) (b :: k) r. (a :~: b) -> ((a ~ b) => r) -> r
gcastWith (a :~: c
forall {k} (a :: k) (b :: k). a :~: b
axiom @a @c) (((a ~ c) => IO (Registers, r)) -> IO (Registers, r))
-> ((a ~ c) => IO (Registers, r)) -> IO (Registers, r)
forall a b. (a -> b) -> a -> b
$ EVM d -> Registers -> IO (Registers, d)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM ((b -> EVM c) -> EVM d
g b -> EVM a
b -> EVM c
k1) Registers
rs1
Capture a
cap -> Capture a -> IO (Registers, r)
onCaptureElsewhere Capture a
cap
handleCaptureElsewhere
:: PromptId
-> CaptureMode
-> ((b -> EVM d) -> EVM e)
-> (b -> EVM a)
-> (r -> EVM c)
-> Capture c
handleCaptureElsewhere :: PromptId
-> CaptureMode
-> ((b -> EVM d) -> EVM e)
-> (b -> EVM a)
-> (r -> EVM c)
-> Capture c
handleCaptureElsewhere PromptId
target1 CaptureMode
mode1 (b -> EVM d) -> EVM e
f1 b -> EVM a
k1 r -> EVM c
k2 =
let k3 :: b -> EVM c
k3 b
a = (Registers -> IO (Registers, c)) -> EVM c
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM \Registers
rs1 -> do
(Registers
rs2, r
b) <- EVM a
-> Registers
-> (Capture a -> IO (Registers, r))
-> IO (Registers, r)
resetPrompt (b -> EVM a
k1 b
a) Registers
rs1 \(Capture PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
g b -> EVM a
k4) ->
Capture c -> IO (Registers, r)
forall a b. Capture a -> IO b
captureVM (Capture c -> IO (Registers, r)) -> Capture c -> IO (Registers, r)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> (r -> EVM c)
-> Capture c
forall b d e c.
PromptId
-> CaptureMode
-> ((b -> EVM d) -> EVM e)
-> (b -> EVM a)
-> (r -> EVM c)
-> Capture c
handleCaptureElsewhere PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
g b -> EVM a
k4 r -> EVM c
k2
EVM c -> Registers -> IO (Registers, c)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (r -> EVM c
k2 r
b) Registers
rs2
in PromptId
-> CaptureMode
-> ((b -> EVM d) -> EVM e)
-> (b -> EVM c)
-> Capture c
forall b c d a.
PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
Capture PromptId
target1 CaptureMode
mode1 (b -> EVM d) -> EVM e
f1 b -> EVM c
k3
locally :: Eff effs' a -> Handle eff effs i r effs' a
locally :: Eff effs' a -> Handle eff effs i r effs' a
locally Eff effs' a
m = (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
(Registers -> Eff effs' a) -> Handle eff effs i r effs' a
Handle \Registers
_ -> Eff effs' a
m
liftH :: Eff (eff ': effs) a -> Handle eff effs i r effs' a
liftH :: Eff (eff : effs) a -> Handle eff effs i r effs' a
liftH (Eff# EVM a
m) = (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
(Registers -> Eff effs' a) -> Handle eff effs i r effs' a
Handle \(Registers PromptId
_ Targets
ts) ->
EVM a -> Eff effs' a
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# ((Registers -> Registers) -> EVM a -> EVM a
forall a. (Registers -> Registers) -> EVM a -> EVM a
parameterizeVM (\(Registers PromptId
pid Targets
_) -> PromptId -> Targets -> Registers
Registers PromptId
pid Targets
ts) EVM a
m)
abort :: r -> Handle eff effs i r effs' a
abort :: r -> Handle eff effs i r effs' a
abort r
a = (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
(Registers -> Eff effs' a) -> Handle eff effs i r effs' a
Handle \(Registers PromptId
pid Targets
_) -> (Registers -> IO (Registers, a)) -> Eff effs' a
forall a (effs :: [Effect]).
(Registers -> IO (Registers, a)) -> Eff effs a
Eff \Registers
_ -> Unwind -> IO (Registers, a)
forall e a. Exception e => e -> IO a
IO.throwIO (Unwind -> IO (Registers, a)) -> Unwind -> IO (Registers, a)
forall a b. (a -> b) -> a -> b
$! PromptId -> Any -> Unwind
UnwindAbort PromptId
pid (r -> Any
forall a. a -> Any
Any r
a)
control :: ((a -> Eff effs r) -> Eff effs r) -> Handle eff effs i r effs' a
control :: ((a -> Eff effs r) -> Eff effs r) -> Handle eff effs i r effs' a
control (a -> Eff effs r) -> Eff effs r
f = (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
(Registers -> Eff effs' a) -> Handle eff effs i r effs' a
Handle \(Registers PromptId
pid Targets
_) -> (Registers -> IO (Registers, a)) -> Eff effs' a
forall a (effs :: [Effect]).
(Registers -> IO (Registers, a)) -> Eff effs a
Eff \Registers
_ ->
((a -> EVM Any) -> IO (Registers, Any)) -> IO (Registers, a)
forall a b.
((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM \a -> EVM Any
k1 -> Capture Any -> IO (Registers, Any)
forall a b. Capture a -> IO b
captureVM (Capture Any -> IO (Registers, Any))
-> Capture Any -> IO (Registers, Any)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((a -> EVM r) -> EVM r)
-> (a -> EVM Any)
-> Capture Any
forall b c d a.
PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
Capture PromptId
pid CaptureMode
IncludePrompt (\a -> EVM r
k2 -> Eff effs r -> EVM r
forall (effs :: [Effect]) a. Eff effs a -> EVM a
unEff# ((a -> Eff effs r) -> Eff effs r
f (EVM r -> Eff effs r
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM r -> Eff effs r) -> (a -> EVM r) -> a -> Eff effs r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> EVM r
k2))) a -> EVM Any
k1
control0 :: ((a -> Eff (eff ': effs) i) -> Eff effs r) -> Handle eff effs i r effs' a
control0 :: ((a -> Eff (eff : effs) i) -> Eff effs r)
-> Handle eff effs i r effs' a
control0 (a -> Eff (eff : effs) i) -> Eff effs r
f = (Registers -> Eff effs' a) -> Handle eff effs i r effs' a
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
(Registers -> Eff effs' a) -> Handle eff effs i r effs' a
Handle \(Registers PromptId
pid Targets
_) -> (Registers -> IO (Registers, a)) -> Eff effs' a
forall a (effs :: [Effect]).
(Registers -> IO (Registers, a)) -> Eff effs a
Eff \Registers
_ ->
((a -> EVM Any) -> IO (Registers, Any)) -> IO (Registers, a)
forall a b.
((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM \a -> EVM Any
k1 -> Capture Any -> IO (Registers, Any)
forall a b. Capture a -> IO b
captureVM (Capture Any -> IO (Registers, Any))
-> Capture Any -> IO (Registers, Any)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((a -> EVM i) -> EVM r)
-> (a -> EVM Any)
-> Capture Any
forall b c d a.
PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
Capture PromptId
pid CaptureMode
ExcludePrompt (\a -> EVM i
k2 -> Eff effs r -> EVM r
forall (effs :: [Effect]) a. Eff effs a -> EVM a
unEff# ((a -> Eff (eff : effs) i) -> Eff effs r
f (EVM i -> Eff (eff : effs) i
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM i -> Eff (eff : effs) i)
-> (a -> EVM i) -> a -> Eff (eff : effs) i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> EVM i
k2))) a -> EVM Any
k1
type Lift :: [Effect] -> [Effect] -> Constraint
class Lift effs1 effs2 where
liftTargets :: Targets -> Targets
instance {-# INCOHERENT #-} effs1 :<< effs2 => Lift effs1 effs2 where
liftTargets :: Targets -> Targets
liftTargets = Int -> Targets -> Targets
DebugCallStack => Int -> Targets -> Targets
dropTargets ((effs1 :<< effs2) => Int
forall (effs1 :: [Effect]) (effs2 :: [Effect]).
(effs1 :<< effs2) =>
Int
reifySubIndex @effs1 @effs2)
{-# INLINE liftTargets #-}
instance Lift '[] effs where
liftTargets :: Targets -> Targets
liftTargets Targets
_ = Targets
noTargets
{-# INLINE liftTargets #-}
instance (eff :< effs2, Lift effs1 effs2) => Lift (eff ': effs1) effs2 where
liftTargets :: Targets -> Targets
liftTargets Targets
ts = Handler eff -> Targets -> Targets
forall (eff :: Effect). Handler eff -> Targets -> Targets
pushTarget (Targets -> Handler eff
forall (effs :: [Effect]) (eff :: Effect).
(DebugCallStack, eff :< effs) =>
Targets -> Handler eff
lookupTarget @effs2 @eff Targets
ts) (Targets -> Targets) -> Targets -> Targets
forall a b. (a -> b) -> a -> b
$! Targets -> Targets
forall (effs1 :: [Effect]) (effs2 :: [Effect]).
Lift effs1 effs2 =>
Targets -> Targets
liftTargets @effs1 @effs2 Targets
ts
lift :: forall effs1 effs2 a. Lift effs1 effs2 => Eff effs1 a -> Eff effs2 a
lift :: Eff effs1 a -> Eff effs2 a
lift = EVM a -> Eff effs2 a
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM a -> Eff effs2 a)
-> (Eff effs1 a -> EVM a) -> Eff effs1 a -> Eff effs2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Registers -> Registers) -> EVM a -> EVM a
forall a. (Registers -> Registers) -> EVM a -> EVM a
parameterizeVM Registers -> Registers
liftRegisters (EVM a -> EVM a) -> (Eff effs1 a -> EVM a) -> Eff effs1 a -> EVM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Eff effs1 a -> EVM a
forall (effs :: [Effect]) a. Eff effs a -> EVM a
unEff# where
liftRegisters :: Registers -> Registers
liftRegisters (Registers PromptId
pid Targets
ts) = PromptId -> Targets -> Registers
Registers PromptId
pid (Targets -> Targets
forall (effs1 :: [Effect]) (effs2 :: [Effect]).
Lift effs1 effs2 =>
Targets -> Targets
liftTargets @effs1 @effs2 Targets
ts)
lift1 :: forall eff effs a. Eff effs a -> Eff (eff ': effs) a
lift1 :: Eff effs a -> Eff (eff : effs) a
lift1 = Eff effs a -> Eff (eff : effs) a
forall (effs1 :: [Effect]) (effs2 :: [Effect]) a.
Lift effs1 effs2 =>
Eff effs1 a -> Eff effs2 a
lift
{-# INLINE lift1 #-}
data IOE :: Effect where
LiftIO :: IO a -> IOE m a
unsafeIOToEff :: IO a -> Eff effs a
unsafeIOToEff :: IO a -> Eff effs a
unsafeIOToEff = EVM a -> Eff effs a
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM a -> Eff effs a) -> (IO a -> EVM a) -> IO a -> Eff effs a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> EVM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE unsafeIOToEff #-}
runIO :: Eff '[IOE] a -> IO a
runIO :: Eff '[IOE] a -> IO a
runIO Eff '[IOE] a
m0 = (Registers, a) -> a
forall a b. (a, b) -> b
snd ((Registers, a) -> a) -> IO (Registers, a) -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff '[] a -> Registers -> IO (Registers, a)
forall a (effs :: [Effect]).
Eff effs a -> Registers -> IO (Registers, a)
unEff (Eff '[IOE] a -> Eff '[] a
forall {effs :: [Effect]} {r}. Eff (IOE : effs) r -> Eff effs r
handleIO Eff '[IOE] a
m0) Registers
initialRegisters where
handleIO :: Eff (IOE : effs) r -> Eff effs r
handleIO = (r -> Eff effs r)
-> (forall (effs' :: [Effect]) b.
(IOE :< effs') =>
IOE (Eff effs') b -> Handle IOE effs r r effs' b)
-> Eff (IOE : effs) r
-> Eff effs r
forall (eff :: Effect) a r (effs :: [Effect]).
(a -> Eff effs r)
-> (forall (effs' :: [Effect]) b.
(eff :< effs') =>
eff (Eff effs') b -> Handle eff effs a r effs' b)
-> Eff (eff : effs) a
-> Eff effs r
handle r -> Eff effs r
forall (f :: * -> *) a. Applicative f => a -> f a
pure \case
LiftIO m -> Eff effs' b -> Handle IOE effs r r effs' b
forall (effs' :: [Effect]) a (eff :: Effect) (effs :: [Effect]) i
r.
Eff effs' a -> Handle eff effs i r effs' a
locally (IO b -> Eff effs' b
forall a (effs :: [Effect]). IO a -> Eff effs a
unsafeIOToEff IO b
m)
instance IOE :< effs => MonadIO (Eff effs) where
liftIO :: IO a -> Eff effs a
liftIO = IOE (Eff effs) a -> Eff effs a
forall (eff :: Effect) a (effs :: [Effect]).
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
send (IOE (Eff effs) a -> Eff effs a)
-> (IO a -> IOE (Eff effs) a) -> IO a -> Eff effs a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> IOE (Eff effs) a
forall a (m :: * -> *). IO a -> IOE m a
LiftIO
{-# INLINE liftIO #-}
data State s :: Effect where
Get :: State s m s
Put :: ~s -> State s m ()
evalState :: s -> Eff (State s ': effs) a -> Eff effs a
evalState :: s -> Eff (State s : effs) a -> Eff effs a
evalState (s
s0 :: s) (Eff Registers -> IO (Registers, a)
m0) = (Registers -> IO (Registers, a)) -> Eff effs a
forall a (effs :: [Effect]).
(Registers -> IO (Registers, a)) -> Eff effs a
Eff \Registers
rs -> do
IORef s
ref <- s -> IO (IORef s)
forall a. a -> IO (IORef a)
newIORef s
s0
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
forall a.
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ (Registers -> IO (Registers, a)
m0 (IORef s -> Registers -> Registers
pushHandler IORef s
ref Registers
rs)) Registers
rs \(Capture PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
f b -> EVM a
k1) ->
((a -> EVM a) -> IO (Registers, a)) -> IO (Registers, a)
forall a b.
((a -> EVM b) -> IO (Registers, b)) -> IO (Registers, a)
controlVM \a -> EVM a
k2 -> IORef s
-> PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> (a -> EVM a)
-> IO (Registers, a)
forall a d e b c.
IORef s
-> PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> IO (Registers, b)
handleCapture IORef s
ref PromptId
target CaptureMode
mode (b -> EVM c) -> EVM d
f b -> EVM a
k1 a -> EVM a
k2
where
pushHandler :: IORef s -> Registers -> Registers
pushHandler :: IORef s -> Registers -> Registers
pushHandler IORef s
ref (Registers PromptId
pid Targets
ts) =
let h :: Handler (State s)
h :: Handler (State s)
h = (forall (effs :: [Effect]) a.
(State s :< effs) =>
State s (Eff effs) a -> Eff effs a)
-> Handler (State s)
forall (eff :: Effect).
(forall (effs :: [Effect]) a.
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a)
-> Handler eff
Handler \case
State s (Eff effs) a
Get -> EVM s -> Eff effs s
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM s -> Eff effs s) -> EVM s -> Eff effs s
forall a b. (a -> b) -> a -> b
$ IO s -> EVM s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO s -> EVM s) -> IO s -> EVM s
forall a b. (a -> b) -> a -> b
$ IORef s -> IO s
forall a. IORef a -> IO a
readIORef IORef s
ref
Put !s -> EVM () -> Eff effs ()
forall (effs :: [Effect]) a. EVM a -> Eff effs a
Eff# (EVM () -> Eff effs ()) -> EVM () -> Eff effs ()
forall a b. (a -> b) -> a -> b
$ IO () -> EVM ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> EVM ()) -> IO () -> EVM ()
forall a b. (a -> b) -> a -> b
$ IORef s -> s -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef s
ref s
s
in PromptId -> Targets -> Registers
Registers PromptId
pid (Handler (State s) -> Targets -> Targets
forall (eff :: Effect). Handler eff -> Targets -> Targets
pushTarget Handler (State s)
h Targets
ts)
handleCapture
:: IORef s
-> PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> IO (Registers, b)
handleCapture :: IORef s
-> PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> IO (Registers, b)
handleCapture IORef s
ref1 PromptId
target1 CaptureMode
mode1 (a -> EVM d) -> EVM e
f1 a -> EVM b
k1 b -> EVM c
k2 = do
s
s <- IORef s -> IO s
forall a. IORef a -> IO a
readIORef IORef s
ref1
let k3 :: a -> EVM c
k3 a
a = (Registers -> IO (Registers, c)) -> EVM c
forall a. (Registers -> IO (Registers, a)) -> EVM a
EVM \Registers
rs1 -> do
IORef s
ref2 <- s -> IO (IORef s)
forall a. a -> IO (IORef a)
newIORef s
s
let m :: IO (Registers, b)
m = EVM b -> Registers -> IO (Registers, b)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (a -> EVM b
k1 a
a) (IORef s -> Registers -> Registers
pushHandler IORef s
ref2 Registers
rs1)
(Registers
rs2, b
b) <- IO (Registers, b)
-> Registers
-> (Capture b -> IO (Registers, b))
-> IO (Registers, b)
forall a.
IO (Registers, a)
-> Registers
-> (Capture a -> IO (Registers, a))
-> IO (Registers, a)
promptVM_ IO (Registers, b)
m Registers
rs1 \(Capture PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
f2 b -> EVM b
k4) ->
IORef s
-> PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM b)
-> (b -> EVM c)
-> IO (Registers, b)
forall a d e b c.
IORef s
-> PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM b)
-> (b -> EVM c)
-> IO (Registers, b)
handleCapture IORef s
ref2 PromptId
target2 CaptureMode
mode2 (b -> EVM c) -> EVM d
f2 b -> EVM b
k4 b -> EVM c
k2
EVM c -> Registers -> IO (Registers, c)
forall a. EVM a -> Registers -> IO (Registers, a)
unEVM (b -> EVM c
k2 b
b) Registers
rs2
Capture c -> IO (Registers, b)
forall a b. Capture a -> IO b
captureVM (Capture c -> IO (Registers, b)) -> Capture c -> IO (Registers, b)
forall a b. (a -> b) -> a -> b
$! PromptId
-> CaptureMode
-> ((a -> EVM d) -> EVM e)
-> (a -> EVM c)
-> Capture c
forall b c d a.
PromptId
-> CaptureMode
-> ((b -> EVM c) -> EVM d)
-> (b -> EVM a)
-> Capture a
Capture PromptId
target1 CaptureMode
mode1 (a -> EVM d) -> EVM e
f1 a -> EVM c
k3
data NonDet :: Effect where
Empty :: NonDet m a
Choose :: NonDet m Bool
instance NonDet :< effs => Alternative (Eff effs) where
empty :: Eff effs a
empty = NonDet (Eff effs) a -> Eff effs a
forall (eff :: Effect) a (effs :: [Effect]).
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
send NonDet (Eff effs) a
forall (m :: * -> *) a. NonDet m a
Empty
{-# INLINE empty #-}
Eff effs a
a <|> :: Eff effs a -> Eff effs a -> Eff effs a
<|> Eff effs a
b = NonDet (Eff effs) Bool -> Eff effs Bool
forall (eff :: Effect) a (effs :: [Effect]).
(eff :< effs) =>
eff (Eff effs) a -> Eff effs a
send NonDet (Eff effs) Bool
forall (m :: * -> *). NonDet m Bool
Choose Eff effs Bool -> (Bool -> Eff effs a) -> Eff effs a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Eff effs a -> Eff effs a -> Bool -> Eff effs a
forall a. a -> a -> Bool -> a
bool Eff effs a
b Eff effs a
a
{-# INLINE (<|>) #-}