Classic Computer Magazine Archive COMPUTE! ISSUE 88 / SEPTEMBER 1987 / PAGE 77

Smart Alec

Ronald Carnell

Who says a computer can't think? "Smart Alec" emulates an important part of human logic and can provide intriguing insights into how we reason deductively. Although it's written in machine language, this Commodore 64 program is as easy to use as BASIC. A disk drive is required.

"Smart Alec" is a program that simulates human thinking, or at least one portion of it. That portion is the realm of deductive reasoning through syllogistic logic. A syllogism is an argument in which a conclusion is inferred from two premises. Here is a simple example:

a Greek is a human
an Athenian is a Greek
therefore an Athenian is a human

The conclusion is derived from the first two statements, which are called categorical propositions. Categorical propositions either affirm or deny that one class or category is included in another. The example shows that we can use two categorical propositions to arrive at a logically correct conclusion. Although humans seldom think in such formal terms, syllogisms are an important part of our reasoning process.

Syllogisms are important to Alec, too. The premises you supply to him are his food (and he's always hungry), and the conclusions he draws are his only reason for existence.

Type in and save Program 1 using the "MLX" machine language entry program found elsewhere in this issue. When you run MLX, you'll be asked for a starting and an ending address for the data you'll be entering. Here are the values to use for Smart Alec:

Starting address:	0801
Ending address:	15F0

Although Smart Alec is written in machine language, it loads and runs exactly like a BASIC program. To help distinguish your input from Alec's responses, your input statements appear in white; his statements appear in black. If these colors are hard to read on your TV or monitor, change the screen background color before you run the program. For instance, type POKE 53281,12 and press RETURN to change the background color to gray. When you're ready to proceed, load and run Smart Alec as you would a BASIC program.


Smart Alec accepts three different kinds of input from you: statements, questions, and direct commands. Let's look at each one in turn, beginning with statements.

A statement is a sentence that can be used as a premise in a syllogism. A statement must be expressed in a format that Alec can understand. Here is the correct format for statements:

article, subject, verb, negation, article, predicate

The italicized items are optional, while those in normal type must appear in every statement. Consider the following sentence:

an apple is not a vegetable

The words a and an are articles. Alec recognizes these, plus the, all, and every as words that may precede a noun. The subject of our sentence is apple and the predicate is vegetables. Our example sentence is a negation—it says an apple is not part of the class known as vegetables—so the optional not has been included. Finally, the sentence is joined by a verb, is.

Most of the verbs recognized by Alec are some form of the verb to be. These include am, are, is, was, were, will be, has been, be, will, does, do, and would.

Alec also accepts a special verb phrase as an assignment. When you tell Alec an apple is a fruit, he is smart enough to know that the reverse isn't necessarily true: a fruit is an apple would be an invalid deduction. There will be times, however, when you will want one thing to equal another—to be exactly the same. Alec accepts the phrase is the same as, or more simply, an equal sign (=) as such an assignment. This verb phrase allows you to set up synonyms that Alec will understand. For example, the statement smart is the same as intelligent instructs Alec that the words smart and intelligent are completely interchangeable. You could also use the phrase smart = intelligent to accomplish the same goal.

If Alec responds with OK, he understands your statement. Other responses depend on the situation. If he doesn't understand the statement, he'll ask you to rephrase it. If you try to tell Alec something he already knows, he'll notify you of that fact. Alec will also respond with I know if you try to directly contradict an existing fact, meaning that he knows the statement cannot be true.

Two Input Modes

There are two ways to supply statements to Alec. The first, an interactive method, involves simply typing the statements. This mode is quite interesting in that you can type in several statements about a given subject and then question Alec immediately about his deductions.

The second way is to enter your statements in the form of program lines, save the file to disk, and then ask Alec to read the file. This is more efficient for complex situations because it allows you to edit the statements and rerun the entire session. Once you have supplied a statement in interactive mode, there is no way to edit it. For instance, I once told Alec that man is the same as human, thinking of man in the sense of mankind. When I later stated that a woman is not a man, Alec was led to the erroneous conclusion that a woman is not human. If you store statements in a file, you can change only the statements you need, without having to retype everything from scratch.

Program 2 is an example of a statement file, which is simply a numbered list of sentences each enclosed in quotation marks.

One final note about statements before we discuss questions: A statement to Alec is not a mere intangible that can be used to reach deductions; it is the very sustenance of his life. Alec calls a statement a morsel, and with good reason. He hungers for morsels just as you and I might crave a peanut. If Alec answers too many questions without being fed any morsels, he begins to get hungry and he will let you know it's time to feed him. His degree of hunger and how often it arrives will depend on just how full Alec already is. If Alec has little information on hand, he begins to hunger quickly and his hunger increases rapidly. If you ignore his requests, Alec's hunger eventually reaches the point where he can't think and will refuse to answer questions until he's fed. If Alec's database is close to full, however, he will get hungry infrequently, and not very significantly.


As noted earlier, you can also ask questions of Smart Alec. The format for questions is just a slightly rearranged version of the statement:

verb article subject term article negation predicate term

The words that Alec recognizes as articles and verbs are identical to those he recognizes in a statement (see above).

Alec can respond in four different ways to a question, with yes, no, I don't know, or I don't know anything about that. The first two responses are self-explanatory. The third response means that Alec can't reach a conclusive deduction. The last means that you are asking about a subject which Alec can't find in his memory.


Alec understands the following words as direct commands:

don't explain
always explain

Some of the commands involve statement files. For instance, the command READ HUMANS causes Alec to read a statement file named humans.

Note that the filename is not enclosed in quotation marks. You can also tell Alec to load or save his entire database for future sessions. The syntax is similar to the read command: The command SAVE HUMANS saves the database with the filename HUMANS, and the command LOAD HUMANS loads the file HUMANS into memory. The size of data files is always the same.

In some cases you might want to learn how much Alec knows about a particular subject. The DISCUSS command causes Alec to disgorge everything he knows about a topic. For instance, if you're curious about how much Alec has learned about humans, you might command him to DISCUSS HUMAN. Alec searches his entire database for any reference to human and repeats everything he has previously been told about that word.

When you first run Smart Alec, he is designed to answer questions concisely, without explaining the underlying logic. The commands EXPLAIN and WHY are synonyms which cause Alec to display the entire chain of logic behind a conclusion, including false starts and interrupted chains of thought. The command ALWAYS EXPLAIN causes Alec to explain his thought process after each question without the need to ask. The command DON'T EXPLAIN resets the original concise mode.

The DUMP command causes Alec to dump, or display, his entire database to a printer. Alec dumps only the words that appear in his memory, without reconstituting them into sentences.

The last command, which Alec hates to receive, is BYE. When you issue this command, indicating that you wish to terminate the session, Alec asks for confirmation. If you respond by pressing Y for yes, Alec returns control of the computer to you. Because he has made so many temporary changes to the computer, the only way Alec can depart safely is by causing the computer to reset.


There are instances where Alec might appear to be wrong. The first such instance is by far the most prevalent. Alec's conclusions are only as accurate as the premises you supply; he depends totally on you to supply factual and unambiguous statements. The example cited earlier, where we told Alec that man is the same as human, is a typically ambiguous premise. The word man was used incorrectly to mean mankind or humanity; when man later appeared in a different context, Alec became confused. Logicians call this the fallacy of equivocation and it emphasizes that Alec is subject to the same fallacies of logic that we humans face.

Sometimes you may suspect that Alec should know something when he doesn't. Consider these propositions:

a mortal will die
a unicorn is not mortal

When Alec was asked if a unicorn would die, he responded I don't know. At first you might think that Alec should be able to deduce the correct answer. However, that reasoning includes assumptions that Alec could not know at that point. You and I know that an entity is either mortal or not mortal—there is no third alternative. But Alec doesn't know this and would not assume it. Based on the information available to Alec at the time, the fact that a unicorn is not mortal does not necessarily make it immortal. In addition, this pair of statements equates the words mortal and die, but that is not what we told Alec. These statements may illustrate the point more clearly:

water will evaporate
alcohol is not water

It becomes obvious, now, that just because alcohol is not water doesn't mean it won't evaporate. Given only the information Alec had in the previous example, you can see that just because a unicorn is not mortal, doesn't mean it won't die.

This example points out one of the more intriguing features of this program. When Smart Alec reaches a conclusion that we suspected, we find it interesting. But when Alec quite correctly reaches a conclusion that goes against our own sense of logic—our own experience of right and wrong—we are forced to examine our own thought processes to see how they differ from Alec's. We discover not only how Alec thinks, but how we think as well.

How Smart Alec Thinks

Smart Alec simulates the appearance of human thought, but he certainly doesn't think in the same manner we do. Alec's "mind" is composed of a two-dimensional array which contains the subjects and predicate terms you supply. A very simple entry might look like this:

Subject:	man	human	mortal
Predicate:	human	mortal	die

In this instance, we have told Alec that a man is the same as a human, a human is mortal, and a mortal will die. The subject of each statement is stored as a header in the array, and the predicate terms are stored beneath the proper header. If we added the statement a man is a mammal, Alec would store mammal beneath the already created header of man.

Smart Alec has room for 256 different headers in his memory. He will refuse to accept more. Under each header, he can store 20 different predicate terms. If a twenty-first term is entered, Alec accepts it and allows it to replace the first predicate term that was entered under that header. In effect, he forgets the first entry.

In addition to each header and predicate term, Alec also stores the articles and verb that were used in the sentence. These are stored as one-byte numbers to conserve memory; they can later be retrieved to reconstruct the original statements.

Using the array we set up as an example, let's ask Alec a question and see how he answers it. The question is will a man die?

Alec dissects the question and discovers that man is the subject and die is the predicate term. He sets man aside for a moment and looks through his memory for die stored as a predicate term. He finds it under the header of mortal. Retrieving man, he compares it to the current header and finds that mortal and man do not match. Alec sets man aside again, and replaces the original predicate term with the current header. In effect, he transforms our question from will a man die? to is a man mortal?

Alec now searches his memory for mortal stored as a predicate term. It's important to note that the mortal that replaced die was a header, but Alec is now searching for it as a predicate term. He finds it listed as such under human. When he looks and finds that man doesn't match human, be will replace mortal with the header human and then look for human as a predicate term. When he finds it under man, he will again retrieve the original subject, man, and find that he does indeed have a direct match. Because the train of thought was uninterrupted, Alec will answer yes, a man will die.

The algorithm for Alec's thinking, then, becomes a fairly simple search-and-replace procedure. It becomes slightly more complicated when Alec can't find a direct path between predicate term and subject on the first attempt. Then he has to start crossing off the tail end of a bad search to insure that he doesn't end up back there when he searches again. He will continue crossing off the tail end of a bad search until he eventually finds the right track to follow, or has no tracks left.

Actually, Alec's own thinking is more complex than this because he allows us to assign a subject and predicate term as equal, and because he allows negative statements.