{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Mutable.Parts (
MutPart(..)
, withPart
, freezePart, copyPart
, movePartInto, movePartOver, movePartWithin
, clonePart, unsafeFreezePart
, modifyPart, modifyPart'
, updatePart, updatePart'
, modifyPartM, modifyPartM'
, updatePartM, updatePartM'
, compMP
, idMP
, mutFst, mutSnd
, FieldMut(..), withField, mutField, Label(..)
, PosMut(..), withPos, mutPos
, TupleMut(..), withTuple
, hkdMutParts, HKDMutParts
, mutRec
, coerceRef, withCoerceRef
, MapRef
) where
import Control.Monad.Primitive
import Data.Coerce
import Data.Generics.Product.Internal.HList
import Data.Kind
import Data.Mutable.Class
import Data.Mutable.Instances
import Data.Vinyl hiding (HList)
import Data.Vinyl.Functor
import GHC.Generics
import GHC.TypeLits
import qualified Control.Category as C
import qualified Data.GenericLens.Internal as GL
import qualified Data.Generics.Internal.Profunctor.Lens as GLP
import qualified Data.Generics.Product.Fields as GL
import qualified Data.Generics.Product.Internal.GLens as GL
import qualified Data.Generics.Product.Internal.Positions as GL
import qualified Data.Generics.Product.Positions as GL
import qualified Data.Vinyl.TypeLevel as V
import qualified Data.Vinyl.XRec as X
newtype MutPart s b a = MutPart { MutPart s b a -> Ref s b -> Ref s a
getMutPart :: Ref s b -> Ref s a }
compMP :: MutPart s a b -> MutPart s b c -> MutPart s a c
compMP :: MutPart s a b -> MutPart s b c -> MutPart s a c
compMP (MutPart Ref s a -> Ref s b
f) (MutPart Ref s b -> Ref s c
g) = (Ref s a -> Ref s c) -> MutPart s a c
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart (Ref s b -> Ref s c
g (Ref s b -> Ref s c) -> (Ref s a -> Ref s b) -> Ref s a -> Ref s c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ref s a -> Ref s b
f)
infixr 9 `compMP`
idMP :: MutPart s a a
idMP :: MutPart s a a
idMP = (Ref s a -> Ref s a) -> MutPart s a a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart Ref s a -> Ref s a
forall a. a -> a
id
instance C.Category (MutPart s) where
id :: MutPart s a a
id = MutPart s a a
forall s a. MutPart s a a
idMP
. :: MutPart s b c -> MutPart s a b -> MutPart s a c
(.) = (MutPart s a b -> MutPart s b c -> MutPart s a c)
-> MutPart s b c -> MutPart s a b -> MutPart s a c
forall a b c. (a -> b -> c) -> b -> a -> c
flip MutPart s a b -> MutPart s b c -> MutPart s a c
forall s a b c. MutPart s a b -> MutPart s b c -> MutPart s a c
compMP
instance X.IsoHKD (MutPart s b) a
mutFst :: MutPart s (a, b) a
mutFst :: MutPart s (a, b) a
mutFst = (Ref s (a, b) -> Ref s a) -> MutPart s (a, b) a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart Ref s (a, b) -> Ref s a
forall a b. (a, b) -> a
fst
mutSnd :: MutPart s (a, b) b
mutSnd :: MutPart s (a, b) b
mutSnd = (Ref s (a, b) -> Ref s b) -> MutPart s (a, b) b
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart Ref s (a, b) -> Ref s b
forall a b. (a, b) -> b
snd
withPart
:: MutPart s b a
-> Ref s b
-> (Ref s a -> m r)
-> m r
withPart :: MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
withPart MutPart s b a
mp Ref s b
x Ref s a -> m r
f = Ref s a -> m r
f (MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp Ref s b
x)
freezePart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> m a
freezePart :: MutPart s b a -> Ref s b -> m a
freezePart MutPart s b a
mp = 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) -> (Ref s b -> Ref s a) -> Ref s b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
copyPart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> a
-> m ()
copyPart :: MutPart s b a -> Ref s b -> a -> m ()
copyPart MutPart s b a
mp = 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 -> a -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
movePartInto
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> Ref s a
-> m ()
movePartInto :: MutPart s b a -> Ref s b -> Ref s a -> m ()
movePartInto MutPart s b a
mp = 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 -> Ref s a -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> Ref s a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
movePartOver
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s a
-> Ref s b
-> m ()
movePartOver :: MutPart s b a -> Ref s a -> Ref s b -> m ()
movePartOver MutPart s b a
mp Ref s a
r = 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 -> m ()) -> (Ref s b -> Ref s a) -> Ref s b -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
movePartWithin
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> Ref s b
-> m ()
movePartWithin :: MutPart s b a -> Ref s b -> Ref s b -> m ()
movePartWithin MutPart s b a
mp Ref s b
r Ref s b
v = 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 (MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp Ref s b
r) (MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp Ref s b
v)
clonePart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> m (Ref s a)
clonePart :: MutPart s b a -> Ref s b -> m (Ref s a)
clonePart MutPart s b a
mp = 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))
-> (Ref s b -> Ref s a) -> Ref s b -> m (Ref s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
unsafeFreezePart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> m a
unsafeFreezePart :: MutPart s b a -> Ref s b -> m a
unsafeFreezePart MutPart s b a
mp = 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) -> (Ref s b -> Ref s a) -> Ref s b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
modifyPart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> a)
-> m ()
modifyPart :: MutPart s b a -> Ref s b -> (a -> a) -> m ()
modifyPart MutPart s b a
mp = Ref s a -> (a -> a) -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> a) -> m ()
modifyRef (Ref s a -> (a -> a) -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> a) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
modifyPart'
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> a)
-> m ()
modifyPart' :: MutPart s b a -> Ref s b -> (a -> a) -> m ()
modifyPart' MutPart s b a
mp = Ref s a -> (a -> a) -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> a) -> m ()
modifyRef' (Ref s a -> (a -> a) -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> a) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
updatePart
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> (a, r))
-> m r
updatePart :: MutPart s b a -> Ref s b -> (a -> (a, r)) -> m r
updatePart MutPart s b a
mp = Ref s a -> (a -> (a, r)) -> m r
forall s a (m :: * -> *) b.
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> (a, b)) -> m b
updateRef (Ref s a -> (a -> (a, r)) -> m r)
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> (a, r)) -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
updatePart'
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> (a, r))
-> m r
updatePart' :: MutPart s b a -> Ref s b -> (a -> (a, r)) -> m r
updatePart' MutPart s b a
mp = Ref s a -> (a -> (a, r)) -> m r
forall s a (m :: * -> *) b.
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> (a, b)) -> m b
updateRef' (Ref s a -> (a -> (a, r)) -> m r)
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> (a, r)) -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
modifyPartM
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> m a)
-> m ()
modifyPartM :: MutPart s b a -> Ref s b -> (a -> m a) -> m ()
modifyPartM MutPart s b a
mp = Ref s a -> (a -> m a) -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> m a) -> m ()
modifyRefM (Ref s a -> (a -> m a) -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> m a) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
modifyPartM'
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> m a)
-> m ()
modifyPartM' :: MutPart s b a -> Ref s b -> (a -> m a) -> m ()
modifyPartM' MutPart s b a
mp = Ref s a -> (a -> m a) -> m ()
forall s a (m :: * -> *).
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> m a) -> m ()
modifyRefM' (Ref s a -> (a -> m a) -> m ())
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> m a) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
updatePartM
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> m (a, r))
-> m r
updatePartM :: MutPart s b a -> Ref s b -> (a -> m (a, r)) -> m r
updatePartM MutPart s b a
mp = Ref s a -> (a -> m (a, r)) -> m r
forall s a (m :: * -> *) b.
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> m (a, b)) -> m b
updateRefM (Ref s a -> (a -> m (a, r)) -> m r)
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> m (a, r)) -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
updatePartM'
:: (Mutable s a, PrimMonad m, PrimState m ~ s)
=> MutPart s b a
-> Ref s b
-> (a -> m (a, r))
-> m r
updatePartM' :: MutPart s b a -> Ref s b -> (a -> m (a, r)) -> m r
updatePartM' MutPart s b a
mp = Ref s a -> (a -> m (a, r)) -> m r
forall s a (m :: * -> *) b.
(Mutable s a, PrimMonad m, PrimState m ~ s) =>
Ref s a -> (a -> m (a, b)) -> m b
updateRefM' (Ref s a -> (a -> m (a, r)) -> m r)
-> (Ref s b -> Ref s a) -> Ref s b -> (a -> m (a, r)) -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart MutPart s b a
mp
mutRec
:: forall a as f rec s.
( Ref s (rec f as) ~ rec (RecRef s f) as
, RecElem rec a a as as (V.RIndex a as)
, RecElemFCtx rec (RecRef s f)
)
=> MutPart s (rec f as) (f a)
mutRec :: MutPart s (rec f as) (f a)
mutRec = (Ref s (rec f as) -> Ref s (f a)) -> MutPart s (rec f as) (f a)
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart ((Ref s (rec f as) -> Ref s (f a)) -> MutPart s (rec f as) (f a))
-> (Ref s (rec f as) -> Ref s (f a)) -> MutPart s (rec f as) (f a)
forall a b. (a -> b) -> a -> b
$ RecRef s f a -> Ref s (f a)
forall s k (f :: k -> *) (a :: k). RecRef s f a -> Ref s (f a)
getRecRef (RecRef s f a -> Ref s (f a))
-> (rec (RecRef s f) as -> RecRef s f a)
-> rec (RecRef s f) as
-> Ref s (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RecElem rec a a as as (RIndex a as),
RecElemFCtx rec (RecRef s f)) =>
rec (RecRef s f) as -> RecRef s f a
forall k (r :: k) (rs :: [k]) (f :: k -> *)
(record :: (k -> *) -> [k] -> *).
(RecElem record r r rs rs (RIndex r rs), RecElemFCtx record f) =>
record f rs -> f r
rget @a @as @(RecRef s f) @rec
coerceRef :: (Ref s b ~ CoerceRef s b a) => MutPart s b a
coerceRef :: MutPart s b a
coerceRef = (Ref s b -> Ref s a) -> MutPart s b a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart Ref s b -> Ref s a
coerce
withCoerceRef
:: CoerceRef s b a
-> (Ref s a -> m r)
-> m r
withCoerceRef :: CoerceRef s b a -> (Ref s a -> m r) -> m r
withCoerceRef CoerceRef s b a
x Ref s a -> m r
f = Ref s a -> m r
f (CoerceRef s b a -> Ref s a
coerce CoerceRef s b a
x)
class (Mutable s (z Identity), Ref s (z Identity) ~ z (RefFor s)) => HKDMutParts s z i o where
hkdMutParts_ :: (z (RefFor s) -> i a) -> o a
instance (Mutable s (z Identity), Ref s (z Identity) ~ z (RefFor s)) => HKDMutParts s z (K1 i (RefFor s c)) (K1 i (MutPart s (z Identity) c)) where
hkdMutParts_ :: (z (RefFor s) -> K1 i (RefFor s c) a)
-> K1 i (MutPart s (z Identity) c) a
hkdMutParts_ z (RefFor s) -> K1 i (RefFor s c) a
f = MutPart s (z Identity) c -> K1 i (MutPart s (z Identity) c) a
forall k i c (p :: k). c -> K1 i c p
K1 (MutPart s (z Identity) c -> K1 i (MutPart s (z Identity) c) a)
-> MutPart s (z Identity) c -> K1 i (MutPart s (z Identity) c) a
forall a b. (a -> b) -> a -> b
$ (Ref s (z Identity) -> Ref s c) -> MutPart s (z Identity) c
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart ((Ref s (z Identity) -> Ref s c) -> MutPart s (z Identity) c)
-> (Ref s (z Identity) -> Ref s c) -> MutPart s (z Identity) c
forall a b. (a -> b) -> a -> b
$ RefFor s c -> Ref s c
forall s a. RefFor s a -> Ref s a
getRefFor (RefFor s c -> Ref s c)
-> (z (RefFor s) -> RefFor s c) -> z (RefFor s) -> Ref s c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i (RefFor s c) a -> RefFor s c
forall i c k (p :: k). K1 i c p -> c
unK1 (K1 i (RefFor s c) a -> RefFor s c)
-> (z (RefFor s) -> K1 i (RefFor s c) a)
-> z (RefFor s)
-> RefFor s c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> K1 i (RefFor s c) a
f
instance (Mutable s (z Identity), Ref s (z Identity) ~ z (RefFor s)) => HKDMutParts s z U1 U1 where
hkdMutParts_ :: (z (RefFor s) -> U1 a) -> U1 a
hkdMutParts_ z (RefFor s) -> U1 a
_ = U1 a
forall k (p :: k). U1 p
U1
instance (Mutable s (z Identity), Ref s (z Identity) ~ z (RefFor s), TypeError ('Text "Cannot use hkdMutParts for uninhabited types: " ':<>: 'ShowType z)) => HKDMutParts s z V1 V1 where
hkdMutParts_ :: (z (RefFor s) -> V1 a) -> V1 a
hkdMutParts_ z (RefFor s) -> V1 a
_ = V1 a
forall a. HasCallStack => a
undefined
instance HKDMutParts s z i o => HKDMutParts s z (M1 a b i) (M1 a b o) where
hkdMutParts_ :: (z (RefFor s) -> M1 a b i a) -> M1 a b o a
hkdMutParts_ z (RefFor s) -> M1 a b i a
f = o a -> M1 a b o a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (o a -> M1 a b o a) -> o a -> M1 a b o a
forall a b. (a -> b) -> a -> b
$ (z (RefFor s) -> i a) -> o a
forall k s (z :: (* -> *) -> *) (i :: k -> *) (o :: k -> *)
(a :: k).
HKDMutParts s z i o =>
(z (RefFor s) -> i a) -> o a
hkdMutParts_ @s (M1 a b i a -> i a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 (M1 a b i a -> i a)
-> (z (RefFor s) -> M1 a b i a) -> z (RefFor s) -> i a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> M1 a b i a
f)
instance (HKDMutParts s z i o, HKDMutParts s z i' o') => HKDMutParts s z (i :*: i') (o :*: o') where
hkdMutParts_ :: (z (RefFor s) -> (:*:) i i' a) -> (:*:) o o' a
hkdMutParts_ z (RefFor s) -> (:*:) i i' a
f = (z (RefFor s) -> i a) -> o a
forall k s (z :: (* -> *) -> *) (i :: k -> *) (o :: k -> *)
(a :: k).
HKDMutParts s z i o =>
(z (RefFor s) -> i a) -> o a
hkdMutParts_ @s ((\(i a
x:*:i' a
_)->i a
x) ((:*:) i i' a -> i a)
-> (z (RefFor s) -> (:*:) i i' a) -> z (RefFor s) -> i a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> (:*:) i i' a
f) o a -> o' a -> (:*:) o o' a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (z (RefFor s) -> i' a) -> o' a
forall k s (z :: (* -> *) -> *) (i :: k -> *) (o :: k -> *)
(a :: k).
HKDMutParts s z i o =>
(z (RefFor s) -> i a) -> o a
hkdMutParts_ @s ((\(i a
_:*:i' a
y)->i' a
y) ((:*:) i i' a -> i' a)
-> (z (RefFor s) -> (:*:) i i' a) -> z (RefFor s) -> i' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. z (RefFor s) -> (:*:) i i' a
f)
instance (Mutable s (z Identity), Ref s (z Identity) ~ z (RefFor s), TypeError ('Text "Cannot use hkdMutParts for sum types: " ':<>: 'ShowType z)) => HKDMutParts s z (i :+: i') o where
hkdMutParts_ :: (z (RefFor s) -> (:+:) i i' a) -> o a
hkdMutParts_ z (RefFor s) -> (:+:) i i' a
_ = o a
forall a. HasCallStack => a
undefined
hkdMutParts
:: forall z s.
( Generic (z (RefFor s))
, Generic (z (MutPart s (z Identity)))
, HKDMutParts s z (Rep (z (RefFor s))) (Rep (z (MutPart s (z Identity))))
)
=> z (MutPart s (z Identity))
hkdMutParts :: z (MutPart s (z Identity))
hkdMutParts = Rep (z (MutPart s (z Identity))) Any -> z (MutPart s (z Identity))
forall a x. Generic a => Rep a x -> a
to (Rep (z (MutPart s (z Identity))) Any
-> z (MutPart s (z Identity)))
-> Rep (z (MutPart s (z Identity))) Any
-> z (MutPart s (z Identity))
forall a b. (a -> b) -> a -> b
$ (z (RefFor s) -> Rep (z (RefFor s)) Any)
-> Rep (z (MutPart s (z Identity))) Any
forall k s (z :: (* -> *) -> *) (i :: k -> *) (o :: k -> *)
(a :: k).
HKDMutParts s z i o =>
(z (RefFor s) -> i a) -> o a
hkdMutParts_ @s @z z (RefFor s) -> Rep (z (RefFor s)) Any
forall a x. Generic a => a -> Rep a x
from
class (Mutable s b, Mutable s a) => FieldMut (fld :: Symbol) s b a | fld b -> a where
fieldMut
:: Label fld
-> MutPart s b a
instance
( Mutable s b
, Mutable s a
, Ref s b ~ GRef s b
, GL.GLens' (HasTotalFieldPSym fld) (GRef_ s (Rep b)) (Ref s a)
, GL.HasField' fld b a
)
=> FieldMut fld s b a where
fieldMut :: Label fld -> MutPart s b a
fieldMut Label fld
_ = (Ref s b -> Ref s a) -> MutPart s b a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart ((Ref s b -> Ref s a) -> MutPart s b a)
-> (Ref s b -> Ref s a) -> MutPart s b a
forall a b. (a -> b) -> a -> b
$ Lens (GRef_ s (Rep b) ()) (GRef_ s (Rep b) ()) (Ref s a) (Ref s a)
-> GRef_ s (Rep b) () -> Ref s a
forall s a. Lens s s a a -> s -> a
GLP.view (forall (s :: * -> *) (t :: * -> *) a b x.
GLens (HasTotalFieldPSym fld) s t a b =>
Lens (s x) (t x) a b
forall (pred :: Pred) (s :: * -> *) (t :: * -> *) a b x.
GLens pred s t a b =>
Lens (s x) (t x) a b
GL.glens @(HasTotalFieldPSym fld)) (GRef_ s (Rep b) () -> Ref s a)
-> (GRef s b -> GRef_ s (Rep b) ()) -> GRef s b -> Ref s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef s b -> GRef_ s (Rep b) ()
forall s a. GRef s a -> GRef_ s (Rep a) ()
unGRef
data HasTotalFieldPSym :: Symbol -> GL.TyFun (Type -> Type) (Maybe Type)
type instance GL.Eval (HasTotalFieldPSym sym) tt = GL.HasTotalFieldP sym tt
withField
:: FieldMut fld s b a
=> Label fld
-> Ref s b
-> (Ref s a -> m r)
-> m r
withField :: Label fld -> Ref s b -> (Ref s a -> m r) -> m r
withField Label fld
l = MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
forall k s b a (m :: k -> *) (r :: k).
MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
withPart (Label fld -> MutPart s b a
forall (fld :: Symbol) s b a.
FieldMut fld s b a =>
Label fld -> MutPart s b a
fieldMut Label fld
l)
mutField
:: forall fld s b a. FieldMut fld s b a
=> Label fld
-> Ref s b
-> Ref s a
mutField :: Label fld -> Ref s b -> Ref s a
mutField = MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart (MutPart s b a -> Ref s b -> Ref s a)
-> (Label fld -> MutPart s b a) -> Label fld -> Ref s b -> Ref s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. FieldMut fld s b a => Label fld -> MutPart s b a
forall (fld :: Symbol) s b a.
FieldMut fld s b a =>
Label fld -> MutPart s b a
fieldMut @_ @s
class (Mutable s b, Mutable s a) => PosMut (i :: Nat) s b a | i b -> a where
posMut :: MutPart s b a
instance
( Mutable s b
, Mutable s a
, Ref s b ~ GRef s b
, gref ~ Fst (Traverse (GRef_ s (GL.CRep b)) 1)
, Coercible (GRef_ s (Rep b) ()) (gref ())
, GL.GLens' (HasTotalPositionPSym i) gref (Ref s a)
, GL.HasPosition' i b a
)
=> PosMut i s b a where
posMut :: MutPart s b a
posMut = (Ref s b -> Ref s a) -> MutPart s b a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart ((Ref s b -> Ref s a) -> MutPart s b a)
-> (Ref s b -> Ref s a) -> MutPart s b a
forall a b. (a -> b) -> a -> b
$ Lens (gref ()) (gref ()) (Ref s a) (Ref s a) -> gref () -> Ref s a
forall s a. Lens s s a a -> s -> a
GLP.view (forall (t :: * -> *) a b x.
GLens (HasTotalPositionPSym i) gref t a b =>
Lens (gref x) (t x) a b
forall (pred :: Pred) (s :: * -> *) (t :: * -> *) a b x.
GLens pred s t a b =>
Lens (s x) (t x) a b
GL.glens @(HasTotalPositionPSym i) @gref) (gref () -> Ref s a)
-> (GRef s b -> gref ()) -> GRef s b -> Ref s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coercible (GRef_ s (Rep b) ()) (gref ()) =>
GRef_ s (Rep b) () -> gref ()
coerce @_ @(gref ()) (GRef_ s (Rep b) () -> gref ())
-> (GRef s b -> GRef_ s (Rep b) ()) -> GRef s b -> gref ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef s b -> GRef_ s (Rep b) ()
forall s a. GRef s a -> GRef_ s (Rep a) ()
unGRef
data HasTotalPositionPSym :: Nat -> GL.TyFun (Type -> Type) (Maybe Type)
type instance GL.Eval (HasTotalPositionPSym t) tt = GL.HasTotalPositionP t tt
withPos
:: forall i s m b a r. PosMut i s b a
=> Ref s b
-> (Ref s a -> m r)
-> m r
withPos :: Ref s b -> (Ref s a -> m r) -> m r
withPos = MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
forall k s b a (m :: k -> *) (r :: k).
MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
withPart (forall s b a. PosMut i s b a => MutPart s b a
forall (i :: Nat) s b a. PosMut i s b a => MutPart s b a
posMut @i)
mutPos
:: forall i s b a. PosMut i s b a
=> Ref s b
-> Ref s a
mutPos :: Ref s b -> Ref s a
mutPos = MutPart s b a -> Ref s b -> Ref s a
forall s b a. MutPart s b a -> Ref s b -> Ref s a
getMutPart (forall b a. PosMut i s b a => MutPart s b a
forall (i :: Nat) s b a. PosMut i s b a => MutPart s b a
posMut @i @s)
class (Mutable s b, Mutable s a) => TupleMut s b a | b -> a where
tupleMut :: MutPart s b a
instance
( Mutable s b
, Mutable s a
, Ref s b ~ GRef s b
, GIsList (GRef_ s (Rep b)) (GRef_ s (Rep b)) (MapRef s as) (MapRef s as)
, GIsList (Rep b) (Rep b) as as
, ListTuple a a as as
, ListTuple c c (MapRef s as) (MapRef s as)
, Ref s a ~ c
)
=> TupleMut s b a where
tupleMut :: MutPart s b a
tupleMut = (Ref s b -> Ref s a) -> MutPart s b a
forall s b a. (Ref s b -> Ref s a) -> MutPart s b a
MutPart ((Ref s b -> Ref s a) -> MutPart s b a)
-> (Ref s b -> Ref s a) -> MutPart s b a
forall a b. (a -> b) -> a -> b
$ ListTuple c c (MapRef s as) (MapRef s as) =>
HList (MapRef s as) -> c
forall tuple tuple' (as :: [*]) (bs :: [*]).
ListTuple tuple tuple' as bs =>
HList as -> tuple
listToTuple @c @c @(MapRef s as) @(MapRef s as)
(HList (MapRef s as) -> c)
-> (GRef s b -> HList (MapRef s as)) -> GRef s b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens
(GRef_ s (Rep b) ())
(GRef_ s (Rep b) ())
(HList (MapRef s as))
(HList (MapRef s as))
-> GRef_ s (Rep b) () -> HList (MapRef s as)
forall s a. Lens s s a a -> s -> a
GLP.view forall (f :: * -> *) (g :: * -> *) (as :: [*]) (bs :: [*]) x.
GIsList f g as bs =>
Iso (f x) (g x) (HList as) (HList bs)
Lens
(GRef_ s (Rep b) ())
(GRef_ s (Rep b) ())
(HList (MapRef s as))
(HList (MapRef s as))
glist
(GRef_ s (Rep b) () -> HList (MapRef s as))
-> (GRef s b -> GRef_ s (Rep b) ())
-> GRef s b
-> HList (MapRef s as)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GRef s b -> GRef_ s (Rep b) ()
forall s a. GRef s a -> GRef_ s (Rep a) ()
unGRef
withTuple
:: TupleMut s b a
=> Ref s b
-> (Ref s a -> m r)
-> m r
withTuple :: Ref s b -> (Ref s a -> m r) -> m r
withTuple = MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
forall k s b a (m :: k -> *) (r :: k).
MutPart s b a -> Ref s b -> (Ref s a -> m r) -> m r
withPart MutPart s b a
forall s b a. TupleMut s b a => MutPart s b a
tupleMut
type G = Type -> Type
type family Traverse (a :: G) (n :: Nat) :: (G, Nat) where
Traverse (M1 mt m s) n
= Traverse1 (M1 mt m) (Traverse s n)
Traverse (l :+: r) n
= '(Fst (Traverse l n) :+: Fst (Traverse r n), n)
Traverse (l :*: r) n
= TraverseProd (:*:) (Traverse l n) r
Traverse (K1 _ p) n
= '(K1 (GL.Pos n) p, n + 1)
Traverse U1 n
= '(U1, n)
type family Traverse1 (w :: G -> G) (z :: (G, Nat)) :: (G, Nat) where
Traverse1 w '(i, n) = '(w i, n)
type family TraverseProd (c :: G -> G -> G) (a :: (G, Nat)) (r :: G) :: (G, Nat) where
TraverseProd w '(i, n) r = Traverse1 (w i) (Traverse r n)
type family Fst (p :: (a, b)) :: a where
Fst '(a, b) = a