{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeInType #-}
-- |
-- Module: Optics.At.Core
-- Description: Optics for 'Map' and 'Set'-like containers.
--
-- This module provides optics for 'Map' and 'Set'-like containers, including an
-- 'AffineTraversal' to traverse a key in a map or an element of a sequence:
--
-- >>> preview (ix 1) ['a','b','c']
-- Just 'b'
--
-- a 'Lens' to get, set or delete a key in a map:
--
-- >>> set (at 0) (Just 'b') (Map.fromList [(0, 'a')])
-- fromList [(0,'b')]
--
-- and a 'Lens' to insert or remove an element of a set:
--
-- >>> IntSet.fromList [1,2,3,4] & contains 3 .~ False
-- fromList [1,2,4]
--
-- The @Optics.At@ module from @optics-extra@ provides additional instances of
-- the classes defined here.
--
module Optics.At.Core
  (
    -- * Type families
    Index
  , IxValue

    -- * Ixed
  , Ixed(..)
  , ixAt

    -- * At
  , At(..)
  , at'
  , sans

  -- * Contains
  , Contains(..)
  ) where

import qualified Data.Array.IArray as Array
import Data.Array.Unboxed (UArray)
import Data.Complex (Complex (..))
import Data.Ix (Ix (..))
import Data.Functor.Identity (Identity (..))
import Data.Kind (Type)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Tree (Tree (..))

import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set

import Data.Maybe.Optics
import Optics.AffineTraversal
import Optics.Iso
import Optics.Lens
import Optics.Optic
import Optics.Setter

-- | Type family that takes a key-value container type and returns the type of
-- keys (indices) into the container, for example @'Index' ('Map' k a) ~ k@.
-- This is shared by 'Ixed', 'At' and 'Contains'.
type family Index (s :: Type) :: Type
type instance Index (e -> a) = e
type instance Index IntSet = Int
type instance Index (Set 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 (Array.Array i e) = i
type instance Index (UArray i e) = i
type instance Index (Complex a) = Int
type instance Index (Identity a) = ()
type instance Index (Maybe a) = ()
type instance Index (Tree a) = [Int]

-- | This class provides a simple 'Lens' that lets you view (and modify)
-- information about whether or not a container contains a given 'Index'.
-- Instances are provided for 'Set'-like containers only.
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

instance Contains IntSet where
  contains :: Index IntSet -> Lens' IntSet Bool
contains Index IntSet
k = LensVL IntSet IntSet Bool Bool -> Lens' IntSet Bool
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL IntSet IntSet Bool Bool -> Lens' IntSet Bool)
-> LensVL IntSet IntSet Bool Bool -> Lens' IntSet Bool
forall a b. (a -> b) -> a -> b
$ \Bool -> f Bool
f 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
<&> \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 Index (Set a)
k = LensVL (Set a) (Set a) Bool Bool -> Lens' (Set a) Bool
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (Set a) (Set a) Bool Bool -> Lens' (Set a) Bool)
-> LensVL (Set a) (Set a) Bool Bool -> Lens' (Set a) Bool
forall a b. (a -> b) -> a -> b
$ \Bool -> f Bool
f 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
<&> \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 #-}

-- | Type family that takes a key-value container type and returns the type of
-- values stored in the container, for example @'IxValue' ('Map' k a) ~ a@. This
-- is shared by both 'Ixed' and 'At'.
type family IxValue (m :: Type) :: Type


-- | Provides a simple 'AffineTraversal' 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
  -- | Type family that takes a key-value container type and returns the kind
  -- of optic to index into it. For most containers, it's 'An_AffineTraversal',
  -- @Representable@ (Naperian) containers it is 'A_Lens', and multi-maps would
  -- have 'A_Traversal'.
  type IxKind (m :: Type) :: OpticKind
  type IxKind m = An_AffineTraversal

  -- | /NB:/ Setting the value of this 'AffineTraversal' 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'.
  --
  -- >>> [1,2,3,4] & ix 2 %~ (*10)
  -- [1,2,30,4]
  --
  -- >>> "abcd" & ix 2 .~ 'e'
  -- "abed"
  --
  -- >>> "abcd" ^? ix 2
  -- Just 'c'
  --
  -- >>> [] ^? ix 2
  -- Nothing
  ix :: Index m -> Optic' (IxKind m) NoIx m (IxValue m)
  default ix :: (At m, IxKind m ~ An_AffineTraversal) => Index m -> Optic' (IxKind m) NoIx m (IxValue m)
  ix = Index m -> Optic' (IxKind m) NoIx m (IxValue m)
forall m. At m => Index m -> AffineTraversal' m (IxValue m)
ixAt
  {-# INLINE ix #-}

-- | 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 -> AffineTraversal' m (IxValue m)
ixAt :: Index m -> AffineTraversal' m (IxValue m)
ixAt = \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 Lens' m (Maybe (IxValue m))
-> Optic
     A_Prism
     NoIx
     (Maybe (IxValue m))
     (Maybe (IxValue m))
     (IxValue m)
     (IxValue m)
-> AffineTraversal' m (IxValue m)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic
  A_Prism
  NoIx
  (Maybe (IxValue m))
  (Maybe (IxValue m))
  (IxValue m)
  (IxValue m)
forall a b. Prism (Maybe a) (Maybe b) a b
_Just
{-# INLINE ixAt #-}

type instance IxValue (e -> a) = a
instance Eq e => Ixed (e -> a) where
  type IxKind (e -> a) = A_Lens
  ix :: Index (e -> a)
-> Optic' (IxKind (e -> a)) NoIx (e -> a) (IxValue (e -> a))
ix Index (e -> a)
e = LensVL (e -> a) (e -> a) a a -> Lens (e -> a) (e -> a) a a
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (e -> a) (e -> a) a a -> Lens (e -> a) (e -> a) a a)
-> LensVL (e -> a) (e -> a) a a -> Lens (e -> a) (e -> a) a a
forall a b. (a -> b) -> a -> b
$ \a -> f a
p e -> a
f -> a -> f 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 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)
-> Optic' (IxKind (Maybe a)) NoIx (Maybe a) (IxValue (Maybe a))
ix () = Optic A_Prism NoIx (Maybe a) (Maybe a) a a
-> Optic An_AffineTraversal NoIx (Maybe a) (Maybe a) a a
forall destKind srcKind (is :: IxList) s t a b.
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @An_AffineTraversal Optic A_Prism NoIx (Maybe a) (Maybe a) a a
forall a b. Prism (Maybe a) (Maybe b) a b
_Just
  {-# INLINE ix #-}

type instance IxValue [a] = a
instance Ixed [a] where
  ix :: Index [a] -> Optic' (IxKind [a]) NoIx [a] (IxValue [a])
ix Index [a]
k = AffineTraversalVL [a] [a] a a -> AffineTraversal [a] [a] a a
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (Key -> AffineTraversalVL [a] [a] a a
forall a. Key -> AffineTraversalVL' [a] a
ixListVL Key
Index [a]
k)
  {-# INLINE ix #-}

type instance IxValue (NonEmpty a) = a
instance Ixed (NonEmpty a) where
  ix :: Index (NonEmpty a)
-> Optic'
     (IxKind (NonEmpty a)) NoIx (NonEmpty a) (IxValue (NonEmpty a))
ix Index (NonEmpty a)
k = AffineTraversalVL (NonEmpty a) (NonEmpty a) a a
-> AffineTraversal (NonEmpty a) (NonEmpty a) a a
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (NonEmpty a) (NonEmpty a) a a
 -> AffineTraversal (NonEmpty a) (NonEmpty a) a a)
-> AffineTraversalVL (NonEmpty a) (NonEmpty a) a a
-> AffineTraversal (NonEmpty a) (NonEmpty a) a a
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a -> f a
f NonEmpty a
xs0 ->
    if Key
Index (NonEmpty a)
k Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< Key
0
    then NonEmpty a -> f (NonEmpty a)
forall r. r -> f r
point NonEmpty a
xs0
    else let go :: NonEmpty a -> Key -> f (NonEmpty a)
go (a
a:|[a]
as) Key
0 = a -> f a
f 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) 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
<$> Key -> (forall r. r -> f r) -> (a -> f a) -> [a] -> f [a]
forall a. Key -> AffineTraversalVL' [a] a
ixListVL (Key
i Key -> Key -> Key
forall a. Num a => a -> a -> a
- Key
1) forall r. r -> f r
point a -> f a
f [a]
as
         in NonEmpty a -> Key -> f (NonEmpty a)
go NonEmpty a
xs0 Key
Index (NonEmpty a)
k
  {-# INLINE ix #-}

type instance IxValue (Identity a) = a
instance Ixed (Identity a) where
  type IxKind (Identity a) = An_Iso
  ix :: Index (Identity a)
-> Optic'
     (IxKind (Identity a)) NoIx (Identity a) (IxValue (Identity a))
ix () = Optic'
  (IxKind (Identity a)) NoIx (Identity a) (IxValue (Identity a))
forall s a t b. (Coercible s a, Coercible t b) => Iso s t a b
coerced
  {-# INLINE ix #-}

type instance IxValue (Tree a) = a
instance Ixed (Tree a) where
  ix :: Index (Tree a)
-> Optic' (IxKind (Tree a)) NoIx (Tree a) (IxValue (Tree a))
ix Index (Tree a)
xs0 = AffineTraversalVL (Tree a) (Tree a) a a
-> AffineTraversal (Tree a) (Tree a) a a
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (Tree a) (Tree a) a a
 -> AffineTraversal (Tree a) (Tree a) a a)
-> AffineTraversalVL (Tree a) (Tree a) a a
-> AffineTraversal (Tree a) (Tree a) a a
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a -> f a
f ->
    let go :: [Key] -> Tree a -> f (Tree a)
go [] (Node a
a [Tree a]
as) = a -> f a
f 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 -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
a' [Tree a]
as
        go (Key
i:[Key]
is) t :: Tree a
t@(Node a
a [Tree a]
as)
          | Key
i Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< Key
0     = Tree a -> f (Tree a)
forall r. r -> f r
point Tree a
t
          | Bool
otherwise = a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Node a
a ([Tree a] -> Tree a) -> f [Tree a] -> f (Tree a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (forall r. r -> f r)
-> (Tree a -> f (Tree a))
-> [Tree a]
-> f [Tree a]
forall a. Key -> AffineTraversalVL' [a] a
ixListVL Key
i forall r. r -> f r
point ([Key] -> Tree a -> f (Tree a)
go [Key]
is) [Tree a]
as
    in [Key] -> Tree a -> f (Tree a)
go [Key]
Index (Tree a)
xs0
  {-# INLINE ix #-}

type instance IxValue (Seq a) = a
instance Ixed (Seq a) where
  ix :: Index (Seq a)
-> Optic' (IxKind (Seq a)) NoIx (Seq a) (IxValue (Seq a))
ix Index (Seq a)
i = AffineTraversalVL (Seq a) (Seq a) a a
-> AffineTraversal (Seq a) (Seq a) a a
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (Seq a) (Seq a) a a
 -> AffineTraversal (Seq a) (Seq a) a a)
-> AffineTraversalVL (Seq a) (Seq a) a a
-> AffineTraversal (Seq a) (Seq a) a a
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a -> f a
f Seq a
m ->
    if Key
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
    then a -> f 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 -> 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
    else Seq a -> f (Seq a)
forall r. r -> f r
point Seq a
m
  {-# INLINE ix #-}

type instance IxValue (IntMap a) = a
-- Default implementation uses IntMap.alterF
instance Ixed (IntMap a)

type instance IxValue (Map k a) = a
-- Default implementation uses Map.alterF
instance Ord k => Ixed (Map k a)

type instance IxValue (Set k) = ()
instance Ord k => Ixed (Set k) where
  ix :: Index (Set k)
-> Optic' (IxKind (Set k)) NoIx (Set k) (IxValue (Set k))
ix Index (Set k)
k = AffineTraversalVL (Set k) (Set k) () ()
-> AffineTraversal (Set k) (Set k) () ()
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (Set k) (Set k) () ()
 -> AffineTraversal (Set k) (Set k) () ())
-> AffineTraversalVL (Set k) (Set k) () ()
-> AffineTraversal (Set k) (Set k) () ()
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point () -> f ()
f 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 () -> f ()
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 r. r -> f r
point Set k
m
  {-# INLINE ix #-}

type instance IxValue IntSet = ()
instance Ixed IntSet where
  ix :: Index IntSet -> Optic' (IxKind IntSet) NoIx IntSet (IxValue IntSet)
ix Index IntSet
k = AffineTraversalVL IntSet IntSet () ()
-> AffineTraversal IntSet IntSet () ()
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL IntSet IntSet () ()
 -> AffineTraversal IntSet IntSet () ())
-> AffineTraversalVL IntSet IntSet () ()
-> AffineTraversal IntSet IntSet () ()
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point () -> f ()
f IntSet
m ->
    if Key -> IntSet -> Bool
IntSet.member Key
Index IntSet
k IntSet
m
    then () -> f ()
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 r. r -> f r
point IntSet
m
  {-# INLINE ix #-}

type instance IxValue (Array.Array i e) = e
-- |
-- @
-- arr 'Array.!' i ≡ arr 'Optics.Operators.^.' 'ix' i
-- arr '//' [(i,e)] ≡ 'ix' i 'Optics.Operators..~' e '$' arr
-- @
instance Ix i => Ixed (Array.Array i e) where
  ix :: Index (Array i e)
-> Optic'
     (IxKind (Array i e)) NoIx (Array i e) (IxValue (Array i e))
ix Index (Array i e)
i = AffineTraversalVL (Array i e) (Array i e) e e
-> AffineTraversal (Array i e) (Array i e) e e
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (Array i e) (Array i e) e e
 -> AffineTraversal (Array i e) (Array i e) e e)
-> AffineTraversalVL (Array i e) (Array i e) e e
-> AffineTraversal (Array i e) (Array i e) e e
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point e -> f e
f Array i e
arr ->
    if (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)
Array.bounds Array i e
arr) i
Index (Array i e)
i
    then e -> f 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 -> 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)]
    else Array i e -> f (Array i e)
forall r. r -> f r
point Array i e
arr
  {-# INLINE ix #-}

type instance IxValue (UArray i e) = e
-- |
-- @
-- arr 'Array.!' i ≡ arr 'Optics.Operators.^.' 'ix' i
-- arr '//' [(i,e)] ≡ 'ix' i 'Optics.Operators..~' e '$' arr
-- @
instance (Array.IArray UArray e, Ix i) => Ixed (UArray i e) where
  ix :: Index (UArray i e)
-> Optic'
     (IxKind (UArray i e)) NoIx (UArray i e) (IxValue (UArray i e))
ix Index (UArray i e)
i = AffineTraversalVL (UArray i e) (UArray i e) e e
-> AffineTraversal (UArray i e) (UArray i e) e e
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (UArray i e) (UArray i e) e e
 -> AffineTraversal (UArray i e) (UArray i e) e e)
-> AffineTraversalVL (UArray i e) (UArray i e) e e
-> AffineTraversal (UArray i e) (UArray i e) e e
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point e -> f e
f UArray i e
arr ->
    if (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)
Array.bounds UArray i e
arr) i
Index (UArray i e)
i
    then e -> f 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 -> 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)]
    else UArray i e -> f (UArray i e)
