Accumulators! More on Arithmetic! and! Recursion!

Similar documents
Accumulators More on Arithmetic and Recursion. Acc-1

Logic Programming. Using Data Structures Part 2. Temur Kutsia

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Topic 5: Examples and higher-order functions

Programming Paradigms

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

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

The Art of Recursion: Problem Set 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))

IKI30820 Logic Programming Programming Style and Techniques Slide 09

CS1 Recitation. Week 2

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Software Development. Modular Design and Algorithm Analysis

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2).

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

CS 310 Advanced Data Structures and Algorithms

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

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Recursion & Iteration

Recursion versus Iteration

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

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

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

Streams and Evalutation Strategies

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Class 6: Efficiency in Scheme

Introduction to Prolog Accumulators, Debugging. CS181: Programming Languages

CS450 - Structure of Higher Level Languages

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

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)

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Introduction Prolog Activity! Prolog. Dr. Mattox Beckman. University of Illinois at Urbana-Champaign Department of Computer Science

6.001 Notes: Section 4.1

Chapter 1. Fundamentals of Higher Order Programming

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4. Hojjat Ghaderi, Fall 2006, University of Toronto

6.001 Recitation 23: Register Machines and Stack Frames

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Introduction to Functional Programming

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

Eiffel Loops & Iteration

Code Generation. Lecture 30

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Functions in MIPS. Functions in MIPS 1

A brief tour of history

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Programming Paradigms

Recursive Definitions

Algorithm Design and Recursion. Search and Sort Algorithms

Using Prolog as a CAS

Programming Languages and Compilers (CS 421)

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Functional Programming Languages (FPL)

Programming Languages 3. Definition and Proof by Induction

Haskell Types COMP360

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Fundamentals of Programming Session 13

Code Generation. Lecture 12

More about The Competition

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

Sequence Abstract Data Type

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

1.3. Conditional expressions To express case distinctions like

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Data Structures And Algorithms

Advanced Algorithms and Data Structures

Computer Science E-119 Practice Midterm

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

More Counting Sort and Sorting-by-Key

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Faced with the choice between changing one s mind and proving that there is no need to do so, almost everyone gets busy on the proof.

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Lab 09: Advanced SQL

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Lisp. Versions of LISP

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Deductive Databases. Motivation. Datalog. Chapter 25

Semantics of COW. July Alex van Oostenrijk and Martijn van Beek

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Alexandria University

Transcription:

Accumulators! More on Arithmetic! and! Recursion! Acc-1

L is a list of length N if...! listlen ( [ ], 0 ).! listlen ( L, N )! listlen ( [ H T ], N ) :- listlen ( T, N1 ), N is N1 + 1.! > On searching for the goal, the list is reduced to empty! > On back substitution, once the goal is found, the counter is incremented from 0! Following is an example sequence of goals (left hand column) and back substitution (right hand column)! listlen( [ a, b, c ], N ). N <== N1 + 1 listlen( [ b, c ], N1 ). N1 <== N2 + 1 listlen( [ c ], N2 ). N2 <== N3 + 1 listlen( [], N3 ). N3 <== 0! Acc-2

Abstract the counter! The following abstracts the counter part from listlen.! addup ( 0 ).! addup ( C ) :- addup ( C1 ), C is C1 + 1.! Notice the recursive definition occurs on a counter one smaller than in the head.! Acc-3

Count Up! An alternate method is to count on the way to the fixed point value in the query! The auxiliary counter accumulates the result on the way to the goal.! adder ( C ) :- adder ( 0, C ). ;Introduce auxiliary counter! adder ( C, C ) :- nl, write ( 'a ' ).! > The goal is reached when the auxiliary counter reaches the fixed point count value! adder ( Acc1, C ) :- write ( 'b ' ), Acc2 is Acc1 + 1, adder ( Acc2, C ).! > The predicates in black always succeed, side effect is to write to the terminal can see order of rule execution! Acc-4

