chapter 1: introduction - ahmed sallam · prolog iv, binprolog, ciao prolog, prolog lpa, visual...

Post on 02-Jul-2018

318 Views

Category:

Documents

12 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Ahmed Sallam Slides based on original lecture slides Dr. Temur Kutsia

LO

GIC

PR

OG

RA

MM

ING

Chapter 1: Introduction

"What" vs "How"

Declarative vs Procedural Programming

Procedural programming

• The programmer has to specify how to get the output for

the range of required inputs.

• The programmer must know the appropriate algorithm.

Declarative programming

• Requires a more descriptive style.

• The programmer must know what relationships hold

between various entities.

Introduction

2

16-Feb-17

Example: List Concatenation

In procedural style:

list procedure cat(list a,list b)

{

list t = list u = copylist(a);

while (t.tail != nil) t = t.tail;

t.tail = b;

return u;

}

In declarative style:

cat([], L, L).

cat([H|T], L, [H|Z]) :- cat(T, L, Z).

Introduction

3

16-Feb-17

Logic Programming

A declarative style programming paradigm.

Computation through logical deduction.

Uses the language of logic to express data and programs.

Most of current logic programming languages use first

order logic (FOL).

Prolog – the most popular logic programming language.

Introduction

4

16-Feb-17

Historical Facts

1970-ies:

• Bob Kowalski. "Predicate Logic as a Programming Language".

IFIP Congress, Stockholm

• Alain Colmerauer and his group. Interpreter of the first logic programming language Prolog.

Marseille

Introduction

5

16-Feb-17

Prolog

• Prolog is the main subject of this course

• Used in Artificial Intelligence, Natural Language

Processing, Automated Reasoning, XML Querying...

• Exists in many dialects (Sicstus Prolog, SWI Prolog,

Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual

Prolog, YAP Prolog, Strawberry Prolog)...

• (Almost) all the dialects agree on the “core" part (ISO

Standard for Prolog)

Introduction

6

16-Feb-17

Prolog in Industrial Applications

Clarissa:

• A fully voice-operated procedure browser.

• Developed at NASA.

• Used on the International Space Station.

• Enables astronauts to give full attention to the task while

they navigate through complex procedures using spoken

commands.

• Implemented in SICStus Prolog.

Introduction

7

16-Feb-17

Prolog in Industrial Applications

Some other solutions:

• FleetWatch – fully integrated operations control and schedules

planning solution. Used by 21 international airlines, among

them Comair (USA), Italian branch of Lauda Air, Malev

(Hungary), DHL Europe, Asiana (South Korea), Hainan (China),

Royal Jordanian, Kuwait Airways, Cimber Air (Denmark), etc.

• SCORE – a long term airport capacity management system for

coordinated airports. Successfully Used at IATA Scheduling

Conferences. Users in more than 20 countries.

• ARGOS – a Decision Support System (DSS) for enhancing

Crisis Management for incidents with Chemical, Biological,

Radiological, and Nuclear (CBRN) releases. Introduction

8

16-Feb-17

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

9

16-Feb-17

PROLOG

Used to solve problems involving

• objects, and

• relationships between objects.

Introduction

10

16-Feb-17

Relationships

Example

John owns the book

• The relationship: ownership

• The objects: book, John

Directional:

• John owns the book

• Not: The book owns John

Introduction

11

16-Feb-17

PROLOG

Program can be thought of as a storehouse of facts and rules.

Conversational Language: The user can ask questions about

the set of facts and rules in the PROLOG program.

Introduction

12

16-Feb-17

PROLOG

Sisters Example:

A rule defining sisters and the facts about the people involved.

The user would ask:

Are these two people sisters?

The system would answer

yes (true) or no (false)

Introduction

13

16-Feb-17

Programming in PROLOG

Declaring Facts about objects and their relationships.

Defining Rules about objects and their relationships.

Asking Questions about objects and their relationships.

Introduction

14

16-Feb-17

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

15

16-Feb-17

Parts of Fact

0

Introduction

