The Barber Paradox

October 31, 2011

I recently listened to the Radiolab podcast on Loops that has a segment on The Barber’s Paradox:

The barber shaves only those men in town who do not shave themselves.

Who shaves the barber?

Apparently this paradox was a big issue for set theory. The following SQL works quite nicely to describe the statement “all people either shave themselves, are shaved by someone else, or don’t shave at all”:

CREATE TABLE Person(
name VARCHAR(40) NOT NULL PRIMARY KEY,
shavedBy VARCHAR(40) REFERENCES Person(name)
);

INSERT INTO Person(name, shavedBy) VALUES(‘Floyd’, ‘Floyd’);
INSERT INTO Person(name, shavedBy) VALUES(‘Andy’, ‘Floyd’);
INSERT INTO Person(name, shavedBy) VALUES(‘Goober’, ‘Floyd’);
INSERT INTO Person(name, shavedBy) VALUES(‘Gomer’, ‘Gomer’);
INSERT INTO Person(name, shavedBy) VALUES(‘Opie’, NULL);

SELECT DISTINCT shavedBy AS barber FROM Person
WHERE shavedBy IS NOT NULL
AND shavedBy != name;

The result is:

barber
——–
Floyd

The relational model defines Entities and Attributes on those Entities. So is the issue with the Barber’s Paradox that the language does not make a distinction between Entities and Attributes or is the self-referencing part problematic? Creating a Barber TABLE to go with the Person TABLE is the wrong approach but I think it is only wrong if a self-referencing Attribute exists. We could define a patient-doctor relationship with either a doctor Attribute or a Doctor Entity and both approaches would work fine.

The SQL solution to the Barber Paradox nicely handles additional cases like a new barber moves to Mayberry or Aunt Bee shaves some men in her spare time.

Q.E.D.?

Advertisements