This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

Similar documents
Parsing Scheme (+ (* 2 3) 1) * 1

Manipulating atomics and clauses

6.001 Notes: Section 4.1

The Prolog to Mercury transition guide

Logic Programming. Efficiency Issues. Temur Kutsia

Processing lists in Prolog - 2

Introduction to Object-Oriented Programming

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

A brief tour of history

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

CS1102: Macros and Recursion

Wednesday, October 15, 14. Functions

Lecture 11: Feb. 10, 2016


The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

Implementação de Linguagens 2016/2017

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

Prolog Programming. Lecture Module 8

CS113: Lecture 4. Topics: Functions. Function Activation Records

Research Report AI A Numerical Equation Solver in Prolog Michael A. Covington Artificial Intelligence Programs The University of Georgia

Announcements. Assignment 2 due next class in CSIL assignment boxes

Operational Semantics

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Call Stacks + On Writing Good Code

Programming Paradigms

Introduction to Logic Programming in Prolog 1 / 39

Operators (2A) Young Won Lim 10/2/13

Compilers: The goal. Safe. e : ASM

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Logic Languages. Hwansoo Han

Recursion, Structures, and Lists

Lexical vs. Dynamic Scope

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SML Style Guide. Last Revised: 31st August 2011

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong

An Explicit Continuation Evaluator for Scheme

P1 Engineering Computation

Lecture 16: Logic Programming in Prolog

The compiler is spewing error messages.

tuprolog with exceptions

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS64 Week 5 Lecture 1. Kyle Dewey

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Drawing Hands, by M. C. Escher (lithograph, 1948)

Class 6: Efficiency in Scheme

Q1:a. Best first search algorithm:

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Putting the fun in functional programming

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Chapter 5: Recursion. Objectives

Tests, Backtracking, and Recursion

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Introduction to Prolog

CSC 2400: Computer Systems. Using the Stack for Function Calls

The Art of Recursion: Problem Set 10

Operators (2A) Young Won Lim 10/5/13

ENEE 457: Computer Systems Security. Lecture 16 Buffer Overflow Attacks

Lecture 24 Notes Search in Graphs

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Lecture 9: A closer look at terms

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1

CS Introduction to Data Structures How to Parse Arithmetic Expressions

List Functions, and Higher-Order Functions

Implementing Continuations

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

C++ for Java Programmers

Code Generation. Lecture 12

Programming Paradigms

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Lecture Notes on Induction and Recursion

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

Project Compiler. CS031 TA Help Session November 28, 2011

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

IR Generation. May 13, Monday, May 13, 13

Programming Paradigms

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

CSE 417 Dynamic Programming (pt 6) Parsing Algorithms

CMSC 330: Organization of Programming Languages

Parallel Programming: Background Information

Parallel Programming: Background Information

CAP 5602 Summer, Lesson 4: Loops. The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop

Mechanized Operational Semantics

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

15 212: Principles of Programming. Some Notes on Continuations

Exercises on the Fundamentals of Prolog

1 Dynamic Memory continued: Memory Leaks

Arithmetic Terms are Symbolic

Transcription:

This lecture covers: 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. 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. 8 - Prolog s execution strategy explained more precisely 1 Revision of the elementary Prolog data types For this lecture we need to remember some key terms: functor concat([], List, List). arity and some Prolog data types: Data type Example Atom cost, [] Number 1 Atomic [], cost, 1 Compound object car(ford, 1995) List [a, b, c] Variable Var Prolog s execution strategy revisited - 1 Consider this program: and the query:?- app([a], [1], List). What goes on the stack? 8 - Prolog s execution strategy explained more precisely 2 8 - Prolog s execution strategy explained more precisely 3 Prolog s execution strategy revisited - 2 Step 1 - add to stack Step 2 - first member of the stack is popped - but doesn t match, so the next item on the stack is used Prolog s execution strategy revisited - 3 Step 3 - The head of this rule matches, so the recursive subgoal is called - as if with the query:?- app([], [1], List). The causes two more instances of the rule to be placed on the stack: 8 - Prolog s execution strategy explained more precisely 4 8 - Prolog s execution strategy explained more precisely 5 1

Prolog s execution strategy revisited - 4 Step 4 - The first member of the stack is popped - and this time matches - giving success and leaving the second clause on the stack: But this is wasteful - we can tell in each step that one clause will never match either query! Prolog s indexing - or being almost as clever as us Good Prolog implementations cut down the number of items added to the stack. The technique is simple: All clauses are indexed by: functor arity type of the first argument and, if the first argument is an atomic, then Prolog also indexes according to the value of the atomic. For each query and subgoal, Prolog uses the index to identify eligible clauses. 8 - Prolog s execution strategy explained more precisely 6 8 - Prolog s execution strategy explained more precisely 7 What effect does Prolog s indexing have on program design? - 1 What effect does Prolog s indexing have on program design? - 2 Consider the following two queries:?- app([a], [1], List).?- app(l1, L2, [a,1]). What goes on the stack for each of these queries? From our knowledge of Prolog s indexing, we can draw up the following guideline: When designing a procedure: try to place the input arguments first; try to put the most discriminating in the first argument position. 8 - Prolog s execution strategy explained more precisely 8 8 - Prolog s execution strategy explained more precisely 9 Determinism So far, we ve defined determinism as having only one way of satisfying a goal, eg: Answer is 2 + 3 can only be satisfied once. Experienced Prolog programmers use a more subtle definition based on how many alternative clauses can be added to the stack. Disadvantage: changes our view of the program from what it means to how it s executed. 8 - Prolog s execution strategy explained more precisely 10 Allocating space on the stack - 1 Each time a query or a subgoal is executed, a stack frame is added to the stack. If the subgoal is a recursive call and the recursion is deep, then there will be many stack frames added potentially wasting much space. 8 - Prolog s execution strategy explained more precisely 11 2

