Presentation is loading. Please wait.

Presentation is loading. Please wait.

Artificial Intelligence CS370D

Similar presentations


Presentation on theme: "Artificial Intelligence CS370D"— Presentation transcript:

1 Artificial Intelligence CS370D
Prolog programming Introduction to Prolog

2 Syntax of Prolog: Terms. Facts and Rules. Programs. Queries.

3 Syntax of Prolog: Terms
A- Constants: 1- Identifiers: sequences of letters, digits, or underscore “_” that start with lower case letters.Ex: noura, x25, x_25, alpha_beta. 2- Numbers: Integers and real numbers:1.001, 2, 3.03 3- Strings enclosed in single quotes: ‘Noura A’ Note: can start with upper case letter, number, spaces or special characters like (==, <=,>=… etc)

4 Syntax of Prolog: Terms
B- Variables: Sequence of letters digits or underscore that start with an upper case letter or the underscore. Ex: _x, Amal, Successor_State, Undescore by itself is the special “anonymous” variable.

5 Defining relations by rules.
Rule is: relation can be defined by making use of the fact. structure with functor ' :- ' and two arguments: head and body. Each rule has two parts: Condition. (The right hand side) (head) Conclusion. (The left hand side) (body)

6 Defining relations by rules.
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.

7 Defining relations by rules.
From previous example (parent relation): offspring(X,Y):-parent(Y,X). offspring(X,Y) -> is a conclusion part. parent(X,Y) -> is a condition part. For all X and Y,Y is an offspring of X if X is a parent of Y.

8 Defining relations by rules.
More complex rules: Let’s add the following facts: female(leyla) male(omar). female(nour) male(khaled). female(meriam). male(ali). female(zahra). Now, let’s define the relation mother: For all X and Y ,X is the mother of Y if X is female and X is parent of Y.

9 Defining relations by rules.
The corresponding Prolog rule is: Mother(X,Y):-female (X) , parent(X,Y). Head (conclusion part): mother (X,Y) Body (condition part): female (X) , parent(X,Y).

10 Defining relations by rules.
female X X X offspring parent mother parent parent grandparent Y Y Y parent Z Nodes correspond to objects: arguments of relations. Arcs between nodes correspond to binary relations. Arcs are oriented so as to point from the first to the second argument. Unary relations are represented by marking the corresponding node by the name of the relation.

11 Defining relations by rules.
Question: define the relation sister: Z parent parent X Y sister For any X and Y X is a sister of Y if Both X and Y have the same parent X is a female sister(X,Y):- female(X), parent(Z,X), parent(Z,Y).

12 Defining relations by rules.
Let’s ask the question: is meriam sister of khaled? ?-sister(meriam,khaled). Another question: who is khaled’s sister? ?-sister(X,khaled). Think about ?-sister(X,meriam).

13 Recursive rule Let’s 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 ).

14 Recursive rule x z Y1 y2 Y3 What about indirect predecessor?
The rule will be as the following: predecessor(X,Z):- parent(X,Y), parent(Y,Z). parent(X,Y1), parent(Y1,Y2), parent(y2,Z). ……. parent Y1 parent y2 Predecessor parent Y3 parent z

15 Recursive rule x z For all X and Z there is a Y such that Y1
X is the parent of Y , and Y is a predecessor of Z. predecessor( X , Z ):- parent( X , Y ), predecessor( Y , Z ). parent Y1 parent y2 Predecessor parent Y3 Predecessor parent z

16 Summary Prolog programs can be extended by simply adding new clauses.
Prolog clauses are of three types: facts, rules and questions. Facts declare things that are always, unconditionally, true. Rules declare things that are true depending on a given condition. By means of questions the user can ask the program what things are true. Prolog clauses consist of the head and the body. The body is a list of goals separated by commas. Commas are understood as conjunctions. Facts are clauses that have a head and the empty body. Questions have only the body. Rules have a head and a body. Variables are instantiated when the system answers questions. Variables are assumed to be universally quantified (for all).


Download ppt "Artificial Intelligence CS370D"

Similar presentations


Ads by Google