lens-4.19.1: Lenses, Folds and Traversals

Control.Lens.Unsound

Description

One commonly asked question is: can we combine two lenses, Lens' a b and Lens' a c into Lens' a (b, c). This is fair thing to ask, but such operation is unsound in general. See lensProduct.

Synopsis

# Documentation

lensProduct :: ALens' s a -> ALens' s b -> Lens' s (a, b) Source #

A lens product. There is no law-abiding way to do this in general. Result is only a valid ReifiedLens if the input lenses project disjoint parts of the structure s. Otherwise "you get what you put in" law

view l (set l v s) ≡ v


is violated by

>>> let badLens :: Lens' (Int, Char) (Int, Int); badLens = lensProduct _1 _1
>>> view badLens (set badLens (1,2) (3,'x'))
(2,2)


but we should get (1,2).

Are you looking for alongside?

prismSum :: APrism s t a b -> APrism s t c d -> Prism s t (Either a c) (Either b d) Source #

A dual of lensProduct: a prism sum.

The law

preview l (review l b) ≡ Just b


breaks with

>>> let badPrism :: Prism' (Maybe Char) (Either Char Char); badPrism = prismSum _Just _Just
>>> preview badPrism (review badPrism (Right 'x'))
Just (Left 'x')


We put in Right value, but get back Left.

Are you looking for without?

adjoin :: Traversal' s a -> Traversal' s a -> Traversal' s a Source #

A generalization of mappending folds: A union of disjoint traversals.

Traversing the same entry twice is illegal.

Are you looking for failing?