Lecture 5: Prolog Programming Techniques -...

93
Lecture 5: Prolog Programming Techniques COMP24412: Symbolic AI Martin Riener School of Computer Science, University of Manchester, UK February 2019 Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 1 / 34

Transcript of Lecture 5: Prolog Programming Techniques -...

Page 1: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Lecture 5: Prolog Programming TechniquesCOMP24412: Symbolic AI

Martin Riener

School of Computer Science, University of Manchester, UK

February 2019

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 1 / 34

Page 2: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

What happened so far

Prolog is a Turing complete, logic based programming languageQueries to Prolog program yields a sequence of answer substitutionsAnswers are found by backward chaining, trying the rules in order ofappearanceSuitable rules to apply are found via unificationFunctions allow the expression of arbitrary large terms, e.g. lists

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 2 / 34

Page 3: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Overview

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 3 / 34

Page 4: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 4 / 34

Page 5: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Finding good predicate names

Programs are written to a file (queries happen at the prompt)All rules defining the same predicate must appear in successionPrograms are loaded via consult(’program.pl’).

Careful: editors might mistake the extension for Perl

Predicates describe relations, don’t assume a direction of evaluation:Compare find(car, List) to member_of(car, List)

Use short phrases for each argument, use _ to seperate them:Compare flight(X,Y,Z) to flightno_from_to(X,Y,Z)

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 5 / 34

Page 6: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Finding good predicate names

Programs are written to a file (queries happen at the prompt)All rules defining the same predicate must appear in successionPrograms are loaded via consult(’program.pl’).

Careful: editors might mistake the extension for Perl

Predicates describe relations, don’t assume a direction of evaluation:Compare find(car, List) to member_of(car, List)

Use short phrases for each argument, use _ to seperate them:Compare flight(X,Y,Z) to flightno_from_to(X,Y,Z)

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 5 / 34

Page 7: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: inductive reasoning

Formulate simplest factsFind rules that extend smaller terms to (slightly) larger terms

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 6 / 34

Page 8: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

A ball game

Anne

Jackie

Peter

Fang

Iana

Paolo

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 7 / 34

Page 9: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Representing the graph

child_throwsto(peter, iana).

child_throwsto(iana, anne).

child_throwsto(fang, paolo).

child_throwsto(paolo, jackie).

child_throwsto(anne, paolo).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 8 / 34

Page 10: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Tossing the ball around

ballfrom_reaches(From,To) :-

child_throwsto(From,To).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 9 / 34

Page 11: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Tossing the ball around

ballfrom_reaches(From,To) :-

child_throwsto(From,To).

ballfrom_reaches(From,To) :-

child_throwsto(From,Neighbour),

ballfrom_reaches(Neighbour,To).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 9 / 34

Page 12: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Tossing the ball around

Why not?

ballfrom_reaches(From,To) :-

ballfrom_reaches(From,Neighbour),

ballfrom_reaches(Neighbour,To).

No constraint on the solution in the first recursion stepLeads to an infinite recursion

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 9 / 34

Page 13: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 10 / 34

Page 14: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Prolog’s execution mechanism

Prolog applies backwards reasoning (start with the query)Multiple goals are derived left-to-rightSearch for unifiers with rule heads, top-to-bottomHead unifies:

try to derive the goals of the rule bodycontinue fulfilling the original goal

No head unifies: backtrack and try next rule!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 11 / 34

Page 15: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

A simple query

?- ballfrom_reaches(iana,paolo).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 12 / 34

Page 16: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

A simple query

?- ballfrom_reaches(iana,paolo).

true

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 12 / 34

Page 17: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

A simple query

?- ballfrom_reaches(iana,paolo).

true ;

false.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 12 / 34

Page 18: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

A simple query

?- ballfrom_reaches(iana,paolo).

true ;

false.

What is happening?

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 12 / 34

Page 19: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)ballfrom_reaches(iana,paolo)

trying ruleballfrom_reaches(From,To) :-

child_throwsto(From,To).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 20: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)ballfrom_reaches(iana,paolo)

