2

In fold function in functional paradigm, we pass and accumulator value which is generally initialized as the identity element of the operator the function applies.

Like for addition over a list it will be 0, for multiplication 1, and so on. i.e., +(0, a) = a and

*(1, a) = a

So what it will be for mod operator %(identity, a) = a? Mod will be from [0,a) so identity does not seem to be possible. I understand that computers don't necessarily follow the mathematical definition of modulus. Moreover do all operators have an identity element or it is dependent on some specific properties that the operator should have? And is there any generalised way to find the identity element of any operator?

My questions are:

  • What is identity element for modulus operator if it exists? %(identity, a) = a
  • Are there any specific properties that the operator must follow to have an identity?
  • Is there a general way to find the identity of an operator if it has an identity.
Abhishek
  • 125
  • 8
  • 4
    No, not all operators form a [group](https://en.wikipedia.org/wiki/Group_(mathematics)) with an [identity element](https://en.wikipedia.org/wiki/Identity_element). `%` does not, for example. – Bergi Dec 01 '17 at 09:21
  • 1
    I'm voting to close this question as off-topic because it has not much to do with programming (or even JS and Haskell specifically). You might get a better response at [math.SE] – Bergi Dec 01 '17 at 09:23
  • This is not a monoid, nor even a semigroup, but the practical way to do what you probably want is to use a function such as `foldr1` in Haskell that does not take any identity element. Even if this is arguably an abuse of notation. – Davislor Dec 01 '17 at 09:58
  • 1
    `%` has a *right* identity `1` because `a % 1 == a` for all `a`, but it has no *left* identity `e` that satisfies `e % a == a` for all `a`. – chepner Dec 01 '17 at 16:04
  • You might be interested in learning about modular arithmetic. This includes definitions of addition and multiplication that differs from the traditional meaning on real numbers. – Code-Apprentice Dec 02 '17 at 01:08
  • 1
    @chepner `a%1 = 0` so I don't think that mod even has a right identity. – Abhishek Dec 03 '17 at 05:13
  • 1
    @Abhishek Oh, right. I think i was getting confused with `1 % a == 1`, which establishes `1` as a left neutral element, not a left identity, for `%`. – chepner Dec 03 '17 at 14:03

1 Answers1

2

Typically a fold is done over an algebraic structure called a monoid [wiki].

A monoid is an algebraic structure. It is defined over a set S with an associative function f (modulo is not an associative function). Furthermore there should be an element e ∈ S that is called the "*identity" element. An element such that f(e,x) = x and f(x,e) = x.

One can proof that for a monoid there is always one identity element (so it is impossible to construct a monoid where two elements are identity elements. The proof is a follows: say there are two elements a and b that are neutral elements, with a ≠ b, then f(a,b) = a (definition above), but f(a,b)=b also holds, since a function can only return one element, and a≠ b, we have reached a contradiction. We can furthermore thus conclude that a = b.

We can not define a monoid for the modulo operation (and natural, integral, ...) numbers, since the operator should be associative, and for these sets, it does not hold that: (a % b) % c = a % (b % c).

To answer your questions (in general):

What is identity element for modulus operator if it exists? %(identity, a) = a

If we take natural, integral, rational, real, etc. numbers, then modulo does not have an identity element.

Are there any specific properties that the operator must follow to have an identity?

Simply that there is an element e ∈ S such that f(x, e) = x. We can "construct" such function from every function, by simply picking an element e ∈ S, and then define it as: f'(x,y) = x if y = e and f'(x,y) = f(x,y) otherwise.

Is there a general way to find the identity of an operator if it has an identity.

Since the above shows that we can construct such function for every function, there are no general methods as far as I know to fund the identity element of an operator.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Side note, your proof need not be by contradiction. The same reasoning leads to a constructive proof that all identity elements are equal. – luqui Dec 02 '17 at 09:45
  • @luqui can you elaborate on your comment about a constructive proof for uniqueness of identity? – Abhishek Dec 04 '17 at 11:49
  • @Abishek Say there are two neutral elements *a* and *b*. Then *f(a,b) = a* by neutrality of *b*, and *f(a,b) = b* by neutrality of *a*, so *a = b*. With Willem's proof, we only have that "*a ≠ b* is impossible". The difference between these two proofs is important in constructive type theories (used by Agda, Coq, HoTT, etc.), and in these settings constructive proof is much richer in how it can be used. – luqui Dec 04 '17 at 19:12