Skip to content

A proposal for generalized abs #66

Description

@hdgarrood

I think it would be nice to get some concept of abs which works for not only Int and Number, but also more exotic numeric types, like Rational, Complex, and even Quaternion (when we eventually implement it ;).

Given that we (well, I) want this to work for numeric types which don't have total orders (such as Complex), I thought a good starting point would be to take laws from https://en.wikipedia.org/wiki/Absolute_value but remove all those to do with ordering, so:

class Semiring a <= Absolute a where
  abs :: a -> a

Laws:

  • Positive-definiteness: abs x == zero iff x == zero
  • Multiplicativeness: abs (x * y) == abs x * abs y
  • Idempotence: abs (abs x) == abs x

We could add more laws by making the constraint a bit stronger, to Ring if we want those that mention subtraction, or maybe even EuclideanRing if we want those that mention division. But the above seems a good starting point, I think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions