Skip to content

Min operation on Coins #10995

Closed
Closed
@JimLarson

Description

Summary

Incorporate a new Coins method: func (c Coins) Min(other Coins) Coins for utility and consistency.

Problem Definition

When working with Coins, it's sometimes useful to compute the minimum of two values, defined as the minimum of each denom, but there's no existing library method for it.

Example: toTransfer := want.Min(spendable)
Example: to compute max(a.Sub(b), zeroCoin) without panicking, compute a.Sub(a.Min(b))

With IsAllLTE() defining a partial order on Coins, Min() becomes the "meet" operator of a lattice, tying the operations into centuries of mathematical precedent.

Proposal

Implement func (c Coins) Min(other Coins) Coins with tests and documentation for its properties.

Max() should be added for symmetry, with the property that a.Add(b) == (a.Max(b)).Add(a.Min(b)), so it could be defined as

func (c Coins) Max(other Coins) Coins {
    return c.Add(other).Sub(c.Min(other))
}

Where 0 is the empty Coins value, <= is IsAllLTE(), and == is a non-panicking variant of IsEqual(), for arbitrary Coins values a, b, and c, we have the properties:

  • <= is a partial order:
    • Reflexivity: a <= a
    • Antisymmetry: a <= b && b <= a imply a == b
    • Transitivity: a <= b && b <= c imply a <= c
  • Min() is the greatest lower bound ("meet") with respect to this order, and Max() is the least upper bound ("join"):
    • a <= b if and only if a.min(b) == a && a.max(b) == b
  • Min() and Max() are commutative and associative
  • Absorption laws:
    • a.Max(a.Min(b)) == a
    • a.Min(a.Max(b)) == a
  • Distributivity:
    • a.Max(b.Min(c)) == a.Max(b).Min(a.Max(c))
    • a.Min(b.Max(c)) == a.Min(b).Max(a.Min(c))
  • 0 is the bottom element
    • 0.Min(a) == 0
    • 0.Max(a) == a

For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions