|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Review: Families of languagues:
declarative: prolog, SQL, Excel
You declare what a correct answer is,
but not how to compute it.
If you ssh into rucs.radford.edu, you can type:
xsbto 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.
% 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). |
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
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:How might we...cat tests.in - | xsb -e "[myProg]."
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
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?!] |
% 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?
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2015, Ian Barland, Radford University Last modified 2015.Dec.05 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |