![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
teaching coverage: I actually spent several minutes, going over what was mentioned at the end of lect09b: the difference between assigning to a local, and assigning to a variable.
Review: Families of languagues:
If you ssh into rucs.radford.edu, you can type:
xsbto 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) “lect10b.P”, then we can run xsb, and load the knowledge base from the file by typing “[lect10b].” (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
Xthat 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. I just exist xsb and re-start, myself.
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
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—exams—hws—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Nov.07 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |