You are on page 1of 710

IIT Delhi

S. Arun-Kumar, CSE

Introduction to Title Page

Computer Science JJ
Contents

II

S. Arun-Kumar J I

sak@cse.iitd.ernet.in Page 1 of 709

Department of Computer Science and Engineering Go Back

I. I. T. Delhi, Hauz Khas, New Delhi 110 016.


Full Screen

March 19, 2007 Close

Quit
Contents
1 Computing: The Functional Way 3
1.1 Introduction to Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Our Computing Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.3 Primitives: Integer & Real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 IIT Delhi
1.4 Example: Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
1.5 Primitives: Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
S. Arun-Kumar, CSE

2 Algorithms: Design & Refinement 114


2.1 Technical Completeness & Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 114 Title Page
2.2 Algorithm Refinement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
2.3 Variations: Algorithms & Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
2.4 Names, Scopes & Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Contents

3 Introducing Reals 242


3.1 Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 JJ II
3.2 Root Finding, Composition and Recursion . . . . . . . . . . . . . . . . . . . . . . 264
J I
4 Correctness, Termination & Complexity 294
4.1 Termination and Space Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . 294
4.2 Efficiency Measures and Speed-ups . . . . . . . . . . . . . . . . . . . . . . . . . 333 Page 2 of 709
4.3 Invariance & Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

5 Compound Data 390 Go Back

5.1 Tuples, Lists & the Generation of Primes . . . . . . . . . . . . . . . . . . . . . . . 390


5.2 Compound Data & Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Full Screen
5.3 Compound Data & List Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 457

6 Higher Order Functions & Structured Data 486 Close


6.1 Higher Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
6.2 Structured Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Quit
6.3 User Defined Structured Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 547

7 Imperative Programming: An Introduction 570


7.1 Introducing a Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
7.2 Imperative Programming: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
7.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 IIT Delhi

8 A large Example: Tautology Checking 640


8.1 Large Example: Tautology Checking . . . . . . . . . . . . . . . . . . . . . . . . . 640 S. Arun-Kumar, CSE
8.2 Tautology Checking Contd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Title Page
9 Lecture-wise Index to Slides 680

Contents

JJ II

J I

Page 3 of 709

Go Back

Full Screen

Close

Quit
1. Computing: The Functional Way
1.1. Introduction to Computing
1. Introduction
IIT Delhi
2. Computing tools

3. Ruler and Compass S. Arun-Kumar, CSE

4. Computing and Computers


Title Page
5. Primitives

6. Algorithm
Contents
7. Problem: Doubling a Square

8. Solution: Doubling a Square JJ II

9. Execution: Step 1
J I
10. Execution: Step 2

11. Doubling a Square: Justified Page 4 of 709

12. Refinement: Square Construction


Go Back
13. Refinement 2: Perpendicular at a point

14. Solution: Perpendicular at a point Full Screen

15. Perpendicular at a point: Justification


Close
Next: Our Computing Tool

Quit
Introduction IIT Delhi

S. Arun-Kumar, CSE

• This course is about computing Title Page

• Computing as a process is nearly Contents

as fundamental as arithmetic JJ II

• Computing as a mental process J I

• Computing may be done with a va- Page 5 of 709

riety of tools which may or may not Go Back

assist the mind Full Screen

Close

Quit
Computing tools
IIT Delhi

S. Arun-Kumar, CSE

• Sticks and stones (counting) Title Page

• Paper and pencil (an aid to mental Contents

computing) JJ II

• Abacus (still used in Japan!) J I

Page 6 of 709

• Slide rules (ask a retired engineer!)


Go Back

• Ruler and compass Full Screen

Close

Quit
Ruler and Compass IIT Delhi

S. Arun-Kumar, CSE

Actually it is a computing tool!


Title Page

• Construct a length that is half of a Contents

given length JJ II

• Bisect an angle J I

• Construct a square that is twice the Page 7 of 709

area of a given square Go Back


• Construct 10 Full Screen

Close

Quit
Computing and IIT Delhi

Computers S. Arun-Kumar, CSE

Title Page

• Computing is much more funda- Contents

mental JJ II

• Computing may be done without a J I

computer too! Page 8 of 709

• But a Computer cannot do much Go Back

besides computing. Full Screen

Close

Quit
Primitives
• Each tool has a set of capabilities IIT Delhi

called primitive operations or primi-


S. Arun-Kumar, CSE

tives
Title Page

Ruler: Can specify lengths, lines Contents

Compass: Can define arcs and cir- JJ II

cles J I

• The primitives may be combined in Page 9 of 709

various ways to perform a computa- Go Back

tion. Full Screen

• Example Constructing a right bisec- Close

tor of a given line segment. Quit


IIT Delhi

Algorithm S. Arun-Kumar, CSE

Given a problem to be solved with a Title Page

given tool, the attempt is to evolve a Contents

combination of primitives of the tool in JJ II

a certain order to solve the problem. J I

An explicit statement of this combina- Page 10 of 709

tion along with the order is an algo- Go Back

rithm Full Screen

Close

Quit
IIT Delhi

Problem: Doubling a S. Arun-Kumar, CSE

Square Title Page

Given a square, construct another Contents

square of twice the area of the origi- JJ II

nal square. J I

A D
Page 11 of 709

Go Back

B C
Full Screen

Close

Quit
IIT Delhi

Solution: Doubling a S. Arun-Kumar, CSE

Square Title Page

Contents
Assume given a square ABCD of
JJ II
side a > 0.
J I
1. Draw the diagonal AC.
Page 12 of 709

2. Complete the square ACEF on Go Back

side AC. Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Execution: Step 1 Title Page

Contents

Draw the diagonal AC.


JJ II
A D

J I

Page 13 of 709
B C

Go Back

Full Screen

Close

Quit
IIT Delhi

Execution: Step 2 S. Arun-Kumar, CSE

Complete the square ACEF on side Title Page

AC. Contents

JJ II

J I

A D
Page 14 of 709

Go Back

B C
Full Screen

Close

Quit
IIT Delhi

Doubling a Square: S. Arun-Kumar, CSE

Justified Title Page

Contents
Assume given a square ABCD of
JJ II
side a > 0.
√ J I
1. Draw the diagonal AC. AC = 2a
Page 15 of 709

2. Complete the square ACEF on Go Back

side AC. Area of ACEF = 2a2. Full Screen

Close

Quit
Refinement: Square
Given a line segment of length b > 0
construct a square of side b. IIT Delhi

Assume given a line segment P Q of S. Arun-Kumar, CSE

length b. Title Page

1. Construct two lines l1 and l2 per- Contents

JJ II
pendicular to P Q passing through
P and Q respectively J I

Page 16 of 709

2. On the same side of P Q mark


Go Back

points R on l1 and S on l2 such that


Full Screen

P R = P Q = QS.
Close

3. Draw RS. P QSR is a square of Quit

side b
Square on Segment: 0
Assume given a line segment P Q of IIT Delhi

length b. S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 17 of 709

Go Back

P Q
Full Screen

Close

Quit
Square on Segment: 1
Construct two lines l1 and l2 perpen-
dicular to P Q passing through P and IIT Delhi

Q respectively S. Arun-Kumar, CSE

Title Page

l l
1 2
Contents

JJ II

J I

Page 18 of 709

Go Back

P Q Full Screen

Close

Quit
Square on Segment: 2
On the same side of P Q mark points IIT Delhi
R on l1 and S on l2 such that P R =
P Q = QS. S. Arun-Kumar, CSE

Title Page

l l
1 2
R S Contents

JJ II

J I

Page 19 of 709

Go Back

P Q Full Screen

Close

Quit
Square on Segment: 3
Draw RS. P QSR is a square of side
b IIT Delhi

S. Arun-Kumar, CSE

l l
1 2
Title Page
R S

Contents

JJ II

J I

Page 20 of 709

P Q Go Back

Full Screen

Close

Quit
Square Construction algorithm
Perpendicular at a
point
Given a line, draw a perpendicular to IIT Delhi

it passing through a given point on it. S. Arun-Kumar, CSE

Assume given a line l containing a Title Page

point X. Contents

JJ II

J I

Page 21 of 709

Go Back

X l
Full Screen

Close

Quit
Solution:
Perpendicular at a IIT Delhi

point S. Arun-Kumar, CSE

Title Page

1. Choose a length c > 0. With X as


Contents
centre mark off points Y and Z on l
JJ II
on either side of X, such that Y X =
c = XZ. Y Z = 2c. J I

Page 22 of 709

2. Draw Circles C1(Y, 2c) and C2(Z, 2c)


Go Back

respectively.
Full Screen

3. Join the points of intersection of the Close

two circles. Quit


Perpendicular at a
Point: 1
Choose a length c > 0. With X as
centre mark off points Y and Z on l IIT Delhi

on either side of X, such that Y X = S. Arun-Kumar, CSE

c = XZ. Y Z = 2c. Title Page

Contents

JJ II

J I

Page 23 of 709

Go Back
Y X Z l

Full Screen

Close

Quit
Perpendicular at a
Point: 2
Draw Circles C1(Y, 2c) and C2(Z, 2c) IIT Delhi

respectively. S. Arun-Kumar, CSE

Title Page

Contents
U

JJ II

J I

Y X Z l Page 24 of 709

Go Back

V Full Screen

Close

Quit
Perpendicular at a
Point: 3
Choose a length c > 0. With X as
centre mark off points Y and Z on l IIT Delhi

on either side of X, such that Y X = S. Arun-Kumar, CSE

c = XZ. Y Z = 2c. Title Page

Contents

JJ II
U

J I

Page 25 of 709

Go Back
Y X Z l

Full Screen

Close
V

Quit
Perpendicular at a
point: Justification IIT Delhi

S. Arun-Kumar, CSE

1. The two circles intersect at points U Title Page

and V on either side of line l. Contents



2. U V is a perpendicular bisector of JJ II

Y Z. J I

3. Since Y X = c = XZ and Y Z = 2c, Page 26 of 709


U V is perpendicular to l and passes Go Back

through X. Full Screen

Close

Back to square 1
Quit
1.2. Our Computing Tool
Previous: Introduction to Computing
IIT Delhi
1. The Digital Computer: Our Computing Tool
2. Algorithms S. Arun-Kumar, CSE

3. Programming Language
Title Page
4. Programs and Languages
5. Programs Contents

6. Programming
JJ II
7. Computing Models
8. Primitives J I
9. Primitive expressions
Page 27 of 709
10. Methods of combination
11. Methods of abstraction
Go Back
12. The Functional Model
13. Mathematical Notation 1: Factorial Full Screen

14. Mathematical Notation 2: Factorial


Close
15. Mathematical Notation 3: Factorial
16. A Functional Program: Factorial Quit
17. A Computation: Factorial

18. A Computation: Factorial

19. A Computation: Factorial

20. A Computation: Factorial IIT Delhi

21. A Computation: Factorial


S. Arun-Kumar, CSE
22. A Computation: Factorial

23. A Computation: Factorial Title Page

24. Standard ML
Contents
25. SML: Important Features

Next: Primitives: Integer & Real JJ II

J I

Page 28 of 709

Go Back

Full Screen

Close

Quit
The Digital Computer:
Our Computing Tool IIT Delhi

Algorithm: A finite specification of the S. Arun-Kumar, CSE

solution to a given problem using the Title Page

primitives of the computing tool. Contents

JJ II
• It specifies a definite input and out-
J I
put
Page 29 of 709

• It is unambiguous Go Back

• It specifies a solution as a finite pro- Full Screen

cess i.e. the number of steps in the Close

computation is finite Quit


Algorithms
An algorithm will be written in a mix- IIT Delhi

ture of English and standard mathe- S. Arun-Kumar, CSE

matical notation. Usually, Title Page

• algorithms written in a natural lan- Contents

guage are often ambiguous JJ II

• mathematical notation is not am- J I

biguous, but still cannot be under- Page 30 of 709

stood by machine Go Back

• algorithms written by us use various Full Screen

mathematical properties. We know Close

them, but the machine doesn’t. Quit


Programming
Language IIT Delhi

• Require a way to communicate with


S. Arun-Kumar, CSE

a machine which has essentially no Title Page

intelligence or understanding. Contents

JJ II
• Translate the algorithm into a form
J I
that may be “understood” by a ma-
Page 31 of 709

chine
Go Back

• This “form” is usually a program Full Screen

Program: An algorithm written in a Close

programming language.
Quit
Programs and
Languages IIT Delhi

• Every programming language has a S. Arun-Kumar, CSE

well defined vocabulary and a well Title Page

defined grammar Contents

• Each program has to be written fol- JJ II

lowing rigid grammatical rules J I

• A programming language and the Page 32 of 709

computer together form our single Go Back

computing tool Full Screen

• Each program uses only the primi- Close

tives of the computing tool Quit


Programs
Program: An algorithm written in the IIT Delhi

grammar of a programming language. S. Arun-Kumar, CSE

Title Page

A grammar is a set of rules for forming Contents

sentences in a language. JJ II

Each programming language also has J I

its own vocabulary and grammar just Page 33 of 709

as in the case of natural languages. Go Back

Full Screen

We will learn the grammar of the lan-


guage as we go along. Close

Quit
Programming
The act of writing programs and test- IIT Delhi

ing them is programming S. Arun-Kumar, CSE

Title Page

Even though most programming lan-


Contents
guages use essentially the same
JJ II
computing primitives, each program-
J I
ming language needs to be learned.
Page 34 of 709

Programming languages differ from Go Back

each other in terms of the conve- Full Screen

nience and facilties they offer even Close

though they are all equally powerful. Quit


Computing Models
We consider mainly two models. IIT Delhi

• Functional: A program is specified S. Arun-Kumar, CSE

simply as a mathematical expres- Title Page

sion Contents

• Imperative: A program is specified JJ II

by a sequence of commands to be J I

executed. Page 35 of 709

Go Back

Programming languages also come


mainly in these two flavours. We Full Screen

will often identify the computing model Close

with the programming language. Quit


Primitives IIT Delhi

Every programming language offers S. Arun-Kumar, CSE

the following capabilities to define and Title Page

use: Contents

• Primitive expressions and data JJ II

• Methods of combination of expres- J I

sions and data Page 36 of 709

• Methods of abstraction of both ex- Go Back

pressions and data Full Screen

The functional model Close

Quit
Primitive expressions
The simplest objects and operations
in the computing model. These in- IIT Delhi

clude S. Arun-Kumar, CSE

• basic data elements: numbers, Title Page

characters, truth values etc. Contents

JJ II
• basic operations on the data ele-
J I
ments: addition, substraction, mul-
Page 37 of 709
tiplication, division, boolean opera-
tions, string operations etc.
Go Back

Full Screen

• a naming mechanism for various Close

quantitities and expressions to be


Quit

used without repeating definitions


IIT Delhi

Methods of S. Arun-Kumar, CSE

combination Title Page

Contents
Means of combining simple expres-
JJ II
sions and objects to obtain more com-
J I
plex expressions and objects.
Page 38 of 709

Examples: composition of functions, Go Back

inductive definitions
Full Screen

Close

Quit
IIT Delhi

Methods of abstraction
S. Arun-Kumar, CSE

Title Page

Means of naming and using groups of Contents

objects and expressions as a single JJ II

unit J I

Examples: functions, data structures, Page 39 of 709

modules, classes etc. Go Back

Full Screen

Close

Quit
The Functional Model
The functional model is very conve- IIT Delhi

nient and easy to use: S. Arun-Kumar, CSE

• Programs are written (more or less) Title Page

in mathematical notation Contents

• It is like using a hand-held calcula- JJ II

tor J I

• Very interactive and so answers are Page 40 of 709

immediately available Go Back

• Very convenient for developing, Full Screen

testing and proving algorithms Close

Quit
Standard ML
IIT Delhi

S. Arun-Kumar, CSE

Mathematical Notation Title Page

1: Factorial Contents

JJ II

1 if n < 1 J I
n! =
n × (n − 1)! otherwise Page 41 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

Mathematical Notation S. Arun-Kumar, CSE

2: Factorial Title Page

Contents

Or more informally, JJ II

 J I
1 if n < 1
n! = Page 42 of 709

1 × 2 × . . . × n otherwise
Go Back

Full Screen

Close

Quit
Mathematical Notation
IIT Delhi

3: Factorial
S. Arun-Kumar, CSE

Title Page

How about this? Contents

 JJ II

1 if n < 1
n! = J I

(n + 1)!/(n + 1) otherwise Page 43 of 709

Mathematically correct but computa- Go Back

tionally incorrect! Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

A Functional Program: Title Page

Factorial Contents

JJ II

J I
fun fact n = if n < 1 then 1
else n * fact (n-1) Page 44 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

A Computation: Title Page

Factorial JJ
Contents

II
sml
J I
Standard ML of New Jersey,
Page 45 of 709

-
Go Back

Full Screen

Close

Quit
IIT Delhi

A Computation:
S. Arun-Kumar, CSE

Title Page

Factorial Contents

sml JJ II

Standard ML of New Jersey, J I

- fun fact n = Page 46 of 709

= Go Back

Full Screen

Close

Quit
IIT Delhi

A Computation: S. Arun-Kumar, CSE

Factorial Title Page

Contents

sml JJ II
Standard ML of New Jersey,
J I
- fun fact n =
Page 47 of 709

= if n < 1 then 1
Go Back
=
Full Screen

Close

Quit
A Computation: IIT Delhi

Factorial S. Arun-Kumar, CSE

Title Page

sml Contents

Standard ML of New Jersey, JJ II


- fun fact n =
J I
= if n < 1 then 1
Page 48 of 709

= else n * fact (n-1);


Go Back
val fact = fn : int -> int
Full Screen
-
Close

Quit
A Computation:
Factorial IIT Delhi

S. Arun-Kumar, CSE

sml Title Page

Standard ML of New Jersey, Contents

- fun fact n = JJ II
= if n < 1 then 1
J I
= else n * fact (n-1);
Page 49 of 709

val fact = fn : int -> int


Go Back
- fact 8;
Full Screen
val it = 40320 : int
Close
-
Quit
A Computation:
Factorial IIT Delhi

sml S. Arun-Kumar, CSE

Standard ML of New Jersey, Title Page

- fun fact n = Contents

= if n < 1 then 1 JJ II
= else n * fact (n-1);
J I
val fact = fn : int -> int
Page 50 of 709

- fact 8;
Go Back
val it = 40320 : int
Full Screen
- fact 9;
Close
val it = 362880 : int
- Quit
A Computation:
Factorial
sml IIT Delhi

Standard ML of New Jersey, S. Arun-Kumar, CSE

- fun fact n = Title Page

= if n < 1 then 1 Contents

= else n * fact (n-1); JJ II

val fact = fn : int -> int J I

- fact 8; Page 51 of 709

val it = 40320 : int Go Back

- fact 9; Full Screen

val it = 362880 : int Close

- Quit

More SML
IIT Delhi

Standard ML S. Arun-Kumar, CSE

• Originated as part of a theorem- Title Page

proving development project Contents

• Runs on both Windows and UNIX JJ II

environments J I

Page 52 of 709

• Is free!
Go Back

• http://www.smlnj.org Full Screen

Close

Quit
SML: Important
Features IIT Delhi

• Has a small vocabulary of just a few S. Arun-Kumar, CSE

short words Title Page

• Far more “intelligent” than currently Contents

available languages: JJ II

J I
– automatically finds out what vari-
Page 53 of 709

ous names mean and


Go Back

– their correct usage


Full Screen

• Haskell, Miranda and Caml are a Close

few other such languages. Quit


1.3. Primitives: Integer & Real
Previous: Primitives: Integer & Real
IIT Delhi
1. Algorithms & Programs
2. SML: Primitive Integer Operations 1 S. Arun-Kumar, CSE

3. SML: Primitive Integer Operations 1


Title Page
4. SML: Primitive Integer Operations 1
5. SML: Primitive Integer Operations 1 Contents

6. SML: Primitive Integer Operations 1


JJ II
7. SML: Primitive Integer Operations 1
8. SML: Primitive Integer Operations 2 J I
9. SML: Primitive Integer Operations 2
Page 54 of 709
10. SML: Primitive Integer Operations 2
11. SML: Primitive Integer Operations 2
Go Back
12. SML: Primitive Integer Operations 2
13. SML: Primitive Integer Operations 3 Full Screen

14. SML: Primitive Integer Operations 3


Close
15. SML: Primitive Integer Operations 3
16. SML: Primitive Integer Operations 3 Quit
17. SML: Primitive Integer Operations 3

18. Quotient & Remainder

19. SML: Primitive Real Operations 1

20. SML: Primitive Real Operations 1 IIT Delhi

21. SML: Primitive Real Operations 1


S. Arun-Kumar, CSE
22. SML: Primitive Real Operations 2

23. SML: Primitive Real Operations 3 Title Page

24. SML: Primitive Real Operations 4


Contents
25. SML: Precision
26. Fibonacci Numbers JJ II
27. Euclidean Algorithm
J I
Next: Technical Completeness & Algorithms

Page 55 of 709

Go Back

Full Screen

Close

Quit
Algorithms & Programs IIT Delhi

• Algorithm S. Arun-Kumar, CSE

• Need for a formal notation Title Page

• Programs Contents

JJ II
• Programming languages
J I

• Programming Page 56 of 709

• Functional Programming Go Back

• Standard ML Full Screen

Factorial Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

SML: Primitive Integer Title Page

Operations 1 Contents

JJ II

sml J I

Standard ML of New Jersey, Page 57 of 709

- Go Back

Full Screen

Close

Quit
IIT Delhi

SML: Primitive Integer S. Arun-Kumar, CSE

Operations 1 Title Page

Contents

sml JJ II

Standard ML of New Jersey, J I

- val x = 5; Page 58 of 709

val x = 5 : int Go Back

-
Full Screen

Close

Quit
SML: Primitive Integer IIT Delhi

Operations 1 S. Arun-Kumar, CSE

Title Page

sml Contents

Standard ML of New Jersey, JJ II

- val x = 5; J I

val x = 5 : int Page 59 of 709

- val y = 6; Go Back

val y = 6 : int
Full Screen

-
Close

Quit
SML: Primitive Integer
Operations 1 IIT Delhi

S. Arun-Kumar, CSE

sml Title Page

Standard ML of New Jersey, Contents

- val x = 5; JJ II

val x = 5 : int J I

- val y = 6; Page 60 of 709

val y = 6 : int Go Back

- x+y;
Full Screen

val it = 11 : int
Close

-
Quit
SML: Primitive Integer
Operations 1 IIT Delhi

sml S. Arun-Kumar, CSE

Standard ML of New Jersey, Title Page

- val x = 5; Contents

val x = 5 : int JJ II

- val y = 6; J I

val y = 6 : int Page 61 of 709

- x+y; Go Back

val it = 11 : int
Full Screen

- x-y;
Close

val it = ˜1 : int
Quit

-
SML: Primitive Integer
Operations 1
IIT Delhi
Standard ML of New Jersey,
S. Arun-Kumar, CSE
- val x = 5;
val x = 5 : int Title Page

- val y = 6; Contents

val y = 6 : int JJ II

- x+y; J I

val it = 11 : int Page 62 of 709

- x-y; Go Back

val it = ˜1 : int Full Screen

- it + 5; Close

val it = 4 : int Quit

-
SML: Primitive Integer
Operations 2
IIT Delhi
val x = 5 : int
S. Arun-Kumar, CSE
- val y = 6;
val y = 6 : int Title Page

- x+y; Contents

val it = 11 : int JJ II

- x-y; J I

val it = ˜1 : int Page 63 of 709

- it + 5; Go Back

val it = 4 : int Full Screen

- x * y; Close

val it = 30 : int Quit

-
SML: Primitive Integer
Operations 2
IIT Delhi
val y = 6 : int
S. Arun-Kumar, CSE
- x+y;
val it = 11 : int Title Page

- x-y; Contents

val it = ˜1 : int JJ II

- it + 5; J I

val it = 4 : int Page 64 of 709

- x * y; Go Back

val it = 30 : int Full Screen

- val a = 25; Close

val a = 25 : int Quit

-
SML: Primitive Integer
Operations 2
IIT Delhi
val it = 11 : int
S. Arun-Kumar, CSE
- x-y;
val it = ˜1 : int Title Page

- it + 5; Contents

val it = 4 : int JJ II

- x * y; J I

val it = 30 : int Page 65 of 709

- val a = 25; Go Back

val a = 25 : int Full Screen

- val b = 7; Close

val b = 7 : int Quit

-
SML: Primitive Integer
Operations 2
IIT Delhi
val it = ˜1 : int
S. Arun-Kumar, CSE
- it + 5;
val it = 4 : int Title Page

- x * y; Contents

val it = 30 : int JJ II

- val a = 25; J I

val a = 25 : int Page 66 of 709

- val b = 7; Go Back

val b = 7 : int Full Screen

- val q = a div b; Close

val q = 3 : int Quit

-
SML: Primitive Integer
Operations 2
- x * y; IIT Delhi

val it = 30 : int S. Arun-Kumar, CSE

- val a = 25; Title Page

val a = 25 : int Contents

- val b = 7; JJ II

val b = 7 : int J I

- val q = a div b; Page 67 of 709

val q = 3 : int Go Back

- val r = a mod b; Full Screen

GC #0.0.0.0.2.45: (0 ms) Close

val r = 4 : int Quit

-
SML: Primitive Integer
Operations 3
IIT Delhi
- val a = 25;
S. Arun-Kumar, CSE
val a = 25 : int
- val b = 7; Title Page

val b = 7 : int Contents

- val q = a div b; JJ II

val q = 3 : int J I

- val r = a mod b; Page 68 of 709

GC #0.0.0.0.2.45: (0 ms) Go Back

val r = 4 : int Full Screen

- a = b*q + r; Close

val it = true : bool Quit

-
SML: Primitive Integer
Operations 3
IIT Delhi
- val b = 7;
S. Arun-Kumar, CSE
val b = 7 : int
- val q = a div b; Title Page

val q = 3 : int Contents

- val r = a mod b; JJ II

GC #0.0.0.0.2.45: (0 ms) J I

val r = 4 : int Page 69 of 709

- a = b*q + r; Go Back

val it = true : bool Full Screen

- val c = ˜7; Close

val c = ˜7 : int Quit

-
SML: Primitive Integer
Operations 3
IIT Delhi
- val q = a div b;
S. Arun-Kumar, CSE
val q = 3 : int
- val r = a mod b; Title Page

GC #0.0.0.0.2.45: (0 ms) Contents

val r = 4 : int JJ II

- a = b*q + r; J I

val it = true : bool Page 70 of 709

- val c = ˜7; Go Back

val c = ˜7 : int Full Screen

- val q1 = a div c; Close

val q1 = ˜4 : int Quit

-
SML: Primitive Integer
Operations 3
IIT Delhi
- val r = a mod b;
S. Arun-Kumar, CSE
GC #0.0.0.0.2.45: (0 ms)
val r = 4 : int Title Page

- a = b*q + r; Contents

val it = true : bool JJ II

- val c = ˜7; J I

val c = ˜7 : int Page 71 of 709

- val q1 = a div c; Go Back

val q1 = ˜4 : int Full Screen

- val r1 = a mod c; Close

val r1 = ˜3 : int Quit

-
SML: Primitive Integer
Operations 3
IIT Delhi
val r = 4 : int
S. Arun-Kumar, CSE
- a = b*q + r;
val it = true : bool Title Page

- val c = ˜7; Contents

val c = ˜7 : int JJ II

- val q1 = a div c; J I

val q1 = ˜4 : int Page 72 of 709

- val r1 = a mod c; Go Back

val r1 = ˜3 : int Full Screen

- a = c*q1 + r1; Close

val it = true : bool Quit

-
Quotient & Remainder
IIT Delhi

For any two integers a and b, the quo-


S. Arun-Kumar, CSE
tient q and remainder r are uniquely
determined to satisfy Title Page

• Contents

JJ II
a=b×q+r
J I

• Page 73 of 709

0 ≤ r < b when b > 0 Go Back

b < r ≤ 0 when b < 0 Full Screen

So 0 ≤ |r| < |b| always. Close

Quit
IIT Delhi

SML: Primitive Real S. Arun-Kumar, CSE

Operations 1 Title Page

Contents

sml JJ II
Standard ML of New Jersey,
J I
- val real_a = real a;
Page 74 of 709

val real_a = 25.0 : real


Go Back

-
Full Screen

Close

Quit
SML: Primitive Real
Operations 1
IIT Delhi

sml S. Arun-Kumar, CSE

Standard ML of New Jersey, Title Page

- val real_a = real a; Contents

val real_a = 25.0 : real JJ II


- real_a + b;
J I
stdIn:40.1-40.11 Error: operator and
Page 75 of 709

operator domain: real * real


Go Back

operand: real * int


Full Screen
in expression:
Close
real_a + b
- Quit
SML: Primitive Real
Operations 1
stdIn:40.1-40.11 Error: operator and IIT Delhi

operator domain: real * real S. Arun-Kumar, CSE

operand: real * int Title Page

in expression: Contents

real_a + b JJ II

- b + real_a; J I

stdIn:1.1-2.6 Error: operator and ope


Page 76 of 709

operator domain: int * int Go Back

operand: int * real Full Screen

in expression: Close

b + real_a Quit

-
SML: Primitive Real
Operations 2
- val a = 25.0; IIT Delhi

val a = 25.0 : real S. Arun-Kumar, CSE

- val b = 7.0; Title Page

val b = 7.0 : real Contents

- a/b; JJ II

val it = 3.57142857143 : real J I

- a div b; Page 77 of 709

stdIn:49.3-49.6 Error: overloaded var Go Back

symbol: div Full Screen

type: real Close

GC #0.0.0.0.3.98: (0 ms) Quit

-
SML: Primitive Real IIT Delhi

Operations 3 S. Arun-Kumar, CSE

- val c = a/b; Title Page

val c = 3.57142857143 : real Contents

- trunc(c); JJ II

val it = 3 : int J I

- trunc (c + 0.5); Page 78 of 709

val it = 4 : int Go Back

- Full Screen

Close

Quit
SML: Primitive Real
Operations 4
- val d = 3.0E10; IIT Delhi

val d = 30000000000.0 : real S. Arun-Kumar, CSE

- val pi = 0.314159265E1; Title Page

val pi = 3.14159265 : real Contents

- - d+pi; JJ II

val it = 30000000003.1 : real J I

- d-pi; Page 79 of 709

val it = 29999999996.9 : real Go Back

- pi + d; Full Screen

val it = 30000000003.1 : real Close

- Quit
SML: Precision
- pi + d*10.0; IIT Delhi

val it = 300000000003.0 : real S. Arun-Kumar, CSE

- pi + d*100.0;
Title Page

val it = 3E12 : real


Contents

- d*100.0 + pi;
JJ II
val it = 3E12 : real
J I
- d*100.0 -pi;
Page 80 of 709
val it = 3E12 : real
- d*10.0 - pi; Go Back

val it = 299999999997.0 : real Full Screen

- Close

Quit
1.4. Example: Fibonacci
1. Fibonacci Numbers: 1

2. Fibonacci Numbers: 2

3. Fibonacci Numbers: 3 IIT Delhi

4. Fibonacci Numbers: 4
S. Arun-Kumar, CSE
5. Fibonacci Numbers: 5

6. Is Fa(n, 1, 1) = F(n)? Title Page

7. Trial & Error


Contents
8. Generalization

9. Proof JJ II
10. Another Generalization

11. Try Proving it! J I

12. Another Generalization


Page 81 of 709
13. Try Proving it!

14. Complexity Go Back

15. Complexity
Full Screen
16. Time Complexity: R

17. Time Complexity Close

18. Time Complexity: R


Quit
19. Bound on R

20. Other Bounds: CF

21. Other Bounds: AF


IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 82 of 709

Go Back

Full Screen

Close

Quit
Fibonacci Numbers: 1 IIT Delhi

 F(0) = 1 S. Arun-Kumar, CSE

F(1) = 1 Title Page

F(n) = F(n − 1) + F(n − 2) if n > 1



Contents

JJ II

fun fib (n) = J I

if (n = 0) orelse (n = 1) Page 83 of 709

then 1 Go Back

else fib (n-1) + fib (n-2); Full Screen

Close

Quit
Fibonacci Numbers: 2 IIT Delhi

 S. Arun-Kumar, CSE

 F(0) = 1
F(1) = 1 Title Page

F(n) = F(n − 1) + F(n − 2) if n > 1 Contents



JJ II

Alternatively, J I

1 if n = 0 Page 84 of 709

F(n) = 1 if n = 1 Go Back

F(n − 1) + F(n − 2) if n > 1



Full Screen

Close

Quit
Fibonacci Numbers: 3
fun fib (n) = IIT Delhi

if (n = 0) orelse (n = 1) S. Arun-Kumar, CSE

then 1 Title Page

else fib (n-1) + fib (n-2); Contents

JJ II

Alternatively, J I

fun fib (n) = Page 85 of 709

if (n = 0) then 1 Go Back

else if (n = 1) then 1 Full Screen

else fib (n-1) + fib (n-2); Close

Quit
Fibonacci Numbers: 4

 F(0) = 1 IIT Delhi

F(1) = 1 S. Arun-Kumar, CSE

F(n) = F(n − 1) + F(n − 2) if n > 1



Title Page

Alternatively, Contents

JJ II

F(n) = Fa(n, 1, 1) J I

where Page 86 of 709

 Go Back

a if n = 0
Fa(n, a, b) = b if n = 1 Full Screen

Fa(n − 1, b, a + b) if n > 1 Close



Quit
IIT Delhi

Fibonacci Numbers: 5 S. Arun-Kumar, CSE

Title Page

fun fib_a (n, a, b) =


Contents
if (n = 0) then a
JJ II
else if (n = 1) then b
J I
else fib_a (n, b, a+b);
Page 87 of 709

fun fib (n) = fib_a (n, 1, 1); Go Back

Full Screen

Close

