Safe Haskell | None |
---|---|
Language | Haskell2010 |
Provides the Positive
type for enforcing a positive invariant.
Since: 0.1
Synopsis
- data Positive a where
- pattern MkPositive :: a -> Positive a
- mkPositiveTH :: (Integral a, Lift a, Show a) => a -> Code Q (Positive a)
- mkPositive :: (Num a, Ord a) => a -> Maybe (Positive a)
- unsafePositive :: (HasCallStack, Num a, Ord a, Show a) => a -> Positive a
- (+!) :: (HasCallStack, Num a, Ord a, Show a) => a -> Positive a
- reallyUnsafePositive :: a -> Positive a
- unPositive :: Positive a -> a
- positiveToNonZero :: Positive a -> NonZero (Positive a)
- _MkPositive :: (Num a, Ord a) => ReversedPrism' (Positive a) a
- rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a
Type
data Positive a where Source #
Newtype wrapper that attaches a Positive
invariant to some a
.
Positive
is an ASemigroup
and
MGroup
i.e. supports addition,
multiplication, and division.
Since: 0.1
pattern MkPositive :: a -> Positive a | Unidirectional pattern synonym for Since: 0.1 |
Instances
Creation
mkPositiveTH :: (Integral a, Lift a, Show a) => a -> Code Q (Positive a) Source #
Template haskell for creating a Positive
at compile-time.
Examples
>>>
$$(mkPositiveTH 1)
UnsafePositive 1
Since: 0.1
unsafePositive :: (HasCallStack, Num a, Ord a, Show a) => a -> Positive a Source #
Throws an error when given a value <= 0.
WARNING: Partial
Examples
>>>
unsafePositive 7
UnsafePositive 7
Since: 0.1
reallyUnsafePositive :: a -> Positive a Source #
This function is an alias for the unchecked constructor UnsafePositive
i.e. it allows us to construct a Positive
without checking the
invariant. This is intended only for when we absolutely know the invariant
holds and a branch (i.e. unsafePositive
) is undesirable for
performance reasons. Exercise extreme caution.
Since: 0.1
Elimination
unPositive :: Positive a -> a Source #
Since: 0.1
Functions
Optics
We provide a ReversedPrism'
_MkPositive
that allows for total
elimination and partial construction, along with a LabelOptic
Getter
for #unPositive
.
Examples
>>>
:set -XOverloadedLabels
>>>
import Optics.Core (view)
>>>
let n = $$(mkPositiveTH 2)
>>>
view #unPositive n
2
_MkPositive :: (Num a, Ord a) => ReversedPrism' (Positive a) a Source #
ReversedPrism'
that enables total elimination and partial construction.
Examples
>>>
import Optics.Core (view)
>>>
pos = $$(mkPositiveTH 2)
>>>
view _MkPositive pos
2
>>>
rmatching _MkPositive 3
Right (UnsafePositive 3)
>>>
rmatching _MkPositive 0
Left 0
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