Why should (every? string? []) yield true?

后端 未结 6 1461
说谎
说谎 2021-01-11 13:44

Looking at the source code for every? makes clear why

(every? string? []) => true

This is because every?

相关标签:
6条回答
  • 2021-01-11 14:22

    Because it functions the same as the forall-quantifier. That is, it is initially assumed true and each application of the predicate is an attempt to prove it false. The existential quantifier (which is called some rather than any? in Clojure for inconsistencies sake) works the opposite way - it assumes false and each application of the predicate is an attempt to prove it true.

    In other words, it's always true that something is true for all of none, and it's always false that something is true for some of none.

    0 讨论(0)
  • 2021-01-11 14:22

    It comes down to the identity values for the "and" and "or" operations. For any boolean B, it is the case that B has the same value as B & T. Thus, an "and" of no values must be true. Similarly, it is also true that B | F has the same value as B, so an "or" of no values is false.

    In a similar vein, the product of no terms is 1 and the sum of no terms is 0, those being the identity values for the multiplication and addition operators.

    0 讨论(0)
  • 2021-01-11 14:27

    Function every? implements the universal quantification.

    From (every? string? []) => false it would follow that [] contains an object x such that (string? x) => false (this is how negation of universal quantifier works). This leads to contradiction, so (every? string? []) must return true.

    0 讨论(0)
  • 2021-01-11 14:35

    Or better ask why should (every? string? []) yield false? Both variants lead to wrong answer in certain requirements. But obviously it's better than get an error on empty sequence.

    0 讨论(0)
  • 2021-01-11 14:37

    It is defined so in mathemathics an there is a good reason for that. It would be a consistency disaster if every? was defined any other way.

    With current definition the result of concatenation satisfies every? foo if and only if all concatenated collections also satisfy every? foo. Making every? return false on empty lists would break this convenient equivalence and a host of others (e.g. removal of an element would sometimes lead to switching every? from true to false.)

    0 讨论(0)
  • 2021-01-11 14:47

    My answer is vacuous truth.
    Wiki covers it well so I'll simply quote the current first paragraph:

    In mathematics and logic, a vacuous truth is a statement that asserts that all members of the empty set have a certain property. For example, the statement "all cell phones in the room are turned off" will be true whenever there are no cell phones in the room. In this case, the statement "all cell phones in the room are turned on" would also be vacuously true, as would the conjunction of the two: "all cell phones in the room are turned on and turned off".

    0 讨论(0)
提交回复
热议问题