Type synonym for a type-modifying iso.
Moduleoptics-core-0.4.2Haskell2010
Optics.Iso
An Isomorphism expresses the fact that two types have the same structure, and hence can be converted from one to the other in either direction.
Contents
- Packageoptics-core-0.4.2
- Exports20
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceIso.hs
Formation
Type synonym for a type-preserving iso.
Introduction
Build an iso from a pair of inverse functions.
If you want to build an Iso from the van Laarhoven representation, use
isoVL from the optics-vl package.
Elimination
An Iso is in particular a Getter, a Review and a Setter, therefore you can specialise types to obtain:
view :: Iso' s a -> s -> a
review :: Iso' s a -> a -> s
over :: Iso s t a b -> (a -> b) -> s -> t
set :: Iso s t a b -> b -> s -> t
If you want to view a type-modifying Iso that is insufficiently polymorphic to be used as a type-preserving Iso', use getting:
view . getting :: Iso s t a b -> s -> a
Computation
Well-formedness
Additional introduction forms
Capture type constraints as an isomorphism.
Note: This is the identity optic:
:t view equalityview equality :: a -> a
Proof of reflexivity.
Data types that are representationally equal are isomorphic.
view coerced 'x' :: Identity CharIdentity 'x'
Type-preserving version of coerced with type parameters rearranged for TypeApplications.
newtype MkInt = MkInt Int deriving Showover (coercedTo @Int) (*3) (MkInt 2)MkInt 6
Special case of coerced for trivial newtype wrappers.
over (coerced1 @Identity) (++ "bar") (Identity "foo")Identity "foobar"
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 %~ (+2)fromList [("hello",3)]
Map.fromList [("hello",1)] & at "hello" % non 0 %~ (subtract 1)fromList []
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.
anon a ≡ non' . nearly a
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 []
The isomorphism for flipping a function.
(view flipped (,)) 1 2(2,1)
This class provides for symmetric bifunctors.
Additional elimination forms
Extract the two components of an isomorphism.
Based on ala from Conor McBride's work on Epigram.
This version is generalized to accept any Iso, not just a newtype.
au (coerced1 @Sum) foldMap [1,2,3,4]10
You may want to think of this combinator as having the following, simpler type:
au :: Iso s t a b -> ((b -> t) -> e -> s) -> e -> a
Combinators
Subtyping
Tag for an iso.
Instances41ReversibleOptic, Is, ArrowOptic, JoinKinds, ToReadOnly, MappingOptic, …
ReversibleOptic An_IsoDefined in optics-core-0.4.2 · Optics.ReIs An_Iso A_FoldDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_GetterDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_LensDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_PrismDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_ReversedLensDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_ReversedPrismDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_ReviewDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_SetterDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_TraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso An_AffineFoldDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso An_AffineTraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingArrow arr => ArrowOptic An_Iso arrDefined in optics-core-0.4.2 · Optics.Arrowk ~ A_Fold => JoinKinds A_Fold An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_Iso A_Fold kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Getter => JoinKinds A_Getter An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Getter => JoinKinds An_Iso A_Getter kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Lens => JoinKinds A_Lens An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Lens => JoinKinds An_Iso A_Lens kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Prism => JoinKinds A_Prism An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Prism => JoinKinds An_Iso A_Prism kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_ReversedLens => JoinKinds A_ReversedLens An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_ReversedLens => JoinKinds An_Iso A_ReversedLens kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_ReversedPrism => JoinKinds A_ReversedPrism An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_ReversedPrism => JoinKinds An_Iso A_ReversedPrism kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Review => JoinKinds A_Review An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Review => JoinKinds An_Iso A_Review kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds A_Setter An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds An_Iso A_Setter kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds An_Iso A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ An_AffineFold => JoinKinds An_AffineFold An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ An_AffineFold => JoinKinds An_Iso An_AffineFold kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ An_AffineTraversal => JoinKinds An_AffineTraversal An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ An_AffineTraversal => JoinKinds An_Iso An_AffineTraversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ An_Iso => JoinKinds An_Iso An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingToReadOnly An_Iso s t a bDefined in optics-core-0.4.2 · Optics.ReadOnly(Functor f, Functor g) => MappingOptic An_Iso f g s t a bDefined in optics-core-0.4.2 · Optics.Mappingtype MappedOptic An_Iso = An_IsoDefined in optics-core-0.4.2 · Optics.Mappingtype ReversedOptic An_Iso = An_IsoDefined in optics-core-0.4.2 · Optics.Retype ReadOnlyOptic An_Iso = A_GetterDefined in optics-core-0.4.2 · Optics.ReadOnly