{-# 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
LensVL s t a b -> Lens s t a b
lensVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \Bool -> f Bool
f IntSet
s -> Bool -> f Bool
f (Key -> IntSet -> Bool
IntSet.member Index IntSet
k IntSet
s) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \Bool
b ->
    if Bool
b then Key -> IntSet -> IntSet
IntSet.insert Index IntSet
k IntSet
s else Key -> IntSet -> IntSet
IntSet.delete 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
LensVL s t a b -> Lens s t a b
lensVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \Bool -> f Bool
f Set a
s -> Bool -> f Bool
f (forall (a :: OpticKind). Ord a => a -> Set a -> Bool
Set.member Index (Set a)
k Set a
s) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \Bool
b ->
    if Bool
b then forall (a :: OpticKind). Ord a => a -> Set a -> Set a
Set.insert Index (Set a)
k Set a
s else forall (a :: OpticKind). Ord a => a -> Set a -> Set a
Set.delete 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 = forall (m :: OpticKind).
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 :: forall (m :: OpticKind).
At m =>
Index m -> AffineTraversal' m (IxValue m)
ixAt = \Index m
i -> forall (m :: OpticKind).
At m =>
Index m -> Lens' m (Maybe (IxValue m))
at Index m
i forall (k :: OpticKind) (l :: OpticKind) (m :: OpticKind)
       (is :: IxList) (js :: IxList) (ks :: IxList) (s :: OpticKind)
       (t :: OpticKind) (u :: OpticKind) (v :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
(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
% forall (a :: OpticKind) (b :: OpticKind).
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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
LensVL s t a b -> Lens s t a b
lensVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \IxValue (e -> a) -> f (IxValue (e -> a))
p e -> a
f -> IxValue (e -> a) -> f (IxValue (e -> a))
p (e -> a
f Index (e -> a)
e) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \a
a e
e' -> if Index (e -> a)
e forall (a :: OpticKind). 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 () = forall (destKind :: OpticKind) (srcKind :: OpticKind)
       (is :: IxList) (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Is srcKind destKind =>
Optic srcKind is s t a b -> Optic destKind is s t a b
castOptic @An_AffineTraversal forall (a :: OpticKind) (b :: OpticKind).
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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL (forall (a :: OpticKind). Key -> AffineTraversalVL' [a] a
ixListVL 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
f NonEmpty a
xs0 ->
    if Index (NonEmpty a)
k forall (a :: OpticKind). Ord a => a -> a -> Bool
< Key
0
    then forall (r :: OpticKind). r -> f r
point NonEmpty a
xs0
    else let go :: NonEmpty a -> Key -> f (NonEmpty a)
go (a
a:|[a]
as) Key
0 = IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
f a
a forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> (forall (a :: OpticKind). a -> [a] -> NonEmpty a
:|[a]
as)
             go (a
a:|[a]
as) Key
i = (a
aforall (a :: OpticKind). a -> [a] -> NonEmpty a
:|) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> forall (a :: OpticKind). Key -> AffineTraversalVL' [a] a
ixListVL (Key
i forall (a :: OpticKind). Num a => a -> a -> a
- Key
1) forall (r :: OpticKind). r -> f r
point IxValue (NonEmpty a) -> f (IxValue (NonEmpty a))
f [a]
as
         in NonEmpty a -> Key -> f (NonEmpty a)
go NonEmpty a
xs0 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 () = forall (s :: OpticKind) (a :: OpticKind) (t :: OpticKind)
       (b :: OpticKind).
(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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (Tree a) -> f (IxValue (Tree a))
f ->
    let go :: [Key] -> Tree a -> f (Tree a)
go [] (Node a
a [Tree a]
as) = IxValue (Tree a) -> f (IxValue (Tree a))
f a
a forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \a
a' -> forall (a :: OpticKind). 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 forall (a :: OpticKind). Ord a => a -> a -> Bool
< Key
0     = forall (r :: OpticKind). r -> f r
point Tree a
t
          | Bool
otherwise = forall (a :: OpticKind). a -> [Tree a] -> Tree a
Node a
a forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> forall (a :: OpticKind). Key -> AffineTraversalVL' [a] a
ixListVL Key
i forall (r :: OpticKind). r -> f r
point ([Key] -> Tree a -> f (Tree a)
go [Key]
is) [Tree a]
as
    in [Key] -> Tree a -> f (Tree a)
go 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (Seq a) -> f (IxValue (Seq a))
f Seq a
m ->
    if Key
0 forall (a :: OpticKind). Ord a => a -> a -> Bool
<= Index (Seq a)
i Bool -> Bool -> Bool
&& Index (Seq a)
i forall (a :: OpticKind). Ord a => a -> a -> Bool
< forall (a :: OpticKind). Seq a -> Key
Seq.length Seq a
m
    then IxValue (Seq a) -> f (IxValue (Seq a))
f (forall (a :: OpticKind). Seq a -> Key -> a
Seq.index Seq a
m Index (Seq a)
i) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \a
a -> forall (a :: OpticKind). Key -> a -> Seq a -> Seq a
Seq.update Index (Seq a)
i a
a Seq a
m
    else forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (Set k) -> f (IxValue (Set k))
f Set k
m ->
    if forall (a :: OpticKind). Ord a => a -> Set a -> Bool
Set.member Index (Set k)
k Set k
m
    then IxValue (Set k) -> f (IxValue (Set k))
f () forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \() -> forall (a :: OpticKind). Ord a => a -> Set a -> Set a
Set.insert Index (Set k)
k Set k
m
    else forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue IntSet -> f (IxValue IntSet)
f IntSet
m ->
    if Key -> IntSet -> Bool
IntSet.member Index IntSet
k IntSet
m
    then IxValue IntSet -> f (IxValue IntSet)
f () forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \() -> Key -> IntSet -> IntSet
IntSet.insert Index IntSet
k IntSet
m
    else forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (Array i e) -> f (IxValue (Array i e))
f Array i e
arr ->
    if forall (a :: OpticKind). Ix a => (a, a) -> a -> Bool
inRange (forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> (i, i)
Array.bounds Array i e
arr) Index (Array i e)
i
    then IxValue (Array i e) -> f (IxValue (Array i e))
f (Array i e
arr forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> i -> e
Array.! Index (Array i e)
i) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \e
e -> Array i e
arr forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> [(i, e)] -> a i e
Array.// [(Index (Array i e)
i,e
e)]
    else forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (UArray i e) -> f (IxValue (UArray i e))
f UArray i e
arr ->
    if forall (a :: OpticKind). Ix a => (a, a) -> a -> Bool
inRange (forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> (i, i)
Array.bounds UArray i e
arr) Index (UArray i e)
i
    then IxValue (UArray i e) -> f (IxValue (UArray i e))
f (UArray i e
arr forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> i -> e
Array.! Index (UArray i e)
i) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \e
e -> UArray i e
arr forall (a :: OpticKind -> OpticKind -> OpticKind) (e :: OpticKind)
       (i :: OpticKind).
(IArray a e, Ix i) =>
a i e -> [(i, e)] -> a i e
Array.// [(Index (UArray i e)
i,e
e)]
    else forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1) -> f (IxValue (a0, a1))
f ~s :: (a0, a1)
s@(a0
a0, a1
a1) ->
    case Index (a0, a1)
i of
      Index (a0, a1)
0 -> (,a1
a1) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1) -> f (IxValue (a0, a1))
f a0
a0
      Index (a0, a1)
