Artificial Intelligence - An Introduction to Prolog Programming

 Introduction to Prolog Programming

The Basics

Prolog (Programming in logic) is one of the most widely used programming languages in artificial intelligence research. As opposed to imperative languages such as C or java (the latter of which also happens to be object-oriented) it is a declarative programming language. That means, when implementing the solution to a problem, instead of specifying how to achieve a certain goal in a certain situation, we specify what is situation (rules and facts) and the goals (query) are and let the prolog interpreter derive the solution for us. Prolog is very useful in some problem areas, such as artificial intelligence, natural language processing, database,.... but pretty useless in others, such as graphics or numerical algorithms.

Basic Features of Prolog

Features of Prolog are:-
  • Facts
  • Terms
  • Queries
  • Logical Variables
  • Shared Variables
  • Data Types in Prolog
  • Rules
In this lecture note we will discuss only two important feature of prolog-

Facts


Facts are statements that describe object properties or relations between objects. Let us imagine we want to encode that Ulysses, Penelope, Telemachus, Achilles, and others are characters of Homer’s Iliad and Odyssey. This translates into Prolog facts ended with a period:



Exercise 1: Defining Relation by Facts

For the above tree create a prolog program of facts (parent, male, female, mother) and answer the following questions using prolog
  1. Is Bob a parent of Pat?
  2. Who is Liz's parent?
  3. Who are Bob's children?
  4. Who is a parent of whom?
  5. Who is a grandparent of Jim?
  6. Who are Tom's grandchildren?
  7. Do Ann and Pat have a common parent?
Solution - 

parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(pat,jim).
parent(bob,pat).
female(pam).
female(liz).
female(ann).
male(tom).
male(jim).
male(bob).

Answer to above questions:-

1.   ?-parent(bob,pat)
      Yes

2.   ?-parent(X,liz)
      X = tom

3.   ?-parent(bob,X)
      X = ann ?;
      pat

4.   ?-parent(X,Y)
      X = pam
      Y = bob ?;

      X = tom
      Y = bob ?;

      X = tom
      Y = liz ?;

      X = bob
      Y = ann ?;

      X = pat
      Y = jim.

5.   ?- grandparent(X,jim)
      X = bob.

6.   ?- grandparent(tom,X)
      X = ann ?;
      X = pat.

7.   ?- parent(X,ann) , parent(X,pat)
      X = bob ?;
      Yes

Rules

Rules enable to derive a new property or relation from a set of existing ones. For instance, the property of being the son of somebody corresponds to either the prop- erty of having a father and being a male, or having a mother and being a male. Accordingly, the Prolog predicate son(X, Y) corresponds either to conjunction male(X), father(Y, X), or to male(X), mother(Y, X). Being a son admits thus two definitions that are transcribed as two Prolog rules:


Exercise 2: Defining Relation by Rules

Add the rule for following relationship
  1. offspring
  2. mother
  3. grandparent
  4. sister
  5. predecessor
What is the output for the following goal?

?offspring(liz,tom).
?offspring(X,Y).
?predecessor(tom,pat).
?sister(ann,pat).
?sister(X,pat).
?mother(X,bob).
?grandparent(X,jim).
?mother(pam,bob).
?grandparent(bob,jim).

Solution - 

parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(pat,jim).
parent(bob,pat).
female(pam).
female(liz).
female(ann).
male(tom).
male(jim).
male(bob).
offspring(liz,tom).
offspring(Y,X):-parent(X,Y).
mother(X,Y):-parent(X,Y),female(X).
grandparent(X,Z):-parent(X,Y),parent(Y,Z).
sister(X,Y):-parent(Z,X),parent(Z,Y),female(X).
predecessor(X,Z):-parent(X,Z).
predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).

Answer to above questions-

1.  ?-offspring(liz,tom)
      true

2.   ?-offspring(X,Y)
      X = liz
      Y = tom ?;
     
      X = bob
      Y = tom ?;
  
      X = ann
      Y = bob ?;
      X = bob
      Y = pam ?;
      X = liz
      Y = tom ?;
  
      X = jim
      Y = pat ?;

3.   ?-sister(A,pat)
      X = ann.

4.   ?-sister(ann,pat)
      true.

5.   ?-mother(X,bob)
      X = pam.

6.   ?-grandparent(X,jim)
      X = bob.

7.   ?-mother(pam,bob)
      true

8.   ?-grandparent(bob,jim)

Comments

Popular posts from this blog

Artificial Intelligence

Smart Card

Biometrics