definitive-base-2.3: The base modules of the Definitive framework.

Safe HaskellNone
LanguageHaskell2010

Algebra.Lens

Contents

Description

A module providing simple Lens functionality.

Lenses are a Haskell abstraction that allows you to access and modify part of a structure, compensating for and improving upon Haskell's horrendous record syntax and giving Haskell a first-class record system.

This module defines three kinds of Lenses : Lenses that allow you to access part of a structure; Traversals that allow you to modify part of a structure; and Isos which may be reversed. Lenses of any kind can be composed with (.), yielding a Lens of the most general kind, so that composing a Lens with a Traversal or Iso yields a Lens, and a Traversal with an Iso yields a Traversal.

Synopsis

The lens types

type Iso s t a b = forall p f. (Functor f, Bifunctor p) => p s (f t) -> p a (f b) Source

type Iso' a b = Simple Iso a b Source

type (:<->:) a b = Iso' a b Source

type LensLike f s t a b = (s -> f t) -> a -> f b Source

type Fold s t a b = forall f. (Semigroup (f b), Applicative f) => LensLike f s t a b Source

type Fold' a b = Simple Fold a b Source

type Getter s t a b = LensLike (Const s) s t a b Source

type Getter' a b = Simple Getter a b Source

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

type Lens' a b = Simple Lens a b Source

type Traversal s t a b = forall f. Applicative f => LensLike f s t a b Source

type Traversal' a b = Simple Traversal a b Source

Constructing lenses

iso :: (a -> s) -> (t -> b) -> Iso s t a b Source

Create an Iso from two inverse functions.

from :: Iso s t a b -> Iso b a t s Source

Reverse an Iso

from :: Iso' a b -> Iso' b a

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

Create a Lens from a getter and setter function.

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

getter :: (a -> b) -> Traversal' a b Source

prism :: (a -> b :+: s) -> (a -> t -> b) -> Traversal s t a b Source

Create a Traversal from a maybe getter and setter function.

prism :: (a -> (a:+:b)) -> (a -> b -> a) -> Traversal' a b

sat :: (a -> Bool) -> Traversal' a a Source

simple :: LensLike f a b a b -> LensLike f a b a b Source

(.+) :: Fold s t a b -> Fold s t a b -> Fold s t a b infixr 8 Source

forl :: LensLike f a b c d -> c -> (a -> f b) -> f d Source

forl_ :: Functor f => LensLike f a a c c -> c -> (a -> f ()) -> f () Source

Extracting values

(^.) :: a -> Getter b b a a -> b infixl 8 Source

Retrieve a value from a structure using a Lens (or Iso)

(^..) :: a -> Iso a a b b -> b infixl 8 Source

(^?) :: (Unit f, Monoid (f b)) => a -> Fold' a b -> f b infixl 8 Source

has :: Fold' a b -> a -> Bool Source

(^??) :: a -> ((b -> Const [b] b) -> a -> Const [b] a) -> [b] infixl 8 Source

(%~) :: LensLike Id s t a b -> (s -> t) -> a -> b infixl 8 Source

(%-) :: LensLike Id s t a b -> t -> a -> b infixl 8 Source

(%%~) :: Iso s t a b -> (b -> a) -> t -> s infixl 8 Source

(%%-) :: Iso s t a b -> a -> t -> s infixl 8 Source

by :: Getter b u a v -> a -> b Source

yb :: Iso s t a b -> t -> b Source

warp :: LensLike Id s t a b -> (s -> t) -> a -> b Source

set :: LensLike Id s t a b -> t -> a -> b Source

(-.) :: Getter c u b v -> (a -> b) -> a -> c infixr 9 Source

(.-) :: (b -> c) -> Iso a a b b -> a -> c infixr 9 Source

Basic lenses

class Lens1 s t a b | a -> s, a t -> b where Source

Methods

l'1 :: Lens s t a b Source

Instances

Lens1 a a [a] [a] 
Lens1 a b ((:*:) a c) ((:*:) b c) 
Lens1 a b (a, c, d) (b, c, d) 
Lens1 a b (a, c, d, e) (b, c, d, e) 
Lens1 a b (a, c, d, e, f) (b, c, d, e, f) 

class Lens2 s t a b | a -> s, a t -> b where Source

Methods

l'2 :: Lens s t a b Source

Instances

Lens2 a b ((:*:) c a) ((:*:) c b) 
Lens2 a b (c, a, d) (c, b, d) 
Lens2 a b (c, a, d, e) (c, b, d, e) 
Lens2 a b (c, a, d, e, f) (c, b, d, e, f) 

class Lens3 s t a b | a -> s, a t -> b where Source

Methods

l'3 :: Lens s t a b Source

Instances

Lens3 a b (c, d, a) (c, d, b) 
Lens3 a b (c, d, a, e) (c, d, b, e) 
Lens3 a b (c, d, a, e, f) (c, d, b, e, f) 