1 -> (a0
a0,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1) -> f (IxValue (a0, a1))
f a1
a1
      Index (a0, a1)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2) -> f (IxValue (a0, a1, a2))
f ~s :: (a0, a1, a2)
s@(a0
a0, a1
a1, a2
a2) ->
    case Index (a0, a1, a2)
i of
      Index (a0, a1, a2)
0 -> (,a1
a1,a2
a2) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2) -> f (IxValue (a0, a1, a2))
f a0
a0
      Index (a0, a1, a2)
1 -> (a0
a0,,a2
a2) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2) -> f (IxValue (a0, a1, a2))
f a1
a1
      Index (a0, a1, a2)
2 -> (a0
a0,a1
a1,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2) -> f (IxValue (a0, a1, a2))
f a2
a2
      Index (a0, a1, a2)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3) -> f (IxValue (a0, a1, a2, a3))
f ~s :: (a0, a1, a2, a3)
s@(a0
a0, a1
a1, a2
a2, a3
a3) ->
    case Index (a0, a1, a2, a3)
i of
      Index (a0, a1, a2, a3)
0 -> (,a1
a1,a2
a2,a3
a3) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3) -> f (IxValue (a0, a1, a2, a3))
f a0
a0
      Index (a0, a1, a2, a3)
1 -> (a0
a0,,a2
a2,a3
a3) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3) -> f (IxValue (a0, a1, a2, a3))
f a1
a1
      Index (a0, a1, a2, a3)
