{-# LANGUAGE ExistentialQuantification, ScopedTypeVariables, Trustworthy #-}
{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Chatty with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a variable-storing monad and functions for access. Not recommended. Use IORef, STRef or the like. Really.
module Data.Chatty.Atoms where

import Control.Applicative
import Control.Arrow
import qualified Control.Category as C
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class
import Data.Dynamic
import Data.Typeable
import Data.Chatty.AVL
import Data.Chatty.Counter
import Unsafe.Coerce

-- | Phantom type for atom IDs
data Atom a = Atom Int
            | forall b. FunAtom Int (Atom b) (b -> a) (b -> a -> b)
            | forall b c. FunAtom2 Int (Atom b) (Atom c) ((b,c) -> a) ((b,c) -> a -> (b,c))

instance Eq (Atom a) where
  (Atom Int
n) == :: Atom a -> Atom a -> Bool
== (Atom Int
m) = Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
m
  (FunAtom Int
i Atom b
_ b -> a
_ b -> a -> b
_) == (FunAtom Int
j Atom b
_ b -> a
_ b -> a -> b
_) = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j
  (FunAtom2 Int
i Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) == (FunAtom2 Int
j Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j
  Atom a
_ == Atom a
_ = Bool
False

instance Ord (Atom a) where
  (Atom Int
n) compare :: Atom a -> Atom a -> Ordering
`compare` (Atom Int
m) = Int
n Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
m
  (FunAtom Int
i Atom b
_ b -> a
_ b -> a -> b
_) `compare` (Atom Int
m) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
m
  (FunAtom2 Int
i Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) `compare` (Atom Int
m) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
m
  (Atom Int
n) `compare` (FunAtom Int
j Atom b
_ b -> a
_ b -> a -> b
_) = Int
n Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j
  (FunAtom Int
i Atom b
_ b -> a
_ b -> a -> b
_) `compare` (FunAtom Int
j Atom b
_ b -> a
_ b -> a -> b
_) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j
  (FunAtom2 Int
i Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) `compare` (FunAtom Int
j Atom b
_ b -> a
_ b -> a -> b
_) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j
  (Atom Int
n) `compare` (FunAtom2 Int
j Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) = Int
n Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j
  (FunAtom Int
i Atom b
_ b -> a
_ b -> a -> b
_) `compare` (FunAtom2 Int
j Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j
  (FunAtom2 Int
i Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) `compare` (FunAtom2 Int
j Atom b
_ Atom c
_ (b, c) -> a
_ (b, c) -> a -> (b, c)
_) = Int
i Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
j

newtype Container = Container ()

-- | The storage monad
newtype AtomStoreT m a = AtomStore { AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
runAtomStoreT :: AVL (Int, Container) -> m (a,AVL (Int,Container)) }

instance Functor m => Functor (AtomStoreT m) where
  fmap :: (a -> b) -> AtomStoreT m a -> AtomStoreT m b
fmap a -> b
f AtomStoreT m a
a = (AVL (Int, Container) -> m (b, AVL (Int, Container)))
-> AtomStoreT m b
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m (b, AVL (Int, Container)))
 -> AtomStoreT m b)
-> (AVL (Int, Container) -> m (b, AVL (Int, Container)))
-> AtomStoreT m b
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> ((a, AVL (Int, Container)) -> (b, AVL (Int, Container)))
-> m (a, AVL (Int, Container)) -> m (b, AVL (Int, Container))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> (a, AVL (Int, Container)) -> (b, AVL (Int, Container))
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a -> b
f) (m (a, AVL (Int, Container)) -> m (b, AVL (Int, Container)))
-> m (a, AVL (Int, Container)) -> m (b, AVL (Int, Container))
forall a b. (a -> b) -> a -> b
$ AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
forall (m :: * -> *) a.
AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
runAtomStoreT AtomStoreT m a
a AVL (Int, Container)
s

instance (Functor m, Monad m) => Applicative (AtomStoreT m) where
  pure :: a -> AtomStoreT m a
pure = a -> AtomStoreT m a
forall (m :: * -> *) a. Monad m => a -> m a
return
  <*> :: AtomStoreT m (a -> b) -> AtomStoreT m a -> AtomStoreT m b
(<*>) = AtomStoreT m (a -> b) -> AtomStoreT m a -> AtomStoreT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad m => Monad (AtomStoreT m) where
  return :: a -> AtomStoreT m a
return a
a = (AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m (a, AVL (Int, Container)))
 -> AtomStoreT m a)
