{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}

{-# OPTIONS_HADDOCK hide #-}

module Capability.Source.Internal.Strategies
  ( MonadReader(..)
  , ReadStatePure(..)
  , ReadState(..)
  , MonadState(..)
  , ReaderIORef(..)
  , ReaderRef(..)
  ) where

import Capability.Source.Internal.Class
import Capability.State.Internal.Class
import Capability.State.Internal.Strategies.Common
import Capability.Accessors
import Control.Lens (view)
import Control.Monad.Catch (MonadMask)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Control.Monad.Reader.Class as Reader
import qualified Control.Monad.State.Class as State
import Control.Monad.Trans.Class (MonadTrans, lift)
import Data.Coerce (Coercible, coerce)
import Data.IORef
import Data.Kind (Type)
import Data.Mutable
import qualified Data.Generics.Product.Fields as Generic
import qualified Data.Generics.Product.Positions as Generic

--------------------------------------------------------------------------------

-- | Derive 'HasSource' from @m@'s 'Control.Monad.Reader.Class.MonadReader'
-- instance.
newtype MonadReader (m :: Type -> Type) (a :: Type) = MonadReader (m a)
  deriving (a -> MonadReader m b -> MonadReader m a
(a -> b) -> MonadReader m a -> MonadReader m b
(forall a b. (a -> b) -> MonadReader m a -> MonadReader m b)
-> (forall a b. a -> MonadReader m b -> MonadReader m a)
-> Functor (MonadReader m)
forall a b. a -> MonadReader m b -> MonadReader m a
forall a b. (a -> b) -> MonadReader m a -> MonadReader m b
forall (m :: * -> *) a b.
Functor m =>
a -> MonadReader m b -> MonadReader m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> MonadReader m a -> MonadReader m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> MonadReader m b -> MonadReader m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> MonadReader m b -> MonadReader m a
fmap :: (a -> b) -> MonadReader m a -> MonadReader m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> MonadReader m a -> MonadReader m b
Functor, Functor (MonadReader m)
a -> MonadReader m a
Functor (MonadReader m)
-> (forall a. a -> MonadReader m a)
-> (forall a b.
    MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b)
-> (forall a b c.
    (a -> b -> c)
    -> MonadReader m a -> MonadReader m b -> MonadReader m c)
-> (forall a b.
    MonadReader m a -> MonadReader m b -> MonadReader m b)
-> (forall a b.
    MonadReader m a -> MonadReader m b -> MonadReader m a)
-> Applicative (MonadReader m)
MonadReader m a -> MonadReader m b -> MonadReader m b
MonadReader m a -> MonadReader m b -> MonadReader m a
MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b
(a -> b -> c)
-> MonadReader m a -> MonadReader m b -> MonadReader m c
forall a. a -> MonadReader m a
forall a b. MonadReader m a -> MonadReader m b -> MonadReader m a
forall a b. MonadReader m a -> MonadReader m b -> MonadReader m b
forall a b.
MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b
forall a b c.
(a -> b -> c)
-> MonadReader m a -> MonadReader m b -> MonadReader m 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
forall (m :: * -> *). Applicative m => Functor (MonadReader m)
forall (m :: * -> *) a. Applicative m => a -> MonadReader m a
forall (m :: * -> *) a b.
Applicative m =>
MonadReader m a -> MonadReader m b -> MonadReader m a
forall (m :: * -> *) a b.
Applicative m =>
MonadReader m a -> MonadReader m b -> MonadReader m b
forall (m :: * -> *) a b.
Applicative m =>
MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> MonadReader m a -> MonadReader m b -> MonadReader m c
<* :: MonadReader m a -> MonadReader m b -> MonadReader m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
MonadReader m a -> MonadReader m b -> MonadReader m a
*> :: MonadReader m a -> MonadReader m b -> MonadReader m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
MonadReader m a -> MonadReader m b -> MonadReader m b
liftA2 :: (a -> b -> c)
-> MonadReader m a -> MonadReader m b -> MonadReader m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> MonadReader m a -> MonadReader m b -> MonadReader m c
<*> :: MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
MonadReader m (a -> b) -> MonadReader m a -> MonadReader m b
pure :: a -> MonadReader m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> MonadReader m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (MonadReader m)
Applicative, Applicative (MonadReader m)
a -> MonadReader m a
Applicative (MonadReader m)
-> (forall a b.
    MonadReader m a -> (a -> MonadReader m b) -> MonadReader m b)
-> (forall a b.
    MonadReader m a -> MonadReader m b -> MonadReader m b)
-> (forall a. a -> MonadReader m a)
-> Monad (MonadReader m)
MonadReader m a -> (a -> MonadReader m b) -> MonadReader m b
MonadReader m a -> MonadReader m b -> MonadReader m b
forall a. a -> MonadReader m a
forall a b. MonadReader m a -> MonadReader m b -> MonadReader m b
forall a b.
MonadReader m a -> (a -> MonadReader m b) -> MonadReader m b
forall (m :: * -> *). Monad m => Applicative (MonadReader m)
forall (m :: * -> *) a. Monad m => a -> MonadReader m a
forall (m :: * -> *) a b.
Monad m =>
MonadReader m a -> MonadReader m b -> MonadReader m b
forall (m :: * -> *) a b.
Monad m =>
MonadReader m a -> (a -> MonadReader m b) -> MonadReader m 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 -> MonadReader m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> MonadReader m a
>> :: MonadReader m a -> MonadReader m b -> MonadReader m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
MonadReader m a -> MonadReader m b -> MonadReader m b
>>= :: MonadReader m a -> (a -> MonadReader m b) -> MonadReader m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
MonadReader m a -> (a -> MonadReader m b) -> MonadReader m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (MonadReader m)
Monad, Monad (MonadReader m)
Monad (MonadReader m)
-> (forall a. IO a -> MonadReader m a) -> MonadIO (MonadReader m)
IO a -> MonadReader m a
forall a. IO a -> MonadReader m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (MonadReader m)
forall (m :: * -> *) a. MonadIO m => IO a -> MonadReader m a
liftIO :: IO a -> MonadReader m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> MonadReader m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (MonadReader m)
MonadIO, Monad (MonadReader m)
Monad (MonadReader m)
-> (forall a.
    (State# (PrimState (MonadReader m))
     -> (# State# (PrimState (MonadReader m)), a #))
    -> MonadReader m a)
-> PrimMonad (MonadReader m)
(State# (PrimState (MonadReader m))
 -> (# State# (PrimState (MonadReader m)), a #))
-> MonadReader m a
forall a.
(State# (PrimState (MonadReader m))
 -> (# State# (PrimState (MonadReader m)), a #))
-> MonadReader m a
forall (m :: * -> *).
Monad m
-> (forall a.
    (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> PrimMonad m
forall (m :: * -> *). PrimMonad m => Monad (MonadReader m)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState (MonadReader m))
 -> (# State# (PrimState (MonadReader m)), a #))
-> MonadReader m a
primitive :: (State# (PrimState (MonadReader m))
 -> (# State# (PrimState (MonadReader m)), a #))
-> MonadReader m a
$cprimitive :: forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState (MonadReader m))
 -> (# State# (PrimState (MonadReader m)), a #))
-> MonadReader m a
$cp1PrimMonad :: forall (m :: * -> *). PrimMonad m => Monad (MonadReader m)
PrimMonad)

instance Reader.MonadReader r m => HasSource tag r (MonadReader m) where
  await_ :: Proxy# tag -> MonadReader m r
await_ Proxy# tag
_ = m r -> MonadReader m r
coerce @(m r) m r
forall r (m :: * -> *). MonadReader r m => m r
Reader.ask
  {-# INLINE await_ #-}

-- | Convert a /pure/ state monad into a reader monad.
--
-- /Pure/ meaning that the monad stack does not allow catching exceptions.
-- Otherwise, an exception occurring in the action passed to 'local' could cause
-- the context to remain modified outside of the call to 'local'. E.g.
--
-- > local @tag (const r') (throw MyException)
-- > `catch` \MyException -> ask @tag
--
-- returns @r'@ instead of the previous value.
--
-- Note, that no @MonadIO@ instance is provided, as this would allow catching
-- exceptions.
--
-- See 'ReadState'.
newtype ReadStatePure (m :: Type -> Type) (a :: Type) = ReadStatePure (m a)
  deriving (a -> ReadStatePure m b -> ReadStatePure m a
(a -> b) -> ReadStatePure m a -> ReadStatePure m b
(forall a b. (a -> b) -> ReadStatePure m a -> ReadStatePure m b)
-> (forall a b. a -> ReadStatePure m b -> ReadStatePure m a)
-> Functor (ReadStatePure m)
forall a b. a -> ReadStatePure m b -> ReadStatePure m a
forall a b. (a -> b) -> ReadStatePure m a -> ReadStatePure m b
forall (m :: * -> *) a b.
Functor m =>
a -> ReadStatePure m b -> ReadStatePure m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> ReadStatePure m a -> ReadStatePure m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> ReadStatePure m b -> ReadStatePure m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> ReadStatePure m b -> ReadStatePure m a
fmap :: (a -> b) -> ReadStatePure m a -> ReadStatePure m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> ReadStatePure m a -> ReadStatePure m b
Functor, Functor (ReadStatePure m)
a -> ReadStatePure m a
Functor (ReadStatePure m)
-> (forall a. a -> ReadStatePure m a)
-> (forall a b.
    ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b)
-> (forall a b c.
    (a -> b -> c)
    -> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c)
-> (forall a b.
    ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b)
-> (forall a b.
    ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a)
-> Applicative (ReadStatePure m)
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a
ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b
(a -> b -> c)
-> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c
forall a. a -> ReadStatePure m a
forall a b.
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a
forall a b.
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
forall a b.
ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b
forall a b c.
(a -> b -> c)
-> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m 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
forall (m :: * -> *). Applicative m => Functor (ReadStatePure m)
forall (m :: * -> *) a. Applicative m => a -> ReadStatePure m a
forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a
forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c
<* :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a
*> :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
liftA2 :: (a -> b -> c)
-> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c
<*> :: ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b
pure :: a -> ReadStatePure m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> ReadStatePure m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (ReadStatePure m)
Applicative, Applicative (ReadStatePure m)
a -> ReadStatePure m a
Applicative (ReadStatePure m)
-> (forall a b.
    ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b)
-> (forall a b.
    ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b)
-> (forall a. a -> ReadStatePure m a)
-> Monad (ReadStatePure m)
ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
forall a. a -> ReadStatePure m a
forall a b.
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
forall a b.
ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b
forall (m :: * -> *). Monad m => Applicative (ReadStatePure m)
forall (m :: * -> *) a. Monad m => a -> ReadStatePure m a
forall (m :: * -> *) a b.
Monad m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
forall (m :: * -> *) a b.
Monad m =>
ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m 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 -> ReadStatePure m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> ReadStatePure m a
>> :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b
>>= :: ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (ReadStatePure m)
Monad)

instance HasState tag r m => HasSource tag r (ReadStatePure m) where
  await_ :: Proxy# tag -> ReadStatePure m r
await_ Proxy# tag
_ = forall b. Coercible (m r) b => m r -> b
coerce @(m r) (m r -> ReadStatePure m r) -> m r -> ReadStatePure m r
forall a b. (a -> b) -> a -> b
$ forall k (tag :: k) s (m :: * -> *). HasState tag s m => m s
forall s (m :: * -> *). HasState tag s m => m s
get @tag
  {-# INLINE await_ #-}

-- | Convert a state monad into a reader monad.
--
-- Use this if the monad stack allows catching exceptions.
--
-- See 'ReadStatePure'.
newtype ReadState (m :: Type -> Type) (a :: Type) = ReadState (m a)
  deriving (a -> ReadState m b -> ReadState m a
(a -> b) -> ReadState m a -> ReadState m b
(forall a b. (a -> b) -> ReadState m a -> ReadState m b)
-> (forall a b. a -> ReadState m b -> ReadState m a)
-> Functor (ReadState m)
forall a b. a -> ReadState m b -> ReadState m a
forall a b. (a -> b) -> ReadState m a -> ReadState m b
forall (m :: * -> *) a b.
Functor m =>
a -> ReadState m b -> ReadState m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> ReadState m a -> ReadState m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> ReadState m b -> ReadState m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> ReadState m b -> ReadState m a
fmap :: (a -> b) -> ReadState m a -> ReadState m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> ReadState m a -> ReadState m b
Functor, Functor (ReadState m)
a -> ReadState m a
Functor (ReadState m)
-> (forall a. a -> ReadState m a)
-> (forall a b.
    ReadState m (a -> b) -> ReadState m a -> ReadState m b)
-> (forall a b c.
    (a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m c)
-> (forall a b. ReadState m a -> ReadState m b -> ReadState m b)
-> (forall a b. ReadState m a -> ReadState m b -> ReadState m a)
-> Applicative (ReadState m)
ReadState m a -> ReadState m b -> ReadState m b
ReadState m a -> ReadState m b -> ReadState m a
ReadState m (a -> b) -> ReadState m a -> ReadState m b
(a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m c
forall a. a -> ReadState m a
forall a b. ReadState m a -> ReadState m b -> ReadState m a
forall a b. ReadState m a -> ReadState m b -> ReadState m b
forall a b. ReadState m (a -> b) -> ReadState m a -> ReadState m b
forall a b c.
(a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m 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
forall (m :: * -> *). Applicative m => Functor (ReadState m)
forall (m :: * -> *) a. Applicative m => a -> ReadState m a
forall (m :: * -> *) a b.
Applicative m =>
ReadState m a -> ReadState m b -> ReadState m a
forall (m :: * -> *) a b.
Applicative m =>
ReadState m a -> ReadState m b -> ReadState m b
forall (m :: * -> *) a b.
Applicative m =>
ReadState m (a -> b) -> ReadState m a -> ReadState m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m c
<* :: ReadState m a -> ReadState m b -> ReadState m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
ReadState m a -> ReadState m b -> ReadState m a
*> :: ReadState m a -> ReadState m b -> ReadState m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
ReadState m a -> ReadState m b -> ReadState m b
liftA2 :: (a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> ReadState m a -> ReadState m b -> ReadState m c
<*> :: ReadState m (a -> b) -> ReadState m a -> ReadState m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
ReadState m (a -> b) -> ReadState m a -> ReadState m b
pure :: a -> ReadState m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> ReadState m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (ReadState m)
Applicative, Applicative (ReadState m)
a -> ReadState m a
Applicative (ReadState m)
-> (forall a b.
    ReadState m a -> (a -> ReadState m b) -> ReadState m b)
-> (forall a b. ReadState m a -> ReadState m b -> ReadState m b)
-> (forall a. a -> ReadState m a)
-> Monad (ReadState m)
ReadState m a -> (a -> ReadState m b) -> ReadState m b
ReadState m a -> ReadState m b -> ReadState m b
forall a. a -> ReadState m a
forall a b. ReadState m a -> ReadState m b -> ReadState m b
forall a b. ReadState m a -> (a -> ReadState m b) -> ReadState m b
forall (m :: * -> *). Monad m => Applicative (ReadState m)
forall (m :: * -> *) a. Monad m => a -> ReadState m a
forall (m :: * -> *) a b.
Monad m =>
ReadState m a -> ReadState m b -> ReadState m b
forall (m :: * -> *) a b.
Monad m =>
ReadState m a -> (a -> ReadState m b) -> ReadState m 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 -> ReadState m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> ReadState m a
>> :: ReadState m a -> ReadState m b -> ReadState m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
ReadState m a -> ReadState m b -> ReadState m b
>>= :: ReadState m a -> (a -> ReadState m b) -> ReadState m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
ReadState m a -> (a -> ReadState m b) -> ReadState m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (ReadState m)
Monad, Monad (ReadState m)
Monad (ReadState m)
-> (forall a. IO a -> ReadState m a) -> MonadIO (ReadState m)
IO a -> ReadState m a
forall a. IO a -> ReadState m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (ReadState m)
forall (m :: * -> *) a. MonadIO m => IO a -> ReadState m a
liftIO :: IO a -> ReadState m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> ReadState m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (ReadState m)
MonadIO, Monad (ReadState m)
Monad (ReadState m)
-> (forall a.
    (State# (PrimState (ReadState m))
     -> (# State# (PrimState (ReadState m)), a #))
    -> ReadState m a)
-> PrimMonad (ReadState m)
(State# (PrimState (ReadState m))
 -> (# State# (PrimState (ReadState m)), a #))
-> ReadState m a
forall a.
(State# (PrimState (ReadState m))
 -> (# State# (PrimState (ReadState m)), a #))
-> ReadState m a
forall (m :: * -> *).
Monad m
-> (forall a.
    (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> PrimMonad m
forall (m :: * -> *). PrimMonad m => Monad (ReadState m)
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState (ReadState m))
 -> (# State# (PrimState (ReadState m)), a #))
-> ReadState m a
primitive :: (State# (PrimState (ReadState m))
 -> (# State# (PrimState (ReadState m)), a #))
-> ReadState m a
$cprimitive :: forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState (ReadState m))
 -> (# State# (PrimState (ReadState m)), a #))
-> ReadState m a
$cp1PrimMonad :: forall (m :: * -> *). PrimMonad m => Monad (ReadState m)
PrimMonad)

instance
  (HasState tag r m, MonadMask m)
  => HasSource tag r (ReadState m)
  where
    await_ :: Proxy# tag -> ReadState m r
await_ Proxy# tag
_ = forall b. Coercible (m r) b => m r -> b
coerce @(m r) (m r -> ReadState m r) -> m r -> ReadState m r
forall a b. (a -> b) -> a -> b
$ forall k (tag :: k) s (m :: * -> *). HasState tag s m => m s
forall s (m :: * -> *). HasState tag s m => m s
get @tag
    {-# INLINE await_ #-}

instance
  ( tag ~ pos, Generic.HasPosition' pos struct v, HasSource oldtag struct m )
  => HasSource tag v (Pos pos oldtag m)
  where
    await_ :: Proxy# tag -> Pos pos oldtag m v
await_ Proxy# tag
_ = forall b. Coercible (m v) b => m v -> b
coerce @(m v) (m v -> Pos pos oldtag m v) -> m v -> Pos pos oldtag m v
forall a b. (a -> b) -> a -> b
$
      forall k (tag :: k) r (m :: * -> *) a.
HasSource tag r m =>
(r -> a) -> m a
forall r (m :: * -> *) a. HasSource oldtag r m => (r -> a) -> m a
awaits @oldtag ((struct -> v) -> m v) -> (struct -> v) -> m v
forall a b. (a -> b) -> a -> b
$ Getting v struct v -> struct -> v
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall s a. HasPosition' pos s a => Lens s s a a
forall (i :: Nat) s a. HasPosition' i s a => Lens s s a a
Generic.position' @pos)
    {-# INLINE await_ #-}

deriving via ((t2 :: (Type -> Type) -> Type -> Type) ((t1 :: (Type -> Type) -> Type -> Type) m))
  instance
  ( forall x. Coercible (m x) (t2 (t1 m) x)
  , Monad m, HasSource tag r (t2 (t1 m)) )
  => HasSource tag r ((t2 :.: t1) m)

instance
  ( Coercible from to, HasSource tag from m
  , forall x y. Coercible x y => Coercible (m x) (m y) )
  => HasSource tag to (Coerce to m)
  where
    await_ :: Proxy# tag -> Coerce to m to
await_ Proxy# tag
tag = forall b. Coercible (m from) b => m from -> b
coerce @(m from) (m from -> Coerce to m to) -> m from -> Coerce to m to
forall a b. (a -> b) -> a -> b
$ Proxy# tag -> m from
forall k (tag :: k) a (m :: * -> *).
HasSource tag a m =>
Proxy# tag -> m a
await_ Proxy# tag
tag
    {-# INLINE await_ #-}

-- | Rename the tag.
instance HasSource oldtag r m => HasSource newtag r (Rename oldtag m) where
  await_ :: Proxy# newtag -> Rename oldtag m r
await_ Proxy# newtag
_ = forall b. Coercible (m r) b => m r -> b
coerce @(m r) (m r -> Rename oldtag m r) -> m r -> Rename oldtag m r
forall a b. (a -> b) -> a -> b
$ forall k (tag :: k) a (m :: * -> *). HasSource tag a m => m a
forall a (m :: * -> *). HasSource oldtag a m => m a
await @oldtag
  {-# INLINE await_ #-}

instance
  ( tag ~ field, Generic.HasField' field record v, HasSource oldtag record m )
  => HasSource tag v (Field field oldtag m)
  where
    await_ :: Proxy# tag -> Field field oldtag m v
await_ Proxy# tag
_ = forall b. Coercible (m v) b => m v -> b
coerce @(m v) (m v -> Field field oldtag m v) -> m v -> Field field oldtag m v
forall a b. (a -> b) -> a -> b
$
      forall k (tag :: k) r (m :: * -> *) a.
HasSource tag r m =>
(r -> a) -> m a
forall r (m :: * -> *) a. HasSource oldtag r m => (r -> a) -> m a
awaits @oldtag ((record -> v) -> m v) -> (record -> v) -> m v
forall a b. (a -> b) -> a -> b
$ Getting v record v -> record -> v
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall s a. HasField' field s a => Lens s s a a
forall (field :: Symbol) s a. HasField' field s a => Lens s s a a
Generic.field' @field)
    {-# INLINE await_ #-}

instance (HasSource tag r m, MonadTrans t, Monad (t m))
  => HasSource tag r (Lift (t m))
  where
    await_ :: Proxy# tag -> Lift (t m) r
await_ Proxy# tag
_ = t m r -> Lift (t m) r
coerce (t m r -> Lift (t m) r) -> t m r -> Lift (t m) r
forall a b. (a -> b) -> a -> b
$ forall a. Monad m => m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift @t @m (m r -> t m r) -> m r -> t m r
forall a b. (a -> b) -> a -> b
$ forall k (tag :: k) a (m :: * -> *). HasSource tag a m => m a
forall (m :: * -> *). HasSource tag r m => m r
await @tag @r
    {-# INLINE await_ #-}

--------------------------------------------------------------------------------

instance State.MonadState s m => HasSource tag s (MonadState m) where
  await_ :: Proxy# tag -> MonadState m s
await_ Proxy# tag
_ = m s -> MonadState m s
coerce @(m s) m s
forall s (m :: * -> *). MonadState s m => m s
State.get
  {-# INLINE await_ #-}

instance
  (HasSource tag (IORef s) m, MonadIO m)
  => HasSource tag s (ReaderIORef m)
  where
    await_ :: Proxy# tag -> ReaderIORef m s
await_ Proxy# tag
_ = m s -> ReaderIORef m s
forall k (m :: k -> *) (a :: k). m a -> ReaderIORef m a
ReaderIORef (m s -> ReaderIORef m s) -> m s -> ReaderIORef m s
forall a b. (a -> b) -> a -> b
$ do
      IORef s
ref <- forall k (tag :: k) a (m :: * -> *). HasSource tag a m => m a
forall a (m :: * -> *). HasSource tag a m => m a
await @tag
      IO s -> m s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO s -> m s) -> IO s -> m s
forall a b. (a -> b) -> a -> b
$ IORef s -> IO s
forall a. IORef a -> IO a
readIORef IORef s
ref
    {-# INLINE await_ #-}

instance
  ( MutableRef ref, RefElement ref ~ s
  , HasSource tag ref m, PrimMonad m, PrimState m ~ MCState ref )
  => HasSource tag s (ReaderRef m)
  where
    await_ :: Proxy# tag -> ReaderRef m s
await_ Proxy# tag
_ = m s -> ReaderRef m s
forall (m :: * -> *) a. m a -> ReaderRef m a
ReaderRef (m s -> ReaderRef m s) -> m s -> ReaderRef m s
forall a b. (a -> b) -> a -> b
$ do
      ref
ref <- forall k (tag :: k) a (m :: * -> *). HasSource tag a m => m a
forall a (m :: * -> *). HasSource tag a m => m a
await @tag
      ref -> m (RefElement ref)
forall c (m :: * -> *).
(MutableRef c, PrimMonad m, PrimState m ~ MCState c) =>
c -> m (RefElement c)
readRef ref
ref
    {-# INLINE await_ #-}