RU beehive logo ITEC dept promo banner
ITEC 380
2013fall
ibarland
aaray

homelecturesrecipeexamshwsD2Lbreeze (snow day)

lect34-prolog-basics1
prolog intro

teaching coverage: For O2, have class choose a syntax for binding local-vars.

Review: Families of languagues:

These are of course all fuzzy distinctions; you can do functional programming in Java; Racket has an object-system and does allow mutation; you can write C++ code which isn't object-oriented, etc..

If you ssh into rucs.radford.edu, you can type:

xsb
to start a prolog interpreter. Our demo has two phases: (a) the knowledge base (a file of facts and rules), and (b) making interactive queries.

% Data:

female(alice).
male(bob).
male(charlie).
female(dee).
male(ethan).


% (A) Find dance partners -- one man, one woman:

partner(A,B) :- male(A), female(B).
partner(A,B) :- female(A), male(B).

The above lines (facts and rules) are the "knowledge base". If you put this into a file named (say) “lect34.P”, then we can run xsb, and load the knowledge base from the file by typing “[lect34].” (note the square-brackets, the lack of file-suffix, and the period).

Once the knowledge base is loaded, we can interactively make queries:

partner(alice,bob).
partner(bob,alice).

Here's the ultra-cool part: I can put in a variable like “X” (anything that starts with upper-case is considered a variable, not a symbol), and prolog will solve the predicate for me — find all values of

X
that make the predicate true!
partner(bob,X).
partner(X,bob).
partner(X,Y).
In the interactive window, we can type “.” to accept the answer, or “,” to have it look for more.


Some more facts:
drinks(alice,gin).
drinks(alice,martini).
drinks(bob,soda).
drinks(charlie,soda).
drinks(charlie,whiskey).
drinks(charlie,martini).
drinks(ethan,water).

Note: If you are in a prolog shell, and you edit your knowledge-base file foo.P, you'll want to re-set your shell to discard all the old rules. I just exist xsb and re-start, myself. (I also give my file a long name, but also a symbolic-link with a very short name: ln -s lect34-family.P fam.P, and then in the xsb interperter just evaluate [fam]..)

tip: There is a sharp divide between entering *rules*, and entering *queries*. The rules go into a file (the knowledge base), which you then load interactively. If you want to add a rule w/o editing (and re-loading) your knowledge base, you can evaluate [user]., type new rules flawlessly, and end with ^D.
tip: Are you driven batty, by the lack of readline library (up-arrow to scroll through previously-typed lines)? See blipkit.wordpress.com/2009/07/11/readline-in-xsb/. (Untested by me.)
tip: Do you want to load myProg.P, then have prolog evaluate a bunch of tests from a file tests.in, and then leave you at an interactive prompt? Try: cat tests.in - | xsb -e "[myProg]."

How might we...
a limitation: Unfortunately, in Prolog, we can't easily ask "is there a person who does not like any drink?" While Prolog is great with "find me example", it's not so good at "find me something with no examples".

We can go further though: What is a rule to tell if two people both like the same drink?

drinkWith_v1(A,B,Drnk) :- drinks(A,Drnk), drinks(B,Drnk).
%
% Close, but not quite.
% Note that we'll have somebody drinking with themselves.
% Fix with '\=':
drinkWith(A,B,Drnk) :- drinks(A,Drnk), drinks(B,Drnk), A \= B.

Self-check: Write a predicate drinkWith/2, that tells if two people could be compatible drinking-buddies.

Note: ITEC380 does not condone underage drinking, driving while drinking, programming while drinking, or programming while under the influence of bad languages.

No fundamental difference between facts and rules; a fact is just a rule with no variables:

drinks(charlie,X).
%
% Charlie should join AA; he drinks anything.
% [Including ...other people?!]
This line is equivalent to drinks(charlie,X) :- ., where there are not constraints on the right-hand-side. To prolog's mind: Any value of X satisfies every constraint on the right-hand-side.

% It's weird to name a variable which we never use;
% the special variable `_` can be used.
%
drinks(charlie,_).
%
% Underscore is special because even if you mention it
% two or more times, it can match two *different* values:
%
twoNonTeaTotalers(A,B) :- drinkWith(A,_), drinkWith(B,_).
% A and B may not drink the same thing 
% (but they both drink *something*, unlike dee.)

Summary: Why Prolog?

homelecturesrecipeexamshwsD2Lbreeze (snow day)


©2013, Ian Barland, Radford University
Last modified 2013.Nov.11 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme