Bit mapped Set Theory: foundations
==================================
There are two main ways to represent a set in a programming language:
- as a sorted array
- as a bit map
For example the set {2,7,5,1} = [1,2,5,7] = 10100110 , where the bits
are numbered from the right to the left. Let's consider the bimapped
way to represent sets more closely.
Inside a binary computer, the natural number 0 corresponds with a bit
map consisting of nothing but zeroes: 00000000 . But this is also the
bitmap of the empty set {} . Therefore 0 = {} . The natural number 1
corresponds with 00000001. Counting from the right, this is bit zero
set up, which corresponds to the set {0} . But 0 = {} hence 1 = {{}} .
The number 2 is 00000010 , which is the set {1} = {{{}}} = 2 . Number
3 is 00000011 , which is the set {0,1} = {{},{{}}} = 3 . Number 4 is
00000100 , which is the set {2} = {{{{}}}} = 4 . Number 5 is the set
00000101 , which is the set {0,2} = {{},{{{}}}} = 5 .
10100110 = 2^7 + 2^5 + 2^2 + 2^1 = 128 + 32 + 4 + 2 = 166 . Where in
turn: 7 = 2^2 + 2^1 + 2^0 , 5 = 2^2 + 2^0 , 2 = 2^1 , 1 = 2^0 . Hence:
166 = {1,2,5,7} = { {{}} , {{{}}} , {{},{{{}}}} , {{{{}}},{{}},{}} } .
It is noted that the commas do not add anything to the unambiguity of
the formulas and can be deleted: {{{}}{{{}}}{{}{{{}}}}{{{{}}}{{}}{}}}
Formation rules:
0. {} is a set (the empty set)
1. if A is a set and B are sets then AB are sets
2. if A are sets then {A} is a set
3. There are no other ways to form (sets or) a set
Implementation details. Assume a stack of strings:
0. Push the strings stack down and place '{}' on top
1. Concatenate next with top of stack and pop
2. Concatenate a left and a right bracket with top
Example: form the set { {} {{}} {{{}}} } . Blanks are for clarity.
A bar symbol | separates the strings being formed on the stack.
{} : rule (0)
{} | {} : rule (0)
{{}} | {} : rule (2)
{} | {{}} | {} : rule (0)
{{}} | {{}} | {} : rule (2)
{{{}}} | {{}} | {} : rule (2)
{{{}}} {{}} | {} : rule (1)
{{{}}} {{}} {} | : rule (1)
{ {{{}}} {{}} {} } | : rule (2)
The whole procedure can be represented as a string '002022112'. For all
non-trivial "task" strings, the leftmost "digit" is always a zero '0'.
And the rightmost digit is a two '2' if we want to obtain a single set.
Note: the operations '0' and '1' must such that the stack pointer can
never become negative. And the end result must be _one_ string on top,
which makes that the stack pointer equals 1 in the end.
Inverse problem. Can an arbitrary nested set of empty sets be written
as a natural number? E.g. {{{}},{{{}}},{{},{{{}}}},{{{{}}},{{}},{}}}
= {{0},{{0}},{0,{{0}}},{{{0}},{0},0}} = {1,{1},{0,{1}},{{1},1,0}} =
{1,2,{0,2},{2,1,0}} = {1,2,5,7} = 166 . The answer is: yes. Motivated
by the fact that the algorithm for converting a number to a set obeys
the above formation rules. (See program listing in subsequent poster)
The natural number corresponding with a set may be called the _numeral_
of a set. Two sets are _equal_ if they have the same numeral.
Any objections so far?
Next posters: algorithms, sample output.
Han de Bruijn