Next section focuses on mathematical tools for describing data — sets, relations, functions, …. These are familiar and very interrelated concepts that we'll delve into in a bit more detail.
set and element-of are undefined operations (just as point/line/plane in geometry; in fact "incident on a line" is really same as "element-of"); it all comes down to being able to answer, is a particular element in the set?
How do we describe sets in math and code?
(Aside: The On-Line Encyclopedia of Integer Sequences)
When writing a library for sets, what are options on representing the data, and how do they correspond to the previous ideas?
Some ideas to touch on:
Start with some (mostly?) familiar definitions…
A is a subset of B, A ⊆ B
A is equal to B, A=B
A is a proper subset of B, A ⊂ B
A is a non-trivial subset of B
Definition (1) is clearly correct -- the sets must have the same members. Definition (2) seems correct (esp. if you're familiar with it already).
Which translates to usable code better?
How to prove (1) and (2) are equivalent? …
Proof: We have to show two things:
Suppose def'n(1) holds, then show def'n(2) holds.
Recall def'n(1): x ∈ A iff x ∈ B. To show def'n(2), we need to show two things:
Show that A ⊆ B.
By def'n of ⊆, this means showing x ∈ A implies x ∈ B. This is indeed subsumed by the "only if" of (1).
Show that B ⊆ A.
By def'n of ⊆, this means showing x ∈ B implies x ∈ A. This is indeed subsumed by the "if" (1).
Now, suppose def'n(2) holds, then show def'n(1) holds.
Recall def'n(2): A ⊆ B, and B ⊆ A. To show def'n(1), we need to show two things:
Show that "x ∈ A if x ∈ B", equivalently "x ∈ B implies x ∈ A".
We are given that B ⊆ A, which (by def'n of ⊆) means that x ∈ B implies x ∈ A — yay!
Show that "x ∈ A only if x ∈ B", equivalently "x ∈ A implies x ∈ B".
We are given that A ⊆ B, which (by def'n of ⊆) means that x ∈ A implies x ∈ B — yippee!
Okay, this was written out in great detail, with constant reminders of what was being assumed and what was to be shown. But step back and observe the structure of the proof: showing two parts of "(1) equivalent to (2)" (each of which again further required an "if" and an "only if" direction.)
For our two representation approaches, how to define the following?
set=?
: Already discussed.
empty-set?
: Easy, given set=?
.
subset?
: ???
Many of these should be familiar already.
Note that union/or/addition all look similar; and likewise intersection/and/multiplication. Be careful about taking this too far though, e.g., it's generally not the case that (A-B)∪B = A !
On your own, consider how to code these for each of the set representations.
Consider the set with the constant-true indicator function:
(define (SuperU elt) true)
.
What set is this?
Note that not only are 4 and my car elements of SuperU,
but so is the entire set ℜ is in SuperU,
as well as the three-item set { ∅, black, ℜ }.
(That's fine to have sets that contain sets, just as lists can contain lists.)
Somewhat disturbingly though,
SuperU is itself contained in SuperU! A set containing itself?!
A bit dizzying, but we'll let it in the door.
Uh-oh, logician/philosopher Bertrand Russell (around 1900) sees SuperU, and senses he can do something wicked.
First, he suggests SuperB ("Bertrand"),
the set of all sets which which contains themselves.
The indicator function is even easy to code up:
(define (SuperB elt) (element-of? elt elt))
.
For instance, SuperU is one of the elements of SuperB.
(Think of "a book which lists all the books which mention their own title".)
This discomfits us, but we don't kick Bertrand out;
maybe if we just ignore him he'll go away and stop giving us a headache.
[Interesting: does SuperB contain itself?
Either answer is consistent,
but it tips us off that our set's "easy condition" doesn't
seem to fully specify the set.]
Not so lucky: encouraged by this success, Bertie cranks it up a notch,
and suggests the set SuperR, of all sets which don't contain themselves.
Again the indicator function is easy:
(define (SuperR elt) (not (element-of? elt elt)))
(Think of "a book which lists all the books which don't mention their own title".)
SuperR seems to include relatively normal elements and sets,
but our head is pounding more than ever.
It's too late, for Bertrand jumps up and cackles as he asks:
Does SuperR contain itself?"Well", we say, flustered: "Either it contains itself or it doesn't — after all that's all there is to sets: they either contain a given item or not. So, let's see…"
(element-of? SuperR SuperR)
, then
then by the def'n of SuperR, it wouldn't contain SuperR."
(not (element-of? SuperR SuperR))
is true,
then by the def'n of SuperR, it would contain SuperR."
Indeed, this was very disturbing news to mathematicians around 1900: the concepts of sets, which underlies all of mathematics, has a paradox! Within mathematics, there could be no worse possible catastrophe. This began a program to re-vamp set theory.
Russell's own approach: a "tiered" classifications sets :
sets of atomic elts, sets containing sets of elts, …
It can be finagled to work, but is very unsatisfying.
Used today, when a rigorous foundation needed: a formalization "Axiomatic set theory". But for our purposes, we'll just use "naive set theory" —
Yes, this is deeply connected to the Halting Problem, discussed at end of the course (though discovered 30 years apart).