I write Haskell, but it can be applied to any OO or functional language with the concept of ADT. I will give the template in Haskell, ignoring the fact that arithmetic operators are already accepted:
class Thing a where
(+) :: a -> a -> a
(-) :: a -> a -> a
x - y = x + negate y
(*) :: (RealFrac b) => a -> b -> a
negate :: a -> a
negate x = x * (-1)
Basically, these are things that can be added and subtracted, as well as multiplied by real fractional values. One example would be a simple list of numbers: addition and subtraction in pairs (in Haskell, "(+) = zipWith (+)"), and multiplication by real multiplies each element in the list by the same amount. I met enough other examples to define it as a class, but I don’t know exactly what to call it.
In Haskell, its usually a monoid, provided that there is some kind of null value.
Is this some well-known kind of object in an zoo of algebraic types? I looked through rings, half rings, intimacy, groups, etc., without finding it.
source
share