Safe Haskell | None |
---|---|
Language | Haskell2010 |
Provides the ModN
type for modular arithmetic.
Since: 0.1
Synopsis
- data ModN (n :: Nat) a where
- mkModN :: forall (n :: Nat) a. (Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => a -> Either String (ModN n a)
- mkModNTH :: forall (n :: Nat) a. (Integral a, KnownNat n, Lift a, MaybeUpperBounded a, Typeable a) => a -> Code Q (ModN n a)
- unsafeModN :: forall (n :: Nat) a. (HasCallStack, Integral a, KnownNat n, MaybeUpperBounded a, Typeable a) => a -> ModN n a
- reallyUnsafeModN :: forall (n :: Nat) a. (Integral a, KnownNat n) => a -> ModN n a
- unModN :: forall (n :: Nat) a. ModN n a -> a
- _MkModN :: forall (n :: Nat) a. (Integral a, KnownNat n, MaybeUpperBounded a, Ord a, Typeable a) => ReversedPrism' (ModN n a) a
- rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a
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
we must verify that the type ModN
n aa
is large
enough to accommodate n
, hence the possible failure.
Examples
>>>
import Data.Text.Display (display)
>>>
display $ unsafeModN @7 10
"3 (mod 7)"
Since: 0.1
pattern MkModN :: a -> ModN n a | Bidirectional pattern synonym for Since: 0.1 |
Instances
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
>>>
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
>>>
$$(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
>>>
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
Optics
We provide a ReversedPrism'
_MkModN
that allows for total
elimination and partial construction, along with a LabelOptic
Getter
for #unModN
.
Examples
>>>
: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
>>>
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