listlen(l,n) 2! We can define list length using an accumulator! listln ( L, N ) :- lenacc ( L, 0, N ).! > Introduce the auxiliary counter Invariant: length ( L ) + accumulator = N! lenacc ( [], A, A ).! lenacc ( [ H T ], A, N ) :- A1 is A + 1, lenacc ( T, A1, N ).! Following is a sequence of goals! listln ( [ a, b, c ], N ). lenacc ( [ a, b, c ], 0, N ).!N <== N1 lenacc ( [ b, c ], 1, N1 ).!N1 <== N2 lenacc ( [ c ], 2, N2 ).!N2 <== N3 lenacc ( [], 3, N3 ).!N3 <== 3! Acc-5

Sum a List of Numbers no accumulator! sumlist ( List, Total ) asserts List is a list of numbers and Total = + / List.! sumlist ( [ ], 0 ).! sumlist ( [ First Rest ], Total ) :- sumlist ( Rest, Rest_total ),! Total is First + Rest_total.! Acc-6

Sum a List of Numbers with accumulator! sumlist ( List, Total ) asserts List is a list of numbers and Total = + / List.!» Use an accumulator!» Here sumlist asserts Total = (+ / List ) + Acc! sumlist ( List, Total ) :- sumlist ( List, 0, Total ).! sumlist ( [ ], Acc, Acc ).! sumlist( [ First Rest ], Acc, Total ) :- NewAcc is Acc + First, sumlist ( Rest, NewAcc, Total ).! Acc-7

A base case stops recursion! A base case is one that stops recursion!» This is a more general notion than the smallest problem.! Generate a sequence of integers from 0 to E, inclusive.!» Need to stop recursion when we have reached E.!!numInRange ( N, E ) :- addupton ( 0, N, E ).!!adduptoe ( Acc, Acc, _ ).!!!adduptoe ( Acc, N, E ) :- Acc < E,! Base case, no recursion!!! Acc1 is Acc + 1,! Need guard to prevent selecting this rule to adduptoe ( Acc1, N, E ).! prevent recursion! Acc-8

Accumulator Using vs Not Using! The definition styles reflect two alternate definitions for counting!» Recursion counts (accumulates) on back substitution.! > Goal becomes smaller problem! > Do not use accumulator!» Iteration counts up, accumulates on the way to the goal! > Accumulate from nothing up to the goal! > Goal counter value does not change!! Some problems require an accumulator!» Parts explosion problem!» Need intermediate results during accumulation! > Partial sums of a list of numbers! Acc-9

Factorial using recursion! Following is a recursive definition of factorial! Factorial ( N ) = N * Factorial ( N 1 )! factr ( N, F) -- F is the factorial of N! factr ( 0, 1 ).! factr ( N, F ) :- J is N 1, factr ( J, F1 ), F is N * F1.! The problem ( J, F1 ) is a smaller version of ( N, F )! Work toward the fixed point of a trivial problem! Does not work for factr ( N,120 ) and factr ( N, F ).!» Cannot do arithmetic J is N 1 because N is undefined.! Acc-10

Factorial using iteration accumulators! An iterative definition of factorial! facti ( N, F ) :- facti ( 0, 1, N, F ). facti ( N, F, N, F ). facti ( I, Fi, N, F ) :- invariant ( I, Fi, J, Fj ), facti ( J, Fj, N, F ).! invariant ( I, Fi, J, Fj ) :- J is I + 1, Fj is J * Fi.! The last two arguments are the goal and they remain the same throughout.! The first two arguments are the accumulator and they start from a fixed point and accumulate the result! Works for queries factr ( N,120 ) and factr ( N, F ) because values are always defined for the is operator.! Acc-11

Fibonacci Ordinary Recursion! Following is a recursive definition of the Fibonacci series. For reference here are the first few terms of the series! Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 1 1 2 3 5 8 13 21 34 55 89 144 233! Fibonacci ( N ) = Fibonacci ( N 1 ) + Fibonacci ( N 2 ).! fib ( 0, 1 ). fib ( 1, 1 ). fib ( N, F ) :- N1 is N 1, N2 is N 2, fib ( N1, F1 ), fib ( N2, F2 ), F is F1 + F2.! Does not work for queries fib ( N, 8 ) and fib ( N, F )!» Values for is operator are undefined.! Acc-12

