# Prolog programming Introduction to Prolog

• View
89

4

Tags:

• #### relation offspring

Embed Size (px)

DESCRIPTION

Prolog programming Introduction to Prolog . CS 461. What is Prolog programming? . Prolog is a programming language for symbolic , non-numeric commutation. It solves problems that involve objects and relations between objects. . Defining relations by facts. - PowerPoint PPT Presentation

### Text of Prolog programming Introduction to Prolog

Prolog programming Introduction to Prolog

Prolog programmingIntroduction to Prolog CS 461What is Prolog programming? Prolog is a programming language for symbolic , non-numeric commutation.

It solves problems that involve objects and relations between objects. Defining relations by factsThe fact that Tom is a parent of Bob can be written in prolog as:

Parent(tom, bob).

tom and bob are objects, Parent(tom, bob) is a relation between these objects. Defining relations by factsThe whole family tree can be described by the following facts:Parent(pam,bob).Parent(tom,bob).Parent(tom,liz).Parent(bob,ann).Parent(bob,pat).Parent(pat,jim).

pamtomboblizannpatjimDefining relations by factsWe can also add information about the sex of the people by the following facts:

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

Defining relations by factsfemale(pam) is a Unary relation

parent(bob,pat) is a Binary relation.

Another question can be asked: Who is Lizs parent?

?- Parent(X, liz).

The answer here will not be Yes/No, rather, prolog will find the value of X. So the answer is : tomQuestions in prolog.pamtomboblizannpatjimClass exercise(1)Who are bobs children?Who is parent of whom? pamtomboblizannpatjimQuestions in prolog.In prolog, we can ask more complicated questions such as: Who is a grandparent of jim?

?- Parent(Y,jim) , Parent( X,Y).

XYjimParentParentgrandparentClass exercise(2)Who are Toms grandchildren?

Questions in prolog.Do ann and pat have a common parents?

?- Parent(X,ann) , Parent(X, pat).The answer will be : X = bob.

Some important pointsA prolog program consists of clauses, each clause ends with a full stop.

The arguments of relation can be concrete objects, constant objects (such as bob and pat) or general object (such as X and Y).

Questions in prolog consist of one or more goals.

parent(X,ann),parent(X,pat) means the conjunctions of the goals:X is parent of ann , andX is parent of Pat.Defining relations by rules.Consider the relation offspring: offspring(liz, tom)

This relation can be defined by making use of the fact that : the parent is the inverse of offspring.

The alternative way can be based of the following logical statements:

For all X and Y, Y is an offspring of X if X is a parent of Y.Defining relations by rules.In prolog we can define the offspring relation in the following way:

Offspring(Y,X) :- Parent(X, Y)

This clause is called a rule

The differences between rule and fact:Fact: is a something that always, unconditionally true.Rule: specify things that are true if some condition is satisfied.

Defining relations by rules.Each rule has two parts:Condition. (The right hand side)Conclusion. (The left hand side)

offspring(X,Y):-parent(Y,X).offspring(X,Y) -> is a conclusion part.parent(X,Y) -> is a condition part.

Defining relations by rules.How rules are actually used by prolog program?Consider the following question:

?- offspring(liz, tom)

There are no facts in the program about the offspring, therefore, the only way to find the answer is to apply the rule about offspring relation.Defining relations by rules.Now we want to apply this rule:Offspring(Y,X) :- Parent(X, Y)

to find the answer of this question: ?- offspring(liz, tom).

X= tom, Y= liz.The initial goal is: offspring(liz,tom).The condition part become: parent(tom,liz).The initial goal will be replaced with the subgoal parent(tom,liz).Program can find the answer of this subgoal by using the facts about parent relation.After finding that the condition part is true, the conclusion part will be also true.

Class exercise(2)Write a rule to describe the grandparent relation?

Home exercise(1)Write a rule to describe the sister relation?

Recursive ruleLets describe the predecessor relation , this relation will be defined in term of parent relation.For all X and Z X is a predecessor of Z if X is a parent of Z

predecessor(X,Z):- parent(X,Z).Recursive ruleWhat about indirect predecessor? The rule will be as the following:predecessor(X,Z):- parent(X,Y),parent(Y,Z) predecessor(X,Z):- parent(X,Y1),parent(Y1,Y2).parent(y2,Z).

..

xY1zy2Y3PredecessorparentparentparentparentRecursive ruleFor all X and Zthere is a Y such that X is the parent of Y , and Y is a predecessor of Z.

predecessor(X, Z) parent(X,Y),predecessor(Y,Z).

xY1zy2Y3PredecessorparentparentparentparentPredecessor

Recommended ##### 1-1 An Introduction to Prolog Sept. 2008. 1-2 Prolog statements Like other programming languages, Prolog…
Documents ##### Introduction to Prolog Programming - ulle/teaching/prolog/ Endriss. An Introduction to Prolog Programming 3 of bigger. Animal X is bigger than animal Y either if this has been stated
Documents ##### Logic Programming in Prolog with tuProlog - unibo. - Logic Programming in Prolog with... · Prolog
Documents ##### Prolog: Simple Programming - CS Home .Prolog: Simple Programming CS F331 Programming Languages CSCE
Documents ##### Introduction to Prolog Programming - .These lecture notes introduce the declarative programming language
Documents ##### Introduction to Prolog Programming - .And Introduction to Prolog Programming 3 of bigger. Animal
Documents ##### Lecture Notes An Introduction to Prolog Programming Ulle Endriss
Documents ##### A Brief Introduction to Prolog Programming - Bill 2014-12-06آ  A Brief Introduction to Prolog Programming
Documents Documents