optics-core-0.4.1.1: Optics as an abstract interface: core definitions

Safe Haskell | None
Language | Haskell2010

Optics.At.Core

Contents

- Type families
- Ixed
- At
- Contains

Description

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.

Synopsis

- type family Index s
- type family IxValue m
- class Ixed m where
  - type IxKind m
  - ix :: Index m -> Optic' (IxKind m) NoIx m (IxValue m)
- ixAt :: At m => Index m -> AffineTraversal' m (IxValue m)
- class (Ixed m, IxKind m ~ An_AffineTraversal) => At m where
  - at :: Index m -> Lens' m (Maybe (IxValue m))
- at' :: At m => Index m -> Lens' m (Maybe (IxValue m))
- sans :: At m => Index m -> m -> m
- class Contains m where
  - contains :: Index m -> Lens' m Bool

# Type families

type family Index s Source #

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`.

#### Instances

Instances details

type Index IntSet Source # |
Instance details Defined in Optics.At.Core type Index IntSet = Int
type Index (Complex a) Source # |
Instance details Defined in Optics.At.Core type Index (Complex a) = Int
type Index (IntMap a) Source # |
Instance details Defined in Optics.At.Core type Index (IntMap a) = Int
type Index (Seq a) Source # |
Instance details Defined in Optics.At.Core type Index (Seq a) = Int
type Index (Set a) Source # |
Instance details Defined in Optics.At.Core type Index (Set a) = a
type Index (Tree a) Source # |
Instance details Defined in Optics.At.Core type Index (Tree a) = [Int]
type Index (NonEmpty a) Source # |
Instance details Defined in Optics.At.Core type Index (NonEmpty a) = Int
type Index (Identity a) Source # |
Instance details Defined in Optics.At.Core type Index (Identity a) = ()
type Index (Maybe a) Source # |
Instance details Defined in Optics.At.Core type Index (Maybe a) = ()
type Index [a] Source # |
Instance details Defined in Optics.At.Core type Index [a] = Int
type Index (UArray i e) Source # |
Instance details Defined in Optics.At.Core type Index (UArray i e) = i
type Index (Map k a) Source # |
Instance details Defined in Optics.At.Core type Index (Map k a) = k
type Index (Array i e) Source # |
Instance details Defined in Optics.At.Core type Index (Array i e) = i
type Index (a, b) Source # |
Instance details Defined in Optics.At.Core type Index (a, b) = Int
type Index (e -> a) Source # |
Instance details Defined in Optics.At.Core type Index (e -> a) = e
type Index (a, b, c) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c) = Int
type Index (a, b, c, d) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d) = Int
type Index (a, b, c, d, e) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d, e) = Int
type Index (a, b, c, d, e, f) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d, e, f) = Int
type Index (a, b, c, d, e, f, g) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d, e, f, g) = Int
type Index (a, b, c, d, e, f, g, h) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d, e, f, g, h) = Int
type Index (a, b, c, d, e, f, g, h, i) Source # |
Instance details Defined in Optics.At.Core type Index (a, b, c, d, e, f, g, h, i) = Int

type family IxValue m Source #

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`.

#### Instances

Instances details

type IxValue IntSet Source # |
Instance details Defined in Optics.At.Core type IxValue IntSet = ()
type IxValue (IntMap a) Source # |
Instance details Defined in Optics.At.Core type IxValue (IntMap a) = a
type IxValue (Seq a) Source # |
Instance details Defined in Optics.At.Core type IxValue (Seq a) = a
type IxValue (Set k) Source # |
Instance details Defined in Optics.At.Core type IxValue (Set k) = ()
type IxValue (Tree a) Source # |
Instance details Defined in Optics.At.Core type IxValue (Tree a) = a
type IxValue (NonEmpty a) Source # |
Instance details Defined in Optics.At.Core type IxValue (NonEmpty a) = a
type IxValue (Identity a) Source # |
Instance details Defined in Optics.At.Core type IxValue (Identity a) = a
type IxValue (Maybe a) Source # |
Instance details Defined in Optics.At.Core type IxValue (Maybe a) = a
type IxValue [a] Source # |
Instance details Defined in Optics.At.Core type IxValue [a] = a
type IxValue (UArray i e) Source # |
Instance details Defined in Optics.At.Core type IxValue (UArray i e) = e
type IxValue (Map k a) Source # |
Instance details Defined in Optics.At.Core type IxValue (Map k a) = a
type IxValue (Array i e) Source # |
Instance details Defined in Optics.At.Core type IxValue (Array i e) = e