16

16-Feb-17

Order of Objects

Introduction

17

16-Feb-17

Examples of Facts

Example

Gold is valuable. v a l u a b l e ( g o l d ) Jane is a female. f e m a l e ( j an e ) John owns some gold.

ow ns( john ,go ld ) John is the father of Mary.

f a t h e r ( j o h n , m a r y )

Are these expressions really facts? Is there anything missing?

Introduction

18

16-Feb-17

Interpretation of Names

The name refers to an object.

Semantic Meaning: Given by the programmer.

Syntactic Meaning: a set of characters, as PROLOG sees it.

Introduction

19

16-Feb-17

Interpretation of Names

Name refers to an object.

Name gold can refer to:

a particular lump of gold, or

the chemical element Gold having atomic number 79.

va luab le ( g o ld ) can mean:

that particular lump of gold, named gold, is valuable, or

the chemical element Gold, named gold, is valuable.

The programmer decides (in her usage) the meaning.

Introduction

20

16-Feb-17

Fact Terminology

Introduction

21

16-Feb-17

Database

Definition In PROLOG, database is a collection of facts.

PROLOG draws its knowledge from these facts.

The programmer is responsible for their accuracy.

Introduction

22

16-Feb-17

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

23

16-Feb-17

Questions

The database contains the facts from which the questions are

answered.

A Question can look exactly like a fact:

owns(mary,book) .

The difference is in which mode one is in

Introduction

24

16-Feb-17

Questions

In the interactive question mode (indicated by the question

mark and dash): ? -

Question: ? - owns(mary,book).

Meanins: If mary is interpreted as a person called Mary, and book is

interpreted as some particular book, then

?- owns(mary,book).

means: Does Mary own the book?

Introduction

25

16-Feb-17

Database Search

Example

Facts in the database:

l i k e s ( j o e , f i s h ) .

l i k e s ( j o e , m a r y ) .

l i k es ( m ar y , book ) .

l i k e s ( j o h n , b o o k ) .

Questions: ?- l i k es ( j oe , m oney) . no

?- l i k e s ( j o e , m a r y ) . yes

?- k i n g ( j o h n , f r a n c e ) . n o

Introduction

26

16-Feb-17

Knowledge

The questions are always answered with respect to the

database.

Example

Facts in the database:

human(socrates).

h u m a n ( a r i s t o t l e ) .

a t hen ian ( soc r a tes ) .

Question:

Is Socrates Greek?

?- g reek (soc ra tes )

The answer with respect to this database is No.

Introduction

27

16-Feb-17

Questions

Up until now questions just reflect exactly the database.

Does Mary like the book?

?- l i k es ( m ar y , book ) . More Interesting Question:

What objects does Mary like?

Variables.

Introduction

28

16-Feb-17

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

29

16-Feb-17

Variables

Tiresome to ask about every object:

l i k e s ( j o h n , t h i s ) .

l i k e s ( j o h n , t h a t ) .

Better to ask: What does John like?

or

Does John like X?

(i.e. use variables)

Introduction

30

16-Feb-17

Question With Variables

Does John like X?

? - l i k e s ( j o h n , X ) .

or

? - l i kes( john,Someth ingThatJohnL ikes ) .

X and SomethingThatJohnLikes are variables.

Variable begins with a capital letter.

Introduction

31

16-Feb-17

PROLOG Answer

Database:

l i k e s ( j o h n , f l o w e r s ) .

Question:

? - l i k e s ( j o h n , X ) .

PROLOG answers:

X=f lowers

Introduction

32

16-Feb-17

Many Answers

Database:

l i k e s ( j o h n , f l o w e r s ) .

l i k e s ( j o h n , m a r y ) .

l i k e s ( p a u l , m a r y ) .

Question:

? - l i k e s ( j o h n , X ) .

PROLOG answers:

X=f lowers

and the user acknowledges

X=mary

and the user acknowledges

no

Introduction

33

16-Feb-17

Placemarker

The first match is found: X=f lowers .

The user acknowledges.

