Logic Behind Software — allMatch On Empty Stream
Have you ever wonder why all match on empty Stream will always return true ? Here you will get all information needed to understand why it works this way.
Join the DZone community and get the full member experience.
Join For FreeBasically, it works this way because of the logic concept called Vacuous truth. It is a simple and short answer but because we do not like such answers, we will dive deeper. But why I even decided to write about such a niche topic. For some time, I have been interested in how mathematics affects the software engineering world directly, and I have even written an article about calculating scalability. I believe such niche topics can be quite an interesting thing for you.
We will start by shortly presenting the main problem described in today's text.
AllMatch On Empty Stream Returning True
Below you can find a code snippet needed to recreate the problem. However, I will explain the context first. 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 my coworker from Softwaremill - 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.
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 your IDE will mark the third line of the above snippet as compilation warning. The message will state thatallMatch
on empty Stream will always be true – at least it was the case in IntelliJ IDEA.
What Vacuous Truth Is And How It Works
In general, it is a logical concept 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.
The term Vacuous Truth can be also used to describe statements with false base condition, for example: if Christmas is in January you will get no gifts. In both cases, false antecedents make it impossible for readers to reason about the output of then-clause of the statement.
Vacuous Truth In Real Life
In math, Vacuous Truth statements are not the main point of interest themselves but 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.
Vacuous Truth in Software Engineering
Except the already provided Java example, similar cases can be recreated in many other modern-day programming languages:
- In Scala method
forall
, present in many interfaces of collections, works in the same way. - In JavaScript method
every
from array prototype is implemented similarly. - In Python built-in function
all
, which tests if all elements of array are true, will also return true for empty array.
Summary
I presented the problem and provided a code snipped needed to recreate it, then I described what the logic concept behind its occurrence is. Additionally, I explained what Vacuous Truth is and what use cases it has in real life. I also mentioned some other methods with similar inner workings present in languages other than Java. I hope that my article was interesting or maybe even surprising and that it will help you somewhere along your path as a software engineer. Thank you for your time.
Opinions expressed by DZone contributors are their own.
Comments