type IxValue (a0, a2) Source #

    ix :: Int -> AffineTraversal' (a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a2) = a0
type IxValue (e -> a) Source # |
Instance details Defined in Optics.At.Core type IxValue (e -> a) = a

type IxValue (a0, a1, a2) Source #

    ix :: Int -> AffineTraversal' (a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2) = a0

type IxValue (a0, a1, a2, a3) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3) = a0

type IxValue (a0, a1, a2, a3, a4) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4) = a0

type IxValue (a0, a1, a2, a3, a4, a5) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5) = a0

type IxValue (a0, a1, a2, a3, a4, a5, a6) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6) = a0

type IxValue (a0, a1, a2, a3, a4, a5, a6, a7) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6, a7) = a0

type IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) Source #

    ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a, a, a) a

Instance details Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) = a0

# Ixed

class Ixed m where Source #

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`.

Minimal complete definition

Nothing

Associated Types

type IxKind m Source #

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 = An_AffineTraversal

Methods

ix :: Index m -> Optic' (IxKind m) NoIx m (IxValue m) Source #

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

default ix :: (At m, IxKind m ~ An_AffineTraversal) => Index m -> Optic' (IxKind m) NoIx m (IxValue m) Source #

#### Instances

Instances details

Ixed IntSet Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind IntSet |
Instance details Defined in Optics.At.Core type IxKind IntSet = An_AffineTraversal

Methods

ix :: Index IntSet -> Optic' (IxKind IntSet) NoIx IntSet (IxValue IntSet) Source #

Ixed (IntMap a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (IntMap a) |
Instance details Defined in Optics.At.Core type IxKind (IntMap a) = An_AffineTraversal

Methods

ix :: Index (IntMap a) -> Optic' (IxKind (IntMap a)) NoIx (IntMap a) (IxValue (IntMap a)) Source #

Ixed (Seq a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Seq a) |
Instance details Defined in Optics.At.Core type IxKind (Seq a) = An_AffineTraversal

Methods

ix :: Index (Seq a) -> Optic' (IxKind (Seq a)) NoIx (Seq a) (IxValue (Seq a)) Source #

Ord k => Ixed (Set k) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Set k) |
Instance details Defined in Optics.At.Core type IxKind (Set k) = An_AffineTraversal

Methods

ix :: Index (Set k) -> Optic' (IxKind (Set k)) NoIx (Set k) (IxValue (Set k)) Source #

Ixed (Tree a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Tree a) |
Instance details Defined in Optics.At.Core type IxKind (Tree a) = An_AffineTraversal

Methods

ix :: Index (Tree a) -> Optic' (IxKind (Tree a)) NoIx (Tree a) (IxValue (Tree a)) Source #

Ixed (NonEmpty a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (NonEmpty a) |
Instance details Defined in Optics.At.Core type IxKind (NonEmpty a) = An_AffineTraversal

Methods

ix :: Index (NonEmpty a) -> Optic' (IxKind (NonEmpty a)) NoIx (NonEmpty a) (IxValue (NonEmpty a)) Source #

Ixed (Identity a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Identity a) |
Instance details Defined in Optics.At.Core type IxKind (Identity a) = An_Iso

Methods

ix :: Index (Identity a) -> Optic' (IxKind (Identity a)) NoIx (Identity a) (IxValue (Identity a)) Source #

Ixed (Maybe a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Maybe a) |
Instance details Defined in Optics.At.Core type IxKind (Maybe a) = An_AffineTraversal

Methods

ix :: Index (Maybe a) -> Optic' (IxKind (Maybe a)) NoIx (Maybe a) (IxValue (Maybe a)) Source #

Ixed [a] Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind [a] |
Instance details Defined in Optics.At.Core type IxKind [a] = An_AffineTraversal

Methods

ix :: Index [a] -> Optic' (IxKind [a]) NoIx [a] (IxValue [a]) Source #

(IArray UArray e, Ix i) => Ixed (UArray i e) Source #

    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (UArray i e) |
Instance details Defined in Optics.At.Core type IxKind (UArray i e) = An_AffineTraversal

Methods

ix :: Index (UArray i e) -> Optic' (IxKind (UArray i e)) NoIx (UArray i e) (IxValue (UArray i e)) Source #

Ord k => Ixed (Map k a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Map k a) |
Instance details Defined in Optics.At.Core type IxKind (Map k a) = An_AffineTraversal

Methods

ix :: Index (Map k a) -> Optic' (IxKind (Map k a)) NoIx (Map k a) (IxValue (Map k a)) Source #

Ix i => Ixed (Array i e) Source #

    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Array i e) |
Instance details Defined in Optics.At.Core type IxKind (Array i e) = An_AffineTraversal

Methods

ix :: Index (Array i e) -> Optic' (IxKind (Array i e)) NoIx (Array i e) (IxValue (Array i e)) Source #

a0 ~ a1 => Ixed (a0, a1) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1) = An_AffineTraversal

Methods

ix :: Index (a0, a1) -> Optic' (IxKind (a0, a1)) NoIx (a0, a1) (IxValue (a0, a1)) Source #

Eq e => Ixed (e -> a) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (e -> a) |
Instance details Defined in Optics.At.Core type IxKind (e -> a) = A_Lens

Methods

ix :: Index (e -> a) -> Optic' (IxKind (e -> a)) NoIx (e -> a) (IxValue (e -> a)) Source #

(a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2) = An_AffineTraversal

Methods

ix :: Index (a0, a1, a2) -> Optic' (IxKind (a0, a1, a2)) NoIx (a0, a1, a2) (IxValue (a0, a1, a2)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3) = An_AffineTraversal

Methods

ix :: Index (a0, a1, a2, a3) -> Optic' (IxKind (a0, a1, a2, a3)) NoIx (a0, a1, a2, a3) (IxValue (a0, a1, a2, a3)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3, a4) = An_AffineTraversal

Methods

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)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5) => Ixed (a0, a1, a2, a3, a4, a5) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3, a4, a5) = An_AffineTraversal

Methods

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)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6) => Ixed (a0, a1, a2, a3, a4, a5, a6) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3, a4, a5, a6) = An_AffineTraversal

Methods

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)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6, a7) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3, a4, a5, a6, a7) = An_AffineTraversal

Methods

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)) Source #

(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) Source # |

Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6, a7, a8) |
Instance details Defined in Optics.At.Core type IxKind (a0, a1, a2, a3, a4, a5, a6, a7, a8) = An_AffineTraversal

Methods

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)) Source #

ixAt :: At m => Index m -> AffineTraversal' m (IxValue m) Source #

A definition of `ix` for types with an `At` instance. This is the default if you don't specify a definition for `ix`.

# At

class (Ixed m, IxKind m ~ An_AffineTraversal) => At m where Source #

`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

