module Distribution.Compat.Lens (
Lens,
Lens',
Traversal,
Traversal',
LensLike,
LensLike',
Getting,
AGetter,
ASetter,
ALens,
ALens',
view,
use,
to,
set,
over,
toDListOf,
toListOf,
toSetOf,
traversed,
filtered,
cloneLens,
aview,
non,
_1, _2,
(&),
(^.),
(.~), (?~), (%~),
(.=), (?=), (%=),
(^#),
(#~), (#%~),
Pretext (..),
) where
import Prelude()
import Distribution.Compat.Prelude
import Control.Applicative (Const (..))
import Data.Functor.Identity (Identity (..))
import Control.Monad.State.Class (MonadState (..), gets, modify)
import Unsafe.Coerce
import qualified Distribution.Compat.DList as DList
import qualified Data.Set as Set
type LensLike f s t a b = (a -> f b) -> s -> f t
type LensLike' f s a = (a -> f a) -> s -> f s
type Lens s t a b = forall f. Functor f => LensLike f s t a b
type Traversal s t a b = forall f. Applicative f => LensLike f s t a b
type Lens' s a = Lens s s a a
type Traversal' s a = Traversal s s a a
type Getting r s a = LensLike (Const r) s s a a
type AGetter s a = LensLike (Const a) s s a a
type ASetter s t a b = LensLike Identity s t a b
type ALens s t a b = LensLike (Pretext a b) s t a b
type ALens' s a = ALens s s a a
view :: Getting a s a -> s -> a
view l s = getConst (l Const s)
use :: MonadState s m => Getting a s a -> m a
use l = gets (view l)
to :: (s -> a) -> AGetter s a
to k f = (unsafeCoerce :: Const t a -> Const t b) . f . k
set :: ASetter s t a b -> b -> s -> t
set l x = over l (const x)
over :: ASetter s t a b -> (a -> b) -> s -> t
over l f s = runIdentity (l (\x -> Identity (f x)) s)
toDListOf :: Getting (DList.DList a) s a -> s -> DList.DList a
toDListOf l s = getConst (l (\x -> Const (DList.singleton x)) s)
toListOf :: Getting (DList.DList a) s a -> s -> [a]
toListOf l = DList.runDList . toDListOf l
toSetOf :: Getting (Set.Set a) s a -> s -> Set.Set a
toSetOf l s = getConst (l (\x -> Const (Set.singleton x)) s)
filtered :: (a -> Bool) -> Traversal' a a
filtered p f s = if p s then f s else pure s
traversed :: Traversable f => Traversal (f a) (f b) a b
traversed = traverse
aview :: ALens s t a b -> s -> a
aview l = pretextPos . l pretextSell
non :: Eq a => a -> Lens' (Maybe a) a
non x afb s = f <$> afb (fromMaybe x s)
where f y = if x == y then Nothing else Just y
_1 :: Lens (a, c) (b, c) a b
_1 f (a, c) = flip (,) c <$> f a
_2 :: Lens (c, a) (c, b) a b
_2 f (c, a) = (,) c <$> f a
(&) :: a -> (a -> b) -> b
(&) = flip ($)
infixl 1 &
infixl 8 ^., ^#
infixr 4 .~, %~, ?~
infixr 4 #~, #%~
infixr 4 .=, %=, ?=
(^.) :: s -> Getting a s a -> a
s ^. l = getConst (l Const s)
(.~) :: ASetter s t a b -> b -> s -> t
(.~) = set
(?~) :: ASetter s t a (Maybe b) -> b -> s -> t
l ?~ b = set l (Just b)
(%~) :: ASetter s t a b -> (a -> b) -> s -> t
(%~) = over
(.=) :: MonadState s m => ASetter s s a b -> b -> m ()
l .= b = modify (l .~ b)
(?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m ()
l ?= b = modify (l ?~ b)
(%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()
l %= f = modify (l %~ f)
(^#) :: s -> ALens s t a b -> a
s ^# l = aview l s
(#~) :: ALens s t a b -> b -> s -> t
(#~) l b s = pretextPeek b (l pretextSell s)
(#%~) :: ALens s t a b -> (a -> b) -> s -> t
(#%~) l f s = pretextPeeks f (l pretextSell s)
pretextSell :: a -> Pretext a b b
pretextSell a = Pretext (\afb -> afb a)
pretextPeeks :: (a -> b) -> Pretext a b t -> t
pretextPeeks f (Pretext m) = runIdentity $ m (\x -> Identity (f x))
pretextPeek :: b -> Pretext a b t -> t
pretextPeek b (Pretext m) = runIdentity $ m (\_ -> Identity b)
pretextPos :: Pretext a b t -> a
pretextPos (Pretext m) = getConst (m Const)
cloneLens :: Functor f => ALens s t a b -> LensLike f s t a b
cloneLens l f s = runPretext (l pretextSell s) f
data Pretext a b t = Pretext { runPretext :: forall f. Functor f => (a -> f b) -> f t }
instance Functor (Pretext a b) where
fmap f (Pretext pretext) = Pretext (\afb -> fmap f (pretext afb))