1 / 34

Chapter 7

King Saud University College of Computer and Information Sciences Information Technology Department IT422 - Intelligent systems . Chapter 7. Part 2 Introduction to Prolog. Introduction. PRO LOG = PRO gramming in LOG ic

rania
Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Saud University College of Computer and Information Sciences Information Technology Department IT422 - Intelligent systems Chapter 7 Part 2 Introduction to Prolog

  2. Introduction • 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.

  3. Introduction • Procedural/Object Oriented Programming (PP/OOP) vs. Declarative Programming • PP/OOP:a program instructs the computer • what to do • how to do • in what order • Declarative Programming:a program describes what to do without a detailed plan of how to do

  4. Introduction • Prolog is the most widely used logic programming language. • Prolog programs are set of definite clauses written in a notation somewhat different from standard FOL. • Three basic mechanisms among others: • Pattern Matching, • Tree based data structuring • Back-Tracking. • Suitable for problems that involve structured objects and relations between them. • The execution of Prolog programs is done through depth-first backward chaining search with no checks for infinite recursion. • It allows symbolic computation. • Examples: • Red sphere is behind green box. • 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. Introduction • Prolog basic concepts by an example: • Let’s consider the Parent relation ali layla nour omar meriam khaled zahra

  6. Defining Relations By Facts • This relation can be defined by the following Prolog program: • 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. Using Prolog Program • 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. Using Prolog Program • Question: is leyla parent of meriam? In prolog ?- parent(leyla, meriam). • Answer: no • Question: who is zahra’s 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. Using Prolog Program • Question: who are omar’s 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. Using Prolog Program • 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. Using Prolog Program • 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 it (X) the same parent of nour ? Answerno

  12. 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. Data objects in Prolog

  14. Prolog Syntax • 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. Prolog Syntax • Atoms and numbers: • An atom can be constructed in three ways: • String of letters, digits and the underscore ‘_’ starting with a lower case e.g. ali, x25, x_y… • Strings of special characters e.g. <__>,::=, .. Predefined string: ‘:-’ • Strings of characters enclosed in single quotes e.g.‘Ali’, ‘Saudi-Arabia’…

  16. Prolog Syntax • 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. Prolog Syntax • 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.

  18. Prolog Syntax • Structures D=date T=triangle 1 may 2007 • point pointpoint • Date(1,may,2007) 3 1 4 3 6 0 functor arguments T=triangle(point(3,1),point(4,3),point(6,0))

  19. 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). • A clause is written “backwards” from what we are used to. Instead of A B => C in Prolog we have C:- A, B. • 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. Defining Relations By Rules • 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. Defining Relations By Rules • 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). • Try to find whether the condition is true. • The goal offspring(khaled,omar) is replaced with the goal parent(omar,khaled)

  22. 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.

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

  24. Defining Relations By Rules • 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. X X X offspring parent mother parent parent grandparent Y Y Y parent Z

  25. Defining Relations By Rules • Question: define the relation 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). Z parent parent X Y sister

  26. 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).

  27. 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. Defining Relations By Rules • Recursive rules Let’s define the relation predecessor. First possibility 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. Defining Relations By Rules • 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. Defining Relations By Rules • 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 predecessor. • Question: who are ali’s successors?

  31. 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 */

  32. Lists in Prolog • Now lets consider some comparisons of lists: • [a,b,c] unifies with [Head|Tail] resulting in Head=a and Tail=[b,c] • [a] unifies with [H|T] resulting in H=a and T=[] • [a,b,c] unifies with [a|T] resulting in T=[b,c] • [a,b,c] doesn't unify with [b|T] • [] doesn't unify with [H|T] • [] unifies with []. Two empty lists always match

  33. 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

  34. Conclusion • 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.

More Related