mutable-0.1.0.1: Automatic piecewise-mutable references for your types
Copyright(c) Justin Le 2020
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Mutable.Branches

Description

Tools for working with potential branches of piecewise-mutable values.

If Data.Mutable.Parts is for product types, then Data.Mutable.Branches is for sum types.

See https://mutable.jle.im/06-mutable-branches.html for an introduction to this module.

Synopsis

Documentation

data MutBranch m s a Source #

A MutBranch m s a represents the information that s could potentially be an a. Similar in spirit to a Prism' s a.

MutBranch m s a means that a is one potential option that s could be in, or that s is a sum type and a is one of the branches/constructors.

See https://mutable.jle.im/06-mutable-branches.html for an introduction to this module.

If MutPart is for product types, then MutBranch is for sum types.

In this case, "branch" means "potential option". For example, the branches of Either are Left and Right.

The simplest way to make these is by using constrMB. For instance, to get the two branches of an Either:

constrMB #_Left   :: MutBranch m (Either a b) a
constrMB #_Right  :: MutBranch m (Either a b) b
ghci> r <- thawRef (Left 10)
ghci> freezeBranch (constrMB #_Left) r
Just 10
ghci> freezeBranch (constrMB #_Right) r
Nothing

It uses OverloadedLabels, but requires an underscore before the constructor name due to limitations in the extension.

One nice way to use these is with withBranch_:

ghci> r <- thawRef (Just 10)
ghci> withBranch_ (constrMB #_Just) $ i ->    -- i is an Int ref
   ..   modifyRef i (+ 1)
ghci> freezeRef r
Just 11
ghci> r <- thawRef Nothing
ghci> withBranch_ (constrMB #_Just) $ i ->    -- i is an Int ref
   ..   modifyRef i (+ 1)
ghci> freezeRef r
Nothing

Perhaps the most useful usage of this abstraction is for recursive data types.

data List a = Nil | Cons a (List a)
  deriving Generic

instance Mutable m a => Mutable m (List a) where
    type Ref m (List a) = GRef m (List a)

GRef m (List a) is now a mutable linked list! Once we make the MutBranch for the nil and cons cases:

nilBranch :: MutBranch m (List a) ()
nilBranch = constrMB #_Nil

consBranch :: MutBranch m (List a) (a, List a)
consBranch = constrMB #_Cons

Here is a function to check if a linked list is currently empty:

isEmpty
    :: (PrimMonad m, Mutable m a)
    => Ref m (List a)
    -> m Bool
isEmpty = hasBranch nilBranch

Here is one to "pop" a mutable linked list, giving us the first value and shifting the rest of the list up.

popStack
    :: (PrimMonad m, Mutable m a)
    => Ref m (List a)
    -> m (Maybe a)
popStack r = do
    c <- projectBranch consBranch r
    case c of
      Nothing      -> pure Nothing
      Just (x, xs) -> do
        moveRef r xs
        Just $ freezeRef x

And here is a function to concatenate a second linked list to the end of a first one.

concatLists
    :: (PrimMonad m, Mutable m a)
    => Ref m (List a)
    -> Ref m (List a)
    -> m ()
concatLists l1 l2 = do
    c <- projectBranch consBranch l1
    case c of
      Nothing      -> moveRef l1 l2
      Just (_, xs) -> concatLists xs l2

Constructors

MutBranch 

Fields

  • projectBranch :: Ref m s -> m (Maybe (Ref m a))

    With a MutBranch, attempt to get the mutable contents of a branch of a mutable s, if possible.

    ghci> r <- thawRef (Left 10)
    ghci> s <- projectBranch (constrMB #_Left) r
    ghci> case s of Just s' -> freezeRef s'
    10
    
    ghci> r <- thawRef (Right True)
    ghci> s <- projectBranch (constrMB #_Left) r
    ghci> case s of Nothing -> "it was Right"
    "it was Right"
    
  • embedBranch :: Ref m a -> m (Ref m s)

    Embed an a ref as a part of a larger s ref. Note that this does not copy or clone: any mutations to the a ref will be reflected in the s ref, as long as the s ref maintains the reference.

    ghci> r <- thawRef 100
    ghci> s <- embedBranch (constMB #_Left) r
    ghci> freezeRef s
    Left 100
    ghci> modifyRef r (+ 1)
    ghci> freezeRef s
    Left 101
    

    Any mutations on s (as long as they keep the same branch) will also affect a:

    ghci> copyRef s (Left 0)
    ghci> freezeRef r
    0
    

    However, "switching branches" on an Either ref will cause it to loose the original reference:

    ghci> copyRef s (Right True)
    ghci> copyRef s (Left 999)
    ghci> freezeRef r
    0
    

thawBranch :: Mutable m a => MutBranch m s a -> a -> m (Ref m s) Source #

With a MutBranch, thaw an a into a mutable s on that branch.

ghci> r <- thawBranch (constrMB #_Left) 10
ghci> freezeRef r
Left 10

freezeBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if is s is an a

-> Ref m s

Structure to read out of

-> m (Maybe a) 

With a MutBranch, read out a specific a branch of an s, if it exists.

ghci> r <- thawRef (Left 10)
ghci> freezeBranch (constrMB #_Left) r
Just 10
ghci> freezeBranch (constrMB #_Right) r
Nothing

hasBranch :: Mutable m a => MutBranch m s a -> Ref m s -> m Bool Source #

Check if an s is currently a certain branch a.

hasn'tBranch :: Mutable m a => MutBranch m s a -> Ref m s -> m Bool Source #

Check if an s is not currently a certain branch a.

moveBranch :: Mutable m s => MutBranch m s a -> Ref m s -> Ref m a -> m () Source #

With a MutBranch, overwrite an s as an a, on that branch.

ghci> r <- thawRef (Left 10)
ghci> s <- thawRef 100
ghci> moveBranch (constrMB #_Left) r s
ghci> freezeRef r
Left 100
ghci> t <- thawRef True
ghci> moveBranch (constrMB #_Right) r t
ghci> freezeRef r
Right True

copyBranch Source #

Arguments

:: (Mutable m s, Mutable m a) 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to write into

-> a

Value to set s to be

-> m () 

With a MutBranch, set s to have the branch a.

ghci> r <- thawRef (Left 10)
ghci> copyBranch (constrMB #_Left) r 5678
ghci> freezeRef r
Left 5678
ghci> copyBranch (constrMB #_Right) r True
ghci> freezeRef r
Right True

cloneBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of

-> m (Maybe (Ref m a)) 

With a MutBranch, attempt to clone out a branch of a mutable s, if possible.

ghci> r <- thawRef (Left 10)
ghci> s <- cloneBranch (constrMB #_Left)
ghci> case s of Just s' -> freezeRef s'
10
ghci> r <- thawRef (Right True)
ghci> s <- cloneBranch (constrMB #_Left)
ghci> case s of Nothing -> "it was Right"
"it was Right"

unsafeThawBranch :: Mutable m a => MutBranch m s a -> a -> m (Ref m s) Source #

A non-copying version of thawBranch that can be more efficient for types where the mutable representation is the same as the immutable one (like Vector).

This is safe as long as you never again use the original pure value, since it can potentially directly mutate it.

unsafeFreezeBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if is s is an a

-> Ref m s

Structure to read out of

-> m (Maybe a) 

A non-copying version of freezeBranch that can be more efficient for types where the mutable representation is the same as the immutable one (like Vector).

This is safe as long as you never again modify the mutable reference, since it can potentially directly mutate the frozen value magically.

withBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if is s is an a

-> Ref m s

Structure to read out of and write into

-> (Ref m a -> m b)

Action to perform on the a branch of s

-> m (Maybe b) 

With a MutBranch, if an s is on the a branch, perform an action on the a reference and overwrite the s with the modified a. Returns the result of the action, if a was found.

ghci> r <- thawRef (Just 10)
ghci> withBranch_ (constrMB #_Just) $ i ->    -- i is an Int ref
   ..   modifyRef i (+ 1)
ghci> freezeRef r
Just 11
ghci> r <- thawRef Nothing
ghci> withBranch_ (constrMB #_Just) $ i ->    -- i is an Int ref
   ..   modifyRef i (+ 1)
ghci> freezeRef r
Nothing

withBranch_ Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if is s is an a

-> Ref m s

Structure to read out of and write into

-> (Ref m a -> m b)

Action to perform on the a branch of s

-> m () 

withBranch, but discarding the returned value.

modifyBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> a)

Pure function modifying a

-> m () 

With a MutBranch, run a pure function over a potential branch a of s. If s is not on that branch, leaves s unchanged.

ghci> r <- thawRef (Just 10)
ghci> modifyBranch (constrMB #_Just) r (+ 1)
ghci> freezeRef r
Just 11
ghci> r <- thawRef Nothing
ghci> modifyBranch (constrMB #_Just) r (+ 1)
ghci> freezeRef r
Nothing

modifyBranch' Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> a)

Pure function modifying a

-> m () 

modifyBranch, but forces the result before storing it back in the reference.

updateBranch Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> (a, b)) 
-> m (Maybe b) 

With a MutBranch, run a pure function over a potential branch a of s. The function returns the updated a and also an output value to observe. If s is not on that branch, leaves s unchanged.

ghci> r <- thawRef (Just 10)
ghci> updateBranch (constrMB #_Just) r $ i -> (i + 1, show i)
Just "10"
ghci> freezeRef r
Just 11
ghci> r <- thawRef Nothing
ghci> updateBranch (constrMB #_Just) r $ i -> (i + 1, show i)
Nothing
ghci> freezeRef r
Nothing

updateBranch' Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> (a, b)) 
-> m (Maybe b) 

updateBranch, but forces the result before storing it back in the reference.

modifyBranchM Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> m a)

Monadic function modifying a

-> m () 

modifyBranch but for a monadic function. Uses copyRef into the reference after the action is completed.

modifyBranchM' Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> m a)

Monadic function modifying a

-> m () 

modifyBranchM, but forces the result before storing it back in the reference.

updateBranchM Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> m (a, b)) 
-> m (Maybe b) 

updateBranch but for a monadic function. Uses copyRef into the reference after the action is completed.

updateBranchM' Source #

Arguments

:: Mutable m a 
=> MutBranch m s a

How to check if s is an a

-> Ref m s

Structure to read out of and write into

-> (a -> m (a, b)) 
-> m (Maybe b) 

updateBranchM, but forces the result before storing it back in the reference.

Built-in MutBranch

compMB :: Monad m => MutBranch m a b -> MutBranch m b c -> MutBranch m a c Source #

Compose two MutBranchs, to drill down on what is being focused.

idMB :: Applicative m => MutBranch m a a Source #

An identity MutBranch, treating the item itself as a whole branch. cloneBranch will always "match".

Using GHC Generics

constrMB :: forall ctor m s a. (Ref m s ~ GRef m s, GMutBranchConstructor ctor m (Rep s) a) => CLabel ctor -> MutBranch m s a Source #

Create a MutBranch for any data type with a Generic instance by specifying the constructor name using OverloadedLabels

ghci> r <- thawRef (Left 10)
ghci> freezeBranch (constrMB #_Left) r
Just 10
ghci> freezeBranch (constrMB #_Right) r
Nothing

Note that due to limitations in OverloadedLabels, you must prefix the constructor name with an undescore.

There also isn't currently any way to utilize OverloadedLabels with operator identifiers, so using it with operator constructors (like : and []) requires explicit TypeApplications:

-- | MutBranch focusing on the cons case of a list
consMB :: (PrimMonad m, Mutable m a) => MutBranch m [a] (a, [a])
consMB = constrMB (CLabel @":")

data CLabel (ctor :: Symbol) Source #

A version of Label that removes an underscore at the beginning when used with -XOverloadedLabels. Used to specify constructors, since labels are currently not able to start with capital letters.

Constructors

CLabel 

Instances

Instances details
ctor_ ~ AppendSymbol "_" ctor => IsLabel ctor_ (CLabel ctor) Source # 
Instance details

Defined in Data.Mutable.Branches

Methods

fromLabel :: CLabel ctor #

class (GMutable m f, Mutable m a) => GMutBranchConstructor (ctor :: Symbol) m f a | ctor f -> a Source #

Typeclass powering constrMB using GHC Generics.

Heavily inspired by Data.Generics.Sum.Constructors.

Minimal complete definition

gmbcProj, gmbcEmbed

Instances

Instances details
(PrimMonad m, Mutable m a, GMutBranchSum ctor (HasCtorP ctor l) m l r a) => GMutBranchConstructor ctor m (l :+: r :: k -> Type) a Source # 
Instance details

Defined in Data.Mutable.Branches

Methods

gmbcProj :: forall (x :: k0). CLabel ctor -> GRef_ m (l :+: r) x -> m (Maybe (Ref m a))

gmbcEmbed :: forall (x :: k0). CLabel ctor -> Ref m a -> m (GRef_ m (l :+: r) x)

(GMutable m f, Mutable m a, GIsList (GRef_ m f) (GRef_ m f) (MapRef m as) (MapRef m as), GIsList f f as as, ListTuple a a as as, ListTuple b b (MapRef m as) (MapRef m as), Ref m a ~ b) => GMutBranchConstructor ctor m (M1 C ('MetaCons ctor fixity fields) f :: Type -> Type) a Source # 
Instance details

Defined in Data.Mutable.Branches

Methods

gmbcProj :: forall (x :: k). CLabel ctor -> GRef_ m (M1 C ('MetaCons ctor fixity fields) f) x -> m (Maybe (Ref m a))

gmbcEmbed :: forall (x :: k). CLabel ctor -> Ref m a -> m (GRef_ m (M1 C ('MetaCons ctor fixity fields) f) x)

GMutBranchConstructor ctor m f a => GMutBranchConstructor ctor m (M1 D meta f :: k -> Type) a Source # 
Instance details

Defined in Data.Mutable.Branches

Methods

gmbcProj :: forall (x :: k0). CLabel ctor -> GRef_ m (M1 D meta f) x -> m (Maybe (Ref m a))

gmbcEmbed :: forall (x :: k0). CLabel ctor -> Ref m a -> m (GRef_ m (M1 D meta f) x)

type family MapRef m as where ... Source #

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 ]

Equations

MapRef m '[] = '[] 
MapRef m (a ': as) = Ref m a ': MapRef m as 

For common types

nilMB :: (PrimMonad m, Mutable m a) => MutBranch m [a] () Source #

MutBranch focusing on the nil case of a list

consMB :: (PrimMonad m, Mutable m a) => MutBranch m [a] (a, [a]) Source #

MutBranch focusing on the cons case of a list

nothingMB :: (PrimMonad m, Mutable m a) => MutBranch m (Maybe a) () Source #

MutBranch focusing on the Nothing case of a Maybe

justMB :: (PrimMonad m, Mutable m a) => MutBranch m (Maybe a) a Source #

MutBranch focusing on the Just case of a Maybe

leftMB :: (PrimMonad m, Mutable m a, Mutable m b) => MutBranch m (Either a b) a Source #

MutBranch focusing on the Left case of an Either

rightMB :: (PrimMonad m, Mutable m a, Mutable m b) => MutBranch m (Either a b) b Source #

MutBranch focusing on the Right case of an Either