references-0.1.0.0: Generalization of lenses, folds and traversals for haskell

Safe HaskellNone
LanguageHaskell98

Control.Reference.TH.Generate

Description

This module can be used to generate references for record fields. If the field surely exists, a Lens will be generated. If the field may not exist, it will be a LensPart.

If the name of the field starts with "_", the name of the field will be the same with "_" removed. If not, the reference name will be the field name with "_" added te the start.

The following code sample:

 data Maybe' a = Just' { _fromJust' :: a }

               | Nothing'

 $(makeReferences ''Maybe)



 data Tuple a b = Tuple { _fst' :: a, _snd' :: b }

 $(makeReferences ''Tuple)

Is equivalent to:

 data Maybe' a = Just' { _fromJust' :: a }

               | Nothing'

               

 fromJust' :: Monad w => LensPart' w (Maybe' a) (Maybe' b) a b

 fromJust' = polyPartial (\case Just' x -> Right (x, \y -> return (Just' y))

                                Nothing' -> Left (return Nothing'))



 data Tuple a b = Tuple { _fst' :: a, _snd' :: b }

 fst' :: Monad w => Lens' w (Tuple a c) (Tuple b c) a b

 fst' = lens _fst' (\b tup -> tup { _fst' = b })

 snd' :: Monad w => Lens' w (Tuple a c) (Tuple a d) c d

 snd' = lens _snd' (\b tup -> tup { _snd' = b })

Synopsis

Documentation

makeReferences :: Name -> Q [Dec] Source

Creates references for fields of a data structure.