Gödel, Artificial Intelligence, and Confusion

Sentient software is the hot topic as of late. Speculative news about Artificial Intelligence (AI) s

Sentient software is the hot topic as of late. Speculative news about Artificial Intelligence (AI) systems such as Watson, Alexa, and even autonomous vehicles are dominating social media. It’s feasible that this impression is nothing more than Baader-Meinhof phenomenon (AKA frequency illusion). However, it seems that the populace has genuine interest in AI. Questions abound. Are there limits? Is it possible to create a factitious soul? Gödel’s incompleteness theorem is at the core of these questions; however, the conclusions are cryptic and often misunderstood.

Gödel’s incompleteness theorem is frequently adduced as proof of antithetical concepts. For instance, Roger Penrose’s book Shadows of the Mind claims that the theorem disproves the possibility of sentient machines (Penrose, 1994, p. 65). Douglas Hofstadter asserts the opposite in his book, I Am Strange Loop (Hofstadter, 2007). This article aims to provide a cursory view of the theorem in laymen’s terms and elucidate its practical implications on AI.

Context

Gödel’s Incompleteness Theorem is best understood within its historical context. This section covers requite concepts and notable events to provide the reader with adequate background knowledge. This is not meant to be comprehensive coverage of the material: rather it is stripped down to essentials.

The Challenge

The mathematics community was never filled with more hope than at the turn of the twentieth century. On August 8th, 1900, David Hilbert gave his seminal address at the Second International Congress of Mathematics in which he declared, “in mathematics there is no ignorabimus” (Petzold, 2008, p. 40). Ignorabimus is a Latin word meaning “we shall not know”. Hilbert believed that, unlike some other branches of science, all things mathematical were knowable. Furthermore, he framed a plan to actualize a mathematical panacea.

In this address, Hilbert outlined ten open problems and challenged the mathematics community to solve them (this was a subset of twenty-three problems published by Hilbert). The problem of relevance for this article is the second which is entitled, The Computability of Arithmetical Axioms. Hilbert’s second problem called for the axiomatization of real numbers “to prove that there are no contradictory, this is, that a finite number of logical steps based upon them can never lead to contradictory results” (Petzold, 2008, p. 41). More concisely, Hilbert wished to axiomatize number theory.

The following sections delve into axiomatization. However, a pertinent idea here is the phrase “finite number of logical steps”. In modern nomenclature, this is known as algorithmic. Hilbert, along with his contemporaries, believed that every mathematical problem was solvable via an algorithmic process. (Petzold, 2008) This is a key concept that will be revisited after exploring axiomatization.

Axiomatization

Stated concisely, axiomatization is a means of deriving a system’s theorems by logical inferences based on a set of axioms. Axioms are unprovable rules that are self-evidently true. The most well-known axiomatized system is Euclidean geometry; therefore, it serves as an archetype for understanding axiomatic systems. The whole of Euclidean geometry is based on five axioms.

  1. A straight-line segment can be drawn joining any two points.
  2. Any straight-line segment can be extended indefinitely in a straight line.
  3. Given any straight-line segment, a circle can be drawn having the segment as radius and one endpoint as center.
  4. All right angles are congruent.
  5. If two lines are drawn which intersect a third in such a way that the sum of the inner angles on one side is less than two right angles, then the two lines inevitably must intersect each other on that side if extended far enough.

(Wolfram Research, Inc., 2017)

As a small aside, the fifth axiom is also known as the parallel postulate. This has the been the subject of mathematical quandary for centuries. It is highly recommended that the enthusiastic reader perform additional research on the subject.

These five axioms form the foundation of geometry. Pythagorean theorem, Pons Asinorum, Congruence of triangles, Thales' theorem, and countless others are derived via logical inferences based on the assumption that these self-evidentiary axioms are true. Axioms provide a solid foundation for a system, much like the cornerstone of a building.

Another key concept introduced in the previous paragraph is logical inferences. It’s not enough to have a firm foundation of axioms. Theorems derived from the axioms must be likewise sound and logical inference offers a guarantee of said soundness.

Logical Inference

The process of connecting axioms to theorems cannot rely on intuition in any way. This is to say that they are definitive rules and constructs in which logical inference can be validated. This is important because the legitimacy of axioms is irrelevant if conclusions drawn from them are not completely consistent. A strong, stable, and trusted system must be composed of theorems that use valid logical inferences stemming from axioms.

It is beyond the scope of this blog post to give even a cursory explanation of logical systems of inference. However, it’s important for the reader to understand that formal logic has stringent rules and notations much like any mathematical system. Logic statements are written and manipulated like any other mathematical formulas. This allows for the creation of proofs that cement the validity from the bottom up.

Each theorem is analogous to a brick in a house. Because the theorem sits firmly on either an axiom or another theorem planted on an axiom, it’s validity is confirmed. This is commonly known as infinite regress. All the theorems taken together form a strong and stable system capable of being trusted. Formalism expands on the concept.

Formalism

Recall the Computability of Arithmetical Axioms problem outlined in The Challenge section. Hilbert envisioned Formalism as the solution to this problem. Formalism, as conceived by Hilbert, is a “system comprised of definitions, axioms, and rules for constructing theorems from the axioms” (Petzold, 2008, p. 45). It is often described as a sort of metamathematics. Hilbert envisioned a formal logic language where axioms are represented as strings and theorems are derived by an algorithmic process. These concepts were introduced in the previous two chapters. A new concept to this section is the qualities that such a system must possess.