-> (AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> (a, AVL (Int, Container)) -> m (a, AVL (Int, Container))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,AVL (Int, Container)
s)
  AtomStoreT m a
m >>= :: AtomStoreT m a -> (a -> AtomStoreT m b) -> AtomStoreT m b
>>= a -> AtomStoreT m b
f = (AVL (Int, Container) -> m (b, AVL (Int, Container)))
-> AtomStoreT m b
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m (b, AVL (Int, Container)))
 -> AtomStoreT m b)
-> (AVL (Int, Container) -> m (b, AVL (Int, Container)))
-> AtomStoreT m b
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> do (a
a,AVL (Int, Container)
s') <- AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
forall (m :: * -> *) a.
AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
runAtomStoreT AtomStoreT m a
m AVL (Int, Container)
s; AtomStoreT m b
-> AVL (Int, Container) -> m (b, AVL (Int, Container))
forall (m :: * -> *) a.
AtomStoreT m a
-> AVL (Int, Container) -> m (a, AVL (Int, Container))
runAtomStoreT (a -> AtomStoreT m b
f a
a) AVL (Int, Container)
s'

instance MonadTrans AtomStoreT where
  lift :: m a -> AtomStoreT m a
lift m a
m = (AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m (a, AVL (Int, Container)))
 -> AtomStoreT m a)
-> (AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> do a
a <- m a
m; (a, AVL (Int, Container)) -> m (a, AVL (Int, Container))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,AVL (Int, Container)
s)

instance MonadIO m => MonadIO (AtomStoreT m) where
  liftIO :: IO a -> AtomStoreT m a
liftIO = m a -> AtomStoreT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> AtomStoreT m a) -> (IO a -> m a) -> IO a -> AtomStoreT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance ChCounter m => ChCounter (AtomStoreT m) where
  countOn :: AtomStoreT m Int
countOn = m Int -> AtomStoreT m Int
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m Int
forall (m :: * -> *). ChCounter m => m Int
countOn

-- | Typeclass for all atom-storing monads.
class ChCounter m => ChAtoms m where
  -- | Reserve a new atom.
  newAtom :: m (Atom v)
  newAtom = (Int -> Atom v) -> m Int -> m (Atom v)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Int -> Atom v
forall a. Int -> Atom a
Atom m Int
forall (m :: * -> *). ChCounter m => m Int
countOn
  -- | Construct a new functional atom.
  funAtom :: Atom b -> (b -> a) -> (b -> a -> b) -> m (Atom a)
  funAtom Atom b
b b -> a
r b -> a -> b
p = do
    Int
i <- m Int
forall (m :: * -> *). ChCounter m => m Int
countOn
    Atom a -> m (Atom a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Atom a -> m (Atom a)) -> Atom a -> m (Atom a)
forall a b. (a -> b) -> a -> b
$ Int -> Atom b -> (b -> a) -> (b -> a -> b) -> Atom a
forall a b. Int -> Atom b -> (b -> a) -> (b -> a -> b) -> Atom a
FunAtom Int
i Atom b
b b -> a
r b -> a -> b
p
  -- | Construct a new doubly-source functional atom
  funAtom2 :: Atom b -> Atom c -> ((b,c) -> a) -> ((b,c) -> a -> (b,c)) -> m (Atom a)
  funAtom2 Atom b
b Atom c
c (b, c) -> a
r (b, c) -> a -> (b, c)
p = do
    Int
i <- m Int
forall (m :: * -> *). ChCounter m => m Int
countOn
    Atom a -> m (Atom a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Atom a -> m (Atom a)) -> Atom a -> m (Atom a)
