Logic Programming
06-25433
Autumn 2012

 

 

 

Lecture 11: Prolog's execution strategy explained more precisely

The execution strategy has been presented as simply placing all matching clauses onto the stack. In fact, Prolog is slightly more selective and a knowledge of this allows more efficient programs to be written.

This lecture includes:

  • revision of the elementary Prolog execution strategy;
  • optimizations to the elementary model based on indexing;
  • optimizations to the elementary model based on determinism;
  • the if...then...else construct and the proper use of the cut.
It answers questions from previous lectures about:
  • how to select a good order of arguments for a procedure;
  • how to order clauses in a procedure.
How to prepare

  No specific preparation for this lecture.

Video

  Recording of the lecture (Vimeo)

Slides

  If you really want the slides from the lecture ... but don't forget printing the slides is not the same as understanding them.

Summary

  Summary and programs with commentary from the lecture

Programs from the lecture

 
Example 1 - appending lists Example 2 - computing a factorial (without an accumulator) Example 3 - computing a factorial (with an accumulator)
Example 4 - deciding if an input is "a" Example 5 - computing a factorial (with disjunction) Example 6 - finding the maximum of two numbers (correct)
Example 7 - finding the maximum of two numbers (incorrect)    

Individual study suggestions and further reading

 

There are useful sections in the SICStus Prolog manual on:

Efficiency is covered in:

  • Covington. Chapter 4, sections 4.12-4.14, pp 107-113.
  • O'Keefe. Chapter 3, pp 74-111- especially on cuts: sections 3.8-3.13, pp 88-101 and if...then...else: section 3.16, pp 108-109.
  • Sterling & Shapiro. Chapter 11, pp 189-213.
Assessed work associated with this lecture

  Tutorial sheet 4