From that place on the next match is found (the search continues).

From the place of the last instantiation no more match was found.

Thus answer: no.

Introduction

34

16-Feb-17

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

35

16-Feb-17

Conjunctions

More Complicated Relationships:

Does Mary like John and does John like Mary?

Both Conditions must be fulfilled.

Introduction

36

16-Feb-17

Conjunctions

Database:

l i k e s ( m a r y , f o o d ) .

l i k e s ( m a r y , c o l a ) .

l i k e s ( j o h n , c o l a ) .

l i k e s ( j o h n , m a r y ) .

Comma means Conjunction:

? - l i k e s ( j o h n , m a r y ) , l i k e s ( m a r y , j o h n ) .

Answer: no

A match for l i k es ( j ohn , m a r y )

but none for l i k es ( m ar y , j ohn )

Introduction

37

16-Feb-17

Conjunctions with Variables

Is there anything that both mary and john like?

Find out what Mary likes and then see if John likes it.

? - l i k e s ( m a r y , X ) , l i k e s ( j o h n , X ) .

Introduction

38

16-Feb-17

Backtracking

Find match for the first goal.

Then see if matches the second.

If not, find another match for the first.

See if this matches the second.

…etc.

Introduction

39

16-Feb-17

Match First

Introduction

40

16-Feb-17

,

Cola).

,

Cola).

Match Second

Introduction

41

16-Feb-17

,

Cola). ,

Cola).

Backtrack

Introduction

42

16-Feb-17

,

Cola). ,

Cola).

Cola

Cola

Success

Introduction

43

16-Feb-17

,

Cola). ,

Cola).

Cola

Cola

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

44

16-Feb-17

Rules

• How to express that John likes all people?

• Listing all people?

• likes(john, alfred).

• likes(john, bertrand).

• likes(john, charles).

• likes(john, david).

• etc.

• Not feasible. More compact way: Using rules.

John likes any object provided it is a person.

Introduction

45

16-Feb-17

Rule Examples

Rules state Dependence:

I use an umbrella if there is rain.

Rules Define:

X is a bird if X is an animal and X has feathers.

Introduction

46

16-Feb-17

Formulating Rules

John likes anyone who likes cola.

John likes any Something if it likes cola

John likes X if X likes cola

Introduction

47

16-Feb-17

Rule Syntax

Introduction

48

16-Feb-17

, Cola).

Variable Scope

The occurrences of X within a rule refer to the same object:

l i k e s ( j o h n , X ) : - l i k e s ( X , c o la ) , l i k e s ( X , f o o d ) .

l i k e s ( j o h n , mary):-

l i k e s ( m a r y , c o la ) , l i k e s ( m a r y , f o o d ) .

l i k e s ( j o h n , adam):- l i k es ( adam , c o l a ) , l i k es ( adam,

f o o d ) .

Introduction

49

16-Feb-17

Royal Parents

Example

The parents of X are Y and Z.

Y is the mother.

Z is the father.

Database:

m a l e ( a l b e r t ) .

male(edward).

f e m a l e ( a l i c e ) .

f e m a l e ( v i c t o r i a ) .

p a r e n t s ( e d w a r d , v i c t o r i a , a l b e r t ) .

p a r e n t s ( a l i c e , v i c t o r i a , a l b e r t ) . Introduction

50

16-Feb-17

Sisters

Example

X is a sister of Y if:

X is female,

X has parents M and F,

Y has parents M and F.

Rule:

s i s t e r ( X , Y ) : - f ema le (X ) ,

pa rents (X ,M,F ) ,

pa rents (Y ,M,F ) .

Introduction

51

16-Feb-17

Sisters Question

Question:

? - s i s t e r ( a l i c e , e d w a r d ) .

The question (goal) matches the head of the rule, if one

replaces X with a l i c e and Y with edward.

The instance of the body becomes new

goal: female(a l i c e ) ,

pa ren ts ( a l i c e , M , F ) ,

pa ren ts ( edward, M, F ) .

Introduction

