34
1 Artificial Intelligence CS 401 Introduction to Prolog

Artificial Intelligence CS 401

  • Upload
    tricia

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

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). - PowerPoint PPT Presentation

Citation preview

Page 1: Artificial Intelligence  CS 401

1

Artificial Intelligence CS 401

Introduction to Prolog

Page 2: Artificial Intelligence  CS 401

2

INTRODUCTION TO PROLOG

PROPROLOGLOG = PROPROgramming in LOGLOGic

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.

Page 3: Artificial Intelligence  CS 401

3

INTRODUCTION TO PROLOG

PROPROLOGLOG = PROPROgramming in LOGLOGic

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.

Page 4: Artificial Intelligence  CS 401

4

INTRODUCTION TO PROLOG

Prolog basic concepts by an example:

Let’s consider the Parent relation

Leyla

Meriam

Omar

khaled

Zahra

Ali

Nour

Page 5: Artificial Intelligence  CS 401

5

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.

Page 6: Artificial Intelligence  CS 401

6

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

Page 7: Artificial Intelligence  CS 401

7

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

Page 8: Artificial Intelligence  CS 401

8

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;

Page 9: Artificial Intelligence  CS 401

9

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;

Page 10: Artificial Intelligence  CS 401

10

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

Page 11: Artificial Intelligence  CS 401

11

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.

Page 12: Artificial Intelligence  CS 401

12

INTRODUCTION TO PROLOG

Data objects in Prolog

Data objects

Simple objects Structures

Constants Variables

Numbers Atoms

Page 13: Artificial Intelligence  CS 401

13

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.

Page 14: Artificial Intelligence  CS 401

14

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’…

Page 15: Artificial Intelligence  CS 401

15

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.

Page 16: Artificial Intelligence  CS 401

16

INTRODUCTION TO PROLOG

StructuresStructures

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)e.g date(1,may,2001), date(Day,may,2007)

Note:Note: This method for data structuring is simple and powerful This method for data structuring is simple and powerful symbolic manipulation.symbolic manipulation.

All structured objects can be pictured as trees.All structured objects can be pictured as trees.

Page 17: Artificial Intelligence  CS 401

17

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

Page 18: Artificial Intelligence  CS 401

18

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

Page 19: Artificial Intelligence  CS 401

19

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

Page 20: Artificial Intelligence  CS 401

20

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)

Page 21: Artificial Intelligence  CS 401

21

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.

Page 22: Artificial Intelligence  CS 401

22

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

Page 23: Artificial Intelligence  CS 401

23

INTRODUCTION TO PROLOG

X

Y

X

Y

X

Y

Z

parent

offspring

parent

female

mother parent

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.

Page 24: Artificial Intelligence  CS 401

24

INTRODUCTION TO PROLOG

Question: define the relation sister

Z

X Y

parent parent

sister

For any X and YX is a sister of Y if1. Both X and Y have the same parent2. X is a female

sister(X,Y):-

female(X),

parent(Z,X),

parent(Z,Y).

Page 25: Artificial Intelligence  CS 401

25

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

Page 26: Artificial Intelligence  CS 401

26

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

Page 27: Artificial Intelligence  CS 401

27

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 ZX is a predecessor of Z ifX is a parent of Y andY is a parent of Z.

Page 28: Artificial Intelligence  CS 401

28

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

Page 29: Artificial Intelligence  CS 401

29

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?

Page 30: Artificial Intelligence  CS 401

30

INTRODUCTION TO PROLOG

What about this program?

predecessor (X,Z):-

parent (X,Z).

predecessor (X,Z):-

parent (Y,Z),

predecessor(X,Y).

Page 31: Artificial Intelligence  CS 401

31

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

Page 32: Artificial Intelligence  CS 401

32

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

Page 33: Artificial Intelligence  CS 401

33

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

Page 34: Artificial Intelligence  CS 401

34

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.