For a system, such as formalism, to truly axiomatize the whole of arithmetic, it must have four qualities which are outlined below.

  • Independence – There are no superfluous axioms.
  • Decidability – A algorithmic process for deriving the validity of formulas.
  • Consistency – It is NOT possible to derive two theorems that contradict one another.
  • Completeness – Ability to derive ALL true formulas from the axioms.

(Petzold, 2008, p. 46)

As a small aside, there is a fair bit of legerdemain happening here. The concepts of truth, formulas, theorems, and proof are purposely glossed over to avoid minutia. Curious readers are encouraged to investigate further.

The two qualities that are particularly cogent to Gödel’s incompleteness theorem are consistency and completeness. Luckily, they are both self-explanatory. A system that is both complete and consistent will yield all possible true formulas, none of which are contradictory.

Why?

The truth is that axiomatization is a fastidious process that can seem maddingly pedantic. One may be forced to question the very premise that it is a good thing. One can further postulate that simple human intuition is sufficient. However, recall the concept of infinite regress called out in the last paragraph of the Logical Inference section. New theorems are built upon existing theorems. Without stringent formal logic rules, systems become a “house of cards”. Mistakes found in foundational theorems can bring the entire system crashing down.

An archetypal example is Cantor’s set theory. The details of the theory are largely irrelevant to this line of inquiry, but the curious reader should refer to this set of blog posts for more information. In short, set theory took the mathematical world by storm. Countless mathematicians augmented it by building new abstractions on top of it. Bertrand Russel discovered a fatal flaw known as Russel’s Paradox which brought the system down like a proverbial “house of cards”. Formalism is meant to avoid similar debacles.

Principia Mathematica

The Principia Mathematica is an infamous three-volume treatise by Alfred North Whitehead and Bertrand Russell published in 1910, 1912, and 1913. It is a truly herculean attempt to formalize the whole of arithmetic. The work is dense and inaccessible to even most mathematicians (Nagel & Newman, 2001). The system set forth sets the stage for Gödel’s incompleteness theorem.

Incompleteness Theorem

In 1931, Kurt Gödel published a seminal, albeit recondite, paper entitled On Formally Undecidable Propositions of Principia Mathematica and Related Systems. The paper dismayed the whole of the mathematical community despite its esoteric content. It not only trampled the validity of Principia Mathematica, it proved that such a system isn’t achievable by any means. The implication being that Hilbert’s second problem, The Computability of Arithmetical Axioms, will never have a satisfactory solution.

In short, Gödel proved that any system complex enough to encompass simple arithmetic cannot be both complete and consistent as defined in the Formalism section. Through a clever method of converting logical expressions to numbers, the proof showed that any such system will enable the creation of a self-referential statement in the form of “this statement is false”.

The previous paragraph is a blatant over-simplification of Gödel’s incompleteness theorem. The intimate details of the proof are well beyond the scope of this humble article. As mentioned so many times throughout this work, the reader is encouraged to continue research independently. On a positive note, the arcane details are not requisite for comprehension of the implications.

Implications

In short, the implications of Gödel’s Incompleteness Theorem are nothing more than that an axiomatic system of logic cannot be both complete and consistent. Expanding on that, it is not possible to derive an algorithm that will generate all possible proofs of a formalized system. One can then infer that it is not possible to write a computer program to generate said proofs.

There have been countless extrapolations based on the implications stated above. For instance, a commonly adduced argument is that there are more truths in the universe than there are proofs. Likewise, there are some things that are obviously true that cannot be formally proven. While these are both true, be careful not to fall into the enticing trap of applying the rule to anything outside of axiomatic systems of logic.

Why the Confusion?

Although it’s a rather unsatisfying observation, the reality is that Gödel’s proofs are onerous to all but accomplished logicians. Despite this, the implications are far reaching. This situation creates a particularly fertile breeding ground for misconceptions. Many venerated experts within other disciplines attempt to apply the theorem by fallacious means.

A cursory Google search for “Gödel’s incompleteness theorem and God” will yield seemingly boundless results with varied interpretations. The fact of the matter is, the theorem strictly applies to formal axiomatic systems of logic. It does not apply to religious texts. Likewise, it has no implications on the validity of the afterlife or mystical intuition. (Tieszen, 2017, p. Kindle Loc. 1173)

As an example, Gödel’s ontological argument is often cited by theists because it formally proves the existence of God. Given the description, it is easy to see how someone ignorant of formal logical proofs could draw fallacious conclusions. As stated previously, Gödel’s proofs apply exclusively to formal axiomatic systems of logic. The concept of God is far from this. Gödel himself said that “it was undertaken as a purely logical investigation, to demonstrate that such a proof could be carried out on the basis of accepted principals of formal logic” (Tieszen, 2017, p. Kindle Loc. 2158). He also hesitated to publish “for fear that a belief in God might be ascribed to him” (Tieszen, 2017, p. Kindle Loc. 2158).

The cogent point is that it is easy to misinterpret the significance of Gödel’s work. It is difficult for anyone lacking a strong background in mathematical logic to draw valid conclusions based on the incompleteness theorem. Gödel’s work is best confined to scientific contexts.

Implications for Artificial Intelligence

The thesis of this work is to define the implications of Gödel’s incompleteness theorem on AI. Unfortunately, a surfeit of background concepts is requisite to comprehension and the author humbly apologizes for the necessary discomfort. Possibly more disappointing is that the verdict is not as definitive as one may suppose as this section explains.

