| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Numeric.Data.NonNegative
Contents
Description
Provides the NonNegative type for enforcing a nonnegative invariant.
Since: 0.1
Synopsis
- data NonNegative a where
- pattern MkNonNegative :: a -> NonNegative a
- mkNonNegativeTH :: (AMonoid a, Lift a, Ord a, Show a) => a -> Code Q (NonNegative a)
- mkNonNegative :: (AMonoid a, Ord a, Show a) => a -> Either String (NonNegative a)
- unsafeNonNegative :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> NonNegative a
- (*!) :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> NonNegative a
- reallyUnsafeNonNegative :: a -> NonNegative a
- unNonNegative :: NonNegative a -> a
- _MkNonNegative :: (AMonoid a, Ord a, Show a) => ReversedPrism' (NonNegative a) a
- rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a
Type
data NonNegative a where Source #
Newtype wrapper that attaches a NonNegative invariant to some a.
NonNegative is a Semifield i.e. supports
addition, multiplication, and division.
Since: 0.1
Bundled Patterns
| pattern MkNonNegative :: a -> NonNegative a | Unidirectional pattern synonym for Since: 0.1 |
Instances
Creation
mkNonNegativeTH :: (AMonoid a, Lift a, Ord a, Show a) => a -> Code Q (NonNegative a) Source #
Template haskell for creating a NonNegative at compile-time.
Examples
>>>$$(mkNonNegativeTH 1)UnsafeNonNegative 1
Since: 0.1
mkNonNegative :: (AMonoid a, Ord a, Show a) => a -> Either String (NonNegative a) Source #
Smart constructor for NonNegative. Returns Nothing if the second
parameter is < 0.
Examples
>>>mkNonNegative 0Right (UnsafeNonNegative 0)
>>>mkNonNegative (-2)Left "Numeric.Data.NonNegative: Received value < zero: -2"
Since: 0.1
unsafeNonNegative :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> NonNegative a Source #
Throws an error when given a value < 0.
WARNING: Partial
Examples
>>>unsafeNonNegative 7UnsafeNonNegative 7
Since: 0.1
(*!) :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> NonNegative a infixl 7 Source #
Postfix operator for unsafeNonNegative.
WARNING: Partial
Examples
>>>(7 *!)UnsafeNonNegative 7
Since: 0.1
reallyUnsafeNonNegative :: a -> NonNegative a Source #
This function is an alias for the unchecked constructor UnsafeNonNegative
i.e. it allows us to construct a NonNegative without checking the
invariant. This is intended only for when we absolutely know the invariant
holds and a branch (i.e. unsafeNonNegative) is undesirable for
performance reasons. Exercise extreme caution.
Since: 0.1
Elimination
unNonNegative :: NonNegative a -> a Source #
Since: 0.1
Optics
We provide a ReversedPrism' _MkNonNegative that allows for total
elimination and partial construction, along with a LabelOptic Getter
for #unNonNegative.
Examples
>>>:set -XOverloadedLabels>>>import Optics.Core (view)>>>let n = $$(mkNonNegativeTH 2)>>>view #unNonNegative n2
_MkNonNegative :: (AMonoid a, Ord a, Show a) => ReversedPrism' (NonNegative a) a Source #
ReversedPrism' that enables total elimination and partial construction.
Examples
>>>import Optics.Core (view)>>>nn = $$(mkNonNegativeTH 2)>>>view _MkNonNegative nn2
>>>rmatching _MkNonNegative 3Right (UnsafeNonNegative 3)
>>>rmatching _MkNonNegative (-2)Left (-2)
Since: 0.1
rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a Source #
Reversed matching. Useful with smart-constructor optics.
Since: 0.1