planet-mitchell-0.1.0: Planet Mitchell

Optic.Lens

Contents

Synopsis

Lens

type Lens s t a b = forall (f :: * -> *). Functor f => (a -> f b) -> s -> f t #

A Lens is actually a lens family as described in http://comonad.com/reader/2012/mirrored-lenses/.

With great power comes great responsibility and a Lens is subject to the three common sense Lens laws:

1) You get back what you put in:

view l (set l v s)  ≡ v


2) Putting back what you got doesn't change anything:

set l (view l s) s  ≡ s


3) Setting twice is the same as setting once:

set l v' (set l v s) ≡ set l v' s


These laws are strong enough that the 4 type parameters of a Lens cannot vary fully independently. For more on how they interact, read the "Why is it a Lens Family?" section of http://comonad.com/reader/2012/mirrored-lenses/.

There are some emergent properties of these laws:

1) set l s must be injective for every s This is a consequence of law #1

2) set l must be surjective, because of law #2, which indicates that it is possible to obtain any v from some s such that set s v = s

3) Given just the first two laws you can prove a weaker form of law #3 where the values v that you are setting match:

set l v (set l v s) ≡ set l v s


Every Lens can be used directly as a Setter or Traversal.

You can also use a Lens for Getting as if it were a Fold or Getter.

Since every Lens is a valid Traversal, the Traversal laws are required of any Lens you create:

l pure ≡ pure
fmap (l f) . l g ≡ getCompose . l (Compose . fmap f . g)

type Lens s t a b = forall f. Functor f => LensLike f s t a b


type Lens' s a = Lens s s a a #

type Lens' = Simple Lens


lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b #

Build a Lens from a getter and a setter.

lens :: Functor f => (s -> a) -> (s -> b -> t) -> (a -> f b) -> s -> f t

>>> s ^. lens getter setter
getter s

>>> s & lens getter setter .~ b
setter s b

>>> s & lens getter setter %~ f
setter s (f (getter s))

lens :: (s -> a) -> (s -> a -> s) -> Lens' s a


HasAny

class HasAny (sel :: k) s t a b | s sel k -> a where #

Minimal complete definition

the

Methods

the :: Lens s t a b #

A lens that focuses on a part of a product as identified by some selector. Currently supported selectors are field names, positions and unique types. Compatible with the lens package's Lens type.

>>> human ^. the @Int
50

>>> human ^. the @"name"
"Tunyasz"

>>> human ^. the @3
"London"

Instances
 HasPosition i s t a b => HasAny (i :: Nat) s t a b Instance detailsDefined in Data.Generics.Product.Any Methodsthe :: Lens s t a b # HasField field s t a b => HasAny (field :: Symbol) s t a b Instance detailsDefined in Data.Generics.Product.Any Methodsthe :: Lens s t a b # (HasType a s, t ~ s, a ~ b) => HasAny (a :: *) s t a b Instance detailsDefined in Data.Generics.Product.Any Methodsthe :: Lens s t a b #

Subtype

class Subtype sup sub where #

Structural subtype relationship

sub is a (structural) subtype of sup, if its fields are a subset of those of sup.

Minimal complete definition

Methods

super :: Lens sub sub sup sup #

Structural subtype lens. Given a subtype relationship sub :< sup, we can focus on the sub structure of sup.

>>> human ^. super @Animal
Animal {name = "Tunyasz", age = 50}

>>> set (super @Animal) (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}


upcast :: sub -> sup #

Cast the more specific subtype to the more general supertype

>>> upcast human :: Animal
Animal {name = "Tunyasz", age = 50}

>>> upcast (upcast human :: Animal) :: Human
...
... The type 'Animal' is not a subtype of 'Human'.
... The following fields are missing from 'Animal':
...


smash :: sup -> sub -> sub #

Plug a smaller structure into a larger one

>>> smash (Animal "dog" 10) human
Human {name = "dog", age = 10, address = "London"}

Instances
 (Generic a, Generic b, GSmash (Rep a) (Rep b), GUpcast (Rep a) (Rep b), ErrorUnless b a (CollectFieldsOrdered (Rep b) \\ CollectFieldsOrdered (Rep a))) => Subtype b a Instance detailsDefined in Data.Generics.Product.Subtype Methodssuper :: Lens a a b b #upcast :: a -> b #smash :: b -> a -> a # Subtype a Void Instance detailsDefined in Data.Generics.Product.Subtype Methodssuper :: Lens Void Void a a #upcast :: Void -> a #smash :: a -> Void -> Void # Subtype Void a Instance detailsDefined in Data.Generics.Product.Subtype Methodssuper :: Lens a a Void Void #upcast :: a -> Void #smash :: Void -> a -> a #

