RU beehive logo ITEC dept promo banner
ITEC 380
2016fall
ibarland

homelecturesrecipeexamshwsD2Lbreeze (snow day; distance)

prolog-basics1
prolog intro

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. You use Prolog in two phases: (a) the knowledge base (a file of facts and rules), and (b) making interactive queries.
(That is, writing the program, and then running the program; Or, setting up database tables, and then making 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”. After you put this information into a file named (say) “lect34.P”, you can run xsb, and then 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 a newline 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. Myself, I just exit xsb and re-start. (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?


Notes from class, Jul.19.

   To run prolog:
   Two phases:
   - enter your data and rules (in a file)
       myStuff.P
   - then, query the rule-base
       xsb
       ?-  consult(myStuff).    % loads the rule-base
   
   Rather than give inputs to a function and get an answer back,
   you provide the inputs *and* the answer, and prolog says
   "yes (that's a correct answer)", or "no (not a correct answer)"
        turns(red,green).
        turns(red,polkadot).
   
   BUT: we can provide a variable for the second-arg, to have
   prolog solve for us.
        turns(red,X).
   
   Give me some ways to make
      x+y=6   true:
          x=1,y=5
          x=7,y=-1
          x=3,y=3
   

homelecturesrecipeexamshwsD2Lbreeze (snow day; distance)


©2016, Ian Barland, Radford University
Last modified 2016.Nov.18 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.