next up previous
Next: About this document Up: No Title Previous: No Title

The Logic Programming Paradigm

The Logic Paradigm

A logic program can be thought of a consisting of

All we have so far is a set of facts.

What are some permissible deductions? What can we ``deduce'' from these simple relation? We can make simple kinds of deductions such as as ``A Person takes-cp2003 IF they are enrolled-in cp2003."

takes-cp2003(Person) :- enrolled-in(cp2003,Person).
In this clause, the variable Person starts with an upper-case letter. It represents any person. In general, constants start with lower-case letter while variables start with an upper-case letter.

Another deduction is ``A Student is-taught-by Teacher IF the Student is enrolled-in class X and Teacher teaches class X.''

is-taught-by(Student,Teacher) :- enrolled-in(X,Student), 
                                teaches(X,Teacher).

Again, note the use of variables.

Notice that all the proof rules are of the form

   if P then Q
which is sometimes written as
   P --> Q   (P implies Q) or (not P) V Q
but which we will write the goal at the left side in Prolog:
  Q :- P.
if P is true then we can conclude that Q is true

Note that P given above could be of the form of conjunctions:

  P1 and P2 and ... and Pn
which we will write in Prolog :
  P1, P2, ... , Pn
Rules that look like this are known as Horn clauses.

E.g not(brother(X, Z)) V not(child(Y,Z)) V uncle(X,Y) is a Horn clause.

But hasjob(X) V unemployeed(X) is not a Horn clause.

Notice however, that our P's and Q's are of the form

  P(X1, ..., Xn)

P is the factor in the structure and Xs are arguments. We will discuss them later.

There is an important difference between facts and rules.

A rule example:

   grandparent(X,Z) :- parent(X,Y), parent(Y,Z).

How do we match these to individual facts? We do so through a process called unification. All the s are variables or constants that must unify or match with a head (the part to the left of the :-) of some rule or fact. So let's assume that I have the goal (or query)

  teaches(cp2003, Teacher)

Well this unifies with

   teaches(cp2003, jinli)

since cp2003 = cp2003 and Teacher = jinli (a variable unifies with anything), but not with

   teaches(cp3210, geoff)

since cp2003 != cp3210 (constants only unify with equivalent constants).

Finally, we need something, a goal, to prove. Let's have our goal be

  ? - teaches(cp2003, Teacher)

which is equivalent to asking: Who teaches cp2003?. We determine that Teacher = jinli.
How about if we ask

  enrolled-in(cp2003, Student)?

Well, now we have more than one answer: Student = joe and Student = mary.

Now let's do something a little more complicated, let's determine whom mary's is-taught-by.

  ? - is-taught-by(mary, Teacher)

In order to prove our goal, we first unify our goal

  is-taught-by(mary, Teacher)

with the head of some clause. It unifies with

  is-taught-by(mary,Teacher) :- enrolled-in(X,mary), 
                             teaches(X,Teacher).

So we now have two subgoals to prove:

  enrolled-in(X,mary)

and

  teaches(X,Teacher)

The first subgoal unifies with

  enrolled-in(cp2003,mary)

which tells us that X = cp2003. The second subgoal unifies with

  teaches(cp2003, jinli)

and

  teaches(cp3210,geoff)

which tells us that if X = cp2003 then Teacher = jinli, otherwise X = cp3210 and Teacher = geoff. We know X must unify with cp2003 to prove the first subgoal, therefore, we have proved our original goal with the proviso that Teacher = jinli. So, mary is taught by jinli.


Case Study -Prolog

  • Prolog is a programming language for symbolic, non-numeric computation.
  • It is specially well suited for solving problems that involve objects and relations between objects.
  • Declarative - Program defines what the problem is, rather than how to solve it. (Fourth-generation language) Variables, Values and Terms

      [x|xs]   or .(a, xs)    % the list's head is x and tail is xs.
      [a,b,...,z] = .(a,(b,...(z,[])...))

    Clause and Relations

    In Prolog there are three types of statements: facts, rules, and queries. All these statements are clauses or, more specifically, Horn clauses.

    A Prolog defines a collection of relations. Each relation is defined by one or more clause, written in the follow notation:

      A0 :- A1, A2, ...An.

    The symbol `:-' means `if', and the symbol ',' means `and'. Always end with . in a program.

    Queries

    Queries on facts:

    What subject is enrolled by joe?

      entolled-in(X, joe).
    
    X= 2003  ?
    X= 3120
    Whose parent is tom?
     parent(tom, X ).
    
     X = liz   ?

    Queries on rules:

    Who is taught by geoff?

      is-taught-by(X, geoff).
    Answer will be:
      X= joe 
    
      takes-cp2003(Sam).
    
    no.

    Who is grandchild of bob?

    grandparent(bob, X).
    X = liz




    next up previous
    Next: About this document Up: No Title Previous: No Title

    Jinli Cao
    Wed Oct 28 08:58:12 EST 1998