95.307 - Programming Paradigms 1999

 8  Introduction to Logic Programming


What's in This Set of Notes ?


8.1 Introduction


8.2 Predicate Calculus


Propositions


Clausal Form

 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


Proving Theorems

T ⊂ P
Q ⊂ T
Q ⊂ P
older(joanne, jake) ⊂ mother (joanne, jake)
wiser(joanne, jake) ⊂ older (joanne, jake)
wiser(joanne, jake) ⊂ mother (joanne, jake)



8.3 Overview of Logic Programming


Relations


Append Relation, e.g. append(X,Y,Z) or appending X to Y gives Z

X
Y
Z
[]
[]
[]
[a]
[]
[a]
[a,b]
[c,d]
[a,b,c,d]
[]
[b]
[b]
[a]
[b]
[a,b]


Queries

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 query

append(X, [b], [a,b])?

will answer yes with x = [a]



8.4 Prolog's Basic Constructs

Term


Facts


Rules

  • Note, these rules require other facts, or rules that be used to infer them.
  • You can assert rules too. assert((son(X, Y) :- father(Y, X), male(X))).
  • To add rules from file to Prolog (don't forget those periods)
  • consult('c:/database-of-rules.pl').


    Queries (Questions)

    ?- likes(dwight, sue).

    Variable Query

    ?- likes(dwight, X).  i.e., who does dwight like?

    8.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
    Begin
    choose a goal Ai, 1 <= i <= n,
    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 exists
    exit the while loop;
    create the new resolvent A1,...,Ai-1, B1, ..., Bk, Ai+1,..., An
    end
    If the resolvent is empty, output yes; otherwise output no


    Example

    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).   (i.e. X is Y's son)

    daughter(X,Y) :- father(Y,X), female(X). (i.e. X is Y's daughter)

    Input: ?- son(ryan,dwight) and the database
    Resolvent is not empty
    Choose son(ryan,dwight) the only choice
    Search and Find son(ryan,dwight) :- father(dwight,ryan), male(ryan)
    New resolvent is ?- father(dwight,ryan), male(ryan).
    Resolvent is not empty
    Choose father(dwight,ryan)
    Search and Find father(dwight,ryan)
    New resolvent is ?male(ryan).
    Resolvent is not empty
    Choose male(ryan).
    Search and Find male(ryan).
    New Resolvent is empty.


    Output: yes