Quit
Is Fa(n, 1, 1) = F(n)? IIT Delhi

S. Arun-Kumar, CSE

Intuition Fa is a generalization of F. Title Page

Question 1 What does it actually gen- Contents

eralize to? JJ II

Question 2 Does the generalization J I

have a non-recursive form? Page 88 of 709

Trial & Error Can we use trial and er- Go Back

ror to get a non-recursive form? Full Screen

Close

Quit
Trial & Error IIT Delhi

S. Arun-Kumar, CSE

Fa(2, a, b) = a + b
Title Page

Fa(3, a, b) = Fa(2, b, a + b)
Contents

= a + 2b
JJ II
Fa(4, a, b) = Fa(3, b, a + b)
J I
= Fa(2, a + b, a + 2b)
= 2a + 3b Page 89 of 709

Fa(5, a, b) = Fa(2, a + 2b, 2a + 3b) Go Back

= 3a + 5b Full Screen

Close

Quit
Generalization
• Fa(0, a, b) = a IIT Delhi

S. Arun-Kumar, CSE

• Fa(1, a, b) = b
Title Page

• Fa(n, a, b) = aF(n − 2) + bF(n − 1) Contents

• When a = 1 and b = 1, for all n ≥ 0, JJ II

Fa(n, a, b) = F(n) J I

Theorem 1 For all integers a, b and Page 90 of 709

n > 1, Go Back

Full Screen

Fa(n, a, b) = aF(n − 2) + bF(n − 1) Close

Quit
Proof by Induction on
n>1
Proof:
Basis For n = 2, Fa(2, a, b) = a + b = IIT Delhi

aF(0) + bF(1) S. Arun-Kumar, CSE

Title Page
Induction hypothesis (IH) Assume
Fa(k, a, b) = aF(k − 2) + bF(k − 1), Contents

for some k > 1 and all integers a, b JJ II

Induction Step J I

Page 91 of 709

Fa(k + 1, a, b)
Go Back
= Fa(k, b, a + b) Definition of Fa
= bF(k − 2) + (a + b)F(k − 1) IH Full Screen

= aF(k − 1) + b(F(k − 2) + F(k − 1)) Close

= aF(k − 1) + bF(k) Definition of F Quit


IIT Delhi

S. Arun-Kumar, CSE

Another Generalization Title Page

Try to prove a different and more di- Contents

rect theorem. JJ II

J I
Theorem 2 For all integers n > 1,
Fa(n, 1, 1) = F(n) Page 92 of 709

Go Back

Full Screen

Close

Quit
Try Proving it!
Proof: By induction on n > 1.
IIT Delhi

Basis For n = 0 and n = 1, Fa(0, 1, 1) =


S. Arun-Kumar, CSE

1 = Fa(1)
Title Page

Induction hypothesis (IH) Assume Contents

Fa(k, 1, 1) = F(k), for some k > 1 JJ II


Induction Step
J I

Fa(k + 1, 1, 1) Page 93 of 709

= Fa(k, 1, 2) Definition of Fa Go Back

= ? ? ? IH Full Screen

STUCK! Close

Quit


Another Generalization
Try to prove a different and more di- IIT Delhi

rect theorem. S. Arun-Kumar, CSE

Theorem 3 For all integers n ≥ 1 and Title Page

j ≥ 1, Contents

JJ II

Fa(n, F(j − 1), F(j)) = F(n + j − 1) J I

Page 94 of 709

Proof: By induction on n ≥ 1, for all


Go Back

values of j ≥ 1. 
Full Screen

Corollary 4 For all integers n ≥ 1, Close

Fa(n, F(0), F(1)) = F(n)


Quit
Try Proving it!
Basis For n = 1, Fa(1, F(j − 1), F(j)) =
F(j) IIT Delhi

Induction hypothesis (IH) For some S. Arun-Kumar, CSE

k > 1 and all j ≥ 1, Title Page

Fa(k, F(j − 1), F(j)) = F(k + j − 1) Contents

Induction Step We need to prove JJ II

Fa(k + 1, F(j − 1), F(j)) = F(k + j). J I

Page 95 of 709

Fa(k + 1, F(j − 1), F(j)) Go Back

= Fa(k, F(j), F(j − 1) + F(j))


Full Screen

= Fa(k, F(j), F(j + 1))


Close

= Fa(k + (j + 1) − 1) IH
Quit

= Fa(k + j)
IIT Delhi

Complexity S. Arun-Kumar, CSE

Title Page

• Time complexity:
Contents

– No of additions: AF(n) JJ II

– No of comparisons: CF(n) J I

– No of recursive calls to F: RF(n) Page 96 of 709

• Space complexity: Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Complexity Title Page

• Time complexity: Contents

• Space complexity: JJ II

J I
– left-to-right evaluation: LRF(n)
Page 97 of 709

– arbitrary evaluation: UF(n) Go Back

Full Screen

Close

Quit
Time Complexity:
IIT Delhi

R S. Arun-Kumar, CSE

• Hardware operations like addition Title Page

and comparisons are usually very Contents

fast compared to software opera- JJ II

tions like recursion unfolding J I

• The number of recursion unfoldings Page 98 of 709

also includes comparisons and ad- Go Back

ditions. Full Screen

Close

Quit
Time Complexity IIT Delhi

S. Arun-Kumar, CSE

• It is enough to put bounds on the


Title Page

number of recursion unfoldings and


Contents
not worry about individual hardware
JJ II
operations.
J I
• Similar theorems may be proved for
Page 99 of 709

any operation by counting and in-


Go Back

duction.
So we concentrate on R.
Full Screen

Close

Quit
Time Complexity: R IIT Delhi

• RF(0) = RF(1) = 0 S. Arun-Kumar, CSE

• RF(n) = 2 + RF(n − 1) + RF(n − 2) Title Page

for n > 1 Contents

JJ II
To solve the equation as initial value
J I
problem and obtain an upper bound
Page 100 of 709
we guess the following theorem.
Go Back

Theorem 5 RF(n) ≤ 2n−1 for all n > 2 Full Screen

Proof: By induction on n > 2.  Close

Quit
Bound on R
Basis n = 3. RF(3) = 2 + 2 + 0 ≤ 23−1 IIT Delhi

Induction hypothesis (IH) For some S. Arun-Kumar, CSE

k > 2, RF(k) ≤ 2k−1 Title Page

Induction Step If n = k + 1 then n > 3 Contents

JJ II
RF(n)
J I
= 2 + RF(n − 2) + RF(n − 1)
2 + 2n−3 + 2n−2 (IH)
Page 101 of 709


2.2n−3 + 2n−2 for n > 3, 2n−3 ≥ 2
Go Back


2n−2 + 2n−2
Full Screen

=
2n−1
Close

=
Quit
IIT Delhi

Other Bounds: CF S. Arun-Kumar, CSE

Title Page

One comparison for each call.


Contents

• CF(0) = CF(1) = 1 JJ II

• CF(n) = 1 + CF(n − 1) + CF(n − 2) for J I

n>1 Page 102 of 709

Theorem 6 CF(n) ≤ 2n for all n ≥ 0. Go Back

Full Screen

Close

Quit
Other Bounds: AF IIT Delhi

S. Arun-Kumar, CSE

No additions for the basis and one ad- Title Page

dition in each call. Contents

• AF(0) = AF(1) = 0 JJ II

• AF(n) = 1 + AF(n − 1) + AF(n − 2) J I

for n > 1 Page 103 of 709

Theorem 7 AF(n) ≤ 2n−1 for all n > Go Back

0. Full Screen

Close

Quit
1.5. Primitives: Booleans
1. Boolean Conditions
IIT Delhi
2. Booleans in SML

3. Booleans in SML S. Arun-Kumar, CSE

4. ∧ vs. andalso
Title Page
5. ∨ vs. orelse

6. SML: orelse Contents

7. SML: andalso
JJ II
8. and, andalso, ⊥

9. or, orelse, ⊥ J I
10. Complex Boolean Conditions
Page 104 of 709

Go Back

Full Screen

Close

Quit
Boolean Conditions IIT Delhi

• Two (truth) value set : {true, S. Arun-Kumar, CSE

false} Title Page

• Boolean conditions are those state- Contents

ments or names which can take JJ II

only truth values. J I

Examples: n < 0, true, Page 105 of 709

• Negation operator: not Go Back

Examples: not (n < 0), not Full Screen

true, not false Close

Quit
Booleans in SML
Standard ML of New Jersey,
- val tt = true; IIT Delhi

val tt = true : bool S. Arun-Kumar, CSE

- not(tt); Title Page

val it = false : bool Contents

- val n = 10; JJ II

val n = 10 : int J I

- n < 10; Page 106 of 709

val it = false : bool Go Back

- not (n<10); Full Screen

val it = true : bool


Close

-
Quit
Booleans in SML IIT Delhi

Examples: S. Arun-Kumar, CSE

- (n >= 10) andalso (n=10); Title Page

val it = true : bool Contents

- n < 0 orelse n >= 10; JJ II

val it = true : bool


- not ((n >= 10) J I

= andalso (n=10)) Page 107 of 709

= orelse n < 0 orelse n >= 10; Go Back

val it = true : bool Full Screen

- Close

Quit
IIT Delhi

vs. andalso
S. Arun-Kumar, CSE

∧ Title Page

p q p∧q p andalso q Contents

true true true true JJ II

true false false false J I


false true false false Page 108 of 709

false false false false


Go Back

Full Screen

Close

Quit
IIT Delhi

vs. orelse
S. Arun-Kumar, CSE

∨ Title Page

p q p∨q p orelse q Contents

true true true true JJ II

true false true true J I


false true true true Page 109 of 709

false false false false


Go Back

Full Screen

Close

Quit
SML: orelse
Standard ML of New Jersey, IIT Delhi

- val tt = true; S. Arun-Kumar, CSE

val tt = true : bool Title Page

- val ff = false;
val ff = false : bool Contents

- fun gtz n = if n=1 then true JJ II

= else gtz (n-1); J I

val gtz = fn : int -> bool Page 110 of 709

- tt orelse (gtz 0); Go Back

val it = true : bool Full Screen

- Close

Quit
SML: andalso IIT Delhi

- (gtz 0) orelse tt; S. Arun-Kumar, CSE

Title Page

Interrupt Contents

- ff andalso (gtz 0);


val it = false : bool JJ II

- (gtz 0) andalso ff; J I

Page 111 of 709

Interrupt Go Back

- Full Screen

Close

Quit
IIT Delhi

and, andalso, ⊥ S. Arun-Kumar, CSE

p q p∧q p andalso q Title Page

true ⊥ ⊥ ⊥ Contents

⊥ true ⊥ ⊥ JJ II

false ⊥ false false J I

⊥ false false ⊥ Page 112 of 709

∧ is commutative whereas andalso Go Back

is not. Full Screen

Close

Quit
IIT Delhi

or, orelse, ⊥ S. Arun-Kumar, CSE

p q p∨q p orelse q Title Page

true ⊥ true true Contents

⊥ true true ⊥ JJ II

false ⊥ ⊥ ⊥ J I

⊥ false ⊥ ⊥ Page 113 of 709

∨ is commutative whereas orelse is Go Back

not. Full Screen

Close

Quit
Complex Boolean IIT Delhi

Conditions S. Arun-Kumar, CSE

Title Page

Assume p and q are boolean condi- Contents

tions JJ II

J I

p orelse q ≡ if p then true else q Page 114 of 709

Go Back

p andalso q ≡ if p then q else false Full Screen

Close

Quit
2. Algorithms: Design & Refinement
2.1. Technical Completeness & Algorithms IIT Delhi

1. Recapitulation: Integers & Real


S. Arun-Kumar, CSE
2. Recap: Integer Operations

3. Recapitulation: Real Operations Title Page

4. Recapitulation: Simple Algorithms


Contents
5. More Algorithms
6. Powering: Math JJ II
7. Powering: SML
J I
8. Technical completeness

9. What SML says Page 115 of 709

10. Technical completeness


Go Back
11. What SML says ... contd

12. Powering: Math 1


Full Screen
13. Powering: SML 1

14. Technical Completness Close

15. What SML says


Quit
16. Powering: Integer Version

17. Exceptions: A new primitive

18. Integer Power: SML

19. Integer Square Root 1 IIT Delhi

20. Integer Square Root 2


S. Arun-Kumar, CSE
21. An analysis

22. Algorithmic idea Title Page

23. Algorithm: isqrt


Contents
24. Algorithm: shrink
25. SML: shrink JJ II
26. SML: intsqrt

27. Run it! J I

28. SML: Reorganizing Code


Page 116 of 709
29. Intsqrt: Reorganized

30. shrink: Another algorithm Go Back

31. Shrink2: SML


Full Screen
32. Shrink2: SML ... contd

Close

Quit
IIT Delhi

Recapitulation: S. Arun-Kumar, CSE

Integers & Real Title Page

Contents

• Primitive Integer Operations JJ II

• Primitive Real Operations J I

• Some algorithms Page 117 of 709

Go Back
Forward
Full Screen

Close

Quit
Recap: Integer IIT Delhi

Operations S. Arun-Kumar, CSE

• Primitive Integer Operations Title Page

– Naming, +, −, ∼
Contents

JJ II
– Multiplication, division
J I
– Quotient & remainder
Page 118 of 709

• Primitive Real Operations Go Back

• Some algorithms Full Screen

Back Close

Quit
Recapitulation: Real
Operations IIT Delhi

• Primitive Integer Operations S. Arun-Kumar, CSE

• Primitive Real Operations Title Page

Contents

– Integer to Real
JJ II

– Real to Integer J I

– Real addition & subtraction Page 119 of 709

– Real division Go Back

– Real Precision Full Screen

• Some algorithms Close

Back Quit
Recapitulation: Simple IIT Delhi

Algorithms S. Arun-Kumar, CSE

• Primitive Integer Operations Title Page

• Primitive Real Operations


Contents

JJ II

• Some algorithms J I

– Factorial Page 120 of 709

– Fibonacci Go Back

– Euclidean GCD Full Screen

Back Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

More Algorithms Title Page

Contents

• Powering JJ II

• Integer square root J I

• Combinations nCk Page 121 of 709

Go Back

Full Screen

Close

Quit
Powering: Math IIT Delhi

For any integer or real number x 6= 0 S. Arun-Kumar, CSE

and non-negative integer n Title Page

xn = |x × x ×{z· · · × x} Contents

n times JJ II

Noting that x0 = 1 we give an induc- J I

tive definition: Page 122 of 709

Go Back

n 1 if n = 0
x =
xn−1 × x otherwise
Full Screen

Close

Quit
IIT Delhi

Powering: SML S. Arun-Kumar, CSE

Title Page

fun power (x:real, n) =


Contents

if n = 0
JJ II
then 1.0
J I
else power (x, n-1) * x
Page 123 of 709

Is it technically complete? Go Back

Full Screen

Close

Quit
Technical IIT Delhi

completeness S. Arun-Kumar, CSE

Title Page
Can it be always guaranteed that
Contents

• x will be real? JJ II

• n will be integer? J I

• n will be non-negative? Page 124 of 709

• x 6= 0? Go Back

If x = 0 what is 0.00?
Full Screen

Close

Quit
What SML says
sml
IIT Delhi
Standard ML of New Jersey
- use "/tmp/power.sml"; S. Arun-Kumar, CSE

[opening /tmp/power.sml] Title Page

val power = fn : real * int -> Contents

real JJ II

val it = () : unit J I

Page 125 of 709

Go Back

Can it be always guaranteed that Full Screen

• x will be real? YES Close

Quit

• n will be integer? YES


Technical IIT Delhi

completeness S. Arun-Kumar, CSE

Can it be always guaranteed that Title Page

• n will be non-negative? NO
Contents

JJ II
• x 6= 0? NO J I

If x = 0 what is 0.00? Page 126 of 709

Go Back

- power(0.0, 0); Full Screen

val it = 1.0 : real Close

Quit
What SML says ...
contd IIT Delhi

sml S. Arun-Kumar, CSE

Standard ML of New Jersey Title Page

val power = fn : real * int -> real Contents

val it = () : unit
JJ II
- power(˜2.5, 0);
J I
val it = 1.0 : real
Page 127 of 709

- power (0.0, 3);


Go Back
val it = 0.0 : real
Full Screen
- power (2.5, ˜3)
Close

Goes on forever! Quit


IIT Delhi

Powering: Math 1
S. Arun-Kumar, CSE

Title Page

For any real number x and integer n Contents

 1.0/x−n if n < 0
 JJ II

xn = 1 if n = 0 J I

 n−1
x × x otherwise Page 128 of 709

Go Back

Full Screen

Close

Quit
Powering: SML 1 IIT Delhi

fun power (x, n) = S. Arun-Kumar, CSE

if n < 0 Title Page

then 1.0/power(x, ˜n) Contents

else if n = 0 JJ II

then 1.0 J I

else power (x, n-1) * x Page 129 of 709

Go Back

Is this definition technically com- Full Screen

plete? Close

Quit
Technical Completness IIT Delhi

• 0.00 = 1.0 whereas 0.0n = 0 for pos- S. Arun-Kumar, CSE

itive n Title Page

Contents

• What if x = 0.0 and n = −m < 0?


JJ II
Then
0.0n J I

= 1.0/(0.0m) Page 130 of 709

= 1.0/0.0 Go Back

Full Screen
Division by zero!
Close

Quit
What SML says
- power (2.5, ˜2); IIT Delhi

val it = 0.16 : real S. Arun-Kumar, CSE

- power (˜2.5, ˜2); Title Page

val it = 0.16 : real Contents

- power (0.0, 2); JJ II

val it = 0.0 : real J I


- power (0.0, ˜2); Page 131 of 709

val it = inf : real


Go Back

-
Full Screen

Close
SML is somewhat more understand-
ing than most languages Quit
Powering: Integer
Version IIT Delhi

 S. Arun-Kumar, CSE


 undefined if n < 0 Title Page

undefined if x = 0&n = 0

n
x = Contents

 1 if x 6= 0&n = 0 JJ II
 n−1

x ×x otherwise J I

Technical completeness requires us Page 132 of 709

to consider the case n < 0. Other- Go Back

wise, the computation can go on for- Full Screen

ever Close

Notation: ⊥ denotes the undef ined


Quit
Exceptions: A new
primitive IIT Delhi

exception negExponent; S. Arun-Kumar, CSE

exception zeroPowerZero; Title Page

fun intpower (x, n) = Contents

if n < 0 JJ II

then raise negExponent J I

else if n = 0 Page 133 of 709

then if x=0 Go Back

then raise zeroPowerZero


Full Screen

else 1
Close

else intpower (x, n-1) * x


Quit
Integer Power: SML
- intpower(3, 4);
val it = 81 : int IIT Delhi

- intpower(˜3, 5); S. Arun-Kumar, CSE

val it = ˜243 : int Title Page

- intpower(3, ˜4); Contents

JJ II

uncaught exception negExponent J I

raised at: intpower.sml:4.16-4.32 Page 134 of 709

- intpower (0, 0); Go Back

Full Screen

uncaught exception zeroPowerZero Close

raised at: stdIn:24.26-24.39 Quit

Back to More Algos


Integer Square Root 1

isqrt(n) = b nc IIT Delhi

S. Arun-Kumar, CSE

- fun isqrt n = Title Page

trunc (Real.Math.sqrt Contents

(real (n))); JJ II

val isqrt = fn : int -> int J I


- isqrt (38); Page 135 of 709

val it = 6 : int
Go Back

- isqrt (˜38);
Full Screen

uncaught exception domain error


Close
raised at: boot/real64.sml:89.32-89
Quit
-
Integer Square Root 2
Suppose Real.Math.sqrt were not IIT Delhi

available to us! S. Arun-Kumar, CSE

isqrt(n) of a non-negative integer n is Title Page

the integer k ≥ 0 such that k 2 ≤ n < Contents

(k + 1)2 JJ II

That is, J I


⊥ if n < 0 Page 136 of 709

isqrt(n) =
k otherwise Go Back

Full Screen

where 0 ≤ k 2 ≤ n < (k + 1)2. Close

This value of k is unique!


Quit
An analysis IIT Delhi

S. Arun-Kumar, CSE

0 ≤ k 2 ≤√n < (k + 1)2 Title Page

⇒ 0≤k ≤ n<k+1 Contents

⇒ 0≤k≤n JJ II

Strategy. Use this fact to close in on J I

the value of k. Start with the inter- Page 137 of 709

val [l, u] = [0, n] and try to shrink it till Go Back

it collapses to the interval [k, k] which


contains a single value. Full Screen

Close

Quit
Algorithmic idea
If n = 0 then isqrt(n) = 0. IIT Delhi

Otherwise with [l, u] = [0, n] and S. Arun-Kumar, CSE

l2 ≤ n < u2
Title Page

Contents

use one or both of the following to JJ II

shrink the interval [l, u]. J I

• if (l + 1)2 ≤ n then try [l + 1, u] Page 138 of 709

otherwise l2 ≤ n < (l + 1)2 and k = l Go Back

• if u2 > n then try [l, u − 1] Full Screen

otherwise (u − 1)2 ≤ n < u2 and Close

k =u−1 Quit
IIT Delhi

S. Arun-Kumar, CSE

Algorithm: isqrt Title Page

 Contents

⊥ if n < 0
JJ II
isqrt(n) = 0 if n = 0
J I
shrink(n, 0, n) if n > 0

Page 139 of 709

where Go Back

Full Screen

Close

Quit
Algorithm: shrink
shrink(n, l, u) = IIT Delhi

 S. Arun-Kumar, CSE


 l if l = u
 Title Page


 shrink(n, l + 1, u) if l < u
and (l + 1)2 ≤ n
 Contents


 JJ II
if (l + 1)2 > n

l


J I
shrink(n, l, u − 1) if l < u
and u2 > n
 Page 140 of 709


 Go Back

u−1 if l < u




and (u − 1)2 ≤ n
 Full Screen


 Close
⊥

if l > u
Quit
SML: shrink
exception intervalError; IIT Delhi

fun shrink (n, l, u) = S. Arun-Kumar, CSE

if l>u orelse Title Page

l*l > n orelse Contents

u*u < n JJ II

then raise intervalError J I

else if (l+1)*(l+1) <= n Page 141 of 709

then shrink (n, l+1, u) Go Back

else l; Full Screen

Close

intsqrt
Quit
SML: intsqrt IIT Delhi

S. Arun-Kumar, CSE

exception negError;
Title Page

fun intsqrt n =
Contents

if n<0
JJ II
then raise negError
J I
else if n=0
then 0 Page 142 of 709

else shrink (n, 0, n) Go Back

Full Screen

shrink Close

Quit
Run it!
exception intervalError
IIT Delhi
val shrink =
S. Arun-Kumar, CSE
fn : int * int * int -> int
Title Page
exception negError
val intsqrt = fn : int -> int Contents

val it = () : unit JJ II

- intsqrt 8; J I

val it = 2 : int Page 143 of 709

- intsqrt 16; Go Back

val it = 4 : int Full Screen

- intsqrt 99; Close

val it = 9 : int Quit


SML: Reorganizing IIT Delhi

Code S. Arun-Kumar, CSE

Title Page

• shrink was used to develop Contents

intsqrt JJ II

• Is shrink general-purpose J I

enough to be kept separate? Page 144 of 709

• Shouldn’t shrink be placed within Go Back

intsqrt? Full Screen

Close

Quit
Intsqrt: Reorganized
IIT Delhi

exception negError; S. Arun-Kumar, CSE

fun intsqrt n = Title Page

let fun shrink (n, l, u) = ... Contents

in if n<0 JJ II
then raise negError
J I
else if n=0
Page 145 of 709

then 0
Go Back
else shrink (n, 0, n)
Full Screen
end
Close

Quit
shrink: Another
algorithm IIT Delhi

Shrink
S. Arun-Kumar, CSE

shrink2(n, l, u) = Title Page

 Contents


 l if l = u or u = l + 1 JJ II


 shrink2(n, m, u) if l < u J I

and m2 ≤ n


Page 146 of 709

 shrink2(n, l, m) if l < u
Go Back



 and m 2>n
 Full Screen

⊥ if l > u

Close

where m = (l + u) div 2 Quit


Shrink2: SML IIT Delhi

S. Arun-Kumar, CSE

fun shrink2 (n, l, u) =


Title Page

if l>u orelse
Contents

l*l > n orelse


JJ II
u*u < n
J I
then raise intervalError
Page 147 of 709
else if l = u
then l Go Back

Full Screen

Close

Quit
Shrink2: SML ... contd IIT Delhi

else S. Arun-Kumar, CSE

let val m = (l+u) div 2; Title Page

val msqr = m*m Contents

in if msqr <= n JJ II

then shrink (n, m, u) J I

else shrink (n, l, m) Page 148 of 709

end; Go Back

Full Screen

Back to More Algos


Close

Quit
2.2. Algorithm Refinement
1. Recap: More Algorithms
IIT Delhi
2. Recap: Power
3. Recap: Technical completeness S. Arun-Kumar, CSE

4. Recap: More Algorithms


Title Page
5. Intsqrt: Reorganized
6. Intsqrt: Reorganized Contents

7. Some More Algorithms


JJ II
8. Combinations: Math
9. Combinations: Details
J I
10. Combinations: SML
11. Perfect Numbers Page 149 of 709

12. Refinement
Go Back
13. Perfect Numbers: SML
Pu
14. l if divisor(k)
Full Screen

15. SML: sum divisors


Close
16. if divisor and ifdivisor
17. SML: Assembly 1 Quit
18. SML: Assembly 2

19. Perfect Numbers . . . contd.

20. Perfect Numbers . . . contd.

21. SML: Assembly 3 IIT Delhi

22. Perfect Numbers: Run


S. Arun-Kumar, CSE
23. Perfect Numbers: Run

24. SML: Code variations Title Page

25. SML: Code variations


Contents
26. SML: Code variations
27. Summation: Generalizations JJ II
28. Algorithmic Improvements:

29. Algorithmic Variations J I

30. Algorithmic Variations


Page 150 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Recap: More Title Page

Algorithms Contents

• xn for real and integer x


JJ II

J I

• Integer square root Page 151 of 709

Forward
Go Back

Full Screen

Close

Quit
Recap: Power
• xn for real and integer x
IIT Delhi

S. Arun-Kumar, CSE

– Technical Completness Title Page

∗ Undefinedness Contents

∗ Termination JJ II

– More complete definition for real J I

x Page 152 of 709

– Power of an integer Go Back

– ⊥ and exceptions Full Screen

• Integer square root Close

Quit
Recap: Technical
completeness IIT Delhi

S. Arun-Kumar, CSE

Can it be always guaranteed that


Title Page

• x will be real? YES Contents

• n will be integer? YES JJ II

• n will be non-negative? NO J I

• x 6= 0? NO Page 153 of 709

If x = 0 what is 0.00?
Go Back

Full Screen

Close

INFINITE COMPUTATION Quit


Recap: More IIT Delhi

Algorithms S. Arun-Kumar, CSE

• xn for real and integer x Title Page

• Integer square root Contents

JJ II
– Analysis J I

– Algorithmic idea Page 154 of 709

– Algorithm Go Back

– where Full Screen

– and let ...in ...end Close

Quit
Intsqrt: Reorganized
exception negError; IIT Delhi

exception intervalError; S. Arun-Kumar, CSE

fun intsqrt n = Title Page

let fun shrink (n, l, u) = Contents

if l>u orelse JJ II
l*l > n orelse
J I
u*u < n
Page 155 of 709

then raise intervalError


Go Back
else if (l+1)*(l+1) <= n
Full Screen
then shrink (n, l+1, u)
Close
else l;
Quit
Intsqrt: Reorganized IIT Delhi

S. Arun-Kumar, CSE

in if n<0 Title Page

then raise negError Contents

else if n=0 JJ II

then 0 J I

else shrink (n, 0, n) Page 156 of 709

end Go Back

Full Screen

Back
Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Some More Algorithms Contents

• Combinations JJ II

J I
• Perfect Numbers
Page 157 of 709

Go Back

Full Screen

Close

Quit
Combinations: Math
nC = n!
k (n−k)!k! IIT Delhi

S. Arun-Kumar, CSE

n(n−1)···(n−k+1)
= k!
Title Page

Contents

n(n−1)···(k+1) JJ II
= (n−k)! J I

= n−1Ck−1 + n−1Ck
Page 158 of 709

Go Back

Since we already have the function Full Screen

fact, we may program nCk using Close

any of the above identities. Let’s pro-


gram it using the last one. Quit
Combinations: Details
Given a set of n ≥ 0 elements, find IIT Delhi

the number of subsets of k elements, S. Arun-Kumar, CSE

where 0 ≤ k ≤ n Title Page



 ⊥ if n < 0 or Contents

k < 0 or JJ II




k>n

 J I

nC = 1 if n = 0 or Page 159 of 709
k
k = 0 or

 Go Back


k=n

 Full Screen

 n−1
Ck−1 + n−1Ck otherwise

Close

Quit
Combinations: SML
exception invalid_arg;
fun comb (n, k) = IIT Delhi

if n < 0 orelse S. Arun-Kumar, CSE

k < 0 orelse Title Page

k > n Contents

then raise invalid_arg JJ II

else if n = 0 orelse J I

k = 0 orelse Page 160 of 709

n = k Go Back

then 1
Full Screen

else comb (n-1, k-1) +


Close

comb (n-1, k);


Quit

Back to Some More Algorithms


Perfect Numbers
An integer n > 0 is perfect if it equals IIT Delhi

the sum of all its proper divisors. S. Arun-Kumar, CSE

A divisor k|n is proper if 0 < k < n


Title Page

k|n ⇐⇒ n mod k = 0 Contents

JJ II

perf ect(n) J I

Page 161 of 709


P
⇐⇒ n = {k : 0 < k < n, k|n} Go Back

Full Screen
Pn−1
⇐⇒ n = k=1 if divisor(k) Close

where Quit
IIT Delhi

S. Arun-Kumar, CSE

Refinement Title Page

Contents

1. if divisor(k) needs to be defined JJ II


Pn−1
2. k=1 if divisor(k) needs to be de- J I

fined algorithmically. Page 162 of 709

Go Back

Full Screen

Close

Quit
Perfect Numbers: SML IIT Delhi

exception nonpositive; S. Arun-Kumar, CSE

fun perfect (n) = Title Page

if n <= 0 Contents

then raise nonpositive JJ II

else J I

n = sum_divisors (1, n-1) Page 163 of 709

Go Back

where sum divisors needs to be Full Screen

defined
Close

Quit
Pu
l if divisor(k) IIT Delhi
Pu
k=l if divisor(k) = S. Arun-Kumar, CSE

Title Page




 0 if l > u Contents

 JJ II

 if divisor(l)+ otherwise J I

 n−1 if divisor(k)

 P Page 164 of 709

k=l+1
Go Back

where if divisor(k) needs to be de- Full Screen

fined Close

Quit
SML: sum divisors
From
Pu the algorithmic definition of IIT Delhi

k=l if divisor(k) S. Arun-Kumar, CSE

Title Page

Contents
fun sum_divisors (l, u) =
JJ II
if l > u
then 0 J I

else ifdivisor (l) + Page 165 of 709

sum_divisors (l+1, u) Go Back

Full Screen

where if divisor(k) still needs to be Close

defined Quit
if divisor and ifdivisor
 IIT Delhi

k if k|n
if divisor(k) = S. Arun-Kumar, CSE

0 otherwise
Title Page

Contents

fun ifdivisor (k) = JJ II

if n mod k = 0 J I

then k Page 166 of 709

else 0 Go Back

Full Screen

Not technically complete! Close

However . . . Quit
SML: Assembly 1
fun sum_divisors (l, u) = IIT Delhi

if l > u then 0 S. Arun-Kumar, CSE

else
Title Page

let fun ifdivisor (k) =


Contents

if n mod k = 0
JJ II
then k
J I
else 0
Page 167 of 709
in ifdivisor (l) +
Go Back
sum_divisors (l+1, u)
end Full Screen

Close

Clearly k ∈ [l, u] Quit


SML: Assembly 2
exception nonpositive;
fun perfect (n) = IIT Delhi

if n <= 0 S. Arun-Kumar, CSE

then raise nonpositive Title Page

else Contents

let fun sum_divisors (l, u) = JJ II

... J I

in n = sum_divisors (1, n-1) Page 168 of 709

end Go Back

Full Screen

Clearly k ∈ [l, u] = [1, n − 1] whenever Close

n > 0. Quit

Technically complete!
Perfect Numbers
. . . contd. IIT Delhi

S. Arun-Kumar, CSE

Clearly for all k, n/2 < k < n,


Title Page

if divisor(k) = 0.
Contents

bn/2c = n div 2 < n/2 JJ II

J I
Hence
Page 169 of 709

n−1
X n div
X 2 Go Back

if divisor(k) = if divisor(k) Full Screen

k=1 k=1 Close

Quit
Perfect Numbers
. . . contd. IIT Delhi

Hence S. Arun-Kumar, CSE

perf ect(n) Title Page

Contents
Pn−1
⇐⇒ n = k=1 if divisor(k) JJ II

J I
Pn div 2
⇐⇒ n = k−1 if divisor(k) Page 170 of 709

where Go Back

 Full Screen

k if k|n
if divisor(k) = Close

0 otherwise
Quit
SML: Assembly 3
exception nonpositive;
fun perfect (n) = IIT Delhi

S. Arun-Kumar, CSE

if n <= 0 Title Page

then raise nonpositive Contents

else JJ II

let fun sum_divisors (l, u) = J I


...
Page 171 of 709

in n = sum_divisors (1, n div 2)


Go Back

end
Full Screen

Close

Clearly k ∈ [l, u] = [1, n div2] when-


Quit
ever n > 0.
Perfect Numbers: Run IIT Delhi

exception nonpositive S. Arun-Kumar, CSE

val perfect = fn : int -> bool Title Page

