topic b: backtracking and lists - university of calgary in...

26
Topic B: Backtracking and Lists 1

Upload: others

Post on 17-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Topic B: Backtracking and Lists

1

Page 2: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Recommended Exercises and Readings

• From Programming in Prolog (5th Ed.)• Readings:

• Chapter 3

2

Page 3: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Searching for the Answer

• In order for a Prolog program to report the correct result, it must be:• Logically Correct• Procedurally Correct

• Why is there a difference?• In theory, the order in which clauses in a conjunction or disjunction are evaluated doesn’t matter

• In practice, Prolog evaluates clauses in a defined order• Impacts how quickly an answer is found• Or even if it is ever found

3

Page 4: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Searching for the Answer Order

• Search order• Prolog searches the knowledge base from top to bottom

• Left to right within each line• Once a matching line is found, and the left‐most rule is satisfied, then the next rule on the line is attempted

• If a term fails, backtracking is performed• Move back one level and try another value• If all of the values have been tried, move back another level• …• Until all combinations have been tested

4

Page 5: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Backtracking Example

• Consider the following knowledgebase:• sings(alice).• sings(bob).• playsGuitar(charles).• playsGuitar(bob).

• Consider the query:• sings(X), playsGuitar(X).

• How does Prolog satisfy this goal?

5

Page 6: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Watching Prolog Work

• Prolog includes a debugger• Enable it with trace.• Display it with notrace.

• Within the debugger• <enter> creeps through each step• s skips to the answer for the current call• l leaps to the next answer• a aborts debugging of the query

6

Page 7: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Backtracking

• Prolog uses brute force to check all possibilities• Facts and rules are processed from top to bottom• Clauses within a rule are satisfied from left to right• When a failure occurs, backtracking undoes the most recent match and choses another value, or moves back one level if there are no further values

• This process repeats until an answer is found, or all possibilities have been exhausted

7

Page 8: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Recursion

• Recursion is critically important in logic programming• Recursive rules use the same functor on both sides of the :‐ symbol• Recursive rules must have a base case, which is either:

• A version of the rule that is not recursive, or• A smaller version of the problem results in failure

8

Page 9: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Recursion Example

• Recall our family tree knowledge base• How do we define the grandparent relationship?

• How do we define the great‐grandparent relationship?

• How do we define the ancestor relationship?

9

Page 10: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Recursion Example

• Order matters!• What happens if we place the base case after the recursive case?

• What happens if we make the recursive call first in the recursive case?

10

Page 11: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

• Consider a transportation company that has the following routes between cities in Alberta

Routing Example

11

Grand Prairie Edmonton

Lloydminster

Camrose

Red Deer

Calgary

Banff

Medicine Hat

Lethbridge

Page 12: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Routing Example

• Create a Prolog knowledgebase that represents the routes

12

Page 13: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Routing Example

• Create a Prolog rule that allows one to identify a route form any city to any other city

• What are the challenges / limitations?

13

Page 14: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Lists

• Lists in Prolog:• Similar to Haskell, both structurally and syntactically• Represented as a head element followed by the tail of the list• Elements are enclosed in square brackets

• [] represents the empty list• [1, 2, 3] is a list of 3 integers• [H | T] is equivalent to (h : t)

14

Page 15: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Lists

• Matches are performed on Prolog lists like other variables• List matching examples:

• [1, 2, 3] = [A, B, C].• [1, 2, 3] = [A, B | C].• [1, 2, 3] = [A, B, C | D].• [1, 2, 3] = [A | B].• [1, 2, 3] = [A, B].• [A, 2] = [1, B].• [A, 2] = [1, B | C].• [A, B] = [1, C | D].

15

Page 16: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

List Functors

• Prolog includes numerous built‐in list predicates• Exact list of predicates varies from implementation to implementation• Some are “more standard” than others• gProlog includes:

• length• member• reverse• permutation• sublist• maplist• …

16

Page 17: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Length of a List

• The length/2 predicate determines how many atoms are in a list• First parameter: A list• Second parameter: A non‐negative integer length• Common uses:

• First and second parameters are both instantiated• The predicate succeeds when the second parameter is the length of the list

• The first parameter is a list and the second parameter is an uninstantiated variable• The variable is instantiated to an integer equal to the length of the list

• Less common use:• The first parameter is an uninstantiated variable and the second parameter is an integer

• The variable is instantiated to a list of the indicated length

17

Page 18: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

List Membership

• The member/2 predicate determines whether or not a value is present in a list• First parameter: A term• Second parameter: A list of terms• Common uses:

• The first parameter is a term, the second is a list• Succeeds if and only if the term is present in the list

• The first parameter is an uninstantiated variable, the second is a list• The variable is instantiated to each term in sequence (by backtracking)

• Less common uses:• The first parameter is a term, the second is an uninstantiated variable

• The variable is instantiated with an infinite sequence of lists containing the value (by backtracking)

18

Page 19: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

List Membership

• Write a predicate that provides the same functionality as member/2• It will be recursive

19

Page 20: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Negation

• How can we negate the outcome of a predicate?• Specifically, how do we test if an element is not present in a list?

• The \+ predicate reverses the outcome of a predicate• Failure is treated as success

• Examples:• member(1, [1, 2, 3]). • member(4, [1, 2, 3]).• \+member(1, [1, 2, 3]).• \+member(4, [1, 2, 3]).

20

Page 21: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Creating Bigger Lists

• One element, H, can be added to the front of an existing list, T• NewList = [H | T]

• Multiple items H1, H2, H3 can be added to the front of T• NewList = [H1, H2, H3 | T]

• Two lists can be concatenated with the append/3 predicate

21

Page 22: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Creating Bigger Lists

• Normal uses of the append/3 predicate:• The first parameter is a list, the second parameter is a list, the third parameter is an uninstantiated variable• The third parameter is an uninstantiated variable that is instantiated to a list containing all of the elements in first followed by all of the elements in second

• One of the first two parameters is a list, the other is an uninstantiatedvariable, the third is a list• The uninstantiated variable is instantiated to a list containing the elements necessary to form the list provided as the third parameter

• The first two parameters are uninstantiated variables, the third parameter is a list• All combinations of lists that can be concatenated to form the third list are generated in sequence (by backtracking)

22

Page 23: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Creating Bigger Lists

• Write a predicate that provides the same functionality as append/3• It will be recursive

23

Page 24: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Other Useful List Predicates

• permutation/2: Is one list a permutation of another• Does it have the same elements but in a different order?• Generate all permutations of a list (by backtracking)

• sublist/2: Is the first list a sublist of another?• Are all elements in first found in second in the same order (possibly with other elements in between them)?

• Generate all sublists of a list (by backtracking)

24

Page 25: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Routing Example

• Create a Prolog rule that allows one to identify a route form any city to any other city• Ensure that the route only includes each city once• Record the route in a list

25

Page 26: Topic B: Backtracking and Lists - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/449_P17/Notes/TopicB... · 2017-06-15 · Backtracking •Prologuses brute force

Summary

• Evaluation order is important• A program can be logically correct while being procedurally incorrect

• Such a program will not return a correct result!

• Backtracking is performed when a failure occurs• Order of facts, rules and clauses within a rule determine how quickly an answer is found (or if it is ever found) and the order in which the answers are found

• Lists can hold an arbitrary number of terms (including duplicates)• Lists are represented as a head element, followed by a tail• Predicates that operate on lists are often recursive

26