# Operator distributivity in expressions - syntactic sugar

When writing boolean expressions out, you often have to use something of the type:

``````A == any of B1, ..., Bn
C < all of D1, ..., Dm
``````

But in code you have to use expanded forms:

``````A == B1 or ... or A == Bn
C < D1 and ... and C < Dm
``````

That looks, to me, like any of and all of are simply the distributed form of or and and. That made me wonder if you can apply distributivity to operators in some way that compilers/interpretors can understand. For the above example, say, something like this:

``````A == B1 +_or ... +_or Bn
C < D1 +_and ... +_and Dm
``````

Essentially, taking operators as relations, it should work something like this:

``````R1(A, distributive_R2(B1, ... , Bn)) becomes
R2(R1(A, B1), ... , R1(A, Bn))
``````

This could apply to more than just booleans:

``````X / Y1 +_+ ... +_+ Yn becomes
X/Y1 + ... + X/Yn
``````

Of course, my `+_` prefix may not be the ideal syntax; I'm sure there's better ones out there. Nevertheless, this strikes me as being possibly quite useful and understandable. Any thoughts?

• Not 100% sure, since your "syntax" is a bit unusual, but this seems to be similar to the 'fold' function you find in many languages (especially functional languages) where you apply a single function on all elements in a list like `foldl (+) 0 [1,2,3]` would sum a list or `foldl (&&) True [True, False, True]` would apply AND on all elements of a bool list. You have this in many languages or could implement easy enough as long as you have higher order functions. – thorsten müller Jul 17 '13 at 11:03

1. I don't like the syntax

`A == B1 or ... or A == Bn` is not obvious. It is for simple forms, but those simple forms are rare. For example, there is no need to write:

``````A == 1 or ... or A == 10
``````

when you can simply write:

``````A >= 1 and A <= 10
``````

If you enumerate through the sequence, the syntax won't help neither:

``````Set = [ 4, 7, 2, 3, 0, 1, 1 ]
A == Set or ... or A == Set[Set.Length]
``````

is too verbose.

Also, what would:

``````A == 1 or ... and A == 10
``````

mean? If this is illegal, how an IDE would prevent it? Why forcing the developer to type the same word twice?

Readability should be your concern too. `C < D1 +_and ... +_and Dm` in your question is already difficult to understand. I can't imagine a piece of code which combines several ranges.

2. `in` and ranges in SQL and Ada

The easier syntax would be the one which is already used in SQL for keywords and in Ada and Matlab for ranges:

``````A in 1..10
``````

or:

``````A in Set
``````

Some SQL variants also use `between`:

``````A between 1 and 10
``````

I largely prefer Ada syntax `1..10`, which is extremely explicit and shorter to write. The `and` keyword makes the reader think about a boolean operation, not a range. Without knowing the syntax, some developers will also be tempted to write either:

``````A between 1, 10
``````

or:

``````A between(1, 10)
``````

The SQL/Ada syntax is nice when it comes to searching a value in a set or a range, but not when it comes to verifying that an expression is true for any value of a sequence. I don't know if those languages have constructs allowing that.

3. C# world, as a side note

Note that in C#, which uses some paradigms from functional programming and has lambda expressions, your suggested syntax would be written this way:

Searching for a value in a sequence:

``````var isInList = sequence.Contains(value);
``````

or:

``````var isInList = sequence.Any(a => a == value);
``````

Verifying that en expression is valid for any element of a sequence:

``````var trueForAll = sequence.All(a => a > 0);
``````

Filtering:

``````var positiveOnly = sequence.Where(a => a > 0);
``````

This is not the best syntax ever, and I largely prefer the one which is inspired by SQL/Ada, but it gives you some ideas about how your problem is solved in different languages.

You can use this syntax to create your own, with specific language keywords. Something like:

``````A in Set              # Verifies that Set contains A.
B in 1..10            # Verifies that 1 ≤ B ≤ 10.
C > 0 for any Set     # Verifies that all elements of Set are positive.
D <> 0 for any 1..10  # Verifies that none of the values between 1 and 10 are equal to zero.
``````