350 likes | 426 Views
Artificial Intelligence CS 401. Introduction to Prolog. INTRODUCTION TO PROLOG. PRO LOG = PRO gramming in LOG ic 1970: using logic as a programming language (R. Kowalski, M. Van Emden, A. Colmerauer from Edinburg and Marseille).
E N D
Artificial Intelligence CS 401 Introduction to Prolog
INTRODUCTION TO PROLOG PROLOG = PROgramming in LOGic • 1970: usinglogic as a programminglanguage (R. Kowalski, M. Van Emden, A. Colmerauerfrom Edinburg and Marseille). • 1972: First Prolog interpreter (Prolog0), Aix-Marseille, P. Roussel. • 1982: Prolog software product in markets. • 1995: extension CLP ConstraintLogicProgramming.
INTRODUCTION TO PROLOG PROLOG = PROgramming in LOGic • Three basic mechanismsamongothers: • Pattern Matching, • Treebased data structuring • Back-Tracking. • Suitable for problemsthatinvolvestructuredobjects and relations betweenthem. • It allowssymbolic computation. • Examples: 1. Redsphereisbehind green box. 2. If object X iscloser to the observer thanobject Y and object Y iscloserthanobject Z than X iscloserthan Z.
INTRODUCTION TO PROLOG Ali • Prolog basic concepts by an example: Let’s consider the Parent relation Leyla Nour Omar Meriam khaled Zahra
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.
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
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
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;
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;
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
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.
INTRODUCTIONTO PROLOG • Data objects in Prolog Data objects Simple objects Structures Constants Variables Numbers Atoms
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.
INTRODUCTION TO PROLOG • 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… 2. Strings of special characters e.g: <__>,::=,.. Predefined string: ‘:-’ 3. Strings of characters enclosed in single quotes: ‘Ali’, ‘Saudi-Arabia’…
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.
INTRODUCTION TO PROLOG • Structures Structuredobjects = objectthat has several components. The components themselvescan, in turn, be structures. e.g date(1,may,2001), date(Day,may,2007) Note: This method for data structuringis simple and powerfulsymbolic manipulation. All structuredobjectscanbepictured as trees.
INTRODUCTION TO PROLOG • Structures 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))
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
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
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). • Try to find whether the condition is true. • The goal offspring(khaled,omar) is replaced with the goal parent(omar,khaled)
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.
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).
INTRODUCTION TO PROLOG 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.
INTRODUCTION TO PROLOG Question: define the relation sister Z parent parent X Y sister sister(X,Y):- female(X), parent(Z,X), parent(Z,Y). • For any X and Y • X is a sister of Y if • Both X and Y have the same parent • X is a female
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).
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).
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.
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). 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? INTRODUCTION TO PROLOG
What about this program? predecessor (X,Z):- parent (X,Z). predecessor (X,Z):- parent (Y,Z), predecessor(X,Y). INTRODUCTION TO PROLOG
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 */
Introduction to Prolog • 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
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
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.