Fibonacci Tail Recursion! A tail recursive definition of the Fibonacci series! > Tail recursion is equivalent to iteration! fibt ( 0, 1 ). fibt ( 1, 1 ). fibt ( N, F ) :- fibt ( 2, 1, 1, N, F ).! fibt ( N, Last2, Last1, N, F ) :- F is Last2 + Last1.! fibt ( I, Last2, Last1, N, F ) :- J is I + 1!, Fi is Last2 + Last1!, fibt ( J, Last1, Fi, N, F ).! Works for queries factr ( N, 120 ) and factr ( N, F )!» values are always defined for is operator.! Acc-13

Parts Explosion The Problem! Parts explosion is the problem of accumulating all the parts for a product from a definition of the components of each part! Acc-14

Snow Blower Parts View 1! Acc-15

Snow Blower Parts View 2! Acc-16

Bicycle Parts! Acc-17

Parts Explosion Example! Consider a bicycle we could have! > the following basic components! basicpart( spokes ). basicpart( rim ). basicpart( tire ).! basicpart( inner_tube ). basicpart( handle_bar ).! basicpart( front_ fork ). basicpart( rear_fork ).! > the following definitions for sub assemblies! assembly( bike, [ wheel, wheel, frame ] ).! assembly( wheel, [ spokes, rim, wheel_cushion ] ).! assembly( wheel_cushion, [ inner_tube, tire ] ).! assembly( frame, [ handle_bar, front_fork, rear_fork ] ).! Acc-18

Parts Explosion The Problem 2! We are interest in obtaining a parts list for a bicycle.! [ rear_ fork, front_ fork, handle_bar, tire, inner_tube, rim, spokes, tire, inner_tube, rim, spokes ]! > We have two wheels so there are two tires, inner_tubes, rims and spokes.! Using accumulators we can avoid wasteful re-computation as in the case for the ordinary recursion definition of the Fibonacci series! Acc-19

Parts Explosion Accumulator 1! partsof ( X,P ) P is the list of parts for item X! partsacc ( X, A, P ) parts_of ( X ) A = P.! partsof ( X, P ) :- partsacc ( X, [], P ).! is catenate (math append)! > Basic part parts list contains the part! partsacc ( X, A, [ X A ] ) :- basicpart ( X ).! > Not a basic part find the components of the part! partsacc ( X, A, P ) :- assembly ( X, Subparts ),! > parsacclist parts_of ( Subparts ) A = P! partsacclist ( Subparts, A, P ).! Acc-20

Parts Explosion Accumulator 2! parsacclist ( ListOfParts, AccParts, P ) parts_of ( ListOfParts ) AccParts = P! > No parts no change in accumulator! partsacclist ( [ ], A, A ).! partsacclist ( [ Head Tail ], A, Total ) :-! > Get the parts for the first on the list! partsacc ( Head, A, HeadParts )! > And catenate with the parts obtained from the rest of the ListOfParts!, partsacclist ( Tail, HeadParts, Total ).! Acc-21

Reverse a list with an accumulator! Define the predicate reverse ( List, ReversedList ) that asserts ReversedList is the List in reverse order.! reverse ( List, Reversed ) :-!!reverse ( List, [ ], Reversed ).! reverse ( [ ], Reversed, Reversed ).!!!reverse ( [ Head Tail] ) SoFar = Reversed! reverse ( [ Head Tail ], SoFar, Reversed ) :-!reverse ( Tail, [ Head SoFar ], Reversed ).!! Acc-22

Reverse a list without accumulator! Define the predicate reverse ( List, ReversedList ) that asserts ReversedList is the List in reverse order.! reverse ( [ ], [] ).! reverse ( [ Head Tail ], ReversedList ) :-!reverse ( Tail, ReversedTail ), append ( ReversedTail, [ Head ], ReversedList ).! Note the extra list traversal required by append compared to the accumulator version.!! Acc-23

Difference Lists and Holes! The accumulator in the parts explosion program is a stack!» Items are stored in the reverse order in which they are found! How do we store accumulated items in the same order in which they are formed?!» Use a queue! Difference lists with holes are equivalent to a queue! Acc-24

Consider the following list! [ a, b, c, d X ]! Examples for Holes! > X is a variable indicating the tail of the list. It is like a hole that can be filled in once a value for X is obtained! For example! Res = [ a, b, c, d X ], X = [ e, f ].! > Yields! Res = [ a, b, c, d, e, f ]! Acc-25