One thing is definite, it is not possible to use a computer to automatically derive proofs from an axiomatic system. Hilbert’s dream of automated formalization is inert. On the bright side, if it were many mathematicians would be out of work. Some claim, as does Roger Penrose, that this necessarily precludes any possibility of AI within the current computational model. Consider this, a human can necessarily comprehend some truths that a machine cannot. The insinuation is that humans are endowed with creativity that is not obtainable by a machine. Mr. Penrose postulates that this is a quantum effect that is beyond our current understanding. (Penrose, 1994)

Douglas Hofstadter passionately refutes Roger Penrose’s claims. He believes that the said limits stem from a fundamental misunderstanding of how the brain works and presents a compelling model of consciousness in his book, I Am Strange Loop (Hofstadter, 2007). Theorem proving is by no means the only way to make a machine “think”. “The human mind is fundamentally not a logic engine but an analogy engine, a learning engine, a guessing engine, and esthetics-driven engine, a self-correcting engine” (Nagel & Newman, 2001, p. Kindle Loc. 146). From this frame of reference, Gödel’s incompleteness theorem doesn’t apply to AI.

Penrose and Hofstadter sit among varied experts with similar opinions. With the considerable amount of resources funneled into AI projects, the final verdict will be decided in due course of time. Not that this should sway the reader in any way, but the author tends to side with Mr. Hofstadter. The reader is encouraged to do their own research and form their own opinions.

Conclusion

Gödel’s incompleteness theorem is inextricably associated with philosophy, religion, and the viability of Artificial Intelligence (AI). However, Gödel’s work is in a recondite field and its applicability beyond axiomatic systems of logic is perplexing and often misapplied. In the final analysis, the theorem’s only definitive assertion is that it is not possible for an axiomatic system of logic to be both consistent and complete. Many experts make conflicting ancillary claims and it’s difficult to draw any absolute conclusions.

This article presents a simplistic high-level view of Gödel’s incompleteness theorem aimed at the novice with limited exposure. It is highly recommended that readers use this as a starting point for much deeper exploration. The books listed in the bibliography are all excellent references for further research.

Biography

Hofstadter, D. (2007). I Am A Strange Loop. Retrieved 8 27, 2017

Nagel, E., & Newman, J. R. (2001). Gödel's Proof: Edited and with a New Foreword by Douglas R. Hofstadter. (D. Hofstadter, Ed.) New York University Press, NY. Retrieved 8 27, 2017

Penrose, R. (1994). Shadows of the Mind. Oxford University Press p. 413. Retrieved 8 27, 2017

Petzold, C. (2008). The Annotated Turing. Indianapolis: Wiley Publishing, Inc.

Tieszen, R. (2017). Simply Gödel. New York: Simply Charly.

Wolfram Research, Inc. (2017, October 30). Euclid's Postulates. Retrieved from Wolfram Math World: http://mathworld.wolfram.com/EuclidsPostulates.html

Diagonalization?

The goal of this article is to provide laymen with a conceptual understanding of diagonalization. Th

The goal of this article is to provide laymen with a conceptual understanding of diagonalization. Those interested in a deep dive full of mathematical jargon will be sorely disappointed. However, this piece is the perfect resource for a general understanding of the topic devoid of the more arcane details. Unlike the majority of my writing, this is not directly applicable to the daily responsibilities of software professionals. It is purely an endeavor to satisfy intellectual curiosity.

Why?

The impetus for this writing comes from a colleague who contacted me after reading my blog series on Set Theory (Set Theory Defined, Set Operations, When Sets Collide). The posts made pithy mention of Cantor’s diagonalization proof with implications on infinite cardinality. My friend’s search for a concise explanation proved to be unfruitful. The conversation naturally progressed toward Alan Turing’s seminal paper: On Computable Numbers, which also employs a diagonalization proof. Cantor and Turing both played a major part in shaping computer science. Therefore, although it is not likely that the majority of software professionals will ever employ diagonalization, it’s a crucial part of computing history.

What Are We Trying to Prove?

Diagonalization is a mathematical proof demonstrating that there are certain numbers that cannot be enumerated. Stated differently, there are numbers that cannot be listed sequentially. Consider all the numbers on the number line as shown in Figure One – Number Line.

4-1

First consider the set of positive whole numbers including zero. These are known as natural or counting numbers and are denoted as `\mathbb{N}`. Most kindergarten curriculum teaches how to enumerate this set: starting with zero add one to the current number to get the next number ad infinitum.

Adding negative numbers to `\mathbb{N}` produces the set of integers denoted by `\mathbb{Z}`. Again, this set is also easy to enumerate by simply listing it as follows: `0, 1, -1, 2, -2, 3, -3, …`.

Now consider expanding on `\mathbb{Z}` by adding fractions to create the set of rational number denoted as `\mathbb{Q}`. The term rational signifies that a number can be expressed as a ratio such as `1/2` or `23/345`. These numbers fit between the whole number on the number line and there is an infinite amount of fractional numbers between each set of natural numbers. That is to say, regardless of the location of two rationals on the number line, it’s always possible to find another number between them. With some ingenuity, these numbers can also be enumerated in several different ways. Enumerating rational numbers, while fascinating, is beyond the scope of this post. The reader is encouraged to either just accept my word as fact or do research. Here is a good place to start.

