Safe Haskell | None |
---|---|
Language | Haskell2010 |
Provides the NonNegative
type for enforcing a nonnegative invariant.
Since: 0.1
Synopsis
- data NonNegative a where
- pattern MkNonNegative :: a -> NonNegative a
- mkNonNegativeTH :: (Integral a, Lift a, Show a) => a -> Code Q (NonNegative a)
- mkNonNegative :: (Num a, Ord a) => a -> Maybe (NonNegative a)
- unsafeNonNegative :: (HasCallStack, Num a, Ord a, Show a) => a -> NonNegative a
- (*!) :: (HasCallStack, Num a, Ord a, Show a) => a -> NonNegative a
- reallyUnsafeNonNegative :: a -> NonNegative a
- unNonNegative :: NonNegative a -> a
- _MkNonNegative :: (Num a, Ord 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
pattern MkNonNegative :: a -> NonNegative a | Unidirectional pattern synonym for Since: 0.1 |
Instances
Creation
mkNonNegativeTH :: (Integral a, Lift 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 :: (Num a, Ord a) => a -> Maybe (NonNegative a) Source #
Smart constructor for NonNegative
. Returns Nothing
if the second
parameter is < 0
.
Examples
>>>
mkNonNegative 0
Just (UnsafeNonNegative 0)
>>>
mkNonNegative (-2)
Nothing
Since: 0.1
unsafeNonNegative :: (HasCallStack, Num a, Ord a, Show a) => a -> NonNegative a Source #
Throws an error when given a value < 0.
WARNING: Partial
Examples
>>>
unsafeNonNegative 7
UnsafeNonNegative 7
Since: 0.1
(*!) :: (HasCallStack, Num a, 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 n
2
_MkNonNegative :: (Num a, Ord a) => ReversedPrism' (NonNegative a) a Source #
ReversedPrism'
that enables total elimination and partial construction.
Examples
>>>
import Optics.Core (view)
>>>
nn = $$(mkNonNegativeTH 2)
>>>
view _MkNonNegative nn
2
>>>
rmatching _MkNonNegative 3
Right (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