val it = () : unit Contents

- perfect ˜8; JJ II

uncaught exception nonpositive J I

raised at: perfect.sml:4.16-4.27


Page 172 of 709

- perfect 5; Go Back

val it = false : bool Full Screen

Close

Quit
Perfect Numbers: Run IIT Delhi

- perfect 6;
S. Arun-Kumar, CSE

val it = true : bool


Title Page

- perfect 23;
Contents

val it = false : bool


JJ II
- perfect 28;
J I
GC #0.0.0.1.3.88: (1 ms)
Page 173 of 709
val it = true : bool
- perfect 30; Go Back

val it = false : bool Full Screen

Close

Quit
SML: Code variations
exception nonpositive;
fun perfect (n) = IIT Delhi

if n <= 0 S. Arun-Kumar, CSE

then raise nonpositive


Title Page

else
Contents

let
JJ II
fun ifdivisor (k) = ...;
J I
fun sum_divisors (l, u) = ...
Page 174 of 709
in
Go Back
n=sum_divisors (1, n div 2)
end Full Screen

Close

Technically complete though Quit

ifdivisor, by itself is not!


SML: Code variations
What about this? IIT Delhi

exception nonpositive;
S. Arun-Kumar, CSE

fun perfect (n) =


Title Page

let
Contents

fun ifdivisor (k) = ...;


JJ II
fun sum_divisors (l, u) = ...
J I
in if n <= 0
Page 175 of 709
then raise nonpositive
else Go Back

n=sum_divisors (1, n div 2) Full Screen

end Close

Quit
SML: Code variations
What about this? IIT Delhi

exception nonpositive; S. Arun-Kumar, CSE

fun ifdivisor (k) = ...; Title Page

fun sum_divisors (l, u) = ...; Contents

fun perfect (n) = JJ II

if n <= 0 J I

then raise nonpositive Page 176 of 709

else Go Back

n=sum_divisors (1, n div 2) Full Screen

Close

Technically incomplete! Quit


Summation:
Generalizations IIT Delhi

S. Arun-Kumar, CSE

Need a method to compute summa-


Title Page

tions in general.
Contents

For any function f : Z → Z and inte-


JJ II
gers l and u,
 J I

0 if l > u
u

 Page 177 of 709
X 
f (i) = Go Back

 fP(l)+ otherwise
i=l  u
 Full Screen

i=l+1 f (i) Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Algorithmic Title Page

Improvements:
Contents

JJ II

1. perf ect2 J I

2. shrink2 Page 178 of 709

Go Back

Full Screen

Close

Quit
Algorithmic Variations
1. For any k|n, m = n div k is also a IIT Delhi

divisor of n S. Arun-Kumar, CSE

Title Page

2. 1 is a divisor of every positive num-


Contents

ber
√ JJ II

3. For n > 2, b nc < n div 2 J I


Pn div 2
4. Hence k=1 if divisor(k) = Page 179 of 709

√ Go Back
b nc
X Full Screen

1+ if divisor2(k)
Close

k=2
Quit
Algorithmic Variations
IIT Delhi

perf ect(n) S. Arun-Kumar, CSE

Pb√nc Title Page

⇐⇒ n = 1 + k=2 if divisor2(k) Contents

where JJ II

 J I
 k+
Page 180 of 709
if divisor2(k) = (n div k) if k|n
0 otherwise Go Back

Full Screen

Are there any glitches? Is it techni- Close

cally correct and complete?


Quit
2.3. Variations: Algorithms & Code

IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 181 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

Recap S. Arun-Kumar, CSE

Title Page

• Combinations Contents

• Perfect Numbers JJ II

• Code Variations J I

• Algorithmic Variations Page 182 of 709

Go Back
forward
Full Screen

Close

Quit
Recap: Combinations IIT Delhi

nC = n! S. Arun-Kumar, CSE

k (n−k)!k! Title Page

Contents

n(n−1)···(n−k+1)
= k! JJ II

J I

n(n−1)···(k+1)
= (n−k)!
Page 183 of 709

Go Back

= n−1Ck−1 + n−1Ck Full Screen

Close

Quit
Combinations 1 IIT Delhi

use "fact.sml";
S. Arun-Kumar, CSE

exception invalid_arg;
Title Page

fun comb_wf (n, k) =


Contents

if n < 0 orelse
JJ II
k < 0 orelse
J I
k > n
Page 184 of 709
then raise invalid_arg
else fact (n) div Go Back

(fact(n-k) * fact(k)); Full Screen

Close

Quit
Combinations 2
exception invalid_arg;
fun comb (n, k) = IIT Delhi

if n < 0 orelse S. Arun-Kumar, CSE

k < 0 orelse Title Page

k > n Contents

then raise invalid_arg JJ II


else if n = 0 orelse J I
k = 0 orelse
Page 185 of 709

n = k
Go Back

then 1
Full Screen

else (* 0<k<n *)
Close
prod (n, n-k+1) div
fact (k) Quit
Combinations 3
exception invalid_arg;
fun comb (n, k) = IIT Delhi

if n < 0 orelse S. Arun-Kumar, CSE

k < 0 orelse Title Page

k > n Contents

then raise invalid_arg JJ II


else if n = 0 orelse J I
k = 0 orelse
Page 186 of 709

n = k
Go Back

then 1
Full Screen

else (* 0<k<n *)
Close
prod (n, k+1) div
fact (n-k) Quit
Perfect 2
perf ect(n) IIT Delhi

S. Arun-Kumar, CSE

Pb√nc Title Page

⇐⇒ n = 1 + k=2 if divisor2(k)
Contents

where JJ II

if divisor2(k) = J I

 k + m if k|n and k = 6 m Page 187 of 709

k if k|n and k = m Go Back

0 otherwise
 Full Screen

Close

where m = (n div k)
Quit
IIT Delhi

Power 2 S. Arun-Kumar, CSE

power Title Page

The previous inductive definition used Contents

xn = (x × x × · · · × x) ×x
JJ II

| {z } J I

n−1 times
Page 188 of 709

We could associate the product differ- Go Back

ently Full Screen

Close

Quit
A Faster Power
xn = |(x × x ×{z· · · × x)} IIT Delhi

n/2 times
S. Arun-Kumar, CSE

× (x
| ×x× {z· · · × x)} Title Page

n/2 times Contents

when n is even and JJ II

xn = |(x × x × {z· · · × x)}


J I

bn/2c times
Page 189 of 709

× |(x × x ×{z· · · × x)}


Go Back

Full Screen

bn/2c times
× x Close

Quit

when n is odd
Power2: Complete
IIT Delhi

S. Arun-Kumar, CSE

power2(x, n) = Title Page



 1.0/power2(x, n) if n < 0 Contents

 1.0 if n = 0 JJ II

(power2(x, bn/2c)) 2 if even(n) J I




(power2(x, bn/2c))2 × x otherwise
 Page 190 of 709

Go Back

where even(n) ⇐⇒ n mod 2 = 0. Full Screen

Close

Quit
IIT Delhi

Power2: SML S. Arun-Kumar, CSE

fun power2 (x, n) = Title Page

if n < 0 Contents

then 1.0/power2 (x, ˜n) JJ II

else if n = 0 J I

then 1.0 Page 191 of 709

else Go Back

Full Screen

Close

Quit
Power2: SML
let fun even m = IIT Delhi

(m mod 2 = 0);
S. Arun-Kumar, CSE

fun square y = y * y;
Title Page

val pwr_n_by_2 =
Contents

power2 (x, n div 2);


JJ II
val sq_pwr_n_by_2 =
J I
square (pwr_n_by_2)
Page 192 of 709
in if even (n)
then sq_pwr_n_by_2 Go Back

else x * sq_pwr_n_by_2 Full Screen

end Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Computation: Issues Title Page

1. Correctness Contents

JJ II
(a) General correctness
J I
(b) Technical Completeness
Page 193 of 709

(c) Termination
Go Back

Full Screen

Close

Quit
General Correctness IIT Delhi

1. Mathematical correctness should S. Arun-Kumar, CSE

be established for all algorithmic Title Page

variations. Contents

JJ II
2. Program Correctness: Mathemati-
J I
cally developed code should not be
moved around arbitrarily. Page 194 of 709

Go Back

• Code variations should also be


Full Screen

mathematically proven
Close

Quit
Code: Justification IIT Delhi

• How does one justify the correct- S. Arun-Kumar, CSE

ness of Title Page

– this version and Contents

JJ II
– this version?
J I

• Can one correct this version? Page 195 of 709

• But first of all, what is incorrect Go Back

about this version? Full Screen

incorrectness Close

Quit
Recall
• A program is an IIT Delhi

– explicit, S. Arun-Kumar, CSE

– unambiguous and Title Page

– technically complete Contents

JJ II
translation of an algorithm written in
J I
mathematical notation.
Page 196 of 709

• Moreover, mathematical notation is Go Back

more concise than a program. Full Screen

• Hence mathematical notation is Close

easier to analyse and diagnose. Quit


Features: Definition
before Use IIT Delhi

S. Arun-Kumar, CSE
incorrect version
Title Page

Definition of a name before use: Contents

• if divisor(k) is defined first. JJ II

• idivisor(k) uses the name n without J I

defining it. Page 197 of 709

• k has been defined (as an argu- Go Back

ment of if divisor(k)) before being Full Screen

used. Close

Quit
Run ifdivisor IIT Delhi

Standard ML of New Jersey,


S. Arun-Kumar, CSE

- fun ifdivisor(k) =
Title Page

= if n mod k = 0
Contents

= then k
JJ II
= else 0
J I
;
Page 198 of 709
stdIn:18.8 Error:
unbound variable Go Back

or constructor: n Full Screen

Close

Quit
Diagnosis: Features of IIT Delhi

programs S. Arun-Kumar, CSE

incorrect version Title Page

• So both sum divisors(l, u) and Contents

perf ect(n) may use if divisor(k). JJ II

J I
• sum divisors(l, u) is defined before
perf ect(n). Page 199 of 709

Go Back

• So perf ect(n) may use both


Full Screen

if divisor(k) and sum divisors(l, u)


Close

Quit
Back to Math
incorrect version

Let IIT Delhi


k if k|n S. Arun-Kumar, CSE

if divisor(k) =
0 otherwise Title Page

Contents

and sum divisors(l, u) = JJ II



0 if l > u J I

if divisor(l)+ otherwise Page 200 of 709

sum divisors(l + 1, u)

Go Back

and perf ect(n) ⇐⇒ Full Screen

Close

n = sum divisors(1, bn/2c) Quit


Incorrectness IIT Delhi

incorrect version S. Arun-Kumar, CSE

• if divisor(k) has a single argument


Title Page

k Contents

JJ II
• But it actually depends upon n too! J I

• But that is not made explicit in its Page 201 of 709

definition. Go Back

Let’s make it explicit! Full Screen

Close

Quit
ifdivisor3
Let IIT Delhi

k if k|n
if divisor3(n, k) = S. Arun-Kumar, CSE

0 otherwise Title Page

and sum divisors(l, u) = Contents

 JJ II
0 if l > u
J I
if divisor3(n, l)+ otherwise
Page 202 of 709
sum divisors(l + 1, u)

Go Back

and perf ect(n) ⇐⇒ Full Screen

n = sum divisors(1, bn/2c) Close

Quit
Run it! IIT Delhi

Standard ML of New Jersey S. Arun-Kumar, CSE

- fun ifdivisor3 (n, k) Title Page

= = if (n mod k = 0) Contents

= then k JJ II

= else 0; J I

val ifdivisor3 = Page 203 of 709

fn : int * int -> int Go Back

- Full Screen

Close

Quit
Try it!
IIT Delhi
- fun sum_divisors (l, u) =
= if l > u S. Arun-Kumar, CSE

= then 0 Title Page

= else ifdivisor3 (n, l)+ Contents

= sum_divisors (l+1, u); JJ II

stdIn:40.18 Error: unbound J I

variable or constructor: n Page 204 of 709

- Go Back

Full Screen

Now sum divisors also depends on Close

n! Quit
IIT Delhi

S. Arun-Kumar, CSE

Hey! Wait a minute! Title Page

But n was defined in ifdivisor3 (n, k)! Contents

JJ II

So then where is the problem? J I

Let’s ignore it for the moment and Page 205 of 709

come back to it later Go Back

Full Screen

Close

Quit
The n’s
Let IIT Delhi


k if k|n S. Arun-Kumar, CSE

if divisor3(n, k) =
0 otherwise Title Page

Contents

and sum divisors2(n, l, u) =


JJ II

0 if l > u J I

if divisor3(n, l)+ otherwise Page 206 of 709

sum divisors(l + 1, u)

Go Back

and perf ect(n) ⇐⇒ Full Screen

Close

n = sum divisors2(n, 1, bn/2c) Quit


Scope IIT Delhi

S. Arun-Kumar, CSE

• The scope of a name begins from Title Page

its definition and ends where the Contents

corresponding scope ends JJ II

• Scopes end with definitions of func- J I

tions Page 207 of 709

• Scopes end with the keyword end Go Back

in any let ... in ...end Full Screen

Close

Quit
Scope Rules IIT Delhi

S. Arun-Kumar, CSE

• Scopes may be disjoint Title Page

• Scopes may be nested one Contents

completely within another JJ II

• A scope cannot span two disjoint J I

scopes Page 208 of 709

• Two scopes cannot (partly) overlap Go Back

Full Screen
forward
Close

Quit
2.4. Names, Scopes & Recursion
1. Disjoint Scopes
IIT Delhi
2. Nested Scopes
3. Overlapping Scopes S. Arun-Kumar, CSE

4. Spannning
Title Page
5. Scope & Names
6. Names & References Contents

7. Names & References


JJ II
8. Names & References
9. Names & References
J I
10. Names & References
11. Names & References Page 209 of 709

12. Names & References


Go Back
13. Names & References
14. Names & References Full Screen

15. Names & References


Close
16. Definition of Names
17. Use of Names Quit
18. local...in...end

19. local...in...end

20. local...in...end

21. local...in...end IIT Delhi

22. Scope & local


S. Arun-Kumar, CSE
23. Computations: Simple

24. Simple computations Title Page

25. Computations: Composition


Contents
26. Composition: Alternative
27. Compositions: Compare JJ II
28. Compositions: Compare

29. Computations: Composition J I

30. Recursion
Page 210 of 709
31. Recursion: Left

32. Recursion: Right Go Back

Full Screen

Close

Quit
Disjoint Scopes
let
val x = 10; IIT Delhi

fun fun1 y = S. Arun-Kumar, CSE

let
... Title Page

in Contents

... JJ II
end
J I

fun fun2 z =
let Page 211 of 709

...
in
Go Back

in ... Full Screen

end
fun1 (fun2 x)
Close

end Quit
Nested Scopes
let
val x = 10; IIT Delhi

fun fun1 y = S. Arun-Kumar, CSE

Title Page

let Contents

val x = 15 JJ II

in J I

x + y Page 212 of 709

end Go Back

in Full Screen

fun1 x Close

end Quit
Overlapping Scopes
let
val x = 10; IIT Delhi

fun fun1 y = S. Arun-Kumar, CSE

... Title Page

Contents

... JJ II

J I

Page 213 of 709

... Go Back

in ... Full Screen

fun1 (fun2 x)
Close

end Quit
Spannning
let
val x = 10; IIT Delhi

fun fun1 y = S. Arun-Kumar, CSE

... Title Page

Contents

... JJ II

J I

fun fun2 z = Page 214 of 709

... Go Back

in ... Full Screen

fun1 (fun2 x)
Close

end Quit
Scope & Names IIT Delhi

• A name may occur either as being S. Arun-Kumar, CSE

defined or as a use of a previously Title Page

defined name Contents

• The same name may be used to re- JJ II

fer to different objects. J I

• The use of a name refers to the Page 215 of 709

textually most recent definition in Go Back

the innermost enclosing scope Full Screen

diagram Close

Quit
Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 216 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 217 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 218 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 219 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 220 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 221 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val z = 5;
IIT Delhi

fun fun1 y =
S. Arun-Kumar, CSE

let Title Page

val x = 15 Contents

JJ II

in
J I
x + y * z
end
Page 222 of 709

in Go Back

Full Screen

fun1 x
Close

end Quit

Back to scope names


Names & References
let
val x = 10; val x = x − 5;
IIT Delhi

fun fun1 y =
let S. Arun-Kumar, CSE

...
in Title Page

... Contents

end JJ II

fun fun2 z = J I

let
... Page 223 of 709

in
... Go Back

end Full Screen

in fun1 (fun2 x) Close

end Quit

Back to scope names


Names & References
let
val x = 10; val x = x − 5;
IIT Delhi

fun fun1 y =
let S. Arun-Kumar, CSE

...
in Title Page

... Contents

end JJ II

fun fun2 z = J I

let
... Page 224 of 709

in
... Go Back

end Full Screen

in fun1 (fun2 x) Close

end Quit

Back to scope names


Names & References
let
val x = 10; val x = x − 5;
IIT Delhi

fun fun1 y =
let S. Arun-Kumar, CSE

...
in Title Page

... Contents

end JJ II

fun fun2 z = J I

let
... Page 225 of 709

in
... Go Back

end Full Screen

in fun1 (fun2 x) Close

end Quit

Back to scope names


Definition of Names IIT Delhi

S. Arun-Kumar, CSE

Definitions are of the form Title Page

qualifier name . . . = body Contents

• val name = JJ II

• fun name ( argnames ) = J I

• local def initions Page 226 of 709

in def inition Go Back

end Full Screen

Close

Quit
Use of Names IIT Delhi

Names are used in expressions. S. Arun-Kumar, CSE

Expressions may occur Title Page

• by themselves – to be evaluated Contents

• as the body of a definition JJ II

• as the body of a let-expression J I

Page 227 of 709


let def initions
in expression Go Back

end Full Screen

Close
use of local
Quit
local...in...end
perfect
IIT Delhi

local
S. Arun-Kumar, CSE

exception invalidArg;
Title Page

Contents
fun ifdivisor3 (n, k) =
JJ II
if n <= 0 orelse
J I
k <= 0 orelse
n < k Page 228 of 709

then raise invalidArg Go Back

else if n mod k = 0 Full Screen

then k Close

else 0; Quit
local...in...end
perfect

fun sum_div2 (n, l, u) = IIT Delhi

if n <= 0 orelse S. Arun-Kumar, CSE

l <= 0 orelse Title Page

l > n orelse Contents

u <= 0 orelse JJ II

u > n J I

then raise invalidArg Page 229 of 709

else if l > u Go Back

then 0 Full Screen

else ifdivisor3 (n, l) Close

+ sum_div2 (n, l+1, u) Quit


local...in...end
perfect
IIT Delhi

in S. Arun-Kumar, CSE

fun perfect n =
Title Page
if n <= 0
Contents
then raise invalidArg
JJ II
else
J I
let
val nby2 = n div 2 Page 230 of 709

in Go Back

n = sum_div2 (n, 1, nby2) Full Screen

end Close

end Quit
local...in...end
perfect

Standard ML of New Jersey, IIT Delhi

- use "perfect2.sml"; S. Arun-Kumar, CSE

[opening perfect2.sml] Title Page

GC #0.0.0.0.1.10: (1 ms) Contents

val perfect = fn : int -> bool JJ II

val it = () : unit J I

- perfect 28; Page 231 of 709

val it = true : bool Go Back

- perfect 6; Full Screen

val it = true : bool


Close

- perfect 8128;
Quit

val it = true : bool


Scope & local
local
fun fun1 y = IIT Delhi

...
S. Arun-Kumar, CSE

Title Page

Contents

fun fun2 z = ... JJ II


fun1
J I

Page 232 of 709

in Go Back

fun fun3 x = ...


fun2 ... Full Screen

fun1 ... Close

end Quit
Computations: Simple IIT Delhi

For most simple expressions it is S. Arun-Kumar, CSE

• left to right, and Title Page

• top to bottom Contents

JJ II
except when
J I
• presence of brackets
Page 233 of 709

• precedence of operators Go Back

determine otherwise. Full Screen

Hence Close

Quit
IIT Delhi

Simple computations S. Arun-Kumar, CSE

Title Page

4 + 6 − (4 + 6) div 2 Contents

= 10 − (4 + 6) div 2 JJ II

= 10 − 10 div 2 J I

= 10 − 5 Page 234 of 709

= 5 Go Back

Full Screen

Close

Quit
Computations: Composition

f (x) = x2 + 1 IIT Delhi

S. Arun-Kumar, CSE

g(x) = 3 ∗ x + 2 Title Page

Then for any value a = 4 Contents

f (g(a)) JJ II

= f (3 ∗ 4 + 2) J I

= f (14) Page 235 of 709

= 142 + 1 Go Back

= 196 + 1 Full Screen

= 197 Close

Quit
Composition:
Alternative
f (x) = x2 + 1
IIT Delhi

S. Arun-Kumar, CSE

g(x) = 3 ∗ x + 2 Title Page

Why not Contents

JJ II
f (g(a))
g(4)2 + 1
J I
=
(3 ∗ 4 + 2)2 + 1
Page 236 of 709
=
(12 + 2)2 + 1
Go Back
=
142 + 1
Full Screen
=
Close
= 196 + 1
= 197 Quit
Compositions:
Compare
IIT Delhi

S. Arun-Kumar, CSE

Title Page

f (g(a)) f (g(a))
= f (3 ∗ 4 + 2) = g(4)2 + 1
Contents

= (3 ∗ 4 + 2)2 + 1
JJ II
= f (14)
= (12 + 2)2 + 1
J I
=
= 142 + 1 = 142 + 1
Page 237 of 709

Go Back
= 196 + 1 = 196 + 1
Full Screen
= 197 = 197
Close

Quit
IIT Delhi

Compositions: S. Arun-Kumar, CSE

Compare Title Page

Contents

Question 1: Which is more correct? JJ II

Why? J I

Question 2: Which is easier to imple- Page 238 of 709

ment?
Question 3: Which is more efficient? Go Back

Full Screen

Close

Quit
IIT Delhi

Computations: S. Arun-Kumar, CSE

Composition Title Page

A computation of f (g(a)) proceeds Contents

thus: JJ II

J I
• g(a) is evaluated to some value, say
Page 239 of 709

b
Go Back

• f (b) is next evaluated Full Screen

Close

Quit
IIT Delhi

Recursion S. Arun-Kumar, CSE

Title Page


1 if n = 0 Contents

f actL(n) =
f actL(n − 1) ∗ n otherwise JJ II

 J I
1 if n = 0
f actR(n) = Page 240 of 709

n ∗ f actR(n − 1) otherwise
Go Back

Full Screen

Close

Quit
Recursion: Left IIT Delhi

S. Arun-Kumar, CSE

f actL(4) Title Page

= (f actL(4 − 1) ∗ 4) Contents

= (f actL(3) ∗ 4) JJ II

= ((f actL(3 − 1) ∗ 3) ∗ 4) J I

= ((f actL(2) ∗ 3) ∗ 4) Page 241 of 709

