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.)
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.
Which of the following are syntactically correct rules? Which are not?
1,3 are incorrect. 2,4 are correct.
Consider the following statement.
"All classes are fun."
How would we write this in Prolog?
Another way to read this rule is if something is a class (classes(X)), then it is fun.
Let's add the fact classes(proglang) after the rule above.
What happens when the query fun(P) is run? (Remember to show what P unifies with!)
P = proglang yes
ProLog finds the first fact it can unify with and tests it against the subclauses of the rule. P unifies with proglang, and therefore classes(X) is true. Therefore, this makes fun(P) turn into fun(Proglang) which is true.
ilivedhere(X) :- sunny(X), warm(X). ilivedhere(X) :- gloomy(X), cold(X). warm(california). gloomy(troy). sunny(california). cold(troy). cold(alaska). sunny(alaska).
Previous: Unifciation
Next: Lists and Recurssion
--