Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- newtype TokenQuantity = TokenQuantity { }
- zero :: TokenQuantity
- add :: TokenQuantity -> TokenQuantity -> TokenQuantity
- subtract :: TokenQuantity -> TokenQuantity -> Maybe TokenQuantity
- pred :: TokenQuantity -> Maybe TokenQuantity
- predZero :: TokenQuantity -> TokenQuantity
- succ :: TokenQuantity -> TokenQuantity
- difference :: TokenQuantity -> TokenQuantity -> TokenQuantity
- equipartition :: TokenQuantity -> NonEmpty a -> NonEmpty TokenQuantity
- partition :: TokenQuantity -> NonEmpty TokenQuantity -> Maybe ( NonEmpty TokenQuantity )
- partitionDefault :: TokenQuantity -> NonEmpty TokenQuantity -> NonEmpty TokenQuantity
- isNonZero :: TokenQuantity -> Bool
- isZero :: TokenQuantity -> Bool
- unsafeSubtract :: TokenQuantity -> TokenQuantity -> TokenQuantity
Type
newtype TokenQuantity Source #
Represents an integral quantity of tokens.
At present, we use
Natural
as our underlying type, as the only use case
for these quantities is to be included in token bundles held within
transaction outputs, and these must never be negative.
When we build support for minting and burning of tokens, we may wish to
parameterize this type and allow it to be instantiated with
Integer
.
Instances
Values
zero :: TokenQuantity Source #
Arithmetic operations
add :: TokenQuantity -> TokenQuantity -> TokenQuantity Source #
subtract :: TokenQuantity -> TokenQuantity -> Maybe TokenQuantity Source #
Subtracts the second token quantity from the first.
Returns
Nothing
if the first quantity is less than the second quantity.
pred :: TokenQuantity -> Maybe TokenQuantity Source #
Finds the predecessor of a given token quantity.
Returns
Nothing
if the given quantity is zero.
succ :: TokenQuantity -> TokenQuantity Source #
Finds the successor of a given token quantity.
difference :: TokenQuantity -> TokenQuantity -> TokenQuantity Source #
Subtracts the second token quantity from the first.
Returns
zero
if the first quantity is less than the second quantity.
Partitioning
:: TokenQuantity |
The token quantity to be partitioned. |
-> NonEmpty a |
Represents the number of portions in which to partition the quantity. |
-> NonEmpty TokenQuantity |
The partitioned quantities. |
Computes the equipartition of a token quantity into
n
smaller quantities.
An
equipartition
of a token quantity is a
partition
of that quantity
into
n
smaller quantities whose values differ by no more than 1.
The resultant list is sorted in ascending order.
:: TokenQuantity |
The token quantity to be partitioned. |
-> NonEmpty TokenQuantity |
The list of weights. |
-> Maybe ( NonEmpty TokenQuantity ) |
The partitioned token quantities. |
Partitions a token quantity 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.
:: TokenQuantity |
The token quantity to be partitioned. |
-> NonEmpty TokenQuantity |
The list of weights. |
-> NonEmpty TokenQuantity |
The partitioned token quantities. |
Partitions a token quantity 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 q ws) == c
length (partitionDefault q ws) == length ws
If the sum of weights is equal to zero, then this function returns an
equipartition
satisfying the following property:
partitionDefault q ws == equipartition q ws
Tests
isNonZero :: TokenQuantity -> Bool Source #
isZero :: TokenQuantity -> Bool Source #
Unsafe operations
unsafeSubtract :: TokenQuantity -> TokenQuantity -> TokenQuantity Source #
Subtracts the second token quantity from the first.
Pre-condition: the first quantity is not less than the second quantity.
Throws a run-time exception if the pre-condition is violated.