trying ruleballfrom_reaches(From,To) :-

child_throwsto(From,To).

instance: From=iana, To=paolo

ballfrom_reaches(iana,paolo) :-

child_throwsto(iana,paolo).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 21: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(peter, iana).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 22: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(peter, iana).

instance: iana ‰ peter – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 23: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(iana, anne).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 24: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(iana, anne).

instance: paolo ‰ anne – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 25: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(fang, paolo).

instance: iana ‰ fang – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 26: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(paolo, jackie).

instance: iana ‰ paolo – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 27: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(anne, paolo).

instance: iana ‰ anne – backtrack!no more child_throwstos – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 28: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,paolo)

trying rulechild_throwsto(anne, paolo).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 29: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)ballfrom_reaches(iana,paolo)

trying ruleballfrom_reaches(From,To) :-

child_throwsto(From,Neighbour),

ballfrom_reaches(Neighbour,To).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 30: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)ballfrom_reaches(iana,paolo)

trying ruleballfrom_reaches(From,To) :-

child_throwsto(From,Neighbour),

ballfrom_reaches(Neighbour,To).

instance: From=iana, To=paoloballfrom_reaches(iana,paolo) :-

child_throwsto(iana,Neighbour),

ballfrom_reaches(Neighbour,paolo).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 31: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying rulechild_throwsto(peter, iana).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 32: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying ruleinstance: iana ‰ peter – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 33: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying rulechild_throwsto(iana, anne).

instance: Neighbour=anne – next goal!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 34: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying rulechild_throwsto(peter, iana).

instance: anne ‰ peter – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 35: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying rulechild_throwsto(iana, anne).

instance: anne ‰ iana – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 36: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying rulechild_throwsto(fang, paolo).

instance: anne ‰ fang – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 37: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying rulechild_throwsto(paolo, jackie).

instance: anne ‰ paolo – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 38: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying rulechild_throwsto(anne, paolo).

instance: no substitution needed

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 39: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)

no goals! tell the user!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 40: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)

user wants more solutions, backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 41: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(anne,paolo)

trying ruleno more child_throwstos – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 42: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying rulechild_throwsto(fang, paolo).

instance: iana ‰ fang – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 43: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying ruleinstance: iana ‰ paolo – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 44: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying ruleinstance: iana ‰ anne – backtrack!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 45: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)child_throwsto(iana,Neighbour),ballfrom_reaches(Neighbour,paolo)

trying ruleetc. etc. etc.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 46: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Step-by-step execution

goal(s)

all paths exhausted, report false!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 13 / 34

Page 47: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 14 / 34

Page 48: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Can we make the steps visible?

Introduce an additional argument:

ballfrom_reaches_via(From,To,direct(To)) :-

child_throwsto(From,To).

ballfrom_reaches_via(From,To,next(Neighbour,Others)) :-

child_throwsto(From,Neighbour),

ballfrom_reaches_via(Neighbour,To,Others).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 15 / 34

Page 49: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Can we make the steps visible?

Query:

?- ballfrom_reaches_via(iana,paolo, Path).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 15 / 34

Page 50: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Can we make the steps visible?

Query:

?- ballfrom_reaches_via(iana,paolo, Path).

Path = next(anne, direct(paolo)) ;

false.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 15 / 34

Page 51: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Can we make the steps visible?

Query:

?- ballfrom_reaches_via(From,To, next(A,next(B,direct(C)))).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 15 / 34

Page 52: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Can we make the steps visible?

Query:

?- ballfrom_reaches_via(From,To, next(A,next(B,direct(C)))).

From = peter,

To = C, C = paolo,

A = iana,

B = anne ;

From = iana,

To = C, C = jackie,

A = anne,

B = paolo ;

false.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 15 / 34

Page 53: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Data-structures: Lists

We used next/2 and direct/1 to track pathsTerm graph of next(iana, next(anne,direct(paolo))):

next

next

direct

iana

anne

paolo

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 16 / 34

Page 54: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Data-structures: Lists