forall a b. (a -> b) -> a -> b
$ Int
-> Atom b
-> Atom c
-> ((b, c) -> a)
-> ((b, c) -> a -> (b, c))
-> Atom a
forall a b c.
Int
-> Atom b
-> Atom c
-> ((b, c) -> a)
-> ((b, c) -> a -> (b, c))
-> Atom a
FunAtom2 Int
i Atom b
b Atom c
c (b, c) -> a
r (b, c) -> a -> (b, c)
p
  -- | Save a value for the given atom.
  putAtom :: Atom v -> v -> m ()
  -- | Get the value from a given atom.
  getAtom :: Atom v -> m v
  -- | Dispose the given atom.
  dispAtom :: Atom v -> m ()
  -- | Clone the given atom.
  cloneAtom :: Atom v -> m (Atom v)
  cloneAtom Atom v
a = do
    Atom v
b <- m (Atom v)
forall (m :: * -> *) v. ChAtoms m => m (Atom v)
newAtom
    v
v <- Atom v -> m v
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom v
a
    Atom v -> v -> m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom v
b v
v
    Atom v -> m (Atom v)
forall (m :: * -> *) a. Monad m => a -> m a
return Atom v
b

instance ChCounter m => ChAtoms (AtomStoreT m) where
  putAtom :: Atom v -> v -> AtomStoreT m ()
putAtom (Atom Int
a) v
v = (AVL (Int, Container) -> m ((), AVL (Int, Container)))
-> AtomStoreT m ()
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m ((), AVL (Int, Container)))
 -> AtomStoreT m ())
-> (AVL (Int, Container) -> m ((), AVL (Int, Container)))
-> AtomStoreT m ()
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> ((), AVL (Int, Container)) -> m ((), AVL (Int, Container))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),(Int, Container) -> AVL (Int, Container) -> AVL (Int, Container)
forall i o v. Indexable i o v => i -> AVL i -> AVL i
avlInsert (Int
a,v -> Container
forall a b. a -> b
unsafeCoerce v
v) AVL (Int, Container)
s)
  putAtom (FunAtom Int
_ Atom b
b b -> v
_ b -> v -> b
p) v
v = do
    b
bv <- Atom b -> AtomStoreT m b
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom b
b
    Atom b -> b -> AtomStoreT m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom b
b (b -> AtomStoreT m ()) -> b -> AtomStoreT m ()
forall a b. (a -> b) -> a -> b
$ b -> v -> b
p b
bv v
v
  putAtom (FunAtom2 Int
_ Atom b
b Atom c
c (b, c) -> v
_ (b, c) -> v -> (b, c)
p) v
v = do
    b
bv <- Atom b -> AtomStoreT m b
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom b
b
    c
cv <- Atom c -> AtomStoreT m c
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom c
c
    let (b
bv',c
cv') = (b, c) -> v -> (b, c)
p (b
bv, c
cv) v
v
    Atom b -> b -> AtomStoreT m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom b
b b
bv'
    Atom c -> c -> AtomStoreT m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom c
c c
cv'
  getAtom :: Atom v -> AtomStoreT m v
getAtom (Atom Int
a) = (AVL (Int, Container) -> m (v, AVL (Int, Container)))
-> AtomStoreT m v
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m (v, AVL (Int, Container)))
 -> AtomStoreT m v)
-> (AVL (Int, Container) -> m (v, AVL (Int, Container)))
-> AtomStoreT m v
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> let Just Container
v = Int -> AVL (Int, Container) -> Maybe Container
forall i o v. Indexable i o v => o -> AVL i -> Maybe v
avlLookup Int
a AVL (Int, Container)
s in (v, AVL (Int, Container)) -> m (v, AVL (Int, Container))
forall (m :: * -> *) a. Monad m => a -> m a
return (Container -> v
forall a b. a -> b
unsafeCoerce Container
v,AVL (Int, Container)
s)
  getAtom (FunAtom Int
_ Atom b
b b -> v
g b -> v -> b
_) = (b -> v) -> AtomStoreT m b -> AtomStoreT m v
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> v
g (AtomStoreT m b -> AtomStoreT m v)
-> AtomStoreT m b -> AtomStoreT m v
forall a b. (a -> b) -> a -> b
$ Atom b -> AtomStoreT m b
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom b
b
  getAtom (FunAtom2 Int
_ Atom b
b Atom c
c (b, c) -> v
g (b, c) -> v -> (b, c)
_) = do
    b
