module Focus where
import Focus.Prelude hiding (adjust, update, alter, insert, delete, lookup)
data Focus element m result = Focus (m (result, Change element)) (element -> m (result, Change element))
deriving instance Functor m => Functor (Focus element m)
instance Monad m => Applicative (Focus element m) where
pure = return
(<*>) = ap
instance Monad m => Monad (Focus element m) where
return result = Focus (return (result, Leave)) (\ element -> return (result, Set element))
(>>=) (Focus aAbsent bPresent) bKleisli = let
sendSome element = do
(aResult, aChange) <- bPresent element
case bKleisli aResult of
Focus bAbsent bOnElement -> case aChange of
Leave -> bOnElement element
Remove -> bAbsent
Set newElement -> bOnElement newElement
sendNone = do
(aResult, aChange) <- aAbsent
case bKleisli aResult of
Focus bAbsent bOnElement -> case aChange of
Set newElement -> bOnElement newElement
Leave -> bAbsent
Remove -> bAbsent
in Focus sendNone sendSome
instance MonadTrans (Focus element) where
lift m = Focus (fmap (,Leave) m) (const (fmap (,Leave) m))
data Change a =
Leave |
Remove |
Set a
deriving (Functor, Eq, Ord, Show)
{-# INLINE member #-}
member :: Monad m => Focus a m Bool
member = fmap (maybe False (const True)) lookup
{-# INLINE[1] lookup #-}
lookup :: Monad m => Focus a m (Maybe a)
lookup = cases (Nothing, Leave) (\ a -> (Just a, Leave))
{-# INLINE[1] lookupWithDefault #-}
lookupWithDefault :: Monad m => a -> Focus a m a
lookupWithDefault a = cases (a, Leave) (\ a -> (a, Leave))
{-# INLINE[1] delete #-}
delete :: Monad m => Focus a m ()
delete = unitCases Leave (const Remove)
{-# RULES
"lookup <* delete" [~1] lookup <* delete = lookupAndDelete
#-}
{-# INLINE lookupAndDelete #-}
lookupAndDelete :: Monad m => Focus a m (Maybe a)
lookupAndDelete = cases (Nothing, Leave) (\ element -> (Just element, Remove))
{-# INLINE insert #-}
insert :: Monad m => a -> Focus a m ()
insert a = unitCases (Set a) (const (Set a))
{-# INLINE insertOrMerge #-}
insertOrMerge :: Monad m => (a -> a -> a) -> a -> Focus a m ()
insertOrMerge merge value = unitCases (Set value) (Set . merge value)
{-# INLINE alter #-}
alter :: Monad m => (Maybe a -> Maybe a) -> Focus a m ()
alter fn = unitCases (maybe Leave Set (fn Nothing)) (maybe Leave Set . fn . Just)
{-# INLINE adjust #-}
adjust :: Monad m => (a -> a) -> Focus a m ()
adjust fn = unitCases Leave (Set . fn)
{-# INLINE update #-}
update :: Monad m => (a -> Maybe a) -> Focus a m ()
update fn = unitCases Leave (maybe Leave Set . fn)
{-# INLINE cases #-}
cases :: Monad m => (b, Change a) -> (a -> (b, Change a)) -> Focus a m b
cases sendNone sendSome = Focus (return sendNone) (return . sendSome)
{-# INLINE unitCases #-}
unitCases :: Monad m => Change a -> (a -> Change a) -> Focus a m ()
unitCases sendNone sendSome = cases ((), sendNone) (\ a -> ((), sendSome a))
{-# INLINE[1] lookupWithDefaultM #-}
lookupWithDefaultM :: Monad m => m a -> Focus a m a
lookupWithDefaultM aM = casesM (liftM2 (,) aM (return Leave)) (\ a -> return (a, Leave))
{-# INLINE insertM #-}
insertM :: Monad m => m a -> Focus a m ()
insertM aM = unitCasesM (fmap Set aM) (const (fmap Set aM))
{-# INLINE insertOrMergeM #-}
insertOrMergeM :: Monad m => (a -> a -> m a) -> m a -> Focus a m ()
insertOrMergeM merge aM = unitCasesM (fmap Set aM) (\ a' -> aM >>= \ a -> fmap Set (merge a a'))
{-# INLINE alterM #-}
alterM :: Monad m => (Maybe a -> m (Maybe a)) -> Focus a m ()
alterM fn = unitCasesM (fmap (maybe Leave Set) (fn Nothing)) (fmap (maybe Leave Set) . fn . Just)
{-# INLINE adjustM #-}
adjustM :: Monad m => (a -> m a) -> Focus a m ()
adjustM fn = updateM (fmap Just . fn)
{-# INLINE updateM #-}
updateM :: Monad m => (a -> m (Maybe a)) -> Focus a m ()
updateM fn = unitCasesM (return Leave) (fmap (maybe Leave Set) . fn)
{-# INLINE casesM #-}
casesM :: Monad m => m (b, Change a) -> (a -> m (b, Change a)) -> Focus a m b
casesM sendNone sendSome = Focus sendNone sendSome
{-# INLINE unitCasesM #-}
unitCasesM :: Monad m => m (Change a) -> (a -> m (Change a)) -> Focus a m ()
unitCasesM sendNone sendSome = Focus (fmap ((),) sendNone) (\ a -> fmap ((),) (sendSome a))
{-# INLINE mappingInput #-}
mappingInput :: Monad m => (a -> b) -> (b -> a) -> Focus a m x -> Focus b m x
mappingInput aToB bToA (Focus consealA revealA) = Focus consealB revealB where
consealB = do
(x, aChange) <- consealA
return (x, fmap aToB aChange)
revealB b = do
(x, aChange) <- revealA (bToA b)
return (x, fmap aToB aChange)
{-# INLINE extractingInput #-}
extractingInput :: Monad m => Focus a m b -> Focus a m (b, Maybe a)
extractingInput (Focus absent present) =
Focus newAbsent newPresent
where
newAbsent = do
(b, change) <- absent
return ((b, Nothing), change)
newPresent element = do
(b, change) <- present element
return ((b, Just element), change)
{-# INLINE extractingChange #-}
extractingChange :: Monad m => Focus a m b -> Focus a m (b, Change a)
extractingChange (Focus absent present) =
Focus newAbsent newPresent
where
newAbsent = do
(b, change) <- absent
return ((b, change), change)
newPresent element = do
(b, change) <- present element
return ((b, change), change)
{-# INLINE projectingChange #-}
projectingChange :: Monad m => (Change a -> c) -> Focus a m b -> Focus a m (b, c)
projectingChange fn (Focus absent present) =
Focus newAbsent newPresent
where
newAbsent = do
(b, change) <- absent
return ((b, fn change), change)
newPresent element = do
(b, change) <- present element
return ((b, fn change), change)
{-# INLINE testingIfModifies #-}
testingIfModifies :: Monad m => Focus a m b -> Focus a m (b, Bool)
testingIfModifies =
projectingChange $ \ case
Leave -> False
_ -> True
{-# INLINE testingIfRemoves #-}
testingIfRemoves :: Monad m => Focus a m b -> Focus a m (b, Bool)
testingIfRemoves =
projectingChange $ \ case
Remove -> True
_ -> False
{-# INLINE testingIfInserts #-}
testingIfInserts :: Monad m => Focus a m b -> Focus a m (b, Bool)
testingIfInserts (Focus absent present) =
Focus newAbsent newPresent
where
newAbsent = do
(output, change) <- absent
let testResult = case change of
Set _ -> True
_ -> False
in return ((output, testResult), change)
newPresent element = do
(output, change) <- present element
return ((output, False), change)
{-# INLINE testingSizeChange #-}
testingSizeChange :: Monad m => sizeChange -> sizeChange -> sizeChange -> Focus a m b -> Focus a m (b, sizeChange)
testingSizeChange dec none inc (Focus absent present) =
Focus newAbsent newPresent
where
newAbsent = do
(output, change) <- absent
let sizeChange = case change of
Set _ -> inc
_ -> none
in return ((output, sizeChange), change)
newPresent element = do
(output, change) <- present element
let sizeChange = case change of
Remove -> dec
_ -> none
in return ((output, sizeChange), change)
{-# INLINE onTVarValue #-}
onTVarValue :: Focus a STM b -> Focus (TVar a) STM b
onTVarValue (Focus concealA presentA) = Focus concealTVar presentTVar where
concealTVar = concealA >>= traverse interpretAChange where
interpretAChange = \ case
Leave -> return Leave
Set !a -> Set <$> newTVar a
Remove -> return Leave
presentTVar var = readTVar var >>= presentA >>= traverse interpretAChange where
interpretAChange = \ case
Leave -> return Leave
Set !a -> writeTVar var a $> Leave
Remove -> return Remove