Although it seems as if we’ve run out room on the number line, that isn’t actually the fact. There is another class of number that has been baffling mathematicians throughout the ages: irrational. It’s a bit perplexing, but irrationals fit between rationals on the number line (no matter how many times I think about that, it amazes me). Grade school curriculum typically introduces the concept with renowned numbers such as `\pi` or `e`. These are numbers that cannot be expressed as a ratio. The decimal representation consists of an infinite series of digits with no repeating pattern. Any calculations involving irrationals are approximations because it’s impossible to express them in a finite context. Adding these to `\mathbb{Q}` produces the set of real numbers denoted as `\mathbb{R}`. Irrational numbers are the target of our inquisition.

As a matter of note, the set of irrational numbers can be further divided into the sets of algebraic and transcendental numbers. Algebraic numbers can in fact be enumerated. However, this is a bit of minutia that isn’t really necessary for understanding diagonalization. Once again, the curious reader is encouraged to rely on Google for further inquiry.

The question is, how is it possible to prove that irrational numbers are not enumerable. With an understanding of the problem, we can turn our attention to the solution which is diagonalization.

Reductio Ad Absurdum

Diagonalization is a type of proof known as reductio ad absurdum which is Latin for reduction to absurdity. It is common amongst mathematicians and philosophers alike. The premise is to first assume a proposition is true and then disprove it via deductive reasoning thus reducing it to an absurd conclusion.

One popular example of a reductio ad absurdum proof is that there is no smallest fractional number. Assume there is such a number: it can be divided by two to create a smaller number. Therefore, the original assumption is absurd. Another illustration is an alibi. First assume the suspect committed the crime. If the accused is known to be at a different location when the crime took place, it’s absurd to assume that they were also at the scene of the crime.

Diagonalization

Having addressed all the introductory trivialities, it’s time to get to the point. The diagonalization proof is as follows. First assume that it is possible to enumerate all irrational numbers. If this is true, it should be impossible to devise a number that is not included in this list. Examine Figure Two – Diagonalization and stretch the mind to imagine that this is in fact the list of all irrational numbers: the list is infinitely long and each number expands on endlessly. Next, draw a diagonal line down the center of the list and write down the resulting infinite number. In this case, the number is `0.13579135…`. Next add 1 to each digit expect in the case of nine which becomes a zero. This results is the number `0.24680246…`. Is this number contained in the list? It’s obviously not the first number because the first digit does not match. The same holds true for the second number because the second digit has to be different. Continue this line of logic for every number and it’s obvious that the devised number is not in the list. The reader should take a few minutes to let that sink in.

4-2

Keep in mind, this is purely a thought experiment. Obviously, Figure Two – Diagonalization is not an infinite list and each number is not truly irrational. It’s impossible to construct such a list in a finite context. However, the line of logic holds true.

It is common to wonder why diagonalization does not apply to `\mathbb{Q}`. The concise answer is that those numbers have finite digits and irrationals do not.

Implications

Accepting that the diagonalization proof is valid, it has some profound implications. At first glance, it’s difficult to understand how the fact that it’s impossible to enumerate irrational numbers has bearing on the world in any way. However, many people have derived some amazing conclusions. Cantor showed that there are in fact multiple infinities. Turing used diagonalization to prove the limits of computability. It’s even been employed by philosophers to prove that there are an insufficient number of proofs to prove all the truths in the universe. More concisely, some truths are unproveable. The implications lead down an exceedingly dark and deep rabbit hole.

Conclusion

Diagonalization is a reductio ad absurdum proof that demonstrates the impossibility of enumerating irrational numbers. It is relatively easy for non-mathematicians to understand. While only tangentially related to software engineering, it’s a fascinating concept that sheds light on the foundations of computing and indeed the world.

As always, thank you for taking the time to read this article. Please feel free to contact me with any questions or concerns.

Just Enough Set Theory – When Sets Collide (Part 3 of 3)

Welcome to the final installment of this three-part series on set theory. The first piece, Set Theory Defined, detailed requisite foundational knowledge. The second article, Set Operations, outlined some beneficial set algorithms. This post develops the concepts laid out in the first two; therefore, it is highly recommended that readers begin there.

Individual sets have many useful properties; however, preforming operations on multiple sets provides even greater utility. This piece outlines four such operations. Each operation provides a concise means for addressing common programming problems that virtually all software professionals encounter. There is a brief description of each from a mathematical perspective followed by JavaScript (ES6) code excerpts demonstrating how to apply theory to real world scenarios.

NOTE: All code samples are written in ES6 and are therefore not likely to execute directly in a browser. The best option is to use Node or transpile the excerpts using either Babel or TypeScript. The working code is available on GitHub along with execution instructions.

Union

The union of two sets is a set containing the distinct elements from both sets. `\cup` is the mathematical symbol for a union and the union of sets `A` and `B` is denoted as `A \cup B`. An expanded way of representing the union relationship is `\{x| x \in A \vee x \in B\}`, which means every element contained in `A` OR (`\vee`) `B`. Figure One – Union depicts two sets with three elements each. The union is a set with five elements because one item, three, is shared and union returns distinct values. The Venn diagram shows the relationship graphically.

figure3-1

Generating the union of two sets is quite easy in ES6 as the code below illustrates.

const A = new Set([1, 2, 3]);
const B = new Set([3, 4, 5]);
const union = new Set([...A, ...B]);
// union = [1,2,3,4,5];

