Maths and Programs

Posted on 2012-10-01

Let’s solve this question (taken from the second assignment of Functional Programming in Scala):

Using forall, implement a function exists which tests whether a set contains at least one element for which the given predicate is true. Note that the functions forall and exists behave like the universal and existential quantifiers of first-order logic.

The code listing for forall is as follows:

where bound is 1000.

A set is a defined as follows, along with the predicate to test for containment.

Here is how I arrive at the answer for the question:

  1. From the question, it is intuitive to try to stop the iteration when there is an element that satisfies the predicate p, which is exactly what exists should do.
  2. The iteration is stopped only when the current element a does not satisfy p.
  3. However, we need to satisfy p in order for exists to correctly do its job.

In conclusion, we need a double negation here. In other words, we need to negate p and then negate the answer returned by forall when called with the negation of p.

From that reasoning, it is easy to code up the first attempt at a solution.

Now, let’s see how this solution fares.

The solution passes the tests. Let’s look at how to explain the code simply. The code says

There exists in set s an element that satisfies the predicate p when not all of the elements in s satisfies the negation of p.

That makes sense. But lo and behold, the code reflects a mathematical truth, and it even looks like it is translated verbatim from mathematical symbols to programmatical symbols!

∃x:P(x) ≡ ¬∀x:¬P(x)

compared with

The correspondence is remarkable!

After reading this post, you should try this question. The answer will pleasantly surprise you.

Finally, write a function map which transforms a given set into another one by applying to each of its element the given function. map has the following signature:

comments powered by Disqus