    {-# LANGUAGE CPP #-}
    {-# LANGUAGE GADTs #-}
    {-# LANGUAGE Rank2Types #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE DefaultSignatures #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE UndecidableInstances #-}
    {-# LANGUAGE TypeOperators #-}

    #ifdef TRUSTWORTHY
    {-# LANGUAGE Trustworthy #-}
    #endif

    #if __GLASGOW_HASKELL__ >= 711
    {-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
    #endif

    #ifndef MIN_VERSION_base
    #define MIN_VERSION_base(x,y,z) 1
    #endif

    #ifndef MIN_VERSION_containers
    #define MIN_VERSION_containers(x,y,z) 1
    #endif
    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Control.Lens.At
    -- Copyright   :  (C) 2012-16 Edward Kmett
    -- License     :  BSD-style (see the file LICENSE)
    -- Maintainer  :  Edward Kmett <ekmett@gmail.com>
    -- Stability   :  experimental
    -- Portability :  non-portable
    --
    ----------------------------------------------------------------------------
    module Control.Lens.At
      (
      -- * At
        At(at)
        , sans
        , iat
      -- * Ixed
      , Index
      , IxValue
      , Ixed(ix)
      , ixAt
      , iix
      -- * Contains
      , Contains(contains)
      , icontains
      ) where

    import Control.Lens.Each
    import Control.Lens.Traversal
    import Control.Lens.Lens
    import Control.Lens.Setter
    import Control.Lens.Type
    import Control.Lens.Indexed
    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.Hashable
    import Data.HashMap.Lazy as HashMap
    import Data.HashSet as HashSet
    import Data.Int
    import Data.IntMap as IntMap
    import Data.IntSet as IntSet
    import Data.List.NonEmpty as NonEmpty
    import Data.Map as Map
    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 hiding (indexed)
    import Data.Word

    #if !MIN_VERSION_base(4,8,0)
    import Control.Applicative
    #endif

    type family Index (s :: *) :: *
    type instance Index (e -> a) = e
    type instance Index IntSet = Int
    type instance Index (Set a) = a
    type instance Index (HashSet a) = a
    type instance Index [a] = Int
    type instance Index (NonEmpty a) = Int
    type instance Index (Seq a) = Int
    type instance Index (a,b) = Int
    type instance Index (a,b,c) = Int
    type instance Index (a,b,c,d) = Int
    type instance Index (a,b,c,d,e) = Int
    type instance Index (a,b,c,d,e,f) = Int
    type instance Index (a,b,c,d,e,f,g) = Int
    type instance Index (a,b,c,d,e,f,g,h) = Int
    type instance Index (a,b,c,d,e,f,g,h,i) = Int
    type instance Index (IntMap a) = Int
    type instance Index (Map k a) = k
    type instance Index (HashMap k a) = k
    type instance Index (Array.Array i e) = i
    type instance Index (UArray i e) = i
    type instance Index (Vector.Vector a) = Int
    type instance Index (Prim.Vector a) = Int
    type instance Index (Storable.Vector a) = Int
    type instance Index (Unboxed.Vector a) = Int
    type instance Index (Complex a) = Int
    type instance Index (Identity a) = ()
    type instance Index (Maybe a) = ()
    type instance Index (Tree a) = [Int]
    type instance Index StrictT.Text = Int
    type instance Index LazyT.Text = Int64
    type instance Index StrictB.ByteString = Int
    type instance Index LazyB.ByteString = Int64

    -- $setup
    -- >>> :set -XNoOverloadedStrings
    -- >>> import Control.Lens
    -- >>> import Debug.SimpleReflect.Expr
    -- >>> import Debug.SimpleReflect.Vars as Vars hiding (f,g)
    -- >>> let f  :: Expr -> Expr; f = Debug.SimpleReflect.Vars.f
    -- >>> let g  :: Expr -> Expr; g = Debug.SimpleReflect.Vars.g
    -- >>> let f' :: Int -> Expr -> Expr; f' = Debug.SimpleReflect.Vars.f'
    -- >>> let h  :: Int -> Expr; h = Debug.SimpleReflect.Vars.h

    -- |
    -- This class provides a simple 'Lens' that lets you view (and modify)
    -- information about whether or not a container contains a given 'Index'.
    class Contains m where
      -- |
      -- >>> IntSet.fromList [1,2,3,4] ^. contains 3
      -- True
      --
      -- >>> IntSet.fromList [1,2,3,4] ^. contains 5
      -- False
      --
      -- >>> IntSet.fromList [1,2,3,4] & contains 3 .~ False
      -- fromList [1,2,4]
      contains :: Index m -> Lens' m Bool

    -- | An indexed version of 'contains'.
    --
    -- >>> IntSet.fromList [1,2,3,4] ^@. icontains 3
    -- (3,True)
    --
    -- >>> IntSet.fromList [1,2,3,4] ^@. icontains 5
    -- (5,False)
    --
    -- >>> IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if odd i then not x else x
    -- fromList [1,2,4]
    --
    -- >>> IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if even i then not x else x
    -- fromList [1,2,3,4]
    icontains :: Contains m => Index m -> IndexedLens' (Index m) m Bool
    icontains :: Index m -> IndexedLens' (Index m) m Bool
    icontains i :: Index m
    i f :: p Bool (f Bool)
    f = Index m -> (Bool -> f Bool) -> m -> f m
    forall m. Contains m => Index m -> Lens' m Bool
    contains Index m
    i (p Bool (f Bool) -> Index m -> Bool -> f Bool
    forall i (p :: * -> * -> *) a b.
    Indexable i p =>
    p a b -> i -> a -> b
    indexed p Bool (f Bool)
    f Index m
    i)
    {-# INLINE icontains #-}

    instance Contains IntSet where
      contains :: Index IntSet -> Lens' IntSet Bool
    contains k :: Index IntSet
    k f :: Bool -> f Bool
    f s :: IntSet
    s = Bool -> f Bool
    f (Key -> IntSet -> Bool
    IntSet.member Key
    Index IntSet
    k IntSet
    s) f Bool -> (Bool -> IntSet) -> f IntSet
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \b :: Bool
    b ->
        if Bool
    b then Key -> IntSet -> IntSet
    IntSet.insert Key
    Index IntSet
    k IntSet
    s else Key -> IntSet -> IntSet
    IntSet.delete Key
    Index IntSet
    k IntSet
    s
      {-# INLINE contains #-}

    instance Ord a => Contains (Set a) where
      contains :: Index (Set a) -> Lens' (Set a) Bool
    contains k :: Index (Set a)
    k f :: Bool -> f Bool
    f s :: Set a
    s = Bool -> f Bool
    f (a -> Set a -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member a
    Index (Set a)
    k Set a
    s) f Bool -> (Bool -> Set a) -> f (Set a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \b :: Bool
    b ->
        if Bool
    b then a -> Set a -> Set a
    forall a. Ord a => a -> Set a -> Set a
    Set.insert a
    Index (Set a)
    k Set a
    s else a -> Set a -> Set a
    forall a. Ord a => a -> Set a -> Set a
    Set.delete a
    Index (Set a)
    k Set a
    s
      {-# INLINE contains #-}

    instance (Eq a, Hashable a) => Contains (HashSet a) where
      contains :: Index (HashSet a) -> Lens' (HashSet a) Bool
    contains k :: Index (HashSet a)
    k f :: Bool -> f Bool
    f s :: HashSet a
    s = Bool -> f Bool
    f (a -> HashSet a -> Bool
    forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
    HashSet.member a
    Index (HashSet a)
    k HashSet a
    s) f Bool -> (Bool -> HashSet a) -> f (HashSet a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \b :: Bool
    b ->
        if Bool
    b then a -> HashSet a -> HashSet a
    forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
    HashSet.insert a
    Index (HashSet a)
    k HashSet a
    s else a -> HashSet a -> HashSet a
    forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
    HashSet.delete a
    Index (HashSet a)
    k HashSet a
    s
      {-# INLINE contains #-}

    -- | This provides a common notion of a value at an index that is shared by both 'Ixed' and 'At'.
    type family IxValue (m :: *) :: *

    -- | Provides a simple 'Traversal' lets you 'traverse' the value at a given
    -- key in a 'Map' or element at an ordinal position in a list or 'Seq'.
    class Ixed m where
      -- |
      -- /NB:/ Setting the value of this 'Traversal' will only set the value in
      -- 'at' if it is already present.
      --
      -- If you want to be able to insert /missing/ values, you want 'at'.
      --
      -- >>> Seq.fromList [a,b,c,d] & ix 2 %~ f
      -- fromList [a,b,f c,d]
      --
      -- >>> Seq.fromList [a,b,c,d] & ix 2 .~ e
      -- fromList [a,b,e,d]
      --
      -- >>> Seq.fromList [a,b,c,d] ^? ix 2
      -- Just c
      --
      -- >>> Seq.fromList [] ^? ix 2
      -- Nothing
      ix :: Index m -> Traversal' m (IxValue m)
      default ix :: (Applicative f, At m) => Index m -> LensLike' f m (IxValue m)
      ix = Index m -> LensLike' f m (IxValue m)
    forall m. At m => Index m -> Traversal' m (IxValue m)
    ixAt
      {-# INLINE ix #-}

    -- | An indexed version of 'ix'.
    --
    -- >>> Seq.fromList [a,b,c,d] & iix 2 %@~ f'
    -- fromList [a,b,f' 2 c,d]
    --
    -- >>> Seq.fromList [a,b,c,d] & iix 2 .@~ h
    -- fromList [a,b,h 2,d]
    --
    -- >>> Seq.fromList [a,b,c,d] ^@? iix 2
    -- Just (2,c)
    --
    -- >>> Seq.fromList [] ^@? iix 2
    -- Nothing
    iix :: Ixed m => Index m -> IndexedTraversal' (Index m) m (IxValue m)
    iix :: Index m -> IndexedTraversal' (Index m) m (IxValue m)
    iix i :: Index m
    i f :: p (IxValue m) (f (IxValue m))
    f = Index m -> (IxValue m -> f (IxValue m)) -> m -> f m
    forall m. Ixed m => Index m -> Traversal' m (IxValue m)
    ix Index m
    i (p (IxValue m) (f (IxValue m))
    -> Index m -> IxValue m -> f (IxValue m)
    forall i (p :: * -> * -> *) a b.
    Indexable i p =>
    p a b -> i -> a -> b
    indexed p (IxValue m) (f (IxValue m))
    f Index m
    i)
    {-# INLINE iix #-}

    -- | A definition of 'ix' for types with an 'At' instance. This is the default
    -- if you don't specify a definition for 'ix'.
    ixAt :: At m => Index m -> Traversal' m (IxValue m)
    ixAt :: Index m -> Traversal' m (IxValue m)
    ixAt i :: Index m
    i = Index m -> Lens' m (Maybe (IxValue m))
    forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
    at Index m
    i ((Maybe (IxValue m) -> f (Maybe (IxValue m))) -> m -> f m)
    -> ((IxValue m -> f (IxValue m))
        -> Maybe (IxValue m) -> f (Maybe (IxValue m)))
    -> (IxValue m -> f (IxValue m))
    -> m
    -> f m
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (IxValue m -> f (IxValue m))
    -> Maybe (IxValue m) -> f (Maybe (IxValue m))
    forall (t :: * -> *) (f :: * -> *) a b.
    (Traversable t, Applicative f) =>
    (a -> f b) -> t a -> f (t b)
    traverse
    {-# INLINE ixAt #-}

    type instance IxValue (e -> a) = a
    instance Eq e => Ixed (e -> a) where
      ix :: Index (e -> a) -> Traversal' (e -> a) (IxValue (e -> a))
    ix e :: Index (e -> a)
    e p :: IxValue (e -> a) -> f (IxValue (e -> a))
    p f :: e -> a
    f = IxValue (e -> a) -> f (IxValue (e -> a))
    p (e -> a
    f e
    Index (e -> a)
    e) f a -> (a -> e -> a) -> f (e -> a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a e' :: e
    e' -> if e
    Index (e -> a)
    e e -> e -> Bool
    forall a. Eq a => a -> a -> Bool
    == e
    e' then a
    a else e -> a
    f e
    e'
      {-# INLINE ix #-}

    type instance IxValue (Maybe a) = a
    instance Ixed (Maybe a) where
      ix :: Index (Maybe a) -> Traversal' (Maybe a) (IxValue (Maybe a))
    ix () f :: IxValue (Maybe a) -> f (IxValue (Maybe a))
    f (Just a :: a
    a) = a -> Maybe a
    forall a. a -> Maybe a
    Just (a -> Maybe a) -> f a -> f (Maybe a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> IxValue (Maybe a) -> f (IxValue (Maybe a))
    f a
    IxValue (Maybe a)
    a
      ix () _ Nothing  = Maybe a -> f (Maybe a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Maybe a
    forall a. Maybe a
    Nothing
      {-# INLINE ix #-}

    type instance IxValue [a] = a
    instance Ixed [a] where
      ix :: Index [a] -> Traversal' [a] (IxValue [a])
    ix k :: Index [a]
    k f :: IxValue [a] -> f (IxValue [a])
    f xs0 :: [a]
    xs0 | Key
    Index [a]
    k Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0     = [a] -> f [a]
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure [a]
    xs0
                 | Bool
    otherwise = [a] -> Key -> f [a]
    go [a]
    xs0 Key
    Index [a]
    k where
        go :: [a] -> Key -> f [a]
    go [] _ = [a] -> f [a]
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure []
        go (a :: a
    a:as :: [a]
    as) 0 = IxValue [a] -> f (IxValue [a])
    f a
    IxValue [a]
    a f a -> (a -> [a]) -> f [a]
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> (a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :[a]
    as)
        go (a :: a
    a:as :: [a]
    as) i :: Key
    i = (a
    aa -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :) ([a] -> [a]) -> f [a] -> f [a]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ([a] -> Key -> f [a]
    go [a]
    as (Key -> f [a]) -> Key -> f [a]
    forall a b. (a -> b) -> a -> b
    $! Key
    i Key -> Key -> Key
    forall a. Num a => a -> a -> a
    - 1)
      {-# INLINE ix #-}

    type instance IxValue (NonEmpty a) = a
    instance Ixed (NonEmpty a) where
      ix :: Index (NonEmpty a)
    -> Traversal' (NonEmpty a) (IxValue (NonEmpty a))
    ix k :: Index (NonEmpty a)
    k f :: IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
    f xs0 :: NonEmpty a
    xs0 | Key
    Index (NonEmpty a)
    k Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0 = NonEmpty a -> f (NonEmpty a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure NonEmpty a
    xs0
                 | Bool
    otherwise = NonEmpty a -> Key -> f (NonEmpty a)
    go NonEmpty a
    xs0 Key
    Index (NonEmpty a)
    k where
        go :: NonEmpty a -> Key -> f (NonEmpty a)
    go (a :: a
    a:|as :: [a]
    as) 0 = IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
    f a
    IxValue (NonEmpty a)
    a f a -> (a -> NonEmpty a) -> f (NonEmpty a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> (a -> [a] -> NonEmpty a
    forall a. a -> [a] -> NonEmpty a
    :|[a]
    as)
        go (a :: a
    a:|as :: [a]
    as) i :: Key
    i = (a
    aa -> [a] -> NonEmpty a
    forall a. a -> [a] -> NonEmpty a
    :|) ([a] -> NonEmpty a) -> f [a] -> f (NonEmpty a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Index [a] -> (IxValue [a] -> f (IxValue [a])) -> [a] -> f [a]
    forall m. Ixed m => Index m -> Traversal' m (IxValue m)
    ix (Key
    i Key -> Key -> Key
    forall a. Num a => a -> a -> a
    - 1) IxValue [a] -> f (IxValue [a])
    IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
    f [a]
    as
      {-# INLINE ix #-}

    type instance IxValue (Identity a) = a
    instance Ixed (Identity a) where
      ix :: Index (Identity a)
    -> Traversal' (Identity a) (IxValue (Identity a))
    ix () f :: IxValue (Identity a) -> f (IxValue (Identity a))
    f (Identity a :: a
    a) = a -> Identity a
    forall a. a -> Identity a
    Identity (a -> Identity a) -> f a -> f (Identity a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> IxValue (Identity a) -> f (IxValue (Identity a))
    f a
    IxValue (Identity a)
    a
      {-# INLINE ix #-}

    type instance IxValue (Tree a) = a
    instance Ixed (Tree a) where
      ix :: Index (Tree a) -> Traversal' (Tree a) (IxValue (Tree a))
    ix xs0 :: Index (Tree a)
    xs0 f :: IxValue (Tree a) -> f (IxValue (Tree a))
    f = [Key] -> Tree a -> f (Tree a)
    go [Key]
    Index (Tree a)
    xs0 where
        go :: [Key] -> Tree a -> f (Tree a)
    go [] (Node a :: a
    a as :: Forest a
    as) = IxValue (Tree a) -> f (IxValue (Tree a))
    f a
    IxValue (Tree a)
    a f a -> (a -> Tree a) -> f (Tree a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a' :: a
    a' -> a -> Forest a -> Tree a
    forall a. a -> Forest a -> Tree a
    Node a
    a' Forest a
    as
        go (i :: Key
    i:is :: [Key]
    is) t :: Tree a
    t@(Node a :: a
    a as :: Forest a
    as)
          | Key
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < 0     = Tree a -> f (Tree a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Tree a
    t
          | Bool
    otherwise = a -> Forest a -> Tree a
    forall a. a -> Forest a -> Tree a
    Node a
    a (Forest a -> Tree a) -> f (Forest a) -> f (Tree a)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Index (Forest a)
    -> (IxValue (Forest a) -> f (IxValue (Forest a)))
    -> Forest a
    -> f (Forest a)
    forall m. Ixed m => Index m -> Traversal' m (IxValue m)
    ix Key
    Index (Forest a)
    i ([Key] -> Tree a -> f (Tree a)
    go [Key]
    is) Forest a
    as
      {-# INLINE ix #-}

    type instance IxValue (Seq a) = a
    instance Ixed (Seq a) where
      ix :: Index (Seq a) -> Traversal' (Seq a) (IxValue (Seq a))
    ix i :: Index (Seq a)
    i f :: IxValue (Seq a) -> f (IxValue (Seq a))
    f m :: Seq a
    m
        | 0 Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Key
    Index (Seq a)
    i Bool -> Bool -> Bool
    && Key
    Index (Seq a)
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Seq a -> Key
    forall a. Seq a -> Key
    Seq.length Seq a
    m = IxValue (Seq a) -> f (IxValue (Seq a))
    f (Seq a -> Key -> a
    forall a. Seq a -> Key -> a
    Seq.index Seq a
    m Key
    Index (Seq a)
    i) f a -> (a -> Seq a) -> f (Seq a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a -> Key -> a -> Seq a -> Seq a
    forall a. Key -> a -> Seq a -> Seq a
    Seq.update Key
    Index (Seq a)
    i a
    a Seq a
    m
        | Bool
    otherwise                  = Seq a -> f (Seq a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Seq a
    m
      {-# INLINE ix #-}

    type instance IxValue (IntMap a) = a
    instance Ixed (IntMap a) where
      ix :: Index (IntMap a) -> Traversal' (IntMap a) (IxValue (IntMap a))
    ix k :: Index (IntMap a)
    k f :: IxValue (IntMap a) -> f (IxValue (IntMap a))
    f m :: IntMap a
    m = case Key -> IntMap a -> Maybe a
    forall a. Key -> IntMap a -> Maybe a
    IntMap.lookup Key
    Index (IntMap a)
    k IntMap a
    m of
         Just v :: a
    v -> IxValue (IntMap a) -> f (IxValue (IntMap a))
    f a
    IxValue (IntMap a)
    v f a -> (a -> IntMap a) -> f (IntMap a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \v' :: a
    v' -> Key -> a -> IntMap a -> IntMap a
    forall a. Key -> a -> IntMap a -> IntMap a
    IntMap.insert Key
    Index (IntMap a)
    k a
    v' IntMap a
    m
         Nothing -> IntMap a -> f (IntMap a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure IntMap a
    m
      {-# INLINE ix #-}

    type instance IxValue (Map k a) = a
    instance Ord k => Ixed (Map k a) where
      ix :: Index (Map k a) -> Traversal' (Map k a) (IxValue (Map k a))
    ix k :: Index (Map k a)
    k f :: IxValue (Map k a) -> f (IxValue (Map k a))
    f m :: Map k a
    m = case k -> Map k a -> Maybe a
    forall k a. Ord k => k -> Map k a -> Maybe a
    Map.lookup k
    Index (Map k a)
    k Map k a
    m of
         Just v :: a
    v  -> IxValue (Map k a) -> f (IxValue (Map k a))
    f a
    IxValue (Map k a)
    v f a -> (a -> Map k a) -> f (Map k a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \v' :: a
    v' -> k -> a -> Map k a -> Map k a
    forall k a. Ord k => k -> a -> Map k a -> Map k a
    Map.insert k
    Index (Map k a)
    k a
    v' Map k a
    m
         Nothing -> Map k a -> f (Map k a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Map k a
    m
      {-# INLINE ix #-}

    type instance IxValue (HashMap k a) = a
    instance (Eq k, Hashable k) => Ixed (HashMap k a) where
      ix :: Index (HashMap k a)
    -> Traversal' (HashMap k a) (IxValue (HashMap k a))
    ix k :: Index (HashMap k a)
    k f :: IxValue (HashMap k a) -> f (IxValue (HashMap k a))
    f m :: HashMap k a
    m = case k -> HashMap k a -> Maybe a
    forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
    HashMap.lookup k
    Index (HashMap k a)
    k HashMap k a
    m of
         Just v :: a
    v  -> IxValue (HashMap k a) -> f (IxValue (HashMap k a))
    f a
    IxValue (HashMap k a)
    v f a -> (a -> HashMap k a) -> f (HashMap k a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \v' :: a
    v' -> k -> a -> HashMap k a -> HashMap k a
    forall k v.
    (Eq k, Hashable k) =>
    k -> v -> HashMap k v -> HashMap k v
    HashMap.insert k
    Index (HashMap k a)
    k a
    v' HashMap k a
    m
         Nothing -> HashMap k a -> f (HashMap k a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure HashMap k a
    m
      {-# INLINE ix #-}

    type instance IxValue (Set k) = ()
    instance Ord k => Ixed (Set k) where
      ix :: Index (Set k) -> Traversal' (Set k) (IxValue (Set k))
    ix k :: Index (Set k)
    k f :: IxValue (Set k) -> f (IxValue (Set k))
    f m :: Set k
    m = if k -> Set k -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member k
    Index (Set k)
    k Set k
    m
         then IxValue (Set k) -> f (IxValue (Set k))
    f () f () -> (() -> Set k) -> f (Set k)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \() -> k -> Set k -> Set k
    forall a. Ord a => a -> Set a -> Set a
    Set.insert k
    Index (Set k)
    k Set k
    m
         else Set k -> f (Set k)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Set k
    m
      {-# INLINE ix #-}

    type instance IxValue IntSet = ()
    instance Ixed IntSet where
      ix :: Index IntSet -> Traversal' IntSet (IxValue IntSet)
    ix k :: Index IntSet
    k f :: IxValue IntSet -> f (IxValue IntSet)
    f m :: IntSet
    m = if Key -> IntSet -> Bool
    IntSet.member Key
    Index IntSet
    k IntSet
    m
         then IxValue IntSet -> f (IxValue IntSet)
    f () f () -> (() -> IntSet) -> f IntSet
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \() -> Key -> IntSet -> IntSet
    IntSet.insert Key
    Index IntSet
    k IntSet
    m
         else IntSet -> f IntSet
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure IntSet
    m
      {-# INLINE ix #-}

    type instance IxValue (HashSet k) = ()
    instance (Eq k, Hashable k) => Ixed (HashSet k) where
      ix :: Index (HashSet k) -> Traversal' (HashSet k) (IxValue (HashSet k))
    ix k :: Index (HashSet k)
    k f :: IxValue (HashSet k) -> f (IxValue (HashSet k))
    f m :: HashSet k
    m = if k -> HashSet k -> Bool
    forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
    HashSet.member k
    Index (HashSet k)
    k HashSet k
    m
         then IxValue (HashSet k) -> f (IxValue (HashSet k))
    f () f () -> (() -> HashSet k) -> f (HashSet k)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \() -> k -> HashSet k -> HashSet k
    forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
    HashSet.insert k
    Index (HashSet k)
    k HashSet k
    m
         else HashSet k -> f (HashSet k)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure HashSet k
    m
      {-# INLINE ix #-}

    type instance IxValue (Array.Array i e) = e
    -- |
    -- @
    -- arr '!' i ≡ arr 'Control.Lens.Getter.^.' 'ix' i
    -- arr '//' [(i,e)] ≡ 'ix' i 'Control.Lens.Setter..~' e '$' arr
    -- @
    instance Ix i => Ixed (Array.Array i e) where
      ix :: Index (Array i e) -> Traversal' (Array i e) (IxValue (Array i e))
    ix i :: Index (Array i e)
    i f :: IxValue (Array i e) -> f (IxValue (Array i e))
    f arr :: Array i e
    arr
        | (i, i) -> i -> Bool
    forall a. Ix a => (a, a) -> a -> Bool
    inRange (Array i e -> (i, i)
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> (i, i)
    bounds Array i e
    arr) i
    Index (Array i e)
    i = IxValue (Array i e) -> f (IxValue (Array i e))
    f (Array i e
    arr Array i e -> i -> e
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> i -> e
    Array.! i
    Index (Array i e)
    i) f e -> (e -> Array i e) -> f (Array i e)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \e :: e
    e -> Array i e
    arr Array i e -> [(i, e)] -> Array i e
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> [(i, e)] -> a i e
    Array.// [(i
    Index (Array i e)
    i,e
    e)]
        | Bool
    otherwise              = Array i e -> f (Array i e)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Array i e
    arr
      {-# INLINE ix #-}

    type instance IxValue (UArray i e) = e
    -- |
    -- @
    -- arr '!' i ≡ arr 'Control.Lens.Getter.^.' 'ix' i
    -- arr '//' [(i,e)] ≡ 'ix' i 'Control.Lens.Setter..~' e '$' arr
    -- @
    instance (IArray UArray e, Ix i) => Ixed (UArray i e) where
      ix :: Index (UArray i e)
    -> Traversal' (UArray i e) (IxValue (UArray i e))
    ix i :: Index (UArray i e)
    i f :: IxValue (UArray i e) -> f (IxValue (UArray i e))
    f arr :: UArray i e
    arr
        | (i, i) -> i -> Bool
    forall a. Ix a => (a, a) -> a -> Bool
    inRange (UArray i e -> (i, i)
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> (i, i)
    bounds UArray i e
    arr) i
    Index (UArray i e)
    i = IxValue (UArray i e) -> f (IxValue (UArray i e))
    f (UArray i e
    arr UArray i e -> i -> e
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> i -> e
    Array.! i
    Index (UArray i e)
    i) f e -> (e -> UArray i e) -> f (UArray i e)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \e :: e
    e -> UArray i e
    arr UArray i e -> [(i, e)] -> UArray i e
    forall (a :: * -> * -> *) e i.
    (IArray a e, Ix i) =>
    a i e -> [(i, e)] -> a i e
    Array.// [(i
    Index (UArray i e)
    i,e
    e)]
        | Bool
    otherwise              = UArray i e -> f (UArray i e)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure UArray i e
    arr
      {-# INLINE ix #-}

    type instance IxValue (Vector.Vector a) = a
    instance Ixed (Vector.Vector a) where
      ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a))
    ix i :: Index (Vector a)
    i f :: IxValue (Vector a) -> f (IxValue (Vector a))
    f v :: Vector a
    v
        | 0 Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Key
    Index (Vector a)
    i Bool -> Bool -> Bool
    && Key
    Index (Vector a)
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Vector a -> Key
    forall a. Vector a -> Key
    Vector.length Vector a
    v = IxValue (Vector a) -> f (IxValue (Vector a))
    f (Vector a
    v Vector a -> Key -> a
    forall a. Vector a -> Key -> a
    Vector.! Key
    Index (Vector a)
    i) f a -> (a -> Vector a) -> f (Vector a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a -> Vector a
    v Vector a -> [(Key, a)] -> Vector a
    forall a. Vector a -> [(Key, a)] -> Vector a
    Vector.// [(Key
    Index (Vector a)
    i, a
    a)]
        | Bool
    otherwise                     = Vector a -> f (Vector a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Vector a
    v
      {-# INLINE ix #-}

    type instance IxValue (Prim.Vector a) = a
    instance Prim a => Ixed (Prim.Vector a) where
      ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a))
    ix i :: Index (Vector a)
    i f :: IxValue (Vector a) -> f (IxValue (Vector a))
    f v :: Vector a
    v
        | 0 Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Key
    Index (Vector a)
    i Bool -> Bool -> Bool
    && Key
    Index (Vector a)
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Vector a -> Key
    forall a. Prim a => Vector a -> Key
    Prim.length Vector a
    v = IxValue (Vector a) -> f (IxValue (Vector a))
    f (Vector a
    v Vector a -> Key -> a
    forall a. Prim a => Vector a -> Key -> a
    Prim.! Key
    Index (Vector a)
    i) f a -> (a -> Vector a) -> f (Vector a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a -> Vector a
    v Vector a -> [(Key, a)] -> Vector a
    forall a. Prim a => Vector a -> [(Key, a)] -> Vector a
    Prim.// [(Key
    Index (Vector a)
    i, a
    a)]
        | Bool
    otherwise                   = Vector a -> f (Vector a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Vector a
    v
      {-# INLINE ix #-}

    type instance IxValue (Storable.Vector a) = a
    instance Storable a => Ixed (Storable.Vector a) where
      ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a))
    ix i :: Index (Vector a)
    i f :: IxValue (Vector a) -> f (IxValue (Vector a))
    f v :: Vector a
    v
        | 0 Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Key
    Index (Vector a)
    i Bool -> Bool -> Bool
    && Key
    Index (Vector a)
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Vector a -> Key
    forall a. Storable a => Vector a -> Key
    Storable.length Vector a
    v = IxValue (Vector a) -> f (IxValue (Vector a))
    f (Vector a
    v Vector a -> Key -> a
    forall a. Storable a => Vector a -> Key -> a
    Storable.! Key
    Index (Vector a)
    i) f a -> (a -> Vector a) -> f (Vector a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a -> Vector a
    v Vector a -> [(Key, a)] -> Vector a
    forall a. Storable a => Vector a -> [(Key, a)] -> Vector a
    Storable.// [(Key
    Index (Vector a)
    i, a
    a)]
        | Bool
    otherwise                       = Vector a -> f (Vector a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Vector a
    v
      {-# INLINE ix #-}

    type instance IxValue (Unboxed.Vector a) = a
    instance Unbox a => Ixed (Unboxed.Vector a) where
      ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a))
    ix i :: Index (Vector a)
    i f :: IxValue (Vector a) -> f (IxValue (Vector a))
    f v :: Vector a
    v
        | 0 Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Key
    Index (Vector a)
    i Bool -> Bool -> Bool
    && Key
    Index (Vector a)
    i Key -> Key -> Bool
    forall a. Ord a => a -> a -> Bool
    < Vector a -> Key
    forall a. Unbox a => Vector a -> Key
    Unboxed.length Vector a
    v = IxValue (Vector a) -> f (IxValue (Vector a))
    f (Vector a
    v Vector a -> Key -> a
    forall a. Unbox a => Vector a -> Key -> a
    Unboxed.! Key
    Index (Vector a)
    i) f a -> (a -> Vector a) -> f (Vector a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \a :: a
    a -> Vector a
    v Vector a -> [(Key, a)] -> Vector a
    forall a. Unbox a => Vector a -> [(Key, a)] -> Vector a
    Unboxed.// [(Key
    Index (Vector a)
    i, a
    a)]
        | Bool
    otherwise                      = Vector a -> f (Vector a)
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Vector a
    v
      {-# INLINE ix #-}

    type instance IxValue StrictT.Text = Char
    instance Ixed StrictT.Text where
      ix :: Index Text -> Traversal' Text (IxValue Text)
    ix e :: Index Text
    e f :: IxValue Text -> f (IxValue Text)
    f s :: Text
    s = case Key -> Text -> (Text, Text)
    StrictT.splitAt Key
    Index Text
    e Text
    s of
         (l :: Text
    l, mr :: Text
    mr) -> case Text -> Maybe (Char, Text)
    StrictT.uncons Text
    mr of
           Nothing      -> Text -> f Text
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Text
    s
           Just (c :: Char
    c, xs :: Text
    xs) -> IxValue Text -> f (IxValue Text)
    f Char
    IxValue Text
    c f Char -> (Char -> Text) -> f Text
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \d :: Char
    d -> [Text] -> Text
    StrictT.concat [Text
    l, Char -> Text
    StrictT.singleton Char
    d, Text
    xs]
      {-# INLINE ix #-}

    type instance IxValue LazyT.Text = Char
    instance Ixed LazyT.Text where
      ix :: Index Text -> Traversal' Text (IxValue Text)
    ix e :: Index Text
    e f :: IxValue Text -> f (IxValue Text)
    f s :: Text
    s = case Int64 -> Text -> (Text, Text)
    LazyT.splitAt Int64
    Index Text
    e Text
    s of
         (l :: Text
    l, mr :: Text
    mr) -> case Text -> Maybe (Char, Text)
    LazyT.uncons Text
    mr of
           Nothing      -> Text -> f Text
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Text
    s
           Just (c :: Char
    c, xs :: Text
    xs) -> IxValue Text -> f (IxValue Text)
    f Char
    IxValue Text
    c f Char -> (Char -> Text) -> f Text
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \d :: Char
    d -> Text -> Text -> Text
    LazyT.append Text
    l (Char -> Text -> Text
    LazyT.cons Char
    d Text
    xs)
      {-# INLINE ix #-}

    type instance IxValue StrictB.ByteString = Word8
    instance Ixed StrictB.ByteString where
      ix :: Index ByteString -> Traversal' ByteString (IxValue ByteString)
    ix e :: Index ByteString
    e f :: IxValue ByteString -> f (IxValue ByteString)
    f s :: ByteString
    s = case Key -> ByteString -> (ByteString, ByteString)
    StrictB.splitAt Key
    Index ByteString
    e ByteString
    s of
         (l :: ByteString
    l, mr :: ByteString
    mr) -> case ByteString -> Maybe (Word8, ByteString)
    StrictB.uncons ByteString
    mr of
           Nothing      -> ByteString -> f ByteString
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ByteString
    s
           Just (c :: Word8
    c, xs :: ByteString
    xs) -> IxValue ByteString -> f (IxValue ByteString)
    f Word8
    IxValue ByteString
    c f Word8 -> (Word8 -> ByteString) -> f ByteString
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \d :: Word8
    d -> [ByteString] -> ByteString
    StrictB.concat [ByteString
    l, Word8 -> ByteString
    StrictB.singleton Word8
    d, ByteString
    xs]
      {-# INLINE ix #-}

    type instance IxValue LazyB.ByteString = Word8
    instance Ixed LazyB.ByteString where
      -- TODO: we could be lazier, returning each chunk as it is passed
      ix :: Index ByteString -> Traversal' ByteString (IxValue ByteString)
    ix e :: Index ByteString
    e f :: IxValue ByteString -> f (IxValue ByteString)
    f s :: ByteString
    s = case Int64 -> ByteString -> (ByteString, ByteString)
    LazyB.splitAt Int64
    Index ByteString
    e ByteString
    s of
         (l :: ByteString
    l, mr :: ByteString
    mr) -> case ByteString -> Maybe (Word8, ByteString)
    LazyB.uncons ByteString
    mr of
           Nothing      -> ByteString -> f ByteString
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ByteString
    s
           Just (c :: Word8
    c, xs :: ByteString
    xs) -> IxValue ByteString -> f (IxValue ByteString)
    f Word8
    IxValue ByteString
    c f Word8 -> (Word8 -> ByteString) -> f ByteString
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \d :: Word8
    d -> ByteString -> ByteString -> ByteString
    LazyB.append ByteString
    l (Word8 -> ByteString -> ByteString
    LazyB.cons Word8
    d ByteString
    xs)
      {-# INLINE ix #-}

    -- | 'At' provides a 'Lens' that can be used to read,
    -- write or delete the value associated with a key in a 'Map'-like
    -- container on an ad hoc basis.
    --
    -- An instance of 'At' should satisfy:
    --
    -- @
    -- 'ix' k ≡ 'at' k '.' 'traverse'
    -- @
    class Ixed m => At m where
      -- |
      -- >>> Map.fromList [(1,"world")] ^.at 1
      -- Just "world"
      --
      -- >>> at 1 ?~ "hello" $ Map.empty
      -- fromList [(1,"hello")]
      --
      -- /Note:/ 'Map'-like containers form a reasonable instance, but not 'Array'-like ones, where
      -- you cannot satisfy the 'Lens' laws.
      at :: Index m -> Lens' m (Maybe (IxValue m))

    -- | Delete the value associated with a key in a 'Map'-like container
    --
    -- @
    -- 'sans' k = 'at' k .~ Nothing
    -- @
    sans :: At m => Index m -> m -> m
    sans :: Index m -> m -> m
    sans k :: Index m
    k m :: m
    m = m
    m m -> (m -> m) -> m
    forall a b. a -> (a -> b) -> b
    & Index m -> Lens' m (Maybe (IxValue m))
    forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
    at Index m
    k ((Maybe (IxValue m) -> Identity (Maybe (IxValue m)))
     -> m -> Identity m)
    -> Maybe (IxValue m) -> m -> m
    forall s t a b. ASetter s t a b -> b -> s -> t
    .~ Maybe (IxValue m)
    forall a. Maybe a
    Nothing
    {-# INLINE sans #-}

    -- | An indexed version of 'at'.
    --
    -- >>> Map.fromList [(1,"world")] ^@. iat 1
    -- (1,Just "world")
    --
    -- >>> iat 1 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.empty
    -- fromList [(1,"hello")]
    --
    -- >>> iat 2 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.empty
    -- fromList []
    --
    iat :: At m => Index m -> IndexedLens' (Index m) m (Maybe (IxValue m))
    iat :: Index m -> IndexedLens' (Index m) m (Maybe (IxValue m))
    iat i :: Index m
    i f :: p (Maybe (IxValue m)) (f (Maybe (IxValue m)))
    f = Index m -> (Maybe (IxValue m) -> f (Maybe (IxValue m))) -> m -> f m
    forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
    at Index m
    i (p (Maybe (IxValue m)) (f (Maybe (IxValue m)))
    -> Index m -> Maybe (IxValue m) -> f (Maybe (IxValue m))
    forall i (p :: * -> * -> *) a b.
    Indexable i p =>
    p a b -> i -> a -> b
    indexed p (Maybe (IxValue m)) (f (Maybe (IxValue m)))
    f Index m
    i)
    {-# INLINE iat #-}

    instance At (Maybe a) where
      at :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a)))
    at () f :: Maybe (IxValue (Maybe a)) -> f (Maybe (IxValue (Maybe a)))
    f = Maybe a -> f (Maybe a)
    Maybe (IxValue (Maybe a)) -> f (Maybe (IxValue (Maybe a)))
    f
      {-# INLINE at #-}

    instance At (IntMap a) where
    #if MIN_VERSION_containers(0,5,8)
      at :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a)))
    at k :: Index (IntMap a)
    k f :: Maybe (IxValue (IntMap a)) -> f (Maybe (IxValue (IntMap a)))
    f = (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
    forall (f :: * -> *) a.
    Functor f =>
    (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)
    IntMap.alterF Maybe a -> f (Maybe a)
    Maybe (IxValue (IntMap a)) -> f (Maybe (IxValue (IntMap a)))
    f Key
    Index (IntMap a)
    k
    #else
      at k f m = f 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
    #endif
      {-# INLINE at #-}

    instance Ord k => At (Map k a) where
    #if MIN_VERSION_containers(0,5,8)
      at :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a)))
    at k :: Index (Map k a)
    k f :: Maybe (IxValue (Map k a)) -> f (Maybe (IxValue (Map k a)))
    f = (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
    forall (f :: * -> *) k a.
    (Functor f, Ord k) =>
    (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
    Map.alterF Maybe a -> f (Maybe a)
    Maybe (IxValue (Map k a)) -> f (Maybe (IxValue (Map k a)))
    f k
    Index (Map k a)
    k
    #else
      at k f m = f 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
    #endif
      {-# INLINE at #-}

    instance (Eq k, Hashable k) => At (HashMap k a) where
      at :: Index (HashMap k a)
    -> Lens' (HashMap k a) (Maybe (IxValue (HashMap k a)))
    at k :: Index (HashMap k a)
    k f :: Maybe (IxValue (HashMap k a)) -> f (Maybe (IxValue (HashMap k a)))
    f m :: HashMap k a
    m = Maybe (IxValue (HashMap k a)) -> f (Maybe (IxValue (HashMap k a)))
    f Maybe a
    Maybe (IxValue (HashMap k a))
    mv f (Maybe a) -> (Maybe a -> HashMap k a) -> f (HashMap k a)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \r :: Maybe a
    r -> case Maybe a
    r of
        Nothing -> HashMap k a -> (a -> HashMap k a) -> Maybe a -> HashMap k a
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe HashMap k a
    m (HashMap k a -> a -> HashMap k a
    forall a b. a -> b -> a
    const (k -> HashMap k a -> HashMap k a
    forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
    HashMap.delete k
    Index (HashMap k a)
    k HashMap k a
    m)) Maybe a
    mv
        Just v' :: a
    v' -> k -> a -> HashMap k a -> HashMap k a
    forall k v.
    (Eq k, Hashable k) =>
    k -> v -> HashMap k v -> HashMap k v
    HashMap.insert k
    Index (HashMap k a)
    k a
    v' HashMap k a
    m
        where mv :: Maybe a
    mv = k -> HashMap k a -> Maybe a
    forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
    HashMap.lookup k
    Index (HashMap k a)
    k HashMap k a
    m
      {-# INLINE at #-}

    instance At IntSet where
      at :: Index IntSet -> Lens' IntSet (Maybe (IxValue IntSet))
    at k :: Index IntSet
    k f :: Maybe (IxValue IntSet) -> f (Maybe (IxValue IntSet))
    f m :: IntSet
    m = Maybe (IxValue IntSet) -> f (Maybe (IxValue IntSet))
    f Maybe ()
    Maybe (IxValue IntSet)
    mv f (Maybe ()) -> (Maybe () -> IntSet) -> f IntSet
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \r :: Maybe ()
    r -> case Maybe ()
    r of
        Nothing -> IntSet -> (() -> IntSet) -> Maybe () -> IntSet
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe IntSet
    m (IntSet -> () -> IntSet
    forall a b. a -> b -> a
    const (Key -> IntSet -> IntSet
    IntSet.delete Key
    Index IntSet
    k IntSet
    m)) Maybe ()
    mv
        Just () -> Key -> IntSet -> IntSet
    IntSet.insert Key
    Index IntSet
    k IntSet
    m
        where mv :: Maybe ()
    mv = if Key -> IntSet -> Bool
    IntSet.member Key
    Index IntSet
    k IntSet
    m then () -> Maybe ()
    forall a. a -> Maybe a
    Just () else Maybe ()
    forall a. Maybe a
    Nothing
      {-# INLINE at #-}

    instance Ord k => At (Set k) where
      at :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k)))
    at k :: Index (Set k)
    k f :: Maybe (IxValue (Set k)) -> f (Maybe (IxValue (Set k)))
    f m :: Set k
    m = Maybe (IxValue (Set k)) -> f (Maybe (IxValue (Set k)))
    f Maybe ()
    Maybe (IxValue (Set k))
    mv f (Maybe ()) -> (Maybe () -> Set k) -> f (Set k)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \r :: Maybe ()
    r -> case Maybe ()
    r of
        Nothing -> Set k -> (() -> Set k) -> Maybe () -> Set k
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set k
    m (Set k -> () -> Set k
    forall a b. a -> b -> a
    const (k -> Set k -> Set k
    forall a. Ord a => a -> Set a -> Set a
    Set.delete k
    Index (Set k)
    k Set k
    m)) Maybe ()
    mv
        Just () -> k -> Set k -> Set k
    forall a. Ord a => a -> Set a -> Set a
    Set.insert k
    Index (Set k)
    k Set k
    m
        where mv :: Maybe ()
    mv = if k -> Set k -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member k
    Index (Set k)
    k Set k
    m then () -> Maybe ()
    forall a. a -> Maybe a
    Just () else Maybe ()
    forall a. Maybe a
    Nothing
      {-# INLINE at #-}

    instance (Eq k, Hashable k) => At (HashSet k) where
      at :: Index (HashSet k)
    -> Lens' (HashSet k) (Maybe (IxValue (HashSet k)))
    at k :: Index (HashSet k)
    k f :: Maybe (IxValue (HashSet k)) -> f (Maybe (IxValue (HashSet k)))
    f m :: HashSet k
    m = Maybe (IxValue (HashSet k)) -> f (Maybe (IxValue (HashSet k)))
    f Maybe ()
    Maybe (IxValue (HashSet k))
    mv f (Maybe ()) -> (Maybe () -> HashSet k) -> f (HashSet k)
    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
    <&> \r :: Maybe ()
    r -> case Maybe ()
    r of
        Nothing -> HashSet k -> (() -> HashSet k) -> Maybe () -> HashSet k
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe HashSet k
    m (HashSet k -> () -> HashSet k
    forall a b. a -> b -> a
    const (k -> HashSet k -> HashSet k
    forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
    HashSet.delete k
    Index (HashSet k)
    k HashSet k
    m)) Maybe ()
    mv
        Just () -> k -> HashSet k -> HashSet k
    forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
    HashSet.insert k
    Index (HashSet k)
    k HashSet k
    m
        where mv :: Maybe ()
    mv = if k -> HashSet k -> Bool
    forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
    HashSet.member k
    Index (HashSet k)
    k HashSet k
    m then () -> Maybe ()
    forall a. a -> Maybe a
    Just () else Maybe ()
    forall a. Maybe a
    Nothing
      {-# INLINE at #-}

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a) a@
    type instance IxValue (a,a2) = a
    instance (a~a2) => Ixed (a,a2) where
      ix :: Index (a, a2) -> Traversal' (a, a2) (IxValue (a, a2))
    ix = LensLike (Indexing f) (a, a2) (a, a2) a2 a2
    -> Key -> IndexedLensLike Key f (a, a2) (a, a2) a2 a2
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike (Indexing f) (a, a2) (a, a2) a2 a2
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a) a@
    type instance IxValue (a,a2,a3) = a
    instance (a~a2, a~a3) => Ixed (a,a2,a3) where
      ix :: Index (a, a2, a3) -> Traversal' (a, a2, a3) (IxValue (a, a2, a3))
    ix = LensLike (Indexing f) (a, a2, a3) (a, a2, a3) a3 a3
    -> Key -> IndexedLensLike Key f (a, a2, a3) (a, a2, a3) a3 a3
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike (Indexing f) (a, a2, a3) (a, a2, a3) a3 a3
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4) = a
    instance (a~a2, a~a3, a~a4) => Ixed (a,a2,a3,a4) where
      ix :: Index (a, a2, a3, a4)
    -> Traversal' (a, a2, a3, a4) (IxValue (a, a2, a3, a4))
    ix = LensLike (Indexing f) (a, a2, a3, a4) (a, a2, a3, a4) a4 a4
    -> Key
    -> IndexedLensLike Key f (a, a2, a3, a4) (a, a2, a3, a4) a4 a4
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike (Indexing f) (a, a2, a3, a4) (a, a2, a3, a4) a4 a4
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4,a5) = a
    instance (a~a2, a~a3, a~a4, a~a5) => Ixed (a,a2,a3,a4,a5) where
      ix :: Index (a, a2, a3, a4, a5)
    -> Traversal' (a, a2, a3, a4, a5) (IxValue (a, a2, a3, a4, a5))
    ix = LensLike (Indexing f) (a, a2, a3, a4, a5) (a, a2, a3, a4, a5) a5 a5
    -> Key
    -> IndexedLensLike
         Key f (a, a2, a3, a4, a5) (a, a2, a3, a4, a5) a5 a5
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike (Indexing f) (a, a2, a3, a4, a5) (a, a2, a3, a4, a5) a5 a5
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4,a5,a6) = a
    instance (a~a2, a~a3, a~a4, a~a5, a~a6) => Ixed (a,a2,a3,a4,a5,a6) where
      ix :: Index (a, a2, a3, a4, a5, a6)
    -> Traversal'
         (a, a2, a3, a4, a5, a6) (IxValue (a, a2, a3, a4, a5, a6))
    ix = LensLike
      (Indexing f) (a, a2, a3, a4, a5, a6) (a, a2, a3, a4, a5, a6) a6 a6
    -> Key
    -> IndexedLensLike
         Key f (a, a2, a3, a4, a5, a6) (a, a2, a3, a4, a5, a6) a6 a6
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike
      (Indexing f) (a, a2, a3, a4, a5, a6) (a, a2, a3, a4, a5, a6) a6 a6
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4,a5,a6,a7) = a
    instance (a~a2, a~a3, a~a4, a~a5, a~a6, a~a7) => Ixed (a,a2,a3,a4,a5,a6,a7) where
      ix :: Index (a, a2, a3, a4, a5, a6, a7)
    -> Traversal'
         (a, a2, a3, a4, a5, a6, a7) (IxValue (a, a2, a3, a4, a5, a6, a7))
    ix = LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7)
      (a, a2, a3, a4, a5, a6, a7)
      a7
      a7
    -> Key
    -> IndexedLensLike
         Key f (a, a2, a3, a4, a5, a6, a7) (a, a2, a3, a4, a5, a6, a7) a7 a7
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7)
      (a, a2, a3, a4, a5, a6, a7)
      a7
      a7
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a,a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4,a5,a6,a7,a8) = a
    instance (a~a2, a~a3, a~a4, a~a5, a~a6, a~a7, a~a8) => Ixed (a,a2,a3,a4,a5,a6,a7,a8) where
      ix :: Index (a, a2, a3, a4, a5, a6, a7, a8)
    -> Traversal'
         (a, a2, a3, a4, a5, a6, a7, a8)
         (IxValue (a, a2, a3, a4, a5, a6, a7, a8))
    ix = LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7, a8)
      (a, a2, a3, a4, a5, a6, a7, a8)
      a8
      a8
    -> Key
    -> IndexedLensLike
         Key
         f
         (a, a2, a3, a4, a5, a6, a7, a8)
         (a, a2, a3, a4, a5, a6, a7, a8)
         a8
         a8
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7, a8)
      (a, a2, a3, a4, a5, a6, a7, a8)
      a8
      a8
    forall s t a b. Each s t a b => Traversal s t a b
    each

    -- | @'ix' :: 'Int' -> 'Traversal'' (a,a,a,a,a,a,a,a,a) a@
    type instance IxValue (a,a2,a3,a4,a5,a6,a7,a8,a9) = a
    instance (a~a2, a~a3, a~a4, a~a5, a~a6, a~a7, a~a8, a~a9) => Ixed (a,a2,a3,a4,a5,a6,a7,a8,a9) where
      ix :: Index (a, a2, a3, a4, a5, a6, a7, a8, a9)
    -> Traversal'
         (a, a2, a3, a4, a5, a6, a7, a8, a9)
         (IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9))
    ix = LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7, a8, a9)
      (a, a2, a3, a4, a5, a6, a7, a8, a9)
      a9
      a9
    -> Key
    -> IndexedLensLike
         Key
         f
         (a, a2, a3, a4, a5, a6, a7, a8, a9)
         (a, a2, a3, a4, a5, a6, a7, a8, a9)
         a9
         a9
    forall (f :: * -> *) s t a.
    Applicative f =>
    LensLike (Indexing f) s t a a
    -> Key -> IndexedLensLike Key f s t a a
    elementOf LensLike
      (Indexing f)
      (a, a2, a3, a4, a5, a6, a7, a8, a9)
      (a, a2, a3, a4, a5, a6, a7, a8, a9)
      a9
      a9
    forall s t a b. Each s t a b => Traversal s t a b
    each