module Control.Lens.Internal
(
IndexedStore(..)
, Focusing(..)
, Traversed(..)
, Action(..)
, AppliedState(..)
, Backwards(..)
, Min(..)
, getMin
, Max(..)
, getMax
) where
import Control.Applicative
import Data.Monoid
newtype Focusing m c a = Focusing { unfocusing :: m (c, a) }
instance Monad m => Functor (Focusing m c) where
fmap f (Focusing m) = Focusing $ do
(c, a) <- m
return (c, f a)
instance (Monad m, Monoid c) => Applicative (Focusing m c) where
pure a = Focusing (return (mempty, a))
Focusing mf <*> Focusing ma = Focusing $ do
(c, f) <- mf
(d, a) <- ma
return (mappend c d, f a)
data IndexedStore c d a = IndexedStore (d -> a) c
instance Functor (IndexedStore c d) where
fmap f (IndexedStore g c) = IndexedStore (f . g) c
newtype AppliedState f a = AppliedState { runAppliedState :: Int -> (f a, Int) }
instance Functor f => Functor (AppliedState f) where
fmap f (AppliedState m) = AppliedState $ \i -> case m i of
(fa, j) -> (fmap f fa, j)
instance Applicative f => Applicative (AppliedState f) where
pure a = AppliedState (\i -> (pure a, i))
AppliedState mf <*> AppliedState ma = AppliedState $ \i -> case mf i of
(ff, j) -> case ma j of
(fa, k) -> (ff <*> fa, k)
newtype Traversed f = Traversed { getTraversed :: f () }
instance Applicative f => Monoid (Traversed f) where
mempty = Traversed (pure ())
Traversed ma `mappend` Traversed mb = Traversed (ma *> mb)
newtype Action m = Action { getAction :: m () }
instance Monad m => Monoid (Action m) where
mempty = Action (return ())
Action ma `mappend` Action mb = Action (ma >> mb)
data Min a = NoMin | Min a
instance Ord a => Monoid (Min a) where
mempty = NoMin
mappend NoMin m = m
mappend m NoMin = m
mappend (Min a) (Min b) = Min (min a b)
getMin :: Min a -> Maybe a
getMin NoMin = Nothing
getMin (Min a) = Just a
data Max a = NoMax | Max a
instance Ord a => Monoid (Max a) where
mempty = NoMax
mappend NoMax m = m
mappend m NoMax = m
mappend (Max a) (Max b) = Max (max a b)
getMax :: Max a -> Maybe a
getMax NoMax = Nothing
getMax (Max a) = Just a
newtype Backwards f a = Backwards { getBackwards :: f a }
instance Functor f => Functor (Backwards f) where
fmap f (Backwards as) = Backwards (fmap f as)
instance Applicative f => Applicative (Backwards f) where
pure = Backwards . pure
Backwards f <*> Backwards a = Backwards (flip id <$> a <*> f)