95.501 | 2000 |
7 Introduction to Logic Programming |
7.1 Introduction |
7.2 Predicate Calculus |
Name | Symbol | Example | Meaning |
negation | ? | ? a | not a |
conjunction | ? | a ? b | a and b |
disjunction | ? | a ? b | a or b |
equivalence | ? | a ? b | a is equivalent to b |
implication | ? | a ? b | a implies b |
? | a ? b | b implies a | |
universal quantifier | " | " X P | For all X, P is true |
existential quantifier | $ | $ X P | There exists a value of X such that P is true |
B1 ? B2 ? B3 ? .... ? Bn ? A1 ? A2 ? ... ? Am
likes(bob, trout) ? likes(bob, fish) ? fish(trout) If bobs likes fish and a trout is a fish, bob likes trout
T ? P
Q ? T
Q ? P
older(joanne, jake) ? mother (joanne, jake)
wiser(joanne, jake) ? older (joanne, jake)
wiser(joanne, jake) ? mother (joanne, jake)
7.3 Overview of Logic Programming |
Append Relation, e.g. append(X,Y,Z) or appending X
to Y gives Z
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
append([a], [b], [a,b])?
will either generate yes or fail with no answer - not the same as no
append([a], [b], X )?will answer yes with X = [a,b]
Note that you can also try this queryappend(X [b], [a,b])?
will answer yes with x = [a]
7.4 Prolog's Basic Constructs |
likes(john, mary).
- the name of the relation, likes, is the predicate
- john and mary are atoms
likes (dwight, X). e.g. dwight likes everyone
times(0, X, 0). e.g. 0 times anything
is 0
assert(likes(dwight,sue)).
consult('d:/ggg/hhh/bb.pl').
son(X,Y) :- father(Y,X), male(X).
daughter(X,Y) :- father(Y,X), female(X).
grandfather(X,Z) :- father(X,Y), father(Y,Z).
Note, these rules require other facts, or rules that be used to infer them To add rules from file to Prolog (don't forget those periods) consult('d:/ggg/hhh/bb.pl').
?likes(dwight, sue).
?likes(dwight, X). e.g., who does dwight like?
7.5 Abstract Interpreter for Logic Programs |
An operational procedure for answer queries Performs yes/no computations
Input: A query Q and a program P
Output: yes if a proof of Q from P was found, no otherwise
Algorithm:
Initialize the resolvent to Q
While the resolvent A1, ..., An is not empty
Beginchoose a goal Ai, 1 ?= i ?= n,end
search for instance of a rule A ? B1, B2, ..., Bk, k>= 0 in P, or a fact A such that A=Ai
If no such clause existsexit the while loop;create the new resolvent A1,...,Ai-1, B1, ..., Bk, Ai+1,..., An
If the resolvent is empty, output yes; otherwise output no
male(jake).
male(ryan).female(samantha).
female(sue).father(bill, dwight).
father(dwight, ryan).
father(dwight, samantha).
father(dwight, sue).son(X,Y) :- father(Y,X), male(X). (e.g. X is Y's son)
daughter(X,Y) :- father(Y,X), female(X). (e.g. X is Y's daughter)
Input: ?son(ryan,dwight) and the database
Resolvent is not emptyChoose son(ryan,dwight) the only choiceResolvent is not empty
Search and Find son(ryan,dwight) :- father(dwight,ryan), male(ryan)
New resolvent is ?father(dwight,ryan), male(ryan).Choose father(dwight,ryan)Resolvent is not empty
Search and Find father(dwight,ryan)
New resolvent is ?male(ryan).Choose male(ryan).New Resolvent is empty.
Search and Find male(ryan).
Output: yes