52

16-Feb-17

7 s i s t e r ( a l i c e , e d wa r d )

X0=a l i c e , Y0=edward

3 f e m a l e ( a l i c e ) ,

p a r e n t s ( a l i c e ,M 0 , F0 ) ,

parents(edward,M0,F0).

6 p a r e n t s (a l i c e , M 0 , F0 ) ,

parents(edward,M0,F0).

M0 = v i c t o r i a , F0=alber t

5 p a r e n t s ( e d w a r d , v i c t o r i a , a l b e r t ) .

Is Alice Edward’s Sister?

( 1 ) m a l e ( a l b e r t ) .

(2) male(edward).

( 3 ) f e m a l e ( a l i c e ) .

( 4 ) f e m a l e ( v i c t o r i a ) .

(5) parents (edward ,

v i c t o r i a , a l b e r t ) .

( 6 ) p a r e n t s ( a l i c e ,

v i c t o r i a , a l b e r t ) .

( 7 ) s i s t e r ( X , Y ) : -

f em a le (X) ,

parents (X,M,F ) ,

pa ren t s (Y ,M,F ) .

Introduction

53

16-Feb-17

Is Alice Edward’s Sister?

7 s i s t e r ( a l i c e , e d wa r d )

X0=a l i c e , Y0=edward

3 f e m a l e ( a l i c e ) ,

p a r e n t s ( a l i c e ,M 0 , F0 ) ,

parents(edward,M0,F0).

6 p a r e n t s (a l i c e , M 0 , F0 ) ,

parents(edward,M0,F0).

M0 = v i c t o r i a , F0=alber t

5 p a r e n t s ( e d w a r d , v i c t o r i a , a l b e r t ) .

Introduction

54

16-Feb-17

( 1 ) m a l e ( a l b e r t ) .

(2) male(edward).

( 3 ) f e m a l e ( a l i c e ) .

( 4 ) f e m a l e ( v i c t o r i a ) .

(5) parents (edward ,

v i c t o r i a , a l b e r t ) .

( 6 ) p a r e n t s ( a l i c e ,

v i c t o r i a , a l b e r t ) .

( 7 ) s i s t e r ( X , Y ) : -

f em a le (X) ,

parents (X,M,F ) ,

pa ren t s (Y ,M,F ) .

Who’s Sister Is Alice?

7 s i s t e r ( a l i c e , X )

X0=a l i ce , Y0=X

3 f e m a l e ( a l i c e ) ,

p a r e n t s ( a l i c e ,M 0 , F0 ) ,

parents (X,M0,F0) .

6 p a r e n t s (a l i c e , M 0 , F0 ) ,

parents (X,M0,F0) .

M0=vic tor ia F0=alber t

5 p a r e n t s ( X , v i c t o r i a , a l b e r t ) .

X=edward

Answer: X = edward. Introduction

55

16-Feb-17

( 1 ) m a l e ( a l b e r t ) .

(2) male(edward).

( 3 ) f e m a l e ( a l i c e ) .

( 4 ) f e m a l e ( v i c t o r i a ) .

(5) parents (edward ,

v i c t o r i a , a l b e r t ) .

( 6 ) p a r e n t s ( a l i c e ,

v i c t o r i a , a l b e r t ) .

( 7 ) s i s t e r ( X , Y ) : -

f em a le (X) ,

parents (X,M,F ) ,

pa ren t s (Y ,M,F ) .

Useful Links

• SWI-Prolog:

• http://www.swi-prolog.org/

• SWI-Prolog Editor (Windows, SWI-x86 only)

http://lakk.bildung.hessen.de/netzwerk/faecher/informatik/swiprolog/ind

exe.html

• Prolog mode for (X)Emacs:

http://turing.ubishops.ca/home/bruda/emacs-prolog/

• Prolog newsgroup:

http://groups.google.com/groups?group=comp.lang.prolog

• Links from SWI-Prolog Web page:

http://www.swi-prolog.org/Links.html

Introduction

56

16-Feb-17

top related