We used next/2 and direct/1 to track pathsTerm graph of next(iana, next(anne,direct(paolo))):

next

next

direct

iana

anne

paolo

What about an empty path?

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 16 / 34

Page 55: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Data-structures: Lists

Replace direct/1 with r s/0Term graph of next(iana, next(anne,next(paolo,r s))) :

next

next

next

r s

iana

anne

paolo

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 16 / 34

Page 56: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Data-structures: Lists

Rename next/2 to r | s/2,Term graph of [iana | [anne | [paolo | [] ]]] :

r | s

r | s

r | s

r s

iana

anne

paolo

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 16 / 34

Page 57: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Data-structures: Lists

Structure built over r s/0 and r | s/2: linked listDatatype:

“The empty list is a list”isa_list(r s).“Any head prepended to a tail list is a list”isa_list([Head | Tail]) :-

isa_list(Tail).

Special list notation:

No need to append to empty list:[Head | r s] = [Head]Use , to avoid writing the tail in squarebrackets:[X | [Y | Tail] ] = [X, Y | Tail]

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 17 / 34

Page 58: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Properties of linked lists

Access to head: Op1qList = [Head | _ ]

Access to tail: Op1qList = [_ | Tail ]

Traversal: Opnqmember_of(X,List)

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 18 / 34

Page 59: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 19 / 34

Page 60: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: member_of/2

Task:Create a predicate member_of(X,List) that is true whenever X is anelement of list List.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 20 / 34

Page 61: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: member_of/2

Task:Create a predicate member_of(X,List) that is true whenever X is anelement of list List.Find base case(s):

member_of(X,[Head|_Tail]) :- % X is member of a list

X = Head. % if X is the head of the list

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 20 / 34

Page 62: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: member_of/2

Task:Create a predicate member_of(X,List) that is true whenever X is anelement of list List.Find base case(s):

member_of(X,[X|_Tail]). % directly unify in head

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 20 / 34

Page 63: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: member_of/2

Task:Create a predicate member_of(X,List) that is true whenever X is anelement of list List.Find base case(s):

member_of(X,[X|_Tail]). % directly unify in head

Find recursive case(s):“Given a smaller term, how to extend it to a larger one?”

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 20 / 34

Page 64: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: member_of/2

Task:Create a predicate member_of(X,List) that is true whenever X is anelement of list List.Find base case(s):

member_of(X,[X|_Tail]). % directly unify in head

Find recursive case(s):“Given a smaller term, how to extend it to a larger one?”

member_of(X,[_Head|Tail]) :- % X is member of the list

member_of(X, Tail). % if X is member of the tail

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 20 / 34

Page 65: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: nonmember_of/2

Task:Create a predicate nonmember_of(X,List) that is true whenever X isnot an element of list List.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 21 / 34

Page 66: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: nonmember_of/2

Task:Create a predicate nonmember_of(X,List) that is true whenever X isnot an element of list List.Find base case(s):

nonmember_of(_X,[]). % Any element is not in the empty list

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 21 / 34

Page 67: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: nonmember_of/2

Task:Create a predicate nonmember_of(X,List) that is true whenever X isnot an element of list List.Find base case(s):

nonmember_of(_X,[]). % Any element is not in the empty list

Find recursive case(s):

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 21 / 34

Page 68: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: nonmember_of/2

Task:Create a predicate nonmember_of(X,List) that is true whenever X isnot an element of list List.Find base case(s):

nonmember_of(_X,[]). % Any element is not in the empty list

Find recursive case(s):

nonmember_of(X,[Head|Tail]) :-

% fill in constraint

nonmember_of(X, Tail). % X is not in the tail

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 21 / 34

Page 69: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Recursively defined predicates: nonmember_of/2

Task:Create a predicate nonmember_of(X,List) that is true whenever X isnot an element of list List.Find base case(s):

nonmember_of(_X,[]). % Any element is not in the empty list

Find recursive case(s):

nonmember_of(X,[Head|Tail]) :-

dif(X,Head), % X is different from the head

nonmember_of(X, Tail). % X is not in the tail

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 21 / 34

