#ifdef DEFAULT_SIGNATURES
#endif
#ifdef TRUSTWORTHY
#endif
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
module Control.Lens.At
(
At(at), sans
, IxValue
, Ixed(ix)
, ixAt, ixEach
, Contains(..)
, containsIx, containsAt, containsLength, containsN, containsTest, containsLookup
, _at
, resultAt
) where
import Control.Applicative
import Control.Lens.Combinators
import Control.Lens.Each
import Control.Lens.Fold
import Control.Lens.Getter
import Control.Lens.Indexed as Lens
import Control.Lens.Setter
import Control.Lens.Type
import Control.Lens.Traversal
import Data.Array.IArray as Array
import Data.Array.Unboxed
import Data.ByteString as StrictB
import Data.ByteString.Lazy as LazyB
import Data.Complex
import Data.Functor.Identity
import Data.Hashable
import Data.HashMap.Lazy as HashMap
import Data.HashSet as HashSet
import Data.IntMap as IntMap
import Data.IntSet as IntSet
import Data.Map as Map
import Data.Maybe
import Data.Monoid
import Data.Set as Set
import Data.Sequence as Seq
import Data.Text as StrictT
import Data.Text.Lazy as LazyT
import Data.Tree
import Data.Vector as Vector hiding (indexed)
import Data.Vector.Primitive as Prim
import Data.Vector.Storable as Storable
import Data.Vector.Unboxed as Unboxed
import Data.Word
_at, resultAt :: Ixed f m => Index m -> IndexedLensLike' (Index m) f m (IxValue m)
_at = ix
resultAt = ix
class Functor f => Contains f m where
contains :: Index m -> IndexedLensLike' (Index m) f m Bool
#ifdef DEFAULT_SIGNATURES
default contains :: (Contravariant f, Functor f, At m) => Index m -> IndexedLensLike' (Index m) f m Bool
contains = containsAt
#endif
containsIx :: (Contravariant f, Functor f, Ixed (Accessor Any) m) => Index m -> IndexedLensLike' (Index m) f m Bool
containsIx i f = coerce . Lens.indexed f i . has (ix i)
containsAt :: (Contravariant f, Functor f, At m) => Index m -> IndexedLensLike' (Index m) f m Bool
containsAt i f = coerce . Lens.indexed f i . views (at i) isJust
containsLength :: forall i s. (Ord i, Num i) => (s -> i) -> i -> IndexedGetter i s Bool
containsLength sn = \ i pafb s -> coerce $ Lens.indexed pafb (i :: i) (0 <= i && i < sn s)
containsN :: Int -> Int -> IndexedGetter Int s Bool
containsN n = \ i pafb _ -> coerce $ Lens.indexed pafb (i :: Int) (0 <= i && i < n)
containsTest :: forall i s. (i -> s -> Bool) -> i -> IndexedGetter i s Bool
containsTest isb = \i pafb s -> coerce $ Lens.indexed pafb (i :: i) (isb i s)
containsLookup :: forall i s a. (i -> s -> Maybe a) -> i -> IndexedGetter i s Bool
containsLookup isb = \i pafb s -> coerce $ Lens.indexed pafb (i :: i) (isJust (isb i s))
instance (Functor f, Contravariant f) => Contains f (e -> a) where
contains i f _ = coerce (Lens.indexed f i True)
instance Functor f => Contains f IntSet where
contains k f s = Lens.indexed f k (IntSet.member k s) <&> \b ->
if b then IntSet.insert k s else IntSet.delete k s
instance (Functor f, Ord a) => Contains f (Set a) where
contains k f s = Lens.indexed f k (Set.member k s) <&> \b ->
if b then Set.insert k s else Set.delete k s
instance (Functor f, Eq a, Hashable a) => Contains f (HashSet a) where
contains k f s = Lens.indexed f k (HashSet.member k s) <&> \b ->
if b then HashSet.insert k s else HashSet.delete k s
instance (Contravariant f, Functor f) => Contains f [a] where
contains = containsLength Prelude.length
instance (Contravariant f, Functor f) => Contains f (Seq a) where
contains = containsLength Seq.length
#if MIN_VERSION_base(4,4,0)
instance (Contravariant f, Functor f) => Contains f (Complex a) where
contains = containsN 2
#else
instance (Contravariant f, Functor f, RealFloat a) => Contains f (Complex a) where
contains = containsN 2
#endif
instance (Contravariant f, Functor f) => Contains f (Tree a) where
contains xs0 pafb = coerce . Lens.indexed pafb xs0 . go xs0 where
go [] (Node _ _) = True
go (i:is) (Node _ as) | i < 0 = False
| otherwise = goto i is as
goto 0 is (a:_) = go is a
goto _ _ [] = False
goto n is (_:as) = (goto $! n 1) is as
instance (Contravariant k, Functor k) => Contains k (Identity a) where
contains () f _ = coerce (Lens.indexed f () True)
instance (Contravariant k, Functor k) => Contains k (a,b) where
contains = containsN 2
instance (Contravariant k, Functor k) => Contains k (a,b,c) where
contains = containsN 3
instance (Contravariant k, Functor k) => Contains k (a,b,c,d) where
contains = containsN 4
instance (Contravariant k, Functor k) => Contains k (a,b,c,d,e) where
contains = containsN 5
instance (Contravariant k, Functor k) => Contains k (a,b,c,d,e,f) where
contains = containsN 6
instance (Contravariant k, Functor k) => Contains k (a,b,c,d,e,f,g) where
contains = containsN 7
instance (Contravariant k, Functor k) => Contains k (a,b,c,d,e,f,g,h) where
contains = containsN 8
instance (Contravariant k, Functor k) => Contains k (a,b,c,d,e,f,g,h,i) where
contains = containsN 9
instance (Contravariant k, Functor k) => Contains k (IntMap a) where
contains = containsLookup IntMap.lookup
instance (Contravariant f, Functor f, Ord k) => Contains f (Map k a) where
contains = containsLookup Map.lookup
instance (Contravariant f, Functor f, Eq k, Hashable k) => Contains f (HashMap k a) where
contains = containsLookup HashMap.lookup
instance (Contravariant f, Functor f, Ix i) => Contains f (Array i e) where
contains = containsTest $ \i s -> inRange (bounds s) i
instance (Contravariant f, Functor f, IArray UArray e, Ix i) => Contains f (UArray i e) where
contains = containsTest $ \i s -> inRange (bounds s) i
instance (Contravariant f, Functor f) => Contains f (Vector.Vector a) where
contains = containsLength Vector.length
instance (Contravariant f, Functor f, Prim a) => Contains f (Prim.Vector a) where
contains = containsLength Prim.length
instance (Contravariant f, Functor f, Storable a) => Contains f (Storable.Vector a) where
contains = containsLength Storable.length
instance (Contravariant f, Functor f, Unbox a) => Contains f (Unboxed.Vector a) where
contains = containsLength Unboxed.length
instance (Contravariant f, Functor f) => Contains f StrictT.Text where
contains = containsTest $ \i s -> StrictT.compareLength s i == GT
instance (Contravariant f, Functor f) => Contains f LazyT.Text where
contains = containsTest $ \i s -> LazyT.compareLength s i == GT
instance (Contravariant f, Functor f) => Contains f StrictB.ByteString where
contains = containsLength StrictB.length
instance (Contravariant f, Functor f) => Contains f LazyB.ByteString where
contains = containsTest $ \i s -> not (LazyB.null (LazyB.drop i s))
type family IxValue (m :: *) :: *
class Functor f => Ixed f m where
ix :: Index m -> IndexedLensLike' (Index m) f m (IxValue m)
#ifdef DEFAULT_SIGNATURES
default ix :: (Applicative f, At m) => Index m -> IndexedLensLike' (Index m) f m (IxValue m)
ix = ixAt
#endif
ixAt :: (Applicative f, At m) => Index m -> IndexedLensLike' (Index m) f m (IxValue m)
ixAt i = at i <. traverse
ixEach :: (Applicative f, Eq (Index m), Each f m m (IxValue m) (IxValue m)) => Index m -> IndexedLensLike' (Index m) f m (IxValue m)
ixEach i = each . Lens.index i
type instance IxValue [a] = a
instance Applicative f => Ixed f [a] where
ix k f xs0 | k < 0 = pure xs0
| otherwise = go xs0 k where
go [] _ = pure []
go (a:as) 0 = Lens.indexed f k a <&> (:as)
go (a:as) i = (a:) <$> (go as $! i 1)
type instance IxValue (Identity a) = a
instance Functor f => Ixed f (Identity a) where
ix () f (Identity a) = Identity <$> Lens.indexed f () a
type instance IxValue (Tree a) = a
instance Applicative f => Ixed f (Tree a) where
ix xs0 f = go xs0 where
go [] (Node a as) = Lens.indexed f xs0 a <&> \a' -> Node a' as
go (i:is) t@(Node a as) | i < 0 = pure t
| otherwise = Node a <$> goto is as i
goto is (a:as) 0 = go is a <&> (:as)
goto is (_:as) n = goto is as $! n 1
goto _ [] _ = pure []
type instance IxValue (Seq a) = a
instance Applicative f => Ixed f (Seq a) where
ix i f m
| 0 <= i && i < Seq.length m = Lens.indexed f i (Seq.index m i) <&> \a -> Seq.update i a m
| otherwise = pure m
type instance IxValue (IntMap a) = a
instance Applicative f => Ixed f (IntMap a) where
ix k f m = case IntMap.lookup k m of
Just v -> Lens.indexed f k v <&> \v' -> IntMap.insert k v' m
Nothing -> pure m
type instance IxValue (Map k a) = a
instance (Applicative f, Ord k) => Ixed f (Map k a) where
ix k f m = case Map.lookup k m of
Just v -> Lens.indexed f k v <&> \v' -> Map.insert k v' m
Nothing -> pure m
type instance IxValue (HashMap k a) = a
instance (Applicative f, Eq k, Hashable k) => Ixed f (HashMap k a) where
ix k f m = case HashMap.lookup k m of
Just v -> Lens.indexed f k v <&> \v' -> HashMap.insert k v' m
Nothing -> pure m
type instance IxValue (Array i e) = e
instance (Applicative f, Ix i) => Ixed f (Array i e) where
ix i f arr
| inRange (bounds arr) i = Lens.indexed f i (arr Array.! i) <&> \e -> arr Array.// [(i,e)]
| otherwise = pure arr
type instance IxValue (UArray i e) = e
instance (Applicative f, IArray UArray e, Ix i) => Ixed f (UArray i e) where
ix i f arr
| inRange (bounds arr) i = Lens.indexed f i (arr Array.! i) <&> \e -> arr Array.// [(i,e)]
| otherwise = pure arr
type instance IxValue (Vector.Vector a) = a
instance Applicative f => Ixed f (Vector.Vector a) where
ix i f v
| 0 <= i && i < Vector.length v = Lens.indexed f i (v Vector.! i) <&> \a -> v Vector.// [(i, a)]
| otherwise = pure v
type instance IxValue (Prim.Vector a) = a
instance (Applicative f, Prim a) => Ixed f (Prim.Vector a) where
ix i f v
| 0 <= i && i < Prim.length v = Lens.indexed f i (v Prim.! i) <&> \a -> v Prim.// [(i, a)]
| otherwise = pure v
type instance IxValue (Storable.Vector a) = a
instance (Applicative f, Storable a) => Ixed f (Storable.Vector a) where
ix i f v
| 0 <= i && i < Storable.length v = Lens.indexed f i (v Storable.! i) <&> \a -> v Storable.// [(i, a)]
| otherwise = pure v
type instance IxValue (Unboxed.Vector a) = a
instance (Applicative f, Unbox a) => Ixed f (Unboxed.Vector a) where
ix i f v
| 0 <= i && i < Unboxed.length v = Lens.indexed f i (v Unboxed.! i) <&> \a -> v Unboxed.// [(i, a)]
| otherwise = pure v
type instance IxValue StrictT.Text = Char
instance Applicative f => Ixed f StrictT.Text where
ix e f s = case StrictT.splitAt e s of
(l, mr) -> case StrictT.uncons mr of
Nothing -> pure s
Just (c, xs) -> Lens.indexed f e c <&> \d -> StrictT.concat [l, StrictT.singleton d, xs]
type instance IxValue LazyT.Text = Char
instance Applicative f => Ixed f LazyT.Text where
ix e f s = case LazyT.splitAt e s of
(l, mr) -> case LazyT.uncons mr of
Nothing -> pure s
Just (c, xs) -> Lens.indexed f e c <&> \d -> LazyT.append l (LazyT.cons d xs)
type instance IxValue StrictB.ByteString = Word8
instance Applicative f => Ixed f StrictB.ByteString where
ix e f s = case StrictB.splitAt e s of
(l, mr) -> case StrictB.uncons mr of
Nothing -> pure s
Just (c, xs) -> Lens.indexed f e c <&> \d -> StrictB.concat [l, StrictB.singleton d, xs]
type instance IxValue LazyB.ByteString = Word8
instance Applicative f => Ixed f LazyB.ByteString where
ix e f s = case LazyB.splitAt e s of
(l, mr) -> case LazyB.uncons mr of
Nothing -> pure s
Just (c, xs) -> Lens.indexed f e c <&> \d -> LazyB.append l (LazyB.cons d xs)
type instance IxValue (k -> a) = a
instance (Functor f, Eq k) => Ixed f (k -> a) where
ix e g f = Lens.indexed g e (f e) <&> \a' e' -> if e == e' then a' else f e'
#if MIN_VERSION_base(4,4,0)
type instance IxValue (Complex a) = a
instance Applicative f => Ixed f (Complex a) where
ix = ixEach
#else
instance (Applicative f, RealFloat a) => Ixed f (Complex a) where
ix = ixEach
#endif
type instance IxValue (a,a) = a
instance (Applicative f, a ~ b) => Ixed f (a,b) where
ix = ixEach
type instance IxValue (a,a,a) = a
instance (Applicative f, a ~ b, b ~ c) => Ixed f (a,b,c) where
ix = ixEach
type instance IxValue (a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d) => Ixed f (a,b,c,d) where
ix = ixEach
type instance IxValue (a,a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d, d ~ e) => Ixed f (a,b,c,d,e) where
ix = ixEach
type instance IxValue (a,a,a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d, d ~ e, e ~ f') => Ixed f (a,b,c,d,e,f') where
ix = ixEach
type instance IxValue (a,a,a,a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d, d ~ e, e ~ f', f' ~ g) => Ixed f (a,b,c,d,e,f',g) where
ix = ixEach
type instance IxValue (a,a,a,a,a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d, d ~ e, e ~ f', f' ~ g, g ~ h) => Ixed f (a,b,c,d,e,f',g,h) where
ix = ixEach
type instance IxValue (a,a,a,a,a,a,a,a,a) = a
instance (Applicative f, a ~ b, b ~ c, c ~ d, d ~ e, e ~ f', f' ~ g, g ~ h, h ~ i) => Ixed f (a,b,c,d,e,f',g,h,i) where
ix = ixEach
class At m where
at :: Index m -> IndexedLens' (Index m) m (Maybe (IxValue m))
sans :: At m => Index m -> m -> m
sans k m = m & at k .~ Nothing
instance At (IntMap a) where
at k f m = Lens.indexed f k mv <&> \r -> case r of
Nothing -> maybe m (const (IntMap.delete k m)) mv
Just v' -> IntMap.insert k v' m
where mv = IntMap.lookup k m
instance Ord k => At (Map k a) where
at k f m = Lens.indexed f k mv <&> \r -> case r of
Nothing -> maybe m (const (Map.delete k m)) mv
Just v' -> Map.insert k v' m
where mv = Map.lookup k m
instance (Eq k, Hashable k) => At (HashMap k a) where
at k f m = Lens.indexed f k mv <&> \r -> case r of
Nothing -> maybe m (const (HashMap.delete k m)) mv
Just v' -> HashMap.insert k v' m
where mv = HashMap.lookup k m