{-# LANGUAGE AllowAmbiguousTypes    #-}
{-# LANGUAGE DefaultSignatures      #-}
{-# LANGUAGE EmptyCase              #-}
{-# LANGUAGE FlexibleContexts       #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs                  #-}
{-# LANGUAGE LambdaCase             #-}
{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE RankNTypes             #-}
{-# LANGUAGE ScopedTypeVariables    #-}
{-# LANGUAGE StandaloneDeriving     #-}
{-# LANGUAGE TypeApplications       #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeInType             #-}
{-# LANGUAGE TypeOperators          #-}
{-# LANGUAGE UndecidableInstances   #-}

module Data.Mutable.Internal (
    Mutable(..)
  , RefFor(..)
  , DefaultMutable(..)
  -- * Instances
  -- ** Generic
  , GRef(..)
  , gThawRef, gFreezeRef
  , gCopyRef, gMoveRef, gCloneRef
  , gUnsafeThawRef, gUnsafeFreezeRef
  , GMutable (GRef_)
  -- ** Higher-Kinded Data Pattern
  , thawHKD, freezeHKD
  , copyHKD, moveHKD, cloneHKD
  , unsafeThawHKD, unsafeFreezeHKD
  -- ** Coercible
  , CoerceRef(..)
  , thawCoerce, freezeCoerce
  , copyCoerce, moveCoerce, cloneCoerce
  , unsafeThawCoerce, unsafeFreezeCoerce
  -- ** Traversable
  , TraverseRef(..)
  , thawTraverse, freezeTraverse
  , copyTraverse, moveTraverse, cloneTraverse
  , unsafeThawTraverse, unsafeFreezeTraverse
  -- ** Immutable
  , ImmutableRef(..), thawImmutable, freezeImmutable, copyImmutable
  -- ** Instances for Generics combinators themselves
  , GMutableRef(..)
  , MutSumF(..)
  -- ** Needed for MutBranch
  , ListRefTuple(..)
  , MapRef
  ) where

import           Control.Monad.Primitive
import           Control.Monad.Trans.Class
import           Control.Monad.Trans.State
import           Data.Bifunctor
import           Data.Coerce
import           Data.Foldable
import           Data.Generics.Product.Internal.HList
import           Data.Kind
import           Data.List
import           Data.Primitive.MutVar
import           Data.Vinyl.Functor
import           GHC.Generics
import qualified Data.GenericLens.Internal            as GL
import qualified Data.Vinyl.XRec                      as X

-- | An instance of @'Mutable' s a@ means that @a@ can be stored
-- a mutable reference in a 'PrimMonad' @m@ (where @s@ is the mutable state
-- token 'PrimState' of that monad).
--
-- The associated type @'Ref' s a@ links any @a@ to the type of its
-- canonical mutable version.
--
-- The /benefit/ of this typeclass, instead of just using
-- 'Data.IORef.IORef' or 'MutVar' or specific mutable versions like
-- 'V.Vector' and 'MV.MVector', is two-fold:
--
-- *   Piecewise-mutable values, so you can write to only one part and not
--     others.  This also allows for cheaper "writes", even if you replace
--     the whole value: you don't need to ever synthesize an entire new
--     value, you can keep each component in a separate variable until you
--     'freezeRef' it out.  This can be especially useful for composite
--     data types containing large structures like 'V.Vector'.
-- *   Generic abstractions (similar to 'Show'), so you can automatically
--     derive instances while preserving piecewise-ness.  For example, the
--     instance
--
--     @
--     instance (Mutable s a, Mutable s b) => Mutable s (a, b)
--     @
--
--     If @a@ and @b@ are piecwise-mutable, then the instance here will
--     appropriately utilize that fact.
--
-- To modify the specific parts of mutable values, it can be useful to use
-- the functions in "Data.Mutable.Parts".
--
-- There are facilities to automatically piecewise mutable versions for
-- user-defined instances of 'Generic'.
--
-- For example, if we have a type like:
--
-- @
-- data TwoVectors = TV
--     { tvInt    :: 'V.Vector' Int
--     , tvDouble :: Vector Double
--     }
--   deriving Generic
--
-- instance Mutable s TwoVectors where
--     type Ref s TwoVectors = 'GRef' s TwoVectors
-- @
--
-- Then now we get:
--
-- @
-- 'thawRef'   :: TwoVectors -> m ('GRef' s TwoVectors)
-- 'freezeRef' :: 'GRef' s TwoVectors -> m TwoVectors
-- @
--
-- And @'GRef' s TwoVectors@ is now a piecewise-mutable reference storing each
-- part in a way that can be modified separately (for example, with tools
-- from "Data.Mutable.Parts").  It does this by internally allocating two
-- 'MV.MVector's.  If the two vectors are large, this can be much more
-- efficient to modify (if you are modifying /several times/) than by just
-- doing alterations on @TwoVector@s.  It is also much better for large
-- vectors if you plan on modifying only a single item in the vector.
--
-- If you are using the "higher-kinded" data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>, then we
-- can also do:
--
-- @
-- data TwoVectors f = TV
--      { tvInt    :: 'X.HKD' f ('V.Vector' Int)
--      , tvDouble :: HKD f (Vector Double)
--      }
--   deriving Generic
--
-- instance Mutable (TwoVectors 'Identity') where
--     type Ref (TwoVectors 'Identity') = TwoVectors ('RefFor' s)
-- @
--
-- And now your mutable ref is literally going to be a product of the
-- components
--
-- @
-- ghci> tvr@(TV is ds) <- thawRef (TV xs ys)
-- ghci> :t tvr
-- TV ('RefFor' 'RealWorld')
-- ghci> :t is
-- 'MV.MVector' RealWorld Int
-- ghci> :t ds
-- MV.MVector RealWorld Double
-- @
--
-- So 'thawRef' will actually just get you the same record type but with
-- the mutable versions of each field.  If you modify the mutable fields,
-- and then later 'freezeRef' the whole thing, the resulting frozen value
-- will incorporate all of the changes to the individual fields.
--
-- In addition, there are a few more "automatically derived" instances you
-- can get by picking 'Ref':
--
-- @
-- -- Make a mutable version for any newtype wrapper, using the 'Mutable'
-- -- of the underlying type
-- newtype MyType = MT (Vector Double)
--
-- type Ref s MyType = CoerceRef s MyType (Vector Double)
--
-- -- Make a mutable version of any container, where the items are all
-- -- mutable references.
-- data MyContainer a = MC a a a a
--   deriving (Functor, Foldable, Traversable)
--
-- type Ref s (MyContainer a) = TraverseRef s MyContainer a
-- @
--
-- See <https://mutable.jle.im/02-mutable-and-ref.html> for more
-- information on this typeclass and how to define instances
-- automatically, and also
--
-- *  <https://mutable.jle.im/05-mutable-parts.html> for more information
--    on dealing with record types
-- *  <https://mutable.jle.im/06-mutable-branches> for more information
--    on dealing with sum types
class Mutable s a where
    -- | Links the type @a@ to the type of its canonical "mutable version".
    --
    -- For example, for 'V.Vector', the mutable version is 'MV.MVector', so
    -- we have
    --
    -- @
    -- type Ref s ('V.Vector' a) = 'MV.MVector' s a
    -- @
    --
    -- This means that using 'thawRef' on a 'V.Vector' will give you an
    -- 'MV.MVector', using 'freezeRef' on a 'MV.Vector' will give you
    -- a 'V.Vector', etc.
    --
    -- @
    -- 'thawRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'V.Vector' a
    --     -> m ('MV.Vector' s a)
    --
    -- 'freezeRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'MV.Vector' s a
    --     -> m ('V.Vector' a)
    --
    -- 'copyRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'MV.Vector' s a
    --     -> 'V.Vector' a
    --     -> m ()
    -- @
    --
    -- This associated type must be unique for @a@, so no two types @a@ can
    -- have the same @'Ref' s a@.  This makes type inference a lot more
    -- useful: if you use 'freezeRef' on an 'MV.MVector', for instance, the
    -- return type will be inferred to be 'V.Vector'.
    --
    -- The /default/ instance is just a plain old 'MutVar' containing the
    -- type.  This is a valid instance, but it treats the entire type
    -- "wholesale" --- it is basically using it as a non-mutable type.  You
    -- won't get any of the performance benefits of piecewise mutation from
    -- it, but it is useful as a base case for non-composite types like
    -- 'Int'.
    --
    -- There are some built-in alternative options for user-defined ADTs
    -- with 'Generic' instances:
    --
    -- @
    -- -- Works for all 'Generic' instances, preserves piecewise mutation
    -- -- for products
    -- type Ref s a = 'GRef' s a
    -- @
    --
    -- If you just set up a blank instance, the implementations of
    -- 'thawRef', 'freezeRef', and 'copyRef' will be inferred using
    -- 'DefaultMutable'.
    --
    -- @
    -- data MyType
    --
    -- -- The default setup is OK
    -- instance Mutable s MyType
    --
    -- -- This is equivalent to the above
    -- instance Mutable s MyType
    --     type Ref s MyType = 'MutVar' s MyType
    --
    -- -- any 'Generic' instance
    -- data MyType = MyType { mtInt :: Int, mtDouble :: Double }
    --   deriving Generic
    --
    -- instance Mutable s MyType where
    --     type Ref s MyType = 'GRef' s MyType
    -- @
    --
    -- See <https://mutable.jle.im/02-mutable-and-ref.html> for more
    -- information on this type family and how to define instances
    -- automatically.
    --
    -- Note that this type synonym ins injective --- this means that if you
    -- write a function polymorphic over @'Ref' s a@, you can always infer
    -- @s@ and @a@ (the value stored in the 'Ref').
    --
    -- In practice, if you want to write your own instance from scratch,
    -- the consequence is that you must have the @s@ type variable
    -- somewhere in your type (see 'UnitRef' and 'VoidRef' for examples).
    type Ref s a = (v :: Type) | v -> a s
    type Ref s a = MutVar s a

    -- | "Thaw" a pure/persistent value into its mutable version, which can
    -- be manipulated using 'Data.Mutable.modifyRef' or other methods
    -- specific for that type (like 'MV.read').
    --
    -- Returns the 'Ref' instance, so, for example, for 'V.Vector':
    --
    -- @
    -- 'thawRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'V.Vector' a
    --     -> m ('MV.Vector' s a)
    -- @
    --
    -- For non-composite (like 'Int'), this is often called the "new var"
    -- function, like 'Data.IORef.newIORef' / 'Data.STRef.newSTRef'
    -- / 'newMutVar' etc.
    thawRef   :: (PrimMonad m, PrimState m ~ s) => a -> m (Ref s a)

    -- | "Freeze" a mutable value into its pure/persistent version.
    --
    -- Takes a 'Ref' instance, but type inference will be able to infer the
    -- pure value's type because 'Ref' is injective.
    --
    -- For example, for 'V.Vector':
    --
    -- @
    -- 'freezeRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'MV.Vector' s a
    --     -> m ('V.Vector' a)
    -- @
    --
    -- For non-composite (like 'Int'), this is often called the "read var"
    -- function, like 'Data.IORef.readIORef' / 'Data.STRef.readSTRef'
    -- / 'readMutVar' etc.
    freezeRef :: (PrimMonad m, PrimState m ~ s) => Ref s a -> m a

    -- | Overwrite a mutable value by provivding a pure/persistent value.
    -- 'copyRef'
    --
    -- Returns the 'Ref' and the value, so, for example, for 'V.Vector':
    --
    -- @
    -- 'copyRef'
    --     :: ('PrimMonad' m, 'PrimState' m ~ s)
    --     => 'MV.Vector' s a
    --     -> 'V.Vector' a
    --     -> m ()
    -- @
    --
    -- Note that if @a@ is a composite type (with an appropriate composite
    -- reference), this will be done "piecewise": it'll write to each
    -- mutable component separately.
    --
    -- For non-composite (like 'Int'), this is often called the "write var"
    -- function, like 'Data.IORef.writeIORef' / 'Data.STRef.writeSTRef'
    -- / 'writeMutVar' etc.
    copyRef
        :: (PrimMonad m, PrimState m ~ s)
        => Ref s a      -- ^ destination to overwrite
        -> a            -- ^ value
        -> m ()

    -- | Deep Copy-move a mutable reference on top of another, overwriting the
    -- second one.
    --
    -- For non-composite types, this is the same as a 'thawRef' and
    -- a 'copyRef'.  For composite types this can be more effficient
    -- because the copying is done piecewise, so the intermediate pure value
    -- is never created.
    moveRef
        :: (PrimMonad m, PrimState m ~ s)
        => Ref s a      -- ^ destination
        -> Ref s a      -- ^ source
        -> m ()

    -- | Create a deep copy of a mutable reference, allocated to a separate
    -- independent reference.
    --
    -- For non-composite types, this is the same as a 'thawRef' and
    -- a 'freezeRef'.  For composite types this can be more effficient
    -- because the cloning is done piecewise, so the intermediate pure value
    -- is never created.
    cloneRef :: (PrimMonad m, PrimState m ~ s) => Ref s a -> m (Ref s a)

    -- this is nice but you can't write an instance for 'TraverseRef' on
    -- this, so maybe not.
    -- -- | Initialize a mutable reference with fields being undefined or
    -- -- with undefined values.  This is only useful if you can modify parts
    -- -- of the mutable value (with things like "Data.Mutable.Parts").  If
    -- -- you attempt to 'freezeRef' (or 'modifyRef' etc.) this before setting
    -- -- all of the fields to reasonable values, this is likely to blow up.
    -- initRef :: m (Ref s a)

    -- | A non-copying version of 'thawRef' that can be more efficient for
    -- types where the mutable representation is the same as the immutable
    -- one (like 'V.Vector').
    --
    -- This is safe as long as you never again use the original pure
    -- value, since it can potentially directly mutate it.
    unsafeThawRef   :: (PrimMonad m, PrimState m ~ s) => a -> m (Ref s a)

    -- | A non-copying version of 'freezeRef' that can be more efficient for
    -- types where the mutable representation is the same as the immutable
    -- one (like 'V.Vector').
    --
    -- This is safe as long as you never again modify the mutable
    -- reference, since it can potentially directly mutate the frozen value
    -- magically.
    unsafeFreezeRef :: (PrimMonad m, PrimState m ~ s) => Ref s a -> m a

    default thawRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => a -> m (Ref s a)
    thawRef   = a -> m (Ref s a)
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
a -> m r
defaultThawRef
    default freezeRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => Ref s a -> m a
    freezeRef = Ref s a -> m a
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
r -> m a
defaultFreezeRef
    default copyRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => Ref s a -> a -> m ()
    copyRef   = Ref s a -> a -> m ()
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
r -> a -> m ()
defaultCopyRef
    default moveRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => Ref s a -> Ref s a -> m ()
    moveRef   = Ref s a -> Ref s a -> m ()
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
r -> r -> m ()
defaultMoveRef
    default cloneRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => Ref s a -> m (Ref s a)
    cloneRef  = Ref s a -> m (Ref s a)
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
r -> m r
defaultCloneRef
    default unsafeThawRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => a -> m (Ref s a)
    unsafeThawRef   = a -> m (Ref s a)
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
a -> m r
defaultUnsafeThawRef
    default unsafeFreezeRef :: (DefaultMutable s a (Ref s a), PrimMonad m, PrimState m ~ s) => Ref s a -> m a
    unsafeFreezeRef = Ref s a -> m a
forall s a r (m :: * -> *).
(DefaultMutable s a r, PrimMonad m, PrimState m ~ s) =>
r -> m a
defaultUnsafeFreezeRef

-- | The default implementations of 'thawRef', 'freezeRef', and 'copyRef'
-- dispatched for different choices of 'Ref'.
--
-- Basically, by specifying 'Ref', you get the rest of the instance for
-- free.
--
-- We have the default case:
--
-- @
-- -- default, if you don't specify 'Ref'
-- instance Mutable s MyType
--
-- -- the above is the same as:
-- instance Mutable s MyType
--     type Ref s MyType = MutVar s) MyType
-- @
--
-- The case for any instance of 'Generic':
--
-- @
-- instance Mutable s MyType
--     type Ref s MyType = GRef s MyType
-- @
--
-- The case for the "higher-kinded data" pattern a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>:
--
-- @
-- instance Mutable s (MyTypeF Identity)
--     type Ref s (MyTypeF Identity) = MyTypeF (RefFor s)
-- @
--
-- The case for any newtype wrapper:
--
-- @
-- newtype MyType = MT (Vector Double)
--
-- instance Mutable s MyType where
--     type Ref s MyType = CoerceRef s MyType (Vector Double)
-- @
--
-- And the case for any 'Traversable instance, where the items will all be
-- mutable references:
--
-- @
-- data MyContainer a = MC a a a a
--   deriving (Functor, Foldable, Traversable)
--
-- instance Mutable s a => Mutable s (MyContainer a) where
--     type Ref s (MyContainer a) = TraverseRef s MyContainer a
-- @
--
class DefaultMutable s a r | r -> a s where
    defaultThawRef         :: (PrimMonad m, PrimState m ~ s) => a -> m r
    defaultFreezeRef       :: (PrimMonad m, PrimState m ~ s) => r -> m a
    defaultCopyRef         :: (PrimMonad m, PrimState m ~ s) => r -> a -> m ()
    defaultMoveRef         :: (PrimMonad m, PrimState m ~ s) => r -> r -> m ()
    defaultCloneRef        :: (PrimMonad m, PrimState m ~ s) => r -> m r
    defaultUnsafeThawRef   :: (PrimMonad m, PrimState m ~ s) => a -> m r
    defaultUnsafeFreezeRef :: (PrimMonad m, PrimState m ~ s) => r -> m a

instance DefaultMutable s a (MutVar s a) where
    defaultThawRef :: a -> m (MutVar s a)
defaultThawRef         = a -> m (MutVar s a)
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar
    defaultFreezeRef :: MutVar s a -> m a
defaultFreezeRef       = MutVar s a -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar
    defaultCopyRef :: MutVar s a -> a -> m ()
defaultCopyRef         = MutVar s a -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar
    defaultMoveRef :: MutVar s a -> MutVar s a -> m ()
defaultMoveRef v :: MutVar s a
v u :: MutVar s a
u     = MutVar (PrimState m) a -> a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar s a
MutVar (PrimState m) a
v (a -> m ()) -> m a -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MutVar (PrimState m) a -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s a
MutVar (PrimState m) a
u
    defaultCloneRef :: MutVar s a -> m (MutVar s a)
defaultCloneRef v :: MutVar s a
v      = a -> m (MutVar s a)
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar (a -> m (MutVar s a)) -> m a -> m (MutVar s a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MutVar (PrimState m) a -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s a
MutVar (PrimState m) a
v
    defaultUnsafeThawRef :: a -> m (MutVar s a)
defaultUnsafeThawRef   = a -> m (MutVar s a)
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar
    defaultUnsafeFreezeRef :: MutVar s a -> m a
defaultUnsafeFreezeRef = MutVar s a -> m a
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar

instance (Generic a, GMutable s (Rep a)) => DefaultMutable s a (GRef s a) where
    defaultThawRef :: a -> m (GRef s a)
defaultThawRef         = a -> m (GRef s a)
forall a s (m :: * -> *).
(Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
a -> m (GRef s a)
gThawRef
    defaultFreezeRef :: GRef s a -> m a
defaultFreezeRef       = GRef s a -> m a
forall a s (m :: * -> *).
(Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
GRef s a -> m a
gFreezeRef
    defaultCopyRef :: GRef s a -> a -> m ()
defaultCopyRef         = GRef s a -> a -> m ()
forall a s (m :: * -> *).
(Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
GRef s a -> a -> m ()
gCopyRef
    defaultMoveRef :: GRef s a -> GRef s a -> m ()
defaultMoveRef         = GRef s a -> GRef s a -> m ()
forall s a (m :: * -> *).
(GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
GRef s a -> GRef s a -> m ()
gMoveRef
    defaultCloneRef :: GRef s a -> m (GRef s a)
defaultCloneRef        = GRef s a -> m (GRef s a)
forall s a (m :: * -> *).
(GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
GRef s a -> m (GRef s a)
gCloneRef
    defaultUnsafeThawRef :: a -> m (GRef s a)
defaultUnsafeThawRef   = a -> m (GRef s a)
forall a s (m :: * -> *).
(Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
a -> m (GRef s a)
gUnsafeThawRef
    defaultUnsafeFreezeRef :: GRef s a -> m a
defaultUnsafeFreezeRef = GRef s a -> m a
forall a s (m :: * -> *).
(Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s) =>
GRef s a -> m a
gUnsafeFreezeRef

instance (Generic (z Identity), Generic (z (RefFor s)), GMutable s (Rep (z Identity)), GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)))
        => DefaultMutable s (z Identity) (z (RefFor s)) where
    defaultThawRef :: z Identity -> m (z (RefFor s))
defaultThawRef         = z Identity -> m (z (RefFor s))
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z Identity), Generic (z (RefFor s)),
 GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z Identity -> m (z (RefFor s))
thawHKD
    defaultFreezeRef :: z (RefFor s) -> m (z Identity)
defaultFreezeRef       = z (RefFor s) -> m (z Identity)
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z Identity), Generic (z (RefFor s)),
 GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z (RefFor s) -> m (z Identity)
freezeHKD
    defaultCopyRef :: z (RefFor s) -> z Identity -> m ()
defaultCopyRef         = z (RefFor s) -> z Identity -> m ()
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z Identity), Generic (z (RefFor s)),
 GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z (RefFor s) -> z Identity -> m ()
copyHKD
    defaultMoveRef :: z (RefFor s) -> z (RefFor s) -> m ()
defaultMoveRef         = z (RefFor s) -> z (RefFor s) -> m ()
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z (RefFor s)), GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z (RefFor s) -> z (RefFor s) -> m ()
moveHKD
    defaultCloneRef :: z (RefFor s) -> m (z (RefFor s))
defaultCloneRef        = z (RefFor s) -> m (z (RefFor s))
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z (RefFor s)), GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z (RefFor s) -> m (z (RefFor s))
cloneHKD
    defaultUnsafeThawRef :: z Identity -> m (z (RefFor s))
defaultUnsafeThawRef   = z Identity -> m (z (RefFor s))
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z Identity), Generic (z (RefFor s)),
 GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z Identity -> m (z (RefFor s))
unsafeThawHKD
    defaultUnsafeFreezeRef :: z (RefFor s) -> m (z Identity)
defaultUnsafeFreezeRef = z (RefFor s) -> m (z Identity)
forall (z :: (* -> *) -> *) (m :: * -> *) s.
(Generic (z Identity), Generic (z (RefFor s)),
 GMutable s (Rep (z Identity)),
 GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s)), PrimMonad m,
 PrimState m ~ s) =>
z (RefFor s) -> m (z Identity)
unsafeFreezeHKD

instance (Traversable f, Mutable s a) => DefaultMutable s (f a) (TraverseRef s f a) where
    defaultThawRef :: f a -> m (TraverseRef s f a)
defaultThawRef         = f a -> m (TraverseRef s f a)
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
f a -> m (TraverseRef s f a)
thawTraverse
    defaultFreezeRef :: TraverseRef s f a -> m (f a)
defaultFreezeRef       = TraverseRef s f a -> m (f a)
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
TraverseRef s f a -> m (f a)
freezeTraverse
    defaultCopyRef :: TraverseRef s f a -> f a -> m ()
defaultCopyRef         = TraverseRef s f a -> f a -> m ()
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
TraverseRef s f a -> f a -> m ()
copyTraverse
    defaultMoveRef :: TraverseRef s f a -> TraverseRef s f a -> m ()
defaultMoveRef         = TraverseRef s f a -> TraverseRef s f a -> m ()
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
TraverseRef s f a -> TraverseRef s f a -> m ()
moveTraverse
    defaultCloneRef :: TraverseRef s f a -> m (TraverseRef s f a)
defaultCloneRef        = TraverseRef s f a -> m (TraverseRef s f a)
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
TraverseRef s f a -> m (TraverseRef s f a)
cloneTraverse
    defaultUnsafeThawRef :: f a -> m (TraverseRef s f a)
defaultUnsafeThawRef   = f a -> m (TraverseRef s f a)
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
f a -> m (TraverseRef s f a)
unsafeThawTraverse
    defaultUnsafeFreezeRef :: TraverseRef s f a -> m (f a)
defaultUnsafeFreezeRef = TraverseRef s f a -> m (f a)
forall (f :: * -> *) s a (m :: * -> *).
(Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) =>
TraverseRef s f a -> m (f a)
unsafeFreezeTraverse

instance (Coercible b a, Mutable s a) => DefaultMutable s b (CoerceRef s b a) where
    defaultThawRef :: b -> m (CoerceRef s b a)
defaultThawRef         = b -> m (CoerceRef s b a)
forall b a s (m :: * -> *).
(Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) =>
b -> m (CoerceRef s b a)
thawCoerce
    defaultFreezeRef :: CoerceRef s b a -> m b
defaultFreezeRef       = CoerceRef s b a -> m b
forall b a s (m :: * -> *).
(Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) =>
CoerceRef s b a -> m b
freezeCoerce
    defaultCopyRef :: CoerceRef s b a -> b -> m ()
defaultCopyRef         = CoerceRef s b a -> b -> m ()
forall b a s (m :: * -> *).
(Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) =>
CoerceRef s b a -> b -> m ()
copyCoerce
    defaultMoveRef :: CoerceRef s b a -> CoerceRef s b a -> m ()
defaultMoveRef         = CoerceRef s b a -> CoerceRef s b a -> m ()
forall k s a (m :: * -> *) (b :: k).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
CoerceRef s b a -> CoerceRef s b a -> m ()
moveCoerce
    defaultCloneRef :: CoerceRef s b a -> m (CoerceRef s b a)
defaultCloneRef        = CoerceRef s b a -> m (CoerceRef s b a)
forall k s a (m :: * -> *) (b :: k).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
CoerceRef s b a -> m (CoerceRef s b a)
cloneCoerce
    defaultUnsafeThawRef :: b -> m (CoerceRef s b a)
defaultUnsafeThawRef   = b -> m (CoerceRef s b a)
forall b a s (m :: * -> *).
(Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) =>
b -> m (CoerceRef s b a)
unsafeThawCoerce
    defaultUnsafeFreezeRef :: CoerceRef s b a -> m b
defaultUnsafeFreezeRef = CoerceRef s b a -> m b
forall b a s (m :: * -> *).
(Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) =>
CoerceRef s b a -> m b
unsafeFreezeCoerce

instance DefaultMutable s a (ImmutableRef s a) where
    defaultThawRef :: a -> m (ImmutableRef s a)
defaultThawRef         = a -> m (ImmutableRef s a)
forall k (m :: * -> *) a (s :: k).
Applicative m =>
a -> m (ImmutableRef s a)
thawImmutable
    defaultFreezeRef :: ImmutableRef s a -> m a
defaultFreezeRef       = ImmutableRef s a -> m a
forall k (m :: * -> *) (s :: k) a.
Applicative m =>
ImmutableRef s a -> m a
freezeImmutable
    defaultCopyRef :: ImmutableRef s a -> a -> m ()
defaultCopyRef         = ImmutableRef s a -> a -> m ()
forall k (m :: * -> *) (s :: k) a.
Applicative m =>
ImmutableRef s a -> a -> m ()
copyImmutable
    defaultMoveRef :: ImmutableRef s a -> ImmutableRef s a -> m ()
defaultMoveRef         = ImmutableRef s a -> ImmutableRef s a -> m ()
forall k (m :: * -> *) (s :: k) a.
Applicative m =>
ImmutableRef s a -> ImmutableRef s a -> m ()
moveImmutable
    defaultCloneRef :: ImmutableRef s a -> m (ImmutableRef s a)
defaultCloneRef        = ImmutableRef s a -> m (ImmutableRef s a)
forall k (m :: * -> *) (s :: k) a.
Applicative m =>
ImmutableRef s a -> m (ImmutableRef s a)
cloneImmutable
    defaultUnsafeThawRef :: a -> m (ImmutableRef s a)
defaultUnsafeThawRef   = a -> m (ImmutableRef s a)
forall k (m :: * -> *) a (s :: k).
Applicative m =>
a -> m (ImmutableRef s a)
thawImmutable
    defaultUnsafeFreezeRef :: ImmutableRef s a -> m a
defaultUnsafeFreezeRef = ImmutableRef s a -> m a
forall k (m :: * -> *) (s :: k) a.
Applicative m =>
ImmutableRef s a -> m a
freezeImmutable

-- | A handy newtype wrapper that allows you to partially apply 'Ref'.
-- @'RefFor' m a@ is the same as @'Ref' s a@, but can be partially applied.
--
-- If used with 'X.HKD', you can treat this syntactically identically as
-- a @'Ref' s a@.
newtype RefFor s a = RefFor { RefFor s a -> Ref s a
getRefFor :: Ref s a }

deriving instance Eq (Ref s a) => Eq (RefFor s a)
deriving instance Ord (Ref s a) => Ord (RefFor s a)

-- | Use a @'RefFor' m a@ as if it were a @'Ref' s a@.
instance X.IsoHKD (RefFor s) a where
    type HKD (RefFor s) a = Ref s a
    unHKD :: HKD (RefFor s) a -> RefFor s a
unHKD = HKD (RefFor s) a -> RefFor s a
forall s a. Ref s a -> RefFor s a
RefFor
    toHKD :: RefFor s a -> HKD (RefFor s) a
toHKD = RefFor s a -> HKD (RefFor s) a
forall s a. RefFor s a -> Ref s a
getRefFor

-- | A 'Ref' that works for any instance of 'Traversable', by using the
-- fields of the 'Traversable' instance to /purely/ store mutable references.
--
-- Note that this really only makes complete sense if the 'Traversable' is
-- fixed-size, or you never modify the length of the traversable as you use
-- it as a reference.
--
-- If you /do/ modify the length, copying and modifying semantics can be
-- a bit funky:
--
-- *   If copying a shorter item into a longer item ref, the "leftovers" items
--     in the longer item are unchanged.
-- *   If copying a longer item into a shorter item ref, the leftover items
--     are unchanged.
--
-- @
-- ghci> r <- 'thawTraverse' [1..10]
-- ghci> 'copyTraverse' r [0,0,0,0]
-- ghci> 'freezeTraverse' r
-- [0,0,0,0,5,6,7,8,9,10]
-- ghci> 'copyTraverse' r [20..50]
-- ghci> 'freezeTraverse' r
-- [20,21,22,23,24,25,26,27,28,29]
-- @
--
newtype TraverseRef s f a = TraverseRef { TraverseRef s f a -> f (Ref s a)
getTraverseRef :: f (Ref s a) }

-- | Use a @'TraverseRef' s f a@ as if it were a @f ('Ref' s a)@
instance X.IsoHKD (TraverseRef s f) a where
    type HKD (TraverseRef s f) a = f (Ref s a)
    unHKD :: HKD (TraverseRef s f) a -> TraverseRef s f a
unHKD = HKD (TraverseRef s f) a -> TraverseRef s f a
forall s (f :: * -> *) a. f (Ref s a) -> TraverseRef s f a
TraverseRef
    toHKD :: TraverseRef s f a -> HKD (TraverseRef s f) a
toHKD = TraverseRef s f a -> HKD (TraverseRef s f) a
forall s (f :: * -> *) a. TraverseRef s f a -> f (Ref s a)
getTraverseRef

-- | Default 'thawRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
thawTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => f a -> m (TraverseRef s f a)
thawTraverse :: f a -> m (TraverseRef s f a)
thawTraverse = (f (Ref s a) -> TraverseRef s f a)
-> m (f (Ref s a)) -> m (TraverseRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f (Ref s a) -> TraverseRef s f a
forall s (f :: * -> *) a. f (Ref s a) -> TraverseRef s f a
TraverseRef (m (f (Ref s a)) -> m (TraverseRef s f a))
-> (f a -> m (f (Ref s a))) -> f a -> m (TraverseRef s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (Ref s a)) -> f a -> m (f (Ref s a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
thawRef

-- | Default 'freezeRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
freezeTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => TraverseRef s f a -> m (f a)
freezeTraverse :: TraverseRef s f a -> m (f a)
freezeTraverse = (Ref s a -> m a) -> f (Ref s a) -> m (f a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Ref s a -> m a
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
freezeRef (f (Ref s a) -> m (f a))
-> (TraverseRef s f a -> f (Ref s a))
-> TraverseRef s f a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TraverseRef s f a -> f (Ref s a)
forall s (f :: * -> *) a. TraverseRef s f a -> f (Ref s a)
getTraverseRef

-- | Default 'copyRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
copyTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => TraverseRef s f a -> f a -> m ()
copyTraverse :: TraverseRef s f a -> f a -> m ()
copyTraverse (TraverseRef rs :: f (Ref s a)
rs) xs :: f a
xs = StateT [a] m () -> [a] -> m ()
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT ((Ref s a -> StateT [a] m ()) -> f (Ref s a) -> StateT [a] m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ Ref s a -> StateT [a] m ()
forall (m :: * -> *) a.
(Mutable (PrimState m) a, PrimMonad m) =>
Ref (PrimState m) a -> StateT [a] m ()
go f (Ref s a)
rs) (f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f a
xs)
  where
    go :: Ref (PrimState m) a -> StateT [a] m ()
go r :: Ref (PrimState m) a
r = do
      Maybe a
x <- ([a] -> (Maybe a, [a])) -> StateT [a] m (Maybe a)
forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state (([a] -> (Maybe a, [a])) -> StateT [a] m (Maybe a))
-> ([a] -> (Maybe a, [a])) -> StateT [a] m (Maybe a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, [a])
-> ((a, [a]) -> (Maybe a, [a])) -> Maybe (a, [a]) -> (Maybe a, [a])
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe a
forall a. Maybe a
Nothing, []) ((a -> Maybe a) -> (a, [a]) -> (Maybe a, [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first a -> Maybe a
forall a. a -> Maybe a
Just) (Maybe (a, [a]) -> (Maybe a, [a]))
-> ([a] -> Maybe (a, [a])) -> [a] -> (Maybe a, [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (a, [a])
forall a. [a] -> Maybe (a, [a])
uncons
      m () -> StateT [a] m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT [a] m ()) -> m () -> StateT [a] m ()
forall a b. (a -> b) -> a -> b
$ (a -> m ()) -> Maybe a -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Ref (PrimState m) a -> a -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> a -> m ()
copyRef Ref (PrimState m) a
r) Maybe a
x

-- | Default 'moveRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
moveTraverse
    :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s)
    => TraverseRef s f a        -- ^ destination
    -> TraverseRef s f a        -- ^ source
    -> m ()
moveTraverse :: TraverseRef s f a -> TraverseRef s f a -> m ()
moveTraverse (TraverseRef rs :: f (Ref s a)
rs) (TraverseRef vs :: f (Ref s a)
vs) = StateT [Ref s a] m () -> [Ref s a] -> m ()
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT ((Ref s a -> StateT [Ref s a] m ())
-> f (Ref s a) -> StateT [Ref s a] m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ Ref s a -> StateT [Ref s a] m ()
forall (m :: * -> *) a.
(Mutable (PrimState m) a, PrimMonad m) =>
Ref (PrimState m) a -> StateT [Ref (PrimState m) a] m ()
go f (Ref s a)
rs) (f (Ref s a) -> [Ref s a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f (Ref s a)
vs)
  where
    go :: Ref (PrimState m) a -> StateT [Ref (PrimState m) a] m ()
go r :: Ref (PrimState m) a
r = do
      Maybe (Ref (PrimState m) a)
x <- ([Ref (PrimState m) a]
 -> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a]))
-> StateT [Ref (PrimState m) a] m (Maybe (Ref (PrimState m) a))
forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state (([Ref (PrimState m) a]
  -> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a]))
 -> StateT [Ref (PrimState m) a] m (Maybe (Ref (PrimState m) a)))
-> ([Ref (PrimState m) a]
    -> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a]))
-> StateT [Ref (PrimState m) a] m (Maybe (Ref (PrimState m) a))
forall a b. (a -> b) -> a -> b
$ (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a])
-> ((Ref (PrimState m) a, [Ref (PrimState m) a])
    -> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a]))
-> Maybe (Ref (PrimState m) a, [Ref (PrimState m) a])
-> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a])
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe (Ref (PrimState m) a)
forall a. Maybe a
Nothing, []) ((Ref (PrimState m) a -> Maybe (Ref (PrimState m) a))
-> (Ref (PrimState m) a, [Ref (PrimState m) a])
-> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Ref (PrimState m) a -> Maybe (Ref (PrimState m) a)
forall a. a -> Maybe a
Just) (Maybe (Ref (PrimState m) a, [Ref (PrimState m) a])
 -> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a]))
