程序代写代做代考 COMP4418: Knowledge Representation and Reasoning

COMP4418: Knowledge Representation and Reasoning
Introduction to Prolog IV Controlling Execution
Maurice Pagnucco
School of Computer Science and Engineering University of New South Wales
NSW 2052, AUSTRALIA morri@cse.unsw.edu.au
Reference: Ivan Bratko, Prolog Programming for Artificial Intelligence, Addison- Wesley, 2001. Chapter 6.
COMP4418 ⃝c UNSW, 2019

COMP4418, Monday 30 September, 2019 Introduction to Prolog IV: Controlling Execution 1
The Cut Operator (!)
􏰎 Sometimes we need a way of preventing Prolog finding all solutions 􏰎 The cut operator is a built-in predicate that prevents backtracking
􏰎 It violates the declarative reading of a Prolog program
􏰎 Use it VERY sparingly!!
COMP4418 ⃝c UNSW, 2019 Generated: 15 September 2019

COMP4418, Monday 30 September, 2019 Introduction to Prolog IV: Controlling Execution 2
Backtracking
lectures(adnan, Subject), studies(Student, Subject)?
Subject = 4418
Student = jane
Subject = 9518
Student = jack
lectures(adnan, Subject), studies(Student, Subject)?
lectures(adnan, 9414)
lectures(adnan, 9518)
studies(jane, 9414)
studies(jack, 9518)
COMP4418 ⃝c UNSW, 2019
Generated: 15 September 2019

COMP4418, Monday 30 September, 2019 Introduction to Prolog IV: Controlling Execution 3
Cut Prunes the Search
􏰎 Prevents backtracking to goals left of the cut by throwing away remaining choice points
COMP4418
⃝c UNSW, 2019
Generated: 15 September 2019
lectures(adnan, Subject), !, studies(Student, Subject)?
lectures(adnan, 9414)
lectures(adnan, 9518)
studies(jane, 9414)
studies(jack, 9518)

COMP4418, Monday 30 September, 2019 Introduction to Prolog IV: Controlling Execution 4
Example
overdue(Today, Title, CatNo, MemFamily) :- loan(CatNo, MemNo, _, DueDate), later(Today, DueDate), !,
book(CatNo, Title, _),
member(MemNo, name(MemFamily, _), _).
COMP4418 ⃝c UNSW, 2019 Generated: 15 September 2019

COMP4418, Monday 30 September, 2019 Introduction to Prolog IV: Controlling Execution 5
Controlling Execution
􏰎 Some methods for controlling execution in Prolog: ◮ Ordering of clauses (facts and rules)
◮ Ordering of subgoals within a rule
◮ Cut (!) operator
􏰎 Use each with care
COMP4418 ⃝c UNSW, 2019 Generated: 15 September 2019