At

class Ixed m => At m where #

At provides a Lens that can be used to read, write or delete the value associated with a key in a Map-like container on an ad hoc basis.

An instance of At should satisfy:

ix k ≡ at k . traverse


Minimal complete definition

at

Methods

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

>>> Map.fromList [(1,"world")] ^.at 1
Just "world"

>>> at 1 ?~ "hello" \$ Map.empty
fromList [(1,"hello")]


Note: Map-like containers form a reasonable instance, but not Array-like ones, where you cannot satisfy the Lens laws.

Instances
 Instance detailsDefined in Control.Lens.At Methods At (Maybe a) Instance detailsDefined in Control.Lens.At Methodsat :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a))) # At (IntMap a) Instance detailsDefined in Control.Lens.At Methodsat :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a))) # Ord k => At (Set k) Instance detailsDefined in Control.Lens.At Methodsat :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k))) # (Eq k, Hashable k) => At (HashSet k) Instance detailsDefined in Control.Lens.At Methodsat :: Index (HashSet k) -> Lens' (HashSet k) (Maybe (IxValue (HashSet k))) # (Eq k, Hashable k) => At (HashMap k a) Instance detailsDefined in Control.Lens.At Methodsat :: Index (HashMap k a) -> Lens' (HashMap k a) (Maybe (IxValue (HashMap k a))) # Ord k => At (Map k a) Instance detailsDefined in Control.Lens.At Methodsat :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a))) # (Eq k, Hashable k) => At (InsOrdHashMap k a) Instance detailsDefined in Data.HashMap.Strict.InsOrd Methodsat :: Index (InsOrdHashMap k a) -> Lens' (InsOrdHashMap k a) (Maybe (IxValue (InsOrdHashMap k a))) #

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

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

sans k = at k .~ Nothing


type family Index s :: * #

Instances
 type Index ByteString Instance detailsDefined in Control.Lens.At type Index ByteString = Int type Index ByteString Instance detailsDefined in Control.Lens.At type Index ByteString = Int64 type Index Text Instance detailsDefined in Control.Lens.At type Index Text = Int type Index Value Instance detailsDefined in Data.Aeson.Lens type Index Value = Text type Index Text Instance detailsDefined in Control.Lens.At type Index Text = Int64 type Index IntSet Instance detailsDefined in Control.Lens.At type Index IntSet = Int type Index [a] Instance detailsDefined in Control.Lens.At type Index [a] = Int type Index (Maybe a) Instance detailsDefined in Control.Lens.At type Index (Maybe a) = () type Index (Complex a) Instance detailsDefined in Control.Lens.At type Index (Complex a) = Int type Index (Identity a) Instance detailsDefined in Control.Lens.At type Index (Identity a) = () type Index (NonEmpty a) Instance detailsDefined in Control.Lens.At type Index (NonEmpty a) = Int type Index (IntMap a) Instance detailsDefined in Control.Lens.At type Index (IntMap a) = Int type Index (Tree a) Instance detailsDefined in Control.Lens.At type Index (Tree a) = [Int] type Index (Seq a) Instance detailsDefined in Control.Lens.At type Index (Seq a) = Int type Index (Set a) Instance detailsDefined in Control.Lens.At type Index (Set a) = a type Index (Vector a) Instance detailsDefined in Control.Lens.At type Index (Vector a) = Int type Index (Vector a) Instance detailsDefined in Control.Lens.At type Index (Vector a) = Int type Index (Vector a) Instance detailsDefined in Control.Lens.At type Index (Vector a) = Int type Index (HashSet a) Instance detailsDefined in Control.Lens.At type Index (HashSet a) = a type Index (Vector a) Instance detailsDefined in Control.Lens.At type Index (Vector a) = Int type Index (e -> a) Instance detailsDefined in Control.Lens.At type Index (e -> a) = e type Index (a, b) Instance detailsDefined in Control.Lens.At type Index (a, b) = Int type Index (HashMap k a) Instance detailsDefined in Control.Lens.At type Index (HashMap k a) = k type Index (Map k a) Instance detailsDefined in Control.Lens.At type Index (Map k a) = k type Index (UArray i e) Instance detailsDefined in Control.Lens.At type Index (UArray i e) = i type Index (Array i e) Instance detailsDefined in Control.Lens.At type Index (Array i e) = i type Index (InsOrdHashMap k v) Instance detailsDefined in Data.HashMap.Strict.InsOrd type Index (InsOrdHashMap k v) = k type Index (a, b, c) Instance detailsDefined in Control.Lens.At type Index (a, b, c) = Int type Index (a, b, c, d) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d) = Int type Index (a, b, c, d, e) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d, e) = Int type Index (a, b, c, d, e, f) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d, e, f) = Int type Index (a, b, c, d, e, f, g) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d, e, f, g) = Int type Index (a, b, c, d, e, f, g, h) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d, e, f, g, h) = Int type Index (a, b, c, d, e, f, g, h, i) Instance detailsDefined in Control.Lens.At type Index (a, b, c, d, e, f, g, h, i) = Int