-> ([Ref (PrimState m) a]
    -> Maybe (Ref (PrimState m) a, [Ref (PrimState m) a]))
-> [Ref (PrimState m) a]
-> (Maybe (Ref (PrimState m) a), [Ref (PrimState m) a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Ref (PrimState m) a]
-> Maybe (Ref (PrimState m) a, [Ref (PrimState m) a])
forall a. [a] -> Maybe (a, [a])
uncons
      m () -> StateT [Ref (PrimState m) a] m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> StateT [Ref (PrimState m) a] m ())
-> m () -> StateT [Ref (PrimState m) a] m ()
forall a b. (a -> b) -> a -> b
$ (Ref (PrimState m) a -> m ())
-> Maybe (Ref (PrimState m) a) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Ref (PrimState m) a -> Ref (PrimState m) a -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> Ref s a -> m ()
moveRef Ref (PrimState m) a
r) Maybe (Ref (PrimState m) a)
x

-- | Default 'cloneRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
cloneTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => TraverseRef s f a -> m (TraverseRef s f a)
cloneTraverse :: TraverseRef s f a -> m (TraverseRef s f a)
cloneTraverse = (f (Ref s a) -> TraverseRef s f a)
-> m (f (Ref s a)) -> m (TraverseRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f (Ref s a) -> TraverseRef s f a
forall s (f :: * -> *) a. f (Ref s a) -> TraverseRef s f a
TraverseRef (m (f (Ref s a)) -> m (TraverseRef s f a))
-> (TraverseRef s f a -> m (f (Ref s a)))
-> TraverseRef s f a
-> m (TraverseRef s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ref s a -> m (Ref s a)) -> f (Ref s a) -> m (f (Ref s a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Ref s a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m (Ref s a)
cloneRef (f (Ref s a) -> m (f (Ref s a)))
-> (TraverseRef s f a -> f (Ref s a))
-> TraverseRef s f a
-> m (f (Ref s a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TraverseRef s f a -> f (Ref s a)
forall s (f :: * -> *) a. TraverseRef s f a -> f (Ref s a)
getTraverseRef

-- | Default 'unsafeThawRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
unsafeThawTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => f a -> m (TraverseRef s f a)
unsafeThawTraverse :: f a -> m (TraverseRef s f a)
unsafeThawTraverse = (f (Ref s a) -> TraverseRef s f a)
-> m (f (Ref s a)) -> m (TraverseRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f (Ref s a) -> TraverseRef s f a
forall s (f :: * -> *) a. f (Ref s a) -> TraverseRef s f a
TraverseRef (m (f (Ref s a)) -> m (TraverseRef s f a))
-> (f a -> m (f (Ref s a))) -> f a -> m (TraverseRef s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (Ref s a)) -> f a -> m (f (Ref s a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
unsafeThawRef

-- | Default 'unsafeFreezeRef' for 'TraverseRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'TraverseRef' as the
-- 'Ref'.  However, it can be useful if you are using a @'TraverseRef'
-- m f a@ just as a normal data type, independent of the 'Ref' class.  See
-- documentation for 'TraverseRef' for more information.
unsafeFreezeTraverse :: (Traversable f, Mutable s a, PrimMonad m, PrimState m ~ s) => TraverseRef s f a -> m (f a)
unsafeFreezeTraverse :: TraverseRef s f a -> m (f a)
unsafeFreezeTraverse = (Ref s a -> m a) -> f (Ref s a) -> m (f a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Ref s a -> m a
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
unsafeFreezeRef (f (Ref s a) -> m (f a))
-> (TraverseRef s f a -> f (Ref s a))
-> TraverseRef s f a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TraverseRef s f a -> f (Ref s a)
forall s (f :: * -> *) a. TraverseRef s f a -> f (Ref s a)
getTraverseRef

-- | A 'Ref' that works by using the 'Mutable' instance of an equivalent
-- type.  This is useful for newtype wrappers, so you can use the
-- underlying data type's 'Mutable' instance.
--
-- @
-- newtype MyVec = MyVec ('V.Vector' Double)
--
-- instance 'Mutable' s MyVec where
--     type 'Ref' s MyVec = 'CoerceRef' s s ('V.Vector' Double)
-- @
--
-- The @Ref s MyVec@ uses the a @'MV.MVector' Double@ under the hood.
--
-- It's essentially a special case of 'GRef' for newtypes.
newtype CoerceRef s b a = CoerceRef { CoerceRef s b a -> Ref s a
getCoerceRef :: Ref s a }

deriving instance Eq (Ref s a) => Eq (CoerceRef s b a)
deriving instance Ord (Ref s a) => Ord (CoerceRef s b a)

-- | Use a @'CoerceRef' s b a@ as if it were a @'Ref' s a@
instance X.IsoHKD (CoerceRef s b) a where
    type HKD (CoerceRef s b) a = Ref s a
    unHKD :: HKD (CoerceRef s b) a -> CoerceRef s b a
unHKD = HKD (CoerceRef s b) a -> CoerceRef s b a
forall k s (b :: k) a. Ref s a -> CoerceRef s b a
CoerceRef
    toHKD :: CoerceRef s b a -> HKD (CoerceRef s b) a
toHKD = CoerceRef s b a -> HKD (CoerceRef s b) a
forall s k (b :: k) a. CoerceRef s b a -> Ref s a
getCoerceRef

-- | Default 'thawRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
thawCoerce :: (Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) => b -> m (CoerceRef s b a)
thawCoerce :: b -> m (CoerceRef s b a)
thawCoerce = (Ref s a -> CoerceRef s b a) -> m (Ref s a) -> m (CoerceRef s b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s a -> CoerceRef s b a
forall k s (b :: k) a. Ref s a -> CoerceRef s b a
CoerceRef (m (Ref s a) -> m (CoerceRef s b a))
-> (b -> m (Ref s a)) -> b -> m (CoerceRef s b a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
thawRef (a -> m (Ref s a)) -> (b -> a) -> b -> m (Ref s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
forall a b. Coercible a b => a -> b
coerce

-- | Default 'freezeRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
freezeCoerce :: (Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) => CoerceRef s b a -> m b
freezeCoerce :: CoerceRef s b a -> m b
freezeCoerce = (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
forall a b. Coercible a b => a -> b
coerce (m a -> m b) -> (CoerceRef s b a -> m a) -> CoerceRef s b a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s a -> m a
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
freezeRef (Ref s a -> m a)
-> (CoerceRef s b a -> Ref s a) -> CoerceRef s b a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoerceRef s b a -> Ref s a
forall s k (b :: k) a. CoerceRef s b a -> Ref s a
getCoerceRef

-- | Default 'copyRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
copyCoerce :: (Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) => CoerceRef s b a -> b -> m ()
copyCoerce :: CoerceRef s b a -> b -> m ()
copyCoerce (CoerceRef r :: Ref s a
r) = Ref s a -> a -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> a -> m ()
copyRef Ref s a
r (a -> m ()) -> (b -> a) -> b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
forall a b. Coercible a b => a -> b
coerce

-- | Default 'moveRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
moveCoerce :: (Mutable s a, PrimMonad m, PrimState m ~ s) => CoerceRef s b a -> CoerceRef s b a -> m ()
moveCoerce :: CoerceRef s b a -> CoerceRef s b a -> m ()
moveCoerce (CoerceRef r :: Ref s a
r) (CoerceRef s :: Ref s a
s) = Ref s a -> Ref s a -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> Ref s a -> m ()
moveRef Ref s a
r Ref s a
s

-- | Default 'cloneRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
cloneCoerce :: (Mutable s a, PrimMonad m, PrimState m ~ s) => CoerceRef s b a -> m (CoerceRef s b a)
cloneCoerce :: CoerceRef s b a -> m (CoerceRef s b a)
cloneCoerce = (Ref s a -> CoerceRef s b a) -> m (Ref s a) -> m (CoerceRef s b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s a -> CoerceRef s b a
forall k s (b :: k) a. Ref s a -> CoerceRef s b a
CoerceRef (m (Ref s a) -> m (CoerceRef s b a))
-> (CoerceRef s b a -> m (Ref s a))
-> CoerceRef s b a
-> m (CoerceRef s b a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m (Ref s a)
cloneRef (Ref s a -> m (Ref s a))
-> (CoerceRef s b a -> Ref s a) -> CoerceRef s b a -> m (Ref s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoerceRef s b a -> Ref s a
forall s k (b :: k) a. CoerceRef s b a -> Ref s a
getCoerceRef

-- | Default 'unsafeThawRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
unsafeThawCoerce :: (Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) => b -> m (CoerceRef s b a)
unsafeThawCoerce :: b -> m (CoerceRef s b a)
unsafeThawCoerce = (Ref s a -> CoerceRef s b a) -> m (Ref s a) -> m (CoerceRef s b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s a -> CoerceRef s b a
forall k s (b :: k) a. Ref s a -> CoerceRef s b a
CoerceRef (m (Ref s a) -> m (CoerceRef s b a))
-> (b -> m (Ref s a)) -> b -> m (CoerceRef s b a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Ref s a)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
unsafeThawRef (a -> m (Ref s a)) -> (b -> a) -> b -> m (Ref s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
forall a b. Coercible a b => a -> b
coerce

-- | Default 'unsafeFreezeRef' for 'CoerceRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'CoerceRef' as the 'Ref'.
-- However, it can be useful if you are using a @'CoerceRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'CoerceRef' for more information.
unsafeFreezeCoerce :: (Coercible b a, Mutable s a, PrimMonad m, PrimState m ~ s) => CoerceRef s b a -> m b
unsafeFreezeCoerce :: CoerceRef s b a -> m b
unsafeFreezeCoerce = (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
forall a b. Coercible a b => a -> b
coerce (m a -> m b) -> (CoerceRef s b a -> m a) -> CoerceRef s b a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s a -> m a
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
unsafeFreezeRef (Ref s a -> m a)
-> (CoerceRef s b a -> Ref s a) -> CoerceRef s b a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoerceRef s b a -> Ref s a
forall s k (b :: k) a. CoerceRef s b a -> Ref s a
getCoerceRef

-- | A "'Ref'" that can be used to give a default 'Mutable' instance that
-- is immutable.  Nothing is allocated ever, all attempts to modify it will
-- be ignored, and 'freezeRef' will just get the original thawed value.
--
-- Really only exists to be used with 'Data.Mutable.Class.Immutable'.
newtype ImmutableRef s a = ImmutableRef { ImmutableRef s a -> a
getImmutableRef :: a }

-- | Use a @'ImmutableRef' a@ as if it were an @a@
instance X.IsoHKD (ImmutableRef s) a where
    type HKD (ImmutableRef s) a = a
    unHKD :: HKD (ImmutableRef s) a -> ImmutableRef s a
unHKD = HKD (ImmutableRef s) a -> ImmutableRef s a
forall k (s :: k) a. a -> ImmutableRef s a
ImmutableRef
    toHKD :: ImmutableRef s a -> HKD (ImmutableRef s) a
toHKD = ImmutableRef s a -> HKD (ImmutableRef s) a
forall k (s :: k) a. ImmutableRef s a -> a
getImmutableRef

-- | Default 'thawRef' for 'ImmutableRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'ImmutableRef' as the 'Ref'.
-- However, it can be useful if you are using a @'ImmutableRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'ImmutableRef' for more information.
thawImmutable :: Applicative m => a -> m (ImmutableRef s a)
thawImmutable :: a -> m (ImmutableRef s a)
thawImmutable = ImmutableRef s a -> m (ImmutableRef s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ImmutableRef s a -> m (ImmutableRef s a))
-> (a -> ImmutableRef s a) -> a -> m (ImmutableRef s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ImmutableRef s a
forall k (s :: k) a. a -> ImmutableRef s a
ImmutableRef

-- | Default 'freezeRef' for 'ImmutableRef'.  This will always return the
-- originally thawed value, ignoring all copies and writes.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'ImmutableRef' as the 'Ref'.
-- However, it can be useful if you are using a @'ImmutableRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'ImmutableRef' for more information.
freezeImmutable :: Applicative m => ImmutableRef s a -> m a
freezeImmutable :: ImmutableRef s a -> m a
freezeImmutable = a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (ImmutableRef s a -> a) -> ImmutableRef s a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImmutableRef s a -> a
forall k (s :: k) a. ImmutableRef s a -> a
getImmutableRef

-- | Default 'copyRef' for 'ImmutableRef'.  This is a no-op and does
-- nothing, since freezing will always return the originally thawed value.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'ImmutableRef' as the 'Ref'.
-- However, it can be useful if you are using a @'ImmutableRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'ImmutableRef' for more information.
copyImmutable :: Applicative m => ImmutableRef s a -> a -> m ()
copyImmutable :: ImmutableRef s a -> a -> m ()
copyImmutable _ _ = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- | Default 'moveRef' for 'ImmutableRef'.  This is a no-op and does
-- nothing, since freezing will always return the originally thawed value.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'ImmutableRef' as the 'Ref'.
-- However, it can be useful if you are using a @'ImmutableRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'ImmutableRef' for more information.
moveImmutable :: Applicative m => ImmutableRef s a -> ImmutableRef s a -> m ()
moveImmutable :: ImmutableRef s a -> ImmutableRef s a -> m ()
moveImmutable _ _ = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- | Default 'cloneRef' for 'ImmutableRef'.  'freezeRef' on this value will
-- return the originally thawed value.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'ImmutableRef' as the 'Ref'.
-- However, it can be useful if you are using a @'ImmutableRef' s b a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'ImmutableRef' for more information.
cloneImmutable :: Applicative m => ImmutableRef s a -> m (ImmutableRef s a)
cloneImmutable :: ImmutableRef s a -> m (ImmutableRef s a)
cloneImmutable = ImmutableRef s a -> m (ImmutableRef s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure



-- | Class for automatic generation of 'Ref' for 'Generic' instances.  See
-- 'GRef' for more information.
class GMutable s (f :: Type -> Type) where
    type GRef_ s f = (u :: Type -> Type) | u -> f

    gThawRef_         :: (PrimMonad m, PrimState m ~ s) => f a -> m (GRef_ s f a)
    gFreezeRef_       :: (PrimMonad m, PrimState m ~ s) => GRef_ s f a -> m (f a)
    gCopyRef_         :: (PrimMonad m, PrimState m ~ s) => GRef_ s f a -> f a -> m ()
    gMoveRef_         :: (PrimMonad m, PrimState m ~ s) => GRef_ s f a -> GRef_ s f a -> m ()
    gCloneRef_        :: (PrimMonad m, PrimState m ~ s) => GRef_ s f a -> m (GRef_ s f a)
    gUnsafeThawRef_   :: (PrimMonad m, PrimState m ~ s) => f a -> m (GRef_ s f a)
    gUnsafeFreezeRef_ :: (PrimMonad m, PrimState m ~ s) => GRef_ s f a -> m (f a)

instance Mutable s c => GMutable s (K1 i c) where
    type GRef_ s (K1 i c) = K1 i (Ref s c)

    gThawRef_ :: K1 i c a -> m (GRef_ s (K1 i c) a)
gThawRef_               = (Ref s c -> K1 i (Ref s c) a)
-> m (Ref s c) -> m (K1 i (Ref s c) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s c -> K1 i (Ref s c) a
forall k i c (p :: k). c -> K1 i c p
K1 (m (Ref s c) -> m (K1 i (Ref s c) a))
-> (K1 i c a -> m (Ref s c)) -> K1 i c a -> m (K1 i (Ref s c) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> m (Ref s c)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
thawRef (c -> m (Ref s c)) -> (K1 i c a -> c) -> K1 i c a -> m (Ref s c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i c a -> c
forall i c k (p :: k). K1 i c p -> c
unK1
    gFreezeRef_ :: GRef_ s (K1 i c) a -> m (K1 i c a)
gFreezeRef_             = (c -> K1 i c a) -> m c -> m (K1 i c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> K1 i c a
forall k i c (p :: k). c -> K1 i c p
K1 (m c -> m (K1 i c a))
-> (K1 i (Ref s c) a -> m c) -> K1 i (Ref s c) a -> m (K1 i c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s c -> m c
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
freezeRef (Ref s c -> m c)
-> (K1 i (Ref s c) a -> Ref s c) -> K1 i (Ref s c) a -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i (Ref s c) a -> Ref s c
forall i c k (p :: k). K1 i c p -> c
unK1
    gCopyRef_ :: GRef_ s (K1 i c) a -> K1 i c a -> m ()
gCopyRef_ (K1 v) (K1 x :: c
x) = Ref s c -> c -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> a -> m ()
copyRef Ref s c
v c
x
    gMoveRef_ :: GRef_ s (K1 i c) a -> GRef_ s (K1 i c) a -> m ()
gMoveRef_ (K1 v) (K1 u) = Ref s c -> Ref s c -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> Ref s a -> m ()
moveRef Ref s c
v Ref s c
u
    gCloneRef_ :: GRef_ s (K1 i c) a -> m (GRef_ s (K1 i c) a)
gCloneRef_              = (Ref s c -> K1 i (Ref s c) a)
-> m (Ref s c) -> m (K1 i (Ref s c) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s c -> K1 i (Ref s c) a
forall k i c (p :: k). c -> K1 i c p
K1 (m (Ref s c) -> m (K1 i (Ref s c) a))
-> (K1 i (Ref s c) a -> m (Ref s c))
-> K1 i (Ref s c) a
-> m (K1 i (Ref s c) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s c -> m (Ref s c)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m (Ref s a)
cloneRef (Ref s c -> m (Ref s c))
-> (K1 i (Ref s c) a -> Ref s c) -> K1 i (Ref s c) a -> m (Ref s c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i (Ref s c) a -> Ref s c
forall i c k (p :: k). K1 i c p -> c
unK1
    gUnsafeThawRef_ :: K1 i c a -> m (GRef_ s (K1 i c) a)
gUnsafeThawRef_         = (Ref s c -> K1 i (Ref s c) a)
-> m (Ref s c) -> m (K1 i (Ref s c) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ref s c -> K1 i (Ref s c) a
forall k i c (p :: k). c -> K1 i c p
K1 (m (Ref s c) -> m (K1 i (Ref s c) a))
-> (K1 i c a -> m (Ref s c)) -> K1 i c a -> m (K1 i (Ref s c) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> m (Ref s c)
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
a -> m (Ref s a)
unsafeThawRef (c -> m (Ref s c)) -> (K1 i c a -> c) -> K1 i c a -> m (Ref s c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i c a -> c
forall i c k (p :: k). K1 i c p -> c
unK1
    gUnsafeFreezeRef_ :: GRef_ s (K1 i c) a -> m (K1 i c a)
gUnsafeFreezeRef_       = (c -> K1 i c a) -> m c -> m (K1 i c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> K1 i c a
forall k i c (p :: k). c -> K1 i c p
K1 (m c -> m (K1 i c a))
-> (K1 i (Ref s c) a -> m c) -> K1 i (Ref s c) a -> m (K1 i c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s c -> m c
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> m a
unsafeFreezeRef (Ref s c -> m c)
-> (K1 i (Ref s c) a -> Ref s c) -> K1 i (Ref s c) a -> m c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i (Ref s c) a -> Ref s c
forall i c k (p :: k). K1 i c p -> c
unK1

instance GMutable s U1 where
    type GRef_ s U1 = U1

    gThawRef_ :: U1 a -> m (GRef_ s U1 a)
gThawRef_   _       = U1 a -> m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1
    gFreezeRef_ :: GRef_ s U1 a -> m (U1 a)
gFreezeRef_ _       = U1 a -> m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1
    gCopyRef_ :: GRef_ s U1 a -> U1 a -> m ()
gCopyRef_ _ _       = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    gMoveRef_ :: GRef_ s U1 a -> GRef_ s U1 a -> m ()
gMoveRef_ _ _       = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    gCloneRef_ :: GRef_ s U1 a -> m (GRef_ s U1 a)
gCloneRef_  _       = U1 a -> m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1
    gUnsafeThawRef_ :: U1 a -> m (GRef_ s U1 a)
gUnsafeThawRef_   _ = U1 a -> m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1
    gUnsafeFreezeRef_ :: GRef_ s U1 a -> m (U1 a)
gUnsafeFreezeRef_ _ = U1 a -> m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1

instance GMutable s V1 where
    type GRef_ s V1 = V1

    gThawRef_ :: V1 a -> m (GRef_ s V1 a)
gThawRef_         = \case {}
    gFreezeRef_ :: GRef_ s V1 a -> m (V1 a)
gFreezeRef_       = \case {}
    gCopyRef_ :: GRef_ s V1 a -> V1 a -> m ()
gCopyRef_         = \case {}
    gMoveRef_ :: GRef_ s V1 a -> GRef_ s V1 a -> m ()
gMoveRef_         = \case {}
    gCloneRef_ :: GRef_ s V1 a -> m (GRef_ s V1 a)
gCloneRef_        = \case {}
    gUnsafeThawRef_ :: V1 a -> m (GRef_ s V1 a)
gUnsafeThawRef_   = \case {}
    gUnsafeFreezeRef_ :: GRef_ s V1 a -> m (V1 a)
gUnsafeFreezeRef_ = \case {}

instance (GMutable s f, GMutable s g) => GMutable s (f :*: g) where
    type GRef_ s (f :*: g) = GRef_ s f :*: GRef_ s g

    gThawRef_ :: (:*:) f g a -> m (GRef_ s (f :*: g) a)
gThawRef_ (x :: f a
x :*: y :: g a
y)             = GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s f a)
-> m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ f a
x m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s g a) -> m ((:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ g a
y
    gFreezeRef_ :: GRef_ s (f :*: g) a -> m ((:*:) f g a)
gFreezeRef_ (v :*: u)           = f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (f a -> g a -> (:*:) f g a) -> m (f a) -> m (g a -> (:*:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ GRef_ s f a
v m (g a -> (:*:) f g a) -> m (g a) -> m ((:*:) f g a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GRef_ s g a -> m (g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ GRef_ s g a
u
    gCopyRef_ :: GRef_ s (f :*: g) a -> (:*:) f g a -> m ()
gCopyRef_ (v :*: u) (x :: f a
x :*: y :: g a
y)   = GRef_ s f a -> f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s f a
v f a
x m () -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> GRef_ s g a -> g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s g a
u g a
y
    gMoveRef_ :: GRef_ s (f :*: g) a -> GRef_ s (f :*: g) a -> m ()
gMoveRef_ (v :*: u) (v' :*: u') = GRef_ s f a -> GRef_ s f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s f a
v GRef_ s f a
v' m () -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> GRef_ s g a -> GRef_ s g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s g a
u GRef_ s g a
u'
    gCloneRef_ :: GRef_ s (f :*: g) a -> m (GRef_ s (f :*: g) a)
gCloneRef_ (v :*: u)            = GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s f a)
-> m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s f a
v m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s g a) -> m ((:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GRef_ s g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s g a
u
    gUnsafeThawRef_ :: (:*:) f g a -> m (GRef_ s (f :*: g) a)
gUnsafeThawRef_ (x :: f a
x :*: y :: g a
y)       = GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (GRef_ s f a -> GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s f a)
-> m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ f a
x m (GRef_ s g a -> (:*:) (GRef_ s f) (GRef_ s g) a)
-> m (GRef_ s g a) -> m ((:*:) (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ g a
y
    gUnsafeFreezeRef_ :: GRef_ s (f :*: g) a -> m ((:*:) f g a)
gUnsafeFreezeRef_ (v :*: u)     = f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (f a -> g a -> (:*:) f g a) -> m (f a) -> m (g a -> (:*:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ GRef_ s f a
v m (g a -> (:*:) f g a) -> m (g a) -> m ((:*:) f g a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GRef_ s g a -> m (g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ GRef_ s g a
u

instance GMutable s f => GMutable s (M1 i c f) where
    type GRef_ s (M1 i c f) = M1 i c (GRef_ s f)

    gThawRef_ :: M1 i c f a -> m (GRef_ s (M1 i c f) a)
gThawRef_               = (GRef_ s f a -> M1 i c (GRef_ s f) a)
-> m (GRef_ s f a) -> m (M1 i c (GRef_ s f) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s f a -> M1 i c (GRef_ s f) a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (m (GRef_ s f a) -> m (M1 i c (GRef_ s f) a))
-> (M1 i c f a -> m (GRef_ s f a))
-> M1 i c f a
-> m (M1 i c (GRef_ s f) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ (f a -> m (GRef_ s f a))
-> (M1 i c f a -> f a) -> M1 i c f a -> m (GRef_ s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f a -> f a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
    gFreezeRef_ :: GRef_ s (M1 i c f) a -> m (M1 i c f a)
gFreezeRef_             = (f a -> M1 i c f a) -> m (f a) -> m (M1 i c f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f a -> M1 i c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (m (f a) -> m (M1 i c f a))
-> (M1 i c (GRef_ s f) a -> m (f a))
-> M1 i c (GRef_ s f) a
-> m (M1 i c f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ (GRef_ s f a -> m (f a))
-> (M1 i c (GRef_ s f) a -> GRef_ s f a)
-> M1 i c (GRef_ s f) a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c (GRef_ s f) a -> GRef_ s f a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
    gCopyRef_ :: GRef_ s (M1 i c f) a -> M1 i c f a -> m ()
gCopyRef_ (M1 v) (M1 x :: f a
x) = GRef_ s f a -> f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s f a
v f a
x
    gMoveRef_ :: GRef_ s (M1 i c f) a -> GRef_ s (M1 i c f) a -> m ()
gMoveRef_ (M1 v) (M1 u) = GRef_ s f a -> GRef_ s f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s f a
v GRef_ s f a
u
    gCloneRef_ :: GRef_ s (M1 i c f) a -> m (GRef_ s (M1 i c f) a)
gCloneRef_ (M1 v)       = GRef_ s f a -> M1 i c (GRef_ s f) a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (GRef_ s f a -> M1 i c (GRef_ s f) a)
-> m (GRef_ s f a) -> m (M1 i c (GRef_ s f) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s f a
v
    gUnsafeThawRef_ :: M1 i c f a -> m (GRef_ s (M1 i c f) a)
gUnsafeThawRef_         = (GRef_ s f a -> M1 i c (GRef_ s f) a)
-> m (GRef_ s f a) -> m (M1 i c (GRef_ s f) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s f a -> M1 i c (GRef_ s f) a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (m (GRef_ s f a) -> m (M1 i c (GRef_ s f) a))
-> (M1 i c f a -> m (GRef_ s f a))
-> M1 i c f a
-> m (M1 i c (GRef_ s f) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ (f a -> m (GRef_ s f a))
-> (M1 i c f a -> f a) -> M1 i c f a -> m (GRef_ s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f a -> f a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
    gUnsafeFreezeRef_ :: GRef_ s (M1 i c f) a -> m (M1 i c f a)
gUnsafeFreezeRef_       = (f a -> M1 i c f a) -> m (f a) -> m (M1 i c f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f a -> M1 i c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (m (f a) -> m (M1 i c f a))
-> (M1 i c (GRef_ s f) a -> m (f a))
-> M1 i c (GRef_ s f) a
-> m (M1 i c f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ (GRef_ s f a -> m (f a))
-> (M1 i c (GRef_ s f) a -> GRef_ s f a)
-> M1 i c (GRef_ s f) a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c (GRef_ s f) a -> GRef_ s f a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1

-- | Wraps ':+:' in a mutable reference.  Used internally to represent
-- generic sum references.
newtype MutSumF s f g a = MutSumF { MutSumF s f g a -> MutVar s ((:+:) f g a)
getMutSumF :: MutVar s ((f :+: g) a) }

instance (GMutable s f, GMutable s g) => GMutable s (f :+: g) where
    type GRef_ s (f :+: g) = MutSumF s (GRef_ s f) (GRef_ s g)
    -- MutVar s :.: (GRef_ s f :+: GRef_ s g)

    gThawRef_ :: (:+:) f g a -> m (GRef_ s (f :+: g) a)
gThawRef_ = \case
      L1 x :: f a
x -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s f a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s f a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s f a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (GRef_ s f a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s f a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ f a
x
      R1 x :: g a
x -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s g a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s g a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s g a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (GRef_ s g a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s g a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ g a
x
    gFreezeRef_ :: GRef_ s (f :+: g) a -> m ((:+:) f g a)
gFreezeRef_ (MutSumF r) = MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
r m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ((:+:) f g a))
-> m ((:+:) f g a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      L1 v :: GRef_ s f a
v -> f a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (f a -> (:+:) f g a) -> m (f a) -> m ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ GRef_ s f a
v
      R1 u :: GRef_ s g a
u -> g a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (g a -> (:+:) f g a) -> m (g a) -> m ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s g a -> m (g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ GRef_ s g a
u
    gCopyRef_ :: GRef_ s (f :+: g) a -> (:+:) f g a -> m ()
gCopyRef_ (MutSumF r) xy :: (:+:) f g a
xy = MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
r m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      L1 v :: GRef_ s f a
v -> case (:+:) f g a
xy of
        L1 x :: f a
x -> GRef_ s f a -> f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s f a
v f a
x
        R1 y :: g a
y -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> (:+:) (GRef_ s f) (GRef_ s g) a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
r ((:+:) (GRef_ s f) (GRef_ s g) a -> m ())
-> (GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s g a
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (GRef_ s g a -> m ()) -> m (GRef_ s g a) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ g a
y
      R1 u :: GRef_ s g a
u -> case (:+:) f g a
xy of
        L1 x :: f a
x -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> (:+:) (GRef_ s f) (GRef_ s g) a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
r ((:+:) (GRef_ s f) (GRef_ s g) a -> m ())
-> (GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s f a
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (GRef_ s f a -> m ()) -> m (GRef_ s f a) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ f a
x
        R1 y :: g a
y -> GRef_ s g a -> g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s g a
u g a
y
    gMoveRef_ :: GRef_ s (f :+: g) a -> GRef_ s (f :+: g) a -> m ()
gMoveRef_ (MutSumF u) (MutSumF v) = MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
v m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      L1 vl :: GRef_ s f a
vl -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
u m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        L1 ul :: GRef_ s f a
ul -> GRef_ s f a -> GRef_ s f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s f a
ul GRef_ s f a
vl
        R1 _  -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> (:+:) (GRef_ s f) (GRef_ s g) a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
u ((:+:) (GRef_ s f) (GRef_ s g) a -> m ())
-> (GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s f a
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (GRef_ s f a -> m ()) -> m (GRef_ s f a) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< GRef_ s f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s f a
vl
      R1 vr :: GRef_ s g a
vr -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
u m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        L1 _  -> MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> (:+:) (GRef_ s f) (GRef_ s g) a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
u ((:+:) (GRef_ s f) (GRef_ s g) a -> m ())
-> (GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s g a
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (GRef_ s g a -> m ()) -> m (GRef_ s g a) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< GRef_ s g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s g a
vr
        R1 ur :: GRef_ s g a
ur -> GRef_ s g a -> GRef_ s g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s g a
ur GRef_ s g a
vr
    gCloneRef_ :: GRef_ s (f :+: g) a -> m (GRef_ s (f :+: g) a)
gCloneRef_ (MutSumF v) = MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
v m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a
    -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      L1 u :: GRef_ s f a
u -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s f a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s f a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s f a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (GRef_ s f a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s f a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< GRef_ s f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s f a
u
      R1 u :: GRef_ s g a
u -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s g a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s g a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s g a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (GRef_ s g a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s g a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< GRef_ s g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s g a
u
    gUnsafeThawRef_ :: (:+:) f g a -> m (GRef_ s (f :+: g) a)
gUnsafeThawRef_ = \case
      L1 x :: f a
x -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s f a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s f a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s f a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s f a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (GRef_ s f a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s f a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ f a
x
      R1 x :: g a
x -> (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
 -> MutSumF s (GRef_ s f) (GRef_ s g) a)
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
-> MutSumF s (GRef_ s f) (GRef_ s g) a
forall k s (f :: k -> *) (g :: k -> *) (a :: k).
MutVar s ((:+:) f g a) -> MutSumF s f g a
MutSumF (m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
 -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> (GRef_ s g a -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> GRef_ s g a
-> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:+:) (GRef_ s f) (GRef_ s g) a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar ((:+:) (GRef_ s f) (GRef_ s g) a
 -> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)))
-> (GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a)
-> GRef_ s g a
-> m (MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s g a -> (:+:) (GRef_ s f) (GRef_ s g) a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (GRef_ s g a -> m (MutSumF s (GRef_ s f) (GRef_ s g) a))
-> m (GRef_ s g a) -> m (MutSumF s (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< g a -> m (GRef_ s g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ g a
x
    gUnsafeFreezeRef_ :: GRef_ s (f :+: g) a -> m ((:+:) f g a)
gUnsafeFreezeRef_ (MutSumF r) = MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
-> m ((:+:) (GRef_ s f) (GRef_ s g) a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar s ((:+:) (GRef_ s f) (GRef_ s g) a)
MutVar (PrimState m) ((:+:) (GRef_ s f) (GRef_ s g) a)
r m ((:+:) (GRef_ s f) (GRef_ s g) a)
-> ((:+:) (GRef_ s f) (GRef_ s g) a -> m ((:+:) f g a))
-> m ((:+:) f g a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      L1 v :: GRef_ s f a
v -> f a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (f a -> (:+:) f g a) -> m (f a) -> m ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ GRef_ s f a
v
      R1 u :: GRef_ s g a
u -> g a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (g a -> (:+:) f g a) -> m (g a) -> m ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s g a -> m (g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ GRef_ s g a
u


-- | A 'Ref' for instances of 'GMutable', which are the "GHC.Generics"
-- combinators.
newtype GMutableRef s f a = GMutableRef { GMutableRef s f a -> GRef_ s f a
getGMutableRef :: GRef_ s f a }

deriving instance Eq (GRef_ s f a) => Eq (GMutableRef s f a)
deriving instance Ord (GRef_ s f a) => Ord (GMutableRef s f a)

thawGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => f a -> m (GMutableRef s f a)
thawGMutableRef :: f a -> m (GMutableRef s f a)
thawGMutableRef = (GRef_ s f a -> GMutableRef s f a)
-> m (GRef_ s f a) -> m (GMutableRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s f a -> GMutableRef s f a
forall s (f :: * -> *) a. GRef_ s f a -> GMutableRef s f a
GMutableRef (m (GRef_ s f a) -> m (GMutableRef s f a))
-> (f a -> m (GRef_ s f a)) -> f a -> m (GMutableRef s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_

freezeGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => GMutableRef s f a -> m (f a)
freezeGMutableRef :: GMutableRef s f a -> m (f a)
freezeGMutableRef = GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ (GRef_ s f a -> m (f a))
-> (GMutableRef s f a -> GRef_ s f a)
-> GMutableRef s f a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GMutableRef s f a -> GRef_ s f a
forall s (f :: * -> *) a. GMutableRef s f a -> GRef_ s f a
getGMutableRef

copyGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => GMutableRef s f a -> f a -> m ()
copyGMutableRef :: GMutableRef s f a -> f a -> m ()
copyGMutableRef (GMutableRef r :: GRef_ s f a
r) = GRef_ s f a -> f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s f a
r

moveGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef :: GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef (GMutableRef r :: GRef_ s f a
r) (GMutableRef s :: GRef_ s f a
s) = GRef_ s f a -> GRef_ s f a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s f a
r GRef_ s f a
s

cloneGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef :: GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef (GMutableRef r :: GRef_ s f a
r) = GRef_ s f a -> GMutableRef s f a
forall s (f :: * -> *) a. GRef_ s f a -> GMutableRef s f a
GMutableRef (GRef_ s f a -> GMutableRef s f a)
-> m (GRef_ s f a) -> m (GMutableRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s f a
r

unsafeThawGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => f a -> m (GMutableRef s f a)
unsafeThawGMutableRef :: f a -> m (GMutableRef s f a)
unsafeThawGMutableRef = (GRef_ s f a -> GMutableRef s f a)
-> m (GRef_ s f a) -> m (GMutableRef s f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s f a -> GMutableRef s f a
forall s (f :: * -> *) a. GRef_ s f a -> GMutableRef s f a
GMutableRef (m (GRef_ s f a) -> m (GMutableRef s f a))
-> (f a -> m (GRef_ s f a)) -> f a -> m (GMutableRef s f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> m (GRef_ s f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_

unsafeFreezeGMutableRef :: (GMutable s f, PrimMonad m, PrimState m ~ s) => GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef :: GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef = GRef_ s f a -> m (f a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ (GRef_ s f a -> m (f a))
-> (GMutableRef s f a -> GRef_ s f a)
-> GMutableRef s f a
-> m (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GMutableRef s f a -> GRef_ s f a
forall s (f :: * -> *) a. GMutableRef s f a -> GRef_ s f a
getGMutableRef

instance Mutable s c => Mutable s (K1 i c (a :: Type)) where
    type Ref s (K1 i c a) = GMutableRef s (K1 i c) a
    thawRef :: K1 i c a -> m (Ref s (K1 i c a))
thawRef         = K1 i c a -> m (Ref s (K1 i c a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
thawGMutableRef
    freezeRef :: Ref s (K1 i c a) -> m (K1 i c a)
freezeRef       = Ref s (K1 i c a) -> m (K1 i c a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
freezeGMutableRef
    copyRef :: Ref s (K1 i c a) -> K1 i c a -> m ()
copyRef         = Ref s (K1 i c a) -> K1 i c a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> f a -> m ()
copyGMutableRef
    moveRef :: Ref s (K1 i c a) -> Ref s (K1 i c a) -> m ()
moveRef         = Ref s (K1 i c a) -> Ref s (K1 i c a) -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef
    cloneRef :: Ref s (K1 i c a) -> m (Ref s (K1 i c a))
cloneRef        = Ref s (K1 i c a) -> m (Ref s (K1 i c a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef
    unsafeThawRef :: K1 i c a -> m (Ref s (K1 i c a))
unsafeThawRef   = K1 i c a -> m (Ref s (K1 i c a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
unsafeThawGMutableRef
    unsafeFreezeRef :: Ref s (K1 i c a) -> m (K1 i c a)
unsafeFreezeRef = Ref s (K1 i c a) -> m (K1 i c a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef

instance Mutable s (U1 (a :: Type)) where
    type Ref s (U1 a) = GMutableRef s U1 a
    thawRef :: U1 a -> m (Ref s (U1 a))
thawRef         = U1 a -> m (Ref s (U1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
thawGMutableRef
    freezeRef :: Ref s (U1 a) -> m (U1 a)
freezeRef       = Ref s (U1 a) -> m (U1 a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
freezeGMutableRef
    copyRef :: Ref s (U1 a) -> U1 a -> m ()
copyRef         = Ref s (U1 a) -> U1 a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> f a -> m ()
copyGMutableRef
    moveRef :: Ref s (U1 a) -> Ref s (U1 a) -> m ()
moveRef         = Ref s (U1 a) -> Ref s (U1 a) -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef
    cloneRef :: Ref s (U1 a) -> m (Ref s (U1 a))
cloneRef        = Ref s (U1 a) -> m (Ref s (U1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef
    unsafeThawRef :: U1 a -> m (Ref s (U1 a))
unsafeThawRef   = U1 a -> m (Ref s (U1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
unsafeThawGMutableRef
    unsafeFreezeRef :: Ref s (U1 a) -> m (U1 a)
unsafeFreezeRef = Ref s (U1 a) -> m (U1 a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef

instance Mutable s (V1 (a :: Type)) where
    type Ref s (V1 a) = GMutableRef s V1 a
    thawRef :: V1 a -> m (Ref s (V1 a))
thawRef         = V1 a -> m (Ref s (V1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
thawGMutableRef
    freezeRef :: Ref s (V1 a) -> m (V1 a)
freezeRef       = Ref s (V1 a) -> m (V1 a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
freezeGMutableRef
    copyRef :: Ref s (V1 a) -> V1 a -> m ()
copyRef         = Ref s (V1 a) -> V1 a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> f a -> m ()
copyGMutableRef
    moveRef :: Ref s (V1 a) -> Ref s (V1 a) -> m ()
moveRef         = Ref s (V1 a) -> Ref s (V1 a) -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef
    cloneRef :: Ref s (V1 a) -> m (Ref s (V1 a))
cloneRef        = Ref s (V1 a) -> m (Ref s (V1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef
    unsafeThawRef :: V1 a -> m (Ref s (V1 a))
unsafeThawRef   = V1 a -> m (Ref s (V1 a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
unsafeThawGMutableRef
    unsafeFreezeRef :: Ref s (V1 a) -> m (V1 a)
unsafeFreezeRef = Ref s (V1 a) -> m (V1 a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef

instance (GMutable s f, GMutable s g) => Mutable s ((f :*: g) a) where
    type Ref s ((f :*: g) a) = GMutableRef s (f :*: g) a
    thawRef :: (:*:) f g a -> m (Ref s ((:*:) f g a))
thawRef         = (:*:) f g a -> m (Ref s ((:*:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
thawGMutableRef
    freezeRef :: Ref s ((:*:) f g a) -> m ((:*:) f g a)
freezeRef       = Ref s ((:*:) f g a) -> m ((:*:) f g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
freezeGMutableRef
    copyRef :: Ref s ((:*:) f g a) -> (:*:) f g a -> m ()
copyRef         = Ref s ((:*:) f g a) -> (:*:) f g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> f a -> m ()
copyGMutableRef
    moveRef :: Ref s ((:*:) f g a) -> Ref s ((:*:) f g a) -> m ()
moveRef         = Ref s ((:*:) f g a) -> Ref s ((:*:) f g a) -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef
    cloneRef :: Ref s ((:*:) f g a) -> m (Ref s ((:*:) f g a))
cloneRef        = Ref s ((:*:) f g a) -> m (Ref s ((:*:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef
    unsafeThawRef :: (:*:) f g a -> m (Ref s ((:*:) f g a))
unsafeThawRef   = (:*:) f g a -> m (Ref s ((:*:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
unsafeThawGMutableRef
    unsafeFreezeRef :: Ref s ((:*:) f g a) -> m ((:*:) f g a)
unsafeFreezeRef = Ref s ((:*:) f g a) -> m ((:*:) f g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef

instance (GMutable s f, GMutable s g) => Mutable s ((f :+: g) a) where
    type Ref s ((f :+: g) a) = GMutableRef s (f :+: g) a
    thawRef :: (:+:) f g a -> m (Ref s ((:+:) f g a))
thawRef         = (:+:) f g a -> m (Ref s ((:+:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
thawGMutableRef
    freezeRef :: Ref s ((:+:) f g a) -> m ((:+:) f g a)
freezeRef       = Ref s ((:+:) f g a) -> m ((:+:) f g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
freezeGMutableRef
    copyRef :: Ref s ((:+:) f g a) -> (:+:) f g a -> m ()
copyRef         = Ref s ((:+:) f g a) -> (:+:) f g a -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> f a -> m ()
copyGMutableRef
    moveRef :: Ref s ((:+:) f g a) -> Ref s ((:+:) f g a) -> m ()
moveRef         = Ref s ((:+:) f g a) -> Ref s ((:+:) f g a) -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> GMutableRef s f a -> m ()
moveGMutableRef
    cloneRef :: Ref s ((:+:) f g a) -> m (Ref s ((:+:) f g a))
cloneRef        = Ref s ((:+:) f g a) -> m (Ref s ((:+:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (GMutableRef s f a)
cloneGMutableRef
    unsafeThawRef :: (:+:) f g a -> m (Ref s ((:+:) f g a))
unsafeThawRef   = (:+:) f g a -> m (Ref s ((:+:) f g a))
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GMutableRef s f a)
unsafeThawGMutableRef
    unsafeFreezeRef :: Ref s ((:+:) f g a) -> m ((:+:) f g a)
unsafeFreezeRef = Ref s ((:+:) f g a) -> m ((:+:) f g a)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GMutableRef s f a -> m (f a)
unsafeFreezeGMutableRef


-- | Automatically generate a piecewise mutable reference for any 'Generic'
-- instance.
--
-- @
-- -- | any 'Generic' instance
-- data MyType = MyType { mtInt :: Int, mtDouble :: Double }
--   deriving (Generic, Show)
--
-- instance Mutable s MyType where
--     type Ref s MyType = 'GRef' s MyType
-- @
--
-- @
-- ghci> r <- 'thawRef' (MyType 3 4.5)
-- ghci> 'freezeRef' r
-- MyType 3 4.5
-- ghci> 'Data.Mutable.Parts.freezePart' ('Data.Mutable.Parts.fieldMut' #mtInt) r
-- 3
-- ghci> 'Data.Mutable.Parts.copyPart' (fieldMut #mtDouble) 1.23
-- ghci> freezeRef r
-- MyType 3 1.23
-- @
--
-- Note that this is basically just a bunch of tupled refs for a product
-- type.  For a sum type (with multiple constructors), an extra layer of
-- indirection is added to account for the dynamically changable shape.
--
-- See "Data.Mutable.Parts" and "Data.Mutable.Branches" for nice ways to
-- inspect and mutate the internals of this type (as demonstrated above).
--
-- If the facilities in those modules are not adequate, you can also
-- manually crack open 'GRef' and work with the internals.  Getting the
-- /type/ of @'unGRef' \@MyType@ should allow you to navigate what is going
-- on, if you are familiar with "GHC.Generics".  However, ideally, you
-- would never need to do this.
newtype GRef s a = GRef { GRef s a -> GRef_ s (Rep a) ()
unGRef :: GRef_ s (Rep a) () }

deriving instance Eq (GRef_ s (Rep a) ()) => Eq (GRef s a)
deriving instance Ord (GRef_ s (Rep a) ()) => Ord (GRef s a)

-- | Default 'thawRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gThawRef
    :: (Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => a
    -> m (GRef s a)
gThawRef :: a -> m (GRef s a)
gThawRef = (GRef_ s (Rep a) () -> GRef s a)
-> m (GRef_ s (Rep a) ()) -> m (GRef s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s (Rep a) () -> GRef s a
forall s a. GRef_ s (Rep a) () -> GRef s a
GRef (m (GRef_ s (Rep a) ()) -> m (GRef s a))
-> (a -> m (GRef_ s (Rep a) ())) -> a -> m (GRef s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a () -> m (GRef_ s (Rep a) ())
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ (Rep a () -> m (GRef_ s (Rep a) ()))
-> (a -> Rep a ()) -> a -> m (GRef_ s (Rep a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a ()
forall a x. Generic a => a -> Rep a x
from

-- | Default 'freezeRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gFreezeRef
    :: (Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => GRef s a
    -> m a
gFreezeRef :: GRef s a -> m a
gFreezeRef = (Rep a () -> a) -> m (Rep a ()) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep a () -> a
forall a x. Generic a => Rep a x -> a
to (m (Rep a ()) -> m a)
-> (GRef s a -> m (Rep a ())) -> GRef s a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s (Rep a) () -> m (Rep a ())
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ (GRef_ s (Rep a) () -> m (Rep a ()))
-> (GRef s a -> GRef_ s (Rep a) ()) -> GRef s a -> m (Rep a ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef s a -> GRef_ s (Rep a) ()
forall s a. GRef s a -> GRef_ s (Rep a) ()
unGRef

-- | Default 'copyRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gCopyRef
    :: (Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => GRef s a
    -> a
    -> m ()
gCopyRef :: GRef s a -> a -> m ()
gCopyRef (GRef v :: GRef_ s (Rep a) ()
v) x :: a
x = GRef_ s (Rep a) () -> Rep a () -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ GRef_ s (Rep a) ()
v (a -> Rep a ()
forall a x. Generic a => a -> Rep a x
from a
x)

-- | Default 'moveRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gMoveRef
    :: (GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => GRef s a
    -> GRef s a
    -> m ()
gMoveRef :: GRef s a -> GRef s a -> m ()
gMoveRef (GRef v :: GRef_ s (Rep a) ()
v) (GRef u :: GRef_ s (Rep a) ()
u) = GRef_ s (Rep a) () -> GRef_ s (Rep a) () -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ GRef_ s (Rep a) ()
v GRef_ s (Rep a) ()
u

-- | Default 'cloneRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gCloneRef
    :: (GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => GRef s a
    -> m (GRef s a)
gCloneRef :: GRef s a -> m (GRef s a)
gCloneRef (GRef v :: GRef_ s (Rep a) ()
v) = GRef_ s (Rep a) () -> GRef s a
forall s a. GRef_ s (Rep a) () -> GRef s a
GRef (GRef_ s (Rep a) () -> GRef s a)
-> m (GRef_ s (Rep a) ()) -> m (GRef s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GRef_ s (Rep a) () -> m (GRef_ s (Rep a) ())
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ GRef_ s (Rep a) ()
v

-- | Default 'unsafeThawRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gUnsafeThawRef
    :: (Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => a
    -> m (GRef s a)
gUnsafeThawRef :: a -> m (GRef s a)
gUnsafeThawRef = (GRef_ s (Rep a) () -> GRef s a)
-> m (GRef_ s (Rep a) ()) -> m (GRef s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GRef_ s (Rep a) () -> GRef s a
forall s a. GRef_ s (Rep a) () -> GRef s a
GRef (m (GRef_ s (Rep a) ()) -> m (GRef s a))
-> (a -> m (GRef_ s (Rep a) ())) -> a -> m (GRef s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a () -> m (GRef_ s (Rep a) ())
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ (Rep a () -> m (GRef_ s (Rep a) ()))
-> (a -> Rep a ()) -> a -> m (GRef_ s (Rep a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a ()
forall a x. Generic a => a -> Rep a x
from

-- | Default 'unsafeFreezeRef' for 'GRef'.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with 'GRef' as the 'Ref'.
-- However, it can be useful if you are using a @'GRef' s a@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'GRef' for more information.
gUnsafeFreezeRef
    :: (Generic a, GMutable s (Rep a), PrimMonad m, PrimState m ~ s)
    => GRef s a
    -> m a
gUnsafeFreezeRef :: GRef s a -> m a
gUnsafeFreezeRef = (Rep a () -> a) -> m (Rep a ()) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep a () -> a
forall a x. Generic a => Rep a x -> a
to (m (Rep a ()) -> m a)
-> (GRef s a -> m (Rep a ())) -> GRef s a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s (Rep a) () -> m (Rep a ())
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ (GRef_ s (Rep a) () -> m (Rep a ()))
-> (GRef s a -> GRef_ s (Rep a) ()) -> GRef s a -> m (Rep a ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef s a -> GRef_ s (Rep a) ()
forall s a. GRef s a -> GRef_ s (Rep a) ()
unGRef


-- | Default 'thawRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
thawHKD
    :: forall z m s.
    ( Generic (z Identity)
    , Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z Identity
    -> m (z (RefFor s))
thawHKD :: z Identity -> m (z (RefFor s))
thawHKD = (Rep (z (RefFor s)) Any -> z (RefFor s))
-> m (Rep (z (RefFor s)) Any) -> m (z (RefFor s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep (z (RefFor s)) Any -> z (RefFor s)
forall a x. Generic a => Rep a x -> a
to (m (Rep (z (RefFor s)) Any) -> m (z (RefFor s)))
-> (z Identity -> m (Rep (z (RefFor s)) Any))
-> z Identity
-> m (z (RefFor s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep (z Identity) Any -> m (Rep (z (RefFor s)) Any)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gThawRef_ (Rep (z Identity) Any -> m (Rep (z (RefFor s)) Any))
-> (z Identity -> Rep (z Identity) Any)
-> z Identity
-> m (Rep (z (RefFor s)) Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z Identity -> Rep (z Identity) Any
forall a x. Generic a => a -> Rep a x
from

-- | Default 'freezeRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
freezeHKD
    :: forall z m s.
    ( Generic (z Identity)
    , Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z (RefFor s)
    -> m (z Identity)
freezeHKD :: z (RefFor s) -> m (z Identity)
freezeHKD = (Rep (z Identity) Any -> z Identity)
-> m (Rep (z Identity) Any) -> m (z Identity)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep (z Identity) Any -> z Identity
forall a x. Generic a => Rep a x -> a
to (m (Rep (z Identity) Any) -> m (z Identity))
-> (z (RefFor s) -> m (Rep (z Identity) Any))
-> z (RefFor s)
-> m (z Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep (z (RefFor s)) Any -> m (Rep (z Identity) Any)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gFreezeRef_ (Rep (z (RefFor s)) Any -> m (Rep (z Identity) Any))
-> (z (RefFor s) -> Rep (z (RefFor s)) Any)
-> z (RefFor s)
-> m (Rep (z Identity) Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from

-- | Default 'copyRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
copyHKD
    :: forall z m s.
    ( Generic (z Identity)
    , Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z (RefFor s)
    -> z Identity
    -> m ()
copyHKD :: z (RefFor s) -> z Identity -> m ()
copyHKD r :: z (RefFor s)
r x :: z Identity
x = GRef_ s (Rep (z Identity)) Any -> Rep (z Identity) Any -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> f a -> m ()
gCopyRef_ (z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from z (RefFor s)
r) (z Identity -> Rep (z Identity) Any
forall a x. Generic a => a -> Rep a x
from z Identity
x)

-- | Default 'moveRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
moveHKD
    :: forall z m s.
    ( Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z (RefFor s)
    -> z (RefFor s)
    -> m ()
moveHKD :: z (RefFor s) -> z (RefFor s) -> m ()
moveHKD r :: z (RefFor s)
r x :: z (RefFor s)
x = GRef_ s (Rep (z Identity)) Any
-> GRef_ s (Rep (z Identity)) Any -> m ()
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> GRef_ s f a -> m ()
gMoveRef_ (z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from z (RefFor s)
r) (z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from z (RefFor s)
x)

-- | Default 'cloneRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
cloneHKD
    :: forall z m s.
    ( Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z (RefFor s)
    -> m (z (RefFor s))
cloneHKD :: z (RefFor s) -> m (z (RefFor s))
cloneHKD = (Rep (z (RefFor s)) Any -> z (RefFor s))
-> m (Rep (z (RefFor s)) Any) -> m (z (RefFor s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep (z (RefFor s)) Any -> z (RefFor s)
forall a x. Generic a => Rep a x -> a
to (m (Rep (z (RefFor s)) Any) -> m (z (RefFor s)))
-> (z (RefFor s) -> m (Rep (z (RefFor s)) Any))
-> z (RefFor s)
-> m (z (RefFor s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef_ s (Rep (z Identity)) Any -> m (Rep (z (RefFor s)) Any)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (GRef_ s f a)
gCloneRef_ (GRef_ s (Rep (z Identity)) Any -> m (Rep (z (RefFor s)) Any))
-> (z (RefFor s) -> GRef_ s (Rep (z Identity)) Any)
-> z (RefFor s)
-> m (Rep (z (RefFor s)) Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> GRef_ s (Rep (z Identity)) Any
forall a x. Generic a => a -> Rep a x
from

-- | Default 'unsafeThawRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
unsafeThawHKD
    :: forall z m s.
    ( Generic (z Identity)
    , Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z Identity
    -> m (z (RefFor s))
unsafeThawHKD :: z Identity -> m (z (RefFor s))
unsafeThawHKD = (Rep (z (RefFor s)) Any -> z (RefFor s))
-> m (Rep (z (RefFor s)) Any) -> m (z (RefFor s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep (z (RefFor s)) Any -> z (RefFor s)
forall a x. Generic a => Rep a x -> a
to (m (Rep (z (RefFor s)) Any) -> m (z (RefFor s)))
-> (z Identity -> m (Rep (z (RefFor s)) Any))
-> z Identity
-> m (z (RefFor s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep (z Identity) Any -> m (Rep (z (RefFor s)) Any)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
f a -> m (GRef_ s f a)
gUnsafeThawRef_ (Rep (z Identity) Any -> m (Rep (z (RefFor s)) Any))
-> (z Identity -> Rep (z Identity) Any)
-> z Identity
-> m (Rep (z (RefFor s)) Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z Identity -> Rep (z Identity) Any
forall a x. Generic a => a -> Rep a x
from

-- | Default 'unsafeFreezeRef' for the higher-kinded data pattern, a la
-- <https://reasonablypolymorphic.com/blog/higher-kinded-data/>.
--
-- You likely won't ever use this directly, since it is automatically
-- provided if you have a 'Mutable' instance with @z ('RefFor' s)@ as the 'Ref'.
-- However, it can be useful if you are using a @z ('RefFor' s)@ just as
-- a normal data type, independent of the 'Ref' class.  See documentation
-- for 'Mutable' for more information.
unsafeFreezeHKD
    :: forall z m s.
    ( Generic (z Identity)
    , Generic (z (RefFor s))
    , GMutable s (Rep (z Identity))
    , GRef_ s (Rep (z Identity)) ~ Rep (z (RefFor s))
    , PrimMonad m
    , PrimState m ~ s
    )
    => z (RefFor s)
    -> m (z Identity)
unsafeFreezeHKD :: z (RefFor s) -> m (z Identity)
unsafeFreezeHKD = (Rep (z Identity) Any -> z Identity)
-> m (Rep (z Identity) Any) -> m (z Identity)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Rep (z Identity) Any -> z Identity
forall a x. Generic a => Rep a x -> a
to (m (Rep (z Identity) Any) -> m (z Identity))
-> (z (RefFor s) -> m (Rep (z Identity) Any))
-> z (RefFor s)
-> m (z Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep (z (RefFor s)) Any -> m (Rep (z Identity) Any)
forall s (f :: * -> *) (m :: * -> *) a.
(GMutable s f, PrimMonad m, PrimState m ~ s) =>
GRef_ s f a -> m (f a)
gUnsafeFreezeRef_ (Rep (z (RefFor s)) Any -> m (Rep (z Identity) Any))
-> (z (RefFor s) -> Rep (z (RefFor s)) Any)
-> z (RefFor s)
-> m (Rep (z Identity) Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from

-- | Useful type family to @'Ref' m@ over every item in a type-level list
--
-- @
-- ghci> :kind! MapRef IO '[Int, V.Vector Double]
-- '[ MutVar RealWorld Int, MVector RealWorld Double ]
-- @
type family MapRef s as where
    MapRef s '[] = '[]
    MapRef s (a ': as) = Ref s a ': MapRef s as

class ListRefTuple (s :: Type) (b :: Type) (as :: [Type]) | as s -> b where
    tupledRef :: GL.Iso' (HList (MapRef s as)) b
    tupledRef = (HList (MapRef s as) -> b)
-> (b -> HList (MapRef s as))
-> Iso (HList (MapRef s as)) (HList (MapRef s as)) b b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
GL.iso (ListRefTuple s b as => HList (MapRef s as) -> b
forall s b (as :: [*]).
ListRefTuple s b as =>
HList (MapRef s as) -> b
listRefToTuple @s @b @as) (ListRefTuple s b as => b -> HList (MapRef s as)
forall s b (as :: [*]).
ListRefTuple s b as =>
b -> HList (MapRef s as)
tupleToListRef @s @b @as)
    {-# INLINE tupledRef #-}
    tupleToListRef :: b -> HList (MapRef s as)
    listRefToTuple :: HList (MapRef s as) -> b