The astute reader will notice that there’s some legerdemain afoot. The code above uses the ES6 Set data structure instead of standard JavaScript arrays. Set holds only unique elements by ignoring add operations for new values that match existing ones. The algorithm is as easy as concatenating the two sets without the concern of distinct elements. If the code was using standard arrays, there would have to be logic to remove duplicated items. Luckily, converting between sets and arrays is virtually effortless.

const setDataStructure = new Set([1, 2, 3]);
const arrayDataStrcture = Array.from(setDataStructure);

The problem with the code above is that it’s a rare requirement to union sets containing primitive values. Software engineering is seldom that straightforward. A more realistic scenario is calculating the union between two sets of complex objects where equality becomes problematic. Unlike primitive variables, objects with identical values are not equal because they compare by reference. This abrogates the Set trick from earlier. Suppose the requirement is to compute all bug reports currently in process across two teams and it’s possible that both teams are working on the same bugs simultaneously. The code below demonstrates a solution by first concatenating the two sets and then removing duplicates using the filter method introduced in the last article. Notice the only equality check is via the Id. Obviously, this won’t work for every scenario and depending on the size of the sets and performance requirements it is possible to write generic deep equality methods (or use a library like underscore).

const teamABugs = [
    { id: 1, name: "Screen Explodes" },
    { id: 2, name: "Keyboard Burts into Flames" },
    { id: 3, name: "Submit button off by 1 pixel" }];
const teamBBugs = [
    { id: 5, name: "Randomly Dials Russian Hackers" },
    { id: 6, name: "Publishes CC info to the www" },
    { id: 3, name: "Submit button off by 1 pixel" }];

const union = [...teamABugs, ...teamBBugs]
    .filter((x, index, array) => array.findIndex(y => y.id == x.id) == index);

Intersection

The intersection of two sets is a set containing distinct shared elements. `A \cap B` is the mathematical representation of a union and the expanded notation is `\{x|x \in A \wedge x \in B \}`. Stated differently, the intersection of set `A` AND (`\wedge`) `B` is every element contained in `A` AND `B`. Figure Two – Intersection depicts the relationship showing the union of `A` and `B` to be a singleton set containing only the number three. Once again, the Venn diagram portrays the relationship.

figure3-2

Much like union, finding the intersection of two sets using the Set data structure and primitive types is easy. The code below shows how it’s a matter of using the filter method to check to see if an item is also stored in the other set.

const A = new Set([1, 2, 3]);
const B = new Set([3, 4, 5]);
const intersect = [...A].filter(x => B.has(x));
// intersect = [3];

The code above is a bit fanciful. Consider instead a role protected resource. Possessing any one of many roles allows users to access said resource. Users each have a set of associated roles. There are a few different ways to achieve this, but finding the intersection between the user’s roles and the resource’s required roles is the most manageable. See the code below.

const resourceRoles = [
    { id: 1, name: "Administrator" },
    { id: 2, name: "Super User" }];
const user =  { id: 314, name: "Edsger Dijkstra", roles: [
    { id: 1, name: "Administrator" }, 
    { id: 2, name: "User" }] }

const hasAccess = resourceRoles
    .filter(x => user.roles.find(y => y.name == x.name)).length > 0;

All of the caveats about equality described in the Union section also apply here. It’s something programmers need to be cognizant of.

Difference

The difference of two sets is sometimes known as the relative complement; both nomenclatures are interchangeable. The concept is simple, the difference is a set made up of the items that are left over after removing the intersection of another set. Otherwise stated, all of the items in set `B` that do not exist in set `A`. Mathematically, this is represented as `\{x|x \in B \wedge x \notin A\}` or the shortened version which is `B\\A`. Figure Three – Difference shows the difference between `B` and `A` to be a set containing four and five. Just as above, there is a representative Venn diagram.

figure3-3

As an aside, there is also an absolute compliment which is somewhat similar; however, it is outside the scope of this article.

Finding the difference of sets is almost identical to finding the intersection as the code below demonstrates. The only variation is that the predicate passed to the filter method is negated.

const A = new Set([1, 2, 3]);
const B = new Set([3, 4, 5]);
const difference = [...B].filter(x => !A.has(x));
// difference = [4,5];

Again, a more realistic example is in order. Image that there is a set of actions that must be completed and a set of actions a user has completed. Finding the difference is an easy way to determine if all required actions are complete.

const requiredActions = [
    { id: 1, name: "Electronic Signing" },
    { id: 2, name: "Submission Form" },
    { id: 3, name: "Payment" }];
const userActions = [
    { id: 1, name: "Electronic Signing" },
    { id: 2, name: "Submission Form" }];

const complete = requiredActions
    .filter(x => !userActions.find(y => y.name == x.name)).length === 0;
// complete = false

Cartesian Product

The Cartesian product of two sets is a set of ordered pairs that contain all possible combinations of elements in the two sets. The mathematical representation is `A \times B`. The expanded notation is `\{(a,b)|a \in A \wedge b \in B\}` which means an ordered pair consisting of every element in `A` AND (`\wedge`) every element in `B`. Figure Four – Cartesian Product demonstrates the concept. As a matter of importance, unlike standard products, the Cartesian product is not commutative. Stated mathematically, `A \times B \ne B \times A`. Switching the order of statement will change the order of the pairs.

figure3-4

