Copyright | (c) 2018-2022 Kowainik |
---|---|
License | MPL-2.0 |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Stability | Stable |
Portability | Portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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.
Synopsis
- data BiMap e a b = BiMap {}
- invert :: BiMap e a b -> BiMap e b a
- iso :: (a -> b) -> (b -> a) -> BiMap e a b
- prism :: (field -> object) -> (object -> Either error field) -> BiMap error object field
- type TomlBiMap = BiMap TomlBiMapError
- data TomlBiMapError
- wrongConstructor :: Show a => Text -> a -> Either TomlBiMapError b
- prettyBiMapError :: TomlBiMapError -> Text
- mkAnyValueBiMap :: forall a (tag :: TValue). (forall (t :: TValue). Value t -> Either MatchError a) -> (a -> Value tag) -> TomlBiMap a AnyValue
- tShow :: Show a => a -> Text
BiMap
concept
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:
Since: 0.4.0
iso :: (a -> b) -> (b -> a) -> BiMap e a b Source #
Creates BiMap
from isomorphism. Can be used in the following way:
newtype Even = Even Integer newtype Odd = Odd Integer succEven :: Even -> Odd succEven (Even n) = Odd (n + 1) predOdd :: Odd -> Even predOdd (Odd n) = Even (n - 1) _EvenOdd ::BiMap
e Even Odd _EvenOdd =iso
succEven predOdd
Since: 0.4.0
:: (field -> object) | Constructor |
-> (object -> Either error field) | Match object to either error or field |
-> BiMap error object field |
Creates BiMap
from prism-like pair of functions. This combinator can be
used to create BiMap
for custom sum types like this:
data User = Admin Integer -- id of admin | Client Text -- name of the client deriving (Show) _Admin ::TomlBiMap
User Integer _Admin = Toml.prism
Admin $ \case Admin i -> Right i other -> Toml.wrongConstructor
"Admin" other _Client ::TomlBiMap
User Text _Client = Toml.prism
Client $ \case Client n -> Right n other -> Toml.wrongConstructor
"Client" other
Since: 0.4.0
TOML BiMap
Type
Error
data TomlBiMapError Source #
Type of errors for TOML BiMap
.
Since: 1.0.0
WrongConstructor | Error for cases with wrong constructors. For
example, you're trying to convert |
WrongValue | Error for cases with wrong values |
| |
ArbitraryError | Arbitrary textual error |
|
Instances
:: Show a | |
=> Text | Name of the expected constructor |
-> a | Actual value |
-> Either TomlBiMapError b |
Helper to construct WrongConstuctor error.
Since: 1.0.0
prettyBiMapError :: TomlBiMapError -> Text Source #
Converts TomlBiMapError
into pretty human-readable text.
Since: 1.0.0