atlas-cardano-0.13.0: Application backend for Plutus smart contracts on Cardano
Copyright© 2018-2021 IOHK 2025 GYELD GMBH
LicenseApache-2.0
Safe HaskellSafe-Inferred
LanguageGHC2021

GeniusYield.Transaction.CoinSelection.Numeric

Description

Modified by: GeniusYield

Originally from: Cardano.Numeric.Util.

Since this module does not depend upon any deps outside hackage, we could have very well obtained it from remotely from cardano-wallet. However, as part of 3rd Atlas milestone, we have explicitly specified that we do not depend upon cardano-wallet. Once cardano-numeric is on hackage or CHaP, this module can be removed.

Synopsis

Coalescing values

padCoalesce #

Arguments

∷ ∀ m a. (Monoid m, Ord m) 
NonEmpty m

Source list

NonEmpty a

Target list

NonEmpty m 

Adjusts the source list so that its length is the same as the target list, either by padding the list, or by coalescing a subset of the elements, while preserving the total sum.

If the source list is shorter than the target list, this function repeatedly inserts mempty into the list until the desired length has been reached.

If the source list is longer than the target list, this function repeatedly coalesces the smallest pair of elements with <> until the desired length has been reached.

The resulting list is guaranteed to be sorted into ascending order, and the sum of the elements is guaranteed to be the same as the sum of elements in the source list.

Examples (shown with ordinary list notation):

>>> padCoalesce [Sum 1] (replicate 4 ()) [Sum 0, Sum 0, Sum 0, Sum 1]

>>> padCoalesce [Sum (-1)] (replicate 4 ()) [Sum (-1), Sum 0, Sum 0, Sum 0]

>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 3 ()) @[Sum 3, Sum 4, Sum 8]

>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 2 ()) @[Sum 7, Sum 8]

>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 1 ()) [Sum 15]

Partitioning natural numbers

equipartitionNatural #

Arguments

HasCallStack 
Natural

The natural number to be partitioned.

NonEmpty a

Represents the number of portions in which to partition the number.

NonEmpty Natural

The partitioned numbers.

Computes the equipartition of a natural number into n smaller numbers.

An equipartition of a natural number n is a partition of that number into n smaller numbers whose values differ by no more than 1.

The resultant list is sorted in ascending order.

partitionNatural #

Arguments

Natural

Natural number to partition

NonEmpty Natural

List of weights

Maybe (NonEmpty Natural) 

Partitions a natural number into a number of parts, where the size of each part is proportional 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.

Examples:

>>> partitionNatural 9 (1 :| [1, 1])
Just (3 :| [3,3])
>>> partitionNatural 10 (1 :| [])
Just (10 :| [])
>>> partitionNatural 30 (1 :| [2, 4, 8])
Just (2 :| [4,8,16])

Pre-condition: there must be at least one non-zero weight.

If the pre-condition is not satisfied, this function returns Nothing.

If the pre-condition is satisfied, this function guarantees that:

  1. The length of the resulting list is identical to the length of the specified list:

    fmap length (partitionNatural n weights) == Just (length weights)
  2. The sum of elements in the resulting list is equal to the original natural number:

    fmap sum (partitionNatural n weights) == Just n
  3. The size of each element in the resulting list is within unity of the ideal proportion.

unsafePartitionNatural #

Arguments

HasCallStack 
Natural

Natural number to partition

NonEmpty Natural

List of weights

NonEmpty Natural 

Partitions a natural number into a number of parts, where the size of each part is proportional 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.

Monomorphic functions

powerIntegral a ⇒ a → a → a #

Power function where all arguments are of the same type.

Helps to avoid the use of boilerplate type annotations.