forall r. r -> f r
point UArray i e
arr
  {-# INLINE ix #-}

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a) a@
type instance IxValue (a0, a2) = a0
instance (a0 ~ a1) => Ixed (a0, a1) where
  ix :: Index (a0, a1)
-> Optic' (IxKind (a0, a1)) NoIx (a0, a1) (IxValue (a0, a1))
ix Index (a0, a1)
i = AffineTraversalVL (a0, a1) (a0, a1) a1 a1
-> AffineTraversal (a0, a1) (a0, a1) a1 a1
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (a0, a1) (a0, a1) a1 a1
 -> AffineTraversal (a0, a1) (a0, a1) a1 a1)
-> AffineTraversalVL (a0, a1) (a0, a1) a1 a1
-> AffineTraversal (a0, a1) (a0, a1) a1 a1
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a1 -> f a1
f ~s :: (a0, a1)
s@(a0, a1) ->
    case Index (a0, a1)
i of
      Index (a0, a1)
0 -> (,a1
a1) (a1 -> (a1, a1)) -> f a1 -> f (a1, a1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a1 -> f a1
f a1
a0
      Index (a0, a1)
1 -> (a1
a0,) (a1 -> (a1, a1)) -> f a1 -> f (a1, a1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a1 -> f a1
f a1
a1
      Index (a0, a1)
_ -> (a0, a1) -> f (a0, a1)
forall r. r -> f r
point (a0, a1)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a) a@
type instance IxValue (a0, a1, a2) = a0
instance (a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) where
  ix :: Index (a0, a1, a2)
-> Optic'
     (IxKind (a0, a1, a2)) NoIx (a0, a1, a2) (IxValue (a0, a1, a2))
ix Index (a0, a1, a2)
i = AffineTraversalVL (a0, a1, a2) (a0, a1, a2) a2 a2
-> AffineTraversal (a0, a1, a2) (a0, a1, a2) a2 a2
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (a0, a1, a2) (a0, a1, a2) a2 a2
 -> AffineTraversal (a0, a1, a2) (a0, a1, a2) a2 a2)
