smart-math-0.1: Mathematical smart constructors
Safe HaskellNone
LanguageHaskell2010

Numeric.Data.NonNegative

Description

Provides the NonNegative type for enforcing a nonnegative invariant.

Since: 0.1

Synopsis

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 NonNegative. This allows us to pattern match on a non-negative term without exposing the unsafe internal details.

Since: 0.1

Instances

Instances details
HasField "unNonNegative" (NonNegative a) a Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Methods

getField :: NonNegative a -> a #

(k ~ A_Getter, x ~ a, y ~ a) => LabelOptic "unNonNegative" k (NonNegative a) (NonNegative a) x y Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Lift a => Lift (NonNegative a :: Type) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Methods

lift :: Quote m => NonNegative a -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => NonNegative a -> Code m (NonNegative a) #

Num a => AMonoid (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Methods

zero :: NonNegative a Source #

Num a => ASemigroup (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Division a, Integral a) => MEuclidean (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Division a, Num a) => MGroup (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Num a => MMonoid (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Methods

one :: NonNegative a Source #

Num a => MSemigroup (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Normed (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Division a, Num a) => Semifield (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Num a => Semiring (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Num a, Ord a, Show a) => FromInteger (NonNegative a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Fractional a, Ord a, Show a) => FromRational (NonNegative a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

(Bounded a, Num a) => Bounded (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Generic (NonNegative a) Source # 
Instance details

Defined in Numeric.Data.NonNegative.Internal

Associated Types

type Rep (NonNegative a)

Since: smart-math-0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

type Rep (NonNegative a) = D1 ('MetaData "NonNegative" "Numeric.Data.NonNegative.Internal" "smart-math-0.1-inplace" 'True) (C1 ('MetaCons "UnsafeNonNegative" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Methods

from :: NonNegative a -> Rep (NonNegative a) x #

to :: Rep (NonNegative a) x -> NonNegative a #

Show a => Show (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Num a => LowerBounded (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

UpperBounded a => UpperBounded (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

UpperBoundless (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

NFData a => NFData (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Methods

rnf :: NonNegative a -> () #

Eq a => Eq (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Ord a => Ord (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

Show a => Display (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

type Rep (NonNegative a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.NonNegative.Internal

type Rep (NonNegative a) = D1 ('MetaData "NonNegative" "Numeric.Data.NonNegative.Internal" "smart-math-0.1-inplace" 'True) (C1 ('MetaCons "UnsafeNonNegative" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Creation

mkNonNegativeTH :: (Integral a, Lift a, Show a) => a -> Code Q (NonNegative a) Source #

Template haskell for creating a NonNegative at compile-time.

Examples

Expand
>>> $$(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

Expand
>>> 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

Expand
>>> 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

Expand
>>> (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

Expand
>>> :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

Expand
>>> 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