The Cartesian product is useful for combinatorics problems. A common example is simulating a deck of cards. Instead of specifying all the cards explicitly in code, it’s easier to define the suits and values as two separate sets and then take the Cartesian product to get the entire deck. See the code below.

const suits = ['Diamond', 'Spade', 'Heart', 'Club'];
const values = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'];

const cards = suits.reduce((acc, x) => [...acc, ...values.map(y => [x, y])], []);
// Alternatively, it’s possible to return the ordered pair as an object instead of an array
// const cards = suits.reduce((acc, x) => [...acc, ...values.map(y => { return { suit: x, value: y } })], []);

This code should be starting to look familiar because all the samples make heavy use of the map, reduce, and filter methods. Using ES6, these methods have great utility for mimicking mathematical set operations. Because the code above is similar to previous examples, it doesn’t require further explanation.

Why Stop at Two?

Up to this point, all the exhibited set operations employ two sets. However, this is for the sake of brevity. Each operations can act on as many sets as required. For instance, `A \cup B \cup C` is perfectly valid as is `A \times B \times C`. The enthused reader should solidify his/her learning by expanding each code sample to use additional sets.

Real World Applications

This series demonstrated how set theory is applied to data structures and demonstrated some novel uses for set operations in order create efficient algorithms. However, this is only a meager representation of all the many and varied applications for software engineering. Relational databases make heavy use of set theory for defining data structure and constructing data queries. In fact, SQL is essentially a set notation. There are several instances in language theory and design where strings are realized as sets and set operations are performed on them. Another prolific use is in computer graphics where points on a plane are treated as sets. The list of applications is considerable. It’s a body of knowledge that no software professional should forsake.

Conclusion

Thus concludes this three-part series on set theory. Hopefully, the reader has gained a high-level understanding as well as enough practical knowledge to apply the learning forthwith. The first article outlined the basics and introduced the concept of set mapping. Empty sets, cardinality, subsets, summation, and power sets were introduced in the second piece. Finally, this post presented operations involving more than one set including unions, intersections, differences, and Cartesian products. The method was to first introduce the ideas mathematically and then demonstrate how to apply them using ES6. These concepts should not be considered optional for software professionals because set theory is ubiquitous in computer science.

As always, thank you for reading and please feel free to contact me with questions. I’m also happy to create more in depth posts upon request.

Just Enough Set Theory – Set Operations (Part 2 of 3)

Welcome to the second installment of this three-part series on set theory. The first piece, Set Theory Defined (recently updated with code samples), detailed requisite foundational knowledge. It is highly recommended that readers begin there if they haven’t already.

The first piece in this series introduced sets and exhibited how ES6 arrays are analogous to them. It also depicted how to transform, or map, a set into a related set. This post expands on set theory by probing into set operations.

NOTE: All code samples are written in ES6 and are therefore not likely to execute directly in a browser. The best option is to use Node or transpile the excerpts using either Babel or TypeScript. The working code is available on GitHub along with execution instructions.

Empty Sets

Empty sets are a rather mundane topic, but nonetheless worth mentioning. As the name implies, they are simply sets that have no elements. They are also commonly referred to as null sets. Mathematically, empty sets are represented as either `\emptyset` or `{}`. The concept relates to empty arrays in software.

Cardinality

The term cardinality sounds impressive; however, it’s simply the number of elements in a set. The mathematical representation of a set with three elements is as depicted in Figure One – Cardinality.

figure2-1

In JavaScript, the cardinality of an array is its length. See the code below.

const someSet = [1, 2, 3, 4, 5]; 
const cardinality = someSet.length; 
// cardinality = 5

Subsets

Subsets are relatively easy to explain, yet have far reaching implications. A subset is a portion of a larger set. For instance, consider the set of all animals (`A` ). The set of all dogs (`D` ) is a subset of the animal set because although every animal is not a dog, every dog is an animal. The mathematical notation for subsets is as follows: `D\subseteqA`. Another way of mathematically expressing the subset relationship is `\forall x(x\inD->x\inA)`. That looks absurd, but the premise is that for any (`\forall`) element (`x`) in `D` , it is implied (`->`) that the element (`x`) also exists in `A`.

figure2-2

Subsets are often taught with Venn Diagrams. See Figure Three – Venn Diagrams for an example. Admittedly, this account of subsets is a bit prosaic. However, the final post in this series relies heavily on the concept so it bears belaboring the point.

figure2-3

ES6 has a built-in filter method on the array object that enables easy access to subsets. Filter takes a predicate as an argument. Recall from the first article that a predicate is a function that takes a single argument and returns a Boolean response. The filter method applies the predicate to each item in a set and creates a new set that includes the items where the predicate returned true. See the code below.

const animals = [
    {name: "Tom", type: "Cat"},
    {name: "Jerry", type: "Mouse"},
    {name: "Pluto", type: "Dog"},
    {name: "Scooby Doo", type: "Dog"}];

const dogs = animals.filter(a => a.type == "Dog");
// dogs = [{name: "Pluto", type: "Dog"}, {name: "Scooby Doo", type: "Dog"}]

Summation

The term summation is a bit misleading because it implies simply adding elements together, however it’s a more powerful concept. Summation applies a function to each element of a set reducing it to a single value. `‎\sum_{x \in S}f(x)‎‎` is the mathematical notation representing the algorithm where `S` can be any set and `f(x)` can be any function. Consider Figure Four – Summation. Given the set `A`, each element in the set is multiplied by two and added together.

figure2-4