Examples for Holes 2! Or could have the following with the hole going down the list! Res = [ a, b, c, d X ]! > more goal searching gives X = [ e, f Y ]! > more goal searching gives Y = [ h, i, j ]! > Back substitution Yields Res = [ a, b, c, d, e, f, h, i, j ]! Acc-26

Efficiency of List Concatenation! Consider the definition of append! > The concatenation of lists is inefficient when the first list is long! append ( [], L, L ).! append ( [ X L1 ], L2, [ X L3 ] ) :- append ( L1, L2, L3 ).! > If we could skip the entire first part of the list in a single step, then concatenation of lists would be efficient! Acc-27

Difference Lists Representation! The list L!» L = [ a, b, c ]! Can be represented by two lists!» L1 = [ a, b, c, d, e ]!L1 = [ a, b, c ]!» L2 = [ d, e]!!l2 = [ ]!» L = L1 L2!!L = L1 L2! In fact L2 can be anything, so we can have the following!» L = [ a, b, c, d, e T ] [ d, e T]!» L = [ a, b, c T ] T The empty list [ ] = L L, for any L! Acc-28

Difference Lists!! S1 E1 S2 E2 (1) concat( S1 E1, S2 E2, S1 E2) with E1 = S2! L1 = [ A, B, C ] = [ A, B, C R1 ] R1 L2 = [ D, E ] = [ D, E R2 ] R2! Pattern match (1) with (2)! (2) concat([ A, B, C R1 ] R1, [ D, E R2 ] R2, CL) Using E1 = S2 we get! R1 = [ D, E R2 ] CL = [ A, B, C, D, E R2 ] R2! Acc-29

Parts Explosion Difference List 1! partsofd (X, P ) P is the list of parts for item X! partsdiff ( X, Hole, P ) parts_of ( X ) Hole = P! > Hole and P are reversed compared to Clocksin & Mellish (v5) to better compare with accumulator version.! partsofd ( X, P ) :- partsdiff ( X, [ ], P ).! > Base case we have a basic part, then the parts list contains the part! partsdiff ( X, Hole, [ X Hole ] ) :- basicpart ( X ).! Acc-30

Parts Explosion Difference List 2! > Not a base part, so we find the components of the part! partsdiff ( X, Hole, P ) :- assembly ( X, Subparts )! > parsdifflistd parts_of ( Subparts ) Hole = P!, partsdifflist ( Subparts, Hole, P ).! Acc-31

Parts Explosion Difference Lists 3! parsdifflist (ListOfParts, Hole, P ) parts_of ( ListOfParts ) Hole = P! partsdifflist ( [ ], Hole, Hole ).! partsdifflist ( [ Head Tail ], Hole, Total ) :-! > Get the parts for the first on the list! partsdiff ( Head, Hole1, Total )! > And catenate with the parts obtained from the rest of the ListOfParts!, partsdifflist ( Tail, Hole, Hole1 ).! Acc-32

Compare Accumulator with Hole! partsof ( X, P ) :- partsacc ( X, [ ], P ). partsofd ( X, P ) :- partsdiff ( X, [ ], P ). Accumulator Difference/Hole partsacc ( X, A, [ X A ] ) :- basicpart ( X ). partsdiff ( X, Hole, [ X Hole ] ) :- basicpart ( X ). partsacc ( X, A, P ) :- assembly ( X, Subparts ), partsacclist ( Subparts, A, P ). partsdiff ( X, Hole, P ) :- assembly ( X, Subparts ), partsdifflist ( Subparts, Hole, P ). Acc-33

Compare Accumulator with Hole 2! partsacclist ( [ ], A, A ). partsdifflist ( [ ], Hole, Hole ). partsacclist ( [ Head Tail ], A, Total ) :- partsacc ( Head, A, HeadParts ), partsacclist ( Tail, HeadParts, Total ). partsdifflist ( [ Head Tail ], Hole, Total ) :- partsdiff ( Head, Hole1, Total ), partsdifflist ( Tail, Hole, Hole1 ). Acc-34