2 -> (a0
a0,a1
a1,,a3
a3) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3) -> f (IxValue (a0, a1, a2, a3))
f a2
a2
      Index (a0, a1, a2, a3)
3 -> (a0
a0,a1
a1,a2
a2,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3) -> f (IxValue (a0, a1, a2, a3))
f a3
a3
      Index (a0, a1, a2, a3)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f ~s :: (a0, a1, a2, a3, a4)
s@(a0
a0, a1
a1, a2
a2, a3
a3, a4
a4) ->
    case Index (a0, a1, a2, a3, a4)
i of
      Index (a0, a1, a2, a3, a4)
0 -> (,a1
a1,a2
a2,a3
a3,a4
a4) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f a0
a0
      Index (a0, a1, a2, a3, a4)
1 -> (a0
a0,,a2
a2,a3
a3,a4
a4) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f a1
a1
      Index (a0, a1, a2, a3, a4)
2 -> (a0
a0,a1
a1,,a3
a3,a4
a4) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f a2
a2
      Index (a0, a1, a2, a3, a4)
3 -> (a0
a0,a1
a1,a2
a2,,a4
a4) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f a3
a3
      Index (a0, a1, a2, a3, a4)
4 -> (a0
a0,a1
a1,a2
a2,a3
a3,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4) -> f (IxValue (a0, a1, a2, a3, a4))
f a4
a4
      Index (a0, a1, a2, a3, a4)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f ~s :: (a0, a1, a2, a3, a4, a5)
s@(a0
a0, a1
a1, a2
a2, a3
a3, a4
a4, a5
a5) ->
    case Index (a0, a1, a2, a3, a4, a5)
i of
      Index (a0, a1, a2, a3, a4, a5)
0 -> (,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a0
a0
      Index (a0, a1, a2, a3, a4, a5)
1 -> (a0
a0,,a2
a2,a3
a3,a4
a4,a5
a5) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a1
a1
      Index (a0, a1, a2, a3, a4, a5)
2 -> (a0
a0,a1
a1,,a3
a3,a4
a4,a5
a5) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a2
a2
      Index (a0, a1, a2, a3, a4, a5)
3 -> (a0
a0,a1
a1,a2
a2,,a4
a4,a5
a5) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a3
a3
      Index (a0, a1, a2, a3, a4, a5)
4 -> (a0
a0,a1
a1,a2
a2,a3
a3,,a5
a5) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a4
a4
      Index (a0, a1, a2, a3, a4, a5)
5 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5)
-> f (IxValue (a0, a1, a2, a3, a4, a5))
f a5
a5
      Index (a0, a1, a2, a3, a4, a5)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f ~s :: (a0, a1, a2, a3, a4, a5, a6)
s@(a0
a0, a1
a1, a2
a2, a3
a3, a4
a4, a5
a5, a6
a6) ->
    case Index (a0, a1, a2, a3, a4, a5, a6)
i of
      Index (a0, a1, a2, a3, a4, a5, a6)
