Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data AbstractLens r w c a = forall x.c x => AbstractLens (GLens r w a x)
- abstractId :: (Monad r, c a) => AbstractLens r w c a
- afterGLens :: Monad r => AbstractLens r w c a -> GLens r w b a -> AbstractLens r w c b
- get :: AbstractLens r w c a -> (forall x. c x => (a -> r x) -> b) -> b
- set :: Monad w => AbstractLens r w c a -> (forall x. c x => x) -> a -> w a
- modify :: AbstractLens r w c a -> (forall x. c x => x -> w x) -> a -> w a
- getM :: AbstractLens r w c a -> (forall x. c x => (a -> r x) -> m b) -> m b
- setM :: (Monad w, Functor m) => AbstractLens r w c a -> (forall x. c x => m x) -> m (a -> w a)
- modifyM :: Functor m => AbstractLens r w c a -> (forall x. c x => m (x -> w x)) -> m (a -> w a)
- type Path = [String]
- class CLens r w c a
- lens :: CLens r w c a => LensOptions -> Path -> Either String (AbstractLens r w c a)
- emptyPathOnly :: (Monad r, c a) => LensOptions -> Path -> Either String (AbstractLens r w c a)
- data LensOptions = LensOptions {
- lensOptionsMatch :: DatatypeName -> FieldName -> String -> Bool
- defaultLensOptions :: LensOptions
Abstract lenses
data AbstractLens r w c a Source #
An abstract lens qualifies existentially over the target type of the lens
Sadly, abstract lenses do not form a category, so we provide special identity and composition functions.
forall x.c x => AbstractLens (GLens r w a x) |
abstractId :: (Monad r, c a) => AbstractLens r w c a Source #
Identity abstract lens
:: Monad r | |
=> AbstractLens r w c a | a -> x |
-> GLens r w b a | b -> a |
-> AbstractLens r w c b | b -> x |
Compose with a pointwise lens on the right
Getters and setters
get :: AbstractLens r w c a -> (forall x. c x => (a -> r x) -> b) -> b Source #
Getter for computed lenses
get l == runIdentity . getM l . Identity
set :: Monad w => AbstractLens r w c a -> (forall x. c x => x) -> a -> w a Source #
Setter for computed lenses
set l == runIdentity . setM l . Identity
modify :: AbstractLens r w c a -> (forall x. c x => x -> w x) -> a -> w a Source #
Modifier for computed lenses
getM :: AbstractLens r w c a -> (forall x. c x => (a -> r x) -> m b) -> m b Source #
Getter with possibility for "compile time" failure
setM :: (Monad w, Functor m) => AbstractLens r w c a -> (forall x. c x => m x) -> m (a -> w a) Source #
Setter with possibility for "compile time" failure
modifyM :: Functor m => AbstractLens r w c a -> (forall x. c x => m (x -> w x)) -> m (a -> w a) Source #
Modifier with possibility for "compile time" failure
Computing lenses
A path is a series of field names. For instance, given
data T1 = T1 { a :: Int, b :: Int } deriving Generic data T2 = T2 { c :: T1, d :: Int } deriving Generic
valid paths on T2 are
[] ["c"] ["d"] ["c", "a"] ["c", "b"]
Compute a lens for a given type and path
The Either
is used to indicate "compile time" failure of the computation
of the lens (for instance, when this path is invalid for this data type).
Some lenses may of course be themselves effectful, depending on the category. However, the lenses returned by the generic computation are pure and total (as is evident from the type of glens).
lens :: CLens r w c a => LensOptions -> Path -> Either String (AbstractLens r w c a) Source #
Manually constructing lenses
emptyPathOnly :: (Monad r, c a) => LensOptions -> Path -> Either String (AbstractLens r w c a) Source #
A lens for abstract types (supports empty paths only)
Useful for defining CLens instances for types such as Int, Bool, Text, etc.
instance CLens c Int where lens = emptyPathOnly
Configuration
data LensOptions Source #
LensOptions | |
|
defaultLensOptions :: LensOptions Source #
Default match just compares field names