Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog programming Introduction to Prolog

Similar presentations


Presentation on theme: "Prolog programming Introduction to Prolog"— Presentation transcript:

1 Prolog programming Introduction to Prolog
CS 370

2 What is Prolog programming?
Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic programming paradigm. Prolog is a programming language for symbolic , non-numeric commutation. a program consists of a database of facts and logical relationships (rules) describing the relationships which hold for the given application.

3 How Prolog program is running?
Rather than running a program to obtain a solution, the user asks a question. When asked a question, the run-time system searches through the database of facts and rules to determine the answer by logical deduction.

4 Example:

5 Defining relations by facts
The fact that Tom is a parent of Bob can be written in prolog as: parent(tom, bob). tom and bob are objects (atoms). parent(tom, bob) is a relation between these objects.

6 Defining relations by facts
The whole family tree can be described by the following facts: pam tom bob liz ann pat jim

7 Defining relations by facts
The 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). pam tom bob liz ann pat jim

8 Defining relations by facts
We 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).

9 Defining relations by facts
female(pam) is a Unary relation parent(bob,pat) is a Binary relation. Think about this relation: sex(pam, feminist).

10 Questions in prolog. Prolog can posed questions about any relation.
So, we can ask a question about Parent relation, For example : Is bob a parent of Pat? ?- parent(bob,pat). The answer will be : true.

11 Questions in prolog. Another question can be asked: Who is Liz’s parent? ?- parent(X, liz). The answer here will not be Yes/No, rather, prolog will find the value of X. So the answer is : X= tom. pam tom bob liz ann pat jim

12 Class exercise(1) Who are bob’s children? pam tom bob liz ann pat jim

13 Class exercise(2) Who is parent of whom? pam tom bob liz ann pat jim

14 Questions in prolog. In prolog, we can ask more complicated questions such as: Who is a grandparent of jim? ?- parent(Y,jim) , parent( X,Y). The answer will be: Y = pat, X = bob. X Parent grandparent Parent Y jim

15 Class exercise(3) Who are Tom’s grandchildren?

16 Questions in prolog. Who is bob’s mother? Who is bob’s sister?
Do ann and pat have a common parents?

17 Some important points A 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 , and X is parent of Pat.


Download ppt "Prolog programming Introduction to Prolog"

Similar presentations


Ads by Google