= (((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Go Back

= ···
Full Screen

Close

Quit
Recursion: Right IIT Delhi

S. Arun-Kumar, CSE

f actR(4) Title Page

= (4 ∗ f actR(4 − 1)) Contents

= (4 ∗ f actR(3)) JJ II

= (4 ∗ (3 ∗ f actR(3 − 1))) J I

= (4 ∗ (3 ∗ f actR(2))) Page 242 of 709

= (4 ∗ (3 ∗ (2 ∗ f actR(2 − 1)))) Go Back

= ··· Full Screen

Close

Quit
3. Introducing Reals
3.1. Floating Point IIT Delhi

1. So Far-1: Computing
S. Arun-Kumar, CSE
2. So Far-2: Algorithms & Programs

3. So far-3: Top-down Design Title Page

4. So Far-4: Algorithms to Programs


Contents
5. So far-5: Caveats
6. So Far-6: Algorithmic Variations JJ II
7. So Far-7: Computations
J I
8. Floating Point

9. Real Operations Page 243 of 709

10. Real Arithmetic


Go Back
11. Numerical Methods

12. Errors
Full Screen
13. Errors

14. Infinite Series Close

15. Truncation Errors


Quit
16. Equation Solving

17. Root Finding-1

18. Root Finding-2

19. Root Finding-3 IIT Delhi

20. Root Finding-4


S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 244 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

So Far-1: Computing S. Arun-Kumar, CSE

• The general nature of computation Title Page

• The notion of primitives, composi- Contents

tion & induction JJ II

J I
• The notion of an algorithm
Page 245 of 709

• The digital computer & program- Go Back

ming language Full Screen

Close

Quit
So Far-2: Algorithms & IIT Delhi

Programs S. Arun-Kumar, CSE

• Algorithms: Finite mathematical Title Page

processes Contents

JJ II
• Programs: Precise, unambiguous
J I
explications of algorithms
Page 246 of 709

• Standard ML: Its primitives Go Back

• Writing technically complete speci- Full Screen

fications Close

Quit
So far-3: Top-down
Design
integer Square Root IIT Delhi

• Begin with the function you need to S. Arun-Kumar, CSE

design Title Page

Contents

• Write a
JJ II

– small compact technically com- J I

plete definition of the function Page 247 of 709

– perhaps using other functions Go Back

that have not yet been defined Full Screen

• Each function in turn is defined in a Close

top-down manner Quit

Perfect Numbers
So Far-4: Algorithms to IIT Delhi

Programs S. Arun-Kumar, CSE

• Perform top development till you re-


Title Page

quire only the available primitives Contents

JJ II
• Directly translate the algorithm into
J I
a Program
Page 248 of 709

• Use scope rules to localize or gen- Go Back

eralize Full Screen

SML code for perfect


Close

Quit
So far-5: Caveats
IIT Delhi

S. Arun-Kumar, CSE

• Don’t arbitrarily vary code from your


Title Page
algorithmic development
Contents

– It might work or JJ II

– It might not work J I

– unless properly justified Page 249 of 709

• May destroy technical complete- Go Back

ness Full Screen

• May create scope violations. Close

Quit
So Far-6: Algorithmic IIT Delhi

Variations S. Arun-Kumar, CSE

Title Page

Algorithmic Variations Contents

• Are safe if developed from first prin- JJ II

ciples. Thus ensuring their J I

– mathematical correctness Page 250 of 709

– technical completeness Go Back

– termination properties Full Screen

Close

Quit
So Far-7: IIT Delhi

Computations S. Arun-Kumar, CSE

Title Page

• Work within the notion of mathe-


Contents

matical equality
JJ II

– Simple expressions J I

– Composition of functions Page 251 of 709

– Recursive computations Go Back

• But are generally irreversible Full Screen

Close

Quit
Floating Point
• Each real number 3E11 is repre- IIT Delhi

sented by a pair of integers S. Arun-Kumar, CSE

1. Mantissa: 3 or 30 or 300 or . . . Title Page

2. Exponent: the power of 10 which Contents

the mantissa has to be multiplied JJ II

by J I

• What is displayed is not necessarily Page 252 of 709

the same as the internal represen- Go Back

tation. Full Screen

• There is no unique representation Close

of a real number Quit


Real Operations IIT Delhi

Depending upon the operations in- S. Arun-Kumar, CSE

volved Title Page

• Each real number is first converted Contents

into a suitable representation JJ II

J I
• The operation is performed
Page 253 of 709

• The result is converted into a suit- Go Back

able representation for display. Full Screen

skip to Numerical methods


Close

Quit
Real Arithmetic
• for addition and subtraction the two IIT Delhi

numbers should have the same ex- S. Arun-Kumar, CSE

ponent for ease of integer opera- Title Page

tions to be performed Contents

• the conversion may involve loss of JJ II

precision J I

• for multiplication and division the Page 254 of 709

exponents may have to be adjusted Go Back

so as not to cause an integer over- Full Screen

flow or underflow. Close

back Quit
Numerical Methods IIT Delhi

S. Arun-Kumar, CSE

• Finite (limited) precision


Title Page

• Accuracy depends upon available Contents

precision JJ II

• Whereas integer arithmetic is exact, J I

real arithmetic is not. Page 255 of 709

• Numerical solutions are a Go Back

finite approximation of the result Full Screen

Close

Quit
IIT Delhi

Errors S. Arun-Kumar, CSE

• Hence an estimate of the error is Title Page

necessary. Contents

• If a is the “correct” value and a∗ is JJ II

the computed value, J I

absolute error = a∗ − a
Page 256 of 709

a ∗−a Go Back

relative error = a
Full Screen

Close

Quit
Errors IIT Delhi

Errors in floating point computations S. Arun-Kumar, CSE

are mainly due Title Page

Contents

finite precision Round-off errors


JJ II

fnite process It is impossible to com- J I

pute the value of a (convergent) in- Page 257 of 709

finite series because computations Go Back

are themselves finite processes. In- Full Screen

finite series
Close

Quit
Infinite Series
cannot be computed to ∞
IIT Delhi

X xm
ex = S. Arun-Kumar, CSE

m! Title Page
m=0
Contents


X (−1)mx2m JJ II

cos x =
(2m)! J I

m=0 Page 258 of 709

∞ Go Back
X (−1)mx2m+1
sin x = Full Screen

(2m + 1)!
m=0 Close

Quit

Truncation
Truncation Errors
and hopefully it is good enough to re-
strict it to appropriate values of n IIT Delhi

n
X xm S. Arun-Kumar, CSE

ex =
m! Title Page

m=0 Contents

n JJ II
X (−1)mx2m
cos x = J I

(2m)!
m=0 Page 259 of 709

Go Back

n
X (−1)mx2m+1 Full Screen

sin x =
(2m + 1)! Close

m=0
Quit
IIT Delhi

S. Arun-Kumar, CSE

Equation Solving Title Page

• The fifth most basic operation Contents

• Root finding: A particular form of JJ II

equation solving J I

Page 260 of 709

f (x) = 0 Go Back

Full Screen

Close

Quit
Root Finding-1
f(b)
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents
x0
JJ II
a
J I
b
Page 261 of 709

Go Back

f(a) Full Screen

Close

Quit
Root Finding-2
f(b)
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents
x0
JJ II
a
J I
b
Page 262 of 709

Go Back

f(a) Full Screen

Close

Quit
Root Finding-3
f(b)
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents
x0
JJ II
a
J I
b
Page 263 of 709

Go Back

f(a) Full Screen

Close

Quit
Root Finding-4
Rather steep isn’t it?
IIT Delhi

f(b) S. Arun-Kumar, CSE

Title Page

Contents

x0 JJ II

a J I

b Page 264 of 709

Go Back

f(a)
Full Screen

Close

ε
Quit
3.2. Root Finding, Composition and Recursion
1. Root Finding: Newton’s Method
IIT Delhi
2. Root Finding: Newton’s Method
3. Root Finding: Newton’s Method S. Arun-Kumar, CSE

4. Root Finding: Newton’s Method


Title Page
5. Root Finding: Newton’s Method
6. Root Finding: Newton’s Method Contents

7. Newton’s Method: Basis


JJ II
8. Newton’s Method: Basis
9. Newton’ Method: Algorithm
J I
10. What can go wrong!-1
11. What can go wrong!-2 Page 265 of 709

12. What can go wrong!-2


Go Back
13. What can go wrong!-3
14. What can go wrong!-4 Full Screen

15. Real Computations & Induction: 1


Close
16. Real Computations & Induction: 2
17. What’s it good for? 1 Quit
18. What’s it good for? 2

19. newton: Computation

20. Generalized Composition

21. Two Computations of h(1) IIT Delhi

22. Two Computations of h(−1)


S. Arun-Kumar, CSE
23. Recursive Computations

24. Recursion: Left Title Page

25. Recursion: Right


Contents
26. Recursion: Nonlinear
27. Some Practical Questions JJ II
28. Some Practical Questions
J I

Page 266 of 709

Go Back

Full Screen

Close

Quit
Root Finding: Newton’s IIT Delhi

Method S. Arun-Kumar, CSE

Consider a function f (x) Title Page

Contents

• smooth and continuously differen-


JJ II
tiable over [a, b]
J I

• with a non-zero derivative f 0(x) ev- Page 267 of 709

erywhere in [a, b] Go Back

• the signs of f (a) and f (b) are differ- Full Screen

ent Close

Quit
Root Finding: Newton’s
Method IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 268 of 709

a Go Back

b
Full Screen

f(a)
Close

Quit
Root Finding: Newton’s
Method IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 269 of 709

a xi
Go Back

b
Full Screen

f(a)
Close

Quit
Root Finding: Newton’s
Method IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 270 of 709

xi+1 xi
a αi Go Back

b
Full Screen

f(a)
Close

Quit
Root Finding: Newton’s
Method IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 271 of 709

xi+1 xi
a αi Go Back

b
Full Screen

f(a)
Close

Quit
Root Finding: Newton’s
Method IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 272 of 709

xi+1 xi
a αi Go Back

xi+2 b
Full Screen

f(a)
Close

Quit
Newton’s Method:
Basis IIT Delhi

f (x )
= f 0(xi) = x −xi
S. Arun-Kumar, CSE

tan αi
i i+1 Title Page

whence Contents
f (x )
xi+1 = xi − f 0(xi ) JJ II
i
J I
Starting from an initial value x0 ∈ [a, b],
if the sequence f (xi) converges to 0 Page 273 of 709

i.e Go Back

Full Screen

f (x0), f (x1), f (x2), · · · → 0 Close

Quit
Newton’s Method:
Basis IIT Delhi

i.e. lim |f (xn)| = 0


S. Arun-Kumar, CSE

n→∞ Title Page

Contents

i.e.∀ε > 0 : ∃N ≥ 0 : ∀n > N : JJ II

J I

|f (xn)| < ε Page 274 of 709

then the sequence Go Back

Full Screen

x 0 , x1 , x2 , · · ·
Close

converges to a root of f . Quit


Newton’ Method:
Algorithm
Select a small enough ε > 0 and x0. IIT Delhi

Then S. Arun-Kumar, CSE

newton(f, f 0, a, b, ε, xi) = Title Page


if |f (xi)| < ε
Contents
xi
newton(f, f 0, a, b, ε, xi+1) otherwise JJ II

J I

where Page 275 of 709

x0 ∈ [a, b] Go Back

and Full Screen

f (xi) Close

xi+1 = xi − 0 ∈ [a, b]
f (xi) Quit
What can go wrong!-1
Oscillations! IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 276 of 709

xi+1 xi
a αi Go Back

xi+2 b
Full Screen
f(a)
Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

What can go wrong!-2 Title Page

An intermediate point may lie outside Contents

[a, b]! The function may not satisfy all JJ II

the assumptions outside [a, b]. There J I

are then no guarantees about the be- Page 277 of 709

haviour of the function. Go Back

Full Screen

Close

Quit
What can go wrong!-2
Interval bounds error! IIT Delhi

S. Arun-Kumar, CSE

f(b)
Title Page

Contents

JJ II

J I

Page 278 of 709

xi+1 xi
a αi Go Back

b
Full Screen
f(a) xi+2
Close

Quit
What can go wrong!-3
The function may be too steep
IIT Delhi

f(b) S. Arun-Kumar, CSE

Title Page

Contents

x0
JJ II
a
J I
b
Page 279 of 709

f(a)
Go Back

Full Screen

ε Close

for the available precision. Quit


What can go wrong!-4
Or too shallow! IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

x0 JJ II
f(b)
a J I

b
f(a) Page 280 of 709

Go Back

Full Screen

Close

Quit
Real Computations &
Induction: 1 IIT Delhi

Newton’s method (when it does work!) S. Arun-Kumar, CSE

computes a sequence Title Page

x 0 , x 1 , x 2 , . . . xn Contents

JJ II

of essentially discrete values such J I

that even if the sequence is not totally Page 281 of 709

ordered, there is some discrete con- Go Back

vergence measure viz.


Full Screen

|f (xi) − 0| Close

which is well-founded. Quit


Real Computations &
Induction: 2 IIT Delhi

S. Arun-Kumar, CSE

That is, for some decreasing se- Title Page

quence of integers ki ≥ 0, Contents

k0 > k 1 > k 2 > · · · > k n = 0 JJ II

J I

we have Page 282 of 709

kiε ≤ |f (xi) − 0| < (ki + 1)ε Go Back

Full Screen
and therefore inductive on integer
multiples of ε Close

Quit
What’s it good for? 1

n
Finding the positive n-th root c of a IIT Delhi

c > 0 and n > 1 amounts to solving S. Arun-Kumar, CSE

the equation Title Page

xn = c Contents

which is equivalent to finding the root JJ II

of f (x) with J I

f (x) = xn − c
Page 283 of 709

f 0(x) = nxn−1
Go Back

√ Full Screen

with [a, b] = [0, c] or [0, c] and an ap- Close

propriately chosen ε. Quit


What’s it good for? 2
Finding roots of polynomials.
IIT Delhi
n
ci xi
X S. Arun-Kumar, CSE
f (x) =
Title Page
i=0
Contents

n JJ II

icixi−1
X
f 0(x) = J I

i=1 Page 284 of 709

and Go Back

• an appropriately chosen ε, Full Screen

• an appropriately chosen [a, b] with Close

one of the limits possibly being c0. Quit


newton : Computation IIT Delhi

S. Arun-Kumar, CSE

newton(f, f 0, a, b, ε, x0) Title Page

newton(f, f 0, a, b, ε, x1) Contents

newton(f, f 0, a, b, ε, x2) JJ II

newton(f, f 0, a, b, ε, x3) J I
.. .. ..
Page 285 of 709

newton(f, f 0, a, b, ε, xn) Go Back

xn Full Screen

Close

Quit
Generalized
Composition IIT Delhi

S. Arun-Kumar, CSE
Computations
Title Page

h(x) = f (x, g(x)) Contents

where  JJ II

0 if x < 0
f (x, y) = J I

y otherwise Page 286 of 709

 Go Back

0 if x = 0
g(x) = Full Screen

g(x − 1) + 1 otherwise Close

Quit
Two Computations of IIT Delhi

S. Arun-Kumar, CSE
h(1)
Title Page

h(1) | h(1) Contents

f (1, g(1)) | f (1, g(1)) JJ II

g(1) | f (1, (g(0) + 1)) J I

g(0) + 1 | f (1, (0 + 1) Page 287 of 709

0+1 | f (1, 1) Go Back

1 | 1 Full Screen

Close

Quit
Two Computations of IIT Delhi

S. Arun-Kumar, CSE
h(−1)
Title Page

h(−1) | h(−1) Contents

f (−1, g(−1)) | f (−1, g(−1)) JJ II

0 | f (−1, (g(−2) + 1)) J I

DONE! | f (−1, ((g(−3) + 1) + 1 Page 288 of 709

| ... Go Back

| FOREVER! Full Screen

Close

Quit
Recursive IIT Delhi

Computations S. Arun-Kumar, CSE

Title Page

• Newton’s method Contents

JJ II
• Factorial
J I

– f actL Page 289 of 709

– f actR Go Back

skip to nonlinear recursion Full Screen


skip to Recursion Revisited
Close

Quit
Recursion: Left IIT Delhi

S. Arun-Kumar, CSE

f actL(4) Title Page

(f actL(4 − 1) ∗ 4) Contents

(f actL(3) ∗ 4) JJ II

((f actL(3 − 1) ∗ 3) ∗ 4) J I

((f actL(2) ∗ 3) ∗ 4) Page 290 of 709

(((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Go Back

···
Full Screen

Close

Quit
Recursion: Right IIT Delhi

S. Arun-Kumar, CSE

f actR(4) Title Page

(4 ∗ f actR(4 − 1)) Contents

(4 ∗ f actR(3)) JJ II

(4 ∗ (3 ∗ f actR(3 − 1))) J I

(4 ∗ (3 ∗ f actR(2))) Page 291 of 709

(4 ∗ (3 ∗ (2 ∗ f actR(2 − 1)))) Go Back

··· Full Screen

Close

Quit
Recursion: Nonlinear IIT Delhi

Fibonacci
S. Arun-Kumar, CSE

f ib(5) Title Page

f ib(4) + f ib(3) Contents

(f ib(3) + f ib(2)) + f ib(3) JJ II

((f ib(2) + f ib(1)) + f ib(2)) + f ib(3) J I

(((f ib(1) + f ib(0)) + f ib(1)) + f ib(2)) + f ib(3)


Page 292 of 709

(((1 + f ib(0)) + f ib(1)) + f ib(2)) + f ib(3) Go Back

··· Full Screen

contd ... Close

Quit
Some Practical
Questions
• What is the essential difference be- IIT Delhi

tween the computations of newton S. Arun-Kumar, CSE

and the two factorial programs? Title Page

Answer: Constant space vs. Linear Contents

space JJ II

• What is the essential similarity be- J I

tween the computations of f actL Page 293 of 709

and f actR? Answer Go Back

• Why can’t we calculate beyond Full Screen

f ib(43) using the definition Fi- Close

bonacci, on ccsun50 or a P-IV? Quit

Answer
Some Practical IIT Delhi

Questions S. Arun-Kumar, CSE

Title Page

• What does a computation of Fi- Contents

bonacci look like? JJ II

• What is the essential difference J I

between the computations of Fi- Page 294 of 709

bonacci and newton or f actL or Go Back

f actR? Full Screen

Close

Quit
4. Correctness, Termination & Complexity
4.1. Termination and Space Complexity
1. Recursion Revisited
IIT Delhi
2. Linear Recursion: Waxing

3. Recursion: Waning S. Arun-Kumar, CSE

4. Nonlinear Recursions
Title Page
5. Fibonacci: contd

6. Recursion: Waxing & Waning


Contents
7. Unfolding Recursion

8. Non-termination JJ II

9. Termination
J I
10. Proofs of termination

11. Proofs of termination: Induction Page 295 of 709

12. Proof of termination: Factorial


Go Back
13. Proof of termination: Factorial

14. Fibonacci: Termination Full Screen

15. GCD computations


Close
16. Well-foundedness: GCD

17. Well-foundedness
Quit
18. Induction is Well-founded
19. Induction is Well-founded
20. Where it doesn’t work
21. Well-foundedness is inductive IIT Delhi

22. Well-foundedness is inductive


S. Arun-Kumar, CSE
23. GCD: Well-foundedness
24. Newton: Well-foundedness
Title Page
25. Newton: Well-foundedness
26. Example: Zero Contents

27. Questions
JJ II
28. The Collatz Problem
29. Questions
J I
30. Space Complexity
31. Newton & Euclid: Absolute Page 296 of 709

32. Newton & Euclid: Relative


Go Back
33. Deriving space requirements
34. GCD: Space
Full Screen
35. Factorial: Space
36. Fibonacci: Space Close

37. Fibonacci: Space


Quit
IIT Delhi

S. Arun-Kumar, CSE

Recursion Revisited Title Page

• Linear recursions Contents

JJ II
– Waxing
J I
– Waning
Page 297 of 709

• Non-linear recursion Go Back

Full Screen

Close

Quit
Linear Recursion: IIT Delhi

Waxing S. Arun-Kumar, CSE

Title Page

f actL(4) Contents

(f actL(3) ∗ 4) JJ II

((f actL(2) ∗ 3) ∗ 4) J I

(((f actL(1) ∗ 2) ∗ 3) ∗ 4) Page 298 of 709

((((f actL(0) ∗ 1) ∗ 2) ∗ 3) ∗ 4) Go Back

contrast with newton Full Screen

Close

Quit
IIT Delhi

Recursion: Waning S. Arun-Kumar, CSE

Title Page

((((1 ∗ 1) ∗ 2) ∗ 3) ∗ 4) Contents

(((1 ∗ 2) ∗ 3) ∗ 4) JJ II
((2 ∗ 3) ∗ 4) J I
(6 ∗ 4)
Page 299 of 709

24
Go Back

contrast with newton


Full Screen

Close

Quit
IIT Delhi

Nonlinear Recursions S. Arun-Kumar, CSE

Title Page

Fibonacci
Contents

• Each computation of f ib has its own JJ II

waxing and waning J I

• There is still an “envelope” which Page 300 of 709

shows waxing and waning. Go Back

Full Screen

Close

Quit
IIT Delhi

Fibonacci: contd
S. Arun-Kumar, CSE

Title Page

(((1 + 1) + f ib(1)) + f ib(2)) + f ib(3) Contents

(2 + f ib(1)) + f ib(2)) + f ib(3) JJ II

((2 + 1) + f ib(2)) + f ib(3) J I

··· Page 301 of 709

Go Back

Full Screen

Close

Quit
Recursion: Waxing &
Waning
• Waning: Occurs when an expres- IIT Delhi

sion is simplified without requiring S. Arun-Kumar, CSE

replacement of names by defini- Title Page

tions. Contents

JJ II
• Waxing: Occurs when a name is
J I
replaced by its definition.
Page 302 of 709

– name by value replacements


Go Back

– occurs in generalized composi- Full Screen

tion but just once if it is not recur- Close

sively defined
Quit

– Unfolding recursion
IIT Delhi

S. Arun-Kumar, CSE

Unfolding Recursion Title Page

• may occur several times (terminat- Contents

ing), or JJ II

J I
• even an infinite number of times
Page 303 of 709
leading to nontermination
Go Back

Full Screen

Close

Quit
Non-termination IIT Delhi

Algorithm S. Arun-Kumar, CSE

• Simple expressions never lead to Title Page

nontermination Contents

JJ II
• (Generalized) composition never
leads to nontermination J I

Page 304 of 709

• Recursion may lead to non-


Go Back

termination or infinite computations,


Full Screen

unless proved otherwise


Close

Quit
Termination IIT Delhi

S. Arun-Kumar, CSE

Since recursion may lead to nontermi- Title Page

nation Contents

• Termination needs to be proved for JJ II

recursive definitions, and J I

• for expressions and definitions that Page 305 of 709

use recursively defined names as Go Back

components. Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Proofs of termination Title Page

A recursive definition guarantees ter- Contents

mination JJ II

J I
• if it is inductive, or
Page 306 of 709

• it is well-founded Go Back

Full Screen

Close

Quit
Proofs of termination:
Induction IIT Delhi

S. Arun-Kumar, CSE

A recursive definition guarantees ter-


Title Page

mination
Contents

• if it is inductive, JJ II

Examples: J I

– Factorial Page 307 of 709

– Fibonacci Go Back

• it is well-founded, though not obvi- Full Screen

ously inductive Close

Quit
Proof of termination:
Factorial IIT Delhi

S. Arun-Kumar, CSE

Factorial
Title Page

Consider f actL defined only for non- Contents

negative integers. We prove that it is JJ II

an algorithm i.e. that it terminates J I

Basis : For n = 0, f actL(n) = 1 which Page 308 of 709

is not a recursive definition. Hence Go Back

it does indeed terminate in a single Full Screen

step. Close

Quit
Proof of termination: IIT Delhi

Factorial S. Arun-Kumar, CSE

Induction hypothesis . For some n > Title Page

0, ∀k : 0 ≤ k ≤ n : f actL(k) termi- Contents

nates in ∝ k steps. JJ II

Induction step . Then f actL(n + 1) = J I

f actL(n) ∗ (n + 1) is guaranteed to Page 309 of 709

terminate in ∝ (n + 1) steps, since Go Back

f actL(n) does so in ∝ n steps. Full Screen

back Close

Quit
Fibonacci: Termination
Fibonacci

The proof is similar to that of f actL. IIT Delhi

S. Arun-Kumar, CSE

Basis For n = 0 or n = 1 f ib(n) = 1.


Title Page

Induction hypothesis For some n > 0, Contents

∀k : 0 ≤ k ≤ n : f ib(k) terminates in JJ II
∝ f (k) steps
J I
Induction Step Then since each of Page 310 of 709

f ib(n) and f ib(n − 1) is guaranteed


Go Back

to terminate in ∝ f (n) and ∝ f (n −


Full Screen

1) steps f ib(n+1) = f ib(n)+f ib(n−


Close

1) is also guaranteed to terminate in


f (n + 1) ∝ f (n) + f (n − 1) steps.
Quit
IIT Delhi

GCD computations S. Arun-Kumar, CSE

Euclidean GCD Title Page

gcd(12, 64) Contents

gcd(64, 12) JJ II

gcd(12, 4) J I

gcd(4, 0) Page 311 of 709

4 Go Back

Full Screen

Close

Quit
Well-foundedness:
GCD IIT Delhi

Euclidean GCD S. Arun-Kumar, CSE

For x, y > 0, 0 ≤ x mod y < y. Hence Title Page

the sequence of remainders obtained Contents

is JJ II

J I
• a sequence of non-negative integers,
and Page 312 of 709

Go Back

• is strictly decreasing
Full Screen

r1 > r2 > · · · > rn−1 > rn = 0 Close

Quit
Well-foundedness
A definition is well-founded if it is pos- IIT Delhi

sible to define a measure (i.e. a func- S. Arun-Kumar, CSE

tion w of its arguments) called the Title Page

well-founded function such that Contents

JJ II
1. the well-founded function takes only
J I
non-negative integer values
Page 313 of 709

2. with each successive recursive call Go Back

the value of the well-founded func- Full Screen

tion is guaranteed to decrease by at


Close

least 1.
Quit
Induction is IIT Delhi

Well-founded S. Arun-Kumar, CSE

The well-founded function usually is a Title Page

measure of the number of computa- Contents

tion steps that the algorithm will take JJ II

to terminate J I

• Factorial w(n) ∝ n Page 314 of 709

Go Back

• Fibonacci w(n) ∝ f (n)


Full Screen

Then Close

Quit
Induction is IIT Delhi

Well-founded S. Arun-Kumar, CSE

Title Page

• w(n) is always non-negative if f actL Contents

and f ib are defined only for non- JJ II

negative integers J I

• The argument to f actL and f ib in Page 315 of 709

each recursive unfolding is strictly Go Back

decreasing. Full Screen

Close

Quit
Where it doesn’t work IIT Delhi

Such proofs do not work for S. Arun-Kumar, CSE

• f act arbitrarily extended to include Title Page

negative integers. (since w(n) no Contents

longer strictly non-negative) JJ II

• f act(n) = f act(n+1) div (n+1), even J I

if n is non-negative (since w(n) is no Page 316 of 709

longer decreasing) Go Back

since the function is no longer well- Full Screen

founded. Close

Quit
IIT Delhi

Well-foundedness is S. Arun-Kumar, CSE

inductive Title Page

But the induction variable is Contents

• hidden or JJ II

J I
• too complex to worry about, or
Page 317 of 709

• it serves no useful purpose for the Go Back

algorithm, except as a counter. Full Screen

Close

Quit
Well-foundedness is
inductive IIT Delhi

Given any well-founded function w(~x) S. Arun-Kumar, CSE

whose values form a decreasing se- Title Page

quence in some algorithm Contents

y0 > y1 > · · · > yn−1 > yn ≥ 0 JJ II

J I

it is possible to put this sequence Page 318 of 709

in 1-1 correspondence with the set


Go Back

{0, . . . , n} via a function ind such that


Full Screen

ind(w(~x)) = n − i Close

Quit
IIT Delhi

GCD: S. Arun-Kumar, CSE

Well-foundedness Title Page

Contents
GCD
JJ II

Well-founded function for gcd J I

w(a, b) = b Page 319 of 709

Go Back

Full Screen

Close

Quit
Newton:
Well-foundedness IIT Delhi

Newton’s Method
S. Arun-Kumar, CSE

Convergence condition Title Page

f (x0), f (x1), f (x2), · · · → 0 Contents

JJ II
Compute the discrete value sequence J I

x 0 , x 1 , x 2 , . . . xn Page 320 of 709

Go Back
such that
Full Screen

k0 > k 1 > k 2 > · · · > k n = 0 Close

where Quit
Newton: IIT Delhi

Well-foundedness S. Arun-Kumar, CSE

Newton’s Method Title Page

Contents

kiε ≤ |f (xi) − 0| < (ki + 1)ε


JJ II

and therefore inductive on integer J I

multiples of ε Hence Page 321 of 709

|f (x)| Go Back

w(x) = b c
ε Full Screen

Close

Quit
Example: Zero IIT Delhi

A peculiar way to define the zero func- S. Arun-Kumar, CSE

tion Title Page

zero(x) = Contents

 JJ II
 zero(x + 1.0) if x ≤ −1.0
J I
0.0 if −1.0 < x < 1.0
Page 322 of 709

zero(x − 1.0) if x ≥ 1.0



Go Back

w(x) = d|x|e is the well-founded func- Full Screen

tion Close

Quit
Questions IIT Delhi

S. Arun-Kumar, CSE

Q: Is it always possible to find a well- Title Page

founded function for each algorithm? Contents

A: Unfortunately not! However if we JJ II

can’t then we cannot call it an algo- J I

rithm!. But if we can then we are guar- Page 323 of 709

anteed that the algorithm will termi-


nate. Go Back

The Collatz Problem Full Screen

Close

Quit
The Collatz Problem IIT Delhi

Does the following algorithm termi- S. Arun-Kumar, CSE

nate? Title Page

collatz(m) = Contents

 JJ II

1 if m ≤ 1 J I
collatz(m div 2) if m is even Page 324 of 709

collatz(3 ∗ m + 1) otherwise

Go Back

Unproven Claim. collatz(m) 1 for all Full Screen

m.
Close

Quit
Questions
Q: what other uses can well-founded IIT Delhi

functions be put to? S. Arun-Kumar, CSE

A: They can be used to estimate Title Page

the complexity of your algorithm in Contents

order of magnitude terms. JJ II

Space Complexity : The amount of J I

memory space required, as a func- Page 325 of 709

tion of the input Go Back

Time Complexity : The amount of time Full Screen

(number of computation steps) as a Close

function of the input Quit


IIT Delhi

Space Complexity S. Arun-Kumar, CSE

Title Page

What is the space complexity of


Contents

• Newton’s method JJ II

• Euclidean GCD J I

• Factorial Page 326 of 709

• Fibonacci Go Back

Full Screen

Close

Quit
Newton & Euclid:
Absolute
Newton’s Method IIT Delhi
Computation
S. Arun-Kumar, CSE

Newton’s method (wherever and Title Page

whenever it works well) requires Contents

space to compute JJ II

• the value of f at each point xi J I

• the value of f 0 at each point xi Page 327 of 709

• the value of xi+1 from the above Go Back

Their absolute space requirements Full Screen

could be different. But . . . Close

Euclidean GCD Quit

Computation
Newton & Euclid: IIT Delhi

Relative S. Arun-Kumar, CSE

Newton’s Method
Title Page
Computation

GCD and Newton’s method (wherever Contents

and whenever it works well) require JJ II

the same amount of space for each J I

recursive unfolding since each fresh Page 328 of 709

unfolding can reuse the space used


by the previous one. Go Back

Euclidean GCD Full Screen

Computation Close

Quit
Deriving space
IIT Delhi

S. Arun-Kumar, CSE

requirements Title Page

We may use the algorithm itself to de- Contents

rive the space required as follows: JJ II

J I
Assume that memory proportional to
calculating and outputting the answer Page 329 of 709

is a unit. Then space as a function of Go Back

the input is given by Full Screen

Close

Quit
GCD: Space IIT Delhi


1 if b = 0 S. Arun-Kumar, CSE

Sgcd(a,b) =
Sgcd(b,a mod b) otherwise Title Page

Contents

This implies (from well-foundedness)


JJ II
that the entire computation ends with
J I
the space of a unit.
Page 330 of 709

Sgcd(a,b) ∝ 1 Go Back

Full Screen

A similar analysis and result holds for


newton Close

Quit
Factorial: Space
f actL
 IIT Delhi

1 if n = 0
Sf actL(n) = S. Arun-Kumar, CSE

Sf actL(n−1)+1 otherwise Title Page

The 1 is for output and the +1 is be- Contents

cause one needs to store space pro- JJ II

portional to remembering “multiply by J I

n”. Page 331 of 709

Go Back

Sf actL(n) ∝ n. Full Screen

Close

A similar analysis and result holds for Quit


f actR.
IIT Delhi

S. Arun-Kumar, CSE

Fibonacci: Space Title Page

Fibonacci Contents

 JJ II

1 if n ≤ 1
Sf ib(n) = J I
Sf ib(n−1) + Sf ib(n−2) if n > 1 Page 332 of 709

Go Back

Full Screen

Close

Quit
Fibonacci: Space
Fibonacci

It is easy to see prove by induction IIT Delhi

that for n > 1, S. Arun-Kumar, CSE

Sf ib(n−1) < Sf ib(n) ≤ 2Sf ib(n−1) Title Page

Contents

That is, as the value of n increases JJ II

by 1 the space requirement approxi- J I

mately doubles. Further, it is easy to Page 333 of 709

show by induction that Go Back

2n−2 < Sf ib(n) ≤ 2n−1 Full Screen

Close

Quit
4.2. Efficiency Measures and Speed-ups
1. Recapitulation
IIT Delhi
2. Recapitulation
3. Time & Space Complexity S. Arun-Kumar, CSE

4. isqrt: Space
Title Page
5. Time Complexity
6. isqrt: Time Complexity Contents

7. isqrt2: Time
JJ II
8. shrink vs. shrink2: Times
9. Factorial: Time Complexity
J I
10. Fibonacci: Time Complexity
11. Comparative Complexity Page 334 of 709

12. Comparisons
Go Back
13. Comparisons
14. Efficiency Measures: Time Full Screen

15. Efficiency Measures: Space


Close
16. Speeding Up: 1
17. Speeding Up: 2 Quit
18. Factoring out calculations

19. Tail Recursion: 1

20. Tail Recursion: 2

21. Factorial: Tail Recursion IIT Delhi

22. Factorial: Tail Recursion


S. Arun-Kumar, CSE
23. A Computation

24. Factorial: Issues Title Page

25. Fibonacci: Tail Recursion


Contents
26. Fibonacci: Tail Recursion
27. fibTR: SML JJ II
28. State in Tail Recursion

29. Invariance J I

Page 335 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Recapitulation Title Page

• Recursion & nontermination Contents

• Termination & well-foundedness JJ II

J I
• Well-foundedness proofs
Page 336 of 709

• Well-foundedness & Complexity Go Back

Full Screen

Close

Quit
Recapitulation
• Recursion & nontermination IIT Delhi

S. Arun-Kumar, CSE

• Termination & well-foundedness


Title Page

• Well-foundedness proofs Contents

– By induction JJ II

– well-founded functions J I

– By well-founded functions Page 337 of 709

– induction as well-foundedness Go Back

– Well-foundedness as induction Full Screen

Close

• Well-foundedness & Complexity


Quit
Time & Space
Complexity IIT Delhi
Questions
S. Arun-Kumar, CSE

An order of magnitude estimate of Title Page

the time or space (memory) required Contents

(in terms of some large computation JJ II


steps).
J I

• Newton & Euclid’s GCD Page 338 of 709

• Deriving space requirements Go Back

– Integer Sqrt Full Screen

– Factorial Close

Quit

– Fibonacci
isqrt : Space
Integer Sqrt shrink IIT Delhi

Sisqrt(n) = Sshrink(n,0,n) for large n. S. Arun-Kumar, CSE

Sshrink(n,l,u) = Title Page

 Contents

1
 if l = u JJ II

Sshrink(n,l+1,u) if l < u . . . J I

shrink(n,l,u−1) if l < u . . .
S

Page 339 of 709

Assuming 1 unit of space for output. Go Back

By induction on |[l, u]| Full Screen

Close

Sisqrt(n) = Sshrink(n,0,n) ∝ 1
Quit
Time Complexity IIT Delhi

S. Arun-Kumar, CSE

As in the case of space we may use Title Page

the algorithm itself to derive the time Contents

complexity. JJ II

• Integer sqrt J I

• Factorial Page 340 of 709

• Fibonacci Go Back

Full Screen
forward
Close

Quit
isqrt: Time Complexity
Integer Sqrt
shrink IIT Delhi

S. Arun-Kumar, CSE
Assume condition-checking (along
with +1 or −1) takes a unit of time. Title Page

Then Tshrink(n,l,u) = Contents

JJ II

0
 if l = u J I

1 + Tshrink(n,l+1,u) if l < u . . . Page 341 of 709

shrink(n,l,u−1) if l < u . . .
1+T
 Go Back

Full Screen

Then Tshrink(n,l,u) ∝ |[l, u]| − 1 and


Close

Tisqrt(n) = Tshrink(n,0,n) ∝ n Quit


isqrt2: Time
shrink

Assume condition-checking (along


with (l + u) div 2) takes a unit of time. IIT Delhi

Then Tshrink2(n,l,u) = S. Arun-Kumar, CSE

Title Page

0
 if u ≤ l ≤ u Contents

1 + Tshrink2(n,m,u) if m2 ≤ n . . . JJ II

1+T

if m 2>n
shrink2(n,l,u−1) J I

Page 342 of 709

If 2k−1 ≤ |[l, u]| − 1 < 2k then the al- Go Back

gorithm terminates in at most k steps. Full Screen

Since k = dlog2 |[l, u]| − 1e, Close

Tshrink2(n,l,u) ∝ dlog2 |[l, u]| − 1e Quit


shrink vs. shrink2 : Times
shrink shrink2
IIT Delhi

1. The time units are different,


S. Arun-Kumar, CSE

2. But they differ by a constant factor Title Page

at most. Contents

3. So clearly, for large n, shrink2 is JJ II

faster than shrink J I

4. But for small n, it depends on the Page 343 of 709

constant factor. Go Back

5. Implicitly assume that the actual Full Screen

unit of time includes the time re- Close

quired to unfold the recursion. Quit


Factorial: Time
Complexity IIT Delhi

S. Arun-Kumar, CSE

f actL
Title Page

Here we assume multiplication takes Contents

unit time. JJ II

0 if n = 0 J I
Tf actL(n) =
Tf actL(n−1)+1 otherwise Page 344 of 709

Go Back
Then
Full Screen

Tf actL(n) ∝ n Close

Quit
Fibonacci: Time
Complexity IIT Delhi

Fibonacci S. Arun-Kumar, CSE

Assuming addition and condition-


Title Page

checking together take a unit of time, Contents

we have JJ II

 J I
0 if n ≤ 1
Tf ib(n) = Page 345 of 709

Tf ib(n−1) + Tf ib(n−2) if n > 1


Go Back

It follows that Full Screen

2n−2 < Tf ib(n) ≤ 2n−1 Close

Quit
IIT Delhi

Comparative S. Arun-Kumar, CSE

Complexity Title Page

Contents

Algorithm Space Time


JJ II
isqrt(n) O(1) O(n)
J I
isqrt2(n) O(1) O(log2 n)
Page 346 of 709
f actL(n) O(n) O(n)
f ib(n) O(2n) O(2n) Go Back

Full Screen

Close

Quit
Comparisons IIT Delhi

For smaller values S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 347 of 709

Go Back

Full Screen

Close

Quit
Comparisons
IIT Delhi

For large values S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 348 of 709

Go Back

Full Screen

Close

Quit
Efficiency Measures: IIT Delhi

Time S. Arun-Kumar, CSE

Title Page

An algorithm for a problem is Contents

asymptotically faster or asymptotically


JJ II
more time-efficient than another for
J I
the same problem if its time complex-
Page 349 of 709

ity is bounded by a function that has


Go Back
a slower growth rate as a function of
the value of its arguments. Full Screen

Close

Quit
IIT Delhi

Efficiency Measures: S. Arun-Kumar, CSE

Space Title Page

Contents

Similarly an algorithm is JJ II
asymptotically more space efficient J I
than another if its space complexity
is bounded by a function that has a Page 350 of 709

slower growth rate. Go Back

Full Screen

Close

Quit
IIT Delhi

Speeding Up: 1 S. Arun-Kumar, CSE

Title Page
Q: Can fibonacci be speeded up or
made more space efficient? Contents

JJ II
A: Perhaps by studying the nature of J I
the function e.g. isqrt2 vs. isqrt
Page 351 of 709

and attempting more efficient algo-


Go Back

rithmic variations.
Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Speeding Up: 2 Title Page

Q: Are there general methods of


Contents

JJ II
speeding up or saving space?
J I
A: Take inspiration from gcd, newton,
Page 352 of 709

shrink
Go Back

Full Screen

Close

Quit
Factoring out
calculations IIT Delhi

S. Arun-Kumar, CSE

gcd(a0, b0) Title Page

compute a1, b1 Contents

gcd(a1, b1) JJ II

compute a2, b2 J I

gcd(a2, b2) Page 353 of 709

··· Go Back

gcd(an, bn) Full Screen

an Close

Quit
IIT Delhi

Tail Recursion: 1 S. Arun-Kumar, CSE

• Factor out calculations and remem-


Title Page

ber only those values that are re- Contents

quired for the next recursive call. JJ II

J I
• Create a vector of state variables
Page 354 of 709

and include them as arguments of


Go Back

the function
Full Screen

Close

Quit
Tail Recursion: 2 IIT Delhi

S. Arun-Kumar, CSE

• Try to reorder the computation us- Title Page

ing the state variables so as to get Contents

the next state completely defined. JJ II

• Redefine the function entirely in J I

terms of the state variables so that Page 355 of 709

the recursive call is the outermost Go Back

operation. Full Screen

Close

Quit
Factorial: Tail
Recursion
f actL Waxing f actL Waning IIT Delhi

• The recursive call precedes the S. Arun-Kumar, CSE

multiplication operation. Change it! Title Page

• Define a state variable p which con- Contents

tains the product of all the values JJ II

that one must remember J I

Page 356 of 709


• Reorder the computation so that the
computation of p is performed be-
Go Back

fore the recursive call. Full Screen

Close

• For that redefine the function in


Quit

terms of p.
Factorial: Tail
Recursion IIT Delhi

Factorial S. Arun-Kumar, CSE


⊥ if n < 0 Title Page

f actL2(n) = 1 if n = 0 Contents

f actL tr(n, 1) otherwise JJ II



J I

where Page 357 of 709

f actL tr(n, p) =
Go Back

p if n = 0 Full Screen

f actL tr(n − 1, np) otherwise Close

Quit
A Computation IIT Delhi

S. Arun-Kumar, CSE

f actL2(4)
Title Page

f actL tr(4, 1)
Contents

f actL tr(3, 4)
JJ II
f actL tr(2, 12)
J I
f actL tr(1, 24)
f actL tr(0, 24) Page 358 of 709

24 Go Back

Full Screen

Reminiscent of gcd and newton! Close

Quit
Factorial: Issues
• Correctness: Prove (by induction
on n) that for all n ≥ 0, f actL2(n) = IIT Delhi

n!. S. Arun-Kumar, CSE

• termination: Prove by induction on Title Page

n that every computation of f actL2 Contents

terminates. JJ II

J I
• Space complexity: Prove that
Sf actL2(n) = O(1) (as against Page 359 of 709

Sf actL(n) ∝ n). Go Back

Full Screen

• Time complexity: Prove that Close

Tf actL2(n) = O(n)
Quit

Complexity table
Fibonacci: Tail
Recursion IIT Delhi

S. Arun-Kumar, CSE

• Remove duplicate computations by Title Page

defining appropriate state variables Contents

• Let a and b be the consecutive fi- JJ II

bonacci numbers f ib(m − 2) and J I

f ib(m − 1) required for the compu- Page 360 of 709

tation of f ib(m). Go Back

• The state consists of the variables Full Screen

n, a, b, m. Close

Quit
Fibonacci: Tail
Recursion IIT Delhi

f ibT R(n) = S. Arun-Kumar, CSE

 Title Page

⊥ if n < 0
if 0 ≤ n ≤ 1
Contents
1
JJ II
f ib iter(n, 1, 1, 1) otherwise

J I

where Page 361 of 709

f ib iter(n, a, b, m) = Go Back

b if m ≥ n Full Screen

f ib iter(n, b, a + b, m + 1) otherwise Close

Quit
fibTR: SML
local IIT Delhi

fun fib_iter (n, a, b, m) = S. Arun-Kumar, CSE

(* fib (m) = b ,fib (m-1) = a *) Title Page

if m >= n then b Contents

else fib_iter (n, b, a+b, m+1);JJ II

in J I

fun fibTR (n) = Page 362 of 709

if n < 0 then raise negativeArgumen Go Back

else if (n <= 1) then 1 Full Screen

else fib_iter (n, 1, 1, 1) Close

end; Quit
State in Tail Recursion
IIT Delhi

S. Arun-Kumar, CSE

• The variables that make up the Title Page

state bear a definite relation to each Contents

other. JJ II

• INVARIANCE. That relationship be- J I

tween the state variables does not Page 363 of 709

change throughout the computation Go Back

of the function. Full Screen

Close

Quit
Invariance IIT Delhi

• The invariant property of a tail- S. Arun-Kumar, CSE

recursive function must hold Title Page

Initially when it is first invoked, and Contents

Continues to hold before every suc- JJ II

cessive invocation J I

• The invariant property character- Page 364 of 709

izes the entire computation and the Go Back

algorithm and is crucial to the proof Full Screen

of correctness Close

Quit
4.3. Invariance & Correctness
1. Recap
IIT Delhi
2. Recursion Transformation
3. Tail Recursion: Examples S. Arun-Kumar, CSE

4. Comparisons
Title Page
5. Transformation Issues
6. Correctness Issues 1 Contents

7. Correctness Issues 2
JJ II
8. Correctness Theorem
9. Invariants & Correctness 1
J I
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr Page 365 of 709

12. Invariance: Example


Go Back
13. Invariance: Example
14. Proof Full Screen

15. Invariance Lemma: f ib iter


Close
16. Proof
17. Correctness: Fibonacci Quit
18. Variants & Invariants

19. Variants & Invariants

20. More Invariants

21. Fast Powering 1 IIT Delhi

22. Fast Powering 2


S. Arun-Kumar, CSE
23. Root Finding: Bisection

24. Advantage Bisection Title Page

Contents

JJ II

J I

Page 366 of 709

Go Back

Full Screen

Close

Quit
Recap IIT Delhi

• Asymptotic Complexity: S. Arun-Kumar, CSE

Title Page

– Space
Contents

– Time JJ II

• Comparative Complexity J I

• Comparisons: Page 367 of 709

– Small inputs Go Back

– Large inputs Full Screen

Close

Quit
Recursion
Transformation IIT Delhi

S. Arun-Kumar, CSE

• To achieve constant space and lin- Title Page

ear time, if possible Contents

• Speeding up using tail recursion JJ II

– Factor out calculations J I

– Reorder the computations with Page 368 of 709

state variables Go Back

– Recursion as the outermost oper- Full Screen

ation Close

Quit
IIT Delhi

Tail Recursion: S. Arun-Kumar, CSE

Examples Title Page

Contents

• Factorial vs. Factorial: JJ II

f actL vs. f actL2 vs. J I

• Fibonacci vs. Fibonacci: Page 369 of 709

f ib vs. f ibT R Go Back

Full Screen

Close

Quit
Comparisons
IIT Delhi

S. Arun-Kumar, CSE

Algorithm Space Time Title Page

isqrt(n) O(1) O(n) Contents

isqrt2(n) O(1) O(log2 n) JJ II

f actL(n) O(n) O(n) J I

f actL2(n) O(1) O(n) Page 370 of 709

f ib(n) O(2n) O(2n) Go Back

f ibT R(n) O(1) O(n) Full Screen

Close

Quit
Transformation Issues IIT Delhi

S. Arun-Kumar, CSE

• Correctness: Prove that the new al- Title Page

gorithm computes the same func-


Contents

tion as the original simple algorithm


JJ II

• Termination: Prove by induction on J I

n that every computation is finite. Page 371 of 709

• Space complexity: Compute it. Go Back

• Time complexity: Compute it. Full Screen

Close

Quit
Correctness Issues 1 IIT Delhi

S. Arun-Kumar, CSE

• Absolute correctness: For any func- Title Page

tion f , that an algorithm A that Contents

claims to implement it, prove that JJ II


f (~x) = A(~x) J I

for all argument values ~x for which Page 372 of 709

f is defined. Go Back

• Transformation correctness: Full Screen

Close

Quit
Correctness Issues 2 IIT Delhi

• Absolute correctness: S. Arun-Kumar, CSE

• Transformation correctness: For Title Page

any algorithm A and a transformed Contents

algorithm B prove that JJ II

A(~x) = B(~x) J I

for all argument values ~x for which Page 373 of 709

A is defined. Then B is absolutely Go Back

correct provided A is absolutely cor- Full Screen

rect. Close

Quit
Correctness Theorem IIT Delhi

S. Arun-Kumar, CSE

Invariant properties f actL2


Title Page

Theorem 8 For all n ≥ 0, Contents

f actL2(n) = n!
JJ II

Proof: For n = 0, it is clear that J I


f actL2(0) = 1 = 0!. For n > 0,
f actL2(n) = f actL tr(n, 1). The proof Page 374 of 709

is done provided we can show that Go Back

f actL tr(n, 1) = n!.  Full Screen

Close

Quit
Invariants & IIT Delhi

Correctness 1 S. Arun-Kumar, CSE

Title Page
Invariant properties f actL2
Contents

• To prove the absolute or trans-


JJ II
formation correctness of a tail-
J I
recursion transformation usually re-
quires an invariant property to be
Page 375 of 709

proven about the tail-recursive func- Go Back

tion. Full Screen

Close

Quit
Invariants &
Correctness 2 IIT Delhi

S. Arun-Kumar, CSE

Invariant properties f actL2


Title Page

• This allows the independent proof Contents

of the properties of the tail- JJ II

recursive function without reference J I

to the function that uses it. Page 376 of 709

• It reflects the design of the al- Go Back

gorithm and its division into sub- Full Screen

problems. Close

Quit
IIT Delhi

Invariance Lemma:
f actL tr
S. Arun-Kumar, CSE

Title Page

Invariant properties f actL2 Contents

Lemma 9 For all n ≥ 0 and p JJ II

f actL tr(n, p) = (n!)p J I

Proof: By induction on n.  Page 377 of 709

Back to theorem Go Back

Full Screen

Close

Quit
Invariance: Example IIT Delhi

f actL2
S. Arun-Kumar, CSE

f actL tr(4, 7) Title Page

f actL tr(3, 28) Contents

f actL tr(2, 84) JJ II

f actL tr(1, 168) J I

f actL tr(0, 168) Page 378 of 709

168 Go Back

Contrast with a f actL2(4) computa- Full Screen

tion Close

Quit
Invariance: Example IIT Delhi

f actL2 S. Arun-Kumar, CSE

So what exactly is invariant? Title Page

f actL tr(4, 7) 168 = 4! × 7 Contents

f actL tr(3, 28) 168 = 3! × 28 JJ II

f actL tr(2, 84) 168 = 2! × 84 J I

f actL tr(1, 168) 168 = 1! × 168 Page 379 of 709

f actL tr(0, 168) 168 = 0! × 168 Go Back

168 Full Screen

Close

Quit
Proof
Basis For n = 0, f actL tr(0, p) = p = IIT Delhi

(0!)p.
S. Arun-Kumar, CSE

Induction hypothesis (IH) For all k, Title Page

0 < k ≤ n, Contents

f actL tr(k, p) = p = (k!)p JJ II

Induction Step J I

f actL tr(n + 1, p) Page 380 of 709

= f actL tr(n, (n + 1)p) Go Back

= (n!)(n + 1)p (IH) Full Screen

= (n + 1)!p Close

Quit
Back to lemma
Invariance Lemma:
f ib iter
IIT Delhi

S. Arun-Kumar, CSE

f ib iter F Title Page

Lemma 10 For all n > 1, a, b, m : 1 ≤ Contents

m ≤ n, if a = F(m − 1) and b = F(m), JJ II

then J I

INV :f ib iter(n, a, b, m) = F(n) Page 381 of 709

. Go Back

Proof: By induction on k = n − m  Full Screen

Close

Quit
Proof
Basis For k = 0, n = m, it follows that
f ib iter(n, a, b, m) = F(n) IIT Delhi

S. Arun-Kumar, CSE

Induction hypothesis (IH) For all n >


1 and 1 ≤ m ≤ n, with n − m ≤ k,
Title Page

INV holds Contents

JJ II
Induction Step Let 1 ≤ m < n such
that n − m = k + 1, F(m) = b and J I

F(m − 1) = a. Then F(m + 1) = a + b Page 382 of 709

and Go Back

Full Screen

f ib iter(n, a, b, m)
Close
= f ib iter(n, b, a + b, m + 1)
= F(n) (IH) Quit
Correctness: Fibonacci IIT Delhi

S. Arun-Kumar, CSE
f ibT R Fibonacci
Title Page

Theorem 11 For all n ≥ 0, Contents

f ibT R(n) = F(n). JJ II

Proof: For 0 ≤ n ≤ 1, it holds J I


trivially. For n > 1, f ibT R(n) =
Page 383 of 709
f ib iter(n, 1, 1, 1) = F(n), by the invari-
ance lemma, with m = 1, a = 1 = Go Back

F(m − 1) and b = 1 = F(m).  Full Screen

Close

Quit
Variants & Invariants IIT Delhi

S. Arun-Kumar, CSE

f actL2
Title Page

f actL3(n) = Contents


⊥ if n < 0 JJ II

1 if n = 0 J I

f actL tr2(n, 1, 1) else


 Page 384 of 709

Go Back
where
Full Screen

Close

Quit
Variants & Invariants IIT Delhi

S. Arun-Kumar, CSE

f actL2
Title Page

f actL tr2(n, p, m) = Contents


p if n = m JJ II

f actL tr2(n, (m + 1)p, m + 1) else J I

Page 385 of 709

f actL tr2(n, p, m) = (m!)p Go Back

for all 1 ≤ m ≤ n. Full Screen

Close

Quit
More Invariants IIT Delhi

• shrink For all n > 0, l, u, if S. Arun-Kumar, CSE

[l, u] ⊆ [0, n], Title Page

Contents


l ≤ b nc ≤ u JJ II

J I
• shrink2
Page 386 of 709

For all n > 0, l, u, if [l, u] ⊆ [0, n],


Go Back


m = b(l + u)/2c and l ≤ b nc ≤ u Full Screen

Close

Quit
IIT Delhi

Fast Powering 1 S. Arun-Kumar, CSE

power2 Title Page

power3(x, n) = Contents

JJ II

 1.0/power3(x, −n) if n < 0
J I
1.0 if n = 0
Page 387 of 709
powerT R(x, n, 1) else

Go Back

where Full Screen

Close

Quit
Fast Powering 2 IIT Delhi

S. Arun-Kumar, CSE
power2
Title Page

powerT R(x, n, p) =
Contents

p if n = 0 JJ II

powerT R(x2, n div 2, p) if even(n) J I

powerT R(x2, n div 2, xp) otherwise



Page 388 of 709

where even(n) ⇐⇒ n mod 2 = 0. Go Back

powerT R(x, n, p) = xnp Full Screen

Close

Quit
Root Finding: Bisection IIT Delhi

Newton’s Method Algorithm S. Arun-Kumar, CSE

Select a small enough ε > 0 and Title Page

x0. Then if sgn(f (a)) 6= sgn(f (b)), Contents

bisect(f, a, b, ε) = JJ II


c if |f (c)| < ε J I

bisect(f, c, b, ε) if sgn(f (c)) 6= sgn(f (b)) Page 389 of 709

bisect(f, a, c, ε) otherwise
 Go Back

Full Screen

where c = (a + b)/2 Close

Quit
Advantage Bisection
More robust than Newton’s method IIT Delhi

• Requires continuity and change of S. Arun-Kumar, CSE

sign Title Page

• Does not require differentiability Contents

JJ II
• Could change the condition suitably J I
to take care of very shallow curves
Page 390 of 709

• Oscillations could occur only if the Go Back

function is too steep. Full Screen

• An intermediate point can never go Close

outside the interval. Quit


5. Compound Data
5.1. Tuples, Lists & the Generation of Primes
1. Recap: Tail Recursion
IIT Delhi
2. Examples: Invariants

3. Tuples S. Arun-Kumar, CSE

4. Lists
Title Page
5. New Lists

6. List Operations
Contents
7. List Operations: cons

8. Generating Primes upto n JJ II

9. More Properties
J I
10. Composites

11. Odd Primes Page 391 of 709

12. primesU pto(n)


Go Back
13. generateF rom(P, m, n, k)

14. generateF rom Full Screen

15. primeW RT (m, P )


Close
16. primeW RT (m, P )

17. primeW RT
Quit
18. Density of Primes

19. The Prime Number Theorem

20. The Prime Number Theorem

21. Complexity IIT Delhi

22. Diagnosis
S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 392 of 709

Go Back

Full Screen

Close

Quit
Recap: Tail Recursion IIT Delhi

S. Arun-Kumar, CSE

• Asymptotic Complexity:
Title Page

Time Linear Contents

Space Constant JJ II

• Correctness: Capture the algorithm J I

through Page 393 of 709

Invariant Invariance Lemma Go Back

Bound function Proof by induction Full Screen

Close

Quit
Examples: Invariants IIT Delhi

f actL tr2 S. Arun-Kumar, CSE

shrink & shrink2 Title Page

Contents


l ≤ b nc ≤ u JJ II

√ J I
m = b(l + u)/2c and l ≤ b nc ≤ u
Page 394 of 709

Fast Powering Go Back

Full Screen

powerT R(x, n, p) = xnp Close

Quit
Tuples: Formation
Simplest form of compound data:
Cartesian products. IIT Delhi

S. Arun-Kumar, CSE

• Each element of a cartesian prod-


Title Page

uct is a tuple
Contents

• Tuples may be constructed as we JJ II

do in mathematics, simply by en- J I


closing the elements (separated by Page 395 of 709

commas) in a pair of parentheses.


Go Back

- val a = (2, 3.0, false); Full Screen

val a = (2,3.0,false) : Close

int * real * bool Quit


Tuples: Decomposition IIT Delhi

• Individual components of a tuple S. Arun-Kumar, CSE

may be taken out too. Title Page

- #1 a; Contents

val it = 2 : int JJ II

- #2 (a); J I

val it = 3.0 : real Page 396 of 709

- #3 a; Go Back

val it = false : bool Full Screen

Close

Quit
Tuples: divmod
Standard ML of New Jersey, ... IIT Delhi

- fun divmod (a, b) = S. Arun-Kumar, CSE

(a div b, a mod b); Title Page

val divmod = Contents

fn : int * int -> int * int


JJ II
- val dm = divmod (24,9);
J I
val dm = (2,6) : int * int
Page 397 of 709

- #1 dm;
Go Back
val it = 2 : int
Full Screen
- #2 dm;
Close
val it = 6 : int
Quit
Constructors &
Destructors
Every way of constructing compound IIT Delhi

data from simpler data elements has S. Arun-Kumar, CSE

Constructors : Operators which con- Title Page

struct compound data from simpler Contents

ones (for tuples it is simply (, , and JJ II

)). J I

Destructors : Operators which allow Page 398 of 709

us to extract the individual compo- Go Back

nents of a compound data item (for Full Screen

tuples they are #1, #2 ... depend- Close

ing upon how many components it Quit

consists of).
Tuples: Identity IIT Delhi

Every tuple that has been broken up S. Arun-Kumar, CSE

into its components using its destruc- Title Page

tors can be put together back again Contents

using its constructors. JJ II

Given a tuple a ∈ A1 × A2 × . . . × An, J I

we have Page 399 of 709

Go Back

a = (#1 a, #2 a, . . . , #n a) Full Screen

Close

Quit
Lists IIT Delhi

An α list represents a sequence of el- S. Arun-Kumar, CSE

ements of a given type α. Title Page

Given a (nonempty) list Contents

• A list is ordered JJ II

• There may be more than one occur- J I

rence of an element in the list Page 400 of 709

• only the first element (called the Go Back

head) of the list is immediately ac- Full Screen

cessible. Close

Quit
New Lists
Given a (nonempty) list L, IIT Delhi

• A new list M may be created from S. Arun-Kumar, CSE

an existing list L by the tl operation. Title Page

• New elements can be added (by the Contents

operation cons) to an existing list, JJ II

one at a time to create new lists. J I

• the last element that was added be- Page 401 of 709

comes the head of the new list. Go Back

• Two lists are equal only if they have Full Screen

the same elements in the same or- Close

der Quit
List Operations
• The empty list: nil or [] IIT Delhi

S. Arun-Kumar, CSE

• Nonempty lists: Given a nonempty


Title Page

list L
Contents
L = [1, 2, 3, 4]
JJ II

head : hd : αList → α J I

Page 402 of 709


hd(L) = 1
Go Back

tail : tl : αList → αList


Full Screen

tl(L) = [2, 3, 4] Close

Quit
List Operations: cons
IIT Delhi

S. Arun-Kumar, CSE

• L = [1, 2, 3, 4] Title Page

cons : cons : α × αList → αList Contents

JJ II

cons(0, nil) = [0] J I

cons(0, L) = 0 :: L = [0, 1, 2, 3, 4] Page 403 of 709

1 :: (0 :: L) = [1, 0, 1, 2, 3, 4] Go Back

back to lists Recap Full Screen

Close

Quit
Polynomial Evaluation
Evaluating a polynomial
IIT Delhi

n
aixi
X S. Arun-Kumar, CSE

p(x) =
Title Page

i=0
Contents

given JJ II

• its coefficients as a list [an, . . . , a0] J I

of values from the highest degree Page 404 of 709

term to the constant a0. Go Back

• a value for the variable x. Full Screen

Assume an empty list of coefficients Close

yields a value 0. Quit


Naive Solution IIT Delhi

poly0(L, x) = S. Arun-Kumar, CSE

 Title Page

0 if L = nil
hxn + poly0(T, x) if L = h :: T
Contents

JJ II

where n = |L| − 1. J I

fun poly0 ([], x) = 0.0 Page 405 of 709

| poly0 ((h::T), x) = Go Back

h*power(x, n)+poly0(T, x) Full Screen

Close

Quit
Complexity of poly0 IIT Delhi

Space. O(n) to store both the list and S. Arun-Kumar, CSE

the intermediate computations. Title Page

Additions. O(n) additions. Contents

JJ II
Multiplications. n(n − 1)/2 by the sim-
J I
plest powering algorithm.
Page 406 of 709

Multiplications. O(log2(n) + O(log2(n − Go Back

1) + · · · + O(log2(1)) ≤ O(n log2(n)) Full Screen

by the fast powering algorithm. Close

Quit
Arden’s Rule
Factor out the multiplications to get IIT Delhi

S. Arun-Kumar, CSE

p(x) = (...((anx+an−1)x+an−2)x+...)x+a0
Title Page

and define a tail-recursive function Contents

which requires only n multiplications. JJ II

poly1(L, x) = poly T R(0, L, x) J I

where Page 407 of 709

poly T R(p, L, x) = Go Back


p if L = nil Full Screen

poly T R(px + h, T, x) if L = h :: T Close

Quit
poly1 in SML
local
fun poly_TR (p, [], x) = p IIT Delhi

| poly_TR (p, (h::T), x) = S. Arun-Kumar, CSE

poly_TR (p*x + h, T, x) Title Page

in Contents

fun poly L x = JJ II

poly_TR (0.0, L, x) J I

end; Page 408 of 709

Go Back

Question 1. What is the right theorem Full Screen

to prove that poly T R is the right gen- Close

eralization for the problem?


Quit
Ans.
poly1 in SML
local IIT Delhi

fun poly_TR (p, [], x) = p S. Arun-Kumar, CSE

| poly_TR (p, (h::T), x) = Title Page

poly_TR (p*x + h, T, x) Contents

in JJ II

fun poly L x = J I

poly_TR (0.0, L, x) Page 409 of 709

end; Go Back

Full Screen
n
X
Ans. poly T R(p, L, x) = pxn+1 + aixi Close

i=0
Quit
Reverse Input
IIT Delhi

S. Arun-Kumar, CSE

Supposing the coefficients were given Title Page

in reverse order [a0, . . . , an]. Revers- Contents

ing this list will be an extra O(n) time JJ II

and space. Though the asymptotic J I

complexity does not change much, it Page 410 of 709

is more interesting to work directly Go Back

with the given list. Full Screen

Close

Quit
IIT Delhi

revpoly0 S. Arun-Kumar, CSE

revpoly0(L, x) = Title Page


0 if L = nil Contents

h + x × revpoly0(T, x) if L = h :: T JJ II

J I
fun revpoly0 ([], x) = 0.0
Page 411 of 709

| revpoly0 ((h::T), x) =
Go Back
h + x * revpoly0 (T, x)
Full Screen

Close

Quit
IIT Delhi

Tail Recursive S. Arun-Kumar, CSE

Title Page

revpoly1(L, x) = revpoly1 T R(L, x, 1, x)


Contents
where
JJ II
revpoly1 T R(L, x, p, s) =
 J I

s if L = nil Page 412 of 709

revpoly1(T, x, px, s + ph) if L = h :: T Go Back

Full Screen

Close

Quit
Tail Recursion: SML IIT Delhi

local S. Arun-Kumar, CSE

fun revpoly1_TR([],x,p,s)=s Title Page

| revpoly1_TR((h::T),x,p,s)= Contents

revpoly1_TR(T,x,p*x,s+p*h) JJ II

in J I

fun revpoly1 (L, x) = Page 413 of 709

revpoly1_TR (L, x, 1.0, 0.0) Go Back

end Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Complexity of revpoly1 Title Page

Contents

Space. O(n) space to store the list JJ II

Multiplications. 2n multiplications J I

Additions. n additions. Page 414 of 709

Go Back

Full Screen

Close

Quit
Generating Primes
upto n IIT Delhi

Definition 12 A positive integer n > 1 S. Arun-Kumar, CSE

is composite iff it has a proper divisor Title Page

d|n with 1 < d < n. Otherwise it is Contents

prime. JJ II

• 2 is the smallest (first) prime. J I

Page 415 of 709

• 2 is the only even prime.


Go Back

• No other even number can be a Full Screen

prime. Close

• All other primes are odd Quit


More Properties
IIT Delhi

• An odd number cannot have any S. Arun-Kumar, CSE

even divisors.
Title Page

• Every number may be expressed Contents

uniquely (upto order) as a product JJ II

of prime factors. J I

• No divisor of a positive integer can Page 416 of 709

be greater than itself. Go Back

• For √ d|n such that d ≤


√ each divisor
Full Screen

b nc, n/d ≥ b nc is also a divisor. Close

Quit
Composites IIT Delhi

S. Arun-Kumar, CSE

• If a number n is composite, then it Title Page

√ a proper divisor d, 2 ≤ d ≤
has Contents

b nc. JJ II

• If a number n is composite, then√ it J I

has a prime divisor p, 2 ≤ p ≤ b nc. Page 417 of 709

• An odd composite number n has √ an


Go Back

odd prime divisor p, 3 ≤ p ≤ b nc. Full Screen

Close

Quit
Odd Primes IIT Delhi

• An odd number > 1 is a prime iff it S. Arun-Kumar, CSE

has no proper odd divisors Title Page

• An odd number > 1 is a prime iff


Contents

JJ II
it is not divisible by any odd prime
smaller than itself. J I

Page 418 of 709

• An odd number n > 1 is a prime iff


Go Back

it is√not divisible by any odd prime


Full Screen

≤ b nc.
Close

Quit
primesU pto(n) IIT Delhi

S. Arun-Kumar, CSE

primesU pto(n) =
Title Page


 [] if n < 2 Contents

 [(1, 2)] if n = 2

 JJ II

primesU pto(n − 1) elseif even(n) J I

generateF rom otherwise





 Page 419 of 709

([(1, 2)], 3, n, 2)

Go Back

where Full Screen

Close

Quit
generateF rom(P, m, n, k) IIT Delhi

bound function n − m S. Arun-Kumar, CSE

Invariant Title Page

2 < m ≤ n ∧ odd(m) Contents

JJ II
implies
J I
P = [(k − 1, pk−1), · · · , (1, p1)]
Page 420 of 709

and Go Back

∀q : pk−1 < q < m : composite(q) Full Screen

Close

Quit
generateF rom
IIT Delhi
generateF rom(P, m, n, k) =
S. Arun-Kumar, CSE


 P if m > n Title Page



 Contents

 generateF rom elseif




JJ II

(((k, m) :: P ), m + 2, n, k + 1) pwrt J I



 Page 421 of 709

generateF rom else





 Go Back

(P, m + 2, n, k)

Full Screen

where pwrt = primeW RT (m, P ) Close

Quit
primeW RT (m, P )
Definition 13 A number m is prime IIT Delhi

with respect to a list L of numbers iff it S. Arun-Kumar, CSE

is not divisible by any of them. Title Page

• A number is prime iff it is prime Contents

with respect to the list of all primes JJ II

smaller than itself. J I

• From properties of odd primes it fol-


Page 422 of 709

lows that a number n is prime iff it Go Back

is prime with
√ respect to the list of all
Full Screen

primes ≤ n Close

Quit
primeW RT (m, P ) IIT Delhi

bound function length(P ) S. Arun-Kumar, CSE

Invariant If P = [(i − 1, pi−1), . . . (1, p1)], Title Page

for some i ≥ 1 then Contents

• pk ≥ m > pk−1, and JJ II

J I
• m is prime with respect to
[(k − 1, pk−1), . . . , (i, pi)]
Page 423 of 709

Go Back

• m is a prime iff it is a prime with


Full Screen

respect to P
Close

Quit
primeW RT IIT Delhi

primeW RT (m, P ) = S. Arun-Kumar, CSE



 true if P = nil Title Page

f alse elseif h|m


 Contents


 primeW RT else JJ II

(m, tl(P ))
 J I

Page 424 of 709

where
Go Back

(i, h) = hd(P )
Full Screen

for some i > 0


Close

Quit
Density of Primes
Let π(n) denote the number of primes
IIT Delhi

upto n. Then S. Arun-Kumar, CSE

Title Page

n π(n) %
Contents

100 25 25.00%
JJ II
1000 168 16.80%
J I
10000 1229 12.29%
100000 9592 9.59% Page 425 of 709

1000000 78, 498 7.85% Go Back

10000000 664579 6.65% Full Screen

100000000 5761455 5.76% Close

Quit
The Prime Number
Theorem IIT Delhi

S. Arun-Kumar, CSE

π(n) Title Page

limn→∞ n/ ln n = 1
Contents

Proved by Gauss. JJ II

• Shows that the primes get sparser J I

at higher n Page 426 of 709

• A larger percentage of numbers as Go Back

we go higher are composite. Full Screen

from David Burton: Elementary Number Close

Theory. Quit
The Prime Number
Theorem
IIT Delhi

π(n)
n π(n) % lim S. Arun-Kumar, CSE

n→∞ n/ ln n Title Page

100 25 25.00% Contents

1000 168 16.80% 1.159 JJ II

10000 1229 12.29% 1.132 J I

100000 9592 9.59% 1.104 Page 427 of 709

1000000 78, 498 7.85% 1.084 Go Back

10000000 664579 6.65% 1.071 Full Screen

100000000 5761455 5.76% 1.061


Close

from David Burton: Elementary Number Quit

Theory.
IIT Delhi

S. Arun-Kumar, CSE

Complexity Title Page

function calls Contents

primesU pto 1 JJ II

generateF rom n/2 J I


P n
primeW RT m=3,odd(m) π(m)
Page 428 of 709

Go Back

Full Screen

Close

Quit
Diagnosis
For each m ≤ n,
• P is in descending order of the IIT Delhi

primes S. Arun-Kumar, CSE

Title Page

• m is checked for divisibility π(m)


Contents

times
JJ II

• From properties of odd primes it J I

should not be necessary


√ to check Page 429 of 709

each m more than π(b mc) times Go Back

for divisibility.
Full Screen

• Organize P in ascending order in- Close

stead of descending. Quit

ascending-order
5.2. Compound Data & Lists
1. Compound Data
IIT Delhi
2. Recap: Tuples
3. Tuple: Formation S. Arun-Kumar, CSE

4. Tuples: Selection
Title Page
5. Tuples: Equality
6. Tuples: Equality errors Contents

7. Lists: Recap
JJ II
8. Lists: Append
9. cons vs. @
J I
10. Lists of Functions
11. Lists of Functions Page 430 of 709

12. Arithmetic Sequences


Go Back
13. Tail Recursion
14. Tail Recursion Invariant Full Screen

15. Tail Recursion


Close
16. Another Tail Recursion: AS3
17. Another Tail Recursion: AS3 iter Quit
18. AS3: Complexity

19. Generating Primes: 2

20. primes2U pto(n)

21. generate2F rom(P, m, n, k) IIT Delhi

22. generate2F rom


S. Arun-Kumar, CSE
23. prime2W RT (m, P )

24. prime2W RT Title Page

25. primes2: Complexity


Contents
26. primes2: Diagnosis

JJ II

J I

Page 431 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Compound Data Title Page

• Forming (compound) data struc- Contents

tures from simpler ones JJ II

J I
• Breaking up compound data into its
Page 432 of 709

components.
Go Back

Full Screen

Close

Quit
Recap: Tuples IIT Delhi

S. Arun-Kumar, CSE

formation : Cartesian products of Title Page

types Contents

selection : Selection of individual com- JJ II

ponents J I

equality : Equality checking Page 433 of 709

equality errors : Equality errors Go Back

Full Screen
forward to Lists
Close

Quit
Tuple: Formation IIT Delhi

Standard ML of New Jersey, S. Arun-Kumar, CSE

- val a = ("arun", 1<2, 2); Title Page

val a = ("arun",true,2) Contents

: string * bool * int JJ II

- val b = ("arun", true, 2); J I

val b = ("arun",true,2) Page 434 of 709

: string * bool * int Go Back

Full Screen

back
Close

Quit
Tuples: Selection
- #2 a;
val it = true : bool IIT Delhi

- #1 a; S. Arun-Kumar, CSE

val it = "arun" : string Title Page

- #3 a; Contents

val it = 2 : int JJ II

- #4 a; J I

stdIn:1.1-1.5 Error: operator and ope


Page 435 of 709

operator domain: {4:’Y; ’Z} Go Back

operand: string * bool * in


Full Screen

in expression: Close

(fn {4=4,...} => 4) a Quit

back
Tuples: Equality IIT Delhi

- a = b; S. Arun-Kumar, CSE

val it = true : bool Title Page

- (1<2, true) = (1.0 < 2.0, true); Contents

val it = true : bool JJ II

- (true, 1.0 < 2.4) J I

= (1.0 < 2.4, true); Page 436 of 709

val it = true : bool Go Back

Full Screen

back Close

Quit
Tuples: Equality errors
- ("arun", (1, true)) IIT Delhi

= ("arun", 1, true); S. Arun-Kumar, CSE

stdIn:1.1-29.39 Error: operator andTitle Page


o
operator domain: (string * (int * Contents
b
operand: (string * (int * b
JJ II
in expression:
J I
("arun",(1,true))
Page 437 of 709

= ("arun",1,true)
Go Back
- ("arun", (1, true))
Full Screen
= (("arun", 1), true);
Close
stdIn:1.1-29.39 Error: operator and o
Quit
IIT Delhi

S. Arun-Kumar, CSE

Lists: Recap Title Page

formation : Sequence α List Contents

JJ II
selection : Selection of individual com-
J I
ponents
Page 438 of 709

new lists : Making new lists from old


Go Back

Full Screen

Close

Quit
Lists: Append
- op @;
IIT Delhi

val it = fn : ’a list * ’a list


S. Arun-Kumar, CSE

-> ’a list
Title Page

- [1,2,3] @ [˜1, ˜3];


Contents
val it = [1,2,3,˜1,˜3]
JJ II
: int list
J I
- [[1,2,3], [˜1, ˜2]]
@ [[1,2,3], [˜1, ˜2]]; Page 439 of 709

val it = Go Back

[[1,2,3], [˜1,˜2], Full Screen

[1,2,3], [˜1,˜2]] Close

: int list list Quit


cons vs. @ IIT Delhi

S. Arun-Kumar, CSE

cons is a constant time = O(1) oper-


Title Page
ation. But @ is linear = O(n) in the
length n of the first list. @ is defined
Contents

JJ II
as
L@M = J I

 Page 440 of 709

M if L = nil
Go Back
h :: (T @M ) if L = h :: T
Full Screen

Close

Quit
IIT Delhi

Lists of Functions S. Arun-Kumar, CSE

- fun add1 x = x+1; Title Page

val add1 = fn : int -> int Contents

- fun add2 x = x + 2; JJ II

val add2 = fn : int -> int J I

- fun add3 x = x + 3; Page 441 of 709

val add3 = fn : int -> int Go Back

Full Screen

Close

Quit
Lists of Functions IIT Delhi

- val addthree S. Arun-Kumar, CSE

= [add1, add2, add3]; Title Page

val addthree Contents

= [fn,fn,fn] : (int -> int) list JJ II

- fun addall x = [(add1 x), (add2 x),


J I

val addall = fn : int -> int list


Page 442 of 709

- addall 3; Go Back

val it = [4,5,6] : int list Full Screen

Close

Quit
Arithmetic Sequences
AS1(a, d, n) = IIT Delhi

 S. Arun-Kumar, CSE


 [] if n ≤ 0 Title Page

Contents


 AS1(a, d, n − 1) else JJ II
@[a + (n − 1) ∗ d]

J I

function calls Order Page 443 of 709

AS1 n O(n) Go Back

@ n O(n) Full Screen

Pn 2)
:: i=0 i O(n Close

Quit
Tail Recursion IIT Delhi

AS2(a, d, n) = S. Arun-Kumar, CSE

 Title Page

 [] if n ≤ 0
Contents

JJ II
AS2 iter(a, d, n − 1, 0, []) else

J I

where Page 444 of 709

for any initial L0 and n ≥ k ≥ 0 Go Back

Full Screen

IN V 2 : L = L0@[a]@ . . . @[a + (k − 1) ∗ d]
Close

Quit
Tail Recursion:
Invariant IIT Delhi

S. Arun-Kumar, CSE

IN V 2 : L = L0@[a]@ . . . @[a + (k − 1) ∗ d]
Title Page

and bound function Contents

n−k JJ II

AS2 iter(a, d, n, k, L) = J I


 L if k ≥ n Page 445 of 709

 Go Back


 AS2 iter(a, d, n, k + 1 else Full Screen

L@[a + k ∗ d])
 Close

Quit
Tail Recursion: IIT Delhi

Complexity S. Arun-Kumar, CSE

Title Page

function calls Order Contents

AS2 1 JJ II

AS2 iter n O(n) J I

@ n O(n) Page 446 of 709


Pn 2)
:: i=0 i O(n Go Back

So tail recursion simply doesn’t help! Full Screen

Close

Quit
Another Tail Recursion
: AS3 IIT Delhi

AS3(a, d, n) = S. Arun-Kumar, CSE


 [] if n ≤ 0 Title Page

Contents

AS3 iter(a, d, n − 1, []) else


 JJ II

J I
where
Page 447 of 709

for any initial L0, n0 ≥ n > 0, and


Go Back

IN V 3 : L = (a + (n − 1) ∗ d) :: · · · :: Full Screen

Close

· · · :: (a + (n0 − 1) ∗ d) :: L0
Quit
Another Tail Recursion:
AS3 iter IIT Delhi

IN V 3 : L = (a + (n − 1) ∗ d) :: · · · S. Arun-Kumar, CSE

· · · :: (a + (n0 − 1) ∗ d) :: L0 Title Page

and bound function n, Contents

AS3 iter(a, d, n, L) = JJ II

 J I


 L if n ≤ 0 Page 448 of 709

Go Back


 AS3 iter(a, d, n − 1, else
Full Screen

(a + (n − 1) ∗ d)::L)

Close

Quit
IIT Delhi

AS3: Complexity S. Arun-Kumar, CSE

Title Page

function calls Order Contents

AS3 1 JJ II

AS3 iter n O(n) J I

@ 0 Page 449 of 709


P n
:: i=0 1 O(n) Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Generating Primes: 2 Title Page

composites Contents

• primesU pto JJ II

J I
• invariant
Page 450 of 709

• generateF rom Go Back

Full Screen

Close

Quit
primes2U pto(n) IIT Delhi

S. Arun-Kumar, CSE

primes2U pto(n) =
Title Page


 [] if n < 2 Contents

 [(1, 2)] if n = 2

 JJ II

primes2U pto(n − 1) elseif even(n) J I

generate2F rom otherwise





 Page 451 of 709

([(1, 2)], 3, n, 2)

Go Back

where Full Screen

Close

Quit
generate2F rom(P, m, n, k) IIT Delhi

bound function n − m S. Arun-Kumar, CSE

Invariant Title Page

2 < m ≤ n ∧ odd(m) Contents

JJ II
implies
J I
P = [(1, p1), · · · , (k − 1, pk−1)]
Page 452 of 709

and Go Back

∀q : pk−1 < q < m : composite(q) Full Screen

Close

Quit
generate2F rom
IIT Delhi
generate2F rom(P, m, n, k) =
S. Arun-Kumar, CSE


 P if m > n Title Page



 Contents

 generate2F rom elseif




JJ II

(P @[(k, m)], m + 2, n, k + 1) pwrt J I





 Page 453 of 709

generate2F rom else





 Go Back

(P, m + 2, n, k)

Full Screen

where pwrt = prime2W RT (m, P ) Close

Quit
prime2W RT (m, P )
IIT Delhi
bound function length(P )
S. Arun-Kumar, CSE

Invariant If P = [(i, pi), . . . (k − 1, pk−1)], Title Page

for some i ≥ 1 then Contents

• pk ≥ m > pk−1, and JJ II

• m is prime with respect to J I

[(1, p1), . . . , (i − 1, pi−1)] Page 454 of 709

• m is a prime iff it is a prime Go Back

with respect √ to [(1, p1), . . . , (j, pj )], Full Screen

where pj ≤ b mc < pj+1 Close

Quit
prime2W RT
IIT Delhi

prime2W RT (m, P ) =
S. Arun-Kumar, CSE


 true if P = nil Title Page

 true if h > m div h



 Contents

f alse elseif h|m JJ II

primeW RT else



 J I

(m, tl(P ))

Page 455 of 709

where Go Back

(i, h) = hd(P ) Full Screen

for some i > 0 Close

Quit
IIT Delhi

: Complexity
S. Arun-Kumar, CSE

primes2 Title Page

Contents

function calls JJ II
primes2U pto 1 J I
generate2F rom n/2 √
P n Page 456 of 709

prime2W RT m=3,odd(m) π(b mc) Go Back

Full Screen

Close

Quit
primes2 : Diagnosis
generate2F rom IIT Delhi

• Uses @ to create an ascending se- S. Arun-Kumar, CSE

quence of primes Title Page

• For each new prime pk this opera- Contents

tion takes time O(k). JJ II

J I
• Can tail recursion be used to re-
Page 457 of 709

duce the complexity due to @?


Go Back

• Can a more efficient algorithm us- Full Screen

ing :: instead of @ be devised (as in Close

the case of AS3)? Quit


5.3. Compound Data & List Algorithms
1. Compound Data: Summary
IIT Delhi
2. Records: Constructors
3. Records: Example 1 S. Arun-Kumar, CSE

4. Records: Example 2
Title Page
5. Records: Destructors
6. Records: Equality Contents

7. Tuples & Records


JJ II
8. Back to Lists
9. Lists: Correctness
J I
10. Lists: Case Analysis
11. Lists: Correctness by Cases Page 458 of 709

12. List-functions: length


Go Back
13. List Functions: search
14. List Functions: search2 Full Screen

15. List Functions: ordered


Close
16. List Functions:insert
17. List Functions: reverse Quit
18. List Functions: reverse2

19. List Functions:merge

20. List Functions:merge

21. List Functions:merge contd. IIT Delhi

22. ML: merge


S. Arun-Kumar, CSE
23. Sorting by Insertion

24. Sorting by Merging Title Page

25. Sorting by Merging


Contents
26. Functions as Data
27. Higher Order Functions JJ II

J I

Page 459 of 709

Go Back

Full Screen

Close

Quit
Compound Data:
Summary IIT Delhi

• Compound Data: S. Arun-Kumar, CSE

Title Page

Tuples: Cartesian products of differ-


Contents

ent types (ordered)


JJ II
Lists: Sequences of the same type J I
of element
Page 460 of 709

Records: Unordered named aggre- Go Back

gations of elements of different Full Screen

types.
Close

• Constructors & Destructors Quit


Records: Constructors
• A record is a set of values drawn IIT Delhi

from various types such that each S. Arun-Kumar, CSE

component (called a field) has a Title Page

unique name. Contents

• Each record has a type defined by JJ II

J I
field names
Page 461 of 709

types of fieldnames
Go Back

The order of presentation of the Full Screen

record fields does not affect its Close

type in any way.


Quit
Records: Example 1
Standard ML of New Jersey, IIT Delhi

- val pinky = S. Arun-Kumar, CSE

{ name = "Pinky", age = 3, Title Page

fav_colour = "pink"}; Contents

- val pinky = {age=3,


JJ II
fav_colour="pink",
J I
name="Pinky"}
Page 462 of 709

: {age:int,
Go Back
fav_colour:string,
Full Screen
name:string
Close
}
Quit
Records: Example 2
- val billu = IIT Delhi

{ age = 1, S. Arun-Kumar, CSE

name = "Billu", Title Page

fav_colour = "blue" Contents

}; JJ II

- val billu = J I

{age=1,fav_colour="blue",name="Billu"
Page 463 of 709

{age:int, fav_colour:string, name:str Go Back

- pinky = billu; Full Screen

val it = false : bool Close

Quit
IIT Delhi

Records: Destructors S. Arun-Kumar, CSE

#age billu; Title Page

val it = 1 : int Contents

- #fav_colour billu; JJ II

val it = "blue" : string J I

- #name billu; Page 464 of 709

val it = "Billu" : string Go Back

Full Screen

Close

Quit
Records: Equality
- val pinky2 = IIT Delhi

{ name = "Pinky", S. Arun-Kumar, CSE

fav_colour = "pink", Title Page

age = 3 Contents

}; JJ II

- val pinky2 = J I

{age=3,fav_colour="pink",name="Pinky"
Page 465 of 709

{age:int, fav_colour:string, name:str Go Back

- pinky = pinky2; Full Screen

val it = true : bool Close

Quit
Tuples & Records IIT Delhi

S. Arun-Kumar, CSE

• A k-tuple may be thought of as a


Title Page
record whose fields are numbered
#1 to #k instead of having names.
Contents

JJ II
• A record may be thought of as J I
a generalization of tuples whose
Page 466 of 709

components are named rather than


Go Back

being numbered.
Full Screen
back
Close

Quit
Back to Lists
IIT Delhi

• Every L : α List satisfies


S. Arun-Kumar, CSE
L = []
Title Page

XOR Contents

L = hd(L) :: tl(L) JJ II

• Many functions on lists (L) are de- J I

fined by induction on its length (|L|). Page 467 of 709

 Go Back

c if L = []
f (L) = Full Screen

g(h, T ) if L = h :: T Close

Quit
Lists: Correctness IIT Delhi

S. Arun-Kumar, CSE

Hence their properties (P ) are proved Title Page

by induction on the length of the list. Contents

Basis |L| = 0. Prove P ([]) JJ II

Induction hypothesis (IH) Assume for J I

some |T | = n > 0, P (T ) holds. Page 468 of 709

Induction Step Prove P (h :: T ) for L = Go Back

h :: T with |L| = n + 1 Full Screen

Close

Quit
Lists: Case Analysis
inductive defns on lists IIT Delhi

• Every list has exactly one of the fol- S. Arun-Kumar, CSE

lowing forms (patterns) Title Page

– [] Contents

JJ II
– h::T
J I
• ML provides convenient
Page 469 of 709

case analysis based on patterns.


Go Back

fun f [] = c Full Screen

| f (h::T) = g (h, T) Close

; Quit
Lists: Correctness by
Cases IIT Delhi

Lists-correctness S. Arun-Kumar, CSE

Title Page

P is proved by case analysis.


Contents

Basis Prove JJ II
P ([])
J I

Induction hypothesis (IH) Assume Page 470 of 709

P (T ) Go Back

Induction Step Prove Full Screen

P (h :: T ) Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

List-functions: length
Title Page

Contents


length [] = 0 JJ II

length (h :: T ) = 1 + (length T ) J I

Page 471 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

List Functions: search S. Arun-Kumar, CSE

Title Page

To determine whether x occurs in a list


Contents

L
 JJ II

 search (x, []) = f alse J I

search (x, h :: T ) = true if x = h Page 472 of 709

search (x, h :: T ) = search(x, T ) else



Go Back

Full Screen

Close

Quit
IIT Delhi

List Functions: search2 S. Arun-Kumar, CSE

Title Page

Or even more conveniently


Contents

 search2 (x, []) = f alse JJ II

search2 (x, h :: T ) = (x = h) or J I

search2 (x, T )

Page 473 of 709

Time Complexity?? Go Back

Full Screen

Close

Quit
List Functions: ordered
IIT Delhi

Definition 14 A list L = [a0, . . . , an−1]


S. Arun-Kumar, CSE
is ordered by a relation ≤ if consec-
utive elements are related by ≤, i.e
Title Page

ai ≤ ai+1, for 0 ≤ i < n − 1. Contents

JJ II


 ordered [] J I

ordered [h]

Page 474 of 709


 ordered (h0 :: h1 :: T ) if h0 ≤ h1 and Go Back

ordered(h1 :: T )

Full Screen

Time Complexity?? Close

Quit
List Functions:insert
Given an ordered list L : α List, insert IIT Delhi

an element x : α at an appropriate po- S. Arun-Kumar, CSE

sition Title Page




 insert (x, []) = [x] Contents



 JJ II

 insert (x, h :: T ) = x :: (h :: T )


J I

if x ≤ h Page 475 of 709





 Go Back

insert (x, h :: T ) = h :: (insert (x, T ))





 Full Screen

else

Close

Time Complexity?? Quit


List Functions:
IIT Delhi

reverse S. Arun-Kumar, CSE

Reverse the elements of a list Title Page

L = [a0, . . . , an−1] to obtain M = Contents

[an−1, . . . , a0]. JJ II


reverse [] = [] J I

reverse (h :: T ) = (reverse T )@[h] Page 476 of 709

Go Back

Time Complexity?? O(n2) Full Screen

Close

Quit
List Functions: reverse2 IIT Delhi

S. Arun-Kumar, CSE

reverse [] = [] Title Page

reverse (h :: T ) = rev ((h :: T ), []) Contents

where JJ II

rev ([], N ) = N J I

rev (h :: T, N ) = rev (T, h :: N ) Page 477 of 709

Go Back

Correctness ??
Time Complexity?? O(n) Full Screen

Close

Quit
List Functions:merge IIT Delhi

S. Arun-Kumar, CSE

Merge two ordered lists |L| = l, |M | =


Title Page

m to produce an ordered list |N | = l +


Contents
m containing exactly the elements of
JJ II
L and M . That is if
L = [1, 3, 5, 9, 11] and J I

M = [0, 3, 4, 4, 10], then Page 478 of 709

merge(L, M ) = N , where Go Back

N = [0, 1, 3, 3, 4, 4, 5, 9, 10, 11] Full Screen

Close

Quit
List Functions:merge
IIT Delhi


 merge([], M ) = M S. Arun-Kumar, CSE



 Title Page

merge(L, []) = L

 Contents



 JJ II

merge(L, M ) = J I

a :: (merge(S, M )) if a ≤ b



 Page 479 of 709



 Go Back

merge(L, M ) =



 Full Screen

b :: (merge(L, T )) else

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

List Functions:merge Title Page

contd. Contents

JJ II
where 
L = a :: S J I

M = b :: T Page 480 of 709

Go Back

Full Screen

Close

Quit
ML: merge IIT Delhi

S. Arun-Kumar, CSE

fun merge ([], M) = M Title Page

| merge (L, []) = L


Contents

| merge (L as a::S,
JJ II
M as b::T) =
J I
if a <= b
Page 481 of 709
then a::merge(S, M)
Go Back
else b::merge(L, T)
Full Screen

Close

Quit
Sorting by Insertion
Given a list of elements to reorder IIT Delhi

them (i.e. with the same number of S. Arun-Kumar, CSE

occurrences of each element as in the Title Page

original list) to produce a new ordered Contents

list. JJ II

Hence sort[10, 8, 3, 6, 9, 7, 4, 8, 1] = J I

[1, 3, 4, 6, 7, 8, 8, 9, 10] Page 482 of 709


isort[] = [] Go Back

isort(h :: T ) = insert(h, (isortT )) Full Screen

Close

Time Complexity??
Quit
Sorting by Merging IIT Delhi

S. Arun-Kumar, CSE


 msort [] = [] Title Page

msort [a] = [a]



Contents


 msort L = merge ((msort M ), JJ II

(msort N ))

J I

where Page 483 of 709

Go Back

(M, N ) = split L
Full Screen

Close

Quit
Sorting by Merging IIT Delhi

where S. Arun-Kumar, CSE


 split [] = ([], []) Title Page

split [a] = ([a], []) Contents

split (a :: b :: P ) = (a :: Lef t, b :: Right)


 JJ II

J I

where Page 484 of 709

(Lef t, Right) = split P Go Back

Full Screen

Time Complexity?? Close

Quit
Functions as Data IIT Delhi

list of functions
S. Arun-Kumar, CSE

• Every function is unary. A function Title Page

of many arguments may be thought Contents

of as a function of a single argu- JJ II

ment i.e. a tuple of appropriate J I

type. Page 485 of 709

• Every function is a value of an ap- Go Back

propriate type. Full Screen

• Hence functions are also data. Close

Quit
Higher Order IIT Delhi

Functions S. Arun-Kumar, CSE

Compound data may be constructed Title Page

from functions as values using the Contents

constructors of the compound data JJ II

structure. J I

Page 486 of 709

Functions may be defined with


Go Back
other functions and/or data as arguments
Full Screen
to produce new values or new functions.
Close

Quit
6. Higher Order Functions & Structured Data
6.1. Higher Order Functions
1. Summary: Compound Data
IIT Delhi
2. List: Examples

3. Lists: Sorting S. Arun-Kumar, CSE

4. Higher Order Functions


Title Page
5. An Example

6. Currying
Contents
7. Currying: Contd

8. Generalization JJ II

9. Generalization: 2
J I
10. Applying a list

11. Trying it out Page 487 of 709

12. Associativity
Go Back
13. Apply to a list
14. Sequences Full Screen

15. Further Generalization


Close
16. Further Generalization

17. Sequences
Quit
18. Efficient Generalization

19. Sequences: 2

20. More Generalizations

21. More Summations IIT Delhi

22. Or Maybe . . . Products


N S. Arun-Kumar, CSE
23. Or Some Other
N
24. Other Title Page

25. Examples of ⊗, e
Contents

JJ II

J I

Page 488 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

Summary: Compound S. Arun-Kumar, CSE

Data Title Page

Contents

• Records and tuples JJ II

• Lists J I

– Correctness Page 489 of 709

– Examples Go Back

Full Screen

Close

Quit
IIT Delhi

List: Examples S. Arun-Kumar, CSE

Title Page
• Length of a list
Contents

• Searching a list JJ II

• Checking whether a list is ordered J I

• Reversing a list Page 490 of 709

• Sorting of lists Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Lists: Sorting Contents

• Sorting by insertion JJ II

J I
• Sorting by Divide-and-Conquer
Page 491 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Higher Order Title Page

Functions JJ
Contents

II

• Functions as data J I

• Higher order functions Page 492 of 709

Go Back

Full Screen

Close

Quit
An Example IIT Delhi

List of functions S. Arun-Kumar, CSE

Title Page

• add1 x = x + 1
Contents

• add2 x = x + 2 JJ II

• add3 x = x + 3 J I

Suppose we needed to define a long Page 493 of 709

list of length n, where the i-th element Go Back

is the function that adds i + 1 to the


argument. Full Screen

Close

Quit
Currying
addc y x = x + y IIT Delhi

S. Arun-Kumar, CSE

ML’s response : Title Page

val addc = fn : Contents

int -> (int -> int)


JJ II

Contrast with ML’s response J I

- op +; Page 494 of 709

val it = fn : int * int -> int Go Back

addc is the curried version of the bi- Full Screen

nary operation +. Close

Quit
Currying: Contd
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 495 of 709

Go Back

Full Screen

Close

Quit
Generalization IIT Delhi

Then S. Arun-Kumar, CSE

Title Page

• addc1 = (addc 1): int -> int


Contents

• addc2 = (addc 2): int -> int JJ II

• addc3 = (addc 3): int -> int J I

and for any i, Page 496 of 709

(addc i): int -> int Go Back

Full Screen
is the required function.
Close

Quit
IIT Delhi

Generalization: 2 S. Arun-Kumar, CSE

list adds n = Title Page


[] if n ≤ 0 Contents

(list adds(n − 1))@[(addc n)] else JJ II

J I

ML’s response : Page 497 of 709

val list_adds = fn : Go Back

int -> (int -> int) list Full Screen

Close

Quit
Applying a list IIT Delhi

addall S. Arun-Kumar, CSE

 Title Page

applyl [] x = [] Contents

applyl (h :: T ) x = (h x) :: (applyl T x) JJ II

J I
ML’s response:
Page 498 of 709

val applyl = fn :
Go Back

(’a -> ’b) list ->


Full Screen
’a -> ’b list
Close

Quit
IIT Delhi

Trying it out S. Arun-Kumar, CSE

interval x n = applyl x (list adds n) Title Page

ML’s response: Contents

val interval = fn : JJ II

int -> int -> int list J I

- interval 53 5; Page 499 of 709

val it = [54,55,56,57,58] Go Back

: int list Full Screen

Close

Quit
Associativity IIT Delhi

• Application associates to the left. S. Arun-Kumar, CSE

f x y = ((f x) y) Title Page

• → associates to the right.


Contents

α → β → γ = α → (β → γ) JJ II

J I

If f : α → β → γ → δ Page 500 of 709

then f a : β → γ → δ Go Back

and f a b : γ → δ Full Screen

and f a b c : δ Close

Quit
Apply to a list
Apply a list Transpose of a matrix IIT Delhi


map f [] = [] S. Arun-Kumar, CSE

map f (h :: T ) = (f h) :: (map f T ) Title Page

Contents

val it = fn : (’a -> ’b) -> JJ II

’a list -> ’b list J I

- map addc3 [4, 6, ˜1, 0]; Page 501 of 709

val it = [7,9,2,3] : int list Go Back

- map real [7,9,2,3]; Full Screen

val it = [7.0,9.0,2.0,3.0] Close

: real list Quit


Sequences IIT Delhi

Arithmetic sequences-1 S. Arun-Kumar, CSE

Arithmetic sequences-2
Title Page
Arithmetic sequences-3
Contents

AS4(a, d, n) =
JJ II


 [] if n ≤ 0 J I

Page 502 of 709


 a :: (map (addc d) else Go Back

(AS4 (a, d, (n − 1))))



Full Screen

Close

Quit
Further Generalization
Given IIT Delhi

f :α∗α→α S. Arun-Kumar, CSE

Then Title Page

curry2 f x y = f (x, y) Contents

JJ II

and J I

(curry2 f ) : α → (α → α) Page 503 of 709

Go Back

and for any d : α,


Full Screen

((curry2 f ) d) : α → α Close

Quit
Further Generalization IIT Delhi

S. Arun-Kumar, CSE

seq(f, a, d, n) =
Title Page


 [] if n ≤ 0 Contents


JJ II


 a :: (map ((curry2 f ) d) J I

(seq (f, a, d, n − 1))) else



Page 504 of 709

is the sequence of length n generated Go Back

with ((curry2 f ) d), starting from a. Full Screen

Close

Quit
Sequences IIT Delhi

Arithmetic: AS5(a, d, n) = S. Arun-Kumar, CSE

seq(op+, a, d, n) Title Page

Geometric: GS1(a, r, n) = Contents

seq(op∗, a, r, n) JJ II

Harmonic: HS1(a, d, n) = J I

map reci (AS5(a, d, n)) Page 505 of 709

Go Back

where
reci x = 1.0/(real x) gives the recipro- Full Screen

cal of a (non-zero) integer. Close

Quit
Efficient Generalization IIT Delhi

Let’s not use map repeatedly. S. Arun-Kumar, CSE

seq2(f, g, a, d, n) = Title Page


 [] if n ≤ 0
Contents

JJ II

(f a) :: (seq2 (f, g(a, d), d, n − 1)) else J I



Page 506 of 709

is the sequence of length n generated Go Back

with a unary f , a binary g starting from Full Screen

f (a). Close

Quit
IIT Delhi

Sequences: 2
S. Arun-Kumar, CSE

Title Page

• AS6(a, d, n) = seq2(id, op+, a, d, n) Contents

• GS2(a, r, n) = seq2(id, op∗, a, r, n) JJ II

J I
• HS2(a, d, n) = seq2(reci, op+, a, d, n)
Page 507 of 709

where id x = x is the identity function. Go Back

Full Screen

Close

Quit
More Generalizations
Often interested in some particular IIT Delhi

measure related to a sequence, rather S. Arun-Kumar, CSE

than in the sequence itself, e.g. sum- Title Page

mations of Contents

• arithmetic, geometric, harmonic se- JJ II

quences J I

• ex,
Page 508 of 709

trigonometric functions upto


Go Back

some n-th term


Full Screen

• (Truncated) Taylor and Maclaurin Close

series Quit
More Summations
Wasteful to first generate the se-
quence and then compute the mea- IIT Delhi

sure S. Arun-Kumar, CSE

Xu Title Page

f (i) Contents

i=l JJ II

J I
where the range [l, u] is defined by a
unary succ function Page 509 of 709

sum(f, succ, l, u) = Go Back

 Full Screen

0 if [l, u] = ∅ Close

f (l)+sum(f, succ, succ(l), u) else


Quit
Or Maybe . . . Products IIT Delhi

Or may be interested in forming prod- S. Arun-Kumar, CSE

ucts of sequences. Title Page

u
Y Contents

f (i) JJ II

i=l J I

prod(f, succ, l, u) = Page 510 of 709


1 if [l, u] = ∅ Go Back

f (l)∗prod(f, succ, succ(l), u) else Full Screen

Close

Quit
Or Some Other N

Or some other binary operation ⊗


IIT Delhi

which has the following properties:


S. Arun-Kumar, CSE

• ⊗ : (α ∗ α) → α is closed Title Page

• ⊗ is associative i.e. Contents

a ⊗ (b ⊗ c) = (a ⊗ b) ⊗ c JJ II

• ⊗ has an identity element e i.e J I

a⊗e=a=e⊗a Page 511 of 709

Go Back

u
O Full Screen

f (l) Close

i=l Quit
IIT Delhi

Other N S. Arun-Kumar, CSE

Title Page

Then if f, succ : α → α Contents

ser(⊗, f, succ, l, u) = JJ II

e if [l, u] = ∅ J I

f (l) ⊗ ser(⊗, f, succ, succ(l), u) else Page 512 of 709

Go Back

Full Screen

Close

Quit
Examples of ⊗, e IIT Delhi

• +, 0 on integers and reals S. Arun-Kumar, CSE

Title Page

• concatenation and the empty string Contents

on strings
JJ II

• andalso, true on booleans J I

• orelse, false on booleans Page 513 of 709

• +, 0 on vectors and matrices Go Back

• ∗, 1 on vectors and matrices


Full Screen

Close

Quit
6.2. Structured Data
1. Transpose of a Matrix

2. Transpose: 0

3. Transpose: 10 IIT Delhi

4. Transpose: 01
S. Arun-Kumar, CSE
5. Transpose: 20

6. Transpose: 02 Title Page

7. Transpose: 30
Contents
8. Transpose: 03

9. trans JJ II
10. is2DM atrix

11. User Defined Types J I

12. Enumeration Types


Page 514 of 709
13. User Defined Structural Types

14. Functions vs. data Go Back

15. Data as 0-ary Functions


Full Screen
16. Data vs. Functions

17. Data vs. Functions: Recursion Close

18. Lists
Quit
19. Constructors

20. Shapes

21. Shapes: Triangle Inequality

22. Shapes: Area IIT Delhi

23. Shapes: Area


S. Arun-Kumar, CSE
24. ML: Try out

25. ML: Try out (contd.) Title Page

26. Enumeration Types


Contents
27. Recursive Data Types
28. Resistors: Datatype JJ II
29. Resistors: Equivalent

30. Resistors J I

31. Resistors: Example


Page 515 of 709
32. Resistors: ML session
Go Back

Full Screen

Close

Quit
Transpose of a Matrix IIT Delhi

Map S. Arun-Kumar, CSE

Assume a 2-D r × c matrix is repre- Title Page

sented by a list of lists of elements. Contents

Then JJ II

transpose L = J I


trans L if is2DM atrix(L) Page 516 of 709

⊥ else Go Back

Full Screen

where Close

Quit
Transpose: 0
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 11 12 13 ]
Title Page

Contents

[ 21 22 23 ] JJ II

[ 31 32 33 ] J I

[ 41 42 43 ] Page 517 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 10
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 11 12 13 ]
Title Page

Contents

[ 21 22 23 ] JJ II

[ 31 32 33 ] J I

[ 41 42 43 ] Page 518 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 01
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 12 13 ] [ 11 21 31 41 ]
Title Page

Contents

[ 22 23 ] JJ II

[ 32 33 ] J I

[ 42 43 ] Page 519 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 20
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 12 13 ] [ 11 21 31 41 ]
Title Page

Contents

[ 22 23 ] JJ II

[ 32 33 ] J I

[ 42 43 ] Page 520 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 02
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 13 ] [ 11 21 31 41 ]
Title Page

Contents

[ 23 ]
[ 12 22 32 42 ] JJ II

[ 33 ] J I

[ 43 ] Page 521 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 30
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ 13 ] [ 11 21 31 41 ]
Title Page

Contents

[ 23 ]
[ 12 22 32 42 ] JJ II

[ 33 ] J I

[ 43 ] Page 522 of 709

Go Back

] ]
Full Screen

Close

Quit
Transpose: 03
IIT Delhi

[ [ S. Arun-Kumar, CSE

[ ] [ 11 21 31 41 ]
Title Page

Contents

[ ]
[ 12 22 32 42 ] JJ II

[ ] J I

[ ] [ 13 23 33 43 ] Page 523 of 709

Go Back

] ]
Full Screen

Close

Quit
trans IIT Delhi

 S. Arun-Kumar, CSE


 trans [] = [] Title Page

trans [] :: T L = []

Contents


 trans LL = (map hd LL) :: JJ II

(trans (map tl LL))



J I

and Page 524 of 709

is2DM atrix = #1(dimensions L) Go Back

where Full Screen

Close

Quit
is2DM atrix
IIT Delhi


 dimensions [] = (true, 0, 0) S. Arun-Kumar, CSE



 Title Page

 dimensions [H] =


Contents

(true, 1, h) JJ II



 J I
dimensions (H :: T L) =



 Page 525 of 709

(b and (h = c), r + 1, c)

Go Back

where dimensions T L = (b, r, c) Full Screen

and h = length H Close

Quit
User Defined Types IIT Delhi

S. Arun-Kumar, CSE

Many languages allow user-defined Title Page

data types.
Contents

• record types: Pinky and Billu JJ II

• Enumerations: aggregates of het- J I

erogeneous data. Page 526 of 709

• other structural constructions (if Go Back

desperate!) Full Screen

Close

Quit
Enumeration Types
Many languages allow user-defined IIT Delhi

data types. S. Arun-Kumar, CSE

• record types: Pinky and Billu Title Page

• Enumerations: aggregates of het- Contents

erogeneous data. JJ II

J I
– days of the week
Page 527 of 709

– colours
Go Back

– geometrical shapes Full Screen

• other structural constructions (if Close

desperate!) Quit
User Defined
Structural Types
IIT Delhi
Many languages allow user-defined
data types. S. Arun-Kumar, CSE

Title Page

• record types: Pinky and Billu


Contents

• Enumerations: aggregates of het- JJ II

erogeneous data. J I

• other structural constructions (if Page 528 of 709

desperate!) Go Back

– trees Full Screen

– graphs Close

– symbolic expressions Quit


IIT Delhi

Functions vs. data S. Arun-Kumar, CSE

• Inspired by the list constructors, nil Title Page

and cons Contents

JJ II
• Grand Unification of functions and
J I
data
Page 529 of 709

– Functions as data
Go Back

– Data as functions Full Screen

Close

Quit
Data as 0-ary Functions
IIT Delhi

• Every data element may be re-


S. Arun-Kumar, CSE

garded as a function with 0 argu-


Title Page

ments
Contents

– Caution: A constant function JJ II


f (x) = 5, for all x : α
J I
where
f : α → int Page 530 of 709

is not the same as a value Go Back

5 : int Full Screen

. Their types are different. Close

Quit
IIT Delhi

Data vs. Functions S. Arun-Kumar, CSE

Title Page

Facilities Functions Data Contents

primitive operations values JJ II

user-defined functions constructors J I

composition application alternative Page 531 of 709

recursion recursion recursion Go Back

Full Screen

Close

Quit
IIT Delhi

Data vs. Functions: S. Arun-Kumar, CSE

Recursion Title Page

Recursion Contents

Basis JJ II

naming J I

composition Page 532 of 709

induction Go Back

Full Screen

Close

Quit
Lists as Structured
Data
datatype ’a list = IIT Delhi

nil | S. Arun-Kumar, CSE

cons of ’a * ’a list Title Page

Contents

Every α list is either JJ II

J I
nil : (Basis, name)
Page 533 of 709

| : or (alternative) Go Back

cons : constructed inductively from Full Screen

an element of type ’a and another Close

list of type ’a list using the con- Quit

structor cons
Constructors
IIT Delhi

• Inspired by the list constructors


S. Arun-Kumar, CSE

nil : α list Title Page

cons : α × α list → α list Contents

• combine heterogeneous types: α JJ II

and α list J I

• allows recursive definition by a form Page 534 of 709

of induction Go Back

Basis : nil Full Screen

Close
Induction : cons
Quit
Shapes IIT Delhi

S. Arun-Kumar, CSE

A non-recursive data type


Title Page

datatype shape =
Contents

JJ II
CIRCLE of real
J I
| RECTANGLE of real * real
Page 535 of 709
| TRIANGLE of
Go Back
real * real * real
Full Screen

Close

Quit
Shapes: Triangle IIT Delhi

Inequality S. Arun-Kumar, CSE

Title Page

fun isTriangle Contents

(TRIANGLE (a, b, c)) = JJ II

(a+b>c) andalso J I

(b+c>a) andalso Page 536 of 709

(c+a>b) Go Back

| isTriangle _ = false
Full Screen

Close

Quit
Shapes: Area IIT Delhi

exception notShape; S. Arun-Kumar, CSE

Title Page

fun area (CIRCLE (r)) = Contents

3.14159 * r * r JJ II

| area (RECTANGLE (l,b)) = J I

l*b Page 537 of 709

| area (s as Go Back

TRIANGLE (a, b, c)) = Full Screen

Close

Quit
Shapes: Area
IIT Delhi

S. Arun-Kumar, CSE

if isTriangle (s) then Title Page

let val s = (a+b+c)/2.0 Contents

in Math.sqrt JJ II

(s*(s-a)*(s-b)*(s-c)) J I

end Page 538 of 709

else raise notShape; Go Back

Full Screen

Close

Quit
ML: Try out
- use "shapes.sml"; IIT Delhi

[opening shapes.sml] S. Arun-Kumar, CSE

datatype shape Title Page

= CIRCLE of real Contents

| RECTANGLE of real * real


JJ II
| TRIANGLE of
J I
real * real * real
Page 539 of 709

val isTriangle =
Go Back
fn : shape -> bool
Full Screen
exception notShape
Close
val area = fn : shape -> real
Quit
ML: Try out (contd.)
val it = () : unit IIT Delhi

- area (TRIANGLE (2.0,1.0,3.0)); S. Arun-Kumar, CSE

Title Page

uncaught exception notShape Contents

raised at: shapes.sml:22.17-22.25 JJ II

- area J I

(TRIANGLE (3.0, 4.0, 5.0)); Page 540 of 709

val it = 6.0 : real Go Back

- Full Screen

Close

Back to User defined types


Quit
Enumeration Types
• Enumeration types are non- IIT Delhi

recursive datatypes with S. Arun-Kumar, CSE

Title Page

• 0-ary constructors
Contents

datatype working = MON | TUE JJ II

| WED | THU | FRI; J I

datatype weekends = SAT | SUN Page 541 of 709

datatype weekdays = working Go Back

| weekends; Full Screen

Close

Back to User defined types


Quit
Recursive Data Types IIT Delhi

• But the really interesting types are S. Arun-Kumar, CSE

the recursive data types Title Page

Back to Lists Contents

JJ II

• As with lists proofs of correctness J I


on recursive data types depend on Page 542 of 709

a case-analysis of the structure (ba-


Go Back

sis and inductive constructors)


Full Screen

Correctness on lists Close

Quit
IIT Delhi

Resistors: Datatype S. Arun-Kumar, CSE

Title Page

datatype resist = Contents

RES of real |
JJ II
SER of resist * resist |
J I
PAR of resist * resist
Page 543 of 709

Go Back

Full Screen

Close

Quit
Resistors: Equivalent
IIT Delhi

fun value (RES (r)) = r S. Arun-Kumar, CSE

| value (SER (R1, R2)) = Title Page

value (R1) + value (R2) Contents

| value (PAR (R1, R2)) =


JJ II
let val r1 = value (R1);
J I
val r2 = value (R2)
Page 544 of 709

in (r1*r2)/(r1+r2)
Go Back
end;
Full Screen

Close

Quit
Resistors
5.0 IIT Delhi

5.0 2.0 S. Arun-Kumar, CSE

Title Page

4.0 Contents

JJ II

J I

Page 545 of 709

3.0 Go Back

Full Screen

+ − Close

Quit
Resistors: Example
val R = PAR(
SER(
IIT Delhi

PAR(
S. Arun-Kumar, CSE

RES (5.0),
Title Page

RES (4.0)
Contents
),
JJ II
SER(
J I
RES (5.0),
RES (2.0) Page 546 of 709

) Go Back

), Full Screen

RES(3.0) Close

); Quit
Resistors: ML session
- use "resistors.sml";
[opening resistors.sml] IIT Delhi

datatype resist = PAR of resist * res S. Arun-Kumar, CSE

| RES of real Title Page

| SER of resist * res Contents

val value = fn : resist -> real JJ II

val R = PAR (SER (PAR #,SER #),RES 3. J I

val it = () : unit Page 547 of 709

- value R; Go Back

val it = 2.26363636364 : real Full Screen

-
Close

Quit
Resistance Expressions
6.3. User Defined Structured Data Types
1. User Defined Types
IIT Delhi
2. Resistors: Grouping
3. Resistors: In Pairs S. Arun-Kumar, CSE

4. Resistor: Values
Title Page
5. Resistance Expressions
6. Resistance Expressions Contents

7. Arithmetic Expressions
JJ II
8. Arithmetic Expressions: 0
9. Arithmetic Expressions: 1
J I
10. Arithmetic Expressions: 2
11. Arithmetic Expressions: 3 Page 548 of 709

12. Arithmetic Expressions: 4


Go Back
13. Arithmetic Expressions: 5
14. Arithmetic Expressions: 6 Full Screen

15. Arithmetic Expressions: 7


Close
16. Arithmetic Expressions: 8
17. Binary Trees Quit
18. Arithmetic Expressions: 0

19. Trees: Traversals

20. Recursive Data Types: Correctness

21. Data Types: Correctness IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 549 of 709

Go Back

Full Screen

Close

Quit
User Defined Types IIT Delhi

S. Arun-Kumar, CSE

• Records
Title Page

• Structural Types Contents

– Constructors JJ II

∗ Non-recursive J I

∗ Enumeration Types Page 550 of 709

– Recursive datatypes Go Back

∗ Resistance circuits Full Screen

Close

Quit
Resistors: Grouping
R3 IIT Delhi

5.0 S. Arun-Kumar, CSE

5.0 2.0 Title Page

Contents

4.0 R2 JJ II

R1 J I

Page 551 of 709

3.0 R4 Go Back

Full Screen

+ − Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Resistors: In Pairs Title Page

val R1 = PAR (RES 5.0,RES 4.0) : resi Contents

val R2 = SER (RES 5.0,RES 2.0) : resi


JJ II

val R3 = SER (R1, R2); J I

val R4 = PAR (R3, RES(3.0)); Page 552 of 709

Go Back

Full Screen

Close

Quit
Resistor: Values IIT Delhi

- value R1;
S. Arun-Kumar, CSE

val it = 2.22222222222 : real


Title Page

- value R2;
Contents

val it = 7.0 : real


JJ II
- value R3;
J I
val it = 9.22222222222 : real
Page 553 of 709
- value R4;
val it = 2.26363636364 : real Go Back

- Full Screen

Close

Quit
Resistance
Expressions
A resistance expression IIT Delhi

PAR S. Arun-Kumar, CSE

Title Page

SER Contents

JJ II

J I
PAR SER
Page 554 of 709

Go Back

RES RES RES RES RES Full Screen

Close

5 4 5 2 3
Quit

Circuit Diagram
Resistance
Expressions
A resistance expression
IIT Delhi

R4 PAR S. Arun-Kumar, CSE

Title Page

R3 SER Contents

JJ II

J I
R1 PAR R2 SER
Page 555 of 709

Go Back

RES RES RES RES RES Full Screen

2
Close

5 4 5 3
Quit

Circuit Diagram
IIT Delhi

S. Arun-Kumar, CSE

Arithmetic Expressions Title Page

Contents

ML arithmetic expressions: JJ II

((5 * ˜4) + ˜(5 - 2)) div 3 J I

are represented as trees Page 556 of 709

Go Back

Full Screen

Close

Quit
Arithmetic
Expressions: 0 IIT Delhi

div S. Arun-Kumar, CSE

Title Page

+ Contents

JJ II

J I

* ~ Page 557 of 709

− Go Back

5 ~ 3 Full Screen

Close

4 5 2
Quit
Arithmetic
Expressions: 1 IIT Delhi

div S. Arun-Kumar, CSE

Title Page

+ Contents

JJ II

J I

* ~
Page 558 of 709

− Go Back

5 ~ 3 Full Screen

Close

4 5 2
Quit
Arithmetic
Expressions: 2 IIT Delhi

S. Arun-Kumar, CSE

div Title Page

Contents

+ JJ II

J I

Page 559 of 709

* ~
Go Back

3 Full Screen

5 −4 3 Close

Quit
Arithmetic
Expressions: 3 IIT Delhi

S. Arun-Kumar, CSE

div
Title Page

Contents

+ JJ II

J I

Page 560 of 709

* ~
Go Back

3 Full Screen

5 −4 3 Close

Quit
Arithmetic
Expressions: 4 IIT Delhi

S. Arun-Kumar, CSE

div Title Page

Contents

+ JJ II

J I

Page 561 of 709

−20 −3
Go Back

Full Screen

3 Close

Quit
Arithmetic
Expressions: 5 IIT Delhi

S. Arun-Kumar, CSE

div Title Page

Contents

+ JJ II

J I

Page 562 of 709

−20 −3
Go Back

Full Screen

3 Close

Quit
Arithmetic
Expressions: 6 IIT Delhi

S. Arun-Kumar, CSE

div Title Page

Contents

−23 JJ II

J I

Page 563 of 709

Go Back

Full Screen

3 Close

Quit
Arithmetic
Expressions: 7 IIT Delhi

S. Arun-Kumar, CSE

div Title Page

Contents

−23 JJ II

J I

Page 564 of 709

Go Back

Full Screen

3 Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Arithmetic Contents

Expressions: 8 JJ II

J I
−8
Page 565 of 709

Go Back

Full Screen

Close

Quit
Binary Trees
IIT Delhi

S. Arun-Kumar, CSE

datatype ’a bintree = Title Page

Empty | Contents

Node of ’a * JJ II

’a bintree * J I

’a bintree Page 566 of 709

Go Back

Full Screen

Close

Quit
Arithmetic
Expressions: 0 IIT Delhi
Arithmetic Expressions
S. Arun-Kumar, CSE
div
Title Page

+ Contents

JJ II

J I
* ~
Page 567 of 709

5 ~ − 3 Go Back

Full Screen

4 5 2 Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Trees: Traversals Title Page

Contents

• preorder JJ II

• inorder J I

• postorder Page 568 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Recursive Data Types: Title Page

Contents

Correctness JJ II

Correctness on lists by cases


J I

P is proved by case analysis. Page 569 of 709

Go Back

Full Screen

Close

Quit
Data Types: IIT Delhi

Correctness S. Arun-Kumar, CSE

Title Page

Basis Prove P (c) for each non-


Contents

recursive constructor c
JJ II

Induction hypothesis (IH) Assume J I

P (T ) for all elements of the data Page 570 of 709

type less than a certain depth Go Back

Induction Step Prove P (r(T1, . . . , Tn)) Full Screen

for each recursive constructor r Close

Quit
7. Imperative Programming: An Introduction
7.1. Introducing a Memory Model
1. Summary: Functional Model
IIT Delhi
2. CPU & Memory: Simplified

3. Resource Management S. Arun-Kumar, CSE

4. Shell: User Interface


Title Page
5. GUI: User Interface

6. Memory Model: Simplified


Contents
7. Memory

8. The Imperative Model JJ II

9. State Changes: σ
J I
10. State

11. State Changes Page 571 of 709

12. State Changes: σ


Go Back
13. State Changes: σ1

14. State Changes: σ2 Full Screen

15. Languages
Close
16. User Programs

17. Imperative Languages


Quit
18. Imperative vs Functional Variables

19. Assignment Commands

20. Assignment Commands

21. Assignment Commands IIT Delhi

22. Assignment Commands


S. Arun-Kumar, CSE
23. Assignment Commands

24. Assignment Commands: Swap Title Page

25. Swap
Contents
26. Swap
27. Swap JJ II

J I

Page 572 of 709

Go Back

Full Screen

Close

Quit
Summary: Functional
Model IIT Delhi

• Stateless (as is most mathematics) S. Arun-Kumar, CSE

• Notion of value is paramount Title Page

Contents

– Integers, reals, booleans, strings


JJ II
and characters are all values
J I

– Every function is also a value Page 573 of 709

– Every complex piece of data is Go Back

also a value Full Screen

• No concept of storage (except for Close

space complexity calculations) Quit


CPU & Memory:
Simplified IIT Delhi

S. Arun-Kumar, CSE

Title Page

Memory
Contents

JJ II

Peripherals J I

CPU
Page 574 of 709

Printer
Disk Go Back

Keyboard
Full Screen

Screen Close

Quit
Resource
Management IIT Delhi

S. Arun-Kumar, CSE

Title Page

Memory
Contents

JJ II

Peripherals J I

CPU Printer Page 575 of 709

Disk
Go Back

Keyboard
Screen Full Screen

Close

Operating System
Quit
Shell: User Interface
IIT Delhi

S. Arun-Kumar, CSE

Memory Title Page

Contents

Peripherals JJ II

CPU Printer
J I
Disk

Keyboard Page 576 of 709

Screen
Go Back

Operating System Full Screen

Shell Close

Quit
GUI: User Interface IIT Delhi

S. Arun-Kumar, CSE

Memory Title Page

Contents

Peripherals
JJ II
CPU Printer
Disk
J I
Keyboard
Screen Page 577 of 709

Operating System Go Back

Shell Full Screen

Graphical User Interface (GUI) Close

Quit
Memory Model:
Simplified IIT Delhi

1. A sequence of storage cells S. Arun-Kumar, CSE

2. Each cell is a container of a single Title Page

unit of information. Contents

• integer, real, boolean, character JJ II

or string J I

3. Each cell has a unique name, called Page 578 of 709

its address Go Back

Full Screen
4. The memory cell addresses range
from 0 to (usually) 2k − 1 (for some Close

k) Quit
Memory
0 1 2 3
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 579 of 709

Go Back

Full Screen

Close

32K−1 Quit
The Imperative Model
IIT Delhi

S. Arun-Kumar, CSE

• Memory or Storage made explicit Title Page

• Notion of state (of memory) Contents

JJ II
– State is simply the value con-
J I
tained in each cell.
Page 580 of 709

– state : Addresses → V alues


Go Back

• State changes Full Screen

Close

Quit
State Changes: σ
0 1 2 3
IIT Delhi

S. Arun-Kumar, CSE

4
Title Page

Contents

3.1 JJ II

true
J I

Page 581 of 709

Go Back

"#a" Full Screen

Close

Assume all other cells are filled with null


Quit
IIT Delhi

State S. Arun-Kumar, CSE

Title Page
The state σ
Contents

• σ(12) = 4 : int JJ II

• σ(20) = null J I

• σ(43) = true : bool Page 582 of 709

• σ(66) = ”#a” : char Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

State Changes Title Page

• A state change takes place when Contents

the value in some cell changes JJ II

J I
• The contents of only one cell may
Page 583 of 709

be changed at a time.
Go Back

Full Screen

Close

Quit
State Changes: σ
0 1 2 3
IIT Delhi

S. Arun-Kumar, CSE

4
Title Page

Contents

3.1 JJ II

true
J I

Page 584 of 709

Go Back

"#a" Full Screen

Close

Assume all other cells are filled with null


Quit
State Changes: σ1
0 1 2 3
IIT Delhi

5
S. Arun-Kumar, CSE

Title Page

Contents

3.1 JJ II

true
J I

Page 585 of 709

Go Back

"#a" Full Screen

Close

Assume all other cells are filled with null


Quit
State Changes: σ2
0 1 2 3
IIT Delhi

5
S. Arun-Kumar, CSE

Title Page

Contents

3.1 JJ II

true
J I

Page 586 of 709

Go Back

12 "#a" Full Screen

Close

Assume all other cells are filled with null


Quit
Languages
IIT Delhi

S. Arun-Kumar, CSE

Memory Title Page

Contents

Peripherals JJ II

CPU Printer J I
Disk
Page 587 of 709

Keyboard
Go Back
Screen
Full Screen

Operating System Close

Programming Language Interface Quit


User Programs
IIT Delhi

S. Arun-Kumar, CSE

Memory Title Page

Contents

Peripherals
JJ II
CPU Printer
Disk J I

Keyboard
Page 588 of 709
Screen
Go Back

Operating System
Full Screen

Programming Language Interface Close

User Programs Quit


Imperative Languages
• How is the memory accessed? IIT Delhi

S. Arun-Kumar, CSE

– Through system calls to the OS.


Title Page

• How are memory cells identified? Contents

– Use Imperative variables. JJ II

– Each such variable is a name J I

mapped to an address . Page 589 of 709

• How are state changes accom- Go Back

plished? Full Screen

Close
– By the assignment command.
Quit
Imperative vs IIT Delhi

Functional Variables
S. Arun-Kumar, CSE

Title Page

Functional Imperative Contents

name of a value name of an address JJ II

constant could change with time J I

Page 590 of 709

The value contained in an imperative


Go Back

variable x is denoted !x. Full Screen

Close

Quit
Assignment IIT Delhi

Commands S. Arun-Kumar, CSE

Title Page

Let x and y be imperative variables.


Contents
Consider the following commands.
JJ II
Assuming !x = 1 and !y = 2.
J I

Page 591 of 709

Go Back

x 1 y 2 Full Screen

Close

Quit
Assignment IIT Delhi

Commands S. Arun-Kumar, CSE

Title Page

Store the value 5 in x.


x := 5 Contents

JJ II

J I

Page 592 of 709

x 5 y 2 Go Back

Full Screen

Close

Quit
Assignment IIT Delhi

Commands S. Arun-Kumar, CSE

Title Page

Copy the value contained in y into x.


x :=!y Contents

JJ II

J I

Page 593 of 709

x 2 y 2 Go Back

Full Screen

Close

Quit
Assignment IIT Delhi

Commands S. Arun-Kumar, CSE

Increment the value contained in x by 1. Title Page

x :=!x + 1 Contents

. JJ II

J I

Page 594 of 709

Go Back

x 3 y 2 Full Screen

Close

Quit
Assignment IIT Delhi

Commands S. Arun-Kumar, CSE

Store the product of the values in x and y Title Page

in y. Contents

y :=!x∗!y JJ II

J I

Page 595 of 709

Go Back

x 3 y 6 Full Screen

Close

Quit
Assignment
Commands: Swap IIT Delhi

Swap the values in x and y. S. Arun-Kumar, CSE

Title Page

Contents

Swapping values implies trying to JJ II

make two state changes simultane- J I

ously! Page 596 of 709

Go Back

Full Screen

Requires a new memory cell t to tem- Close

porarily store one of the values. Quit


Swap IIT Delhi

How does one get a new memory S. Arun-Kumar, CSE

cell? Title Page

val t = ref 0
Contents

Then the rest is easy JJ II

J I
val t = ref 0;
t := !x; Page 597 of 709

x := !y; Go Back

y := !t; Full Screen

Close

Quit
IIT Delhi

Swap S. Arun-Kumar, CSE

Title Page

Could be made simpler! Contents

val t = ref (!x); JJ II

x := !y; J I

y := !t; Page 598 of 709

Go Back

Full Screen

Close

Quit
Swap
IIT Delhi

S. Arun-Kumar, CSE

Could use a temporary functional vari- Title Page

able t instead of an imperative vari- Contents

able JJ II

val t = !x; J I

x := !y; Page 599 of 709

y := t; Go Back

Full Screen

Close

Quit
7.2. Imperative Programming:
1. Imperative vs Functional
IIT Delhi
2. Features of the Store
3. References: Experiments S. Arun-Kumar, CSE

4. References: Experiments
Title Page
5. References: Experiments
6. Aliases Contents

7. References: Experiments
JJ II
8. References: Aliases
9. References: Experiments
J I
10. After Garbage Collection
11. Side Effects Page 600 of 709

12. Imperative ML
Go Back
13. Imperative ML
14. Imperative ML Full Screen

15. Imperative ML
Close
16. Nasty Surprises
17. Imperative ML Quit
18. Imperative ML

19. Common Errors

20. Aliasing & References

21. Dangling References IIT Delhi

22. New Reference


S. Arun-Kumar, CSE
23. Imperative Commands: Basic

24. Imperative Commands: Compound Title Page

25. Predefined Compound Commands


Contents

JJ II

J I

Page 601 of 709

Go Back

Full Screen

Close

Quit
Imperative vs IIT Delhi

S. Arun-Kumar, CSE

Functional Title Page

• Functional Model Contents

• Memory/Store Model JJ II

J I
• Imperative Model
Page 602 of 709

• State Changes Go Back

• Accessing the store Full Screen

Close

Quit
Features of the Store
IIT Delhi

S. Arun-Kumar, CSE

Memory is treated as a datatype with Title Page

constructors Contents

Allocation ref : α → α ref JJ II

J I
Dereferencing ! : α ref → α
Page 603 of 709

Updation :=: α ref ∗ α → unit Go Back

Deallocation of memory is automatic! Full Screen

Close

Quit
References:
Experiments
- val a = ref 0; IIT Delhi

val a = ref 0 : int ref S. Arun-Kumar, CSE

Title Page

0 1 2 3
Contents
0
JJ II

J I

Page 604 of 709

Go Back

Full Screen

Close

Quit

a
References:
Experiments
- val b = ref 0; IIT Delhi

val b = ref 0 : int ref S. Arun-Kumar, CSE

Title Page

0 1 2 3
Contents
0
JJ II

J I

0 Page 605 of 709

Go Back

Full Screen

Close

Quit

a b
IIT Delhi

References: S. Arun-Kumar, CSE

Experiments Title Page

Contents

- a = b;
JJ II
val it = false : bool
J I
- !a = !b;
Page 606 of 709

val it = true : bool


Go Back

Full Screen

Close

Quit
Aliases
- val x = ref 0;
IIT Delhi

val x = ref 0 : int ref


S. Arun-Kumar, CSE

0 1 2 3 Title Page

0
Contents

JJ II
0
J I
0
Page 607 of 709

Go Back

Full Screen

Close

a b x Quit
References:
Experiments
- val y = x; IIT Delhi

val y = ref 0 : int ref S. Arun-Kumar, CSE

Title Page

0 1 2 3
Contents
0
JJ II

0 J I

0 Page 608 of 709

Go Back

Full Screen

Close

Quit

a b x y
References: Aliases
- x := !x + 1;
IIT Delhi

val it = () : unit
S. Arun-Kumar, CSE

0 1 2 3 Title Page

0
Contents

JJ II
1
J I
0
Page 609 of 709

Go Back

Full Screen

Close

a b x y Quit
IIT Delhi

References: S. Arun-Kumar, CSE

Experiments Title Page

Contents

- !y;
JJ II
val it = 1 : int
J I
- x = y;
Page 610 of 709

val it = true : bool


Go Back

Full Screen

Close

Quit
After Garbage
Collection IIT Delhi

GC #0.0.0.0.2.45: (0 ms) S. Arun-Kumar, CSE

Title Page
0 1 2 3

0 0 1 Contents

JJ II

J I

Page 611 of 709

Go Back

Full Screen

Close

y
Quit
a b x
Side Effects IIT Delhi

• Assignment does not produce a S. Arun-Kumar, CSE

value Title Page

Contents

• It produces only a state change


JJ II
(side effect)
J I

• But side-effects are compatible with Page 612 of 709

functional programming since it is Go Back

provided as a new data type with Full Screen

constructors and destructors.


Close

Quit
Imperative ML IIT Delhi

• Does not provide direct access to S. Arun-Kumar, CSE

memory addresses Title Page

• Does not allow for uninitialized im- Contents

perative variables JJ II

J I
• Provides a type with every memory
Page 613 of 709
location
Go Back

• Manages the memory completely Full Screen

automatically Close

Quit
Imperative ML
• Does not provide direct access to
IIT Delhi

memory addresses
S. Arun-Kumar, CSE

– Prevents the use of memory ad- Title Page

dresses as integers that can be Contents

manipulated by the user program JJ II

• Does not allow for uninitialized im- J I

perative variables Page 614 of 709

• Provides a type with every memory Go Back

location Full Screen

• Manages the memory completely Close

automatically Quit
Imperative ML
• Does not provide direct access to
IIT Delhi

memory addresses
S. Arun-Kumar, CSE

• Does not allow for uninitialized im- Title Page

perative variables Contents

– Most imperative languages keep JJ II

declarations separate from initial- J I

izations Page 615 of 709

• Provides a type with every memory Go Back

cell Full Screen

• Manages the memory completely Close

automatically Quit
Imperative ML
• Does not provide direct access to
IIT Delhi

memory addresses
S. Arun-Kumar, CSE

• Does not allow for uninitialized im- Title Page

perative variables Contents

– A frequent source of surprising JJ II

results in most imperative lan- J I

guage programs Page 616 of 709

• Provides a type with every memory Go Back

cell Full Screen

• Manages the memory completely Close

automatically Quit
Nasty Surprises
Separation of declaration from initial-
IIT Delhi

ization
S. Arun-Kumar, CSE

• Uninitialized variables Title Page

• Execution time errors if not de- Contents

tected by compiler, since every JJ II

memory location contains some J I

data Page 617 of 709

• Might use a value stored previously Go Back

in that location by some imperative Full Screen

variable that no longer exists. Close

• Errors due to type violations. Quit


Imperative ML IIT Delhi

• Does not provide direct access to S. Arun-Kumar, CSE

memory addresses Title Page

• Does not allow for uninitialized im- Contents

perative variables JJ II

J I
• Provides a type with every memory
Page 618 of 709
cell
Go Back

• Manages the memory completely Full Screen

automatically and securely. Close

Quit
Imperative ML
• Does not provide direct access to
memory addresses IIT Delhi

• Does not allow for uninitialized im- S. Arun-Kumar, CSE

perative variables Title Page

Contents

• Provides a type with every memory


JJ II
cell
J I
• Manages the memory completely Page 619 of 709

automatically and securely


Go Back

– Memory has to be managed by Full Screen

the user program in most lan- Close

guages Quit

– Prone to various errors


IIT Delhi

Common Errors S. Arun-Kumar, CSE

Title Page

• Memory access errors due to inte- Contents

ger arithmetic, especially in large JJ II

structures (arrays) J I

• Dangling references on dealloca- Page 620 of 709

tion of aliased memory Go Back

Full Screen

Close

Quit
Aliasing & References IIT Delhi

Before deallocation:
0 1 2 3 S. Arun-Kumar, CSE

0 0 1
Title Page

Contents

JJ II

J I

Page 621 of 709

Go Back

Full Screen

a b x y Close

Quit
Dangling References
Deallocate x through a system call
0 1 2 3
IIT Delhi
0 0
S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 622 of 709

Go Back

a b y Full Screen

Close

y is left dangling!
Quit
New Reference
val z = ref 12;
IIT Delhi
0 1 2 3

0 0 12 S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 623 of 709

Go Back

Full Screen
a b y z
Close

Quit

By sheer coincidence !y = 12
IIT Delhi

Imperative Commands: S. Arun-Kumar, CSE

Basic Title Page

A Command is an ML expression that Contents

creates a side effect and returns an JJ II

empty tuple (() : unit). J I

Page 624 of 709

Assignment
Go Back

print Full Screen

Close

Quit
Imperative Commands:
Compound IIT Delhi

S. Arun-Kumar, CSE

Any complex ML expression or func- Title Page

tion definition whose type is of the Contents

form α → unit is a compound com- JJ II

mand. J I

• Predefined ML compound com- Page 625 of 709

mands Go Back

• Could be user-defined. After all, ev- Full Screen

erything is a value! Close

Quit
Predefined Compound
Commands IIT Delhi

branching if e then c1 else c0.


S. Arun-Kumar, CSE

Title Page

cases case e of p1 ⇒ c1| · · · |pn ⇒ cn Contents

Sequencing (c1; c2; . . . ; cn). Sequenc- JJ II

ing is associative J I

looping while e do c1 is defined recur- Page 626 of 709

sively as Go Back

if e then (c1; while e do c1) else () Full Screen

Close

Quit
7.3. Arrays
1. Why Imperative
IIT Delhi
2. Arrays

3. Indexing Arrays S. Arun-Kumar, CSE

4. Indexing Arrays
Title Page
5. Indexing Arrays

6. Physical Addressing Contents

7. Arrays
JJ II
8. 2D Arrays

9. 2D Arrays: Indexing J I
10. Ordering of indices
Page 627 of 709
11. Arrays vs. Lists

12. Arrays: Physical


Go Back
13. Lists: Physical
Full Screen

Close

Quit
Why Imperative
IIT Delhi

• Historical reasons: Early machine


S. Arun-Kumar, CSE
instruction set.
Title Page

• Programming evolved from the ma- Contents

chine architecture.
JJ II

• Legacy software: J I

– numerical packages Page 628 of 709

– operating systems Go Back

• Are there any real benefits of imper- Full Screen

ative programming? Close

Quit
Arrays
An array of length n is a contiguous
sequence of n memory cells IIT Delhi

C0, C1, . . . , Cn−1 S. Arun-Kumar, CSE

Title Page

Contents

JJ II

C0 Ci J I

Cn−1 Page 629 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Indexing Arrays Title Page

For any array Contents

JJ II
• i, 0 ≤ i < n is the index of cell Ci.
J I
• Ci is at a distance of i cells away Page 630 of 709

from C0
Go Back

Full Screen

Close

Quit
Indexing Arrays
IIT Delhi

S. Arun-Kumar, CSE

Title Page

C0 Ci Contents

Cn−1 JJ II

J I

Page 631 of 709

Go Back

Full Screen

a0 a n−1 ai
Close

Quit
IIT Delhi

Indexing Arrays S. Arun-Kumar, CSE

Title Page

• The start address of the array and Contents

the address of C0 are the same (say JJ II


a0)
J I

• The address ai of cell Ci is Page 632 of 709

ai = a0 + i Go Back

Full Screen

Close

Quit
Physical Addressing
If each element occupies s physical IIT Delhi

memory locations, then S. Arun-Kumar, CSE

ai = a0 + i × s
Title Page

Contents

JJ II

J I

Page 633 of 709

Go Back

Full Screen

Close

a0 a n−1 ai Quit
Arrays
IIT Delhi

S. Arun-Kumar, CSE

A 2-dimensional array of Title Page

• r rows numbered 0 to r − 1 Contents

• each row containing c elements JJ II

numbered 0 to c − 1 J I

Page 634 of 709

is also a contiguous sequence of rc


Go Back
memory cells
Full Screen

Close

C0,0, C0,1, . . . , C0,c−1, C1,0, . . . , Cr−1,c−1


Quit
2D Arrays
A 2 dimensional-array is represented IIT Delhi

as an array of length r × c, where S. Arun-Kumar, CSE

• a00 is the start address of the array, Title Page

and Contents

• the address of the (i, j)-th cell is JJ II

given by J I

aij = a00 + (c × i + j) Page 635 of 709

Go Back

• the physical address of the (i, j)-th


Full Screen

cell is given by
Close
aij = a00 + (c × i + j) × s
Quit
2D Arrays: Indexing IIT Delhi

• The index (i, j) of a 2D array may S. Arun-Kumar, CSE

be thought of as being similar to a Title Page

2-digit number in base c Contents

• The successor of index (i, j) is the JJ II

successor of a number in base c i.e. J I

Page 636 of 709



(i + 1, 0) if j = n − 1 Go Back

succ(i, j) =
(i, j + 1) else Full Screen

Close

Quit
IIT Delhi

Ordering of indices S. Arun-Kumar, CSE

Title Page

There is a natural “<” ordering on in-


Contents
dices given by
JJ II

(i, j) < (k, l) ⇐⇒ J I

(i < k) or Page 637 of 709

(i = k and j < l) Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Arrays vs. Lists Title Page

Lists Arrays Contents

JJ II
Unbounded lengths Fixed length
Insertions possible Very complex J I

Indirect access Direct access Page 638 of 709

Go Back

Full Screen

Close

Quit
Arrays: Physical
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 639 of 709

Go Back

Full Screen

Close

a0 a n−1 ai Quit
Lists: Physical
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 640 of 709

Go Back

Full Screen

Close

Quit
8. A large Example: Tautology Checking
8.1. Large Example: Tautology Checking IIT Delhi

1. Logical Arguments
S. Arun-Kumar, CSE
2. Saintly and Rich

3. About Cats Title Page

4. About God
Contents
5. Russell’s Argument
6. Russell’s Argument JJ II
7. Russell’s Argument
J I
8. Russell’s Argument

9. Propositions Page 641 of 709

10. Compound Propositions


Go Back
11. Valuations

12. Valuations
Full Screen
13. Tautology

14. Properties Close

15. Negation Normal Form


Quit
16. Literals & Clauses

17. Conjunctive Normal Form

18. Validity

19. Logical Validity IIT Delhi

20. Validity & Tautologies


S. Arun-Kumar, CSE
21. Problem

22. Tautology Checking Title Page

23. Falsifying
Contents

JJ II

J I

Page 642 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

Logical Arguments S. Arun-Kumar, CSE

Title Page
Examples.
Contents

• Saintly and Rich JJ II

• About cats J I

• About God Page 643 of 709

• Russell’s argument Go Back

Full Screen

Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Saintly and Rich Title Page

Contents

hy1 The landed are rich. JJ II

hy2 One cannot be both saintly and rich. J I

conc The landed are not saintly Page 644 of 709

Go Back

Full Screen

Close

Quit
IIT Delhi

About Cats S. Arun-Kumar, CSE

hy1 Tame cats are non-violent and vege- Title Page

tarian. Contents

hy2 Non-violent cats would not kill mice. JJ II

J I
hy3 Vegetarian cats are bottle-fed.
Page 645 of 709

hy4 Cats eat meat. Go Back

conc Cats are not tame. Full Screen

Close

Quit
About God IIT Delhi

S. Arun-Kumar, CSE

hy1 God is omniscient and omnipotent.


Title Page

hy2 An omniscient being would know Contents

there is evil. JJ II

hy3 An omnipotent being would prevent J I

evil. Page 646 of 709

hy4 There is evil. Go Back

conc There is no God Full Screen

Close

Quit
Russell’s Argument
hy1 If we can directly know that God ex- IIT Delhi

ists, then we can know God exists by S. Arun-Kumar, CSE

experience. Title Page

Contents

hy2 If we can indirectly know that God ex-


JJ II
ists, then we can know God exists by
J I
logical inference from experience.
Page 647 of 709

hy3 If we can know that God exists, then Go Back

we can directly know that God exists, Full Screen

or we can indirectly know that God ex-


Close

ists.
Quit
Russell’s Argument
hy4 If we cannot know God empirically, IIT Delhi

then we cannot know God by experi- S. Arun-Kumar, CSE

ence and we cannot know God by logi- Title Page

cal inference from experience. Contents

JJ II
hy5 If we can know God empirically, then
“God exists” is a scientific hypothesis J I

and is empirically justifiable. Page 648 of 709

Go Back

hy6 “God exists” is not empirically justi-


Full Screen
fiable.
Close

conc We cannot know that God exists. Quit


Russell’s Argument
hy1 If we can directly know that God ex- IIT Delhi

ists, then we can know God exists by S. Arun-Kumar, CSE

experience. Title Page

Contents
hy2 If we can indirectly know that God ex-
JJ II
ists, then we can know God exists by
J I
logical inference from experience.
Page 649 of 709

hy3 If we can know that God exists, then


Go Back

(we can directly know that God exists,


Full Screen

or we can indirectly know that God ex-


Close

ists).
Quit
Russell’s Argument
hy4 If we cannot know God empirically, IIT Delhi

then (we cannot know God by experi- S. Arun-Kumar, CSE

ence and we cannot know God by log- Title Page

ical inference from experience.) Contents

hy5 If we can know God empirically, then JJ II

(“God exists” is a scientific hypothesis J I

and is empirically justifiable.) Page 650 of 709

Go Back

hy6 “God exists” is not empirically justi-


Full Screen
fiable.
Close

conc We cannot know that God exists. Quit


Propositions IIT Delhi

S. Arun-Kumar, CSE

A proposition is a sentence to which a Title Page

truth value may be assigned. Contents

In any real or imaginary world of facts JJ II

a proposition has a truth value, true or J I

false. Page 651 of 709

An atom is a simple proposition that Go Back

has no propositions as components. Full Screen

Close

Quit
Compound
Propositions IIT Delhi

Compound propositions may be S. Arun-Kumar, CSE

formed from atoms by using the fol- Title Page

lowing operators/constructors. Contents

JJ II

operator symbol J I

not ¬ Page 652 of 709

and ∧ Go Back

or ∨ Full Screen

if. . . then. . . ⇒ Close

equivalent ⇐⇒ Quit
IIT Delhi

Valuations S. Arun-Kumar, CSE

Given truth values to individual atoms Title Page

the truth values of compound proposi- Contents

tions are evaluated as follows: JJ II

p ¬p J I

true false Page 653 of 709

false true Go Back

Full Screen

Close

Quit
IIT Delhi

Valuations
S. Arun-Kumar, CSE

Title Page

p q p∧q p∨q p⇒q p ⇐⇒ q Contents

true true true true true true JJ II

true false false true false false J I

false true false true true false Page 654 of 709

false false false false true true Go Back

Full Screen

Close

Quit
Tautology IIT Delhi

S. Arun-Kumar, CSE

A (compound) proposition is a tautol-


Title Page

ogy if it is true regardless of what truth


Contents
values are assigned to its atoms.
JJ II
Examples.
J I

• p∨¬p Page 655 of 709

• (p∧q)⇒p Go Back

• (p∧¬p)⇒q Full Screen

Close

Quit
Properties
• Every proposition may be ex- IIT Delhi

pressed in a logically equivalent S. Arun-Kumar, CSE

form using only the operators ¬, ∧ Title Page

and ∨ Contents

(p ⇐⇒ q) = (p⇒q)∧(q⇒p)
JJ II

(p⇒q) = (¬p∨q) J I

• De Morgan’s laws may be applied Page 656 of 709

to push ¬ inward Go Back

¬(p∧q) = ¬p∨¬q Full Screen

Close

¬(p∨q) = ¬p∧¬q
Quit
Negation Normal Form IIT Delhi

S. Arun-Kumar, CSE

• Double negations may be removed Title Page

since Contents

¬¬p = p JJ II

• Every proposition may be ex- J I

pressed in a form containing only ∧ Page 657 of 709

and ∨ with ¬ appearing only in front Go Back

of atoms. Full Screen

Close

Quit
IIT Delhi

Literals & Clauses S. Arun-Kumar, CSE

Title Page

• A literal is either an atom or ¬ ap- Contents

plied to an atom JJ II

• ∨ is commutative and associative J I


Wm
• A clause is of the form j=1lj , Page 658 of 709

where each lj is a literal. Go Back

Full Screen

Close

Quit
Conjunctive Normal IIT Delhi

Form S. Arun-Kumar, CSE

Title Page

• ∨ may be distributed over ∧ Contents

p∨(q∧r) = (p∨q)∧(p∨r) JJ II

• ∧ is commutative and associative. J I

• Every proposition V may be ex- Page 659 of 709

pressed in the form ni=1qi, where Go Back

each qi is a clause. Full Screen

Close

Quit
IIT Delhi

Validity S. Arun-Kumar, CSE

• A logical argument consists of a Title Page

number of hypotheses and a single Contents

conclusion ([h1, . . . , hn]|c) JJ II

J I
• A logical argument is valid if the
Page 660 of 709
conclusion logically follows from the
hypotheses.
Go Back

Full Screen

Close

Quit
Logical Validity IIT Delhi

S. Arun-Kumar, CSE

The conclusion logically follows Title Page

from the given hypotheses if for Contents

any truth assignment to the atoms,


JJ II

either some hypothesis hi is false J I

or whenever every one of the hypothe- Page 661 of 709

ses is true the conclusion is also Go Back

true Full Screen

Close

Quit
Validity & Tautologies IIT Delhi

S. Arun-Kumar, CSE

• A tautology is a valid argument


Title Page
in which there is a conclusion
without any hypothesis.
Contents

JJ II
• A logical argument [h1, . . . , hn]|c, is J I
valid if and only if
Page 662 of 709

(h1∧ . . . ∧hn)⇒c Go Back

is a tautology Full Screen

Close

Quit
IIT Delhi

Problem S. Arun-Kumar, CSE

Given an argument [h1, . . . , hn]|c, Title Page

Contents

• determine whether (h1∧ . . . ∧hn)⇒c


JJ II
is a tautology, and
J I

• If it is not a tautology, to deter- Page 663 of 709

mine what truth assignments to the Go Back

atoms make it false. Full Screen

Close

Quit
Tautology Checking IIT Delhi
Vn
A proposition in CNF ( i=1pi) S. Arun-Kumar, CSE

• is a tautology if and only if every Title Page

proposition pi , 1 ≤ i ≤ m, is a tau- Contents

tology. JJ II

• otherwise at least one clause pi J I

must be false Page 664 of 709

Wm
• Clause pi = j=1lij is false
Go Back

if and only if every literal lij , 1 ≤ j ≤ Full Screen

m is false Close

Quit
Falsifying IIT Delhi

Vn
For a proposition in CNF ( i=1pi) that S. Arun-Kumar, CSE

is not a tautology Title Page

Wm
• A clause pi = j=1lij is false Contents

JJ II
• A truth assignment that falsifies the
J I
argument
Page 665 of 709

– sets the atoms that occur nega- Go Back

tively in pi to true,
Full Screen

– sets every other atom to false Close

Quit
8.2. Tautology Checking Contd.
1. Tautology Checking
IIT Delhi
2. Normal Forms

3. Top-down Development S. Arun-Kumar, CSE

4. The Signature
Title Page
5. The Core subproblem

6. The datatype Contents

7. Convert to CNF
JJ II
8. Rewrite into NNF

9. ⇐⇒ and ⇒ Elimination J I
10. Push ¬ inward
Page 666 of 709
11. Push ¬ inward

12. conj of disj


Go Back
13. Push ∨ inward

14. Tautology & Falsification Full Screen

Close

Quit
Tautology Checking IIT Delhi

S. Arun-Kumar, CSE

• Logical arguments Title Page

• Propositional forms Contents

• Propositions JJ II

J I
• Compound Propositions
Page 667 of 709

• Truth table Go Back

• Tautologies Full Screen

Close

Quit
Normal Forms
IIT Delhi

S. Arun-Kumar, CSE

• Properties Title Page

• Negation Normal Form Contents

JJ II
• Conjunctive Normal Forms
J I
• Valid Propositional Arguments as Page 668 of 709

tautologies
Go Back

• The problem Full Screen

Close

Quit
Top-down
Development IIT Delhi

• Transform the argument into a S. Arun-Kumar, CSE

single proposition. Title Page

• Transfom the single proposition into Contents

one in CNF JJ II

J I

• Check whether every clause is a Page 669 of 709

tautology Go Back

Full Screen

• If any clause is not a tautology, find


Close

the truth assignment(s) that make it


Quit

false
The Signature
signature PropLogic = IIT Delhi

sig datatype Prop = ?? S. Arun-Kumar, CSE

type Argument = Title Page

Prop list * Prop Contents

val falsifyArg : JJ II
Argument -> Prop list list
J I
val Valid:
Page 670 of 709

Argument -> bool *


Go Back

Prop list list


Full Screen
...
Close
end;
Quit
The Core subproblem IIT Delhi

S. Arun-Kumar, CSE

• Representing propositions Title Page

• Transformation of propositions into Contents

CNF JJ II

– Transform into Negation Normal J I

Form (NNF) Page 671 of 709

– Transform NNF into Conjunctive Go Back

Normal Form (CNF) Full Screen

Close

Quit
The datatype IIT Delhi

datatype Prop = S. Arun-Kumar, CSE

ATOM of string | Title Page

NOT of Prop | Contents

AND of Prop * Prop | JJ II

OR of Prop * Prop | J I

IMP of Prop * Prop | Page 672 of 709

EQL of Prop * Prop Go Back

Full Screen

Close

Quit
Convert to CNF
IIT Delhi

Convert a given proposition into CNF


S. Arun-Kumar, CSE

fun cnf (P) =


Title Page

conj_of_disj (
Contents

nnf (rewrite (P)));


JJ II

J I
where
Page 673 of 709

• rewrite eliminates ⇐⇒ and ⇒ Go Back

• nnf converts into NNF Full Screen

• conj of disj converts into CNF Close

Quit
IIT Delhi

S. Arun-Kumar, CSE

Rewrite into NNF Title Page

• Eliminate ⇐⇒ and then ⇒ Contents

JJ II
• Push ¬ inward using De
J I
Morgan’s laws and eliminate
double negations. Page 674 of 709

Go Back

Full Screen

Close

Quit
⇐⇒ and ⇒ Elimination IIT Delhi

fun rewrite (ATOM a) = ATOM a S. Arun-Kumar, CSE

| rewrite (IMP (P, Q)) = Title Page

OR (NOT (rewrite(P)), Contents

rewrite(Q))
JJ II
| rewrite (EQL (P, Q)) =
J I
rewrite (AND (IMP(P, Q),
Page 675 of 709

IMP (Q, P)))


Go Back
| ...
Full Screen

Proposition made up of only ¬, ∧ and


∨. Close

Quit
IIT Delhi

Push ¬ inward S. Arun-Kumar, CSE

fun nnf (ATOM a) = Title Page

ATOM a Contents

| nnf (NOT (ATOM a)) = JJ II

NOT (ATOM a) J I

| nnf (NOT (NOT (P))) = Page 676 of 709

nnf (P) Go Back

Full Screen

Close

Quit
Push ¬ inward IIT Delhi

| nnf (NOT (AND (P, Q))) = S. Arun-Kumar, CSE

nnf (OR (NOT (P), Title Page

NOT (Q))) Contents

| nnf (NOT (OR (P, Q))) = JJ II

nnf (AND (NOT (P), J I

NOT (Q))) Page 677 of 709

| ... Go Back

Proposition made up of only ∧ and ∨ Full Screen

applied to positive or negative literals. Close

Quit
conj of disj IIT Delhi

fun conj_of_disj (AND (P, Q)) = S. Arun-Kumar, CSE

AND (conj_of_disj (P), Title Page

conj_of_disj (Q)) Contents

| conj_of_disj (OR (P, Q)) = JJ II

distOR (conj_of_disj (P), J I

conj_of_disj (Q)) Page 678 of 709

| conj_of_disj (P) = P Go Back

Full Screen

where distOR is Close

Quit
Push ∨ inward IIT Delhi

S. Arun-Kumar, CSE

Use distributivity of ∨ over ∧


Title Page

fun distOR (P, AND (Q, R)) =


Contents

AND (distOR (P, Q),


JJ II
distOR (P, R))
J I
| distOR (AND (Q, R), P) =
AND (distOR (Q, P), Page 679 of 709

distOR (R, P)) Go Back

| distOR (P, Q)= OR (P, Q) Full Screen

Close

Quit
Tautology &
Falsification IIT Delhi

Falsifying a proposition S. Arun-Kumar, CSE

• A proposition Q in CNF, not a tau- Title Page

tology if and only if at least one of Contents

the clauses can be made false, by JJ II

a suitable truth assignment J I

• The list of atoms which are set true Page 680 of 709

to falsify a clause is called a falsifier. Go Back

Full Screen

• A proposition is a tautology
Close

if and only if there is no falsifier!


Quit
9. Lecture-wise Index to Slides
IIT Delhi
Index

Introduction to Computing (3-8) S. Arun-Kumar, CSE

1. Introduction
Title Page
2. Computing tools
3. Ruler and Compass Contents
4. Computing and Computers
5. Primitives JJ II
6. Algorithm
7. Problem: Doubling a Square J I

8. Solution: Doubling a Square


Page 681 of 709
9. Execution: Step 1
10. Execution: Step 2 Go Back
11. Doubling a Square: Justified
12. Refinement: Square Construction Full Screen

13. Refinement 2: Perpendicular at a point


Close
14. Solution: Perpendicular at a point
15. Perpendicular at a point: Justification
Quit
Next: Our Computing Tool

Our Computing Tool (9-33)

Previous: Introduction to Computing IIT Delhi

1. The Digital Computer: Our Computing Tool


S. Arun-Kumar, CSE
2. Algorithms
3. Programming Language
Title Page
4. Programs and Languages
5. Programs Contents

6. Programming
7. Computing Models JJ II

8. Primitives
J I
9. Primitive expressions
10. Methods of combination Page 682 of 709
11. Methods of abstraction
12. The Functional Model Go Back

13. Mathematical Notation 1: Factorial


Full Screen
14. Mathematical Notation 2: Factorial
15. Mathematical Notation 3: Factorial
Close
16. A Functional Program: Factorial
17. A Computation: Factorial Quit
18. A Computation: Factorial
19. A Computation: Factorial
20. A Computation: Factorial
21. A Computation: Factorial IIT Delhi
22. A Computation: Factorial
23. A Computation: Factorial S. Arun-Kumar, CSE

24. Standard ML
25. SML: Important Features Title Page

Next: Primitives: Integer & Real Contents

Primitives: Integer & Real (34-60) JJ II

Previous: Primitives: Integer & Real


J I
1. Algorithms & Programs
2. SML: Primitive Integer Operations 1 Page 683 of 709

3. SML: Primitive Integer Operations 1


Go Back
4. SML: Primitive Integer Operations 1
5. SML: Primitive Integer Operations 1
Full Screen
6. SML: Primitive Integer Operations 1
7. SML: Primitive Integer Operations 1 Close
8. SML: Primitive Integer Operations 2
9. SML: Primitive Integer Operations 2 Quit
10. SML: Primitive Integer Operations 2
11. SML: Primitive Integer Operations 2
12. SML: Primitive Integer Operations 2
13. SML: Primitive Integer Operations 3 IIT Delhi
14. SML: Primitive Integer Operations 3
15. SML: Primitive Integer Operations 3 S. Arun-Kumar, CSE

16. SML: Primitive Integer Operations 3


Title Page
17. SML: Primitive Integer Operations 3
18. Quotient & Remainder
Contents
19. SML: Primitive Real Operations 1
20. SML: Primitive Real Operations 1 JJ II
21. SML: Primitive Real Operations 1
22. SML: Primitive Real Operations 2 J I

23. SML: Primitive Real Operations 3


Page 684 of 709
24. SML: Primitive Real Operations 4
25. SML: Precision
Go Back
26. Fibonacci Numbers
27. Euclidean Algorithm Full Screen

Next: Technical Completeness & Algorithms Close

Technical Completeness & Algorithms (61-92)


Quit
1. Recapitulation: Integers & Real
2. Recap: Integer Operations
3. Recapitulation: Real Operations
4. Recapitulation: Simple Algorithms
IIT Delhi
5. More Algorithms
6. Powering: Math S. Arun-Kumar, CSE

7. Powering: SML
8. Technical completeness Title Page

9. What SML says


Contents
10. Technical completeness
11. What SML says ... contd
JJ II
12. Powering: Math 1
13. Powering: SML 1 J I
14. Technical Completness
15. What SML says Page 685 of 709

16. Powering: Integer Version


Go Back
17. Exceptions: A new primitive
18. Integer Power: SML
Full Screen
19. Integer Square Root 1
20. Integer Square Root 2 Close
21. An analysis
22. Algorithmic idea Quit
23. Algorithm: isqrt
24. Algorithm: shrink
25. SML: shrink
26. SML: intsqrt IIT Delhi
27. Run it!
28. SML: Reorganizing Code S. Arun-Kumar, CSE

29. Intsqrt: Reorganized


Title Page
30. shrink: Another algorithm
31. Shrink2: SML
Contents
32. Shrink2: SML ... contd
Algorithm Refinement (93-122) JJ II

1. Recap: More Algorithms


J I
2. Recap: Power
3. Recap: Technical completeness Page 686 of 709

4. Recap: More Algorithms


5. Intsqrt: Reorganized Go Back

6. Intsqrt: Reorganized
Full Screen
7. Some More Algorithms
8. Combinations: Math
Close
9. Combinations: Details
10. Combinations: SML Quit
11. Perfect Numbers
12. Refinement
13. Perfect Numbers: SML
Pu
14. l if divisor(k) IIT Delhi
15. SML: sum divisors
16. if divisor and ifdivisor S. Arun-Kumar, CSE

17. SML: Assembly 1


Title Page
18. SML: Assembly 2
19. Perfect Numbers . . . contd.
Contents
20. Perfect Numbers . . . contd.
21. SML: Assembly 3 JJ II
22. Perfect Numbers: Run
23. Perfect Numbers: Run J I

24. SML: Code variations


Page 687 of 709
25. SML: Code variations
26. SML: Code variations
Go Back
27. Summation: Generalizations
28. Algorithmic Improvements: Full Screen

29. Algorithmic Variations


30. Algorithmic Variations Close

Variations: Algorithms & Code (123-149) Quit


1. Recap
2. Recap: Combinations
3. Combinations 1
4. Combinations 2
IIT Delhi
5. Combinations 3
6. Perfect 2 S. Arun-Kumar, CSE

7. Power 2
8. A Faster Power Title Page

9. Power2: Complete
Contents
10. Power2: SML
11. Power2: SML
JJ II
12. Computation: Issues
13. General Correctness J I
14. Code: Justification
15. Recall Page 688 of 709

16. Features: Definition before Use


Go Back
17. Run ifdivisor
18. Diagnosis: Features of programs
Full Screen
19. Back to Math
20. Incorrectness Close
21. ifdivisor3
22. Run it! Quit
23. Try it!
24. Hey! Wait a minute!
25. The n’s
26. Scope IIT Delhi
27. Scope Rules
S. Arun-Kumar, CSE
Names, Scopes & Recursion (150-181)
1. Disjoint Scopes Title Page
2. Nested Scopes
3. Overlapping Scopes Contents

4. Spannning
5. Scope & Names JJ II

6. Names & References


J I
7. Names & References
8. Names & References Page 689 of 709

9. Names & References


10. Names & References Go Back

11. Names & References


Full Screen
12. Names & References
13. Names & References
Close
14. Names & References
15. Names & References Quit
16. Definition of Names
17. Use of Names
18. local...in...end
19. local...in...end IIT Delhi
20. local...in...end
21. local...in...end S. Arun-Kumar, CSE

22. Scope & local


Title Page
23. Computations: Simple
24. Simple computations
Contents
25. Computations: Composition
26. Composition: Alternative JJ II
27. Compositions: Compare
28. Compositions: Compare J I

29. Computations: Composition


Page 690 of 709
30. Recursion
31. Recursion: Left
Go Back
32. Recursion: Right
Floating Point (182-201) Full Screen

1. So Far-1: Computing
Close
2. So Far-2: Algorithms & Programs
3. So far-3: Top-down Design Quit
4. So Far-4: Algorithms to Programs
5. So far-5: Caveats
6. So Far-6: Algorithmic Variations
7. So Far-7: Computations IIT Delhi
8. Floating Point
9. Real Operations S. Arun-Kumar, CSE

10. Real Arithmetic


Title Page
11. Numerical Methods
12. Errors
Contents
13. Errors
14. Infinite Series JJ II
15. Truncation Errors
16. Equation Solving J I

17. Root Finding-1


Page 691 of 709
18. Root Finding-2
19. Root Finding-3
Go Back
20. Root Finding-4
Root Finding, Composition and Recursion (202-229) Full Screen

1. Root Finding: Newton’s Method


Close
2. Root Finding: Newton’s Method
3. Root Finding: Newton’s Method Quit
4. Root Finding: Newton’s Method
5. Root Finding: Newton’s Method
6. Root Finding: Newton’s Method
7. Newton’s Method: Basis
IIT Delhi
8. Newton’s Method: Basis
9. Newton’ Method: Algorithm S. Arun-Kumar, CSE

10. What can go wrong!-1


11. What can go wrong!-2 Title Page

12. What can go wrong!-2


Contents
13. What can go wrong!-3
14. What can go wrong!-4
JJ II
15. Real Computations & Induction: 1
16. Real Computations & Induction: 2 J I
17. What’s it good for? 1
18. What’s it good for? 2 Page 692 of 709

19. newton: Computation


Go Back
20. Generalized Composition
21. Two Computations of h(1)
Full Screen
22. Two Computations of h(−1)
23. Recursive Computations Close
24. Recursion: Left
25. Recursion: Right Quit
26. Recursion: Nonlinear
27. Some Practical Questions
28. Some Practical Questions
Termination and Space Complexity (230-266) IIT Delhi

1. Recursion Revisited
S. Arun-Kumar, CSE
2. Linear Recursion: Waxing
3. Recursion: Waning Title Page
4. Nonlinear Recursions
5. Fibonacci: contd Contents

6. Recursion: Waxing & Waning


7. Unfolding Recursion JJ II

8. Non-termination
J I
9. Termination
10. Proofs of termination Page 693 of 709

11. Proofs of termination: Induction


12. Proof of termination: Factorial Go Back

13. Proof of termination: Factorial


Full Screen
14. Fibonacci: Termination
15. GCD computations
Close
16. Well-foundedness: GCD
17. Well-foundedness Quit
18. Induction is Well-founded
19. Induction is Well-founded
20. Where it doesn’t work
21. Well-foundedness is inductive IIT Delhi
22. Well-foundedness is inductive
23. GCD: Well-foundedness S. Arun-Kumar, CSE

24. Newton: Well-foundedness


Title Page
25. Newton: Well-foundedness
26. Example: Zero
Contents
27. Questions
28. The Collatz Problem JJ II
29. Questions
30. Space Complexity J I

31. Newton & Euclid: Absolute


Page 694 of 709
32. Newton & Euclid: Relative
33. Deriving space requirements
Go Back
34. GCD: Space
35. Factorial: Space Full Screen

36. Fibonacci: Space


37. Fibonacci: Space Close

Efficiency Measures and Speed-ups (267-295) Quit


1. Recapitulation
2. Recapitulation
3. Time & Space Complexity
4. isqrt: Space
IIT Delhi
5. Time Complexity
6. isqrt: Time Complexity S. Arun-Kumar, CSE

7. isqrt2: Time
8. shrink vs. shrink2: Times Title Page

9. Factorial: Time Complexity


Contents
10. Fibonacci: Time Complexity
11. Comparative Complexity
JJ II
12. Comparisons
13. Comparisons J I
14. Efficiency Measures: Time
15. Efficiency Measures: Space Page 695 of 709

16. Speeding Up: 1


Go Back
17. Speeding Up: 2
18. Factoring out calculations
Full Screen
19. Tail Recursion: 1
20. Tail Recursion: 2 Close
21. Factorial: Tail Recursion
22. Factorial: Tail Recursion Quit
23. A Computation
24. Factorial: Issues
25. Fibonacci: Tail Recursion
26. Fibonacci: Tail Recursion IIT Delhi
27. fibTR: SML
28. State in Tail Recursion S. Arun-Kumar, CSE

29. Invariance
Title Page
Invariance & Correctness (296-319)
1. Recap Contents

2. Recursion Transformation
3. Tail Recursion: Examples JJ II

4. Comparisons
J I
5. Transformation Issues
6. Correctness Issues 1 Page 696 of 709

7. Correctness Issues 2
8. Correctness Theorem Go Back

9. Invariants & Correctness 1


Full Screen
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr
Close
12. Invariance: Example
13. Invariance: Example Quit
14. Proof
15. Invariance Lemma: f ib iter
16. Proof
17. Correctness: Fibonacci IIT Delhi
18. Variants & Invariants
19. Variants & Invariants S. Arun-Kumar, CSE

20. More Invariants


Title Page
21. Fast Powering 1
22. Fast Powering 2
Contents
23. Root Finding: Bisection
24. Advantage Bisection JJ II
Tuples, Lists & the Generation of Primes (320-341)
J I
1. Recap: Tail Recursion
2. Examples: Invariants Page 697 of 709

3. Tuples
4. Lists Go Back

5. New Lists
Full Screen
6. List Operations
7. List Operations: cons
Close
8. Generating Primes upto n
9. More Properties Quit
10. Composites
11. Odd Primes
12. primesU pto(n)
13. generateF rom(P, m, n, k) IIT Delhi
14. generateF rom
15. primeW RT (m, P ) S. Arun-Kumar, CSE

16. primeW RT (m, P )


Title Page
17. primeW RT
18. Density of Primes
Contents
19. The Prime Number Theorem
20. The Prime Number Theorem JJ II
21. Complexity
22. Diagnosis J I

Compound Data & Lists (342-367)


Page 698 of 709

1. Compound Data
2. Recap: Tuples Go Back

3. Tuple: Formation
Full Screen
4. Tuples: Selection
5. Tuples: Equality
Close
6. Tuples: Equality errors
7. Lists: Recap Quit
8. Lists: Append
9. cons vs. @
10. Lists of Functions
11. Lists of Functions IIT Delhi
12. Arithmetic Sequences
13. Tail Recursion S. Arun-Kumar, CSE

14. Tail Recursion Invariant


Title Page
15. Tail Recursion
16. Another Tail Recursion: AS3
Contents
17. Another Tail Recursion: AS3 iter
18. AS3: Complexity JJ II
19. Generating Primes: 2
20. primes2U pto(n) J I

21. generate2F rom(P, m, n, k)


Page 699 of 709
22. generate2F rom
23. prime2W RT (m, P )
Go Back
24. prime2W RT
25. primes2: Complexity Full Screen

26. primes2: Diagnosis


Close
Compound Data & List Algorithms (368-394)
1. Compound Data: Summary Quit
2. Records: Constructors
3. Records: Example 1
4. Records: Example 2
5. Records: Destructors
IIT Delhi
6. Records: Equality
7. Tuples & Records S. Arun-Kumar, CSE

8. Back to Lists
9. Lists: Correctness Title Page

10. Lists: Case Analysis


Contents
11. Lists: Correctness by Cases
12. List-functions: length
JJ II
13. List Functions: search
14. List Functions: search2 J I
15. List Functions: ordered
16. List Functions:insert Page 700 of 709

17. List Functions: reverse


Go Back
18. List Functions: reverse2
19. List Functions:merge
Full Screen
20. List Functions:merge
21. List Functions:merge contd. Close
22. ML: merge
23. Sorting by Insertion Quit
24. Sorting by Merging
25. Sorting by Merging
26. Functions as Data
27. Higher Order Functions IIT Delhi

Higher Order Functions (395-419)


S. Arun-Kumar, CSE
1. Summary: Compound Data
2. List: Examples Title Page
3. Lists: Sorting
4. Higher Order Functions Contents

5. An Example
6. Currying JJ II

7. Currying: Contd
J I
8. Generalization
9. Generalization: 2 Page 701 of 709

10. Applying a list


11. Trying it out Go Back

12. Associativity
Full Screen
13. Apply to a list
14. Sequences
Close
15. Further Generalization
16. Further Generalization Quit
17. Sequences
18. Efficient Generalization
19. Sequences: 2
20. More Generalizations IIT Delhi
21. More Summations
22. Or Maybe . . . Products S. Arun-Kumar, CSE
N
23. Or Some Other
Title Page
N
24. Other
25. Examples of ⊗, e
Contents
Structured Data (420-451)
1. Transpose of a Matrix JJ II

2. Transpose: 0
J I
3. Transpose: 10
4. Transpose: 01 Page 702 of 709

5. Transpose: 20
6. Transpose: 02 Go Back

7. Transpose: 30
Full Screen
8. Transpose: 03
9. trans
Close
10. is2DM atrix
11. User Defined Types Quit
12. Enumeration Types
13. User Defined Structural Types
14. Functions vs. data
15. Data as 0-ary Functions IIT Delhi
16. Data vs. Functions
17. Data vs. Functions: Recursion S. Arun-Kumar, CSE

18. Lists
Title Page
19. Constructors
20. Shapes
Contents
21. Shapes: Triangle Inequality
22. Shapes: Area JJ II
23. Shapes: Area
24. ML: Try out J I

25. ML: Try out (contd.)


Page 703 of 709
26. Enumeration Types
27. Recursive Data Types
Go Back
28. Resistors: Datatype
29. Resistors: Equivalent Full Screen

30. Resistors
31. Resistors: Example Close

32. Resistors: ML session


Quit
User Defined Structured Data Types (452-472)
1. User Defined Types
2. Resistors: Grouping
3. Resistors: In Pairs IIT Delhi

4. Resistor: Values
S. Arun-Kumar, CSE
5. Resistance Expressions
6. Resistance Expressions
Title Page
7. Arithmetic Expressions
8. Arithmetic Expressions: 0 Contents
9. Arithmetic Expressions: 1
10. Arithmetic Expressions: 2 JJ II
11. Arithmetic Expressions: 3
J I
12. Arithmetic Expressions: 4
13. Arithmetic Expressions: 5
Page 704 of 709
14. Arithmetic Expressions: 6
15. Arithmetic Expressions: 7 Go Back

16. Arithmetic Expressions: 8


17. Binary Trees Full Screen

18. Arithmetic Expressions: 0


Close
19. Trees: Traversals
20. Recursive Data Types: Correctness Quit
21. Data Types: Correctness
Introducing a Memory Model (473-499)
1. Summary: Functional Model
2. CPU & Memory: Simplified IIT Delhi

3. Resource Management
S. Arun-Kumar, CSE
4. Shell: User Interface
5. GUI: User Interface Title Page
6. Memory Model: Simplified
7. Memory Contents

8. The Imperative Model


9. State Changes: σ JJ II

10. State
J I
11. State Changes
12. State Changes: σ Page 705 of 709

13. State Changes: σ1


14. State Changes: σ2 Go Back

15. Languages
Full Screen
16. User Programs
17. Imperative Languages
Close
18. Imperative vs Functional Variables
19. Assignment Commands Quit
20. Assignment Commands
21. Assignment Commands
22. Assignment Commands
23. Assignment Commands IIT Delhi
24. Assignment Commands: Swap
25. Swap S. Arun-Kumar, CSE

26. Swap
Title Page
27. Swap
Imperative Programming: (500-524) Contents

1. Imperative vs Functional
2. Features of the Store JJ II

3. References: Experiments
J I
4. References: Experiments
5. References: Experiments Page 706 of 709

6. Aliases
7. References: Experiments Go Back

8. References: Aliases
Full Screen
9. References: Experiments
10. After Garbage Collection
Close
11. Side Effects
12. Imperative ML Quit
13. Imperative ML
14. Imperative ML
15. Imperative ML
16. Nasty Surprises IIT Delhi
17. Imperative ML
18. Imperative ML S. Arun-Kumar, CSE

19. Common Errors


Title Page
20. Aliasing & References
21. Dangling References
Contents
22. New Reference
23. Imperative Commands: Basic JJ II
24. Imperative Commands: Compound
25. Predefined Compound Commands J I

Arrays (525-537)
Page 707 of 709

1. Why Imperative
2. Arrays Go Back

3. Indexing Arrays
Full Screen
4. Indexing Arrays
5. Indexing Arrays
Close
6. Physical Addressing
7. Arrays Quit
8. 2D Arrays
9. 2D Arrays: Indexing
10. Ordering of indices
11. Arrays vs. Lists IIT Delhi
12. Arrays: Physical
13. Lists: Physical S. Arun-Kumar, CSE

Large Example: Tautology Checking (538-560)


Title Page
1. Logical Arguments
2. Saintly and Rich Contents

3. About Cats
4. About God JJ II

5. Russell’s Argument
J I
6. Russell’s Argument
7. Russell’s Argument Page 708 of 709

8. Russell’s Argument
9. Propositions Go Back

10. Compound Propositions


Full Screen
11. Valuations
12. Valuations
Close
13. Tautology
14. Properties Quit
15. Negation Normal Form
16. Literals & Clauses
17. Conjunctive Normal Form
18. Validity IIT Delhi
19. Logical Validity
20. Validity & Tautologies S. Arun-Kumar, CSE

21. Problem
Title Page
22. Tautology Checking
23. Falsifying
Contents
Tautology Checking Contd. (561-574)
1. Tautology Checking JJ II

2. Normal Forms
J I
3. Top-down Development
4. The Signature Page 709 of 709

5. The Core subproblem


6. The datatype Go Back

7. Convert to CNF
Full Screen
8. Rewrite into NNF
9. ⇐⇒ and ⇒ Elimination
Close
10. Push ¬ inward
11. Push ¬ inward Quit
12. conj of disj
13. Push ∨ inward
14. Tautology & Falsification
IIT Delhi

S. Arun-Kumar, CSE

Title Page

Contents

JJ II

J I

Page 710 of 709

Go Back

Full Screen

Close

Quit

You might also like