Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia

Similar presentations


Presentation on theme: "1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia"— Presentation transcript:

1 1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia batouche@ccis.edu.sa

2 2 Artificial Intelligence CSC 361 Introduction to Prolog

3 3 INTRODUCTION TO PROLOG PROLOGPROLOG PROLOG = PROgramming in LOGic  1970: using logic as a programming language (R. Kowalski, M. Van Emden, A. Colmerauer from Edinburg and Marseille).  1972: First Prolog interpreter (Prolog0), Aix-Marseille, P. Roussel.  1982: Prolog software product in markets.  1995: extension CLP Constraint Logic Programming.

4 4 INTRODUCTION TO PROLOG PROLOGPROLOG PROLOG = PROgramming in LOGic  Three basic mechanisms among others: 1. Pattern Matching, 2. Tree based data structuring 3. Back-Tracking.  Suitable for problems that involve structured objects and relations between them.  It allows symbolic computation.  Examples: 1. Red sphere is behind green box. 2. If object X is closer to the observer than object Y and object Y is closer than object Z than X is closer than Z.

5 5 INTRODUCTION TO PROLOG Prolog basic concepts by an example: Let’s consider the Parent relation Leyla Meriam Omar khaled Zahra Ali Nour

6 6 INTRODUCTION TO PROLOG  DEFINING RELATIONS BY FACTS  This relation can be defined by the following Prolog proram: parent(leyla, omar). parent(ali, omar). parent(omar, meriam). parent(omar, khaled). parent(ali, nour). parent(khaled, zahra).  This program consists of 6 Clauses. Each clause declares one fact about the relation parent.  The clause parent(omar, meriam). Is an instance of the relation parent. A relation is defined as a set of instances.

7 7 INTRODUCTION TO PROLOG What can we do with this program? Let’s ask the system questions about the relation parent:  Question: is omar parent of khaled? In prolog ?- parent(omar, khaled). Answer: yes

8 8 INTRODUCTION TO PROLOG  Question: is leyla parent of meriam? In prolog ?- parent(leyla, meriam). Answer: no  Question: who is zahra parent? In prolog ?- parent(X, zahra). The system tells what is the value of X for which the statement is true. Answer: X= khaled

9 9 INTRODUCTION TO PROLOG  Question: who are omar children? In prolog ?- parent(omar,X). Answer: X= meriam X= khaled no  Question: Who is parent of whom? In prolog ?- parent(X,Y). Answer: X= leyla Y=omar; X=ali Y=omar; X=omar Y=meriam; X=omar Y= khaled; X=ali Y=nour; X=khaled y=zahra;

10 10 INTRODUCTION TO PROLOG  Question: who is grandparent of khaled? In prolog ?- parent(X, khaled) parent(Y, X). Note: the logical meaning remains the same if we change the order of the two requirements. Answer: X= omar Y= ali; X= omar Y=leyla  Question: Who are ali grandchildren? In prolog ?- parent(ali,X) parent (X,Y). Answer: X= omar y=khaled; X=omar Y=meriam;

11 11 INTRODUCTION TO PROLOG  Question: Do meriam and nour have a common parent? In prolog ?- parent(X, meriam) parent(X,nour). First who is parent of meriam X ? Second is (this same) X parent of nour ? Answer no

12 12 INTRODUCTION TO PROLOG What can we conclude from this example:  It is easy in Prolog to define a relation such as parent by stating the n- tuples of objects that satisfy the relation. In this case we have defined a relation by a set of facts represented by clauses.  We can easily express queries in Prolog.  A Prolog program consists of clauses. Each clause terminates with a full stop.  The arguments of relations can be: concrete objects, constants, general objects such as X and Y.  Questions to the system consist of one or more goals. The answer can be positive (goal satisfiable) or negative (goal unsatisfiable).  If several answers satisfy the question than Prolog will find as many of them as desired by the user.

13 13 INTRODUCTION TO PROLOG Data objects in Prolog Data objects Simple objects Structures Constants Variables Numbers Atoms

14 14 INTRODUCTION TO PROLOG  Prolog system recognizes the type of an object in the program by its syntactic form.  The syntax of Prolog specifies different forms for each type of data object.  All data objects in Prolog are called TERMS.

15 15 INTRODUCTION TO PROLOG Atoms and numbers: An atom can be constructed in three ways: 1. String of letters, digits and the underscore ‘_’ starting with a lower case e.g ali, x25, x_y… 2. Strings of special characters e.g:,::=,.. Predefined string: ‘:-’ 3. Strings of characters enclosed in single quotes: ‘Ali’, ‘Saudi-Arabia’…

16 16 INTRODUCTION TO PROLOG Numbers used in Prolog include integer numbers and real numbers e.g 1997, -94, 0, 3.14, 0.0035…. Variables: Strings of letters, digits and underscore characters. They start with an upper case letter or an underscore character e.g: X, Result, _23, _x33…. Note: Prolog uses anonymous variables that are represented by underscores in clauses.

