Upload
frank-snow
View
214
Download
1
Embed Size (px)
Citation preview
Prolog
CS 171/271
Before we begin…
Download and install SWI-Prolog, version 5.6.4: http://gollem.science.uva.nl/cgi-bin/nph-download/SWI-Prolog/w32pl564.exe
What is Prolog? A logic programming language
“Prolog” stands for “Programming in Logic” (or “Programmation en Logique” in French)
Designed in 1972 by Alain Colmerauer and Robert Kowalski (much later than Lisp)
Used in AI programming, expert systems, and computational linguistics Functional programming (ala Lisp) is possible,
but very cumbersome
Logic Programming Creates logical models that describe
the world in which a problem exists Uses first-order logic (but can also be
used with propositional logic) A subset of declarative programming
Creates a set of conditions that describe a solution space
Two phases: declaration and interpretation
Step 1: Declaration Creating a knowledge base (KB) of
sentences describing the world Declaring facts
fun(ai). (AI is fun!) likes(mark,bacon). (Mark likes bacon)
Defining rules heartbroken(X) :- loves(X,Y), not(loves(Y,X)). (X is heartbroken if X loves Y and Y does not love X… *sniff*)
Sentences end with a period
A simple Knowledge Baseorbits(mercury, sun).orbits(venus, sun).orbits(earth, sun).orbits(mars, sun).
orbits(moon, earth).
orbits(phobos, mars).orbits(deimos, mars).
planet(P) :- orbits(P,sun).satellite(S) :- orbits(S,P), planet(P).
Step 2: Interpretation
Deriving new sentences from the sentences in the KB by making queries
Uses a Prolog interpreter SWI-Prolog, GNU Prolog, etc.
Knowledge bases must first be loaded/consulted using consult(‘filename.pl’).
Some simple queries?- consult(‘solar.pl’).% Is the moon a satellite??- satellite(moon).% Is the sun a planet??- planet(sun).% Is Uranus a planet??- planet(uranus).% What are the planets??- planet(Planet).% What objects orbit Mars??- orbits(X, mars).% Is the moon made of cheese??- made_of_cheese(moon).
Another KB exampleparents(william, diana, charles).parents(henry, diana, charles).parents(charles, elizabeth, philip).parents(diana, frances, edward).parents(anne, elizabeth, philip).parents(andrew, elizabeth, philip).parents(edwardW, elizabeth, philip).married(diana, charles).married(elizabeth, philip).married(frances, edward).married(anne, mark).
parent(C,M) :- parents(C,M,D).parent(C,D) :- parents(C,M,D).sibling(X,Y) :- parents(X,M,D), parents(Y,M,D). % What’s wrong with this?
aORuDirect(C, A) :- parent(C,P), sibling(P,A).aORuMarr(C, A) :- aORuDirect(C,X), married(X,A).aORuMarr(C, A) :- aORuDirect(C,X), married(A,X).aORu(C,A) :- aORuDirect(C,A).aORu(C,A) :- aORuMarr(C,A).
Predicates Used to express facts or statements
about objects and their relationships Examples: likes heartbroken orbits Have arguments
teaches(jpv,cs171). (2 arguments) Names/atoms are alphanumeric, can
contain underscores, begin with a lowercase letter
Meanings of predicates, arguments, and order of arguments are arbitrary (but should be descriptive and consistent!)
Variables Can be used to stand for any object Are instantiated during matching Examples: Planet X Scope is statement-wide Usually used in rules, but can also be used in facts
goes_to_heaven(Dog) :- dog(Dog). (All dogs go to heaven.)
must_perish(Thing). (All things must perish.) Variable names are alphanumeric, can contain
underscores, must begin with an uppercase letter Use the underscore to indicate an anonymous variable
female(X) :- mother(X,_). (All mothers are female, regardless of who the child is.)
Queries Describe problems to be solved Consist of goals to be satisfied
Example: father(X) :- male(X), parent(X,_). Goals are male(X) and parent (X,_).
Goals are checked against facts in the KB Rules are satisfied if all the goals in the “if” part (in
the body, separated by commas) are satisfied Variables can take on any value
Press semi-colon to look for further matches Uses backtracking to satisfy goals in all
possible ways (“brute-forcing” it)
Prolog and FOL Rules are usually in the form of Horn
clauses :- is reversed
Backtracking in Prolog is known in FOL terms as backward chaining Begins with a goal (the query) Recursively builds a set of substitutions that
satisfy the goals necessary to conclude the goal All variables in Prolog are assumed to be
universally quantified (though skolemization can still be done)
Recursion and Functions% Make sure they’re not left-recursiveancestor(Person, Ancestor) :- parent(Person, Ancestor).
ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).
% Functions are defined as relationsfac(0,1).fac(N,F) :- N > 0, M is N - 1, fac(M,Fm), F is N * Fm.
Other Prolog features
Support for tree structures and lists (similar to Haskell)
Many built-in predicates (arithmetic, logic, I/O, etc.) and operators
Can parse context-free grammars through definite clause grammars (DCGs)
Towers of Hanoi in Prolog
hanoi(N) :- move(N,a,b,c). move(0,_,_,_).move(N,From,To,Using) :- M is N-1, move(M,From,Using,To), print_move(From,To), move(M,Using,To,From).
print_move(From,To) :- write([move,disk,from,From,to,To]), nl.
Resources SWI-Prolog
http://www.swi-prolog.org/ Visual Prolog
http://www.visual-prolog.com/ GNU Prolog
http://gnu-prolog.inria.fr/ InterProlog (Java)
http://www.declarativa.com/interprolog/ P# (.NET)
http://www.dcs.ed.ac.uk/home/jjc/psharp/psharp-1.1.4/dlpsharp.html