0 -> (,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a0
a0
      Index (a0, a1, a2, a3, a4, a5, a6)
1 -> (a0
a0,,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a1
a1
      Index (a0, a1, a2, a3, a4, a5, a6)
2 -> (a0
a0,a1
a1,,a3
a3,a4
a4,a5
a5,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a2
a2
      Index (a0, a1, a2, a3, a4, a5, a6)
3 -> (a0
a0,a1
a1,a2
a2,,a4
a4,a5
a5,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a3
a3
      Index (a0, a1, a2, a3, a4, a5, a6)
4 -> (a0
a0,a1
a1,a2
a2,a3
a3,,a5
a5,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a4
a4
      Index (a0, a1, a2, a3, a4, a5, a6)
5 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,,a6
a6) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a5
a5
      Index (a0, a1, a2, a3, a4, a5, a6)
6 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6))
f a6
a6
      Index (a0, a1, a2, a3, a4, a5, a6)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f ~s :: (a0, a1, a2, a3, a4, a5, a6, a7)
s@(a0
a0, a1
a1, a2
a2, a3
a3, a4
a4, a5
a5, a6
a6, a7
a7) ->
    case Index (a0, a1, a2, a3, a4, a5, a6, a7)
i of
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
0 -> (,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a0
a0
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
1 -> (a0
a0,,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a1
a1
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
2 -> (a0
a0,a1
a1,,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a2
a2
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
3 -> (a0
a0,a1
a1,a2
a2,,a4
a4,a5
a5,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a3
a3
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
4 -> (a0
a0,a1
a1,a2
a2,a3
a3,,a5
a5,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a4
a4
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
5 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,,a6
a6,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a5
a5
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
6 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,,a7
a7) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a6
a6
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
7 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7))
f a7
a7
      Index (a0, a1, a2, a3, a4, a5, a6, a7)
_ -> forall (r :: OpticKind). 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
AffineTraversalVL s t a b -> AffineTraversal s t a b
atraversalVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \forall (r :: OpticKind). r -> f r
point IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f ~s :: (a0, a1, a2, a3, a4, a5, a6, a7, a8)
s@(a0
a0, a1
a1, a2
a2, a3
a3, a4
a4, a5
a5, a6
a6, a7
a7, a8
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 -> (,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a0
a0
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
1 -> (a0
a0,,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a1
a1
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
2 -> (a0
a0,a1
a1,,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a2
a2
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
3 -> (a0
a0,a1
a1,a2
a2,,a4
a4,a5
a5,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a3
a3
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
4 -> (a0
a0,a1
a1,a2
a2,a3
a3,,a5
a5,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a4
a4
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
5 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,,a6
a6,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a5
a5
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
6 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,,a7
a7,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a6
a6
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
7 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,,a8
a8) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a7
a7
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
8 -> (a0
a0,a1
a1,a2
a2,a3
a3,a4
a4,a5
a5,a6
a6,a7
a7,) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)
-> f (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8))
f a8
a8
      Index (a0, a1, a2, a3, a4, a5, a6, a7, a8)
_ -> forall (r :: OpticKind). 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' :: forall (m :: OpticKind).
At m =>
Index m -> Lens' m (Maybe (IxValue m))
at' Index m
k = forall (m :: OpticKind).
At m =>
Index m -> Lens' m (Maybe (IxValue m))
at Index m
k forall (k :: OpticKind) (l :: OpticKind) (m :: OpticKind)
       (is :: IxList) (js :: IxList) (ks :: IxList) (s :: OpticKind)
       (t :: OpticKind) (u :: OpticKind) (v :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
(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
% forall (s :: OpticKind) (a :: OpticKind) (b :: OpticKind)
       (t :: OpticKind).
(s -> a) -> (b -> t) -> Iso s t a b
iso forall {a :: OpticKind}. Maybe a -> Maybe a
f forall {a :: OpticKind}. Maybe a -> Maybe a
f
  where
    f :: Maybe a -> Maybe a
f = \case
      Just !a
x -> forall (a :: OpticKind). a -> Maybe a
Just a
x
      Maybe a
Nothing -> forall (a :: OpticKind). 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 :: forall (m :: OpticKind). At m => Index m -> m -> m
sans Index m
k = forall (k :: OpticKind) (is :: IxList) (s :: OpticKind)
       (t :: OpticKind) (a :: OpticKind) (b :: OpticKind).
Is k A_Setter =>
Optic k is s t a b -> b -> s -> t
set (forall (m :: OpticKind).
At m =>
Index m -> Lens' m (Maybe (IxValue m))
at Index m
k) forall (a :: OpticKind). Maybe a
Nothing
{-# INLINE sans #-}

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

instance At IntSet where
  at :: Index IntSet -> Lens' IntSet (Maybe (IxValue IntSet))
at Index IntSet
k = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
LensVL s t a b -> Lens s t a b
lensVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \Maybe (IxValue IntSet) -> f (Maybe (IxValue IntSet))
f IntSet
m ->
    let mv :: Maybe ()
mv = if Key -> IntSet -> Bool
IntSet.member Index IntSet
k IntSet
m
             then forall (a :: OpticKind). a -> Maybe a
Just ()
             else forall (a :: OpticKind). Maybe a
Nothing
    in Maybe (IxValue IntSet) -> f (Maybe (IxValue IntSet))
f Maybe ()
mv forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \Maybe ()
r -> case Maybe ()
r of
      Maybe ()
Nothing -> forall (b :: OpticKind) (a :: OpticKind).
b -> (a -> b) -> Maybe a -> b
maybe IntSet
m (forall (a :: OpticKind) (b :: OpticKind). a -> b -> a
const (Key -> IntSet -> IntSet
IntSet.delete Index IntSet
k IntSet
m)) Maybe ()
mv
      Just () -> Key -> IntSet -> IntSet
IntSet.insert 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 = forall (s :: OpticKind) (t :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
LensVL s t a b -> Lens s t a b
lensVL forall (a :: OpticKind) b. (a -> b) -> a -> b
$ \Maybe (IxValue (Set k)) -> f (Maybe (IxValue (Set k)))
f Set k
m ->
    let mv :: Maybe ()
mv = if forall (a :: OpticKind). Ord a => a -> Set a -> Bool
Set.member Index (Set k)
k Set k
m
             then forall (a :: OpticKind). a -> Maybe a
Just ()
             else forall (a :: OpticKind). Maybe a
Nothing
    in Maybe (IxValue (Set k)) -> f (Maybe (IxValue (Set k)))
f Maybe ()
mv forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> \Maybe ()
r -> case Maybe ()
r of
      Maybe ()
Nothing -> forall (b :: OpticKind) (a :: OpticKind).
b -> (a -> b) -> Maybe a -> b
maybe Set k
m (forall (a :: OpticKind) (b :: OpticKind). a -> b -> a
const (forall (a :: OpticKind). Ord a => a -> Set a -> Set a
Set.delete Index (Set k)
k Set k
m)) Maybe ()
mv
      Just () -> forall (a :: OpticKind). Ord a => a -> Set a -> Set a
Set.insert Index (Set k)
k Set k
m
  {-# INLINE at #-}

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

ixListVL :: Int -> AffineTraversalVL' [a] a
ixListVL :: forall (a :: OpticKind). Key -> AffineTraversalVL' [a] a
ixListVL Key
k forall (r :: OpticKind). r -> f r
point a -> f a
f [a]
xs0 =
  if Key
k forall (a :: OpticKind). Ord a => a -> a -> Bool
< Key
0
  then forall (r :: OpticKind). r -> f r
point [a]
xs0
  else let go :: [a] -> Key -> f [a]
go [] Key
_ = forall (r :: OpticKind). r -> f r
point []
           go (a
a:[a]
as) Key
0 = a -> f a
f a
a forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
f a -> (a -> b) -> f b
<&> (forall (a :: OpticKind). a -> [a] -> [a]
:[a]
as)
           go (a
a:[a]
as) Key
i = (a
aforall (a :: OpticKind). a -> [a] -> [a]
:) forall (f :: OpticKind -> OpticKind) (a :: OpticKind)
       (b :: OpticKind).
Functor f =>
(a -> b) -> f a -> f b
<$> ([a] -> Key -> f [a]
go [a]
as forall (a :: OpticKind) b. (a -> b) -> a -> b
$! Key
i forall (a :: OpticKind). Num a => a -> a -> a
- Key
1)
       in [a] -> Key -> f [a]
go [a]
xs0 Key
k
{-# INLINE ixListVL #-}

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