For some time, I have been interested in how mathematics affects the software engineering world directly and started writing Math Behind Software series. I believe such niche topic can be quite an interesting read.
Today I want to add new entry to my series where I will explain why calling.allMatch on empty stream will always return true and what it has to do with math.
AllMatch On Empty Stream
I have always expected that calling allMatch
on an empty Stream will return false because there are no possible elements to match, so naturally, none of the elements can match. This may have been an overengineered understanding but nevertheless, it was what I thought.
Fortunately (or unfortunately), I was informed by — Jakub Cichy that I was wrong. I must admit that I was quite surprised but after reading more about the topic it became clearer. That is how I learned about the existence of the concept known as Vacuous truth.
Below you can find a code snippet needed to recreate the problem.
public class Main { public static void main(String[] args) { boolean b = Stream.empty().allMatch(e -> e.equals("2")); System.out.println(b); } }
As a side note, I can add that if you are using IntelliJ IDEA it will mark the third line of the above snippet as compilation warning. The message will state thatallMatch
on empty Stream will always be true. Just as in the image below.
What Is Vacuous Truth?
It is a concept from branch of mathematics known as logic. It is used to describe all statements that are evaluated as truth because their base condition (Antecedent) cannot be fulfilled.
What it exactly means is that in the process of evaluation of every logic statement, the first half of a hypothetical proposition is responsible for deciding if evaluation of a particular statement will precede to then-clause or not — this is what antecedent is.
For example, in the statement “all people reading this are developers” an antecedent is an implicit condition that this article will be read by anyone, so if this article gets 0 views the above statement will be automatically evaluated as being true.
Another example of such statement is “if Christmas is in January you will get no gifts”. In both cases, false antecedents make them impossible for readers to reason about the output of then-clause of the statement.
Vacuous Truth in Other Languages
Except the already provided Java example, similar cases can be recreated in other modern-day programming languages:
- In Scala: forall,present in many collections
- In JavaScript: every from array prototype
- In Python and Kotlin: all – which tests if all elements of an array are true, will also return true for empty arrays
Vacuous Truth In RealLife
In math, Vacuous Truth statements are not the main point of interest themselves. Nonetheless, they are often used in proofs based on mathematical inductions.
Outside the mathematical context, Vacuous Truth statements can be described as misleading. In most cases, such statements provide reasonable information about real-life objects which do not exist in particular situations. To some extent, all fake news can be categorized as Vacuous Truth statements.
Here on the other hand are some examples of vacuously truth statements that you probably used in your day to day life:
- when hell freezes
- when pigs fly
- when shrimps learn to whistle
- when the sun starts rising in the west
Closing Thoughts
Remember that Vacuous truth is a concept from the mathematics field of logic, and is the reason why .allMatch
and their counterparts will return true for empty sequences.
Hope that my text was interesting for you. Thank you for your time.
Comments are closed.