cardano-cli
Safe HaskellNone
LanguageHaskell2010

Cardano.CLI.Vary.VEither

Synopsis

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

Instances

Instances details
Foldable (VEither errs) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

fold :: Monoid m => VEither errs m -> m Source #

foldMap :: Monoid m => (a -> m) -> VEither errs a -> m Source #

foldMap' :: Monoid m => (a -> m) -> VEither errs a -> m Source #

foldr :: (a -> b -> b) -> b -> VEither errs a -> b Source #

foldr' :: (a -> b -> b) -> b -> VEither errs a -> b Source #

foldl :: (b -> a -> b) -> b -> VEither errs a -> b Source #

foldl' :: (b -> a -> b) -> b -> VEither errs a -> b Source #

foldr1 :: (a -> a -> a) -> VEither errs a -> a Source #

foldl1 :: (a -> a -> a) -> VEither errs a -> a Source #

toList :: VEither errs a -> [a] Source #

null :: VEither errs a -> Bool Source #

length :: VEither errs a -> Int Source #

elem :: Eq a => a -> VEither errs a -> Bool Source #

maximum :: Ord a => VEither errs a -> a Source #

minimum :: Ord a => VEither errs a -> a Source #

sum :: Num a => VEither errs a -> a Source #

product :: Num a => VEither errs a -> a Source #

Traversable (VEither errs) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

traverse :: Applicative f => (a -> f b) -> VEither errs a -> f (VEither errs b) Source #

sequenceA :: Applicative f => VEither errs (f a) -> f (VEither errs a) Source #

mapM :: Monad m => (a -> m b) -> VEither errs a -> m (VEither errs b) Source #

sequence :: Monad m => VEither errs (m a) -> m (VEither errs a) Source #

Applicative (VEither errs) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

pure :: a -> VEither errs a Source #

(<*>) :: VEither errs (a -> b) -> VEither errs a -> VEither errs b Source #

liftA2 :: (a -> b -> c) -> VEither errs a -> VEither errs b -> VEither errs c Source #

(*>) :: VEither errs a -> VEither errs b -> VEither errs b Source #

(<*) :: VEither errs a -> VEither errs b -> VEither errs a Source #

Functor (VEither errs) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

fmap :: (a -> b) -> VEither errs a -> VEither errs b Source #

(<$) :: a -> VEither errs b -> VEither errs a Source #

Monad (VEither errs) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

(>>=) :: VEither errs a -> (a -> VEither errs b) -> VEither errs b Source #

(>>) :: VEither errs a -> VEither errs b -> VEither errs b Source #

return :: a -> VEither errs a Source #

Semigroup (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

(<>) :: VEither errs a -> VEither errs a -> VEither errs a Source #

sconcat :: NonEmpty (VEither errs a) -> VEither errs a Source #

stimes :: Integral b => b -> VEither errs a -> VEither errs a Source #

Generic (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Associated Types

type Rep (VEither errs a) 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

from :: VEither errs a -> Rep (VEither errs a) x Source #

to :: Rep (VEither errs a) x -> VEither errs a Source #

(Show a, Show (Vary errs)) => Show (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

showsPrec :: Int -> VEither errs a -> ShowS Source #

show :: VEither errs a -> String Source #

showList :: [VEither errs a] -> ShowS Source #

(NFData a, NFData (Vary errs)) => NFData (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

rnf :: VEither errs a -> () Source #

(Eq a, Eq (Vary errs)) => Eq (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

(==) :: VEither errs a -> VEither errs a -> Bool Source #

(/=) :: VEither errs a -> VEither errs a -> Bool Source #

(Ord a, Ord (Vary errs)) => Ord (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Methods

compare :: VEither errs a -> VEither errs a -> Ordering Source #

(<) :: VEither errs a -> VEither errs a -> Bool Source #

(<=) :: VEither errs a -> VEither errs a -> Bool Source #

(>) :: VEither errs a -> VEither errs a -> Bool Source #

(>=) :: VEither errs a -> VEither errs a -> Bool Source #

max :: VEither errs a -> VEither errs a -> VEither errs a Source #

min :: VEither errs a -> VEither errs a -> VEither errs a Source #

type Rep (VEither errs a) Source # 
Instance details

Defined in Cardano.CLI.Vary.VEither

Conversion

toVary :: forall (errs :: [Type]) a. VEither errs a -> Vary (a ': errs) Source #

Turns the VEither into a normal Vary, no longer considering the a a 'preferred' value.

In many cases, you probably want to mattern match on VEither.VLeft instead!

fromVary :: forall a (errs :: [Type]). Vary (a ': errs) -> VEither errs a Source #

Turns a Vary into a VEither. Now the a is considered the 'preferred' value.

In many cases, you probably want to use VEither.VLeft instead!

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) 

fromRight :: forall a (errs :: [Type]). a -> VEither errs a Source #

Construct a VEither from an a.

Exists for symmetry with fromLeft. Indeed, this is just another name for VRight (and for pure).

toEither :: forall (errs :: [Type]) a. VEither errs a -> Either (Vary errs) a Source #

Turns a VEither into a normal Either.

fromEither :: forall (errs :: [Type]) a. Either (Vary errs) a -> VEither errs a Source #

Turns a normal Either into a VEither.

veither :: forall (errs :: [Type]) c a. (Vary errs -> c) -> (a -> c) -> VEither errs a -> c Source #

Case analysis on a VEither. Similar to either.

See also VEither.mapLeft, VEither.mapLeftOn and VEither.mapRight.

intoOnly :: VEither ('[] :: [Type]) a -> a Source #

If you have a VEither which does not actually contain any errors, you can be sure it always contains an a.

Similar to Vary.intoOnly.

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 #

Handle a particular error possibility.

Works very similarly to Vary.on.

onRight :: forall a b (errs :: [Type]). (a -> b) -> (VEither errs a -> b) -> VEither errs a -> b Source #

Handle the success posibility.

Works very similarly to Vary.on. Usually used together with VError.onLeft.

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 #

Map a function over one of the error values inside the VEither.

Any other VLeft and also VRight are kept untouched.

Similar to Vary.mapOn.

mapLeft :: forall (xs :: [Type]) (ys :: [Type]) a. (Vary xs -> Vary ys) -> VEither xs a -> VEither ys a Source #

Map a function over the VEither if it contains a VLeft, otherwise leave it alone.

See also VEither.mapLeftOn, VEither.mapRight and VEither.veither.

mapRight :: forall x y (errs :: [Type]). (x -> y) -> VEither errs x -> VEither errs y Source #

Map a function over the VEither if it contains a VRight, otherwise leave it alone.

Exists for symmetry with VEither.mapLeft and VEither.mapLeftOn.

Indeed, it is just another name for fmap.

See also VEither.veither.

morph :: forall (ys :: [Type]) (xs :: [Type]) a. Subset (a ': xs) (a ': ys) => VEither xs a -> VEither ys a Source #

Extend a smaller VEiher into a bigger one, change the order of its error types, or get rid of duplicate error types.

Similar to Vary.morph

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.