-> AffineTraversalVL (a0, a1, a2) (a0, a1, a2) a2 a2
-> AffineTraversal (a0, a1, a2) (a0, a1, a2) a2 a2
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a2 -> f a2
f ~s :: (a0, a1, a2)
s@(a0, a1, a2) ->
    case Index (a0, a1, a2)
i of
      Index (a0, a1, a2)
0 -> (,a2
a1,a2
a2) (a2 -> (a2, a2, a2)) -> f a2 -> f (a2, a2, a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a2 -> f a2
f a2
a0
      Index (a0, a1, a2)
1 -> (a2
a0,,a2
a2) (a2 -> (a2, a2, a2)) -> f a2 -> f (a2, a2, a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a2 -> f a2
f a2
a1
      Index (a0, a1, a2)
2 -> (a2
a0,a2
a1,) (a2 -> (a2, a2, a2)) -> f a2 -> f (a2, a2, a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a2 -> f a2
f a2
a2
      Index (a0, a1, a2)
_ -> (a0, a1, a2) -> f (a0, a1, a2)
forall r. r -> f r
point (a0, a1, a2)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3) = a0
instance (a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) where
  ix :: Index (a0, a1, a2, a3)
-> Optic'
     (IxKind (a0, a1, a2, a3))
     NoIx
     (a0, a1, a2, a3)
     (IxValue (a0, a1, a2, a3))
ix Index (a0, a1, a2, a3)
i = AffineTraversalVL (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3
-> AffineTraversal (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3
 -> AffineTraversal (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3)
-> AffineTraversalVL (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3
-> AffineTraversal (a0, a1, a2, a3) (a0, a1, a2, a3) a3 a3
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a3 -> f a3
f ~s :: (a0, a1, a2, a3)
s@(a0, a1, a2, a3) ->
    case Index (a0, a1, a2, a3)
i of
      Index (a0, a1, a2, a3)
0 -> (,a3
a1,a3
a2,a3
a3) (a3 -> (a3, a3, a3, a3)) -> f a3 -> f (a3, a3, a3, a3)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a3 -> f a3
f a3
a0
      Index (a0, a1, a2, a3)
1 -> (a3
a0,,a3
a2,a3
a3) (a3 -> (a3, a3, a3, a3)) -> f a3 -> f (a3, a3, a3, a3)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a3 -> f a3
f a3
a1
      Index (a0, a1, a2, a3)
2 -> (a3
a0,a3
a1,,a3
a3) (a3 -> (a3, a3, a3, a3)) -> f a3 -> f (a3, a3, a3, a3)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a3 -> f a3
f a3
a2
      Index (a0, a1, a2, a3)
3 -> (a3
a0,a3
a1,a3
a2,) (a3 -> (a3, a3, a3, a3)) -> f a3 -> f (a3, a3, a3, a3)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a3 -> f a3
f a3
a3
      Index (a0, a1, a2, a3)
_ -> (a0, a1, a2, a3) -> f (a0, a1, a2, a3)
forall r. r -> f r
point (a0, a1, a2, a3)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3, a4) = a0
instance (a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) where
  ix :: Index (a0, a1, a2, a3, a4)
-> Optic'
     (IxKind (a0, a1, a2, a3, a4))
     NoIx
     (a0, a1, a2, a3, a4)
     (IxValue (a0, a1, a2, a3, a4))
ix Index (a0, a1, a2, a3, a4)
i = AffineTraversalVL (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4
-> AffineTraversal (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4
 -> AffineTraversal (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4)
-> AffineTraversalVL
     (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4
-> AffineTraversal (a0, a1, a2, a3, a4) (a0, a1, a2, a3, a4) a4 a4
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a4 -> f a4
f ~s :: (a0, a1, a2, a3, a4)
s@(a0, a1, a2, a3, a4) ->
    case Index (a0, a1, a2, a3, a4)
i of
      Index (a0, a1, a2, a3, a4)
0 -> (,a4
a1,a4
a2,a4
a3,a4
a4) (a4 -> (a4, a4, a4, a4, a4)) -> f a4 -> f (a4, a4, a4, a4, a4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a4 -> f a4
f a4
a0
      Index (a0, a1, a2, a3, a4)
1 -> (a4
a0,,a4
a2,a4
a3,a4
a4) (a4 -> (a4, a4, a4, a4, a4)) -> f a4 -> f (a4, a4, a4, a4, a4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a4 -> f a4
f a4
a1
      Index (a0, a1, a2, a3, a4)
2 -> (a4
a0,a4
a1,,a4
a3,a4
a4) (a4 -> (a4, a4, a4, a4, a4)) -> f a4 -> f (a4, a4, a4, a4, a4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a4 -> f a4
f a4
a2
      Index (a0, a1, a2, a3, a4)
3 -> (a4
a0,a4
a1,a4
a2,,a4
a4) (a4 -> (a4, a4, a4, a4, a4)) -> f a4 -> f (a4, a4, a4, a4, a4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a4 -> f a4
f a4
a3
      Index (a0, a1, a2, a3, a4)
4 -> (a4
a0,a4
a1,a4
a2,a4
a3,) (a4 -> (a4, a4, a4, a4, a4)) -> f a4 -> f (a4, a4, a4, a4, a4)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a4 -> f a4
f a4
a4
      Index (a0, a1, a2, a3, a4)
_ -> (a0, a1, a2, a3, a4) -> f (a0, a1, a2, a3, a4)
forall r. r -> f r
point (a0, a1, a2, a3, a4)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3, a4, a5) = a0
instance
  (a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5
  ) => Ixed (a0, a1, a2, a3, a4, a5) where
  ix :: Index (a0, a1, a2, a3, a4, a5)
-> Optic'
     (IxKind (a0, a1, a2, a3, a4, a5))
     NoIx
     (a0, a1, a2, a3, a4, a5)
     (IxValue (a0, a1, a2, a3, a4, a5))
ix Index (a0, a1, a2, a3, a4, a5)
i = AffineTraversalVL
  (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL
   (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5
 -> AffineTraversal
      (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5)
-> AffineTraversalVL
     (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5) (a0, a1, a2, a3, a4, a5) a5 a5
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a5 -> f a5
f ~s :: (a0, a1, a2, a3, a4, a5)
s@(a0, a1, a2, a3, a4, a5) ->
    case Index (a0, a1, a2, a3, a4, a5)
i of
      Index (a0, a1, a2, a3, a4, a5)
0 -> (,a5
a1,a5
a2,a5
a3,a5
a4,a5
a5) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a0
      Index (a0, a1, a2, a3, a4, a5)
1 -> (a5
a0,,a5
a2,a5
a3,a5
a4,a5
a5) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a1
      Index (a0, a1, a2, a3, a4, a5)
2 -> (a5
a0,a5
a1,,a5
a3,a5
a4,a5
a5) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a2
      Index (a0, a1, a2, a3, a4, a5)
3 -> (a5
a0,a5
a1,a5
a2,,a5
a4,a5
a5) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a3
      Index (a0, a1, a2, a3, a4, a5)
4 -> (a5
a0,a5
a1,a5
a2,a5
a3,,a5
a5) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a4
      Index (a0, a1, a2, a3, a4, a5)
5 -> (a5
a0,a5
a1,a5
a2,a5
a3,a5
a4,) (a5 -> (a5, a5, a5, a5, a5, a5))
-> f a5 -> f (a5, a5, a5, a5, a5, a5)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a5 -> f a5
f a5
a5
      Index (a0, a1, a2, a3, a4, a5)
_ -> (a0, a1, a2, a3, a4, a5) -> f (a0, a1, a2, a3, a4, a5)
forall r. r -> f r
point (a0, a1, a2, a3, a4, a5)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3, a4, a5, a6) = a0
instance
  (a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6
  ) => Ixed (a0, a1, a2, a3, a4, a5, a6) where
  ix :: Index (a0, a1, a2, a3, a4, a5, a6)
-> Optic'
     (IxKind (a0, a1, a2, a3, a4, a5, a6))
     NoIx
     (a0, a1, a2, a3, a4, a5, a6)
     (IxValue (a0, a1, a2, a3, a4, a5, a6))
ix Index (a0, a1, a2, a3, a4, a5, a6)
i = AffineTraversalVL
  (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL
   (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6
 -> AffineTraversal
      (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6)
-> AffineTraversalVL
     (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6) (a0, a1, a2, a3, a4, a5, a6) a6 a6
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a6 -> f a6
f ~s :: (a0, a1, a2, a3, a4, a5, a6)
s@(a0, a1, a2, a3, a4, a5, a6) ->
    case Index (a0, a1, a2, a3, a4, a5, a6)
i of
      Index (a0, a1, a2, a3, a4, a5, a6)
0 -> (,a6
a1,a6
a2,a6
a3,a6
a4,a6
a5,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a0
      Index (a0, a1, a2, a3, a4, a5, a6)
1 -> (a6
a0,,a6
a2,a6
a3,a6
a4,a6
a5,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a1
      Index (a0, a1, a2, a3, a4, a5, a6)
2 -> (a6
a0,a6
a1,,a6
a3,a6
a4,a6
a5,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a2
      Index (a0, a1, a2, a3, a4, a5, a6)
3 -> (a6
a0,a6
a1,a6
a2,,a6
a4,a6
a5,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a3
      Index (a0, a1, a2, a3, a4, a5, a6)
4 -> (a6
a0,a6
a1,a6
a2,a6
a3,,a6
a5,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a4
      Index (a0, a1, a2, a3, a4, a5, a6)
5 -> (a6
a0,a6
a1,a6
a2,a6
a3,a6
a4,,a6
a6) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a5
      Index (a0, a1, a2, a3, a4, a5, a6)
6 -> (a6
a0,a6
a1,a6
a2,a6
a3,a6
a4,a6
a5,) (a6 -> (a6, a6, a6, a6, a6, a6, a6))
-> f a6 -> f (a6, a6, a6, a6, a6, a6, a6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a6 -> f a6
f a6
a6
      Index (a0, a1, a2, a3, a4, a5, a6)
_ -> (a0, a1, a2, a3, a4, a5, a6) -> f (a0, a1, a2, a3, a4, a5, a6)
forall r. r -> f r
point (a0, a1, a2, a3, a4, a5, a6)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a, a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3, a4, a5, a6, a7) = a0
instance
  (a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7
  ) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) where
  ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7)
-> Optic'
     (IxKind (a0, a1, a2, a3, a4, a5, a6, a7))
     NoIx
     (a0, a1, a2, a3, a4, a5, a6, a7)
     (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
ix Index (a0, a1, a2, a3, a4, a5, a6, a7)
i = AffineTraversalVL
  (a0, a1, a2, a3, a4, a5, a6, a7)
  (a0, a1, a2, a3, a4, a5, a6, a7)
  a7
  a7
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6, a7)
     (a0, a1, a2, a3, a4, a5, a6, a7)
     a7
     a7
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL
   (a0, a1, a2, a3, a4, a5, a6, a7)
   (a0, a1, a2, a3, a4, a5, a6, a7)
   a7
   a7
 -> AffineTraversal
      (a0, a1, a2, a3, a4, a5, a6, a7)
      (a0, a1, a2, a3, a4, a5, a6, a7)
      a7
      a7)
-> AffineTraversalVL
     (a0, a1, a2, a3, a4, a5, a6, a7)
     (a0, a1, a2, a3, a4, a5, a6, a7)
     a7
     a7
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6, a7)
     (a0, a1, a2, a3, a4, a5, a6, a7)
     a7
     a7
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a7 -> f a7
f ~s :: (a0, a1, a2, a3, a4, a5, a6, a7)
s@(a0, a1, a2, a3, a4, a5, a6, a7) ->
    case Index (a0, a1, a2, a3, a4, a5, a6, a7)
i of
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
0 -> (,a7
a1,a7
a2,a7
a3,a7
a4,a7
a5,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a0
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
1 -> (a7
a0,,a7
a2,a7
a3,a7
a4,a7
a5,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a1
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
2 -> (a7
a0,a7
a1,,a7
a3,a7
a4,a7
a5,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a2
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
3 -> (a7
a0,a7
a1,a7
a2,,a7
a4,a7
a5,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a3
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
4 -> (a7
a0,a7
a1,a7
a2,a7
a3,,a7
a5,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a4
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
5 -> (a7
a0,a7
a1,a7
a2,a7
a3,a7
a4,,a7
a6,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a5
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
6 -> (a7
a0,a7
a1,a7
a2,a7
a3,a7
a4,a7
a5,,a7
a7) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a6
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
7 -> (a7
a0,a7
a1,a7
a2,a7
a3,a7
a4,a7
a5,a7
a6,) (a7 -> (a7, a7, a7, a7, a7, a7, a7, a7))
-> f a7 -> f (a7, a7, a7, a7, a7, a7, a7, a7)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a7 -> f a7
f a7
a7
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
_ -> (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (a0, a1, a2, a3, a4, a5, a6, a7)
forall r. r -> f r
point (a0, a1, a2, a3, a4, a5, a6, a7)
s

-- | @'ix' :: 'Int' -> 'AffineTraversal'' (a, a, a, a, a, a, a, a, a) a@
type instance IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) = a0
instance
  (a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7, a0 ~ a8
  ) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7, a8) where
  ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> Optic'
     (IxKind (a0, a1, a2, a3, a4, a5, a6, a7, a8))
     NoIx
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
ix Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
i = AffineTraversalVL
  (a0, a1, a2, a3, a4, a5, a6, a7, a8)
  (a0, a1, a2, a3, a4, a5, a6, a7, a8)
  a8
  a8
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     a8
     a8
forall s t a b.
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (AffineTraversalVL
   (a0, a1, a2, a3, a4, a5, a6, a7, a8)
   (a0, a1, a2, a3, a4, a5, a6, a7, a8)
   a8
   a8
 -> AffineTraversal
      (a0, a1, a2, a3, a4, a5, a6, a7, a8)
      (a0, a1, a2, a3, a4, a5, a6, a7, a8)
      a8
      a8)
-> AffineTraversalVL
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     a8
     a8
-> AffineTraversal
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     (a0, a1, a2, a3, a4, a5, a6, a7, a8)
     a8
     a8
forall a b. (a -> b) -> a -> b
$ \forall r. r -> f r
point a8 -> f a8
f ~s :: (a0, a1, a2, a3, a4, a5, a6, a7, a8)
s@(a0, a1, a2, a3, a4, a5, a6, a7, a8) ->
    case Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
i of
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
0 -> (,a8
a1,a8
a2,a8
a3,a8
a4,a8
a5,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a0
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
1 -> (a8
a0,,a8
a2,a8
a3,a8
a4,a8
a5,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a1
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
2 -> (a8
a0,a8
a1,,a8
a3,a8
a4,a8
a5,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a2
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
3 -> (a8
a0,a8
a1,a8
a2,,a8
a4,a8
a5,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a3
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
4 -> (a8
a0,a8
a1,a8
a2,a8
a3,,a8
a5,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a4
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
5 -> (a8
a0,a8
a1,a8
a2,a8
a3,a8
a4,,a8
a6,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a5
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
6 -> (a8
a0,a8
a1,a8
a2,a8
a3,a8
a4,a8
a5,,a8
a7,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a6
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
7 -> (a8
a0,a8
a1,a8
a2,a8
a3,a8
a4,a8
a5,a8
a6,,a8
a8) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a7
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
8 -> (a8
a0,a8
a1,a8
a2,a8
a3,a8
a4,a8
a5,a8
a6,a8
a7,) (a8 -> (a8, a8, a8, a8, a8, a8, a8, a8, a8))
-> f a8 -> f (a8, a8, a8, a8, a8, a8, a8, a8, a8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a8 -> f a8
f a8
a8
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
_ -> (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (a0, a1, a2, a3, a4, a5, a6, a7, a8)
forall r. r -> f r
point (a0, a1, a2, a3, a4, a5, a6, a7, a8)
s

-- | '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 '%' '_Just'
-- @
class (Ixed m, IxKind m ~ An_AffineTraversal) => At m where
  -- |
  -- >>> Map.fromList [(1,"world")] ^. at 1
  -- Just "world"
  --
  -- >>> at 1 ?~ "hello" $ Map.empty
  -- fromList [(1,"hello")]
  --
  -- /Note:/ Usage of this function might introduce space leaks if you're not
  -- careful to make sure that values put inside the 'Just' constructor are
  -- evaluated. To force the values and avoid such leaks, use 'at'' instead.
  --
  -- /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))

-- | Version of 'at' strict in the value inside the `Just` constructor.
--
-- Example:
--
-- >>> (at () .~ Just (error "oops") $ Nothing) `seq` ()
-- ()
--
-- >>> (at' () .~ Just (error "oops") $ Nothing) `seq` ()
-- *** Exception: oops
-- ...
--
-- >>> view (at ()) (Just $ error "oops") `seq` ()
-- ()
--
-- >>> view (at' ()) (Just $ error "oops") `seq` ()
-- *** Exception: oops
-- ...
--
-- It also works as expected for other data structures:
--
-- >>> (at 1 .~ Just (error "oops") $ Map.empty) `seq` ()
-- ()
--
-- >>> (at' 1 .~ Just (error "oops") $ Map.empty) `seq` ()
-- *** Exception: oops
-- ...
at' :: At m => Index m -> Lens' m (Maybe (IxValue m))
at' :: Index m -> Lens' m (Maybe (IxValue m))
at' Index m
k = Index m -> Lens' m (Maybe (IxValue m))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index m
k Lens' m (Maybe (IxValue m))
-> Optic
     An_Iso
     NoIx
     (Maybe (IxValue m))
     (Maybe (IxValue m))
     (Maybe (IxValue m))
     (Maybe (IxValue m))
-> Lens' m (Maybe (IxValue m))
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% (Maybe (IxValue m) -> Maybe (IxValue m))
-> (Maybe (IxValue m) -> Maybe (IxValue m))
-> Optic
     An_Iso
     NoIx
     (Maybe (IxValue m))
     (Maybe (IxValue m))
     (Maybe (IxValue m))
     (Maybe (IxValue m))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Maybe (IxValue m) -> Maybe (IxValue m)
forall a. Maybe a -> Maybe a
f Maybe (IxValue m) -> Maybe (IxValue m)
forall a. Maybe a -> Maybe a
f
  where
    f :: Maybe a -> Maybe a
f = \case
      Just !a
x -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
      Maybe a
Nothing -> Maybe a
forall a. Maybe a
Nothing
{-# INLINE at' #-}

-- | Delete the value associated with a key in a 'Map'-like container
--
-- @
-- 'sans' k = 'at' k 'Optics.Operators..~' Nothing
-- @
sans :: At m => Index m -> m -> m
sans :: Index m -> m -> m
sans Index m
k = Optic A_Lens NoIx m m (Maybe (IxValue m)) (Maybe (IxValue m))
-> Maybe (IxValue m) -> m -> m
forall k (is :: IxList) s t a b.
Is k A_Setter =>
Optic k is s t a b -> b -> s -> t
set (Index m
-> Optic A_Lens NoIx m m (Maybe (IxValue m)) (Maybe (IxValue m))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index m
k) Maybe (IxValue m)
forall a. Maybe a
Nothing
{-# INLINE sans #-}

instance At (Maybe a) where
  at :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a)))
at () = LensVL (Maybe a) (Maybe a) (Maybe a) (Maybe a)
-> Lens (Maybe a) (Maybe a) (Maybe a) (Maybe a)
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL forall a. a -> a
LensVL (Maybe a) (Maybe a) (Maybe a) (Maybe a)
id
  {-# INLINE at #-}

instance At (IntMap a) where
  at :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a)))
at Index (IntMap a)
k = LensVL (IntMap a) (IntMap a) (Maybe a) (Maybe a)
-> Lens (IntMap a) (IntMap a) (Maybe a) (Maybe a)
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (IntMap a) (IntMap a) (Maybe a) (Maybe a)
 -> Lens (IntMap a) (IntMap a) (Maybe a) (Maybe a))
-> LensVL (IntMap a) (IntMap a) (Maybe a) (Maybe a)
-> Lens (IntMap a) (IntMap a) (Maybe a) (Maybe a)
forall a b. (a -> b) -> a -> b
$ \Maybe a -> f (Maybe 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)
f Key
Index (IntMap a)
k
  {-# INLINE at #-}

instance Ord k => At (Map k a) where
  at :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a)))
at Index (Map k a)
k = LensVL (Map k a) (Map k a) (Maybe a) (Maybe a)
-> Lens (Map k a) (Map k a) (Maybe a) (Maybe a)
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (Map k a) (Map k a) (Maybe a) (Maybe a)
 -> Lens (Map k a) (Map k a) (Maybe a) (Maybe a))
-> LensVL (Map k a) (Map k a) (Maybe a) (Maybe a)
-> Lens (Map k a) (Map k a) (Maybe a) (Maybe a)
forall a b. (a -> b) -> a -> b
$ \Maybe a -> f (Maybe 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)
f k
Index (Map k a)
k
  {-# INLINE at #-}

instance At IntSet where
  at :: Index IntSet -> Lens' IntSet (Maybe (IxValue IntSet))
at Index IntSet
k = LensVL IntSet IntSet (Maybe ()) (Maybe ())
-> Lens IntSet IntSet (Maybe ()) (Maybe ())
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL IntSet IntSet (Maybe ()) (Maybe ())
 -> Lens IntSet IntSet (Maybe ()) (Maybe ()))
-> LensVL IntSet IntSet (Maybe ()) (Maybe ())
-> Lens IntSet IntSet (Maybe ()) (Maybe ())
forall a b. (a -> b) -> a -> b
$ \Maybe () -> f (Maybe ())
f IntSet
m ->
    let 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
    in Maybe () -> f (Maybe ())
f Maybe ()
mv f (Maybe ()) -> (Maybe () -> IntSet) -> f IntSet
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Maybe ()
r -> case Maybe ()
r of
      Maybe ()
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
  {-# INLINE at #-}

instance Ord k => At (Set k) where
  at :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k)))
at Index (Set k)
k = LensVL (Set k) (Set k) (Maybe ()) (Maybe ())
-> Lens (Set k) (Set k) (Maybe ()) (Maybe ())
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL (LensVL (Set k) (Set k) (Maybe ()) (Maybe ())
 -> Lens (Set k) (Set k) (Maybe ()) (Maybe ()))
-> LensVL (Set k) (Set k) (Maybe ()) (Maybe ())
-> Lens (Set k) (Set k) (Maybe ()) (Maybe ())
forall a b. (a -> b) -> a -> b
$ \Maybe () -> f (Maybe ())
f Set k
m ->
    let 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
    in Maybe () -> f (Maybe ())
f Maybe ()
mv f (Maybe ()) -> (Maybe () -> Set k) -> f (Set k)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Maybe ()
r -> case Maybe ()
r of
      Maybe ()
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
  {-# INLINE at #-}

----------------------------------------
-- Internal

ixListVL :: Int -> AffineTraversalVL' [a] a
ixListVL :: Key -> AffineTraversalVL' [a] a
ixListVL Key
k forall r. r -> f r
point a -> f a
f [a]
xs0 =
  if Key
k Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< Key
0
  then [a] -> f [a]
forall r. r -> f r
point [a]
xs0
  else let go :: [a] -> Key -> f [a]
go [] Key
_ = [a] -> f [a]
forall r. r -> f r
point []
           go (a
a:[a]
as) Key
0 = a -> f a
f 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) 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
- Key
1)
       in [a] -> Key -> f [a]
go [a]
xs0 Key
k
{-# INLINE ixListVL #-}

-- $setup
-- >>> import Optics.Core