ES6’s reduce method of the array object is comparable to summation. Aptly named, reduce applies a function to each member of a set reducing it to a single value. It accepts two arguments: a function and an optional starting value. The function accepts an accumulated value and the current item. The state of the accumulated value after all items are processed is the final return value. The code below is the same process detailed in Figure Four – Summation.

const someSet = [1, 2, 3];
const sum = someSet.reduce((acc, x) => acc + x * 2, 0);
// sum = 12

Reduce is useful for many operations beyond mathematical functions. The code below utilizes it to extract email addresses from a set of users.

const users = [
    {id: 1, email: "email@email.com"},
    {id: 2, email: "email2@email2.com"},
    {id: 3, email: "email3@email.com"}];

const emails = users.map(u => u.email).reduce((acc, x) => `${acc};${x}`);
// emails = "email@email.com;email2@email2.com;email3@email.com"

This above doesn’t do the reduce method proper justice because its efficacy is virtually endless. There are many more options that are outside the scope of this feature. The reader is highly encouraged to find more information on Mozilla’s excellent JavaScript reference.

Power Set

Power sets are something every programmer has to deal with at some point in his/her career, even if they can’t formally identify them by name. In mathematical parlance, power sets are denoted as `P(A)`. A power set is the set of all subsets including the empty set and itself: more succinctly, all possible set combinations. A power set always contains `2^n` elements where `n` is the cardinality of the original set (`|P(A)|=2^(|A|)`).

Power sets are difficult to conceptualize without an example. Figure Five – Power Set depicts a set with three elements. The power set is all possible combinations of the three elements. The result is a set with a cardinality of eight (`2^3`).

figure2-5

Unfortunately, there isn’t an innate JavaScript method for creating power sets. However, that’s an easy problem to overcome given some ingenuity. See the code below.

const someSet = [0, 1, 2];
const powerSet = someSet.reduce((acc, x) => [...acc, ...acc.map(y => [x, ...y])], [[]]);
// powerSet = [[], [0], [1], [1,0], [2], [2,0], [2,1], [2,1,0]]

The code above is a bit intimidating at first glance so it merits additional explanation. The power set always contains an empty set, so the second argument to the reduce method is a set that contains nothing but that. This is the starting value. When the function acts on the first item in the set, the value of acc is [[]] and the value of x is 0. The result of concatenating the current item to each item in acc is concatenated on to the value of acc making it [[], [0]]. The same algorithm is applied to each item in the set. This is difficult to envisage, so the code below details essentially what happens upon invocation.

const ps = (acc, x) => [...acc, ...acc.map(y => [x, ...y])]; 

// First element
let acc = ps([[]], 0);
// acc = [[], [0]]

// Second element
acc = ps(acc, 1);
// acc = [[], [0], [1], [1,0]]

// Third element
acc = ps(acc, 2);
// acc = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]]

The reader is highly encouraged to review this section multiple times until the concept solidifies.

Conclusion

The post outlined a few useful set operations. ES6 uses the reduce method to apply the concept of summation to sets. A power set is a set of all possible set combinations. Although there is no built in ES6 functionality for this, it’s an easy algorithm to create. Make sure to come back for the final post entitled When Sets Collide. It is by far the most useful in the series covering set operations that act on multiple individual sets.

Just Enough Set Theory – Set Theory Defined (Part 1 of 3)

Set theory is incredibly intuitive and has many practical applications in software engineering. In f

Set theory is incredibly intuitive and has many practical applications in software engineering. In fact, any professional programmer without an understanding is at a disadvantage. Unfortunately, many in the industry relegate it to the purview of mathematicians. This is understandable because most material on the subject delineates set theory with first order logic as a basis for math. The good news is that it doesn’t have to be this way. As this series demonstrates, it is accessible to anyone regardless of background.

The three articles in this series aim to introduce set theory, expound upon set operations, and demonstrate the learning using JavaScript (ES6). The goal is to provide the reader with actionable knowledge to improve his/her software skills without a surfeit of superfluous details. This first installment describes the theory in order to provide a firm foundation for future practical application.

NOTE: All code samples are written in ES6 and are therefore not likely to execute directly in a browser. The best option is to use Node or transpile the excerpts using either Babel or TypeScript. The working code is available on GitHub along with execution instructions.

What is Set Theory

The inception of set theory dates back to the nineteenth century with Georg Cantor. On the surface, it’s brilliantly simple. A set is simply a collection of unordered objects. In mathematical parlance, objects contained in a set are known as members or elements. An element can be literally anything, including another set. Sets are typically depicted as objects inside curly braces and are denoted by capital letters. For instance, `A= { 1,2,3 }` is the mathematical representation of the set `A` with the members `1`, `2`, and `3`. Set membership is signified as: `1\inA`. Figure One – Sets illustrates these symbols.

figure1

Set theory relies on FOPL (First Order Predicate Logic) to construct sets. Expanding on the definition above, sets are a collection of objects that satisfy a predicate. A predicate is a function that accepts a single argument and returns a Boolean (true or false) value. For instance, the set of all dogs has the predicate `IsDog(n)` In other words, elements of a set share some arbitrary property. FOPL is fascinating, but not particularly relevant to this article. A general acumen of predicates is sufficient for comprehension of this material. A cursory web search for First Order Logic will present sufficient resources for the curious reader.

Set Mapping

There are a few interesting operations that can be performed on sets, most of which are covered in the next installment. However, mapping from one set to another is germane to a foundational understanding of set theory. A set is transformed, or mapped, into another related set via the use of a function.