class Lens4 s t a b | a -> s, a t -> b where Source

Methods

l'4 :: Lens s t a b Source

Instances

Lens4 a b (c, d, e, a) (c, d, e, b) 
Lens4 a b (c, d, e, a, f) (c, d, e, b, f) 

class Lens5 s t a b | a -> s, a t -> b where Source

Methods

l'5 :: Lens s t a b Source

Instances

Lens5 a b (c, d, e, f, a) (c, d, e, f, b) 

class Trav1 s t a b | a -> s, a t -> b where Source

Methods

t'l :: Traversal s t a b Source

Instances

Trav1 a b [a] [b] 
Trav1 a b ((:+:) a c) ((:+:) b c) 

class Trav2 s t a b | a -> s, a t -> b where Source

Methods

t'r :: Traversal s t a b Source

Instances

Trav2 a b (Maybe a) (Maybe b) 
Trav2 a b ((:+:) c a) ((:+:) c b) 

class Compound a b s t | s -> a, b s -> t where Source

Methods

each :: Traversal a b s t Source

Instances

Compound a b [a] [b] 
Compound a b ((:+:) a a) ((:+:) b b) 
Compound a b (a, a) (b, b) 
Compound a b (a, a, a) (b, b, b) 

i'list :: [a] :<->: (() :+: (a :*: [a])) Source

i'pair :: Iso s t a b -> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b') Source

Isomorphisms

class Isomorphic b a t s | t -> b, t a -> s where Source

Methods

i'_ :: Iso s t a b Source

Instances

Isomorphic Bool Bool (Maybe a) (Maybe Void) 
Isomorphic a b (Max a) (Max b) 
Isomorphic a b (Product a) (Product b) 
Isomorphic a b (Dual a) (Dual b) 
Isomorphic a b (Id a) (Id b) 
Isomorphic a b (Void, a) (Void, b) 
Isomorphic a b (Const a c) (Const b c) 
Isomorphic [a] [b] (OrdList a) (OrdList b) 
Isomorphic (f (g a)) (f' (g' b)) ((:.:) f g a) ((:.:) f' g' b) 
Isomorphic (k a a) (k b b) (Endo k a) (Endo k b) 
Isomorphic (a -> m b) (c -> m' d) (Kleisli m a b) (Kleisli m' c d) 
Isomorphic (f a b) (f c d) (Flip f b a) (Flip f d c) 

Miscellaneous

thunk :: Iso a b (IO a) (IO b) Source

curried :: Iso (a -> b -> c) (a' -> b' -> c') ((a, b) -> c) ((a', b') -> c') Source

Type wrappers

i'Id :: Iso (Id a) (Id b) a b Source

i'OrdList :: Iso (OrdList a) (OrdList b) [a] [b] Source

i'Const :: Iso (Const a c) (Const b c) a b Source

i'Dual :: Iso (Dual a) (Dual b) a b Source

i'Endo :: Iso (Endo k a) (Endo k b) (k a a) (k b b) Source

i'Flip :: Iso (Flip f b a) (Flip f d c) (f a b) (f c d) Source

i'Max :: Iso (Max a) (Max b) a b Source

i'Compose :: Iso ((f :.: g) a) ((f' :.: g') b) (f (g a)) (f' (g' b)) Source

i'Backwards :: Iso (Backwards f a) (Backwards g b) (f a) (g b) Source

i'Accum :: Iso (Accum a) (Accum b) (Maybe a) (Maybe b) Source

Algebraic isomorphisms

commuted :: Commutative f => Iso (f a b) (f c d) (f b a) (f d c) Source

adding :: (Num n, Semigroup n) => n -> Iso' n n Source

Higher-order isomorphisms

warp2 :: Iso s t a b -> (s -> s -> t) -> a -> a -> b Source

mapping :: (Functor f, Functor f') => Iso s t a b -> Iso (f s) (f' t) (f a) (f' b) Source

mapping' :: Functor f => Iso s t a b -> Iso (f s) (f t) (f a) (f b) Source

promapping :: Bifunctor f => Iso s t a b -> Iso (f t x) (f s y) (f b x) (f a y) Source

promapping :: Bifunctor f => Iso' a b -> Iso' (f a c) (f b c)

applying :: Applicative f => Lens s t a b -> Lens (f s) (f t) (f a) (f b) Source

class IsoFunctor f where Source

Methods

mapIso :: Iso s t a b -> Iso (f s) (f t) (f a) (f b) Source

Instances

IsoFunctor ((->) a) 

(<.>) :: IsoFunctor2 f => (a :<->: c) -> (b :<->: d) -> f a b :<->: f c d infixr 9 Source

An infix synonym for mapIso2

class IsoFunctor2 f where Source

Methods

mapIso2 :: (a :<->: c) -> (b :<->: d) -> f a b :<->: f c d Source