| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Numeric.Data.Positive
Description
Provides the Positive type for enforcing a positive invariant.
Since: 0.1
Synopsis
- data Positive a where
- pattern MkPositive :: a -> Positive a
- mkPositiveTH :: (AMonoid a, Lift a, Ord a, Show a) => a -> Code Q (Positive a)
- mkPositive :: (AMonoid a, Ord a, Show a) => a -> Either String (Positive a)
- unsafePositive :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> Positive a
- (+!) :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> Positive a
- reallyUnsafePositive :: a -> Positive a
- unPositive :: Positive a -> a
- positiveToNonZero :: Positive a -> NonZero (Positive a)
- _MkPositive :: (AMonoid a, Ord a, Show 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
Bundled Patterns
| pattern MkPositive :: a -> Positive a | Unidirectional pattern synonym for Since: 0.1 |
Instances
Creation
mkPositiveTH :: (AMonoid a, Lift a, Ord 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 :: (AMonoid a, HasCallStack, Ord a, Show a) => a -> Positive a Source #
Throws an error when given a value <= 0.
WARNING: Partial
Examples
>>>unsafePositive 7UnsafePositive 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 n2
_MkPositive :: (AMonoid a, Ord a, Show a) => ReversedPrism' (Positive a) a Source #
ReversedPrism' that enables total elimination and partial construction.
Examples
>>>import Optics.Core (view)>>>pos = $$(mkPositiveTH 2)>>>view _MkPositive pos2
>>>rmatching _MkPositive 3Right (UnsafePositive 3)
>>>rmatching _MkPositive 0Left 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