type family IxValue m :: * #

This provides a common notion of a value at an index that is shared by both Ixed and At.

Instances
 Instance detailsDefined in Control.Lens.At Instance detailsDefined in Control.Lens.At type IxValue Text Instance detailsDefined in Control.Lens.At type IxValue Text = Char type IxValue Value Instance detailsDefined in Data.Aeson.Lens type IxValue Value = Value type IxValue Text Instance detailsDefined in Control.Lens.At type IxValue Text = Char type IxValue IntSet Instance detailsDefined in Control.Lens.At type IxValue IntSet = () type IxValue [a] Instance detailsDefined in Control.Lens.At type IxValue [a] = a type IxValue (Maybe a) Instance detailsDefined in Control.Lens.At type IxValue (Maybe a) = a type IxValue (Identity a) Instance detailsDefined in Control.Lens.At type IxValue (Identity a) = a type IxValue (NonEmpty a) Instance detailsDefined in Control.Lens.At type IxValue (NonEmpty a) = a type IxValue (IntMap a) Instance detailsDefined in Control.Lens.At type IxValue (IntMap a) = a type IxValue (Tree a) Instance detailsDefined in Control.Lens.At type IxValue (Tree a) = a type IxValue (Seq a) Instance detailsDefined in Control.Lens.At type IxValue (Seq a) = a type IxValue (Set k) Instance detailsDefined in Control.Lens.At type IxValue (Set k) = () type IxValue (Vector a) Instance detailsDefined in Control.Lens.At type IxValue (Vector a) = a type IxValue (Vector a) Instance detailsDefined in Control.Lens.At type IxValue (Vector a) = a type IxValue (Vector a) Instance detailsDefined in Control.Lens.At type IxValue (Vector a) = a type IxValue (HashSet k) Instance detailsDefined in Control.Lens.At type IxValue (HashSet k) = () type IxValue (Vector a) Instance detailsDefined in Control.Lens.At type IxValue (Vector a) = a type IxValue (e -> a) Instance detailsDefined in Control.Lens.At type IxValue (e -> a) = a type IxValue (a, a2) Instance detailsDefined in Control.Lens.At type IxValue (a, a2) = a type IxValue (HashMap k a) Instance detailsDefined in Control.Lens.At type IxValue (HashMap k a) = a type IxValue (Map k a) Instance detailsDefined in Control.Lens.At type IxValue (Map k a) = a type IxValue (UArray i e) Instance detailsDefined in Control.Lens.At type IxValue (UArray i e) = e type IxValue (Array i e) Instance detailsDefined in Control.Lens.At type IxValue (Array i e) = e type IxValue (InsOrdHashMap k v) Instance detailsDefined in Data.HashMap.Strict.InsOrd type IxValue (InsOrdHashMap k v) = v type IxValue (a, a2, a3) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3) = a type IxValue (a, a2, a3, a4) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4) = a type IxValue (a, a2, a3, a4, a5) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4, a5) = a type IxValue (a, a2, a3, a4, a5, a6) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6) = a type IxValue (a, a2, a3, a4, a5, a6, a7) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7) = a type IxValue (a, a2, a3, a4, a5, a6, a7, a8) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7, a8) = a type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) Instance detailsDefined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) = a

Contains

class Contains m where #

This class provides a simple Lens that lets you view (and modify) information about whether or not a container contains a given Index.

Minimal complete definition

contains

Methods

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

>>> 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
 Instance detailsDefined in Control.Lens.At Methods Ord a => Contains (Set a) Instance detailsDefined in Control.Lens.At Methodscontains :: Index (Set a) -> Lens' (Set a) Bool # (Eq a, Hashable a) => Contains (HashSet a) Instance detailsDefined in Control.Lens.At Methodscontains :: Index (HashSet a) -> Lens' (HashSet a) Bool #