Modulelens-5.3.5Haskell2010
Control.Lens.Iso
- 5 types
- 2 classes
- 33 values
- Packagelens-5.3.5
- Exports45
- LanguageHaskell2010
- LicenceBSD-2-Clause
- SourceType.hs
Isomorphism Lenses
4 declarationsWhen you see this as an argument to a function, it expects an Iso.
Isomorphism Construction
1 declarationConsuming Isomorphisms
3 declarationsConvert from AnIso back to any Iso.
This is useful when you need to store an isomorphism as a data type inside a container and later reconstitute it as an overloaded function.
See cloneLens or cloneTraversal for more information on why you might want to do this.
Extract the two functions, one from s -> a and
one from b -> t that characterize an Iso.
Working with isomorphisms
6 declarationsBased on ala from Conor McBride's work on Epigram.
This version is generalized to accept any Iso, not just a newtype.
au (_Wrapping Sum) foldMap [1,2,3,4]10
You may want to think of this combinator as having the following, simpler type:
au :: AnIso s t a b -> ((b -> t) -> e -> s) -> e -> a
au = xplat . from
Based on ala' from Conor McBride's work on Epigram.
This version is generalized to accept any Iso, not just a newtype.
For a version you pass the name of the newtype constructor to, see alaf.
auf (_Wrapping Sum) (foldMapOf both) Prelude.length ("hello","world")10
Mnemonically, the German auf plays a similar role to à la, and the combinator is au with an extra function argument:
auf :: Iso s t a b -> ((r -> t) -> e -> s) -> (r -> b) -> e -> a
but the signature is general.
Note: The direction of the Iso required for this function changed in lens 4.18 to match up
with the behavior of au. For the old behavior use xplatf or for a version that is compatible
across both old and new versions of lens you can just use coerce!
Common Isomorphisms
If v is an element of a type a, and a' is a sans the element v, then non v is an isomorphism from
Maybe a' to a.
non ≡ non' . only
Keep in mind this is only a real isomorphism if you treat the domain as being Maybe (a sans v).
This is practically quite useful when you want to have a Data.Map.Map where all the entries should have non-zero values.
Map.fromList [("hello",1)] & at "hello" . non 0 +~ 2fromList [("hello",3)]
Map.fromList [("hello",1)] & at "hello" . non 0 -~ 1fromList []
Map.fromList [("hello",1)] ^. at "hello" . non 01
Map.fromList [] ^. at "hello" . non 00
This combinator is also particularly useful when working with nested maps.
e.g. When you want to create the nested Data.Map.Map when it is missing:
Map.empty & at "hello" . non Map.empty . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]
and when have deleting the last entry from the nested Data.Map.Map mean that we
should delete its entry from the surrounding one:
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . non Map.empty . at "world" .~ NothingfromList []
It can also be used in reverse to exclude a given value:
non 0 # rem 10 4Just 2
non 0 # rem 10 5Nothing
non' p generalizes non (p # ()) to take any unit Prism
This function generates an isomorphism between Maybe (a | isn't p a) and a.
Map.singleton "hello" Map.empty & at "hello" . non' _Empty . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . non' _Empty . at "world" .~ NothingfromList []
anon a p generalizes non a to take any value and a predicate.
This function assumes that p a holds True and generates an isomorphism between Maybe (a | not (p a)) and a.
Map.empty & at "hello" . anon Map.empty Map.null . at "world" ?~ "!!!"fromList [("hello",fromList [("world","!!!")])]
Map.fromList [("hello",Map.fromList [("world","!!!")])] & at "hello" . anon Map.empty Map.null . at "world" .~ NothingfromList []
This isomorphism can be used to convert to or from an instance of Enum.
LT^.from enum0
97^.enum :: Char'a'
Note: this is only an isomorphism from the numeric range actually used
and it is a bit of a pleasant fiction, since there are questionable
Enum instances for Double, and Float that exist solely for
[1.0 .. 4.0] sugar and the instances for those and Integer don't
cover all values in their range.
The isomorphism for flipping a function.
((,)^.flipped) 1 2(2,1)
This class provides a generalized notion of list reversal extended to other containers.
Methods
reversing :: t -> t
Instances13Reversing, …
Reversing ByteStringDefined in lens-5.3.5 · Control.Lens.Internal.IsoReversing ByteStringDefined in lens-5.3.5 · Control.Lens.Internal.IsoReversing TextDefined in lens-5.3.5 · Control.Lens.Internal.IsoReversing TextDefined in lens-5.3.5 · Control.Lens.Internal.IsoStorable a => Reversing (Vector a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoReversing (Seq a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoReversing (NonEmpty a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoReversing (Deque a)Defined in lens-5.3.5 · Control.Lens.Internal.DequeReversing (Vector a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoReversing (Vector a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoReversing [a]Defined in lens-5.3.5 · Control.Lens.Internal.IsoPrim a => Reversing (Vector a)Defined in lens-5.3.5 · Control.Lens.Internal.IsoUnbox a => Reversing (Vector a)Defined in lens-5.3.5 · Control.Lens.Internal.Iso
An Iso between a list, ByteString, Text fragment, etc. and its reversal.
"live" ^. reversed"evil"
"live" & reversed %~ ('d':)"lived"
Uncommon Isomorphisms
This isomorphism can be used to inspect an IndexedTraversal to see how it associates the structure and it can also be used to bake the IndexedTraversal into a Magma so that you can traverse over it multiple times with access to the original indices.
This provides a way to peek at the internal structure of a
Control.Lens.Traversal.Traversal or Control.Lens.Traversal.IndexedTraversal
Instances7FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Functor, Foldable, Traversable, …
FoldableWithIndex i (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.MagmaFunctorWithIndex i (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.MagmaTraversableWithIndex i (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.MagmaFunctor (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.MagmaFoldable (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.MagmaTraversable (Magma i t b)Defined in lens-5.3.5 · Control.Lens.Internal.Magma(Show i, Show a) => Show (Magma i t b a)Defined in lens-5.3.5 · Control.Lens.Internal.Magma
Contravariant functors
Lift an Iso into a Contravariant functor.
contramapping :: Contravariant f => Iso s t a b -> Iso (f a) (f b) (f s) (f t)
contramapping :: Contravariant f => Iso' s a -> Iso' (f a) (f s)
Profunctors
4 declarationsFormally, the class Profunctor represents a profunctor
from Hask -> Hask.
Intuitively it is a bifunctor where the first argument is contravariant and the second argument is covariant.
You can define a Profunctor by either defining dimap or by defining both lmap and rmap.
If you supply dimap, you should ensure that:
dimap id id ≡ idIf you supply lmap and rmap, ensure:
lmap id ≡ id
rmap id ≡ id
If you supply both, you should also ensure:
dimap f g ≡ lmap f . rmap gThese ensure by parametricity:
dimap (f . g) (h . i) ≡ dimap g h . dimap f i
lmap (f . g) ≡ lmap g . lmap f
rmap (f . g) ≡ rmap f . rmap g
Instances46Profunctor, …
Profunctor ReifiedFoldDefined in lens-5.3.5 · Control.Lens.ReifiedProfunctor ReifiedGetterDefined in lens-5.3.5 · Control.Lens.ReifiedMonad m => Profunctor (Kleisli m)Defined in profunctors-5.6.3 · Data.Profunctor.UnsafeProfunctor TaggedDefined in profunctors-5.6.3 · Data.Profunctor.UnsafeProfunctor (Indexed i)Defined in lens-5.3.5 · Control.Lens.Internal.IndexedProfunctor (ReifiedIndexedFold i)Defined in lens-5.3.5 · Control.Lens.ReifiedProfunctor (ReifiedIndexedGetter i)Defined in lens-5.3.5 · Control.Lens.ReifiedProfunctor (CopastroSum p)Defined in profunctors-5.6.3 · Data.Profunctor.ChoiceProfunctor (CotambaraSum p)Defined in profunctors-5.6.3 · Data.Profunctor.ChoiceProfunctor (PastroSum p)Defined in profunctors-5.6.3 · Data.Profunctor.ChoiceProfunctor (Environment p)Defined in profunctors-5.6.3 · Data.Profunctor.ClosedProfunctor (FreeMapping p)Defined in profunctors-5.6.3 · Data.Profunctor.MappingProfunctor (Copastro p)Defined in profunctors-5.6.3 · Data.Profunctor.StrongProfunctor (Cotambara p)Defined in profunctors-5.6.3 · Data.Profunctor.StrongProfunctor (Pastro p)Defined in profunctors-5.6.3 · Data.Profunctor.StrongProfunctor (Baz t)Defined in profunctors-5.6.3 · Data.Profunctor.TraversingProfunctor (Bazaar a)Defined in profunctors-5.6.3 · Data.Profunctor.TraversingProfunctor (FreeTraversing p)Defined in profunctors-5.6.3 · Data.Profunctor.TraversingProfunctor (Coyoneda p)Defined in profunctors-5.6.3 · Data.Profunctor.YonedaProfunctor (Yoneda p)Defined in profunctors-5.6.3 · Data.Profunctor.YonedaProfunctor p => Profunctor (TambaraSum p)Defined in profunctors-5.6.3 · Data.Profunctor.ChoiceProfunctor p => Profunctor (Closure p)Defined in profunctors-5.6.3 · Data.Profunctor.ClosedProfunctor p => Profunctor (CofreeMapping p)Defined in profunctors-5.6.3 · Data.Profunctor.MappingProfunctor p => Profunctor (Tambara p)Defined in profunctors-5.6.3 · Data.Profunctor.StrongProfunctor p => Profunctor (CofreeTraversing p)Defined in profunctors-5.6.3 · Data.Profunctor.TraversingFunctor f => Profunctor (Costar f)Defined in profunctors-5.6.3 · Data.Profunctor.TypesFunctor f => Profunctor (Star f)Defined in profunctors-5.6.3 · Data.Profunctor.TypesFunctor w => Profunctor (Cokleisli w)Defined in profunctors-5.6.3 · Data.Profunctor.UnsafeProfunctor (->)Defined in profunctors-5.6.3 · Data.Profunctor.UnsafeProfunctor (Exchange a b)Defined in lens-5.3.5 · Control.Lens.Internal.IsoProfunctor (Market a b)Defined in lens-5.3.5 · Control.Lens.Internal.PrismProfunctor (Forget r)Defined in profunctors-5.6.3 · Data.Profunctor.Types(Functor f, Profunctor p) => Profunctor (WrappedPafb f p)Defined in lens-5.3.5 · Control.Lens.Internal.ProfunctorContravariant f => Profunctor (Clown f)Defined in profunctors-5.6.3 · Data.Profunctor.UnsafeFunctor f => Profunctor (Joker f)Defined in profunctors-5.6.3 · Data.Profunctor.UnsafeArrow p => Profunctor (WrappedArrow p)Defined in profunctors-5.6.3 · Data.Profunctor.TypesProfunctor p => Profunctor (WrappedProfunctor p)Defined in invariant-0.6.4 · Data.Functor.InvariantProfunctor p => Profunctor (Codensity p)Defined in profunctors-5.6.3 · Data.Profunctor.Ran(Profunctor p, Profunctor q) => Profunctor (Product p q)Defined in profunctors-5.6.3 · Data.Profunctor.Unsafe(Profunctor p, Profunctor q) => Profunctor (Sum p q)Defined in profunctors-5.6.3 · Data.Profunctor.Unsafe(Functor f, Profunctor p) => Profunctor (Tannen f p)Defined in profunctors-5.6.3 · Data.Profunctor.Unsafe(Functor f, Profunctor p) => Profunctor (Cayley f p)Defined in profunctors-5.6.3 · Data.Profunctor.Cayley(Profunctor p, Profunctor q) => Profunctor (Procompose p q)Defined in profunctors-5.6.3 · Data.Profunctor.Composition(Profunctor p, Profunctor q) => Profunctor (Rift p q)Defined in profunctors-5.6.3 · Data.Profunctor.Composition(Profunctor p, Profunctor q) => Profunctor (Ran p q)Defined in profunctors-5.6.3 · Data.Profunctor.Ran(Profunctor p, Functor f, Functor g) => Profunctor (Biff p f g)Defined in profunctors-5.6.3 · Data.Profunctor.Unsafe
Lift two Isos into both arguments of a Profunctor simultaneously.
dimapping :: Profunctor p => Iso s t a b -> Iso s' t' a' b' -> Iso (p a s') (p b t') (p s a') (p t b')
dimapping :: Profunctor p => Iso' s a -> Iso' s' a' -> Iso' (p a s') (p s a')
Lift an Iso contravariantly into the left argument of a Profunctor.
lmapping :: Profunctor p => Iso s t a b -> Iso (p a x) (p b y) (p s x) (p t y)
lmapping :: Profunctor p => Iso' s a -> Iso' (p a x) (p s x)
Lift an Iso covariantly into the right argument of a Profunctor.
rmapping :: Profunctor p => Iso s t a b -> Iso (p x s) (p y t) (p x a) (p y b)
rmapping :: Profunctor p => Iso' s a -> Iso' (p x s) (p x a)
Bifunctors
3 declarationsLift an Iso into the second argument of a Bifunctor. This is essentially the same as mapping, but it takes a 'Bifunctor p' constraint instead of a 'Functor (p a)' one.
seconding :: Bifunctor p => Iso s t a b -> Iso (p x s) (p y t) (p x a) (p y b)
seconding :: Bifunctor p => Iso' s a -> Iso' (p x s) (p x a)
Coercions
1 declarationData types that are representationally equal are isomorphic.
This is only available on GHC 7.8+