bimap -package:bimap -package:pgp-wordlist
base Data.Bifunctor,
lens Control.Lens.Combinators Control.Lens.Review,
semigroupoids Data.Bifunctor.Apply,
streaming Streaming,
diagrams-lib Diagrams.Prelude,
rio RIO.Prelude,
base-prelude BasePrelude,
basement Basement.Compat.Bifunctor,
foundation Foundation,
rebase Rebase.Prelude,
verset Verset Map over both arguments at the same time.
bimap f g ≡ first f . second g
Examples
>>> bimap toUpper (+1) ('j', 3)
('J',4)
>>> bimap toUpper (+1) (Left 'j')
Left 'J'
>>> bimap toUpper (+1) (Right 3)
Right 4
Transform a graph by applying given functions to the vertices of each
part. Complexity:
O((n + m) * log(n)) time.
bimap f g empty == empty
bimap f g . vertex == vertex . Data.Bifunctor.bimap f g
bimap f g (edge x y) == edge (f x) (g y)
bimap id id == id
bimap f1 g1 . bimap f2 g2 == bimap (f1 . f2) (g1 . g2)
Map over both sides of a symbolic
Either at the same time
>>> let f = uninterpret "f" :: SInteger -> SInteger
>>> let g = uninterpret "g" :: SInteger -> SInteger
>>> prove $ \x -> fromLeft (bimap f g (sLeft x)) .== f x
Q.E.D.
>>> prove $ \x -> fromRight (bimap f g (sRight x)) .== g x
Q.E.D.
Bidirectional map. Essentially, a bijection between subsets of its two
argument types.
For one value of the left-hand type this map contains one value of the
right-hand type and vice versa.
Type-level
bimap.
Example
>>> data Example where Ex :: a -> Example -- Hide the type of examples to avoid brittleness in different GHC versions
>>> :kind! Ex (Eval (Bimap ((+) 1) (Flip (-) 1) '(2, 4)) :: (Natural, Natural))
Ex (Eval (Bimap ((+) 1) (Flip (-) 1) '(2, 4)) :: (Natural, Natural)) :: Example
= Ex '(3, 3)
Implementation of
Tagged Partial Bidirectional Isomorphism.
This module contains the
BiMap type that represents conversion
between two types with the possibility of failure.
See
Toml.Codec.BiMap.Conversion for examples of
BiMap
with specific types. The
BiMap concept is general and is not
specific to TOML, but in this package most usages of
BiMap are
between TOML values and Haskell values.
Partial bidirectional isomorphism.
BiMap a b contains two
function:
a -> Either e b
b -> Either e a
If you think of types as sets then this data type can be illustrated
by the following picture:
BiMap also implements
Category typeclass. And this
instance can be described clearly by this illustration:
Partly invertible finite maps.
Time complexities are given under the assumption that all relevant
instance functions, as well as arguments of function type, take
constant time, and "n" is the number of keys involved in the
operation.
Finite maps from
k to
v, with a way to quickly get
from
v to
k for certain values of type
v
(those for which
tag is defined).
Every value of this type must satisfy
biMapInvariant.
Bijection between finite sets.
Both data types are strict here.
The
bimapAccumL function behaves like a combination of
bimap and
bifoldl; it traverses a structure from left to
right, threading a state of type
a and using the given
actions to compute new elements for the structure.
Examples
Basic usage:
>>> bimapAccumL (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")
(8,("True","oof"))
The
bimapAccumR function behaves like a combination of
bimap and
bifoldr; it traverses a structure from right
to left, threading a state of type
a and using the given
actions to compute new elements for the structure.
Examples
Basic usage:
>>> bimapAccumR (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")
(7,("True","oof"))