A mathematical function is analogous to a software function with added constraints. They are similar in that they accept an input and return an output. The difference is that a mathematical function can only accept a single input, must return an output, are determinate, and side effects are impermissible. Sources often refer to functions as relations between sets because they map a member of a set to member of another set. While mathematical functions are relevant to the understanding of set theory, programmers need not be particularly concerned with this concept. The significant notion is that of a function in general, which should be apparent to most software professionals. As an aside, further understanding of mathematical functions is particularly useful for other programming concepts.

Mapping works by applying a function to each member of a set and placing the output into another set. Figure Two – Set Mapping illustrates the concept. This is particularly applicable to programming, so understanding is imperative.

figure2

Given the information above, the impetus of the map method of arrays in JavaScript (ES6) is obvious. Arrays are a convenient analog to sets. See the code sample below.

const wholeNumbers = [1, 2, 3];

const evenNumbers = wholeNumbers.map(n => n * 2);
// evenNumbers = [2, 4, 6]

The above isn’t exactly a realistic scenario: generating an array of doubled numbers isn’t auspicious. A more real world use of the map method is to modify complex objects. See the code below.

const people = [{id: 1, name: "Ada Lovelace"}, {id:2, name: "Charles Babbage"}];

const names = people.map(p => p.name);
// names = ["Ada Lovelace", "Charles Babbage"]

Map is exceedingly suitable for many use cases. Understanding set theory elucidates its utility.

Warning

As a fair warning, the remainder of this post provides a prospectus of the areas of set theory that aren’t directly applicable to everyday programming activities. Although intriguing, the uninterested reader should feel free to skip to the conclusion.

To Infinity and Beyond

The conception of sets isn’t exactly revolutionary. Kindergarten pedagogy teaches children to categorize objects into sets. It’s simple and intuitive. The innovation is revealed by examining sets of infinite size.

Conceptually, there are two methods for comparing the sizes of sets. The first is to enumerate the members and compare the resulting counts. This is blindingly obvious; however, it has a substantial flaw. It isn’t possible to calculate the number of members in an infinite set. As a second option, Cantor postulated that if it is possible to create a function that maps the first set to the second set without skipping members, then the sets must be of equal size.

The canonical example is to compare the set of natural numbers (whole numbers excluding zero) to the set of even natural numbers. Figure Three – Counting Sets demonstrates the concept. Although it’s not exactly intuitive, and is often controversial, this establishes that the two infinite sets are equally sized. This might lead one to believe infinity is simply infinity. However, it’s a bit more abstruse.

figure3

Consider the set of real numbers (natural, rational, irrational, and transcendental) between one and two. Think back to the number lines that are an inexorable part of preparatory education and envision a set encompassing all numbers on the line between one and two. Regardless of the placement of two distinct points on the line, it is possible to find a smaller number between them. The interesting thing about this infinite set is that it is not possible to create a function that maps the set of natural numbers to this set without skipping members. This implies that although both sets are infinite, the set of real numbers between one and two is actually larger than the set of all natural numbers. Cantor verified this in a beautifully elegant proof known as Cantors Diagonalization.

While theoretically straightforward, the notion of multiple sizes of infinity is a bit vexatious. John von Neumann once said, “in mathematics you don't understand things. You just get used to them.". This concept holds true to his conjecture. The good news is that the notion of different sizes of infinity is only applicable in the most esoteric areas of computer science. The majority of programmers need not concern themselves with it.

Don’t be Naïve

Set theory took the mathematical world by storm with its simplicity and elegance. Many foundational theories are built on the cornerstone of set theory. However, it contains a substantial flaw which could have spelled doom except that mathematicians couldn’t deny its utility. Therefore, it split into two separate theories known as naïve and axiomatic set theory. It’s similar to how general and special relativity exist simultaneously.

Naïve set theory is sufficient for many applications. In fact, it is adequate for almost all software engineering use cases. Axiomatic set theory does apply to some esoteric areas of computability and logic. However, it is far removed from the greatest majority of programming tasks.

As for axiomatic set theory, it is an extension of the original theory that introduces several axioms that address flaws. The underlying issue with naïve set theory is that a paradox can arise when defining predicates. The most popular demonstration of the defect is Russell’s Paradox. Succinctly stated: does the set of all sets that do not include themselves include itself? If the answer is yes, then the definition is contradictory because it does contain itself. If the answer is no, then the predicate is likewise inconsistent because it cannot contain all sets that do not contain themselves. Don’t worry if this seems perplexing, it often requires reflection.

The finer points of axiomatic set theory are beyond the scope of this article. However, the intrigued reader should perform a web search for Zermelo–Fraenkel set theory to learn more. Regardless of its applicability to programming, it’s quite captivating.

Conclusion

The most pertinent programming related concepts detailed in this post are sets and set mapping. A set is simply a collection of objects. Set mapping is applying a function to each member of a set to produce a related set. The following pieces in this series expound on how these concepts are applicable.

Set theory is surprisingly simple yet it reveals some mystifying truths such as the fact that there are multiple sizes of infinity. There are essentially two branches of set theory: naïve and axiomatic. Naïve set theory is sufficient for the majority of software engineering applications.

Make sure to come back for the next article. With the foundational concepts out of the way, the post delves into set operations which provide valuable mental models for programmers. These are concepts that will improve your development abilities.