Methods

at :: Index m -> Lens' m (Maybe (IxValue m)) Source #

    >>> 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.

#### Instances

Instances details

At IntSet Source # |
Instance details Defined in Optics.At.Core Methods at :: Index IntSet -> Lens' IntSet (Maybe (IxValue IntSet)) Source #
At (IntMap a) Source # |
Instance details Defined in Optics.At.Core Methods at :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a))) Source #
Ord k => At (Set k) Source # |
Instance details Defined in Optics.At.Core Methods at :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k))) Source #
At (Maybe a) Source # |
Instance details Defined in Optics.At.Core Methods at :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a))) Source #
Ord k => At (Map k a) Source # |
Instance details Defined in Optics.At.Core Methods at :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a))) Source #

at' :: At m => Index m -> Lens' m (Maybe (IxValue m)) Source #

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
    ...

sans :: At m => Index m -> m -> m Source #

Delete the value associated with a key in a `Map`-like container

    sans k = at k .~ Nothing

# Contains

class Contains m where Source #

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.

Methods

contains :: Index m -> Lens' m Bool Source #

    >>> 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]

#### Instances

Instances details

Contains IntSet Source # |
Instance details Defined in Optics.At.Core Methods contains :: Index IntSet -> Lens' IntSet Bool Source #
Ord a => Contains (Set a) Source # |
Instance details Defined in Optics.At.Core Methods contains :: Index (Set a) -> Lens' (Set a) Bool Source #