Allocating space on the stack - 2 A growing stack A static-size stack frame n frame n Frame n-1 Frame n-1 Frame n-2 Frame n-2... frame 1 frame 1 How can stack growth be restricted? Last call optimization (LCO) and accumulators - 1 LCO is a space optimization technique that is automatically applied at the point when the last subgoal in a rule is to be called when a procedure is deterministic. This is often used with accumulators - an accumulating parameter builds up a result as the computation proceeds. 8 - Prolog s execution strategy explained more precisely 12 8 - Prolog s execution strategy explained more precisely 13 Last call optimization (LCO) and accumulators - 2 This is one way of calculating a factorial: fac1(0, 1). fac1(n, Fact) :- fac1(n1, Fact1), Fact is N * Fact1. This isn t tail recursive and so grows the stack, so we need to rewrite it. 8 - Prolog s execution strategy explained more precisely 14 Adding an accumulator - 1 Adding an accumulator involves adding another argument: fac2(0, Fact, Fact). All subgoals before this call are fac2(n, Accum, Fact) :- deterministic. tab(n), write(accum), nl, Accum1 is N * Accum, fac2(n1, Accum1, Fact). Demos 1 & 2 8 - Prolog s execution strategy explained more precisely 15 Adding an accumulator - 2 Why are accumulators of importance in Prolog? Because they are an important way of ensuring that non-deterministic subgoals are called last and so enabling last call optimization. Last call optimization and indexing - 1 Consider the problem of checking whether a given vowel is an a and updating a counter. A straightforward version might be: % 1 - a update_count(a, A, A1) :- A1 is A + 1. % 2 - not "a" update_count(letter, A, A) :- Letter \== a. However, this would waste much space on the stack. 8 - Prolog s execution strategy explained more precisely 16 8 - Prolog s execution strategy explained more precisely 17 3

Last clause determinacy detection and indexing - 2 A less space-wasteful version is: % 1 - not a update_count(letter, A, A) :- Letter \== a. % 2 - a update_count(a, A, A1) :- A1 is A + 1. Use of if then else - 1 Prolog expresses or by having alternative clauses in a procedure - and this is usually the best method. But, there are significant efficiency advantages when the first subgoals of rules are based on: arithmetic comparisons; type tests. We can use Prolog s if then else and the compiler produces very efficient code. 8 - Prolog s execution strategy explained more precisely 18 8 - Prolog s execution strategy explained more precisely 19 Use of if then else - 2 Example based on factorial: fac3(n, Acc, Fact) :- ( N > 0 -> Acc1 is N * Acc, fac3(n1, Acc1, Fact) ; N =:= 0 -> Acc = Fact ). Cuts Prolog includes a predicate called cut and written as! which allows the stack to be manipulated during the running of a program. This is bad thing! Cuts are classified into two patterns of use: green cuts red cuts 8 - Prolog s execution strategy explained more precisely 20 8 - Prolog s execution strategy explained more precisely 21 Green cuts Red cuts Don t change the meaning of the program but stops Prolog storing unnecessary backtracking information on the stack. Take away the cut and the program will produce the same results. Change the meaning of the program. Take away the cut and the program will produce the different results. Some people believe that programmers need two years of experience before using the cut. 8 - Prolog s execution strategy explained more precisely 22 8 - Prolog s execution strategy explained more precisely 23 4

An example of the cut well used Consider the following code: max1(x, Y, X) :- X >= Y,!. max1(x, Y, Y) :- X < Y. % green cut Clearly this computes the maximum of two numbers - and it is correct. An example of the cut badly used The test in the second clause should be superfluous - if the first number isn t smaller than the first, the second clause applies: max2(x, Y, X) :- X >= Y,!. max2(_x, Y, Y). % red cut Conclusion: cuts need a lot of care. Demo 3 8 - Prolog s execution strategy explained more precisely 24 8 - Prolog s execution strategy explained more precisely 25 Summary - 1 A simplistic model Prolog s execution method hides several optimizations which can be quite powerful. Indexing is the simplest, very powerful and helps us design the order of arguments. Last call optimization suggests ways in which clauses in a procedure should be ordered. Summary - 2 Last call optimization is powerful but often requires the use of accumulators. The if then else construct can be efficient in very limited circumstances. Cuts are difficult to apply and can lead the inexperienced into great difficulties. 8 - Prolog s execution strategy explained more precisely 26 8 - Prolog s execution strategy explained more precisely 27 5