56
Ahmed Sallam Slides based on original lecture slides Dr. Temur Kutsia LOGIC PROGRAMMING Chapter 1: Introduction

LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

  • Upload
    others

  • View
    67

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Ahmed SallamSlides based on original lecture slides Dr. Temur Kutsia

LOG

IC PRO

GRA

MM

ING

Chapter 1: Introduction

Page 2: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

"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

2/12/2015

Page 3: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 4: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 5: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 6: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 7: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

• Implemented in SICStus Prolog .

Introduction

7

2/12/2015

Page 8: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Prolog in Industrial Applications

Some other solutions :• FleetWatch – fully integrated operations control and schedules

planning solution. Used by 21 international airlines, amongthem 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 SchedulingConferences. Users in more than 20 countries .

• ARGOS – a Decision Support System (DSS) for enhancingCrisis Management for incidents with Chemical, Biological,Radiological, and Nuclear (CBRN) releases .

Introduction

8

2/12/2015

Page 9: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

9

2/12/2015

Page 10: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

PROLOG

Used to solve problems involving• objects, and• relationships between objects .

Introduction

10

2/12/2015

Page 11: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Relationships

ExampleJohn owns the book

• The relationship: ownership• The objects: book, John

Directional :• John owns the book• Not: The book owns John

Introduction

11

2/12/2015

Page 12: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 13: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 14: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 15: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

15

2/12/2015

Page 16: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Parts of Fact

0Introduction

16

2/12/2015

Page 17: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Order of Objects

Introduction

17

2/12/2015

Page 18: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Examples of Facts

ExampleGold is valuable .v a l u a b l e ( g o l d )

Jane is a female .f ema le ( j ane )

John owns some gold .owns( 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

2/12/2015

Page 19: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 20: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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 .

v a l u a b l e ( g o l d ) 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

2/12/2015

Page 21: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Fact Terminology

Introduction

21

2/12/2015

Page 22: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Database

DefinitionIn PROLOG, database is a collection of facts .

PROLOG draws its knowledge from these facts . The programmer is responsible for their accuracy .

Introduction

22

2/12/2015

Page 23: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

23

2/12/2015

Page 24: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 25: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 26: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Database Search

ExampleFacts 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 e s ( m a r y ,b o o k ) .l i k e s ( j o h n , b o o k ) .

Questions :?- l i k e s ( j o e , m o n e y ) . 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

2/12/2015

Page 27: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Knowledge

The questions are always answered with respect to thedatabase .

ExampleFacts in the database:human(socrates) .h u m a n ( a r i s t o t l e ) .a t h e n i a n ( s o c r a te s ) .

Question :Is Socrates Greek ??- g reek (soc ra tes )

The answer with respect to this database is No.Introduction

27

2/12/2015

Page 28: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Questions

Up until now questions just reflect exactly the database .

Does Mary like the book ??- l i k e s ( m a r y ,b o o k ) .

More Interesting Question: What objects does Mary like ?

Variables .

Introduction

28

2/12/2015

Page 29: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

29

2/12/2015

Page 30: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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?orDoes John like X ?(i.e. use variables)

Introduction

30

2/12/2015

Page 31: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 32: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 33: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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 lowersand the user acknowledgesX=maryand the user acknowledgesno

Introduction

33

2/12/2015

Page 34: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 35: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

35

2/12/2015

Page 36: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Conjunctions

More Complicated Relationships :

Does Mary like John and does John like Mary?

Both Conditions must be fulfilled .

Introduction

36

2/12/2015

Page 37: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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: noA match for l i k e s ( j o h n , m a r y )but none for l i k e s ( m a r y , j o h n )

Introduction

37

2/12/2015

Page 38: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 39: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 40: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Match First

Introduction

40

2/12/2015

, Cola).

, Cola).

Page 41: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Match Second

Introduction

41

2/12/2015

, Cola).

, Cola).

Page 42: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Backtrack

Introduction

42

2/12/2015

, Cola).

, Cola).

Cola

Cola

Page 43: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Success

Introduction

43

2/12/2015

, Cola).

, Cola).

Cola

Cola

Page 44: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Contents

Basics of PROLOG

Facts

Questions

Variables

Conjunction

Rules

Introduction

44

2/12/2015

Page 45: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 46: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 47: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 48: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Rule Syntax

Introduction

48

2/12/2015

, Cola).

Page 49: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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 l a ) , 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 l a ) , 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 kes (adam , c o l a ) , l i kes (adam, f o o d ) .

Introduction

49

2/12/2015

Page 50: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 51: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Sisters

ExampleX 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 ren ts (X ,M,F) ,pa ren ts (Y ,M,F) .

Introduction

51

2/12/2015

Page 52: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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

2/12/2015

Page 53: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

7 s i s t e r ( a l i c e , e d w a r d )X0=a l i ce ,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 , F 0 ) ,parents(edward,M0,F0).

6 p a r e n t s ( a l i c e , M 0 , F 0 ) ,parents(edward,M0,F0).

M 0 = v i c t o r i a ,F0=a lber 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 ema le (X) ,

pa ren ts (X ,M,F) ,

pa ren ts (Y ,M,F ) .

Introduction

53

2/12/2015

Page 54: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

Is Alice Edward’s Sister?

7 s i s t e r ( a l i c e , e d w a r d )X0=a l i ce ,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 , F 0 ) ,parents(edward,M0,F0).

6 p a r e n t s ( a l i c e , M 0 , F 0 ) ,parents(edward,M0,F0).

M 0 = v i c t o r i a ,F0=a lber 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

2/12/2015

( 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 ema le (X) ,

pa ren ts (X ,M,F) ,

pa ren ts (Y ,M,F ) .

Page 55: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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 , F 0 ) ,parents (X,M0,F0) .

6 p a r e n t s ( a l i c e , M 0 , F 0 ) ,parents (X,M0,F0) .

M0=v ic to r iaF0=a lber 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

2/12/2015

( 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 ema le (X) ,

pa ren ts (X ,M,F) ,

pa ren ts (Y ,M,F ) .

Page 56: LOGIC PROGRAMMING Chapter 1: Introduction Ahmed Sallam · 2018. 9. 6. · Prolog IV, BinProlog, Ciao Prolog, Prolog LPA, Visual Prolog, YAP Prolog, Strawberry Prolog (... • (Almost)

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/indexe.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

2/12/2015