Page 70: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 22 / 34

Page 71: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

More ball games

Anne

Jackie

Peter

Fang

Iana

Paolo

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 23 / 34

Page 72: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Representing the graph

child_throwsto(anne, peter).

child_throwsto(peter, iana).

child_throwsto(iana, anne).

child_throwsto(jackie, fang).

child_throwsto(fang, paolo).

child_throwsto(paolo, jackie).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 24 / 34

Page 73: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Tossing the ball around

ballfrom_reaches_lvia(From,To,[To]) :-

child_throwsto(From,To).

ballfrom_reaches_lvia(From,To,[Neighbour|Path]) :-

child_throwsto(From,Neighbour),

ballfrom_reaches_lvia(Neighbour,To,Path).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 25 / 34

Page 74: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Some Queries

?- ballfrom_reaches_lvia(jackie, paolo,Path).

Path = [fang, paolo] ;

Path = [fang, paolo, jackie, fang, paolo] ;

Path = [fang, paolo, jackie, fang, paolo, jackie, fang, paolo] ;

Path = [fang, paolo, jackie, fang, paolo, jackie, fang, paolo, jackie|...] ;

Does it ever stop?

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 26 / 34

Page 75: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Some Queries

Does it ever stop?Try to append , false. to the query:

?- ballfrom_reaches_lvia(jackie, paolo,Path), false.

% Hit Ctrl+C

Action (h for help) ? abort

% Execution Aborted

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 26 / 34

Page 76: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Some Queries

Does it ever stop?The ,false enforces backtracking, visiting all possible answersFinite solution space: interpreter returns falseRemember: P ^K Ñ K for any P in FOLInfinite answer sequence: infinite derivation (non-termination)Infinite set of answers leads to non-termination

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 26 / 34

Page 77: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Non-termination spreads

Consider

ballfrom_reaches2(From, To) :-

ballfrom_reaches_lvia(From, To, _Path).

Even worse:

?- ballfrom_reaches2(jackie, anne).

% unreachable, but explores infinitely long paths (non-termination)

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 27 / 34

Page 78: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Non-termination spreads

Considerballfrom_reaches2(From, To) :-

ballfrom_reaches_lvia(From, To, _Path).

Execution:?- ballfrom_reaches2(jackie, paolo).

true ;

true ;

true ;

true % abort

?- ballfrom_reaches2(jackie, paolo), false.

% non-termination

Even worse:

?- ballfrom_reaches2(jackie, anne).

% unreachable, but explores infinitely long paths (non-termination)

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 27 / 34

Page 79: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Reordering goals can change termination behaviour

?- Xs = [something], isa_list(Xs).

Xs = [something].

?- isa_list(Xs), Xs = [something].

Xs = [something] ;

% loops enumerating all lists

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 28 / 34

Page 80: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Consequences of non-termination

A recursive goal without instantiated variables always loopsPutting always terminating predicates as early goals often helps(Restricts what gets passed to recursive goals)Properties of pure, monotonic Prolog programs (no non-logicalelements, no negation)

Generalizing a non-terminating rule / query can not lead to terminatione.g.: isa_list([1,2,3|Xs]) to isa_list(Xs)Instatiating a rule query can improve terminatione.g. isa_list([1,2,3|Xs]) to isa_list([1,2,3|notalist])Removing goals from a rule can only increase the number of solutions

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 29 / 34

Page 81: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Outline

1 How to write a program

2 Execution of Prolog programs

3 Beyond Datalog: Lists

4 Two simple predicates over lists

5 Termination

6 Accumulators

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 30 / 34

Page 82: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Improving the termination behaviour of the ball game

Queries cannot terminate with an infinite set of pathsIdea: consider only acyclic paths (fintely many for finite graphs)Add additional argument to pass history to recursive goals

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 31 / 34

Page 83: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Improving the termination behaviour of the ball game

Queries cannot terminate with an infinite set of pathsIdea: consider only acyclic paths (fintely many for finite graphs)Add additional argument to pass history to recursive goals

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 31 / 34

