Safe Haskell | None |
---|---|
Language | Haskell2010 |
Cardano.CLI.Vary.VEither
Synopsis
- data VEither (errs :: [Type]) a where
- toVary :: forall (errs :: [Type]) a. VEither errs a -> Vary (a ': errs)
- fromVary :: forall a (errs :: [Type]). Vary (a ': errs) -> VEither errs a
- fromLeft :: forall err (errs :: [Type]) a. err :| errs => err -> VEither errs a
- fromRight :: forall a (errs :: [Type]). a -> VEither errs a
- toEither :: forall (errs :: [Type]) a. VEither errs a -> Either (Vary errs) a
- fromEither :: forall (errs :: [Type]) a. Either (Vary errs) a -> VEither errs a
- veither :: forall (errs :: [Type]) c a. (Vary errs -> c) -> (a -> c) -> VEither errs a -> c
- intoOnly :: VEither ('[] :: [Type]) a -> a
- onLeft :: forall err b (errs :: [Type]) a. (err -> b) -> (VEither errs a -> b) -> VEither (err ': errs) a -> b
- onRight :: forall a b (errs :: [Type]). (a -> b) -> (VEither errs a -> b) -> VEither errs a -> b
- handle :: forall err (errs :: [Type]) a. (err -> VEither errs a) -> VEither (err ': errs) a -> VEither errs a
- mapLeftOn :: forall x y (xs :: [Type]) (ys :: [Type]) a. Mappable x y xs ys => (x -> y) -> VEither xs a -> VEither ys a
- mapLeft :: forall (xs :: [Type]) (ys :: [Type]) a. (Vary xs -> Vary ys) -> VEither xs a -> VEither ys a
- mapRight :: forall x y (errs :: [Type]). (x -> y) -> VEither errs x -> VEither errs y
- morph :: forall (ys :: [Type]) (xs :: [Type]) a. Subset (a ': xs) (a ': ys) => VEither xs a -> VEither ys a
- morphed :: forall (xs :: [Type]) (ys :: [Type]) a res. Subset (a ': xs) (a ': ys) => (VEither ys a -> res) -> VEither xs a -> res
General Usage
This module is intended to be used qualified:
>>>
import Cardano.CLI.Vary.VEither (VEither(VLeft, VRight))
>>>
import qualified Vary.VEither as VEither
And for many functions, it is useful or outright necessary to enable the following extensions:
>>>
:set -XDataKinds
Finally, some example snippets in this module make use of &
, the left-to-right function application operator.
>>>
import Data.Function ((&))
Core type definition
data VEither (errs :: [Type]) a where Source #
Bundled Patterns
pattern VLeft :: forall a errs. Vary errs -> VEither errs a | |
pattern VRight :: a -> VEither errs a | Matches when the VEither contains the preferred value of type |
Instances
Conversion
fromLeft :: forall err (errs :: [Type]) a. err :| errs => err -> VEither errs a Source #
Shorthand to construct a VEither
from a single error value.
Instead of:
>>>
(VLeft (Vary.from @Bool True)) :: VEither '[Bool] String
VLeft (Vary.from @Bool True)
You can just write:
>>>
VEither.fromLeft @Bool True :: VEither '[Bool] String
VLeft (Vary.from @Bool True)
veither :: forall (errs :: [Type]) c a. (Vary errs -> c) -> (a -> c) -> VEither errs a -> c Source #
case analysis ("pattern matching"):
Besides the VLeft
and VRight
patterns,
VEither
supports a bunch of handy combinator functions,
similar to Vary.on
and co.
onLeft :: forall err b (errs :: [Type]) a. (err -> b) -> (VEither errs a -> b) -> VEither (err ': errs) a -> b Source #
onRight :: forall a b (errs :: [Type]). (a -> b) -> (VEither errs a -> b) -> VEither errs a -> b Source #
handle :: forall err (errs :: [Type]) a. (err -> VEither errs a) -> VEither (err ': errs) a -> VEither errs a Source #
Handle a single error, by mapping it either to the success type a
or to one of the other errors in errs
.
This is syntactic sugar over using VEither.onLeft
,
but can be nicer to use if one or only a few error variants need to be handled,
because it lets you build a simple pipeline:
>>>
:{
examplePipe ve = ve & VEither.handle @Int (pure . show) & VEither.handle @Bool (pure . show) :}
>>>
:t examplePipe
examplePipe :: VEither (Int : Bool : errs) String -> VEither errs String>>>
examplePipe (VEither.fromLeft False :: VEither '[Int, Bool, Float] String)
VRight "False"
Transforming
mapLeftOn :: forall x y (xs :: [Type]) (ys :: [Type]) a. Mappable x y xs ys => (x -> y) -> VEither xs a -> VEither ys a Source #
mapLeft :: forall (xs :: [Type]) (ys :: [Type]) a. (Vary xs -> Vary ys) -> VEither xs a -> VEither ys a Source #
morph :: forall (ys :: [Type]) (xs :: [Type]) a. Subset (a ': xs) (a ': ys) => VEither xs a -> VEither ys a Source #
morphed :: forall (xs :: [Type]) (ys :: [Type]) a res. Subset (a ': xs) (a ': ys) => (VEither ys a -> res) -> VEither xs a -> res Source #
Execute a function expecting a larger (or differently-ordered) variant
with a smaller (or differently-ordered) variant,
by calling morph
on it before running the function.