bv <- Atom b -> AtomStoreT m b
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom b
b
    c
cv <- Atom c -> AtomStoreT m c
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom c
c
    v -> AtomStoreT m v
forall (m :: * -> *) a. Monad m => a -> m a
return (v -> AtomStoreT m v) -> v -> AtomStoreT m v
forall a b. (a -> b) -> a -> b
$ (b, c) -> v
g (b
bv,c
cv)
  dispAtom :: Atom v -> AtomStoreT m ()
dispAtom (Atom Int
a) = (AVL (Int, Container) -> m ((), AVL (Int, Container)))
-> AtomStoreT m ()
forall (m :: * -> *) a.
(AVL (Int, Container) -> m (a, AVL (Int, Container)))
-> AtomStoreT m a
AtomStore ((AVL (Int, Container) -> m ((), AVL (Int, Container)))
 -> AtomStoreT m ())
-> (AVL (Int, Container) -> m ((), AVL (Int, Container)))
-> AtomStoreT m ()
forall a b. (a -> b) -> a -> b
$ \AVL (Int, Container)
s -> ((), AVL (Int, Container)) -> m ((), AVL (Int, Container))
forall (m :: * -> *) a. Monad m => a -> m a
return ((),Int -> AVL (Int, Container) -> AVL (Int, Container)
forall i o v. Indexable i o v => o -> AVL i -> AVL i
avlRemove Int
a AVL (Int, Container)
s)
  dispAtom (FunAtom Int
_ Atom b
_ b -> v
_ b -> v -> b
_) = () -> AtomStoreT m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  dispAtom (FunAtom2 Int
_ Atom b
_ Atom c
_ (b, c) -> v
_ (b, c) -> v -> (b, c)
_) = () -> AtomStoreT m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- Stop it. This just doesn't work safely.
{-- | Arrow type operating on atoms. Works by overwriting the educt. You should really *not* use
-- this for general a->b arrows, but only for a->a, unless you are sure that all Atom a references
-- are gone! Otherwise segfaults are waiting for you!
newtype Atomar m a b = Atomar { runAtomar :: Atom a -> m (Atom b) }

instance ChAtoms m => C.Category (Atomar m) where
  id = Atomar return
  a . b = Atomar (runAtomar a <=< runAtomar b)

instance ChAtoms m => Arrow (Atomar m) where
  arr = Atomar . mapAtom
  first f = Atomar $ \a -> do
    let afst = funAtom a fst $ \(_,s) f -> (f,s)
    mapAtom afst
-}

-- | Run a pure function on atoms.
mapAtom :: ChAtoms m => (a -> a) -> Atom a -> m ()
mapAtom :: (a -> a) -> Atom a -> m ()
mapAtom a -> a
f Atom a
a = do
  a
v <- Atom a -> m a
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom a
a
  Atom a -> a -> m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom a
a (a -> m ()) -> a -> m ()
forall a b. (a -> b) -> a -> b
$ a -> a
f a
v

-- | Arrow type operating on atoms. Works by cloning the educt, then overwriting the clone.
-- You shouldn't use this inside long-term environments, as massive usage blows up the memory.
newtype Redundant m a b = Redundant { Redundant m a b -> Atom a -> m (Atom b)
runRedundant :: Atom a -> m (Atom b) }

instance ChAtoms m => C.Category (Redundant m) where
  id :: Redundant m a a
id = (Atom a -> m (Atom a)) -> Redundant m a a
forall (m :: * -> *) a b. (Atom a -> m (Atom b)) -> Redundant m a b
Redundant Atom a -> m (Atom a)
forall (m :: * -> *) a. Monad m => a -> m a
return
  Redundant m b c