Page 84: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Improving the termination behaviour of the ball game

Queries cannot terminate with an infinite set of pathsIdea: consider only acyclic paths (fintely many for finite graphs)Add additional argument to pass history to recursive goals

aballfrom_reaches_lvia_acc(From,To,[To],Acc) :-

child_throwsto(From,To),

% ...

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 31 / 34

Page 85: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Improving the termination behaviour of the ball game

Queries cannot terminate with an infinite set of pathsIdea: consider only acyclic paths (fintely many for finite graphs)Add additional argument to pass history to recursive goals

aballfrom_reaches_lvia_acc(From,To,[To],Acc) :-

child_throwsto(From,To),

% ...

aballfrom_reaches_lvia_acc(From,To,[Neighbour | Others], Acc) :-

child_throwsto(From,Neighbour),

% ...

aballfrom_reaches_lvia_acc(Neighbour,To,Others, [Neighbour|Acc]).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 31 / 34

Page 86: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Improving the termination behaviour of the ball game

Queries cannot terminate with an infinite set of pathsIdea: consider only acyclic paths (fintely many for finite graphs)Add additional argument to pass history to recursive goals

aballfrom_reaches_lvia_acc(From,To,[To],Acc) :-

child_throwsto(From,To),

nonmember_of(To, Acc).

aballfrom_reaches_lvia_acc(From,To,[Neighbour | Others], Acc) :-

child_throwsto(From,Neighbour),

nonmember_of(From, Acc),

aballfrom_reaches_lvia_acc(Neighbour,To,Others, [Neighbour|Acc]).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 31 / 34

Page 87: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Hiding the accumulator

This is still too general:

?- aballfrom_reaches_lvia_acc(jackie, paolo, Path, Acc).

Path = [fang, paolo],

Acc = [] ;

Path = [fang, paolo],

Acc = [_1204],

dif(_1204, paolo),

dif(_1204, jackie) ;

% ...

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 32 / 34

Page 88: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Hiding the accumulator

This is still too general:

?- aballfrom_reaches_lvia_acc(jackie, paolo, Path, Acc).

Path = [fang, paolo],

Acc = [] ;

Path = [fang, paolo],

Acc = [_1204],

dif(_1204, paolo),

dif(_1204, jackie) ;

% ...

Accumulator can have an arbitrary tail – start with an empty Acc

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 32 / 34

Page 89: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Hiding the accumulator

Accumulator can have an arbitrary tail – start with an empty Acc

?- aballfrom_reaches_lvia_acc(jackie, paolo, Path, []).

Path = [fang, paolo] ;

false.

?- aballfrom_reaches_lvia_acc(jackie, anne, Path, []).

false.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 32 / 34

Page 90: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Hiding the accumulator

Accumulator can have an arbitrary tail – start with an empty Acc

?- aballfrom_reaches_lvia_acc(jackie, paolo, Path, []).

Path = [fang, paolo] ;

false.

?- aballfrom_reaches_lvia_acc(jackie, anne, Path, []).

false.

Hide the accumulator from the user

aballfrom_reaches_lvia(From, To, Neighbour) :-

aballfrom_reaches_lvia_acc(From, To, Neighbour, []).

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 32 / 34

Page 91: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Hiding the accumulator

The new predicate always terminates:

?- aballfrom_reaches_lvia(From, To, Path), false.

false.

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 32 / 34

Page 92: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

Summary

Prolog’s execution order weakens logical propertiesSwapping the order of goals does not influence the set of solutions buttermination properties may changeAppending false is a simple check for terminationNarrowing answer set is an easy way to improve termination propertiesAccumulators pass information about the current goals to recursivegoals

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 33 / 34

Page 93: Lecture 5: Prolog Programming Techniques - …syllabus.cs.manchester.ac.uk/.../2018slides/lecture5.pdfLecture5: PrologProgrammingTechniques COMP24412: SymbolicAI MartinRiener School

That’s all for today!

Martin Riener (Manchester) Lecture 5: Prolog Programming Techniques February 2019 34 / 34