The Wronk [Last Update 2002.11.20]

Matthew Wronka
<wronkm@rpi.edu>

Prolog 1: Smalltalk 0. Prolog rules! OR An introduction to rules in Prolog.

A brief review of what you (should have) learned.

Well, what have we learned so far, my fellow logic squires?
  • Simple facts
    spock_has_funny_ears.
  • facts with arguments
    hears(programmer, computer_crashing).
  • variables and unification
    says(geoff, hi). /* a fact. */
    ?- says(geoff, What). /* a query. Notice that the W in What is capitalized, signifying a variable. */
    What = hi /* what proglog returns; What unifies to hi */
    yes /* yep, What unified to something. commence rejoicing. */

Understand all those? Let's move on to rules.

An introduction to why ProLog rules. Errr...An introduct to rules in Prolog. That's the one.

Rules are the way one makes conditional statments in ProLog. These have a lot in common with If-Then statements in other languages, or broad statements about the world like All Xs are Y. The main interpretation of rules in ProLog is divided into goals and subgoals. Observe my 31337 ProLog skillz, padawan.

cool_person(X) :- cs_student(X).

To prove the main goal, figuring out if someone is a cool_person, we need to figure out if the subgoal, cs_student, is true for that person. Another way of saying this is if cs_student(X) is true, then X is a cool_person.

Understand? Let me diagram the above rule.

cool_person(X)		:-			cs_student(X).  <---- end your prolog statements with a period.
^			^			^		     
main goal		syntax of a rule	subgoal 1 (there can be more, muwahahahhaha.  erm, more on that later.)

Get it? Got it? Good.

Rules and Unification (the latter being the default answer to every question Professor Schupp asks.)

Let's work with the previous rule defined and add a fact to the bottom of it.

cool_person(X) :- cs_student(X).
cs_student(anon). /* no comment. */

?- cool_person(anon).

yes /* the great ProLog interpreter returns yes. */

Ok, so the interpreter figured out that anon met the subgoal of cs_student, and therefore anon is a cool_person. Let's try something more interesting.

?- cool_person(Whowhatwhenwherewhyhow). /* notice that big list starts with a capital W, signifying a variable.) */

Whowhatwhenwherewhyhow = anon /* these two lines returned by the interpreter. */
yes

How did ProLog figure this out? Let me teach you, my ProLog apprentice. So you will be a master of the ProLog. For now, you listen.

Prolog unifies with every fact line-by-line. If it cannot make a rule true, it finds the next fact and tries that and so on until either it finds something true or cannot find anything true and returns no. Remember, only the first match is retured.

Rules with more than one subgoal and rules with more than one way of proving something.

Consider the following, my force-attuned apprentice:

cool(X) :- california_native(X), sunglasses(X).
cool(X) :- black_car(X), fast_car(X).
cool(X) :- leet_hacker(X).

Let us analyze this chunk of ProLog code. Something is cool if it is a california_native and has sunglasses. Something is cool if it is a black_car and a fast_car. Something is also cool if it is a leet_hacker. Not too bad, huh? Remember, ProLog goes line-by-line, so it will try finding something that is a california_native and sunglasses before it worries about testing the other two groups of subgoals.


A tidbit of information on the above set of rules.

To prolog, this is what the above set of rules looks like:

cool(X_1) :- california_native(X_1), sunglasses(X_1).
cool(X_2) :- black_car(X_2), fast_car(X_2).
cool(X_3) :- leet_hacker(X_3).

Variable name scoping in ProLog is per-rule (called a clause.) This means that although above you can write the same variable name for every rule, ProLog treats each one differently. Variables can bind to many different things over the course of a ProLog program. The X variable above could bind with completely different rules than shown at other parts of the program. Don't worry too much about this. Just don't expect a variable to hold a certain piece of information if you give 10 rules the same variable.


The last example before the eViL quiz.

cool(X) :- california_native(X), sunglasses(X).
cool(X) :- black_car(X), fast_car(X).
cool(X) :- leet_hacker(X).

california_native(geoff).
sunglasses(geoff).
black_car(geoff).
leet_hacker(geoff).
sunglasses(duffman).
blackcar(duffman).
fast_car(duffman).

?- cool(geoff).
yes /* of course. */

?- cool(duffman).
yes /* rule 1 fails, rule 2 passes. */

?- cool(Who).
Who = geoff
yes

Rules aren't too bad, right? Now for the trials, padawan. Only then can you prove yourself to be a master of Prolog rules.

Exercises

Part 1

Which of the following are syntactically correct rules? Which are not?

  1. smalltalk(X) :- fun && beautiful.
  2. prolog(Y) :- good(fun), easy(understanding).
  3. ml :- good, bad, ugly :- smalltalk icky.
  4. computerscience(Z) :- thebestmajor(Z).

Answers:

Part 2

Consider the following statement.

"All classes are fun."

How would we write this in Prolog?


Answer:

Part 3

ilivedhere(X) :- sunny(X), warm(X).
ilivedhere(X) :- gloomy(X), cold(X).

warm(california).
gloomy(troy).
sunny(california).
cold(troy).
cold(alaska).
sunny(alaska).

Questions (yes or no?):
?- ilivedhere (buffalo).
?- ilivedhere (troy).
?- ilivedhere (alaska).
?- ilivedhere (california).
?- ilivedhere (X).

Previous: Unifciation

Next: Lists and Recurssion

Home

Me
About Me
Class Schedule
Prospective Employers
Site
About
Front Page
PHP SysInfo
Texts
State of the World
Email Rules
Lasthope
Stuff
People
Projects
My Computers
Old Site
Links
Inflatable Code
The Register
Slashdot
Everything2
Wiretap
Google
CERT

Every little picofarad has a nanohenry all its own.
		-- Don Vonada

Copyright ©2000-2002 Matthew Wronka. This page was last modfied Wed, 20 Nov 2002 19:46:07 -0500.
Valid HTML 4.01! Valid CSS! Best viewed with any HTML 4.01 browser Bobby WorldWide Approved 508 Bobby WorldWide Approved AA
Powered by PHP Powered by Apache Powered by FreeBSD