17 17 INTRODUCTION TO PROLOG Structures Structures. Structured objects = object that has several components. The components themselves can, in turn, be structures. e.g date(1,may,2001), date(Day,may,2007) Note: This method for data structuring is simple and powerful symbolic manipulation. All structured objects can be pictured as trees. All structured objects can be pictured as trees.

18 18 INTRODUCTION TO PROLOG StructuresStructures D=date T=triangle 1 may 2007 point point point Date(1,may,2007) 3 1 4 3 6 0 functor arguments T=triangle(point(3,1),point(4,3),point(6,0))

19 19 INTRODUCTION TO PROLOG  Defining Relations By Rules Fact ≠ Rule  A fact is always, unconditionally, true.  A rule specifies a thing that is true if some condition is specified. It has: a condition part (right hand side of the rule). a conclusion part (left hand side of the rule). e.g: let’s define the relation offspring: For all X and Y Y is an offspring of X if X is a parent of Y

20 20 INTRODUCTION TO PROLOG Corresponding Prolog clause: offspring (Y,X):-parent (X,Y). head (conclusion part) body (condition part) Interpretation: for all X and Y if X is parent of Y then Y is an offspring of X

21 21 INTRODUCTION TO PROLOG If we ask the question: ?-offspring(khaled,omar). Y is substituted with khaled and X with omar : the process is called instantiation Y=khaled X=omar Offspring(khaled,omar):-parent(omar,khaled). 1. Try to find whether the condition is true. 2. The goal offspring(khaled,omar) is replaced with the goal parent(omar,khaled)

22 22 INTRODUCTION TO PROLOG 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.

23 23 INTRODUCTION TO PROLOG 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). ‘,’ : to express conjunction (AND operator).

24 24 INTRODUCTION TO PROLOG X Y X Y X Y Z parent offspring parent female mother parent grandparent 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.

25 25 INTRODUCTION TO PROLOG Question: define the relation sister Z XY parent sister For any X and Y X is a sister of Y if 1.Both X and Y have the same parent 2.X is a female sister(X,Y):- female(X), parent(Z,X), parent(Z,Y).

26 26 INTRODUCTION TO PROLOG 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).

27 27 INTRODUCTION TO PROLOG To summarize: 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).

28 28 INTRODUCTION TO PROLOG Recursive rules Let’s define the relation predecessor. First possibiliy For all X and Z X is a predecessor of Z if X is a parent of Z. Second possibility For all X and Z X is a predecessor of Z if X is a parent of Y and Y is a parent of Z.

29 29 INTRODUCTION TO PROLOG Recursive rules More general definition: For all X and Z X is a predecessor of Z If there exits a Y such that X is a parent of Y Y is a predecessor of Z Corresponding Prolog clause: predecessor (X,Z):- parent (X,Y), predecessor(Y,Z).

30 30 INTRODUCTION TO PROLOG Complete program for predecessor relation: predecessor (X,Z):- parent (X,Z). predecessor (X,Z):- parent (X,Y), predecessor(Y,Z). This is a recursive definition of the relation predecesor. Question: who are ali’s successors?

31 31 INTRODUCTION TO PROLOG What about this program? predecessor (X,Z):- parent (X,Z). predecessor (X,Z):- parent (Y,Z), predecessor(X,Y).

32 32 Introduction to Prolog Lists in Prolog  Prolog allows manipulation of lists.  Syntax: a list always starts and ends with square brackets, and each of the items they contain is separated by a comma. e.g. [first,second,third]  Prolog also has a special facility:  Split the first part of the list (called the head) away from the rest of the list (known as the tail).  Place a special symbol | (pronounced 'bar') in the list to distinguish between the first item in the list and the remaining list.   For example, consider the following. [first,second,third] = [X|Y] where X = first and Y=[second,third]  [ ] /* this is a special list, it is called the empty list because it contains nothing */

33 33 Introduction to Prolog Lists in Prolog Now lets consider some comparisons of lists: 1. [a,b,c] unifies with [Head|Tail] resulting in Head=a and Tail=[b,c] 2. [a] unifies with [H|T] resulting in H=a and T=[] 3. [a,b,c] unifies with [a|T] resulting in T=[b,c] 4. [a,b,c] doesn't unify with [b|T] 5. [] doesn't unify with [H|T] 6. [] unifies with []. Two empty lists always match

34 34 Introduction to Prolog Lists in Prolog Consider the following fact. p([H|T], H, T). Lets see what happens when we ask some simple queries. ?- p([a,b,c], X, Y). X=a Y=[b,c] yes ?- p([a], X, Y). X=a Y=[] yes ?- p([], X, Y). no

35 35 Introduction to Prolog Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.


Download ppt "1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia"

Similar presentations


Ads by Google