HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletomland-1.3.3.3Haskell2010

Toml.Codec.BiMap

SPDX-License-Identifier : MPL-2.0 Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

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.

  • 3 types
  • 7 values
  • Packagetomland-1.3.3.3
  • Exports10
  • LanguageHaskell2010
  • LicenceMPL-2.0
  • SourceBiMap.hs

BiMap concept

4 declarations
datadata BiMap e a b
#

Partial bidirectional isomorphism. BiMap a b contains two function:

  1. a -> Either e b
  2. b -> Either e a

If you think of types as sets then this data type can be illustrated by the following picture:

bimap-type

BiMap also implements Category typeclass. And this instance can be described clearly by this illustration:

bimap-cat

Constructors

Instances1Category
valueiso :: (a -> b) -> (b -> a) -> BiMap e a b
#

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
valueprism
  1. :: (field -> object)

    Constructor

  2. -> (object -> Either error field)

    Match object to either error or field

  3. -> 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

TOML BiMap

0 declarations

Type

Error

datadata TomlBiMapError
#

Type of errors for TOML BiMap.

Constructors

Instances5Eq, Show, Generic, NFData, Rep

Smart constructors

Internals