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

Numeric.Data.ModN

Description

Provides the ModN type for modular arithmetic.

Since: 0.1

Synopsis

Type

data ModN (n :: Nat) a where Source #

Newtype wrapper that represents \( \mathbb{Z}/n\mathbb{Z} \). ModN is a Ring i.e. supports addition, subtraction, and multiplication.

When constructing a ModN n a we must verify that the type a is large enough to accommodate n, hence the possible failure.

Examples

Expand
>>> import Data.Text.Display (display)
>>> display $ unsafeModN @7 10
"3 (mod 7)"

Since: 0.1

Bundled Patterns

pattern MkModN :: a -> ModN n a

Bidirectional pattern synonym for ModN. Construction will apply modular reduction to the parameter.

Since: 0.1

Instances

Instances details
HasField "unModN" (ModN n a) a Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

getField :: ModN n a -> a #

(k ~ A_Getter, x ~ a, y ~ a) => LabelOptic "unModN" k (ModN n a) (ModN n a) x y Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

labelOptic :: Optic k NoIx (ModN n a) (ModN n a) x y Source #

Lift a => Lift (ModN n a :: Type) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

lift :: Quote m => ModN n a -> m Exp #

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

(Integral a, KnownNat n, MaybeLowerBounded a, MaybeUpperBounded a, Typeable a) => AGroup (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

(.-.) :: ModN n a -> ModN n a -> ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => AMonoid (ModN n a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

zero :: ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a) => ASemigroup (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

(.+.) :: ModN n a -> ModN n a -> ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => MMonoid (ModN n a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

one :: ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a) => MSemigroup (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

(.*.) :: ModN n a -> ModN n a -> ModN n a Source #

(Integral a, KnownNat n, MaybeLowerBounded a, MaybeUpperBounded a, Typeable a) => Ring (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => Semiring (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => FromInteger (ModN n a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

afromInteger :: Integer -> ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => Bounded (ModN n a) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

minBound :: ModN n a #

maxBound :: ModN n a #

Generic (ModN n a) Source # 
Instance details

Defined in Numeric.Data.ModN.Internal

Associated Types

type Rep (ModN n a)

Since: smart-math-0.1

Instance details

Defined in Numeric.Data.ModN.Internal

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

Methods

from :: ModN n a -> Rep (ModN n a) x #

to :: Rep (ModN n a) x -> ModN n a #

(KnownNat n, Show a) => Show (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

showsPrec :: Int -> ModN n a -> ShowS #

show :: ModN n a -> String #

showList :: [ModN n a] -> ShowS #

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => LowerBounded (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

lowerBound :: ModN n a Source #

(Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => UpperBounded (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

upperBound :: ModN n a Source #

NFData a => NFData (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

rnf :: ModN n a -> () #

Eq a => Eq (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

(==) :: ModN n a -> ModN n a -> Bool #

(/=) :: ModN n a -> ModN n a -> Bool #

Ord a => Ord (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

Methods

compare :: ModN n a -> ModN n a -> Ordering #

(<) :: ModN n a -> ModN n a -> Bool #

(<=) :: ModN n a -> ModN n a -> Bool #

(>) :: ModN n a -> ModN n a -> Bool #

(>=) :: ModN n a -> ModN n a -> Bool #

max :: ModN n a -> ModN n a -> ModN n a #

min :: ModN n a -> ModN n a -> ModN n a #

(KnownNat n, Show a) => Display (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

type Rep (ModN n a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModN.Internal

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

Creation

mkModN :: forall (n :: Nat) a. (Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => a -> Either String (ModN n a) Source #

Constructor for ModN.

Examples

Expand
>>> mkModN @5 7
Right (MkModN 2 (mod 5))
>>> mkModN @10 7
Right (MkModN 7 (mod 10))
>>> mkModN @128 (9 :: Int8)
Left "Type 'Int8' has a maximum size of 127. This is not large enough to safely implement mod 128."

Since: 0.1

mkModNTH :: forall (n :: Nat) a. (Integral a, KnownNat n, Lift a, MaybeUpperBounded a, Typeable a) => a -> Code Q (ModN n a) Source #

Template haskell for creating a ModN at compile-time.

Examples

Expand
>>> $$(mkModNTH @11 7)
MkModN 7 (mod 11)

Since: 0.1

unsafeModN :: forall (n :: Nat) a. (HasCallStack, Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => a -> ModN n a Source #

Variant of mkModN that throws an error when type a is not large enough to fit n.

WARNING: Partial

Examples

Expand
>>> unsafeModN @7 12
MkModN 5 (mod 7)

Since: 0.1

reallyUnsafeModN :: forall (n :: Nat) a. (Integral a, KnownNat n) => a -> ModN n a Source #

This function reduces the argument modulo p but does not check that n fits within a. Note that correct behavior requires this, so this is dangerous. This is intended only for when we absolutely know n fits in a and the check is undesirable for performance reasons. Exercise extreme caution.

Since: 0.1

Elimination

unModN :: forall (n :: Nat) a. ModN n a -> a Source #

Since: 0.1

Optics

We provide a ReversedPrism' _MkModN that allows for total elimination and partial construction, along with a LabelOptic Getter for #unModN.

Examples

Expand
>>> :set -XOverloadedLabels
>>> import Optics.Core (view)
>>> let n = $$(mkModNTH @7 9)
>>> view #unModN n
2

_MkModN :: forall (n :: Nat) a. (Integral a, KnownNat n, MaybeUpperBounded a, Ord a, Typeable a) => ReversedPrism' (ModN n a) a Source #

ReversedPrism' that enables total elimination and partial construction.

Examples

Expand
>>> import Optics.Core (view)
>>> n = $$(mkModNTH @7 9)
>>> view _MkModN n
2
>>> rmatching (_MkModN @7) 9
Right (MkModN 2 (mod 7))
>>> rmatching (_MkModN @128) (9 :: Int8)
Left 9

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