Copyright | © 2018-2020 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
This module provides the
Coin
data type, which represents a quantity of
lovelace.
Synopsis
- newtype Coin = Coin { }
- fromIntegral :: ( Bits i, Integral i) => i -> Maybe Coin
- fromNatural :: Natural -> Coin
- fromWord64 :: Word64 -> Coin
- toInteger :: Coin -> Integer
- toNatural :: Coin -> Natural
- toQuantity :: ( Bits i, Integral i) => Coin -> Maybe ( Quantity n i)
- toWord64 :: Coin -> Maybe Word64
- unsafeFromIntegral :: HasCallStack => ( Bits i, Integral i, Show i) => i -> Coin
- unsafeToQuantity :: HasCallStack => ( Bits i, Integral i) => Coin -> Quantity n i
- unsafeToWord64 :: HasCallStack => Coin -> Word64
- add :: Coin -> Coin -> Coin
- subtract :: Coin -> Coin -> Maybe Coin
- difference :: Coin -> Coin -> Coin
- distance :: Coin -> Coin -> Coin
- equipartition :: Coin -> NonEmpty a -> NonEmpty Coin
- partition :: Coin -> NonEmpty Coin -> Maybe ( NonEmpty Coin )
- partitionDefault :: Coin -> NonEmpty Coin -> NonEmpty Coin
- unsafePartition :: HasCallStack => Coin -> NonEmpty Coin -> NonEmpty Coin
Type
A
Coin
represents a quantity of lovelace.
Reminder: 1 ada = 1,000,000 lovelace.
The
Coin
type has
Semigroup
and
Monoid
instances that correspond
to ordinary addition and summation.
Instances
Conversions (Safe)
Conversions (Unsafe)
unsafeFromIntegral :: HasCallStack => ( Bits i, Integral i, Show i) => i -> Coin Source #
unsafeToQuantity :: HasCallStack => ( Bits i, Integral i) => Coin -> Quantity n i Source #
unsafeToWord64 :: HasCallStack => Coin -> Word64 Source #
Arithmetic operations
subtract :: Coin -> Coin -> Maybe Coin Source #
Subtracts the second coin from the first.
Returns
Nothing
if the second coin is strictly greater than the first.
difference :: Coin -> Coin -> Coin Source #
Subtracts the second coin from the first.
Returns 'Coin 0' if the second coin is strictly greater than the first.
distance :: Coin -> Coin -> Coin Source #
Absolute difference between two coin amounts. The result is never negative.
Partitioning
:: Coin |
The coin to be partitioned. |
-> NonEmpty a |
Represents the number of portions in which to partition the coin. |
-> NonEmpty Coin |
The partitioned coins. |
Computes the equipartition of a coin into
n
smaller coins.
An
equipartition
of a coin is a
partition
of that coin into
n
smaller
coins whose values differ by no more than 1.
The resultant list is sorted in ascending order.
:: Coin |
The coin to be partitioned. |
-> NonEmpty Coin |
The list of weights. |
-> Maybe ( NonEmpty Coin ) |
The partitioned coins. |
Partitions a coin into a number of parts, where the size of each part is proportional (modulo rounding) to the size of its corresponding element in the given list of weights, and the number of parts is equal to the number of weights.
Returns
Nothing
if the sum of weights is equal to zero.
:: Coin |
The token quantity to be partitioned. |
-> NonEmpty Coin |
The list of weights. |
-> NonEmpty Coin |
The partitioned token quantities. |
Partitions a coin into a number of parts, where the size of each part is proportional (modulo rounding) to the size of its corresponding element in the given list of weights, and the number of parts is equal to the number of weights.
This function always satisfies the following properties:
fold (partitionDefault c ws) == c
length (partitionDefault c ws) == length ws
If the sum of weights is equal to zero, then this function returns an
equipartition
satisfying the following property:
partitionDefault c ws == equipartition c ws
:: HasCallStack | |
=> Coin |
The coin to be partitioned. |
-> NonEmpty Coin |
The list of weights. |
-> NonEmpty Coin |
The partitioned coins. |
Partitions a coin into a number of parts, where the size of each part is proportional (modulo rounding) to the size of its corresponding element in the given list of weights, and the number of parts is equal to the number of weights.
Throws a run-time error if the sum of weights is equal to zero.