a . :: Redundant m b c -> Redundant m a b -> Redundant m a c
. Redundant m a b
b = (Atom a -> m (Atom c)) -> Redundant m a c
forall (m :: * -> *) a b. (Atom a -> m (Atom b)) -> Redundant m a b
Redundant (Redundant m b c -> Atom b -> m (Atom c)
forall (m :: * -> *) a b. Redundant m a b -> Atom a -> m (Atom b)
runRedundant Redundant m b c
a (Atom b -> m (Atom c))
-> (Atom a -> m (Atom b)) -> Atom a -> m (Atom c)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Redundant m a b -> Atom a -> m (Atom b)
forall (m :: * -> *) a b. Redundant m a b -> Atom a -> m (Atom b)
runRedundant Redundant m a b
b)

instance ChAtoms m => Arrow (Redundant m) where
  arr :: (b -> c) -> Redundant m b c
arr b -> c
f = (Atom b -> m (Atom c)) -> Redundant m b c
forall (m :: * -> *) a b. (Atom a -> m (Atom b)) -> Redundant m a b
Redundant ((Atom b -> m (Atom c)) -> Redundant m b c)
-> (Atom b -> m (Atom c)) -> Redundant m b c
forall a b. (a -> b) -> a -> b
$ \Atom b
a -> do
    b
v <- Atom b -> m b
forall (m :: * -> *) v. ChAtoms m => Atom v -> m v
getAtom Atom b
a
    Atom c
b <- m (Atom c)
forall (m :: * -> *) v. ChAtoms m => m (Atom v)
newAtom
    Atom c -> c -> m ()
forall (m :: * -> *) v. ChAtoms m => Atom v -> v -> m ()
putAtom Atom c
b (c -> m ()) -> c -> m ()
forall a b. (a -> b) -> a -> b
$ b -> c
f b
v
    Atom c -> m (Atom c)
forall (m :: * -> *) a. Monad m => a -> m a
return Atom c
b
  first :: Redundant m b c -> Redundant m (b, d) (c, d)
first Redundant m b c
f = (Atom (b, d) -> m (Atom (c, d))) -> Redundant m (b, d) (c, d)
forall (m :: * -> *) a b. (Atom a -> m (Atom b)) -> Redundant m a b
Redundant ((Atom (b, d) -> m (Atom (c, d))) -> Redundant m (b, d) (c, d))
-> (Atom (b, d) -> m (Atom (c, d))) -> Redundant m (b, d) (c, d)
forall a b. (a -> b) -> a -> b
$ \Atom (b, d)
a -> do
    Atom c
c <- Redundant m b c -> Atom b -> m (Atom c)
forall (m :: * -> *) a b. Redundant m a b -> Atom a -> m (Atom b)
runRedundant Redundant m b c
f (Atom b -> m (Atom c)) -> m (Atom b) -> m (Atom c)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Atom (b, d)
-> ((b, d) -> b) -> ((b, d) -> b -> (b, d)) -> m (Atom b)
forall (m :: * -> *) b a.
ChAtoms m =>
Atom b -> (b -> a) -> (b -> a -> b) -> m (Atom a)
funAtom Atom (b, d)
a (b, d) -> b
forall a b. (a, b) -> a
fst (\(b
_, d
s) b
f -> (b
f, d
s))
    Atom (b, d)
-> Atom c
-> (((b, d), c) -> (c, d))
-> (((b, d), c) -> (c, d) -> ((b, d), c))
-> m (Atom (c, d))
forall (m :: * -> *) b c a.
ChAtoms m =>
Atom b
-> Atom c -> ((b, c) -> a) -> ((b, c) -> a -> (b, c)) -> m (Atom a)
funAtom2 Atom (b, d)
a Atom c
c (\((b
_,d
s),c
f) -> (c
f,d
s)) ((((b, d), c) -> (c, d) -> ((b, d), c)) -> m (Atom (c, d)))
-> (((b, d), c) -> (c, d) -> ((b, d), c)) -> m (Atom (c, d))
forall a b. (a -> b) -> a -> b
$ \((b
e,d
s),c
f) (c
f',d
s') -> ((b
e,d
s'),c
f')