GHC.Natural

base-4.8.2.0: Basic libraries

Copyright | (C) 2014 Herbert Valerio Riedel, (C) 2011 Edward Kmett
License | see libraries/base/LICENSE
Maintainer | libraries@haskell.org
Stability | internal
Portability | non-portable (GHC Extensions)
Safe Haskell | Unsafe
Language | Haskell2010

GHC.Natural

Contents

- The `Natural` number type
- Conversions
- Checked subtraction
- Modular arithmetic

Description

The arbitrary-precision `Natural` number type.

Note: This is an internal GHC module with an API subject to change. It's recommended use the Numeric.Natural module to import the `Natural` type.

Since: 4.8.0.0

Synopsis

- data Natural
  - = NatS# GmpLimb#
  - | NatJ# !BigNat
- isValidNatural :: Natural -> Bool
- wordToNatural :: Word -> Natural
- naturalToWordMaybe :: Natural -> Maybe Word
- minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
- powModNatural :: Natural -> Natural -> Natural -> Natural

# The `Natural` number type

Warning: The internal implementation of `Natural` (i.e. which constructors are available) depends on the `Integer` backend used!

data Natural

Type representing arbitrary-precision non-negative integers.

Operations whose result would be negative ``throw` (`Underflow` :: `ArithException`)`.

Since: 4.8.0.0

Constructors

NatS# GmpLimb# | in `[0, maxBound::Word]`
NatJ# !BigNat | in `]maxBound::Word, +inf[` Invariant: `NatJ#` is used iff value doesn't fit in `NatS#` constructor.

Instances

Enum Natural |
Eq Natural |
Integral Natural |
Data Natural |
Num Natural |
Ord Natural |
Read Natural |
Real Natural |
Show Natural |
Ix Natural |
Bits Natural |
PrintfArg Natural |

isValidNatural :: Natural -> Bool

Test whether all internal invariants are satisfied by `Natural` value

This operation is mostly useful for test-suites and/or code which constructs `Integer` values directly.

Since: 4.8.0.0

# Conversions

wordToNatural :: Word -> Natural

Construct `Natural` from `Word` value.

Since: 4.8.0.0

naturalToWordMaybe :: Natural -> Maybe Word

Try downcasting `Natural` to `Word` value. Returns `Nothing` if value doesn't fit in `Word`.

Since: 4.8.0.0

# Checked subtraction

minusNaturalMaybe :: Natural -> Natural -> Maybe Natural

`Natural` subtraction. Returns `Nothing`s for non-positive results.

Since: 4.8.0.0

# Modular arithmetic

powModNatural :: Natural -> Natural -> Natural -> Natural

"``powModNatural` b e m`" computes base `b` raised to exponent `e` modulo `m`.

Since: 4.8.0.0