Artificial Intelligence CS 370 D Prolog programming Introduction

  • Slides: 11
Download presentation
Artificial Intelligence CS 370 D Prolog programming Introduction to Prolog 1

Artificial Intelligence CS 370 D Prolog programming Introduction to Prolog 1

INTRODUCTION TO PROLOG = PROgramming in LOGic n we defines facts and rules and

INTRODUCTION TO PROLOG = PROgramming in LOGic n we defines facts and rules and give this to the logic program n Ask it what we want to know n It will look and reason, using available facts and rules, and then tells us an answer (or answers) 2

INTRODUCTION TO PROLOG = PROgramming in LOGic PRO LOG v Three basic mechanisms among

INTRODUCTION TO PROLOG = PROgramming in LOGic PRO LOG v Three basic mechanisms among others: 1. Pattern Matching, 2. Tree based data structuring 3. Back-Tracking. v Suitable for problems that involve structured objects and relations between them. v It allows symbolic computation. v Examples: 1. 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. 3

INTRODUCTION TO PROLOG Prolog basic concepts by an example: n Ali Leyla Nour Omar

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

INTRODUCTION TO PROLOG q DEFINING RELATIONS BY FACTS v This relation can be defined

INTRODUCTION TO PROLOG q DEFINING RELATIONS BY FACTS v 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). v This program consists of 6 Clauses. Each clause declares one fact about the relation parent. v The clause parent(omar, meriam). Is an instance of the relation parent. A relation is defined as a set of instances. 5

INTRODUCTION TO PROLOG What can we do with this program? Let’s ask the system

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 6

INTRODUCTION TO PROLOG Ø Question: is leyla parent of meriam? In prolog ? -

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 7

INTRODUCTION TO PROLOG Ø Question: who are omar children? In prolog ? - parent(omar,

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; 8

INTRODUCTION TO PROLOG Ø Question: who is grandparent of khaled? In prolog ? -

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; 9

INTRODUCTION TO PROLOG Ø Question: Do meriam and nour have a common parent? In

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 10

INTRODUCTION TO PROLOG n What can we conclude from this example: v It is

INTRODUCTION TO PROLOG n What can we conclude from this example: v 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. v We can easily express queries in Prolog. v A Prolog program consists of clauses. Each clause terminates with a full stop. v The arguments of relations can be: concrete objects, constants, general objects such as X and Y. v Questions to the system consist of one or more goals. The answer can be positive (goal satisfiable) or negative (goal unsatisfiable). v If several answers satisfy the question than Prolog will find as many of them as desired by the user. 11