You are on page 1of 644

Computing: The . . .

Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

Introduction to Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Computer Science Lecture-wise Index to . . .

Home Page

Title Page

S. Arun-Kumar JJ II

sak@cse.iitd.ernet.in J I

Department of Computer Science and Engineering Page 1 of 644

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

February 16, 2006 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1. Computing: The Functional Way
1.1. Introduction to Computing
1. Introduction Computing: The . . .
Algorithms: Design & . . .
2. Computing tools
Introducing Reals

3. Ruler and Compass Correctness, . . .


Compound Data
4. Computing and Computers Higher Order Functions . . .

5. Primitives Imperative . . .
A large Example: . . .
6. Algorithm Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 2 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Introduction Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• This course is about computing Higher Order Functions . . .


Imperative . . .

• Computing as a process is nearly


A large Example: . . .
Lecture-wise Index to . . .

as fundamental as arithmetic Home Page

Title Page

• Computing as a mental process JJ II

• Computing may be done with a va- J I

riety of tools which may or may not Page 3 of 644

Go Back

assist the mind Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Computing tools
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

• Sticks and stones (counting) Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Paper and pencil (an aid to mental Lecture-wise Index to . . .

Home Page

computing) Title Page

• abacus (still used in Japan!) JJ II

J I
• slide rules (ask a retired engineer!) Page 4 of 644

• ruler and compass Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ruler and Compass
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Actually it is a computing tool! Compound Data


Higher Order Functions . . .
Imperative . . .

• Construct a length that is half of a A large Example: . . .


Lecture-wise Index to . . .

given length Home Page

Title Page
• Bisect an angle JJ II

• Construct a square that is twice the J I

area of a given square Page 5 of 644

√ Go Back

• Construct 10 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing and
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Computers
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• Computing is much more funda- A large Example: . . .


Lecture-wise Index to . . .

mental Home Page

Title Page

• Computing may be done without a JJ II

computer too! J I

• But a Computer cannot do much Page 6 of 644

Go Back
besides computing. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitives
• Each tool has a set of capabilities Computing: The . . .
Algorithms: Design & . . .

called primitive operations or primi- Introducing Reals


Correctness, . . .

tives Compound Data


Higher Order Functions . . .
Imperative . . .

Ruler: Can specify lengths, lines A large Example: . . .


Lecture-wise Index to . . .

Compass: Can define arcs and cir- Home Page

cles Title Page

JJ II

• The primitives may be combined in J I

various ways to perform a computa- Page 7 of 644

tion. Go Back

Full Screen

• Example Constructing a right bisec- Close

tor of a given line segment. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Algorithm Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Given a problem to be solved with a Imperative . . .
A large Example: . . .

given tool, the attempt is to evolve a Lecture-wise Index to . . .

combination of primitives of the tool in Home Page

Title Page

a certain order to solve the problem. JJ II

An explicit statement of this combina- J I

tion along with the order is an algo- Page 8 of 644

rithm Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1.2. Our Computing Tool
Computing: The . . .
1. The Digital Computer: Our Computing Tool
Algorithms: Design & . . .
2. Algorithms Introducing Reals
Correctness, . . .
3. Programming Language
Compound Data
4. Programs and Languages Higher Order Functions . . .
Imperative . . .
5. Programs
A large Example: . . .
6. Programming Lecture-wise Index to . . .

7. Computing Models Home Page

8. Primitives
Title Page
9. Primitive expressions
JJ II
10. Methods of combination
11. Methods of abstraction J I

12. The Functional Model


Page 9 of 644

13. Mathematical Notation 1: Factorial


Go Back
14. Mathematical Notation 2: Factorial
15. Mathematical Notation 3: Factorial Full Screen

16. A Functional Program: Factorial Close

17. A Computation: Factorial


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. A Computation: Factorial
19. A Computation: Factorial
20. A Computation: Factorial
Computing: The . . .

21. A Computation: Factorial Algorithms: Design & . . .


Introducing Reals
22. A Computation: Factorial Correctness, . . .

23. A Computation: Factorial Compound Data


Higher Order Functions . . .
24. Standard ML Imperative . . .

25. SML: Important Features A large Example: . . .


Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 10 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Digital Computer:
Our Computing Tool Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Algorithm: A finite specification of the Compound Data


Higher Order Functions . . .

solution to a given problem using the Imperative . . .


A large Example: . . .

primitives of the computing tool. Lecture-wise Index to . . .

Home Page

• It specifies a definite input and out- Title Page

put JJ II

J I
• It is unambiguous Page 11 of 644

• It specifies a solution as a finite pro- Go Back

cess i.e. the number of steps in the Full Screen

computation is finite Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithms
An algorithm will be written in a mix- Computing: The . . .
Algorithms: Design & . . .

ture of English and standard mathe- Introducing Reals


Correctness, . . .

matical notation. Usually,


Compound Data
Higher Order Functions . . .
Imperative . . .

• Algorithms written in a natural lan- A large Example: . . .


Lecture-wise Index to . . .

guage are often ambiguous Home Page

Title Page

• mathematical notation is not am- JJ II

biguous, but still cannot be under- J I

stood by machine Page 12 of 644

• algorithms written by us use various


Go Back

Full Screen

mathematical properties. We know Close

them, but the machine doesn’t. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programming
Language Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Require a way to communicate with Compound Data


Higher Order Functions . . .

a machine which has essentially no Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
intelligence or understanding. Home Page

• Translate the algorithm into a form Title Page

that may be “understood” by a ma- JJ II

J I
chine
Page 13 of 644

• This “form” is usually a program Go Back

Program: An algorithm written in a Full Screen

programming language. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programs and
Languages Computing: The . . .
Algorithms: Design & . . .

• Every programming language has a Introducing Reals


Correctness, . . .
Compound Data

well defined vocabulary and a well Higher Order Functions . . .


Imperative . . .

defined grammar A large Example: . . .


Lecture-wise Index to . . .

• Each program has to be written fol- Home Page

Title Page
lowing rigid grammatical rules
JJ II

• A programming language and the J I

computer together form our single Page 14 of 644

computing tool Go Back

Full Screen

• Each program uses only the primi- Close

tives of the computing tool Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programs Computing: The . . .

Program: An algorithm written in the


Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

grammar of a programming language. Compound Data


Higher Order Functions . . .
Imperative . . .

A grammar is a set of rules for forming A large Example: . . .


Lecture-wise Index to . . .

sentences in a language. Home Page

Title Page

Each programming language also has JJ II

its own vocabulary and grammar just J I

as in the case of natural languages. Page 15 of 644

Go Back

We will learn the grammar of the lan- Full Screen

guage as we go along. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programming
Computing: The . . .
The act of writing programs and test- Algorithms: Design & . . .
Introducing Reals

ing them is programming Correctness, . . .


Compound Data
Higher Order Functions . . .

Even though most programming lan-


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

guages use essentially the same Home Page

computing primitives, each program- Title Page

ming language needs to be learned. JJ II

J I

Programming languages differ from Page 16 of 644

each other in terms of the conve- Go Back

nience and facilties they offer even Full Screen

though they are all equally powerful. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing Models
We consider mainly two models. Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Functional: A program is specified Correctness, . . .


Compound Data

simply as a mathematical expres- Higher Order Functions . . .


Imperative . . .
A large Example: . . .

sion Lecture-wise Index to . . .

Home Page

• Imperative: A program is specified Title Page

by a sequence of commands to be JJ II

executed. J I

Page 17 of 644

Programming languages also come Go Back

mainly in these two flavours. We Full Screen

will often identify the computing model Close

with the programming language. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitives Computing: The . . .
Algorithms: Design & . . .

Every programming language offers Introducing Reals


Correctness, . . .
Compound Data

the following capabilities to define and Higher Order Functions . . .


Imperative . . .

use: A large Example: . . .


Lecture-wise Index to . . .

• Primitive expressions and data Home Page

Title Page

• Methods of combination of expres- JJ II

sions and data J I

• Methods of abstraction of both ex- Page 18 of 644

Go Back

pressions and data Full Screen

The functional model Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitive expressions
The simplest objects and operations Computing: The . . .

in the computing model. These in- Algorithms: Design & . . .


Introducing Reals

clude Correctness, . . .
Compound Data
Higher Order Functions . . .

• basic data elements: numbers, Imperative . . .


A large Example: . . .

characters, truth values etc.


Lecture-wise Index to . . .

Home Page

• basic operations on the data ele- Title Page

ments: addition, substraction, mul- JJ II

tiplication, division, boolean opera- J I

Page 19 of 644

tions, string operations etc. Go Back

• a naming mechanism for various Full Screen

quantitities and expressions to be Close

used without repeating definitions Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Methods of Correctness, . . .
Compound Data

combination Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Means of combining simple expres- Home Page

sions and objects to obtain more com- Title Page

plex expressions and objects. JJ II

J I
Examples: composition of functions, Page 20 of 644

inductive definitions Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Methods of abstraction Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

Means of naming and using groups of Lecture-wise Index to . . .

objects and expressions as a single Home Page

Title Page
unit JJ II

Examples: functions, data structures, J I

modules, classes etc. Page 21 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Functional Model
The functional model is very conve- Computing: The . . .
Algorithms: Design & . . .

nient and easy to use: Introducing Reals


Correctness, . . .
Compound Data

• Programs are written (more or less) Higher Order Functions . . .


Imperative . . .

in mathematical notation A large Example: . . .


Lecture-wise Index to . . .

• It is like using a hand-held calcula- Home Page

Title Page

tor JJ II

• Very interactive and so answers are J I

immediately available Page 22 of 644

Go Back

• Very convenient for developing, Full Screen

testing and proving algorithms Close

Standard ML Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Mathematical Notation
Compound Data
Higher Order Functions . . .
Imperative . . .

1: Factorial A large Example: . . .


Lecture-wise Index to . . .

Home Page

 Title Page

1 if n < 1
n! = JJ II

n × (n − 1)! otherwise J I

Page 23 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Mathematical Notation Correctness, . . .


Compound Data

2: Factorial
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Or more informally, Home Page

Title Page

1 if n < 1 JJ II

n! =
1 × 2 × . . . × n otherwise J I

Page 24 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Mathematical Notation
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

3: Factorial Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

How about this? Lecture-wise Index to . . .

Home Page

1 if n < 1 Title Page

n! =
(n + 1)!/(n + 1) otherwise JJ II

J I

Mathematically correct but computa- Page 25 of 644

tionally incorrect! Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

A Functional Program: Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Factorial Lecture-wise Index to . . .

Home Page

Title Page

fun fact n = if n < 1 then 1 JJ II

else n * fact (n-1) J I

Page 26 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

A Computation: Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Factorial Lecture-wise Index to . . .

Home Page

sml Title Page

Standard ML of New Jersey, JJ II

J I
-
Page 27 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

A Computation: Compound Data


Higher Order Functions . . .
Imperative . . .

Factorial A large Example: . . .


Lecture-wise Index to . . .

Home Page
sml Title Page

Standard ML of New Jersey, JJ II

- fun fact n = J I

= Page 28 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

A Computation: Correctness, . . .
Compound Data
Higher Order Functions . . .

Factorial Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

sml Home Page

Standard ML of New Jersey, Title Page

- fun fact n = JJ II

J I
= if n < 1 then 1
Page 29 of 644

= Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Factorial
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

sml A large Example: . . .


Lecture-wise Index to . . .

Standard ML of New Jersey, Home Page

- fun fact n = Title Page

= if n < 1 then 1 JJ II

J I
= else n * fact (n-1);
Page 30 of 644

val fact = fn : int -> int Go Back

- Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation: Computing: The . . .

Factorial Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

sml Higher Order Functions . . .


Imperative . . .

Standard ML of New Jersey, A large Example: . . .


Lecture-wise Index to . . .

- fun fact n = Home Page

= if n < 1 then 1 Title Page

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

J I
val fact = fn : int -> int
Page 31 of 644

- fact 8; Go Back

val it = 40320 : int Full Screen

- Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Factorial Computing: The . . .
Algorithms: Design & . . .

sml Introducing Reals


Correctness, . . .
Compound Data

Standard ML of New Jersey, Higher Order Functions . . .


Imperative . . .

- fun fact n = A large Example: . . .


Lecture-wise Index to . . .

= if n < 1 then 1 Home Page

= else n * fact (n-1); Title Page

val fact = fn : int -> int JJ II

J I
- fact 8;
Page 32 of 644

val it = 40320 : int Go Back

- fact 9; Full Screen

val it = 362880 : int Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Factorial Computing: The . . .

sml Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Standard ML of New Jersey, Compound Data
Higher Order Functions . . .

- fun fact n = Imperative . . .


A large Example: . . .

= if n < 1 then 1 Lecture-wise Index to . . .

Home Page
= else n * fact (n-1); Title Page

val fact = fn : int -> int JJ II

- fact 8; J I

val it = 40320 : int Page 33 of 644

- fact 9; Go Back

Full Screen
val it = 362880 : int
Close

- Quit

More SML
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Standard ML Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
• Originated as part of a theorem- Imperative . . .
A large Example: . . .

proving development project Lecture-wise Index to . . .

Home Page

• Runs on both Windows and UNIX Title Page

environments JJ II

• Is free! J I

Page 34 of 644

• http://www.smlnj.org Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Important
Features Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Has a small vocabulary of just a few Correctness, . . .


Compound Data
Higher Order Functions . . .

short words Imperative . . .


A large Example: . . .

• Far more “intelligent” than currently


Lecture-wise Index to . . .

Home Page

available languages: Title Page

– automatically finds out what vari- JJ II

ous names mean and J I

Page 35 of 644

– their correct usage Go Back

• Haskell, Miranda and Caml are a Full Screen

Close

few other such languages. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1.3. Primitives: Integer & Real
Computing: The . . .
1. Algorithms & Programs
Algorithms: Design & . . .
2. SML: Primitive Integer Operations 1 Introducing Reals
Correctness, . . .
3. SML: Primitive Integer Operations 1
Compound Data
4. SML: Primitive Integer Operations 1 Higher Order Functions . . .
Imperative . . .
5. SML: Primitive Integer Operations 1
A large Example: . . .
6. SML: Primitive Integer Operations 1 Lecture-wise Index to . . .

7. SML: Primitive Integer Operations 1 Home Page

8. SML: Primitive Integer Operations 2


Title Page
9. SML: Primitive Integer Operations 2
JJ II
10. SML: Primitive Integer Operations 2
11. SML: Primitive Integer Operations 2 J I

12. SML: Primitive Integer Operations 2


Page 36 of 644

13. SML: Primitive Integer Operations 3


Go Back
14. SML: Primitive Integer Operations 3
15. SML: Primitive Integer Operations 3 Full Screen

16. SML: Primitive Integer Operations 3 Close

17. SML: Primitive Integer Operations 3


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Quotient & Remainder
19. SML: Primitive Real Operations 1
20. SML: Primitive Real Operations 1
Computing: The . . .

21. SML: Primitive Real Operations 1 Algorithms: Design & . . .


Introducing Reals
22. SML: Primitive Real Operations 2 Correctness, . . .

23. SML: Primitive Real Operations 3 Compound Data


Higher Order Functions . . .
24. SML: Primitive Real Operations 4 Imperative . . .

25. SML: Precision A large Example: . . .


Lecture-wise Index to . . .
26. Fibonacci Numbers
Home Page
27. Euclidean Algorithm
Title Page

JJ II

J I

Page 37 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithms & Programs Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
• Algorithm Correctness, . . .
Compound Data

• Need for a formal notation


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• Programs Lecture-wise Index to . . .

Home Page

• Programming languages Title Page

JJ II
• Programming
J I

• Functional Programming Page 38 of 644

• Standard ML
Go Back

Full Screen

Factorial
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

SML: Primitive Integer Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Operations 1 Lecture-wise Index to . . .

Home Page

sml Title Page

JJ II
Standard ML of New Jersey,
J I
- Page 39 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

SML: Primitive Integer


Introducing Reals
Correctness, . . .
Compound Data

Operations 1
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

sml Home Page

Standard ML of New Jersey, Title Page

JJ II
- val x = 5;
J I
val x = 5 : int Page 40 of 644

- Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Operations 1 Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

sml A large Example: . . .


Lecture-wise Index to . . .

Standard ML of New Jersey, Home Page

- val x = 5; Title Page

JJ II
val x = 5 : int
J I
- val y = 6; Page 41 of 644

val y = 6 : int Go Back

- Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer Computing: The . . .

Operations 1 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

sml Higher Order Functions . . .


Imperative . . .

Standard ML of New Jersey, A large Example: . . .


Lecture-wise Index to . . .

- val x = 5; Home Page

val x = 5 : int Title Page

JJ II
- val y = 6;
J I
val y = 6 : int Page 42 of 644

- x+y; Go Back

val it = 11 : int Full Screen

- Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

sml Correctness, . . .
Compound Data

Standard ML of New Jersey, Higher Order Functions . . .


Imperative . . .

- val x = 5; A large Example: . . .


Lecture-wise Index to . . .

val x = 5 : int Home Page

- val y = 6; Title Page

JJ II
val y = 6 : int
J I
- x+y; Page 43 of 644

val it = 11 : int Go Back

- x-y; Full Screen

val it = ˜1 : int Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 1 Computing: The . . .
Algorithms: Design & . . .
Standard ML of New Jersey, Introducing Reals
Correctness, . . .

- val x = 5; Compound Data


Higher Order Functions . . .

val x = 5 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- val y = 6; Home Page

val y = 6 : int Title Page

- x+y; JJ II

val it = 11 : int J I

- x-y; Page 44 of 644

val it = ˜1 : int Go Back

Full Screen

- it + 5; Close

val it = 4 : int Quit

- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val x = 5 : int Introducing Reals
Correctness, . . .

- val y = 6; Compound Data


Higher Order Functions . . .

val y = 6 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- x+y; Home Page

val it = 11 : int Title Page

- x-y; JJ II

val it = ˜1 : int J I

- it + 5; Page 45 of 644

val it = 4 : int Go Back

Full Screen

- x * y; Close

val it = 30 : int Quit

- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val y = 6 : int Introducing Reals
Correctness, . . .

- x+y; Compound Data


Higher Order Functions . . .

val it = 11 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- x-y; Home Page

val it = ˜1 : int Title Page

- it + 5; JJ II

val it = 4 : int J I

- x * y; Page 46 of 644

val it = 30 : int Go Back

Full Screen

- val a = 25; Close

val a = 25 : int Quit

- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val it = 11 : int Introducing Reals
Correctness, . . .

- x-y; Compound Data


Higher Order Functions . . .

val it = ˜1 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- it + 5; Home Page

val it = 4 : int Title Page

- x * y; JJ II

val it = 30 : int J I

- val a = 25; Page 47 of 644

val a = 25 : int Go Back

Full Screen

- val b = 7; Close

val b = 7 : int Quit

- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val it = ˜1 : int Introducing Reals
Correctness, . . .

- it + 5; Compound Data
Higher Order Functions . . .

val it = 4 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- x * y; Home Page

val it = 30 : int Title Page

- val a = 25; JJ II

val a = 25 : int J I

- val b = 7; Page 48 of 644

val b = 7 : int Go Back

Full Screen

- val q = a div b; Close

val q = 3 : int Quit

- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
- x * y; Introducing Reals
Correctness, . . .

val it = 30 : int Compound Data


Higher Order Functions . . .

- val a = 25; Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
val a = 25 : int
Home Page

- val b = 7; Title Page

val b = 7 : int JJ II

- val q = a div b; J I

val q = 3 : int Page 49 of 644

- val r = a mod b; Go Back

Full Screen

GC #0.0.0.0.2.45: (0 ms) Close

val r = 4 : int Quit

- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val a = 25; Introducing Reals
Correctness, . . .

val a = 25 : int Compound Data


Higher Order Functions . . .

- val b = 7; Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
val b = 7 : int Home Page

- val q = a div b; Title Page

val q = 3 : int JJ II

- val r = a mod b; J I

GC #0.0.0.0.2.45: (0 ms) Page 50 of 644

val r = 4 : int Go Back

Full Screen

- a = b*q + r; Close

val it = true : bool Quit

- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val b = 7; Introducing Reals
Correctness, . . .

val b = 7 : int Compound Data


Higher Order Functions . . .

- val q = a div b; Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
val q = 3 : int Home Page

- val r = a mod b; Title Page

GC #0.0.0.0.2.45: (0 ms) JJ II

val r = 4 : int J I

- a = b*q + r; Page 51 of 644

val it = true : bool Go Back

Full Screen

- val c = ˜7; Close

val c = ˜7 : int Quit

- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val q = a div b; Introducing Reals
Correctness, . . .

val q = 3 : int Compound Data


Higher Order Functions . . .

- val r = a mod b; Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
GC #0.0.0.0.2.45: (0 ms) Home Page

val r = 4 : int Title Page

- a = b*q + r; JJ II

val it = true : bool J I

- val c = ˜7; Page 52 of 644

val c = ˜7 : int Go Back

Full Screen

- val q1 = a div c; Close

val q1 = ˜4 : int Quit

- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val r = a mod b; Introducing Reals
Correctness, . . .

GC #0.0.0.0.2.45: (0 ms) Compound Data


Higher Order Functions . . .

val r = 4 : int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- a = b*q + r; Home Page

val it = true : bool Title Page

- val c = ˜7; JJ II

val c = ˜7 : int J I

- val q1 = a div c; Page 53 of 644

val q1 = ˜4 : int Go Back

Full Screen

- val r1 = a mod c; Close

val r1 = ˜3 : int Quit

- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
val r = 4 : int Introducing Reals
Correctness, . . .

- a = b*q + r; Compound Data


Higher Order Functions . . .

val it = true : bool Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
- val c = ˜7; Home Page

val c = ˜7 : int Title Page

- val q1 = a div c; JJ II

val q1 = ˜4 : int J I

- val r1 = a mod c; Page 54 of 644

val r1 = ˜3 : int Go Back

Full Screen

- a = c*q1 + r1; Close

val it = true : bool Quit

- •
First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Quotient & Remainder Computing: The . . .
Algorithms: Design & . . .

For any two integers a and b, the quo- Introducing Reals


Correctness, . . .
Compound Data

tient q and remainder r are uniquely Higher Order Functions . . .


Imperative . . .

determined to satisfy A large Example: . . .


Lecture-wise Index to . . .

•a = b×q+r Home Page

Title Page

• 0 ≤ |r| < |b| JJ II

So J I

Page 55 of 644

0 ≤ r < b when b > 0 Go Back

b < r ≤ 0 when b < 0 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

SML: Primitive Real Correctness, . . .


Compound Data

Operations 1 Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

sml Home Page

Standard ML of New Jersey, Title Page

- val real_a = real a; JJ II

J I
val real_a = 25.0 : real
Page 56 of 644

- Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 1 Computing: The . . .
Algorithms: Design & . . .

sml Introducing Reals


Correctness, . . .
Compound Data

Standard ML of New Jersey, Higher Order Functions . . .


Imperative . . .

- val real_a = real a; A large Example: . . .


Lecture-wise Index to . . .

val real_a = 25.0 : real Home Page

- real_a + b; Title Page

stdIn:40.1-40.11 Error: operator and JJ II

J I
operator domain: real * real
Page 57 of 644

operand: real * int Go Back

in expression: Full Screen

real_a + b Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 1
Computing: The . . .

stdIn:40.1-40.11 Error: operator and Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
operator domain: real * real Compound Data
Higher Order Functions . . .

operand: real * int Imperative . . .


A large Example: . . .

in expression: Lecture-wise Index to . . .

Home Page

real_a + b Title Page

- b + real_a; JJ II

stdIn:1.1-2.6 Error: operator and ope J I

operator domain: int * int Page 58 of 644

operand: int * real Go Back

Full Screen
in expression:
Close

b + real_a Quit

- • • •
First • • •
Prev • •
Next Last Go Back Full Screen Close Quit
SML: Primitive Real
Operations 2
Computing: The . . .

- val a = 25.0; Algorithms: Design & . . .


Introducing Reals

val a = 25.0 : real Correctness, . . .


Compound Data
Higher Order Functions . . .
- val b = 7.0; Imperative . . .
A large Example: . . .

val b = 7.0 : real Lecture-wise Index to . . .

- a/b; Home Page

Title Page
val it = 3.57142857143 : real JJ II
- a div b; J I

stdIn:49.3-49.6 Error: overloaded var Page 59 of 644

symbol: div Go Back

type: real Full Screen

GC #0.0.0.0.3.98: (0 ms) Close

Quit

- • •
First• • • •
Prev • •
Next Last Go Back Full Screen Close Quit
SML: Primitive Real Computing: The . . .
Algorithms: Design & . . .

Operations 3 Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

- val c = a/b; Imperative . . .


A large Example: . . .

val c = 3.57142857143 : real Lecture-wise Index to . . .

Home Page
- trunc(c); Title Page

val it = 3 : int JJ II

- trunc (c + 0.5); J I

val it = 4 : int Page 60 of 644

- Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 4
Computing: The . . .

- val d = 3.0E10; Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
val d = 30000000000.0 : real Compound Data
Higher Order Functions . . .

- val pi = 0.314159265E1; Imperative . . .


A large Example: . . .

val pi = 3.14159265 : real Lecture-wise Index to . . .

Home Page
- - d+pi; Title Page

val it = 30000000003.1 : real JJ II

- d-pi; J I

val it = 29999999996.9 : real Page 61 of 644

- pi + d; Go Back

val it = 30000000003.1 : real Full Screen

Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Precision
- pi + d*10.0; Computing: The . . .
Algorithms: Design & . . .

val it = 300000000003.0 : real Introducing Reals


Correctness, . . .

- pi + d*100.0; Compound Data


Higher Order Functions . . .
Imperative . . .

val it = 3E12 : real A large Example: . . .


Lecture-wise Index to . . .

- d*100.0 + pi; Home Page

val it = 3E12 : real Title Page

- d*100.0 -pi; JJ II

val it = 3E12 : real J I

Page 62 of 644
- d*10.0 - pi;
Go Back

val it = 299999999997.0 : real Full Screen

- Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci Numbers Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

F(0) = 1 Correctness, . . .
Compound Data
Higher Order Functions . . .
F(1) = 1 Imperative . . .
A large Example: . . .

F(n) = F(n − 1) + F(n − 2) if n > 1 Lecture-wise Index to . . .

Home Page

Title Page

fun fib (n) = JJ II

if (n = 0) orelse (n = 1) J I

then 1 Page 63 of 644

else fib (n-1) + fib (n-2); Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Euclidean Algorithm
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Relies on a theorem proved by Euclid Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

fun gcd (a, b) = Home Page

if b=0 then a Title Page

else gcd (b, a mod b); JJ II

J I

Page 64 of 644

Is it really inductive? Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2. Algorithms: Design & Refinement
Computing: The . . .
Algorithms: Design & . . .
2.1. Technical Completeness & Algorithms
Introducing Reals
1. Recapitulation: Integers & Real Correctness, . . .
Compound Data
2. Recap: Integer Operations
Higher Order Functions . . .
3. Recapitulation: Real Operations Imperative . . .
A large Example: . . .
4. Recapitulation: Simple Algorithms
Lecture-wise Index to . . .
5. More Algorithms
Home Page
6. Powering: Math
7. Powering: SML Title Page

8. Technical completeness JJ II
9. What SML says
J I
10. Technical completeness
Page 65 of 644
11. What SML says ... contd
12. Powering: Math 1 Go Back

13. Powering: SML 1 Full Screen

14. Technical Completness


Close
15. What SML says
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Powering: Integer Version
17. Exceptions: A new primitive
18. Integer Power: SML
Computing: The . . .

19. Integer Square Root 1 Algorithms: Design & . . .


Introducing Reals
20. Integer Square Root 2 Correctness, . . .

21. An analysis Compound Data


Higher Order Functions . . .
22. Algorithmic idea Imperative . . .

23. Algorithm: isqrt A large Example: . . .


Lecture-wise Index to . . .
24. Algorithm: shrink
Home Page
25. SML: shrink
26. SML: intsqrt Title Page

27. Run it! JJ II


28. SML: Reorganizing Code
J I
29. Intsqrt: Reorganized
Page 66 of 644
30. shrink: Another algorithm
31. Shrink2: SML Go Back

32. Shrink2: SML ... contd


Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Recapitulation: Correctness, . . .
Compound Data
Higher Order Functions . . .

Integers & Real Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• Primitive Integer Operations Home Page

Title Page

• Primitive Real Operations JJ II

• Some algorithms J I

Page 67 of 644
Forward
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Integer Computing: The . . .
Algorithms: Design & . . .

Operations Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

• Primitive Integer Operations Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

– Naming, +, −, ∼ Home Page

– Multiplication, division Title Page

– Quotient & remainder JJ II

J I

• Primitive Real Operations Page 68 of 644

• Some algorithms Go Back

Full Screen
Back
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation: Real
Operations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Primitive Integer Operations Correctness, . . .


Compound Data
Higher Order Functions . . .

• Primitive Real Operations Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

– Integer to Real Home Page

– Real to Integer Title Page

JJ II
– Real addition & subtraction J I

– Real division Page 69 of 644

– Real Precision Go Back

Full Screen

• Some algorithms Close

Back Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation: Simple Computing: The . . .
Algorithms: Design & . . .

Algorithms Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

• Primitive Integer Operations Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• Primitive Real Operations Home Page

• Some algorithms Title Page

JJ II
– Factorial J I

– Fibonacci Page 70 of 644

– Euclidean GCD Go Back

Full Screen
Back
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

More Algorithms
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Powering Home Page

• Integer square root Title Page

JJ II

• Combinations nCk J I

Page 71 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: Math Computing: The . . .
Algorithms: Design & . . .

For any integer or real number x 6= 0 Introducing Reals


Correctness, . . .

and non-negative integer n Compound Data


Higher Order Functions . . .
Imperative . . .

xn = x
A large Example: . . .

| ×x× {z· · · × x} Lecture-wise Index to . . .

Home Page
n times
Title Page

Noting that x0 = 1 we give an induc- JJ II

tive definition: J I

 Page 72 of 644

n 1 if n = 0
x = Go Back

xn−1 × x otherwise Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Powering: SML
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

fun power (x:real, n) = A large Example: . . .


Lecture-wise Index to . . .

if n = 0 Home Page

then 1.0 Title Page

else power (x, n-1) * x JJ II

J I

Page 73 of 644
Is it technically complete? Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Computing: The . . .
Algorithms: Design & . . .

completeness
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Can it be always guaranteed that Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• x will be real? Home Page

• n will be integer? Title Page

JJ II

• n will be non-negative? J I

• x 6= 0? Page 74 of 644

Go Back

If x = 0 what is 0.00? Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says
sml Computing: The . . .
Algorithms: Design & . . .
Standard ML of New Jersey Introducing Reals
Correctness, . . .

- use "/tmp/power.sml"; Compound Data


Higher Order Functions . . .

[opening /tmp/power.sml] Imperative . . .


A large Example: . . .

val power = fn : real * int -> Lecture-wise Index to . . .

Home Page

real Title Page

val it = () : unit JJ II

J I

Page 75 of 644

Can it be always guaranteed that Go Back

Full Screen

• x will be real? YES Close

• n will be integer? YES Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Computing: The . . .
Algorithms: Design & . . .

completeness Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

Can it be always guaranteed that Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• n will be non-negative? NO Home Page

• x 6= 0? NO Title Page

JJ II

If x = 0 what is 0.00? J I

Page 76 of 644

- power(0.0, 0); Go Back

val it = 1.0 : real Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says ...
contd Computing: The . . .
Algorithms: Design & . . .

sml Introducing Reals


Correctness, . . .
Compound Data
Standard ML of New Jersey Higher Order Functions . . .
Imperative . . .

val power = fn : real * int -> real A large Example: . . .


Lecture-wise Index to . . .

val it = () : unit Home Page

- power(˜2.5, 0); Title Page

val it = 1.0 : real JJ II

J I
- power (0.0, 3);
Page 77 of 644

val it = 0.0 : real Go Back

- power (2.5, ˜3) Full Screen

Close

Goes on forever! Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Powering: Math 1 Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
For any real number x and integer n Lecture-wise Index to . . .

 1.0/x−n if n < 0
 Home Page

Title Page

xn = 1 if n = 0 JJ II
 n−1
x × x otherwise J I

Page 78 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: SML 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
fun power (x, n) = Compound Data
Higher Order Functions . . .

if n < 0 Imperative . . .
A large Example: . . .

then 1.0/power(x, ˜n) Lecture-wise Index to . . .

Home Page

else if n = 0 Title Page

then 1.0 JJ II

else power (x, n-1) * x J I

Page 79 of 644

Is this definition technically com- Go Back

plete? Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Completness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• 0.00 = 1.0 whereas 0.0n = 0 for pos-


Correctness, . . .
Compound Data
Higher Order Functions . . .

itive n Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

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

Then Title Page

0.0n JJ II

= 1.0/(0.0m) J I

= 1.0/0.0 Page 80 of 644

Go Back

Division by zero! Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says
- power (2.5, ˜2); Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
val it = 0.16 : real Correctness, . . .
Compound Data

- power (˜2.5, ˜2); Higher Order Functions . . .


Imperative . . .

val it = 0.16 : real A large Example: . . .


Lecture-wise Index to . . .

- power (0.0, 2); Home Page

val it = 0.0 : real Title Page

JJ II
- power (0.0, ˜2);
J I
val it = inf : real Page 81 of 644

- Go Back

Full Screen

SML is somewhat more understand- Close

ing than most languages Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: Integer Computing: The . . .

Version
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
 Higher Order Functions . . .

 undefined if n < 0 Imperative . . .


A large Example: . . .

xn = 1 if n = 0 Lecture-wise Index to . . .

 n−1 Home Page

x × x otherwise Title Page

Technical completeness requires us JJ II

J I
to consider the case n < 0. Other-
Page 82 of 644

wise, the computation can go on for- Go Back

ever Full Screen

Notation: ⊥ denotes the undef ined Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Exceptions: A new Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

primitive Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

exception negExponent; A large Example: . . .


Lecture-wise Index to . . .

fun intpower (x, n) = Home Page

if n < 0 Title Page

JJ II
then raise negExponent
J I
else if n = 0 then 1 Page 83 of 644

else intpower (x, n-1) * x Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Power: SML
Computing: The . . .

- intpower(3, 4); Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

val it = 81 : int Compound Data


Higher Order Functions . . .

- intpower(˜3, 5); Imperative . . .


A large Example: . . .

val it = ˜243 : int Lecture-wise Index to . . .

Home Page

- intpower(3, ˜4); Title Page

JJ II

uncaught exception negExponent J I

raised at: intpower.sml:4.16-4.32 Page 84 of 644

- Go Back

Full Screen

Close
Back to More Algos
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Square Root 1
√ Computing: The . . .

isqrt(n) = b nc Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

- fun isqrt n = Higher Order Functions . . .


Imperative . . .

trunc (Real.Math.sqrt A large Example: . . .


Lecture-wise Index to . . .

(real (n))); Home Page

val isqrt = fn : int -> int Title Page

JJ II
- isqrt (38);
J I
val it = 6 : int Page 85 of 644

- isqrt (˜38); Go Back

uncaught exception domain error Full Screen

raised at: boot/real64.sml:89.32-89 Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Square Root 2 Computing: The . . .

Suppose Real.Math.sqrt were not Algorithms: Design & . . .


Introducing Reals

available to us! Correctness, . . .


Compound Data
Higher Order Functions . . .

isqrt(n) of a non-negative integer n is Imperative . . .


A large Example: . . .

the integer k ≥ 0 such that k 2 ≤ n < Lecture-wise Index to . . .

(k + 1)2
Home Page

Title Page

That is, JJ II

⊥ if n < 0 J I

isqrt(n) =
k otherwise Page 86 of 644

Go Back

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

This value of k is unique! Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An analysis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

0 ≤ k 2 ≤ n < (k + 1)2

Higher Order Functions . . .
Imperative . . .
A large Example: . . .
⇒ 0≤k ≤ n<k+1 Lecture-wise Index to . . .

⇒ 0≤k≤n Home Page

Title Page

Strategy. Use this fact to close in on JJ II

the value of k. Start with the inter- J I


val [l, u] = [0, n] and try to shrink it till Page 87 of 644

it collapses to the interval [k, k] which Go Back

contains a single value. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Algorithmic idea Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

If n = 0 then isqrt(n) = 0. Imperative . . .


A large Example: . . .

Otherwise with [l, u] = [0, n] use one Lecture-wise Index to . . .

Home Page

or both of the following to shrink the Title Page

interval [l, u]. JJ II

• if (l + 1)2 ≤ n then try [l + 1, u] J I

Page 88 of 644

• if u2 > n then try [l, u − 1]. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Algorithm: isqrt Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
 Lecture-wise Index to . . .

⊥ if n < 0 Home Page

isqrt(n) = 0 if n = 0 Title Page

shrink(n, 0, n) if n > 0
 JJ II

J I

where Page 89 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithm: shrink Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

shrink(n, l, u) = Compound Data


Higher Order Functions . . .
Imperative . . .
 A large Example: . . .


 l if l = u Lecture-wise Index to . . .



 shrink(n, l + 1, u) if l < u Home Page

and (l + 1)2 ≤ n
 Title Page

JJ II


 shrink(n, l, u − 1) if l < u J I


 and u 2>n Page 90 of 644

⊥ if l > u
 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: shrink
Computing: The . . .

exception intervalError; Algorithms: Design & . . .


Introducing Reals

fun shrink (n, l, u) = Correctness, . . .


Compound Data
Higher Order Functions . . .
if l>u orelse Imperative . . .
A large Example: . . .

l*l > n orelse Lecture-wise Index to . . .

u*u < n Home Page

Title Page
then raise intervalError JJ II
else if (l+1)*(l+1) <= n J I

then shrink (n, l+1, u) Page 91 of 644

else l; Go Back

Full Screen

Close
intsqrt
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: intsqrt Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

exception negError; Correctness, . . .


Compound Data
Higher Order Functions . . .
fun intsqrt n = Imperative . . .
A large Example: . . .

if n<0 Lecture-wise Index to . . .

then raise negError Home Page

Title Page
else if n=0 JJ II
then 0 J I

else shrink (n, 0, n) Page 92 of 644

Go Back

Full Screen

shrink Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run it!
exception intervalError Computing: The . . .
Algorithms: Design & . . .

val shrink = fn : int * int * int -> Introducing Reals


Correctness, . . .

exception negError Compound Data


Higher Order Functions . . .

val intsqrt = fn : int -> int Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
val it = () : unit Home Page

- intsqrt 8; Title Page

val it = 2 : int JJ II

- intsqrt 16; J I

val it = 4 : int Page 93 of 644

Go Back
- intsqrt 99;
Full Screen

val it = 9 : int Close

- Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

SML: Reorganizing Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Code Compound Data


Higher Order Functions . . .
Imperative . . .

• shrink was used to develop


A large Example: . . .
Lecture-wise Index to . . .

intsqrt Home Page

Title Page

• Is shrink general-purpose JJ II

enough to be kept separate? J I

• Shouldn’t shrink be placed within Page 94 of 644

Go Back

intsqrt? Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized Computing: The . . .
Algorithms: Design & . . .

exception negError; Introducing Reals


Correctness, . . .
Compound Data

fun intsqrt n = Higher Order Functions . . .


Imperative . . .

let fun shrink (n, l, u) = ... A large Example: . . .


Lecture-wise Index to . . .

in if n<0 Home Page

then raise negError Title Page

else if n=0 JJ II

J I
then 0
Page 95 of 644

else shrink (n, 0, n) Go Back

end Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
shrink: Another
algorithm Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Shrink Correctness, . . .
Compound Data

shrink2(n, l, u) = Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .


 l if l = u or u = l + 1 Home Page



 shrink2(n, m, u) if l < u Title Page

and m2 ≤ n

 JJ II

J I

 shrink2(n, l, m) if l < u Page 96 of 644


 and m 2>n
 Go Back

⊥ if l > u

Full Screen

where m = (l + u) div 2 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Shrink2: SML Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

fun shrink2 (n, l, u) = Compound Data


Higher Order Functions . . .
Imperative . . .

if l>u orelse A large Example: . . .


Lecture-wise Index to . . .

l*l > n orelse Home Page

u*u < n Title Page

then raise intervalError JJ II

else if l = u J I

Page 97 of 644
then l
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shrink2: SML ... contd Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

else Correctness, . . .
Compound Data
Higher Order Functions . . .
let val m = (l+u) div 2; Imperative . . .
A large Example: . . .

val msqr = m*m Lecture-wise Index to . . .

in if msqr <= n Home Page

Title Page
then shrink (n, m, u)
JJ II
else shrink (n, l, m) J I

end; Page 98 of 644

Go Back

Back to More Algos Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.2. Algorithm Refinement
Computing: The . . .
1. Recap: More Algorithms
Algorithms: Design & . . .
2. Recap: Power Introducing Reals
Correctness, . . .
3. Recap: Technical completeness
Compound Data
4. Recap: More Algorithms Higher Order Functions . . .
Imperative . . .
5. Intsqrt: Reorganized
A large Example: . . .
6. Intsqrt: Reorganized Lecture-wise Index to . . .

7. Some More Algorithms Home Page

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

12. Refinement
Page 99 of 644

13. Perfect Numbers: SML


Pu Go Back
14. l if divisor(k)

15. SML: sum divisors Full Screen

16. if divisor and ifdivisor Close

17. SML: Assembly 1


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. SML: Assembly 2
19. Perfect Numbers . . . contd.
20. Perfect Numbers . . . contd.
Computing: The . . .

21. SML: Assembly 3 Algorithms: Design & . . .


Introducing Reals
22. Perfect Numbers: Run Correctness, . . .

23. Perfect Numbers: Run Compound Data


Higher Order Functions . . .
24. SML: Code variations Imperative . . .

25. SML: Code variations A large Example: . . .


Lecture-wise Index to . . .
26. SML: Code variations
Home Page
27. Summation: Generalizations
28. Algorithmic Improvements: Title Page

29. Algorithmic Variations JJ II


30. Algorithmic Variations
J I

Page 100 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Recap: More
Compound Data
Higher Order Functions . . .
Imperative . . .

Algorithms
A large Example: . . .
Lecture-wise Index to . . .

Home Page

• xn for real and integer x Title Page

JJ II
• Integer square root J I
Forward
Page 101 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Power Computing: The . . .

• xn for real and integer x


Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

– Technical Completness Higher Order Functions . . .


Imperative . . .

∗ Undefinedness A large Example: . . .


Lecture-wise Index to . . .

∗ Termination Home Page

Title Page
– More complete definition for real JJ II
x J I

– Power of an integer Page 102 of 644

– ⊥ and exceptions Go Back

Full Screen

• Integer square root Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Technical
completeness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Can it be always guaranteed that Compound Data


Higher Order Functions . . .
Imperative . . .

• x will be real? YES A large Example: . . .


Lecture-wise Index to . . .

• n will be integer? YES Home Page

Title Page

• n will be non-negative? NO JJ II

• x 6= 0? NO J I

Page 103 of 644

If x = 0 what is 0.00? Go Back

Full Screen

INFINITE COMPUTATION Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: More Computing: The . . .
Algorithms: Design & . . .

Algorithms Introducing Reals


Correctness, . . .
Compound Data

• xn for real and integer x


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Integer square root Home Page

– Analysis Title Page

JJ II
– Algorithmic idea
J I

– Algorithm Page 104 of 644

– where Go Back

– and let ...in ...end


Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized
exception negError; Computing: The . . .
Algorithms: Design & . . .

exception intervalError; Introducing Reals


Correctness, . . .
Compound Data

fun intsqrt n = Higher Order Functions . . .


Imperative . . .

let fun shrink (n, l, u) = A large Example: . . .


Lecture-wise Index to . . .

if l>u orelse Home Page

l*l > n orelse Title Page

u*u < n JJ II

J I
then raise intervalError
Page 105 of 644

else if (l+1)*(l+1) <= n Go Back

then shrink (n, l+1, u) Full Screen

else l; Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
in if n<0 Imperative . . .
A large Example: . . .

then raise negError Lecture-wise Index to . . .

else if n=0 Home Page

Title Page
then 0 JJ II
else shrink (n, 0, n) J I

end Page 106 of 644

Go Back

Back Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Some More Algorithms A large Example: . . .


Lecture-wise Index to . . .

Home Page

• Combinations Title Page

• Perfect Numbers JJ II

J I

Page 107 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: Math
nC = n! Computing: The . . .
k (n−k)!k! Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

n(n−1)···(n−k+1) Higher Order Functions . . .

= k!
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

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

JJ II

= n−1Ck−1 + n−1Ck
J I

Page 108 of 644

Since we already have the function Go Back

fact, we may program nCk using Full Screen

any of the above identities. Let’s pro- Close

gram it using the last one. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: Details
Computing: The . . .

Given a set of n ≥ 0 elements, find Algorithms: Design & . . .


Introducing Reals

the number of subsets of k elements, Correctness, . . .


Compound Data

where 0 ≤ k ≤ n Higher Order Functions . . .


Imperative . . .
A large Example: . . .


 ⊥ if n < 0 or Lecture-wise Index to . . .

Home Page

k < 0 or



 Title Page

k>n


 JJ II
nC = 1 if n = 0 or J I
k
k = 0 or

 Page 109 of 644


k=n Go Back



 n−1
Ck−1 + n−1Ck otherwise
 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: SML
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data

k < 0 orelse Higher Order Functions . . .


Imperative . . .

k > n A large Example: . . .


Lecture-wise Index to . . .

then raise invalid_arg Home Page

else if n = 0 orelse Title Page

JJ II
k = 0 orelse
J I
n = k Page 110 of 644

then 1 Go Back

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

comb (n-1, k); Close

Quit

Back to Some More Algorithms


•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers
An integer n > 0 is perfect if it equals Computing: The . . .
Algorithms: Design & . . .

the sum of all its proper divisors. Introducing Reals


Correctness, . . .

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


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

k|n ⇐⇒ n mod k = 0 Lecture-wise Index to . . .

Home Page

Title Page

perf ect(n) JJ II

J I
P
⇐⇒ n = {k : 0 < k < n, k|n} Page 111 of 644

Go Back

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

Close

where Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Refinement Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

1. if divisor(k) needs to be defined Home Page

Pn−1 Title Page

2. k=1 if divisor(k) needs to be de- JJ II

fined algorithmically. J I

Page 112 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: SML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

exception nonpositive; Compound Data


Higher Order Functions . . .

fun perfect (n) = Imperative . . .


A large Example: . . .

if n <= 0 Lecture-wise Index to . . .

Home Page

then raise nonpositive Title Page

else JJ II

n = sum_divisors (1, n-1) J I

Page 113 of 644

where sum divisors needs to be Go Back

defined Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Pu
l if divisor(k) Computing: The . . .
Algorithms: Design & . . .

Pu Introducing Reals

k=l if divisor(k) =
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
 A large Example: . . .

if l > u
Lecture-wise Index to . . .


 0 Home Page

Title Page

 if divisor(l)+ otherwise JJ II

 n−1 if divisor(k)

 P
J I
k=l+1
Page 114 of 644

where if divisor(k) needs to be de- Go Back

fined Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: sum divisors
Computing: The . . .

From
Pu the algorithmic definition of Algorithms: Design & . . .
Introducing Reals

k=l if divisor(k)
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

fun sum_divisors (l, u) = Home Page

if l > u Title Page

then 0 JJ II

else ifdivisor (l) + J I

sum_divisors (l+1, u) Page 115 of 644

Go Back

Full Screen

where if divisor(k) still needs to be Close

defined Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
if divisor and ifdivisor
Computing: The . . .
 Algorithms: Design & . . .

k if k|n Introducing Reals

if divisor(k) = Correctness, . . .

0 otherwise Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

fun ifdivisor (k) = Home Page

if n mod k = 0 Title Page

JJ II
then k J I
else 0 Page 116 of 644

Go Back

Not technically complete! Full Screen

However . . . Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 1
fun sum_divisors (l, u) = Computing: The . . .
Algorithms: Design & . . .

if l > u then 0 Introducing Reals


Correctness, . . .
Compound Data
else Higher Order Functions . . .
Imperative . . .

let fun ifdivisor (k) = A large Example: . . .


Lecture-wise Index to . . .

if n mod k = 0 Home Page

then k Title Page

else 0 JJ II

J I
in ifdivisor (l) +
Page 117 of 644
sum_divisors (l+1, u) Go Back

end Full Screen

Close

Clearly k ∈ [l, u] Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 2
exception nonpositive; Computing: The . . .

fun perfect (n) = Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

if n <= 0 Compound Data


Higher Order Functions . . .

then raise nonpositive Imperative . . .


A large Example: . . .

else Lecture-wise Index to . . .

Home Page

let fun sum_divisors (l, u) = Title Page

... JJ II

in n = sum_divisors (1, n-1) J I

end Page 118 of 644

Go Back

Full Screen

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

n > 0.
Technically complete! Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers
. . . contd.
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

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


Higher Order Functions . . .
Imperative . . .

if divisor(k) = 0. A large Example: . . .


Lecture-wise Index to . . .

Home Page

bn/2c = n div 2 < n/2 Title Page

Hence JJ II

J I
n−1
X n div
X 2 Page 119 of 644

if divisor(k) = if divisor(k) Go Back

k=1 k=1 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers
. . . contd. Computing: The . . .
Algorithms: Design & . . .

Hence Introducing Reals


Correctness, . . .
Compound Data

perf ect(n) Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Pn−1
⇐⇒ n = k=1 if divisor(k)
Home Page

Title Page

Pn div 2 JJ II

⇐⇒ n = k−1 if divisor(k) J I

where
Page 120 of 644

Go Back

k if k|n Full Screen
if divisor(k) =
0 otherwise Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 3
exception nonpositive;
fun perfect (n) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

if n <= 0 Higher Order Functions . . .


Imperative . . .

then raise nonpositive A large Example: . . .


Lecture-wise Index to . . .

else Home Page

let fun sum_divisors (l, u) = Title Page

JJ II
...
J I
in n = sum_divisors (1, n div 2) Page 121 of 644

end Go Back

Full Screen

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

ever n > 0. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: Run Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

exception nonpositive Correctness, . . .


Compound Data
Higher Order Functions . . .
val perfect = fn : int -> bool Imperative . . .
A large Example: . . .

val it = () : unit Lecture-wise Index to . . .

- perfect ˜8; Home Page

Title Page

uncaught exception nonpositive JJ II


raised at: perfect.sml:4.16-4.27 J I

- perfect 5; Page 122 of 644

val it = false : bool Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: Run Computing: The . . .
Algorithms: Design & . . .

- perfect 6; Introducing Reals


Correctness, . . .

val it = true : bool Compound Data


Higher Order Functions . . .
Imperative . . .
- perfect 23; A large Example: . . .
Lecture-wise Index to . . .

val it = false : bool Home Page

- perfect 28; Title Page

GC #0.0.0.1.3.88: (1 ms) JJ II

val it = true : bool J I

Page 123 of 644


- perfect 30;
Go Back

val it = false : bool Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Code variations
exception nonpositive;
fun perfect (n) = Computing: The . . .
Algorithms: Design & . . .

if n <= 0 Introducing Reals


Correctness, . . .
Compound Data
then raise nonpositive Higher Order Functions . . .
Imperative . . .

else A large Example: . . .


Lecture-wise Index to . . .

let Home Page

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

fun sum_divisors (l, u) = ... JJ II

in J I

Page 124 of 644


n=sum_divisors (1, n div 2) Go Back

end Full Screen

Close

Technically complete though Quit

ifdivisor, by itself is not!


• •
First• • •
Prev Next Last Go Back •Full Screen •Close •Quit
SML: Code variations
What about this? Computing: The . . .
Algorithms: Design & . . .

exception nonpositive; Introducing Reals


Correctness, . . .

fun perfect (n) = Compound Data


Higher Order Functions . . .
Imperative . . .

let A large Example: . . .


Lecture-wise Index to . . .

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

fun sum_divisors (l, u) = ... Title Page

in if n <= 0 JJ II

then raise nonpositive J I

Page 125 of 644


else
Go Back

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

end Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Code variations
Computing: The . . .

What about this? Algorithms: Design & . . .


Introducing Reals

exception nonpositive; Correctness, . . .


Compound Data

fun ifdivisor (k) = ...; Higher Order Functions . . .


Imperative . . .
A large Example: . . .

fun sum_divisors (l, u) = ...; Lecture-wise Index to . . .

fun perfect (n) = Home Page

Title Page
if n <= 0
JJ II
then raise nonpositive J I

else Page 126 of 644

n=sum_divisors (1, n div 2) Go Back

Full Screen

Technically incomplete! Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Summation:
Generalizations
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Need a method to compute summa- Compound Data


Higher Order Functions . . .
Imperative . . .
tions in general. A large Example: . . .
Lecture-wise Index to . . .

For any function f : Z → Z and inte- Home Page

gers l and u, Title Page

 JJ II
0 if l > u
u

 J I
X 
f (i) = Page 127 of 644

 fP(l)+ otherwise Go Back

i=l  u

i=l+1 f (i) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Algorithmic Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Improvements: Home Page

Title Page

1. perf ect2 JJ II

2. shrink2 J I

Page 128 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithmic Variations Computing: The . . .

1. For any k|n, m = n div k is also a Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

divisor of n Compound Data


Higher Order Functions . . .
Imperative . . .
2. 1 is a divisor of every positive num- A large Example: . . .
Lecture-wise Index to . . .

ber Home Page


3. For n > 2, b nc < n div 2 Title Page

Pn div 2 JJ II

4. Hence k=1 if divisor(k) = J I

√ Page 129 of 644

b nc
X Go Back

1+ if divisor2(k) Full Screen

k=2 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithmic Variations Computing: The . . .
Algorithms: Design & . . .

perf ect(n) Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

Pb√nc Imperative . . .
A large Example: . . .

⇐⇒ n = 1 + k=2 if divisor2(k) Lecture-wise Index to . . .

Home Page

where Title Page



 k+ JJ II

if divisor2(k) = (n div k) if k|n J I

Page 130 of 644

0 otherwise

Go Back

Are there any glitches? Is it techni- Full Screen

cally correct and complete? Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.3. Variations: Algorithms & Code
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 131 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Recap Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• Combinations A large Example: . . .


Lecture-wise Index to . . .

• Perfect Numbers Home Page

Title Page

• Code Variations JJ II

• Algorithmic Variations J I

Page 132 of 644


forward
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Combinations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
nC = n! Compound Data
k (n−k)!k! Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

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

Title Page

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

Page 133 of 644

= n−1Ck−1 + n−1Ck
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 1 Computing: The . . .
Algorithms: Design & . . .

use "fact.sml"; Introducing Reals


Correctness, . . .

exception invalid_arg; Compound Data


Higher Order Functions . . .
Imperative . . .
fun comb_wf (n, k) = A large Example: . . .
Lecture-wise Index to . . .

if n < 0 orelse Home Page

k < 0 orelse Title Page

k > n JJ II

then raise invalid_arg J I

Page 134 of 644


else fact (n) div
Go Back

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

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 2
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data

k < 0 orelse Higher Order Functions . . .


Imperative . . .

k > n A large Example: . . .


Lecture-wise Index to . . .

then raise invalid_arg Home Page

else if n = 0 orelse Title Page

k = 0 orelse JJ II

J I
n = k Page 135 of 644

then 1 Go Back

else (* 0<k<n *) Full Screen

prod (n, n-k+1) div Close

fact (k) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 3
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data

k < 0 orelse Higher Order Functions . . .


Imperative . . .

k > n A large Example: . . .


Lecture-wise Index to . . .

then raise invalid_arg Home Page

else if n = 0 orelse Title Page

k = 0 orelse JJ II

J I
n = k Page 136 of 644

then 1 Go Back

else (* 0<k<n *) Full Screen

prod (n, k+1) div Close

fact (n-k) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect 2
Computing: The . . .
Algorithms: Design & . . .
perf ect(n) Introducing Reals
Correctness, . . .
Compound Data

Pb√nc Higher Order Functions . . .


Imperative . . .

⇐⇒ n = 1 + k=2 if divisor2(k) A large Example: . . .


Lecture-wise Index to . . .

where Home Page

Title Page

if divisor2(k) = JJ II

 k + m if k|n and k = 6 m J I

k if k|n and k = m Page 137 of 644

Go Back

0 otherwise

Full Screen

where m = (n div k) Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Power 2 Introducing Reals


Correctness, . . .
Compound Data

power Higher Order Functions . . .


Imperative . . .
A large Example: . . .

The previous inductive definition used Lecture-wise Index to . . .

Home Page

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

JJ II
n−1 times
J I

We could associate the product differ- Page 138 of 644

ently Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Faster Power
xn = |(x × x ×{z· · · × x)} Computing: The . . .
Algorithms: Design & . . .

n/2 times Introducing Reals


Correctness, . . .

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


Compound Data
Higher Order Functions . . .
Imperative . . .

n/2 times A large Example: . . .


Lecture-wise Index to . . .

when n is even and Home Page

Title Page

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

bn/2c times J I

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


Page 139 of 644

Go Back

bn/2c times Full Screen

× x Close

Quit

when n is odd •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Power2: Complete Introducing Reals


Correctness, . . .
Compound Data

power2(x, n) = Higher Order Functions . . .


Imperative . . .
A large Example: . . .
 Lecture-wise Index to . . .


 1.0/power2(x, n) if n < 0 Home Page
 1.0 if n = 0 Title Page

(power2(x, bn/2c)) 2 if even(n) JJ II




(power2(x, bn/2c))2 × x otherwise
 J I

Page 140 of 644

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

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Power2: SML Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
fun power2 (x, n) = Imperative . . .
A large Example: . . .

if n < 0 Lecture-wise Index to . . .

then 1.0/power2 (x, ˜n) Home Page

Title Page
else if n = 0 JJ II
then 1.0 J I

else Page 141 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Power2: SML
let fun even m = Computing: The . . .
Algorithms: Design & . . .

(m mod 2 = 0); Introducing Reals


Correctness, . . .

fun square y = y * y; Compound Data


Higher Order Functions . . .
Imperative . . .
val pwr_n_by_2 = A large Example: . . .
Lecture-wise Index to . . .

power2 (x, n div 2); Home Page

val sq_pwr_n_by_2 = Title Page

square (pwr_n_by_2) JJ II

in if even (n) J I

Page 142 of 644


then sq_pwr_n_by_2
Go Back

else x * sq_pwr_n_by_2 Full Screen

end Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Computation: Issues
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

1. Correctness Lecture-wise Index to . . .

Home Page

(a) General correctness Title Page

(b) Technical Completeness JJ II

J I
(c) Termination Page 143 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
General Correctness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

1. Mathematical correctness should Compound Data


Higher Order Functions . . .

be established for all algorithmic Imperative . . .


A large Example: . . .

variations. Lecture-wise Index to . . .

Home Page

2. Program Correctness: Mathemati- Title Page

cally developed code should not be JJ II

moved around arbitrarily. J I

Page 144 of 644

• Code variations should also be Go Back

mathematically proven Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code: Justification Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• How does one justify the correct- Correctness, . . .


Compound Data
Higher Order Functions . . .

ness of Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

– this version and Home Page

– this version? Title Page

• Can one correct this version? JJ II

J I

• But first of all, what is incorrect Page 145 of 644

about this version? Go Back

Full Screen
incorrectness
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recall
• A program is an Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

– explicit, Correctness, . . .
Compound Data
Higher Order Functions . . .

– unambiguous and Imperative . . .


A large Example: . . .

– technically complete
Lecture-wise Index to . . .

Home Page

translation of an algorithm written in Title Page

mathematical notation. JJ II

J I
• Moreover, mathematical notation is Page 146 of 644

more concise than a program. Go Back

• Hence mathematical notation is Full Screen

Close

easier to analyse and diagnose. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Features: Definition Computing: The . . .

before Use Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

incorrect version Compound Data


Higher Order Functions . . .
Imperative . . .

Definition of a name before use: A large Example: . . .


Lecture-wise Index to . . .

• if divisor(k) is defined first. Home Page

Title Page

• idivisor(k) uses the name n without JJ II

defining it. J I

• k has been defined (as an argu- Page 147 of 644

Go Back

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

used. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run ifdivisor Computing: The . . .
Algorithms: Design & . . .

Standard ML of New Jersey, Introducing Reals


Correctness, . . .

- fun ifdivisor(k) = Compound Data


Higher Order Functions . . .
Imperative . . .
= if n mod k = 0 A large Example: . . .
Lecture-wise Index to . . .

= then k Home Page

= else 0 Title Page

; JJ II

stdIn:18.8 Error: J I

Page 148 of 644


unbound variable
Go Back

or constructor: n Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Diagnosis: Features of Computing: The . . .
Algorithms: Design & . . .

programs Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
incorrect version Imperative . . .
A large Example: . . .

• So both sum divisors(l, u) and Lecture-wise Index to . . .

Home Page

perf ect(n) may use if divisor(k). Title Page

• sum divisors(l, u) is defined before JJ II

perf ect(n). J I

Page 149 of 644

• So perf ect(n) may use both Go Back

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

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Back to Math
incorrect version
Computing: The . . .

Let Algorithms: Design & . . .


Introducing Reals
 Correctness, . . .

k if k|n Compound Data

if divisor(k) = Higher Order Functions . . .

0 otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

and sum divisors(l, u) = Home Page

 Title Page

0 if l > u JJ II

if divisor(l)+ otherwise J I

sum divisors(l + 1, u)

Page 150 of 644

Go Back

and perf ect(n) ⇐⇒ Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Incorrectness
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
incorrect version Compound Data
Higher Order Functions . . .

• if divisor(k) has a single argument Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

k Home Page

• But it actually depends upon n too! Title Page

JJ II
• But that is not made explicit in its J I

definition. Page 151 of 644

Go Back

Let’s make it explicit! Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ifdivisor3
Let Computing: The . . .
Algorithms: Design & . . .
 Introducing Reals
k if k|n Correctness, . . .

if divisor3(n, k) = Compound Data

0 otherwise Higher Order Functions . . .


Imperative . . .
A large Example: . . .

and sum divisors(l, u) = Lecture-wise Index to . . .

Home Page

0 if l > u Title Page

if divisor3(n, l)+ otherwise JJ II

sum divisors(l + 1, u) J I

Page 152 of 644

and perf ect(n) ⇐⇒ Go Back

Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run it! Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Standard ML of New Jersey Correctness, . . .


Compound Data
Higher Order Functions . . .
- fun ifdivisor3 (n, k) Imperative . . .
A large Example: . . .

= = if (n mod k = 0) Lecture-wise Index to . . .

= then k Home Page

Title Page
= else 0; JJ II
val ifdivisor3 = J I

fn : int * int -> int Page 153 of 644

- Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Try it!
Computing: The . . .
Algorithms: Design & . . .
- fun sum_divisors (l, u) = Introducing Reals
Correctness, . . .

= if l > u Compound Data


Higher Order Functions . . .

= then 0 Imperative . . .
A large Example: . . .

= else ifdivisor3 (n, l)+ Lecture-wise Index to . . .

Home Page

= sum_divisors (l+1, u); Title Page

stdIn:40.18 Error: unbound JJ II

variable or constructor: n J I

- Page 154 of 644

Go Back

Now sum divisors also depends on


Full Screen

n! Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Hey! Wait a minute! Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

But n was defined in ifdivisor3 (n, k)! Lecture-wise Index to . . .

Home Page

So then where is the problem? Title Page

JJ II

Let’s ignore it for the moment and J I

come back to it later Page 155 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The n’s
Computing: The . . .

Let Algorithms: Design & . . .


Introducing Reals
 Correctness, . . .
k if k|n Compound Data

if divisor3(n, k) = Higher Order Functions . . .

0 otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

and sum divisors2(n, l, u) = Home Page

 Title Page

0 if l > u JJ II
if divisor3(n, l)+ otherwise J I

sum divisors(l + 1, u)

Page 156 of 644

Go Back

and perf ect(n) ⇐⇒ Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Scope Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• The scope of a name begins from Higher Order Functions . . .


Imperative . . .

its definition and ends where the A large Example: . . .


Lecture-wise Index to . . .

corresponding scope ends Home Page

• Scopes end with definitions of func-


Title Page

JJ II
tions J I

• Scopes end with the keyword end Page 157 of 644

in any let ... in ...end Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Scope Rules Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Scopes may be disjoint Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Scopes may be nested one Lecture-wise Index to . . .

completely within another Home Page

Title Page

• A scope cannot span two disjoint JJ II

scopes J I

• Two scopes cannot (partly) overlap Page 158 of 644

Go Back

forward
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.4. Names, Scopes & Recursion
Computing: The . . .
1. Disjoint Scopes
Algorithms: Design & . . .
2. Nested Scopes Introducing Reals
Correctness, . . .
3. Overlapping Scopes
Compound Data
4. Spannning Higher Order Functions . . .
Imperative . . .
5. Scope & Names
A large Example: . . .
6. Names & References Lecture-wise Index to . . .

7. Names & References Home Page

8. Names & References


Title Page
9. Names & References
JJ II
10. Names & References
11. Names & References J I

12. Names & References


Page 159 of 644

13. Names & References


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

16. Definition of Names Close

17. Use of Names


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. local...in...end
19. local...in...end
20. local...in...end
Computing: The . . .

21. local...in...end Algorithms: Design & . . .


Introducing Reals
22. Scope & local Correctness, . . .

23. Computations: Simple Compound Data


Higher Order Functions . . .
24. Simple computations Imperative . . .

25. Computations: Composition A large Example: . . .


Lecture-wise Index to . . .
26. Composition: Alternative
Home Page
27. Compositions: Compare
28. Compositions: Compare Title Page

29. Computations: Composition JJ II


30. Recursion
J I
31. Recursion: Left
Page 160 of 644
32. Recursion: Right

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Disjoint Scopes
let Computing: The . . .

val x = 10; Algorithms: Design & . . .


Introducing Reals

fun fun1 y = Correctness, . . .

let
Compound Data
Higher Order Functions . . .

... Imperative . . .
A large Example: . . .

in Lecture-wise Index to . . .

... Home Page

end Title Page

fun fun2 z =
JJ II

let J I
... Page 161 of 644

in
in ... Go Back

end Full Screen

fun1 (fun2 x) Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Nested Scopes
let Computing: The . . .

val x = 10; Algorithms: Design & . . .


Introducing Reals

fun fun1 y = Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .

let A large Example: . . .


Lecture-wise Index to . . .

val x = 15 Home Page

Title Page

in JJ II

x + y J I

end Page 162 of 644

in Go Back

Full Screen

fun1 x Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Overlapping Scopes
let Computing: The . . .

val x = 10; Algorithms: Design & . . .


Introducing Reals

fun fun1 y = Correctness, . . .


Compound Data
Higher Order Functions . . .

... Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

... Home Page

Title Page

JJ II

J I
... Page 163 of 644

in ... Go Back

Full Screen

fun1 (fun2 x) Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Spannning
let Computing: The . . .

val x = 10; Algorithms: Design & . . .


Introducing Reals

fun fun1 y = Correctness, . . .


Compound Data
Higher Order Functions . . .

... Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

... Home Page

Title Page

JJ II
fun fun2 z =
J I
... Page 164 of 644

in ... Go Back

Full Screen

fun1 (fun2 x) Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scope & Names Computing: The . . .
Algorithms: Design & . . .

• A name may occur either as being


Introducing Reals
Correctness, . . .
Compound Data

defined or as a use of a previously Higher Order Functions . . .


Imperative . . .

defined name A large Example: . . .


Lecture-wise Index to . . .

• The same name may be used to re- Home Page

Title Page

fer to different objects. JJ II

• The use of a name refers to the J I

textually most recent definition in Page 165 of 644

the innermost enclosing scope Go Back

Full Screen
diagram
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 166 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 167 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 168 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 169 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 170 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 171 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals


Correctness, . . .
Compound Data

let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val x = 15 Lecture-wise Index to . . .

Home Page

in Title Page

x + y * z JJ II

end J I

in
Page 172 of 644

Go Back

fun1 x Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals

let Correctness, . . .

... Compound Data


Higher Order Functions . . .

in Imperative . . .
A large Example: . . .

... Lecture-wise Index to . . .

end Home Page

Title Page

fun fun2 z =
let
JJ II

... J I

in
... Page 173 of 644

end Go Back

in fun1 (fun2 x) Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals

let Correctness, . . .

... Compound Data


Higher Order Functions . . .

in Imperative . . .
A large Example: . . .

... Lecture-wise Index to . . .

end Home Page

Title Page

fun fun2 z =
let
JJ II

... J I

in
... Page 174 of 644

end Go Back

in fun1 (fun2 x) Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .

fun fun1 y = Introducing Reals

let Correctness, . . .

... Compound Data


Higher Order Functions . . .

in Imperative . . .
A large Example: . . .

... Lecture-wise Index to . . .

end Home Page

Title Page

fun fun2 z =
let
JJ II

... J I

in
... Page 175 of 644

end Go Back

in fun1 (fun2 x) Full Screen

end Close

Quit

Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Definition of Names Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Definitions are of the form Higher Order Functions . . .
Imperative . . .

qualifier name . . . = body A large Example: . . .


Lecture-wise Index to . . .

• val name = Home Page

Title Page

• fun name ( argnames ) = JJ II

• local def initions J I

in def inition Page 176 of 644

Go Back

end Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Use of Names Computing: The . . .
Algorithms: Design & . . .

Names are used in expressions. Introducing Reals


Correctness, . . .

Expressions may occur Compound Data


Higher Order Functions . . .
Imperative . . .

• by themselves – to be evaluated
A large Example: . . .
Lecture-wise Index to . . .

Home Page

• as the body of a definition Title Page

• as the body of a let-expression JJ II

let def initions J I

Page 177 of 644

in expression Go Back

end Full Screen

use of local Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect Computing: The . . .
Algorithms: Design & . . .

local Introducing Reals


Correctness, . . .

exception invalidArg; Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

fun ifdivisor3 (n, k) = Home Page

if n <= 0 orelse Title Page

k <= 0 orelse JJ II

n < k J I

then raise invalidArg Page 178 of 644

Go Back
else if n mod k = 0 Full Screen

then k Close

else 0; Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect
Computing: The . . .

fun sum_div2 (n, l, u) = Algorithms: Design & . . .


Introducing Reals

if n <= 0 orelse Correctness, . . .


Compound Data
Higher Order Functions . . .
l <= 0 orelse Imperative . . .
A large Example: . . .

l > n orelse Lecture-wise Index to . . .

u <= 0 orelse Home Page

Title Page
u > n JJ II
then raise invalidArg J I

else if l > u Page 179 of 644

then 0 Go Back

else ifdivisor3 (n, l) Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect Computing: The . . .
Algorithms: Design & . . .

in Introducing Reals
Correctness, . . .

fun perfect n = Compound Data


Higher Order Functions . . .
Imperative . . .
if n <= 0 A large Example: . . .
Lecture-wise Index to . . .

then raise invalidArg Home Page

else Title Page

let JJ II

val nby2 = n div 2 J I

in Page 180 of 644

Go Back

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

end Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect
Computing: The . . .
Standard ML of New Jersey, Algorithms: Design & . . .
Introducing Reals

- use "perfect2.sml"; Correctness, . . .


Compound Data

[opening perfect2.sml] Higher Order Functions . . .


Imperative . . .

GC #0.0.0.0.1.10: (1 ms) A large Example: . . .


Lecture-wise Index to . . .

val perfect = fn : int -> bool Home Page

val it = () : unit Title Page

JJ II
- perfect 28;
J I
val it = true : bool Page 181 of 644

- perfect 6; Go Back

val it = true : bool Full Screen

- perfect 8128; Close

val it = true : bool Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scope & local
local Computing: The . . .

fun fun1 y = Algorithms: Design & . . .

... Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

fun fun2 z = ... Home Page

fun1 Title Page

JJ II

J I

in Page 182 of 644

fun fun3 x = ... Go Back

fun2 ... Full Screen

fun1 ... Close

end Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computations: Simple Computing: The . . .
Algorithms: Design & . . .

For most simple expressions it is Introducing Reals


Correctness, . . .
Compound Data

• left to right, and Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• top to bottom Lecture-wise Index to . . .

Home Page

except when Title Page

• presence of brackets JJ II

J I

• precedence of operators Page 183 of 644

determine otherwise. Go Back

Full Screen

Hence Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Simple computations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

4 + 6 − (4 + 6) div 2
A large Example: . . .
Lecture-wise Index to . . .

= 10 − (4 + 6) div 2 Home Page

= 10 − 10 div 2 Title Page

JJ II
= 10 − 5 J I

= 5 Page 184 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computations: Composition
Computing: The . . .

f (x) = x2 + 1 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

g(x) = 3 ∗ x + 2 Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Then for any value a = 4 Lecture-wise Index to . . .

Home Page

f (g(a)) Title Page

= f (3 ∗ 4 + 2) JJ II

= f (14) J I

= 142 + 1 Page 185 of 644

= 196 + 1 Go Back

Full Screen

= 197 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Composition:
Alternative
Computing: The . . .

f (x) = x2 + 1
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

g(x) = 3 ∗ x + 2 Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Why not Lecture-wise Index to . . .

Home Page

f (g(a)) Title Page

= g(4)2 + 1 JJ II

= (3 ∗ 4 + 2)2 + 1 J I

(12 + 2)2 + 1
Page 186 of 644

= Go Back

= 142 + 1 Full Screen

= 196 + 1 Close

= 197 Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compositions: Computing: The . . .

Compare
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
f (g(a)) f (g(a)) A large Example: . . .

= f (3 ∗ 4 + 2) = g(4)2 + 1
Lecture-wise Index to . . .

Home Page

= f (14) = (3 ∗ 4 + 2)2 + 1 Title Page

= = (12 + 2)2 + 1 JJ II

= 142 + 1 = 142 + 1 J I

Page 187 of 644

= 196 + 1 = 196 + 1 Go Back

= 197 = 197 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Compositions: Correctness, . . .
Compound Data
Higher Order Functions . . .

Compare Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Question 1: Which is more correct? Home Page

Why? Title Page

JJ II
Question 2: Which is easier to imple- J I
ment?
Question 3: Which is more efficient? Page 188 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Computations: Introducing Reals


Correctness, . . .
Compound Data

Composition Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
A computation of f (g(a)) proceeds Home Page

thus: Title Page

• g(a) is evaluated to some value, say JJ II

J I
b
Page 189 of 644

• f (b) is next evaluated Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Recursion Compound Data


Higher Order Functions . . .
Imperative . . .
 A large Example: . . .

1 if n = 0 Lecture-wise Index to . . .

f actL(n) =
f actL(n − 1) ∗ n otherwise
Home Page

Title Page

1 if n = 0 JJ II

f actR(n) =
n ∗ f actR(n − 1) otherwise J I

Page 190 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Left
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

f actL(4) Higher Order Functions . . .


Imperative . . .

= (f actL(4 − 1) ∗ 4) A large Example: . . .


Lecture-wise Index to . . .

= (f actL(3) ∗ 4) Home Page

= ((f actL(3 − 1) ∗ 3) ∗ 4) Title Page

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

J I
= (((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Page 191 of 644

= ··· Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Right Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

f actR(4) Higher Order Functions . . .


Imperative . . .

(4 ∗ f actR(4 − 1))
A large Example: . . .
= Lecture-wise Index to . . .

= (4 ∗ f actR(3)) Home Page

= (4 ∗ (3 ∗ f actR(3 − 1))) Title Page

JJ II
= (4 ∗ (3 ∗ f actR(2))) J I

= (4 ∗ (3 ∗ (2 ∗ f actR(2 − 1)))) Page 192 of 644

= ··· Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3. Introducing Reals
Computing: The . . .
Algorithms: Design & . . .
3.1. Floating Point
Introducing Reals
1. So Far-1: Computing Correctness, . . .
Compound Data
2. So Far-2: Algorithms & Programs
Higher Order Functions . . .
3. So far-3: Top-down Design Imperative . . .
A large Example: . . .
4. So Far-4: Algorithms to Programs
Lecture-wise Index to . . .
5. So far-5: Caveats
Home Page
6. So Far-6: Algorithmic Variations
7. So Far-7: Computations Title Page

8. Floating Point JJ II
9. Real Operations
J I
10. Real Arithmetic
Page 193 of 644
11. Numerical Methods
12. Errors Go Back

13. Errors Full Screen

14. Infinite Series


Close
15. Truncation Errors
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Equation Solving
17. Root Finding-1
18. Root Finding-2
Computing: The . . .

19. Root Finding-3 Algorithms: Design & . . .


Introducing Reals
20. Root Finding-4 Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 194 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

So Far-1: Computing Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
• The general nature of computation Imperative . . .
A large Example: . . .

• The notion of primitives, composi-


Lecture-wise Index to . . .

Home Page

tion & induction Title Page

• The notion of an algorithm JJ II

J I
• The digital computer & program- Page 195 of 644

ming language Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-2: Algorithms & Computing: The . . .
Algorithms: Design & . . .

Programs Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

• Algorithms: Finite mathematical Imperative . . .


A large Example: . . .

processes Lecture-wise Index to . . .

Home Page

• Programs: Precise, unambiguous Title Page

explications of algorithms JJ II

J I
• Standard ML: Its primitives Page 196 of 644

• Writing technically complete speci- Go Back

fications Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So far-3: Top-down
Design Computing: The . . .

integer Square Root Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

• Begin with the function you need to Compound Data


Higher Order Functions . . .

design Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Write a Home Page

– small compact technically com- Title Page

JJ II
plete definition of the function
J I

– perhaps using other functions Page 197 of 644

that have not yet been defined Go Back

• Each function in turn is defined in a Full Screen

Close

top-down manner Quit

Perfect
•First Numbers
•Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-4: Algorithms to Computing: The . . .
Algorithms: Design & . . .

Programs
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

• Perform top development till you re- Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

quire only the available primitives Home Page

• Directly translate the algorithm into Title Page

a Program JJ II

J I

• Use scope rules to localize or gen- Page 198 of 644

eralize Go Back

SML code for perfect Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So far-5: Caveats Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Don’t arbitrarily vary code from your Compound Data


Higher Order Functions . . .

algorithmic development Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

– It might work or Home Page

– It might not work Title Page

JJ II
– unless properly justified J I

• May destroy technical complete- Page 199 of 644

ness Go Back

Full Screen

• May create scope violations. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-6: Algorithmic Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Variations Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Algorithmic Variations A large Example: . . .


Lecture-wise Index to . . .

• Are safe if developed from first prin- Home Page

Title Page
ciples. Thus ensuring their JJ II

– mathematical correctness J I

– technical completeness Page 200 of 644

Go Back

– termination properties Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-7:
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Computations Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• Work within the notion of mathe- A large Example: . . .


Lecture-wise Index to . . .

matical equality Home Page

– Simple expressions Title Page

JJ II
– Composition of functions J I

– Recursive computations Page 201 of 644

• But are generally irreversible


Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Floating Point
• Each real number 3E11 is repre- Computing: The . . .
Algorithms: Design & . . .

sented by a pair of integers Introducing Reals


Correctness, . . .
Compound Data

1. Mantissa: 3 or 30 or 300 or . . . Higher Order Functions . . .


Imperative . . .
A large Example: . . .

2. Exponent: the power of 10 which Lecture-wise Index to . . .

the mantissa has to be multiplied Home Page

Title Page
by
JJ II

• What is displayed is not necessarily J I

the same as the internal represen- Page 202 of 644

tation. Go Back

Full Screen

• There is no unique representation Close

of a real number Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Operations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Depending upon the operations in- Correctness, . . .


Compound Data
Higher Order Functions . . .

volved Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Each real number is first converted Home Page

into a suitable representation Title Page

• The operation is performed JJ II

J I

• The result is converted into a suit- Page 203 of 644

able representation for display. Go Back

skip to Numerical methods Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Arithmetic
• for addition and subtraction the two Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

numbers should have the same ex- Correctness, . . .


Compound Data

ponent for ease of integer opera- Higher Order Functions . . .


Imperative . . .

tions to be performed A large Example: . . .


Lecture-wise Index to . . .

• the conversion may involve loss of Home Page

Title Page

precision JJ II

• for multiplication and division the J I

exponents may have to be adjusted Page 204 of 644

so as not to cause an integer over- Go Back

Full Screen

flow or underflow. Close

back Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Numerical Methods
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Finite (limited) precision Compound Data


Higher Order Functions . . .
Imperative . . .

• Accuracy depends upon available A large Example: . . .


Lecture-wise Index to . . .

precision Home Page

Title Page

• Whereas integer arithmetic is exact, JJ II


real arithmetic is not. J I

• Numerical solutions are a Page 205 of 644

finite approximation of the result Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Errors Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

• Hence an estimate of the error is Imperative . . .


A large Example: . . .

necessary. Lecture-wise Index to . . .

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


Home Page

Title Page

the computed value, JJ II

absolute error = a∗ − a J I

a∗−a Page 206 of 644

relative error = a Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Errors Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Errors in floating point computations Compound Data


Higher Order Functions . . .

are mainly due Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

finite precision Round-off errors Home Page

fnite process It is impossible to com- Title Page

JJ II
pute the value of a (convergent) in-
J I
finite series because computations Page 207 of 644

are themselves finite processes. In- Go Back

finite series Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Infinite Series
cannot be computed to ∞ Computing: The . . .
Algorithms: Design & . . .
∞ m Introducing Reals
X x
ex
Correctness, . . .

= Compound Data

m! Higher Order Functions . . .

m=0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .


(−1)mx2m
Home Page
X
cos x = Title Page

(2m)! JJ II
m=0
J I

∞ Page 208 of 644

X (−1)mx2m+1 Go Back
sin x =
(2m + 1)! Full Screen

m=0 Close

Quit
Truncation
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Truncation Errors
and hopefully it is good enough to re-
strict it to appropriate values of n Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
n
xm
Correctness, . . .
X
ex
Compound Data

= Higher Order Functions . . .

m! Imperative . . .
A large Example: . . .
m=0 Lecture-wise Index to . . .

Home Page

n
X (−1)mx2m Title Page

cos x = JJ II
(2m)!
m=0 J I

Page 209 of 644

n
X (−1)mx2m+1 Go Back

sin x = Full Screen

(2m + 1)!
m=0 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Equation Solving Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• The fifth most basic operation Lecture-wise Index to . . .

Home Page

• Root finding: A particular form of Title Page

equation solving JJ II

J I
f (x) = 0 Page 210 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-1
Computing: The . . .

f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

x0 Home Page

a Title Page

b JJ II

J I

Page 211 of 644

f(a) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-2
Computing: The . . .

f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

x0 Home Page

a Title Page

b JJ II

J I

Page 212 of 644

f(a) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-3
Computing: The . . .

f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

x0 Home Page

a Title Page

b JJ II

J I

Page 213 of 644

f(a) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-4
Rather steep isn’t it?
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

f(b) Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

x0
Home Page

Title Page

a
JJ II

b J I

Page 214 of 644

Go Back

f(a) Full Screen

Close

ε Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3.2. Root Finding, Composition and Recursion
Computing: The . . .
1. Root Finding: Newton’s Method
Algorithms: Design & . . .
2. Root Finding: Newton’s Method Introducing Reals
Correctness, . . .
3. Root Finding: Newton’s Method
Compound Data
4. Root Finding: Newton’s Method Higher Order Functions . . .
Imperative . . .
5. Root Finding: Newton’s Method
A large Example: . . .
6. Root Finding: Newton’s Method Lecture-wise Index to . . .

7. Newton’s Method: Basis Home Page

8. Newton’s Method: Basis


Title Page
9. Newton’ Method: Algorithm
JJ II
10. What can go wrong!-1
11. What can go wrong!-2 J I

12. What can go wrong!-2


Page 215 of 644

13. What can go wrong!-3


Go Back
14. What can go wrong!-4
15. Real Computations & Induction: 1 Full Screen

16. Real Computations & Induction: 2 Close

17. What’s it good for? 1


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. What’s it good for? 2
19. newton: Computation
20. Generalized Composition
Computing: The . . .

21. Two Computations of h(1) Algorithms: Design & . . .


Introducing Reals
22. Two Computations of h(−1) Correctness, . . .

23. Recursive Computations Compound Data


Higher Order Functions . . .
24. Recursion: Left Imperative . . .

25. Recursion: Right A large Example: . . .


Lecture-wise Index to . . .
26. Recursion: Nonlinear
Home Page
27. Some Practical Questions
28. Some Practical Questions Title Page

JJ II

J I

Page 216 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s Computing: The . . .
Algorithms: Design & . . .

Method Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

Consider a function f (x) Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• smooth and continuously differen- Home Page

tiable over [a, b] Title Page

• with a non-zero derivative f 0(x) ev-


JJ II

J I
erywhere in [a, b] Page 217 of 644

• the signs of f (a) and f (b) are differ- Go Back

ent Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

a Page 218 of 644

b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

a xi
Page 219 of 644

b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I
xi+1 xi
a αi Page 220 of 644

b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I
xi+1 xi
a αi Page 221 of 644

b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I
xi+1 xi
a αi Page 222 of 644

xi+2 b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’s Method:
Basis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

f (x ) Correctness, . . .

tan αi = f 0(xi) = x −xi Compound Data


Higher Order Functions . . .
i i+1 Imperative . . .

whence A large Example: . . .


Lecture-wise Index to . . .

f (x )
xi+1 = xi − f 0(xi ) Home Page

i Title Page

Starting from an initial value x0 ∈ [a, b], JJ II

if the sequence f (xi) converges to 0 J I

Page 223 of 644


i.e Go Back

f (x0), f (x1), f (x2), · · · → 0 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’s Method:
Basis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

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

n→∞ Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

i.e.∀ε > 0 : ∃N ≥ 0 : ∀n > N : Home Page

Title Page

JJ II

|f (xn)| < ε J I

then the sequence Page 224 of 644

Go Back

x 0 , x1 , x2 , · · · Full Screen

converges to a root of f .
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’ Method:
Algorithm
Computing: The . . .

Select a small enough ε > 0 and x0. Algorithms: Design & . . .


Introducing Reals

Then Correctness, . . .
Compound Data

newton(f, f 0, a, b, ε, xi) =
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
 Lecture-wise Index to . . .

xi if |f (xi)| < ε Home Page

newton(f, f 0, a, b, ε, xi+1) otherwise Title Page

JJ II
where J I
x0 ∈ [a, b] Page 225 of 644

and Go Back

Full Screen
f (xi)
xi+1 = xi − 0 ∈ [a, b] Close

f (xi) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-1
Computing: The . . .

Oscillations! Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I
xi+1 xi
a αi Page 226 of 644

xi+2 b Go Back

f(a) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

What can go wrong!-2 Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

An intermediate point may lie outside Lecture-wise Index to . . .

[a, b]! The function may not satisfy all Home Page

Title Page

the assumptions outside [a, b]. There JJ II


are then no guarantees about the be- J I

haviour of the function. Page 227 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-2
Computing: The . . .

Interval bounds error! Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I
xi+1 xi
a αi Page 228 of 644

b Go Back

f(a) xi+2 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-3
The function may be too steep Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

f(b)
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

x0 Home Page

a Title Page

b JJ II

J I

Page 229 of 644


f(a)
Go Back

Full Screen
ε
Close

for the available precision. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-4
Computing: The . . .

Or too shallow! Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

x0
f(b) Title Page

a
JJ II
b
f(a) J I

Page 230 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Computations &
Induction: 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Newton’s method (when it does work!) Correctness, . . .


Compound Data

computes a sequence Higher Order Functions . . .


Imperative . . .
A large Example: . . .

x 0 , x 1 , x 2 , . . . xn Lecture-wise Index to . . .

Home Page

of essentially discrete values such Title Page

JJ II
that even if the sequence is not totally
J I
ordered, there is some discrete con- Page 231 of 644

vergence measure viz. Go Back

|f (xi) − 0|
Full Screen

Close

which is well-founded. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Computations & Computing: The . . .

Induction: 2 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

That is, for some decreasing se- Higher Order Functions . . .


Imperative . . .

quence of integers ki ≥ 0, A large Example: . . .


Lecture-wise Index to . . .

Home Page

k0 > k 1 > k 2 > · · · > k n = 0 Title Page

JJ II
we have J I

kiε ≤ |f (xi) − 0| < (ki + 1)ε Page 232 of 644

Go Back

and therefore inductive on integer Full Screen

multiples of ε Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What’s it good for? 1

n
Finding the positive n-th root c of a Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
c > 0 and n > 1 amounts to solving Correctness, . . .
Compound Data

the equation Higher Order Functions . . .


Imperative . . .

xn = c
A large Example: . . .
Lecture-wise Index to . . .

Home Page

which is equivalent to finding the root Title Page

of f (x) with JJ II

f (x) = xn − c
J I

f 0(x) = nxn−1
Page 233 of 644

Go Back


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

Close

propriately chosen ε. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What’s it good for? 2
Finding roots of polynomials. Computing: The . . .
Algorithms: Design & . . .
n Introducing Reals

ci xi
X Correctness, . . .

f (x) = Compound Data


Higher Order Functions . . .
Imperative . . .
i=0 A large Example: . . .
Lecture-wise Index to . . .

n Home Page

icixi−1
X
f 0(x) = Title Page

JJ II
i=1 J I

and Page 234 of 644

Go Back
• an appropriately chosen ε, Full Screen

• an appropriately chosen [a, b] with Close

one of the limits possibly being c0. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
newton : Computation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

newton(f, f 0, a, b, ε, x0) Higher Order Functions . . .


Imperative . . .

newton(f, f 0, a, b, ε, x1)
A large Example: . . .
Lecture-wise Index to . . .

newton(f, f 0, a, b, ε, x2) Home Page

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

.. .. .. JJ II

J I

newton(f, f 0, a, b, ε, xn) Page 235 of 644

xn Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generalized
Composition
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Computations Compound Data


Higher Order Functions . . .
Imperative . . .

h(x) = f (x, g(x)) A large Example: . . .


Lecture-wise Index to . . .

where 
Home Page

0 if x < 0 Title Page

f (x, y) = JJ II
y otherwise J I

 Page 236 of 644

0 if x = 0 Go Back
g(x) =
g(x − 1) + 1 otherwise Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Two Computations of Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

h(1) Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

h(1) | h(1) Lecture-wise Index to . . .

f (1, g(1)) | f (1, g(1)) Home Page

Title Page

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

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

0+1 | f (1, 1) Page 237 of 644

1 | 1 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Two Computations of Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

h(−1) Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

h(−1) | h(−1) Lecture-wise Index to . . .

f (−1, g(−1)) | f (−1, g(−1)) Home Page

Title Page

0 | f (−1, (g(−2) + 1)) JJ II

DONE! | f (−1, ((g(−3) + 1) + 1 J I

| ... Page 238 of 644

| FOREVER! Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursive Computing: The . . .
Algorithms: Design & . . .

Computations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
• Newton’s method Home Page

• Factorial Title Page

JJ II
– f actL J I

– f actR Page 239 of 644

skip to nonlinear recursion Go Back

skip to Recursion Revisited Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Left
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

f actL(4) Higher Order Functions . . .


Imperative . . .

(f actL(4 − 1) ∗ 4) A large Example: . . .


Lecture-wise Index to . . .

(f actL(3) ∗ 4) Home Page

((f actL(3 − 1) ∗ 3) ∗ 4) Title Page

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

J I
(((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Page 240 of 644

··· Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Right Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

f actR(4) Higher Order Functions . . .


Imperative . . .

(4 ∗ f actR(4 − 1))
A large Example: . . .
Lecture-wise Index to . . .

(4 ∗ f actR(3)) Home Page

(4 ∗ (3 ∗ f actR(3 − 1))) Title Page

JJ II
(4 ∗ (3 ∗ f actR(2))) J I

(4 ∗ (3 ∗ (2 ∗ f actR(2 − 1)))) Page 241 of 644

··· Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Nonlinear Computing: The . . .
Algorithms: Design & . . .

Fibonacci Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
f ib(5) Imperative . . .
A large Example: . . .

f ib(4) + f ib(3) Lecture-wise Index to . . .

(f ib(3) + f ib(2)) + f ib(3) Home Page

Title Page

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

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

(((1 + f ib(0)) + f ib(1)) + f ib(2)) + f ib(3) Page 242 of 644

··· Go Back

Full Screen
contd ...
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Some Practical
Questions
• What is the essential difference be- Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

tween the computations of newton Correctness, . . .


Compound Data

and the two factorial programs? Higher Order Functions . . .


Imperative . . .

Answer: Constant space vs. Linear A large Example: . . .


Lecture-wise Index to . . .

space Home Page

Title Page

• What is the essential similarity be- JJ II

tween the computations of f actL J I

and f actR? Answer Page 243 of 644

• Why can’t we calculate beyond Go Back

Full Screen

f ib(43) using the definition Fi- Close

bonacci, on ccsun50 or a P-IV? Quit

Answer • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
Computing: The . . .

Some Practical Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Questions Compound Data


Higher Order Functions . . .
Imperative . . .

• What does a computation of Fi- A large Example: . . .


Lecture-wise Index to . . .

bonacci look like? Home Page

Title Page

• What is the essential difference JJ II

between the computations of Fi- J I

bonacci and newton or f actL or Page 244 of 644

f actR? Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. Correctness, Termination & Complexity
4.1. Termination and Space Complexity
1. Recursion Revisited Computing: The . . .
Algorithms: Design & . . .
2. Linear Recursion: Waxing
Introducing Reals

3. Recursion: Waning Correctness, . . .


Compound Data
4. Nonlinear Recursions Higher Order Functions . . .

5. Fibonacci: contd Imperative . . .


A large Example: . . .
6. Recursion: Waxing & Waning Lecture-wise Index to . . .

7. Unfolding Recursion
Home Page
8. Non-termination
Title Page
9. Termination
10. Proofs of termination JJ II

11. Proofs of termination: Induction J I


12. Proof of termination: Factorial
Page 245 of 644
13. Proof of termination: Factorial
Go Back
14. Fibonacci: Termination
15. GCD computations Full Screen

16. Well-foundedness: GCD


Close
17. Well-foundedness
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Induction is Well-founded
19. Induction is Well-founded
20. Where it doesn’t work
Computing: The . . .
21. Well-foundedness is inductive Algorithms: Design & . . .
Introducing Reals
22. Well-foundedness is inductive
Correctness, . . .
23. GCD: Well-foundedness Compound Data

24. Newton: Well-foundedness Higher Order Functions . . .


Imperative . . .
25. Newton: Well-foundedness A large Example: . . .

26. Example: Zero Lecture-wise Index to . . .

27. Questions Home Page

28. The Collatz Problem


Title Page
29. Questions
30. Space Complexity JJ II

31. Newton & Euclid: Absolute J I


32. Newton & Euclid: Relative
Page 246 of 644
33. Deriving space requirements
34. GCD: Space Go Back

35. Factorial: Space Full Screen

36. Fibonacci: Space


Close
37. Fibonacci: Space
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Recursion Revisited
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• Linear recursions Lecture-wise Index to . . .

Home Page

– Waxing Title Page

– Waning JJ II

J I
• Non-linear recursion Page 247 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Linear Recursion: Algorithms: Design & . . .


Introducing Reals

Waxing Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

f actL(4) Lecture-wise Index to . . .

Home Page

(f actL(3) ∗ 4) Title Page

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

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

((((f actL(0) ∗ 1) ∗ 2) ∗ 3) ∗ 4) Page 248 of 644

Go Back
contrast with newton
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Recursion: Waning Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

((((1 ∗ 1) ∗ 2) ∗ 3) ∗ 4) A large Example: . . .


Lecture-wise Index to . . .

(((1 ∗ 2) ∗ 3) ∗ 4) Home Page

((2 ∗ 3) ∗ 4) Title Page

(6 ∗ 4) JJ II

J I
24 Page 249 of 644

contrast with newton Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Nonlinear Recursions
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Fibonacci A large Example: . . .
Lecture-wise Index to . . .

• Each computation of f ib has its own Home Page

waxing and waning Title Page

JJ II

• There is still an “envelope” which J I

shows waxing and waning. Page 250 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Fibonacci: contd Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

(((1 + 1) + f ib(1)) + f ib(2)) + f ib(3) Lecture-wise Index to . . .

Home Page

(2 + f ib(1)) + f ib(2)) + f ib(3) Title Page

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

··· J I

Page 251 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Waxing &
Waning Computing: The . . .

• Waning: Occurs when an expres- Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

sion is simplified without requiring Compound Data


Higher Order Functions . . .

replacement of names by defini- Imperative . . .


A large Example: . . .

tions. Lecture-wise Index to . . .

Home Page

• Waxing: Occurs when a name is Title Page

replaced by its definition. JJ II

J I
– name by value replacements Page 252 of 644

– occurs in generalized composi- Go Back

tion but just once if it is not recur- Full Screen

sively defined Close

Quit

– Unfolding recursion • • • • •
First Prev Next Last Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Unfolding Recursion Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• may occur several times (terminat-


Lecture-wise Index to . . .

Home Page

ing), or Title Page

• even an infinite number of times JJ II

leading to nontermination J I

Page 253 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Non-termination Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Algorithm Correctness, . . .
Compound Data
Higher Order Functions . . .

• Simple expressions never lead to Imperative . . .


A large Example: . . .

nontermination Lecture-wise Index to . . .

Home Page

• (Generalized) composition never Title Page

leads to nontermination JJ II

J I
• Recursion may lead to non- Page 254 of 644

termination or infinite computations, Go Back

unless proved otherwise Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Termination Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

Since recursion may lead to nontermi- Higher Order Functions . . .


Imperative . . .

nation A large Example: . . .


Lecture-wise Index to . . .

• Termination needs to be proved for Home Page

recursive definitions, and


Title Page

JJ II

• for expressions and definitions that J I

use recursively defined names as Page 255 of 644

components. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Proofs of termination
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

A recursive definition guarantees ter- Lecture-wise Index to . . .

Home Page

mination Title Page

• if it is inductive, or JJ II

J I
• it is well-founded Page 256 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proofs of termination:
Induction
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

A recursive definition guarantees ter- Compound Data


Higher Order Functions . . .
Imperative . . .

mination A large Example: . . .


Lecture-wise Index to . . .

• if it is inductive, Home Page

Examples: Title Page

JJ II
– Factorial J I

– Fibonacci Page 257 of 644

Go Back

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

ously inductive Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof of termination: Computing: The . . .

Factorial Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Factorial Higher Order Functions . . .
Imperative . . .

Consider f actL defined only for non- A large Example: . . .


Lecture-wise Index to . . .

negative integers. We prove that it is Home Page

an algorithm i.e. that it terminates Title Page

JJ II

Basis : For n = 0, f actL(n) = 1 which J I

is not a recursive definition. Hence Page 258 of 644

it does indeed terminate in a single Go Back

step. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof of termination: Computing: The . . .
Algorithms: Design & . . .

Factorial Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Induction hypothesis . For some n > Imperative . . .
A large Example: . . .

0, ∀k : 0 ≤ k ≤ n : f actL(k) termi- Lecture-wise Index to . . .

nates in ∝ k steps. Home Page

Title Page

Induction step . Then f actL(n + 1) = JJ II

f actL(n) ∗ (n + 1) is guaranteed to J I

terminate in ∝ (n + 1) steps, since Page 259 of 644

f actL(n) does so in ∝ n steps. Go Back

Full Screen
back
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Termination
Fibonacci
Computing: The . . .

The proof is similar to that of f actL. Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

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


Higher Order Functions . . .
Imperative . . .

Induction hypothesis For some n > 0, A large Example: . . .


Lecture-wise Index to . . .

∀k : 0 ≤ k ≤ n : f ib(k) terminates in Home Page

∝ f (k) steps Title Page

Induction Step Then since each of JJ II

J I
f ib(n) and f ib(n − 1) is guaranteed Page 260 of 644

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

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

1) is also guaranteed to terminate in Close

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

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

GCD computations Introducing Reals


Correctness, . . .
Compound Data

Euclidean GCD Higher Order Functions . . .


Imperative . . .
A large Example: . . .

gcd(12, 64) Lecture-wise Index to . . .

Home Page

gcd(64, 12) Title Page

gcd(12, 4) JJ II

gcd(4, 0) J I

4 Page 261 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness:
GCD Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Euclidean GCD Correctness, . . .


Compound Data
Higher Order Functions . . .

For x, y > 0, 0 ≤ x mod y < y. Hence Imperative . . .


A large Example: . . .

the sequence of remainders obtained Lecture-wise Index to . . .

Home Page

is Title Page

• a sequence of non-negative integers, JJ II

and J I

Page 262 of 644

• is strictly decreasing Go Back

Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness Computing: The . . .

A definition is well-founded if it is pos- Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

sible to define a measure (i.e. a func- Compound Data


Higher Order Functions . . .

tion w of its arguments) called the Imperative . . .


A large Example: . . .

well-founded function such that Lecture-wise Index to . . .

Home Page

1. the well-founded function takes only Title Page

non-negative integer values JJ II

J I
2. with each successive recursive call Page 263 of 644

the value of the well-founded func- Go Back

tion is guaranteed to decrease by at Full Screen

least 1. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Induction is Computing: The . . .
Algorithms: Design & . . .

Well-founded Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

The well-founded function usually is a Imperative . . .


A large Example: . . .

measure of the number of computa- Lecture-wise Index to . . .

Home Page

tion steps that the algorithm will take Title Page

to terminate JJ II

• Factorial w(n) ∝ n J I

Page 264 of 644

• Fibonacci w(n) ∝ f (n) Go Back

Then Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Induction is Algorithms: Design & . . .


Introducing Reals

Well-founded
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• w(n) is always non-negative if f actL A large Example: . . .


Lecture-wise Index to . . .

and f ib are defined only for non- Home Page

negative integers Title Page

JJ II

• The argument to f actL and f ib in J I

each recursive unfolding is strictly Page 265 of 644

decreasing. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Where it doesn’t work Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Such proofs do not work for Correctness, . . .


Compound Data
Higher Order Functions . . .

• f act arbitrarily extended to include Imperative . . .


A large Example: . . .

negative integers. (since w(n) no Lecture-wise Index to . . .

longer strictly non-negative) Home Page

Title Page

• f act(n) = f act(n+1) div (n+1), even JJ II

if n is non-negative (since w(n) is no J I

longer decreasing) Page 266 of 644

Go Back

since the function is no longer well- Full Screen

founded. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Well-foundedness is Introducing Reals


Correctness, . . .

inductive Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

But the induction variable is Lecture-wise Index to . . .

Home Page

• hidden or Title Page

• too complex to worry about, or JJ II

J I
• it serves no useful purpose for the Page 267 of 644

algorithm, except as a counter. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness is
inductive Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Given any well-founded function w(~x) Correctness, . . .


Compound Data

whose values form a decreasing se- Higher Order Functions . . .


Imperative . . .

quence in some algorithm A large Example: . . .


Lecture-wise Index to . . .

Home Page

y0 > y1 > · · · > yn−1 > yn ≥ 0 Title Page

it is possible to put this sequence JJ II

J I
in 1-1 correspondence with the set Page 268 of 644

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

ind(w(~x)) = n − i
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

GCD:
Correctness, . . .
Compound Data
Higher Order Functions . . .

Well-foundedness Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

GCD Home Page

Well-founded function for gcd Title Page

JJ II

w(a, b) = b J I

Page 269 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton:
Well-foundedness Computing: The . . .
Algorithms: Design & . . .

Newton’s Method Introducing Reals


Correctness, . . .
Compound Data

Convergence condition Higher Order Functions . . .


Imperative . . .
A large Example: . . .

f (x0), f (x1), f (x2), · · · → 0 Lecture-wise Index to . . .

Home Page

Compute the discrete value sequence Title Page

JJ II

x 0 , x 1 , x 2 , . . . xn J I

Page 270 of 644

such that Go Back

k0 > k 1 > k 2 > · · · > k n = 0 Full Screen

Close

where Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton: Computing: The . . .
Algorithms: Design & . . .

Well-foundedness Introducing Reals


Correctness, . . .
Compound Data

Newton’s Method Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

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

Title Page

and therefore inductive on integer JJ II


multiples of ε Hence J I

|f (x)| Page 271 of 644

w(x) = b c Go Back

ε Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Example: Zero Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

A peculiar way to define the zero func- Compound Data


Higher Order Functions . . .

tion Imperative . . .
A large Example: . . .

zero(x) = Lecture-wise Index to . . .

Home Page

 zero(x + 1.0) if x ≤ −1.0 Title Page

0.0 if −1.0 < x < 1.0 JJ II

zero(x − 1.0) if x ≥ 1.0 J I



Page 272 of 644

w(x) = d|x|e is the well-founded func- Go Back

tion Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Questions Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

Q: Is it always possible to find a well- Higher Order Functions . . .


Imperative . . .

founded function for each algorithm? A large Example: . . .


Lecture-wise Index to . . .

Home Page

A: Unfortunately not! However if we Title Page


can’t then we cannot call it an algo- JJ II
rithm!. But if we can then we are guar-
J I
anteed that the algorithm will termi-
nate. Page 273 of 644

Go Back
The Collatz Problem
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Collatz Problem Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Does the following algorithm termi- Compound Data


Higher Order Functions . . .

nate? Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
collatz(m) = Home Page

1 if m ≤ 1 Title Page

collatz(m div 2) if m is even JJ II

J I
collatz(3 ∗ m + 1) otherwise

Page 274 of 644

Unproven Claim. collatz(m) 1 for all Go Back

m. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Questions
Q: what other uses can well-founded Computing: The . . .
Algorithms: Design & . . .

functions be put to? Introducing Reals


Correctness, . . .
Compound Data

A: They can be used to estimate Higher Order Functions . . .


Imperative . . .

the complexity of your algorithm in A large Example: . . .


Lecture-wise Index to . . .

order of magnitude terms. Home Page

Title Page
Space Complexity : The amount of
JJ II
memory space required, as a func- J I

tion of the input Page 275 of 644

Time Complexity : The amount of time Go Back

Full Screen
(number of computation steps) as a Close

function of the input Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Space Complexity Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .

What is the space complexity of A large Example: . . .


Lecture-wise Index to . . .

• Newton’s method Home Page

Title Page

• Euclidean GCD JJ II

• Factorial J I

Page 276 of 644

• Fibonacci Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton & Euclid:
Absolute
Computing: The . . .
Newton’s Method Algorithms: Design & . . .

Computation Introducing Reals


Correctness, . . .
Compound Data

Newton’s method (wherever and Higher Order Functions . . .


Imperative . . .

whenever it works well) requires A large Example: . . .


Lecture-wise Index to . . .

space to compute Home Page

• the value of f at each point xi Title Page

JJ II

• the value of f 0 at each point xi J I

• the value of xi+1 from the above Page 277 of 644

Go Back

Their absolute space requirements Full Screen

could be different. But . . . Close

Euclidean GCD Quit


Computation
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton & Euclid: Computing: The . . .
Algorithms: Design & . . .

Relative Introducing Reals


Correctness, . . .
Compound Data

Newton’s Method Higher Order Functions . . .


Imperative . . .
Computation A large Example: . . .
Lecture-wise Index to . . .

GCD and Newton’s method (wherever Home Page

and whenever it works well) require Title Page


the same amount of space for each JJ II
recursive unfolding since each fresh
J I
unfolding can reuse the space used
by the previous one. Page 278 of 644

Go Back
Euclidean GCD
Computation Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Deriving space
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

requirements
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

We may use the algorithm itself to de- Lecture-wise Index to . . .

Home Page

rive the space required as follows: Title Page

Assume that memory proportional to JJ II

calculating and outputting the answer J I

is a unit. Then space as a function of Page 279 of 644

the input is given by Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
GCD: Space Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

1 if b = 0 Correctness, . . .
Compound Data
Sgcd(a,b) = Higher Order Functions . . .
Sgcd(b,a mod b) otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

This implies (from well-foundedness) Home Page

that the entire computation ends with Title Page

the space of a unit. JJ II

J I
Sgcd(a,b) ∝ 1 Page 280 of 644

Go Back

A similar analysis and result holds for Full Screen

newton Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Space
f actL
Computing: The . . .

 Algorithms: Design & . . .

1 if n = 0 Introducing Reals

Sf actL(n) = Correctness, . . .

Sf actL(n−1)+1 otherwise Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

The 1 is for output and the +1 is be- Lecture-wise Index to . . .

cause one needs to store space pro- Home Page

Title Page

portional to remembering “multiply by JJ II

n”. J I

Page 281 of 644

Sf actL(n) ∝ n. Go Back

Full Screen

A similar analysis and result holds for Close

f actR. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Fibonacci: Space Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Fibonacci
Home Page

1 if n ≤ 1 Title Page

Sf ib(n) =
Sf ib(n−1) + Sf ib(n−2) if n > 1 JJ II

J I

Page 282 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Space
Fibonacci

It is easy to see prove by induction Computing: The . . .


Algorithms: Design & . . .
Introducing Reals

that for n > 1, Correctness, . . .


Compound Data
Higher Order Functions . . .

Sf ib(n−1) < Sf ib(n) ≤ 2Sf ib(n−1) Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

That is, as the value of n increases Home Page

by 1 the space requirement approxi- Title Page

JJ II
mately doubles. Further, it is easy to
J I
show by induction that Page 283 of 644

2n−2 < Sf ib(n) ≤ 2n−1 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4.2. Efficiency Measures and Speed-ups
Computing: The . . .
1. Recapitulation
Algorithms: Design & . . .
2. Recapitulation Introducing Reals
Correctness, . . .
3. Time & Space Complexity
Compound Data
4. isqrt: Space Higher Order Functions . . .
Imperative . . .
5. Time Complexity
A large Example: . . .
6. isqrt: Time Complexity Lecture-wise Index to . . .

7. isqrt2: Time Home Page

8. shrink vs. shrink2: Times


Title Page
9. Factorial: Time Complexity
JJ II
10. Fibonacci: Time Complexity
11. Comparative Complexity J I

12. Comparisons
Page 284 of 644

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

16. Speeding Up: 1 Close

17. Speeding Up: 2


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Factoring out calculations
19. Tail Recursion: 1
20. Tail Recursion: 2
Computing: The . . .

21. Factorial: Tail Recursion Algorithms: Design & . . .


Introducing Reals
22. Factorial: Tail Recursion Correctness, . . .

23. A Computation Compound Data


Higher Order Functions . . .
24. Factorial: Issues Imperative . . .

25. Fibonacci: Tail Recursion A large Example: . . .


Lecture-wise Index to . . .
26. Fibonacci: Tail Recursion
Home Page
27. fibTR: SML
28. State in Tail Recursion Title Page

29. Invariance JJ II

J I

Page 285 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Recapitulation Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• Recursion & nontermination Lecture-wise Index to . . .

Home Page

• Termination & well-foundedness Title Page

• Well-foundedness proofs JJ II

J I
• Well-foundedness & Complexity Page 286 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation Computing: The . . .

• Recursion & nontermination Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

• Termination & well-foundedness Compound Data


Higher Order Functions . . .
Imperative . . .

• Well-foundedness proofs A large Example: . . .


Lecture-wise Index to . . .

– By induction Home Page

Title Page

– well-founded functions JJ II

– By well-founded functions J I

– induction as well-foundedness Page 287 of 644

Go Back

– Well-foundedness as induction Full Screen

• Well-foundedness & Complexity Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Time & Space
Complexity Computing: The . . .
Algorithms: Design & . . .
Questions Introducing Reals
Correctness, . . .
Compound Data

An order of magnitude estimate of Higher Order Functions . . .


Imperative . . .

the time or space (memory) required A large Example: . . .


Lecture-wise Index to . . .

(in terms of some large computation Home Page

steps). Title Page

JJ II
• Newton & Euclid’s GCD J I

• Deriving space requirements Page 288 of 644

Go Back

– Integer Sqrt Full Screen

– Factorial Close

– Fibonacci Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt : Space
Computing: The . . .
Integer Sqrt shrink Algorithms: Design & . . .
Introducing Reals

Sisqrt(n) = Sshrink(n,0,n) for large n. Correctness, . . .


Compound Data

Sshrink(n,l,u) = Higher Order Functions . . .


Imperative . . .
A large Example: . . .
 Lecture-wise Index to . . .

1
 if l = u Home Page

Sshrink(n,l+1,u) if l < u . . . Title Page

JJ II

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

J I

Assuming 1 unit of space for output. Page 289 of 644

Go Back

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

Sisqrt(n) = Sshrink(n,0,n) ∝ 1 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Time Complexity Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

As in the case of space we may use Higher Order Functions . . .


Imperative . . .

the algorithm itself to derive the time A large Example: . . .


Lecture-wise Index to . . .

complexity. Home Page

• Integer sqrt Title Page

JJ II

• Factorial J I

• Fibonacci Page 290 of 644

Go Back

forward
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt: Time Complexity
Integer Sqrt Computing: The . . .

shrink Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Assume condition-checking (along Compound Data


Higher Order Functions . . .

with +1 or −1) takes a unit of time. Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
Then Tshrink(n,l,u) = Home Page

 Title Page

0
 if l = u JJ II

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

shrink(n,l,u−1) if l < u . . .
1+T
 Page 291 of 644

Go Back

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

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt2: Time
shrink

Assume condition-checking (along Computing: The . . .

with (l + u) div 2) takes a unit of time. Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Then Tshrink2(n,l,u) = Compound Data
Higher Order Functions . . .

 Imperative . . .

if u ≤ l ≤ u
A large Example: . . .

0
 Lecture-wise Index to . . .

1 + Tshrink2(n,m,u) if m2 ≤ n . . . Home Page

1+T

if m 2>n Title Page

shrink2(n,l,u−1) JJ II

J I
If 2k−1 ≤ |[l, u]| − 1 < 2k then the al- Page 292 of 644

gorithm terminates in at most k steps. Go Back

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

Close

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

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
shrink vs. shrink2 : Times
shrink shrink2
Computing: The . . .
Algorithms: Design & . . .

1. The time units are different, Introducing Reals


Correctness, . . .
Compound Data

2. But they differ by a constant factor Higher Order Functions . . .


Imperative . . .

at most. A large Example: . . .


Lecture-wise Index to . . .

3. So clearly, for large n, shrink2 is Home Page

faster than shrink Title Page

JJ II

4. But for small n, it depends on the J I

constant factor. Page 293 of 644

Go Back

5. Implicitly assume that the actual Full Screen

unit of time includes the time re- Close

quired to unfold the recursion. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Time Computing: The . . .

Complexity Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
f actL Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Here we assume multiplication takes Lecture-wise Index to . . .

unit time. Home Page

 Title Page

0 if n = 0
Tf actL(n) = JJ II

Tf actL(n−1)+1 otherwise J I

Page 294 of 644

Then Go Back

Tf actL(n) ∝ n Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Time
Complexity Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Fibonacci Compound Data
Higher Order Functions . . .

Assuming addition and condition- Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

checking together take a unit of time, Home Page

we have Title Page



0 if n ≤ 1 JJ II

Tf ib(n) =
Tf ib(n−1) + Tf ib(n−2) if n > 1 J I

Page 295 of 644

It follows that Go Back

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


Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Comparative Introducing Reals


Correctness, . . .
Compound Data

Complexity Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Algorithm Space Time Home Page

isqrt(n) O(1) O(n) Title Page

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

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

O(2n) O(2n)
Page 296 of 644

f ib(n) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Comparisons Computing: The . . .
Algorithms: Design & . . .

For smaller values Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 297 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Comparisons Computing: The . . .
Algorithms: Design & . . .

For large values Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 298 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Efficiency Measures: Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Time Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

An algorithm for a problem is A large Example: . . .


Lecture-wise Index to . . .

asymptotically faster or asymptotically Home Page

more time-efficient than another for Title Page

the same problem if its time complex- JJ II

ity is bounded by a function that has J I

Page 299 of 644

a slower growth rate as a function of Go Back

the value of its arguments. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Efficiency Measures: Correctness, . . .


Compound Data
Higher Order Functions . . .

Space Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Similarly an algorithm is Home Page

asymptotically more space efficient Title Page

than another if its space complexity JJ II

is bounded by a function that has a J I

slower growth rate. Page 300 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Speeding Up: 1
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Q: Can fibonacci be speeded up or Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

made more space efficient? Home Page

A: Perhaps by studying the nature of Title Page

the function e.g. isqrt2 vs. isqrt JJ II

J I
and attempting more efficient algo- Page 301 of 644

rithmic variations. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Speeding Up: 2 Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Q: Are there general methods of Home Page

speeding up or saving space? Title Page

A: Take inspiration from gcd, newton, JJ II

J I
shrink Page 302 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factoring out Computing: The . . .

calculations Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

gcd(a0, b0) Higher Order Functions . . .


Imperative . . .
A large Example: . . .
compute a1, b1 Lecture-wise Index to . . .

gcd(a1, b1) Home Page

compute a2, b2 Title Page

JJ II
gcd(a2, b2) J I

··· Page 303 of 644

gcd(an, bn) Go Back

an Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Tail Recursion: 1
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

• Factor out calculations and remem- Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

ber only those values that are re- Home Page

quired for the next recursive call. Title Page

• Create a vector of state variables JJ II

J I
and include them as arguments of
Page 304 of 644

the function Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Tail Recursion: 2 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Try to reorder the computation us- Higher Order Functions . . .


Imperative . . .

ing the state variables so as to get A large Example: . . .


Lecture-wise Index to . . .

the next state completely defined. Home Page

Title Page

• Redefine the function entirely in JJ II

terms of the state variables so that J I

the recursive call is the outermost Page 305 of 644

operation. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Tail
Recursion
Computing: The . . .
f actL Waxing f actL Waning Algorithms: Design & . . .
Introducing Reals

• The recursive call precedes the Correctness, . . .


Compound Data
Higher Order Functions . . .
multiplication operation. Change it! Imperative . . .
A large Example: . . .

• Define a state variable p which con-


Lecture-wise Index to . . .

Home Page

tains the product of all the values Title Page

that one must remember JJ II

• Reorder the computation so that the J I

Page 306 of 644

computation of p is performed be- Go Back

fore the recursive call. Full Screen

• For that redefine the function in Close

terms of p. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Tail
Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Factorial Correctness, . . .
Compound Data

 Higher Order Functions . . .

⊥ if n < 0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
f actL2(n) = 1 if n = 0 Home Page

f actL tr(n, 1) otherwise



Title Page

where JJ II

J I
f actL tr(n, p) = Page 307 of 644

p if n = 0 Go Back

f actL tr(n − 1, np) otherwise Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

f actL2(4) Compound Data


Higher Order Functions . . .
Imperative . . .

f actL tr(4, 1) A large Example: . . .


Lecture-wise Index to . . .

f actL tr(3, 4) Home Page

f actL tr(2, 12) Title Page

f actL tr(1, 24) JJ II

f actL tr(0, 24) J I

Page 308 of 644


24
Go Back

Reminiscent of gcd and newton! Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Issues
• Correctness: Prove (by induction
Computing: The . . .

on n) that for all n ≥ 0, f actL2(n) = Algorithms: Design & . . .


Introducing Reals

n!. Correctness, . . .
Compound Data
Higher Order Functions . . .

• termination: Prove by induction on Imperative . . .


A large Example: . . .

n that every computation of f actL2 Lecture-wise Index to . . .

Home Page

terminates. Title Page

• Space complexity: Prove that JJ II

Sf actL2(n) = O(1) (as against J I

Page 309 of 644


Sf actL(n) ∝ n). Go Back

• Time complexity: Prove that Full Screen

Tf actL2(n) = O(n) Close

Quit

Complexity table
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Tail Computing: The . . .

Recursion Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Remove duplicate computations by Higher Order Functions . . .


Imperative . . .

defining appropriate state variables A large Example: . . .


Lecture-wise Index to . . .

• Let a and b be the consecutive fi- Home Page

Title Page
bonacci numbers f ib(m − 2) and JJ II
f ib(m − 1) required for the compu- J I

tation of f ib(m). Page 310 of 644

• The state consists of the variables Go Back

Full Screen

n, a, b, m. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Tail
Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

f ibT R(n) = Correctness, . . .


Compound Data
Higher Order Functions . . .

⊥ if n < 0
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

1 if 0 ≤ n ≤ 1 Home Page

f ib iter(n, 1, 1, 1) otherwise

Title Page

JJ II
where J I
f ib iter(n, a, b, m) = Page 311 of 644


b if m ≥ n Go Back

Full Screen

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

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
fibTR: SML
Computing: The . . .

local Algorithms: Design & . . .


Introducing Reals

fun fib_iter (n, a, b, m) = Correctness, . . .


Compound Data

(* fib (m) = b ,fib (m-1) = a *) Higher Order Functions . . .


Imperative . . .
A large Example: . . .

if m >= n then b Lecture-wise Index to . . .

else fib_iter (n, b, a+b, m+1); Home Page

Title Page
in
JJ II
fun fibTR (n) = J I

if n < 0 then raise negativeArgumen Page 312 of 644

else if (n <= 1) then 1 Go Back

else fib_iter (n, 1, 1, 1) Full Screen

end; Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

State in Tail Recursion


Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

• The variables that make up the Higher Order Functions . . .


Imperative . . .
A large Example: . . .
state bear a definite relation to each Lecture-wise Index to . . .

other. Home Page

Title Page

• INVARIANCE. That relationship be- JJ II

tween the state variables does not J I

change throughout the computation Page 313 of 644

of the function. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance Computing: The . . .
Algorithms: Design & . . .

• The invariant property of a tail- Introducing Reals


Correctness, . . .
Compound Data

recursive function must hold Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Initially when it is first invoked, and Lecture-wise Index to . . .

Continues to hold before every suc-


Home Page

Title Page

cessive invocation JJ II

• The invariant property character- J I

izes the entire computation and the Page 314 of 644

Go Back
algorithm and is crucial to the proof
Full Screen

of correctness Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4.3. Invariance & Correctness
Computing: The . . .
1. Recap
Algorithms: Design & . . .
2. Recursion Transformation Introducing Reals
Correctness, . . .
3. Tail Recursion: Examples
Compound Data
4. Comparisons Higher Order Functions . . .
Imperative . . .
5. Transformation Issues
A large Example: . . .
6. Correctness Issues 1 Lecture-wise Index to . . .

7. Correctness Issues 2 Home Page

8. Correctness Theorem
Title Page
9. Invariants & Correctness 1
JJ II
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr J I

12. Invariance: Example


Page 315 of 644

13. Invariance: Example


Go Back
14. Proof
15. Invariance Lemma: f ib iter Full Screen

16. Proof Close

17. Correctness: Fibonacci


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Variants & Invariants
19. Variants & Invariants
20. More Invariants
Computing: The . . .

21. Fast Powering 1 Algorithms: Design & . . .


Introducing Reals
22. Fast Powering 2 Correctness, . . .

23. Root Finding: Bisection Compound Data


Higher Order Functions . . .
24. Advantage Bisection Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 316 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Asymptotic Complexity: Compound Data


Higher Order Functions . . .
Imperative . . .

– Space A large Example: . . .


Lecture-wise Index to . . .

– Time Home Page

• Comparative Complexity
Title Page

JJ II

• Comparisons: J I

– Small inputs Page 317 of 644

Go Back

– Large inputs Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion Computing: The . . .

Transformation Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• To achieve constant space and lin- Higher Order Functions . . .


Imperative . . .

ear time, if possible A large Example: . . .


Lecture-wise Index to . . .

• Speeding up using tail recursion Home Page

Title Page

– Factor out calculations JJ II

– Reorder the computations with J I

state variables Page 318 of 644

Go Back

– Recursion as the outermost oper- Full Screen

ation Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Tail Recursion:
Correctness, . . .
Compound Data
Higher Order Functions . . .

Examples Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Factorial vs. Factorial: Home Page

f actL vs. f actL2 vs. Title Page

JJ II

• Fibonacci vs. Fibonacci: J I

f ib vs. f ibT R Page 319 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Comparisons
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Algorithm Space Time Higher Order Functions . . .


Imperative . . .
A large Example: . . .
isqrt(n) O(1) O(n) Lecture-wise Index to . . .

isqrt2(n) O(1) O(log2 n) Home Page

f actL(n) O(n) O(n) Title Page

JJ II
f actL2(n) O(1) O(n) J I

f ib(n) O(2n) O(2n) Page 320 of 644

f ibT R(n) O(1) O(n) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Transformation Issues Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

• Correctness: Prove that the new al- Compound Data


Higher Order Functions . . .
Imperative . . .

gorithm computes the same func- A large Example: . . .


Lecture-wise Index to . . .

tion as the original simple algorithm Home Page

• Termination: Prove by induction on Title Page

JJ II
n that every computation is finite. J I

• Space complexity: Compute it. Page 321 of 644

• Time complexity: Compute it.


Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Correctness Issues 1 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Absolute correctness: For any func- Higher Order Functions . . .


Imperative . . .

tion f , that an algorithm A that A large Example: . . .


Lecture-wise Index to . . .

claims to implement it, prove that Home Page

f (~x) = A(~x) Title Page

JJ II

for all argument values ~x for which J I

f is defined. Page 322 of 644

• Transformation correctness: Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Correctness Issues 2 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
• Absolute correctness: Correctness, . . .
Compound Data

• Transformation correctness: For


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

any algorithm A and a transformed Lecture-wise Index to . . .

algorithm B prove that Home Page

A(~x) = B(~x) Title Page

JJ II

for all argument values ~x for which J I

A is defined. Then B is absolutely Page 323 of 644

correct provided A is absolutely cor- Go Back

Full Screen

rect. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Correctness Theorem Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Invariant properties f actL2 Higher Order Functions . . .
Imperative . . .

Theorem 1 For all n ≥ 0, A large Example: . . .


Lecture-wise Index to . . .

f actL2(n) = n! Home Page

Proof: For n = 0, it is clear that Title Page

f actL2(0) = 1 = 0!. For n > 0, JJ II

f actL2(n) = f actL tr(n, 1). The proof J I

is done provided we can show that Page 324 of 644

f actL tr(n, 1) = n!.  Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariants &
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Correctness 1 Correctness, . . .
Compound Data
Higher Order Functions . . .

Invariant properties f actL2 Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• To prove the absolute or trans- Home Page

formation correctness of a tail- Title Page

recursion transformation usually re- JJ II

quires an invariant property to be J I

Page 325 of 644


proven about the tail-recursive func- Go Back

tion. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariants & Computing: The . . .

Correctness 2 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Invariant properties f actL2 Higher Order Functions . . .
Imperative . . .

• This allows the independent proof A large Example: . . .


Lecture-wise Index to . . .

of the properties of the tail- Home Page

recursive function without reference Title Page

JJ II
to the function that uses it. J I

• It reflects the design of the al- Page 326 of 644

gorithm and its division into sub- Go Back

problems. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Invariance Lemma:
Correctness, . . .
Compound Data
Higher Order Functions . . .

f actL tr Imperative . . .
A large Example: . . .

Invariant properties f actL2 Lecture-wise Index to . . .

Home Page

Lemma 2 For all n ≥ 0 and p Title Page

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

Proof: By induction on n.  J I

Page 327 of 644


Back to theorem
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance: Example Computing: The . . .
Algorithms: Design & . . .

f actL2 Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

f actL tr(4, 7) Imperative . . .


A large Example: . . .

f actL tr(3, 28) Lecture-wise Index to . . .

f actL tr(2, 84) Home Page

Title Page

f actL tr(1, 168) JJ II

f actL tr(0, 168) J I

168 Page 328 of 644

Go Back

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

tion Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance: Example Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
f actL2 Correctness, . . .
Compound Data

So what exactly is invariant? Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
f actL tr(4, 7) 168 = 4! × 7 Home Page

f actL tr(3, 28) 168 = 3! × 28 Title Page

f actL tr(2, 84) 168 = 2! × 84 JJ II

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

f actL tr(0, 168) 168 = 0! × 168 Page 329 of 644

Go Back
168
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof
Basis For n = 0, f actL tr(0, p) = p = Computing: The . . .
Algorithms: Design & . . .

(0!)p. Introducing Reals


Correctness, . . .
Compound Data

Induction hypothesis (IH) For all k, Higher Order Functions . . .


Imperative . . .

0 < k ≤ n, A large Example: . . .


Lecture-wise Index to . . .

f actL tr(k, p) = p = (k!)p Home Page

Induction Step Title Page

JJ II

f actL tr(n + 1, p) J I

= f actL tr(n, (n + 1)p) Page 330 of 644

= (n!)(n + 1)p (IH) Go Back

Full Screen

= (n + 1)!p Close

Back to lemma Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Invariance Lemma:
f ib iter
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
f ib iter F Imperative . . .
A large Example: . . .

Lemma 3 For all n > 1, a, b, m : 1 ≤ Lecture-wise Index to . . .

Home Page

m ≤ n, if a = F(m − 1) and b = F(m), Title Page

then JJ II
INV :f ib iter(n, a, b, m) = F(n) J I

. Page 331 of 644

Proof: By induction on k = n − m  Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof
Basis For k = 0, n = m, it follows that Computing: The . . .

f ib iter(n, a, b, m) = F(n) Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Induction hypothesis (IH) For all n > Compound Data


Higher Order Functions . . .

1 and 1 ≤ m ≤ n, with n − m ≤ k, Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

INV holds Home Page

Induction Step Let 1 ≤ m < n such Title Page

that n − m = k + 1, F(m) = b and JJ II

F(m − 1) = a. Then F(m + 1) = a + b J I

and Page 332 of 644

Go Back

f ib iter(n, a, b, m) Full Screen

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

= F(n) (IH) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Correctness: Fibonacci Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

f ibT R Fibonacci Compound Data


Higher Order Functions . . .
Imperative . . .

Theorem 4 For all n ≥ 0, f ibT R(n) = A large Example: . . .


Lecture-wise Index to . . .

F(n). Home Page

Proof: For 0 ≤ n ≤ 1, it holds Title Page

trivially. For n > 1, f ibT R(n) = JJ II

f ib iter(n, 1, 1, 1) = F(n), by the invari- J I

ance lemma, with m = 1, a = 1 = Page 333 of 644

F(m − 1) and b = 1 = F(m).  Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Variants & Invariants Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
f actL2 Higher Order Functions . . .
Imperative . . .

f actL3(n) = A large Example: . . .


Lecture-wise Index to . . .

 Home Page

⊥ if n < 0 Title Page

1 if n = 0 JJ II

f actL tr2(n, 1, 1) else



J I

Page 334 of 644

where Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Variants & Invariants Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
f actL2 Higher Order Functions . . .
Imperative . . .

f actL tr2(n, p, m) = A large Example: . . .


Lecture-wise Index to . . .

 Home Page

p if n = m Title Page

f actL tr2(n, (m + 1)p, m + 1) else JJ II

J I

f actL tr2(n, p, m) = (m!)p Page 335 of 644

for all 1 ≤ m ≤ n. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Invariants Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• shrink For all n > 0, l, u, if Compound Data


Higher Order Functions . . .

[l, u] ⊆ [0, n], Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

√ Home Page

l ≤ b nc ≤ u Title Page

• shrink2 JJ II

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

Page 336 of 644

√ Go Back

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

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Fast Powering 1 Introducing Reals


Correctness, . . .
Compound Data

power2 Higher Order Functions . . .


Imperative . . .
A large Example: . . .

power3(x, n) = Lecture-wise Index to . . .

Home Page

 1.0/power3(x, −n) if n < 0 Title Page

1.0 if n = 0 JJ II

powerT R(x, n, 1) else J I



Page 337 of 644

where Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fast Powering 2
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
power2 Compound Data
Higher Order Functions . . .
Imperative . . .

powerT R(x, n, p) = A large Example: . . .


Lecture-wise Index to . . .

p if n = 0 Home Page

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


Title Page

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

J I

where even(n) ⇐⇒ n mod 2 = 0. Page 338 of 644

powerT R(x, n, p) = xnp Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Bisection Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Newton’s Method Algorithm Correctness, . . .
Compound Data
Higher Order Functions . . .
Select a small enough ε > 0 and Imperative . . .
A large Example: . . .

x0. Then if sgn(f (a)) 6= sgn(f (b)), Lecture-wise Index to . . .

bisect(f, a, b, ε) = Home Page

Title Page

c if |f (c)| < ε JJ II

bisect(f, c, b, ε) if sgn(f (c)) 6= sgn(f (b)) J I

bisect(f, a, c, ε) otherwise Page 339 of 644



Go Back

where c = (a + b)/2 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Advantage Bisection
Computing: The . . .
More robust than Newton’s method Algorithms: Design & . . .
Introducing Reals

• Requires continuity and change of Correctness, . . .


Compound Data
Higher Order Functions . . .

sign Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Does not require differentiability Home Page

• Could change the condition suitably Title Page

to take care of very shallow curves JJ II

J I

• Oscillations could occur only if the Page 340 of 644

function is too steep. Go Back

• An intermediate point can never go


Full Screen

Close

outside the interval. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5. Compound Data
5.1. Tuples, Lists & the Generation of Primes
1. Recap: Tail Recursion Computing: The . . .
Algorithms: Design & . . .
2. Examples: Invariants
Introducing Reals

3. Tuples Correctness, . . .
Compound Data
4. Lists Higher Order Functions . . .

5. New Lists Imperative . . .


A large Example: . . .
6. List Operations Lecture-wise Index to . . .

7. List Operations: cons


Home Page
8. Generating Primes upto n
Title Page
9. More Properties
10. Composites JJ II

11. Odd Primes J I


12. primesU pto(n)
Page 341 of 644
13. generateF rom(P, m, n, k)
Go Back
14. generateF rom
15. primeW RT (m, P ) Full Screen

16. primeW RT (m, P )


Close
17. primeW RT
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Density of Primes
19. The Prime Number Theorem
20. The Prime Number Theorem
Computing: The . . .

21. Complexity Algorithms: Design & . . .


Introducing Reals
22. Diagnosis Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 342 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Tail Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Asymptotic Complexity: Compound Data


Higher Order Functions . . .
Imperative . . .

Time Linear A large Example: . . .


Lecture-wise Index to . . .

Space Constant Home Page

Title Page

• Correctness: Capture the algorithm JJ II

through J I

Invariant Invariance Lemma Page 343 of 644

Go Back

Bound function Proof by induction Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Examples: Invariants Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

f actL tr2 Correctness, . . .


Compound Data
Higher Order Functions . . .

shrink & shrink2 Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .


l ≤ b nc ≤ u
Home Page

√ Title Page

m = b(l + u)/2c and l ≤ b nc ≤ u JJ II

J I

Fast Powering Page 344 of 644

Go Back

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

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Tuples
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

divmod(a, b) = (a div b, a mod b) Home Page

Title Page

divmod: int * int -> int * JJ II


int J I

Page 345 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists Computing: The . . .
Algorithms: Design & . . .

An αlist represents a sequence of el- Introducing Reals


Correctness, . . .
Compound Data

ements of a given type α. Higher Order Functions . . .


Imperative . . .

Given a (nonempty) list A large Example: . . .


Lecture-wise Index to . . .

• A list is ordered Home Page

Title Page

• There may be more than one occur- JJ II

rence of an element in the list J I

• only the first element (called the Page 346 of 644

Go Back

head) of the list is immediately ac- Full Screen

cessible. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
New Lists
Given a (nonempty) list L, Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• A new list M may be created from Correctness, . . .


Compound Data

an existing list L by the tl operation. Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• New elements can be added (by the Lecture-wise Index to . . .

operation cons) to an existing list, Home Page

Title Page

one at a time to create new lists. JJ II

• the last element that was added be- J I

comes the head of the new list. Page 347 of 644

Go Back

• Two lists are equal only if they have Full Screen

the same elements in the same or- Close

der Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Operations Computing: The . . .

• The empty list: nil or [] Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

• Nonempty lists: Given a nonempty Compound Data


Higher Order Functions . . .
Imperative . . .
list L A large Example: . . .
Lecture-wise Index to . . .

L = [1, 2, 3, 4] Home Page

head : hd : αList → α Title Page

JJ II

hd(L) = 1 J I

Page 348 of 644

tail : tl : αList → αList Go Back

Full Screen

tl(L) = [2, 3, 4] Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

List Operations: cons


Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

• L = [1, 2, 3, 4] Higher Order Functions . . .


Imperative . . .
A large Example: . . .

cons : cons : α × αList → αList Lecture-wise Index to . . .

Home Page

cons(0, nil) = [0] Title Page

JJ II

cons(0, L) = 0 :: L = [0, 1, 2, 3, 4] J I

1 :: (0 :: L) = [1, 0, 1, 2, 3, 4] Page 349 of 644

Go Back
back to lists Recap
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generating Primes
upto n Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Definition 5 A positive integer n > 1 Correctness, . . .


Compound Data

is composite iff it has a proper divisor


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

d|n with 1 < d < n. Otherwise it is Lecture-wise Index to . . .

prime. Home Page

Title Page

• 2 is the smallest (first) prime. JJ II

• 2 is the only even prime. J I

Page 350 of 644

• No other even number can be a Go Back

prime. Full Screen

Close

• All other primes are odd Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Properties Computing: The . . .
Algorithms: Design & . . .

• An odd number cannot have any Introducing Reals


Correctness, . . .

even divisors. Compound Data


Higher Order Functions . . .
Imperative . . .

• Every number may be expressed A large Example: . . .


Lecture-wise Index to . . .

uniquely (upto order) as a product Home Page

of prime factors. Title Page

JJ II

• No divisor of a positive integer can J I

be greater than itself. Page 351 of 644

• For √ d|n such that d ≤


√ each divisor
Go Back

Full Screen

b nc, n/d ≥ b nc is also a divisor. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Composites Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• If a number n is composite, then it Higher Order Functions . . .


Imperative . . .

√ a proper divisor d, 2 ≤ d ≤
has A large Example: . . .
Lecture-wise Index to . . .

b nc. Home Page

• If a number n is composite, then√ it


Title Page

JJ II
has a prime divisor p, 2 ≤ p ≤ b nc. J I

• An odd composite number n has √ an


Page 352 of 644

odd prime divisor p, 3 ≤ p ≤ b nc. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Odd Primes Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
• An odd number > 1 is a prime iff it Compound Data
Higher Order Functions . . .

has no proper odd divisors Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• An odd number > 1 is a prime iff Home Page

it is not divisible by any odd prime Title Page

smaller than itself. JJ II

J I
• An odd number n > 1 is a prime iff Page 353 of 644

it is√not divisible by any odd prime Go Back

≤ b nc. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

primesU pto(n) Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

primesU pto(n) = Compound Data


Higher Order Functions . . .
Imperative . . .


 [] if n < 2 A large Example: . . .
Lecture-wise Index to . . .

 [(1, 2)] if n = 2

 Home Page

primesU pto(n − 1) elseif even(n) Title Page

JJ II
generateF rom otherwise



 J I
([(1, 2)], 3, n, 2)

Page 354 of 644

where Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

generateF rom(P, m, n, k) Algorithms: Design & . . .


Introducing Reals

bound function n − m Correctness, . . .


Compound Data
Higher Order Functions . . .

Invariant Imperative . . .
A large Example: . . .

2 < m ≤ n ∧ odd(m) Lecture-wise Index to . . .

Home Page

implies Title Page

P = [(k − 1, pk−1), · · · , (1, p1)] JJ II

J I

and Page 355 of 644

∀q : pk−1 < q < m : composite(q) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
generateF rom
Computing: The . . .
Algorithms: Design & . . .

generateF rom(P, m, n, k) = Introducing Reals


Correctness, . . .
 Compound Data


 P if m > n Higher Order Functions . . .
Imperative . . .

 A large Example: . . .
 Lecture-wise Index to . . .

 generateF rom elseif

 Home Page

(((k, m) :: P ), m + 2, n, k + 1) pwrt Title Page


 JJ II

 J I
generateF rom else



 Page 356 of 644

(P, m + 2, n, k)

Go Back

where pwrt = primeW RT (m, P ) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT (m, P )
Computing: The . . .

Definition 6 A number m is prime with Algorithms: Design & . . .


Introducing Reals

respect to a list L of numbers iff it is Correctness, . . .


Compound Data

not divisible by any of them. Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• A number is prime iff it is prime Lecture-wise Index to . . .

Home Page

with respect to the list of all primes Title Page

smaller than itself. JJ II

• From properties of odd primes it fol- J I

Page 357 of 644

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

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT (m, P ) Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

bound function length(P ) Correctness, . . .


Compound Data
Higher Order Functions . . .

Invariant If P = [(i − 1, pi−1), . . . (1, p1)], Imperative . . .


A large Example: . . .

for some i ≥ 1 then Lecture-wise Index to . . .

Home Page

• pk ≥ m > pk−1, and Title Page

• m is prime with respect to JJ II

[(k − 1, pk−1), . . . , (i, pi)] J I

Page 358 of 644

• m is a prime iff it is a prime with Go Back

respect to P Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

primeW RT (m, P ) = Correctness, . . .


Compound Data
 Higher Order Functions . . .


 true if P = nil Imperative . . .
A large Example: . . .

f alse elseif h|m Lecture-wise Index to . . .



Home Page


 primeW RT else Title Page

(m, tl(P ))

JJ II

J I
where Page 359 of 644

(i, h) = hd(P ) Go Back

for some i > 0 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Density of Primes Computing: The . . .
Algorithms: Design & . . .
Let π(n) denote the number of primes Introducing Reals
Correctness, . . .

upto n. Then Compound Data


Higher Order Functions . . .
Imperative . . .
n π(n) % A large Example: . . .
Lecture-wise Index to . . .

100 25 25.00% Home Page

1000 168 16.80% Title Page

10000 1229 12.29% JJ II

100000 9592 9.59% J I

1000000 78, 498 7.85% Page 360 of 644

Go Back

10000000 664579 6.65% Full Screen

100000000 5761455 5.76% Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Prime Number
Theorem Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
π(n) Imperative . . .
limn→∞ n/ ln n = 1 A large Example: . . .
Lecture-wise Index to . . .

Proved by Gauss. Home Page

Title Page

• Shows that the primes get sparser JJ II

at higher n J I

• A larger percentage of numbers as Page 361 of 644

Go Back

we go higher are composite. Full Screen

from David Burton: Elementary Number Close

Theory. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Prime Number
Theorem
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
π(n) Correctness, . . .

n π(n) % lim Compound Data

n→∞ n/ ln n Higher Order Functions . . .


Imperative . . .

100 25 25.00% A large Example: . . .


Lecture-wise Index to . . .

1000 168 16.80% 1.159 Home Page

10000 1229 12.29% 1.132 Title Page

JJ II
100000 9592 9.59% 1.104
J I
1000000 78, 498 7.85% 1.084 Page 362 of 644

10000000 664579 6.65% 1.071 Go Back

100000000 5761455 5.76% 1.061 Full Screen

Close
from David Burton: Elementary Number
Theory. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Complexity Higher Order Functions . . .


Imperative . . .
A large Example: . . .

function calls
Lecture-wise Index to . . .

Home Page

primesU pto 1 Title Page

generateF rom n/2 JJ II


P n
primeW RT m=3,odd(m) π(m)
J I

Page 363 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Diagnosis
For each m ≤ n,
Computing: The . . .

• P is in descending order of the Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

primes Compound Data


Higher Order Functions . . .
Imperative . . .

• m is checked for divisibility π(m) A large Example: . . .


Lecture-wise Index to . . .

times Home Page

• From properties of odd primes it Title Page

JJ II
should not be necessary
√ to check J I
each m more than π(b mc) times Page 364 of 644

for divisibility. Go Back

• Organize P in ascending order in- Full Screen

stead of descending. Close

Quit

ascending-order
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5.2. Compound Data & Lists
Computing: The . . .
1. Compound Data
Algorithms: Design & . . .
2. Recap: Tuples Introducing Reals
Correctness, . . .
3. Tuple: Formation
Compound Data
4. Tuples: Selection Higher Order Functions . . .
Imperative . . .
5. Tuples: Equality
A large Example: . . .
6. Tuples: Equality errors Lecture-wise Index to . . .

7. Lists: Recap Home Page

8. Lists: Append
Title Page
9. cons vs. @
JJ II
10. Lists of Functions
11. Lists of Functions J I

12. Arithmetic Sequences


Page 365 of 644

13. Tail Recursion


Go Back
14. Tail Recursion Invariant
15. Tail Recursion Full Screen

16. Another Tail Recursion: AS3 Close

17. Another Tail Recursion: AS3 iter


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. AS3: Complexity
19. Generating Primes: 2
20. primes2U pto(n)
Computing: The . . .

21. generate2F rom(P, m, n, k) Algorithms: Design & . . .


Introducing Reals
22. generate2F rom Correctness, . . .

23. prime2W RT (m, P ) Compound Data


Higher Order Functions . . .
24. prime2W RT Imperative . . .

25. primes2: Complexity A large Example: . . .


Lecture-wise Index to . . .
26. primes2: Diagnosis
Home Page

Title Page

JJ II

J I

Page 366 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Compound Data Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Forming (compound) data struc-


Lecture-wise Index to . . .

Home Page

tures from simpler ones Title Page

• Breaking up compound data into its JJ II

components. J I

Page 367 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Recap: Tuples Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

formation : Cartesian products of Higher Order Functions . . .


Imperative . . .

types A large Example: . . .


Lecture-wise Index to . . .

selection : Selection of individual com- Home Page

ponents Title Page

JJ II

equality : Equality checking J I

equality errors : Equality errors Page 368 of 644

Go Back

forward to Lists
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuple: Formation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Standard ML of New Jersey, Compound Data


Higher Order Functions . . .

- val a = ("arun", 1<2, 2); Imperative . . .


A large Example: . . .

val a = ("arun",true,2) Lecture-wise Index to . . .

Home Page

: string * bool * int Title Page

- val b = ("arun", true, 2); JJ II

val b = ("arun",true,2) J I

: string * bool * int Page 369 of 644

Go Back

back Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples: Selection
- #2 a; Computing: The . . .

val it = true : bool Algorithms: Design & . . .


Introducing Reals

- #1 a; Correctness, . . .
Compound Data
Higher Order Functions . . .
val it = "arun" : string Imperative . . .
A large Example: . . .

- #3 a; Lecture-wise Index to . . .

val it = 2 : int Home Page

Title Page

- #4 a; JJ II
stdIn:1.1-1.5 Error: operator and ope J I

operator domain: {4:’Y; ’Z} Page 370 of 644

operand: string * bool * in Go Back

in expression: Full Screen

Close
(fn {4=4,...} => 4) a
Quit

•First •Prev •Next •Lastback


•Go Back •Full Screen •Close •Quit
Tuples: Equality Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

- a = b; Compound Data
Higher Order Functions . . .

val it = true : bool Imperative . . .


A large Example: . . .

- (1<2, true) = (1.0 < 2.0, true); Lecture-wise Index to . . .

Home Page

val it = true : bool Title Page

- (true, 1.0 < 2.4) JJ II

= (1.0 < 2.4, true); J I

val it = true : bool Page 371 of 644

Go Back

back Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples: Equality errors
- ("arun", (1, true)) Computing: The . . .
Algorithms: Design & . . .

= ("arun", 1, true); Introducing Reals


Correctness, . . .
Compound Data
stdIn:1.1-29.39 Error: operator and Higher Order Functions . . . o
Imperative . . .

operator domain: (string * (int * A large Example: . . .


Lecture-wise Index to . . .
b
operand: (string * (int * Home Page b
in expression: Title Page

("arun",(1,true)) JJ II

J I
= ("arun",1,true)
Page 372 of 644

- ("arun", (1, true)) Go Back

= (("arun", 1), true); Full Screen

stdIn:1.1-29.39 Error: operator and Close o


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Lists: Recap
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

formation : Sequence α List Lecture-wise Index to . . .

Home Page

selection : Selection of individual com- Title Page

ponents JJ II

J I
new lists : Making new lists from old Page 373 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Append
- op @; Computing: The . . .
Algorithms: Design & . . .

val it = fn : ’a list * ’a list Introducing Reals


Correctness, . . .

-> ’a list Compound Data


Higher Order Functions . . .
Imperative . . .
- [1,2,3] @ [˜1, ˜3]; A large Example: . . .
Lecture-wise Index to . . .

val it = [1,2,3,˜1,˜3] Home Page

: int list Title Page

- [[1,2,3], [˜1, ˜2]] JJ II

@ [[1,2,3], [˜1, ˜2]]; J I

val it = Page 374 of 644

Go Back

[[1,2,3], [˜1,˜2], Full Screen

[1,2,3], [˜1,˜2]] Close

: int list list Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
cons vs. @
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

cons is a constant time = O(1) oper- Compound Data


Higher Order Functions . . .

ation. But @ is linear = O(n) in the Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

length n of the first list. @ is defined Home Page

as Title Page

L@M = JJ II
 J I
M if L = nil
Page 375 of 644

h :: (T @M ) if L = h :: T Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Lists of Functions Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
- fun add1 x = x+1; Imperative . . .
A large Example: . . .

val add1 = fn : int -> int Lecture-wise Index to . . .

- fun add2 x = x + 2; Home Page

Title Page
val add2 = fn : int -> int JJ II
- fun add3 x = x + 3; J I

val add3 = fn : int -> int Page 376 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists of Functions Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

- val addthree Correctness, . . .


Compound Data
Higher Order Functions . . .
= [add1, add2, add3]; Imperative . . .
A large Example: . . .

val addthree Lecture-wise Index to . . .

= [fn,fn,fn] : (int -> int) list Home Page

Title Page
- fun addall x = [(add1 x), (add2 x), JJ II
val addall = fn : int -> int list J I

- addall 3; Page 377 of 644

val it = [4,5,6] : int list Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Sequences Computing: The . . .

AS1(a, d, n) = Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
 Compound Data


 [] if n ≤ 0 Higher Order Functions . . .
Imperative . . .
 A large Example: . . .
Lecture-wise Index to . . .


 AS1(a, d, n − 1) else Home Page

@[a + (n − 1) ∗ d]
 Title Page

JJ II

function calls Order J I

Page 378 of 644


AS1 n O(n) Go Back

@ n O(n) Full Screen


Pn 2)
:: i=0 i O(n Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tail Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

AS2(a, d, n) = Correctness, . . .
Compound Data
Higher Order Functions . . .
 Imperative . . .

 [] if n ≤ 0 A large Example: . . .
Lecture-wise Index to . . .

Home Page

AS2 iter(a, d, n − 1, 0, []) else



Title Page

JJ II
where J I

for any initial L0 and n ≥ k ≥ 0 Page 379 of 644

Go Back

IN V 2 : L = L0@[a]@ . . . @[a + (k − 1) ∗ d] Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tail Recursion:
Invariant Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

IN V 2 : L = L0@[a]@ . . . @[a + (k − 1) ∗ d] Compound Data


Higher Order Functions . . .
Imperative . . .

and bound function A large Example: . . .


Lecture-wise Index to . . .

n−k Home Page

AS2 iter(a, d, n, k, L) = Title Page

JJ II


 L if k ≥ n J I
 Page 380 of 644


 AS2 iter(a, d, n, k + 1 else Go Back

Full Screen

L@[a + k ∗ d])

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Tail Recursion: Algorithms: Design & . . .


Introducing Reals

Complexity
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

function calls Order A large Example: . . .


Lecture-wise Index to . . .

AS2 1 Home Page

AS2 iter n O(n) Title Page

JJ II
@ n O(n) J I
Pn 2)
:: i=0 i O(n Page 381 of 644

So tail recursion simply doesn’t help! Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Another Tail Recursion
: AS3 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

AS3(a, d, n) = Correctness, . . .
Compound Data
 Higher Order Functions . . .

 [] if n ≤ 0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

AS3 iter(a, d, n − 1, []) else



Title Page

where JJ II

for any initial L0, n0 ≥ n > 0, and J I

Page 382 of 644

Go Back

IN V 3 : L = (a + (n − 1) ∗ d) :: · · · :: Full Screen

· · · :: (a + (n0 − 1) ∗ d) :: L0 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Another Tail Recursion:
AS3 iter
Computing: The . . .
Algorithms: Design & . . .

IN V 3 : L = (a + (n − 1) ∗ d) :: · · ·
Introducing Reals
Correctness, . . .
Compound Data

· · · :: (a + (n0 − 1) ∗ d) :: L0 Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
and bound function n, Home Page

AS3 iter(a, d, n, L) = Title Page


 L if n ≤ 0 JJ II

J I


Page 383 of 644


 AS3 iter(a, d, n − 1, else Go Back

(a + (n − 1) ∗ d)::L)

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

AS3: Complexity Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
function calls Order Lecture-wise Index to . . .

AS3 1 Home Page

AS3 iter n O(n) Title Page

JJ II
@ 0 J I
P n
:: i=0 1 O(n) Page 384 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Generating Primes: 2
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
composites Lecture-wise Index to . . .

Home Page

• primesU pto Title Page

• invariant JJ II

J I
• generateF rom Page 385 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

primes2U pto(n) Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

primes2U pto(n) = Compound Data


Higher Order Functions . . .
Imperative . . .


 [] if n < 2 A large Example: . . .
Lecture-wise Index to . . .

 [(1, 2)] if n = 2

 Home Page

primes2U pto(n − 1) elseif even(n) Title Page

JJ II
generate2F rom otherwise



 J I
([(1, 2)], 3, n, 2)

Page 386 of 644

where Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

generate2F rom(P, m, n, k) Algorithms: Design & . . .


Introducing Reals

bound function n − m Correctness, . . .


Compound Data
Higher Order Functions . . .

Invariant Imperative . . .
A large Example: . . .

2 < m ≤ n ∧ odd(m) Lecture-wise Index to . . .

Home Page

implies Title Page

P = [(1, p1), · · · , (k − 1, pk−1)] JJ II

J I

and Page 387 of 644

∀q : pk−1 < q < m : composite(q) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
generate2F rom
Computing: The . . .
Algorithms: Design & . . .

generate2F rom(P, m, n, k) = Introducing Reals


Correctness, . . .
 Compound Data


 P if m > n Higher Order Functions . . .
Imperative . . .

 A large Example: . . .
 Lecture-wise Index to . . .

 generate2F rom elseif

 Home Page

(P @[(k, m)], m + 2, n, k + 1) pwrt Title Page


 JJ II

 J I
generate2F rom else



 Page 388 of 644

(P, m + 2, n, k)

Go Back

where pwrt = prime2W RT (m, P ) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
prime2W RT (m, P )
Computing: The . . .
Algorithms: Design & . . .
bound function length(P ) Introducing Reals
Correctness, . . .

Invariant If P = [(i, pi), . . . (k − 1, pk−1)],


Compound Data
Higher Order Functions . . .
Imperative . . .

for some i ≥ 1 then A large Example: . . .


Lecture-wise Index to . . .

• pk ≥ m > pk−1, and Home Page

• m is prime with respect to


Title Page

JJ II
[(1, p1), . . . , (i − 1, pi−1)] J I

• m is a prime iff it is a prime Page 389 of 644

with respect √ to [(1, p1), . . . , (j, pj )], Go Back

where pj ≤ b mc < pj+1 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
prime2W RT Computing: The . . .
Algorithms: Design & . . .

prime2W RT (m, P ) = Introducing Reals


Correctness, . . .
 Compound Data


 true if P = nil Higher Order Functions . . .
Imperative . . .

 true if h > m div h A large Example: . . .



 Lecture-wise Index to . . .

f alse elseif h|m Home Page

primeW RT else

 Title Page

 JJ II
(m, tl(P ))

J I

where Page 390 of 644

(i, h) = hd(P ) Go Back

Full Screen

for some i > 0 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

primes2 : Complexity Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

function calls Home Page

primes2U pto 1 Title Page

generate2F rom n/2 √


JJ II

P n J I
prime2W RT m=3,odd(m) π(b mc) Page 391 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primes2 : Diagnosis
Computing: The . . .
generate2F rom Algorithms: Design & . . .
Introducing Reals

• Uses @ to create an ascending se- Correctness, . . .


Compound Data
Higher Order Functions . . .

quence of primes Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
• For each new prime pk this opera- Home Page

tion takes time O(k). Title Page

• Can tail recursion be used to re- JJ II

J I
duce the complexity due to @?
Page 392 of 644

• Can a more efficient algorithm us- Go Back

ing :: instead of @ be devised (as in Full Screen

the case of AS3)? Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5.3. Compound Data & List Algorithms
Computing: The . . .
1. Compound Data: Summary
Algorithms: Design & . . .
2. Records: Constructors Introducing Reals
Correctness, . . .
3. Records: Example 1
Compound Data
4. Records: Example 2 Higher Order Functions . . .
Imperative . . .
5. Records: Destructors
A large Example: . . .
6. Records: Equality Lecture-wise Index to . . .

7. Tuples & Records Home Page

8. Back to Lists
Title Page
9. Lists: Correctness
JJ II
10. Lists: Case Analysis
11. Lists: Correctness by Cases J I

12. List-functions: length


Page 393 of 644

13. List Functions: search


Go Back
14. List Functions: search2
15. List Functions: ordered Full Screen

16. List Functions:insert Close

17. List Functions: reverse


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. List Functions: reverse2
19. List Functions:merge
20. List Functions:merge
Computing: The . . .

21. List Functions:merge contd. Algorithms: Design & . . .


Introducing Reals
22. ML: merge Correctness, . . .

23. Sorting by Insertion Compound Data


Higher Order Functions . . .
24. Sorting by Merging Imperative . . .

25. Sorting by Merging A large Example: . . .


Lecture-wise Index to . . .
26. Functions as Data
Home Page
27. Higher Order Functions
Title Page

JJ II

J I

Page 394 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compound Data:
Summary Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Compound Data: Compound Data


Higher Order Functions . . .
Imperative . . .

Tuples: Cartesian products of differ- A large Example: . . .


Lecture-wise Index to . . .

ent types (ordered) Home Page

Lists: Sequences of the same type Title Page

of element JJ II

J I
Records: Unordered named aggre- Page 395 of 644

gations of elements of different Go Back

types. Full Screen

Close
• Constructors & Destructors Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Constructors
Computing: The . . .

• A record is a set of values drawn Algorithms: Design & . . .


Introducing Reals

from various types such that each Correctness, . . .


Compound Data
Higher Order Functions . . .

component (called a field) has a Imperative . . .


A large Example: . . .

unique name. Lecture-wise Index to . . .

Home Page

• Each record has a type defined by Title Page

field names JJ II

J I
types of fieldnames Page 396 of 644

The order of presentation of the Go Back

record fields does not affect its Full Screen

type in any way. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Example 1
Standard ML of New Jersey, Computing: The . . .
Algorithms: Design & . . .

- val pinky = Introducing Reals


Correctness, . . .
Compound Data
{ name = "Pinky", age = 3, Higher Order Functions . . .
Imperative . . .

fav_colour = "pink"}; A large Example: . . .


Lecture-wise Index to . . .

- val pinky = {age=3, Home Page

fav_colour="pink", Title Page

name="Pinky"} JJ II

J I
: {age:int,
Page 397 of 644

fav_colour:string, Go Back

name:string Full Screen

} Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Example 2
Computing: The . . .

- val billu = Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
{ age = 1, Compound Data
Higher Order Functions . . .

name = "Billu", Imperative . . .


A large Example: . . .

fav_colour = "blue" Lecture-wise Index to . . .

Home Page
}; Title Page

- val billu = JJ II

{age=1,fav_colour="blue",name="Billu" J I

{age:int, fav_colour:string, name:str Page 398 of 644

- pinky = billu; Go Back

val it = false : bool Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Records: Destructors Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
#age billu; Imperative . . .
A large Example: . . .

val it = 1 : int Lecture-wise Index to . . .

- #fav_colour billu; Home Page

Title Page
val it = "blue" : string JJ II
- #name billu; J I

val it = "Billu" : string Page 399 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Equality
Computing: The . . .

- val pinky2 = Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
{ name = "Pinky", Compound Data
Higher Order Functions . . .

fav_colour = "pink", Imperative . . .


A large Example: . . .

age = 3 Lecture-wise Index to . . .

Home Page
}; Title Page

- val pinky2 = JJ II

{age=3,fav_colour="pink",name="Pinky" J I

{age:int, fav_colour:string, name:str Page 400 of 644

- pinky = pinky2; Go Back

val it = true : bool Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples & Records Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• A k-tuple may be thought of as a Compound Data


Higher Order Functions . . .

record whose fields are numbered Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

#1 to #k instead of having names. Home Page

• A record may be thought of as Title Page

a generalization of tuples whose JJ II

J I
components are named rather than Page 401 of 644

being numbered. Go Back

back Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Back to Lists Computing: The . . .
Algorithms: Design & . . .

• Every L : α List satisfies Introducing Reals


Correctness, . . .

L = [] Compound Data
Higher Order Functions . . .
Imperative . . .

XOR A large Example: . . .


Lecture-wise Index to . . .

L = hd(L) :: tl(L) Home Page

Title Page

• Many functions on lists (L) are de- JJ II

fined by induction on its length (|L|). J I

Page 402 of 644



c if L = [] Go Back

f (L) =
g(h, T ) if L = h :: T Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Lists: Correctness Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

Hence their properties (P ) are proved Higher Order Functions . . .


Imperative . . .

by induction on the length of the list. A large Example: . . .


Lecture-wise Index to . . .

Basis |L| = 0. Prove P ([]) Home Page

Title Page

Induction hypothesis (IH) Assume for JJ II

some |T | = n > 0, P (T ) holds. J I

Induction Step Prove P (h :: T ) for L = Page 403 of 644

h :: T with |L| = n + 1 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Case Analysis
Computing: The . . .
inductive defns on lists Algorithms: Design & . . .
Introducing Reals

• Every list has exactly one of the fol- Correctness, . . .


Compound Data
Higher Order Functions . . .
lowing forms (patterns) Imperative . . .
A large Example: . . .

– [] Lecture-wise Index to . . .

Home Page

– h::T Title Page

• ML provides convenient JJ II

J I
case analysis based on patterns. Page 404 of 644

fun f [] = c Go Back

| f (h::T) = g (h, T) Full Screen

Close
;
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Correctness by
Cases
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Lists-correctness Compound Data
Higher Order Functions . . .

P is proved by case analysis.


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Basis Prove Home Page

P ([]) Title Page

JJ II

Induction hypothesis (IH) Assume J I

P (T ) Page 405 of 644

Go Back

Induction Step Prove Full Screen

P (h :: T ) Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

List-functions: length
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

 Home Page

length [] = 0 Title Page

length (h :: T ) = 1 + (length T ) JJ II

J I

Page 406 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

List Functions: search


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

To determine whether x occurs in a list A large Example: . . .


Lecture-wise Index to . . .

L Home Page

 search (x, []) = f alse Title Page

search (x, h :: T ) = true if x = h JJ II

J I
search (x, h :: T ) = search(x, T ) else

Page 407 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

List Functions: search2


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Or even more conveniently A large Example: . . .


Lecture-wise Index to . . .

 search2 (x, []) = f alse Home Page

search2 (x, h :: T ) = (x = h) or Title Page

JJ II
search2 (x, T )

J I

Time Complexity?? Page 408 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions: ordered Computing: The . . .
Algorithms: Design & . . .

Definition 7 A list L = [a0, . . . , an−1] Introducing Reals


Correctness, . . .

is ordered by a relation ≤ if consec- Compound Data


Higher Order Functions . . .

utive elements are related by ≤, i.e Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

ai ≤ ai+1, for 0 ≤ i < n − 1. Home Page

 Title Page


 ordered [] JJ II

ordered [h]

J I


 ordered (h0 :: h1 :: T ) if h0 ≤ h1 and Page 409 of 644

ordered(h1 :: T )
 Go Back

Full Screen

Time Complexity?? Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:insert
Given an ordered list L : α List, insert Computing: The . . .
Algorithms: Design & . . .

an element x : α at an appropriate po- Introducing Reals


Correctness, . . .
Compound Data

sition Higher Order Functions . . .


Imperative . . .


 insert (x, []) = [x] A large Example: . . .
Lecture-wise Index to . . .


 Home Page


 insert (x, h :: T ) = x :: (h :: T ) Title Page


JJ II
if x ≤ h J I



 Page 410 of 644

insert (x, h :: T ) = h :: (insert (x, T ))




 Go Back

else
 Full Screen

Close

Time Complexity?? Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

List Functions:
Algorithms: Design & . . .

reverse Introducing Reals


Correctness, . . .
Compound Data

Reverse the elements of a list Higher Order Functions . . .


Imperative . . .
A large Example: . . .

L = [a0, . . . , an−1] to obtain M = Lecture-wise Index to . . .

[an−1, . . . , a0]. Home Page

Title Page

reverse [] = [] JJ II

reverse (h :: T ) = (reverse T )@[h] J I

Page 411 of 644

Time Complexity?? O(n2) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:
Computing: The . . .

reverse2 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
 Compound Data

reverse [] = [] Higher Order Functions . . .


Imperative . . .

reverse (h :: T ) = rev ((h :: T ), []) A large Example: . . .


Lecture-wise Index to . . .

Home Page
where
Title Page

rev ([], N ) = N JJ II

rev (h :: T, N ) = rev (T, h :: N ) J I

Page 412 of 644

Correctness ?? Go Back
Time Complexity?? O(n) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:merge
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Merge two ordered lists |L| = l, |M | = Compound Data


Higher Order Functions . . .

m to produce an ordered list |N | = l + Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

m containing exactly the elements of Home Page

L and M . That is if Title Page

L = [1, 3, 5, 9, 11] and JJ II

M = [0, 3, 4, 4, 10], then J I

merge(L, M ) = N , where Page 413 of 644

Go Back

N = [0, 1, 3, 3, 4, 4, 5, 9, 10, 11] Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:merge Computing: The . . .
Algorithms: Design & . . .
 Introducing Reals


 merge([], M ) = M Correctness, . . .
Compound Data

 Higher Order Functions . . .

 Imperative . . .

merge(L, []) = L A large Example: . . .




 Lecture-wise Index to . . .


 Home Page

merge(L, M ) = Title Page

JJ II
a :: (merge(S, M )) if a ≤ b



 J I



 Page 414 of 644

merge(L, M ) =


 Go Back

b :: (merge(L, T )) else

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

List Functions:merge Higher Order Functions . . .


Imperative . . .

contd. A large Example: . . .


Lecture-wise Index to . . .

Home Page

where  Title Page

L = a :: S JJ II

M = b :: T J I

Page 415 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: merge
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
fun merge ([], M) = M Higher Order Functions . . .
Imperative . . .

| merge (L, []) = L A large Example: . . .


Lecture-wise Index to . . .

| merge (L as a::S, Home Page

M as b::T) = Title Page

if a <= b JJ II

J I
then a::merge(S, M)
Page 416 of 644

else b::merge(L, T) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Insertion
Computing: The . . .

Given a list of elements to reorder Algorithms: Design & . . .


Introducing Reals

them (i.e. with the same number of Correctness, . . .


Compound Data
Higher Order Functions . . .
occurrences of each element as in the Imperative . . .
A large Example: . . .

original list) to produce a new ordered Lecture-wise Index to . . .

list. Home Page

Title Page

Hence sort[10, 8, 3, 6, 9, 7, 4, 8, 1] = JJ II

[1, 3, 4, 6, 7, 8, 8, 9, 10] J I

isort[] = [] Page 417 of 644

Go Back

isort(h :: T ) = insert(h, (isortT )) Full Screen

Time Complexity?? Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Merging Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
 Compound Data


 msort [] = [] Higher Order Functions . . .
Imperative . . .

msort [a] = [a] A large Example: . . .



Lecture-wise Index to . . .


 msort L = merge ((msort M ), Home Page

(msort N ))
 Title Page

JJ II

where J I

Page 418 of 644

(M, N ) = split L Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Merging Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

where Correctness, . . .
Compound Data
 Higher Order Functions . . .

 split [] = ([], []) Imperative . . .


A large Example: . . .

split [a] = ([a], []) Lecture-wise Index to . . .

Home Page

split (a :: b :: P ) = (a :: Lef t, b :: Right)



Title Page

JJ II
where J I

Page 419 of 644


(Lef t, Right) = split P
Go Back

Time Complexity?? Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Functions as Data Computing: The . . .
Algorithms: Design & . . .

list of functions Introducing Reals


Correctness, . . .
Compound Data

• Every function is unary. A function Higher Order Functions . . .


Imperative . . .

of many arguments may be thought A large Example: . . .


Lecture-wise Index to . . .

of as a function of a single argu- Home Page

ment i.e. a tuple of appropriate Title Page

JJ II
type.
J I

• Every function is a value of an ap- Page 420 of 644

propriate type. Go Back

Full Screen

• Hence functions are also data. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Higher Order Computing: The . . .
Algorithms: Design & . . .

Functions
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Compound data may be constructed Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
from functions as values using the Home Page

constructors of the compound data Title Page

structure. JJ II

J I
Functions may be defined with
Page 421 of 644

other functions and/or data as arguments Go Back

to produce new values or new functions. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6. Higher Order Functions & Structured Data
6.1. Higher Order Functions
1. Summary: Compound Data Computing: The . . .
Algorithms: Design & . . .
2. List: Examples
Introducing Reals

3. Lists: Sorting Correctness, . . .


Compound Data
4. Higher Order Functions Higher Order Functions . . .

5. An Example Imperative . . .
A large Example: . . .
6. Currying Lecture-wise Index to . . .

7. Currying: Contd
Home Page
8. Generalization
Title Page
9. Generalization: 2
10. Applying a list JJ II

11. Trying it out J I


12. Associativity
Page 422 of 644
13. Apply to a list
Go Back
14. Sequences
15. Further Generalization Full Screen

16. Further Generalization


Close
17. Sequences
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Efficient Generalization
19. Sequences: 2
20. More Generalizations
Computing: The . . .

21. More Summations Algorithms: Design & . . .


Introducing Reals
22. Or Maybe . . . Products Correctness, . . .
N
23. Or Some Other Compound Data

N Higher Order Functions . . .


24. Other Imperative . . .

25. Examples of ⊗, e A large Example: . . .


Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 423 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Summary: Compound Correctness, . . .


Compound Data
Higher Order Functions . . .

Data Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Records and tuples Home Page

Title Page

• Lists JJ II

– Correctness J I

– Examples Page 424 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

List: Examples Correctness, . . .


Compound Data
Higher Order Functions . . .

• Length of a list Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• Searching a list Home Page

• Checking whether a list is ordered


Title Page

JJ II

• Reversing a list J I

• Sorting of lists Page 425 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Lists: Sorting A large Example: . . .


Lecture-wise Index to . . .

Home Page

• Sorting by insertion Title Page

• Sorting by Divide-and-Conquer JJ II

J I

Page 426 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Higher Order
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

Functions
Lecture-wise Index to . . .

Home Page

• Functions as data
Title Page

JJ II

• Higher order functions J I

Page 427 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An Example Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
List of functions Compound Data
Higher Order Functions . . .

• add1 x = x + 1
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• add2 x = x + 2 Home Page

Title Page
• add3 x = x + 3 JJ II
Suppose we needed to define a long J I

list of length n, where the i-th element Page 428 of 644

is the function that adds i + 1 to the Go Back

argument. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Currying Computing: The . . .

addc y x = x + y Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

ML’s response :
Compound Data
Higher Order Functions . . .
Imperative . . .

val addc = fn : A large Example: . . .


Lecture-wise Index to . . .

int -> (int -> int) Home Page

Contrast with ML’s response Title Page

JJ II

- op +; J I

val it = fn : int * int -> int Page 429 of 644

Go Back

addc is the curried version of the bi- Full Screen

nary operation +. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Currying: Contd Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 430 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generalization Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Then Compound Data


Higher Order Functions . . .
Imperative . . .

• addc1 = (addc 1): int -> int A large Example: . . .


Lecture-wise Index to . . .

• addc2 = (addc 2): int -> int Home Page

Title Page

• addc3 = (addc 3): int -> int JJ II

and for any i, J I

(addc i): int -> int Page 431 of 644

Go Back

is the required function. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Generalization: 2 Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

list adds n = Imperative . . .


A large Example: . . .
 Lecture-wise Index to . . .

[] if n ≤ 0 Home Page

(list adds(n − 1))@[(addc n)] else Title Page

JJ II
ML’s response :
J I
val list_adds = fn : Page 432 of 644

int -> (int -> int) list Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Applying a list Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
addall Compound Data
Higher Order Functions . . .
 Imperative . . .

applyl [] x = [] A large Example: . . .


Lecture-wise Index to . . .

applyl (h :: T ) x = (h x) :: (applyl T x) Home Page

Title Page

ML’s response: JJ II

J I
val applyl = fn :
Page 433 of 644

(’a -> ’b) list -> Go Back

’a -> ’b list Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Trying it out Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
interval x n = applyl x (list adds n) Imperative . . .
A large Example: . . .

ML’s response: Lecture-wise Index to . . .

Home Page
val interval = fn :
Title Page

int -> int -> int list JJ II

- interval 53 5; J I

val it = [54,55,56,57,58] Page 434 of 644

: int list Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Associativity Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Application associates to the left. Correctness, . . .


Compound Data

f x y = ((f x) y) Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• → associates to the right. Home Page

α → β → γ = α → (β → γ) Title Page

JJ II
If f : α → β → γ → δ J I

then f a : β → γ → δ Page 435 of 644

and f a b : γ → δ Go Back

and f a b c : δ Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Apply to a list
Apply a list Transpose of a matrix Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
 Correctness, . . .

map f [] = [] Compound Data


Higher Order Functions . . .

map f (h :: T ) = (f h) :: (map f T ) Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

Home Page
val it = fn : (’a -> ’b) -> Title Page

’a list -> ’b list JJ II

- map addc3 [4, 6, ˜1, 0]; J I

val it = [7,9,2,3] : int list Page 436 of 644

- map real [7,9,2,3]; Go Back

Full Screen
val it = [7.0,9.0,2.0,3.0]
Close

: real list Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sequences Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Arithmetic sequences-1 Compound Data

Arithmetic sequences-2 Higher Order Functions . . .


Imperative . . .
Arithmetic sequences-3 A large Example: . . .
Lecture-wise Index to . . .

AS4(a, d, n) = Home Page

 Title Page


 [] if n ≤ 0 JJ II

J I


 a :: (map (addc d) else Page 437 of 644

(AS4 (a, d, (n − 1))))


 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Further Generalization
Given Computing: The . . .
Algorithms: Design & . . .

f :α∗α→α Introducing Reals


Correctness, . . .
Compound Data

Then Higher Order Functions . . .


Imperative . . .
A large Example: . . .

curry2 f x y = f (x, y) Lecture-wise Index to . . .

Home Page

and Title Page

JJ II

(curry2 f ) : α → (α → α) J I

Page 438 of 644

and for any d : α, Go Back

((curry2 f ) d) : α → α Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Further Generalization
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

seq(f, a, d, n) = Compound Data


Higher Order Functions . . .
Imperative . . .
 A large Example: . . .


 [] if n ≤ 0 Lecture-wise Index to . . .

 Home Page

Title Page
 a :: (map ((curry2 f ) d)
 JJ II
(seq (f, a, d, n − 1))) else

J I

is the sequence of length n generated Page 439 of 644

with ((curry2 f ) d), starting from a.


Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sequences Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Arithmetic: AS5(a, d, n) = Correctness, . . .


Compound Data

seq(op+, a, d, n) Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Geometric: GS1(a, r, n) = Lecture-wise Index to . . .

seq(op∗, a, r, n) Home Page

Title Page

Harmonic: HS1(a, d, n) = JJ II

map reci (AS5(a, d, n)) J I

Page 440 of 644

where
reci x = 1.0/(real x) gives the recipro- Go Back

cal of a (non-zero) integer. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Efficient Generalization Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Let’s not use map repeatedly. Correctness, . . .


Compound Data
Higher Order Functions . . .
seq2(f, g, a, d, n) = Imperative . . .
A large Example: . . .
 Lecture-wise Index to . . .

 [] if n ≤ 0 Home Page

Title Page

(f a) :: (seq2 (f, g(a, d), d, n − 1)) else


 JJ II

J I
is the sequence of length n generated Page 441 of 644

with a unary f , a binary g starting from Go Back

f (a). Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Sequences: 2 Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• AS6(a, d, n) = seq2(id, op+, a, d, n) Lecture-wise Index to . . .

Home Page

• GS2(a, r, n) = seq2(id, op∗, a, r, n) Title Page

• HS2(a, d, n) = seq2(reci, op+, a, d, n) JJ II

J I

where id x = x is the identity function. Page 442 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Generalizations Computing: The . . .

Often interested in some particular Algorithms: Design & . . .


Introducing Reals

measure related to a sequence, rather


Correctness, . . .
Compound Data
Higher Order Functions . . .

than in the sequence itself, e.g. sum- Imperative . . .


A large Example: . . .

mations of Lecture-wise Index to . . .

Home Page

• arithmetic, geometric, harmonic se- Title Page

quences JJ II

• ex,
J I
trigonometric functions upto Page 443 of 644

some n-th term Go Back

• (Truncated) Taylor and Maclaurin Full Screen

series Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Summations
Wasteful to first generate the se- Computing: The . . .

quence and then compute the mea- Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

sure Compound Data


Higher Order Functions . . .

Xu Imperative . . .
A large Example: . . .

f (i) Lecture-wise Index to . . .

Home Page

i=l Title Page

where the range [l, u] is defined by a JJ II

unary succ function J I

sum(f, succ, l, u) = Page 444 of 644

Go Back

0 if [l, u] = ∅ Full Screen

f (l)+sum(f, succ, succ(l), u) else Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Or Maybe . . . Products Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Or may be interested in forming prod- Correctness, . . .


Compound Data

ucts of sequences. Higher Order Functions . . .


Imperative . . .
A large Example: . . .
u
Y Lecture-wise Index to . . .

f (i) Home Page

Title Page
i=l
JJ II

prod(f, succ, l, u) = J I
 Page 445 of 644
1 if [l, u] = ∅ Go Back

f (l)∗prod(f, succ, succ(l), u) else Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Or Some Other N

Or some other binary operation ⊗ Computing: The . . .


Algorithms: Design & . . .

which has the following properties: Introducing Reals


Correctness, . . .
Compound Data

• ⊗ : (α ∗ α) → α is closed Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• ⊗ is associative i.e. Lecture-wise Index to . . .

a ⊗ (b ⊗ c) = (a ⊗ b) ⊗ c Home Page

Title Page

• ⊗ has an identity element e i.e JJ II

a⊗e=a=e⊗a J I

Page 446 of 644

u
O Go Back

f (l) Full Screen

Close

i=l
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Other N Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

Then if f, succ : α → α Lecture-wise Index to . . .

ser(⊗, f, succ, l, u) = Home Page

Title Page

e if [l, u] = ∅ JJ II

f (l) ⊗ ser(⊗, f, succ, succ(l), u) else J I

Page 447 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Examples of ⊗, e Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• +, 0 on integers and reals Compound Data


Higher Order Functions . . .
Imperative . . .

• concatenation and the empty string A large Example: . . .


Lecture-wise Index to . . .

on strings Home Page

• andalso, true on booleans Title Page

JJ II

• orelse, false on booleans J I

• +, 0 on vectors and matrices Page 448 of 644

Go Back

• ∗, 1 on vectors and matrices Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6.2. Structured Data
1. Transpose of a Matrix
2. Transpose: 0
Computing: The . . .

3. Transpose: 10 Algorithms: Design & . . .


Introducing Reals
4. Transpose: 01 Correctness, . . .

5. Transpose: 20 Compound Data


Higher Order Functions . . .
6. Transpose: 02 Imperative . . .

7. Transpose: 30 A large Example: . . .


Lecture-wise Index to . . .
8. Transpose: 03
Home Page
9. trans
10. is2DM atrix Title Page

11. User Defined Types JJ II


12. Enumeration Types
J I
13. User Defined Structural Types
Page 449 of 644
14. Functions vs. data
15. Data as 0-ary Functions Go Back

16. Data vs. Functions


Full Screen
17. Data vs. Functions: Recursion
Close
18. Lists
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Constructors
20. Shapes
21. Shapes: Triangle Inequality
Computing: The . . .

22. Shapes: Area Algorithms: Design & . . .


Introducing Reals
23. Shapes: Area Correctness, . . .

24. ML: Try out Compound Data


Higher Order Functions . . .
25. ML: Try out (contd.) Imperative . . .

26. Enumeration Types A large Example: . . .


Lecture-wise Index to . . .
27. Recursive Data Types
Home Page
28. Resistors: Datatype
29. Resistors: Equivalent Title Page

30. Resistors JJ II
31. Resistors: Example
J I
32. Resistors: ML session
Page 450 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose of a Matrix Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Map Correctness, . . .
Compound Data
Higher Order Functions . . .

Assume a 2-D r × c matrix is repre- Imperative . . .


A large Example: . . .

sented by a list of lists of elements. Lecture-wise Index to . . .

Home Page

Then Title Page

transpose L = JJ II

trans L if is2DM atrix(L) J I


Page 451 of 644
else Go Back

where Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 0
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 11 12 13 ]
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 21 22 23 ] Home Page

[ 31 33 ]
Title Page

32 JJ II

[ 41 42 43 ] J I

Page 452 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 10
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 11 12 13 ]
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 21 22 23 ] Home Page

[ 31 33 ]
Title Page

32 JJ II

[ 41 42 43 ] J I

Page 453 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 01
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 12 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 22 23 ] Home Page

[ 33 ]
Title Page

32 JJ II

[ 42 43 ] J I

Page 454 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 20
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 12 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 22 23 ] Home Page

[ 33 ]
Title Page

32 JJ II

[ 42 43 ] J I

Page 455 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 02
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 23 ] Home Page

[ 12 22 32 42 ]
[ 33 ]
Title Page

JJ II

[ 43 ] J I

Page 456 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 30
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ 23 ] Home Page

[ 12 22 32 42 ]
[ 33 ]
Title Page

JJ II

[ 43 ] J I

Page 457 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 03
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .

[ ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

[ ] Home Page

[ 12 22 32 42 ]
[ ]
Title Page

JJ II

[ ] [ 13 23 33 43 ] J I

Page 458 of 644

] ] Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

trans Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
 Compound Data


 trans [] = [] Higher Order Functions . . .
Imperative . . .

trans [] :: T L = []
 A large Example: . . .
Lecture-wise Index to . . .


 trans LL = (map hd LL) :: Home Page

(trans (map tl LL))


 Title Page

JJ II

and J I

is2DM atrix = #1(dimensions L) Page 459 of 644

where Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
is2DM atrix Computing: The . . .
Algorithms: Design & . . .
 Introducing Reals

 dimensions [] = (true, 0, 0) Correctness, . . .


 Compound Data

 Higher Order Functions . . .

 Imperative . . .

 dimensions [H] =

 A large Example: . . .
Lecture-wise Index to . . .

(true, 1, h) Home Page


 Title Page


dimensions (H :: T L) = JJ II



 J I
(b and (h = c), r + 1, c)

Page 460 of 644

where dimensions T L = (b, r, c) Go Back

and h = length H Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

User Defined Types Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Many languages allow user-defined Compound Data


Higher Order Functions . . .
Imperative . . .

data types. A large Example: . . .


Lecture-wise Index to . . .

• record types: Pinky and Billu Home Page

Title Page

• Enumerations: aggregates of het- JJ II

erogeneous data. J I

• other structural constructions (if Page 461 of 644

desperate!)
Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Enumeration Types
Many languages allow user-defined Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

data types. Correctness, . . .


Compound Data
Higher Order Functions . . .
• record types: Pinky and Billu Imperative . . .
A large Example: . . .

• Enumerations: aggregates of het-


Lecture-wise Index to . . .

Home Page

erogeneous data. Title Page

– days of the week JJ II

J I
– colours Page 462 of 644

– geometrical shapes Go Back

• other structural constructions (if Full Screen

Close

desperate!) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Defined
Structural Types Computing: The . . .
Algorithms: Design & . . .
Many languages allow user-defined Introducing Reals
Correctness, . . .

data types. Compound Data


Higher Order Functions . . .
Imperative . . .

• record types: Pinky and Billu A large Example: . . .


Lecture-wise Index to . . .

• Enumerations: aggregates of het- Home Page

erogeneous data. Title Page

JJ II

• other structural constructions (if J I

desperate!) Page 463 of 644

Go Back
– trees Full Screen

– graphs Close

– symbolic expressions Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Functions vs. data Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

• Inspired by the list constructors, nil Imperative . . .


A large Example: . . .

and cons Lecture-wise Index to . . .

Home Page

• Grand Unification of functions and Title Page

data JJ II

J I
– Functions as data Page 464 of 644

– Data as functions Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Data as 0-ary Functions Computing: The . . .
Algorithms: Design & . . .

• Every data element may be re- Introducing Reals


Correctness, . . .

garded as a function with 0 argu- Compound Data


Higher Order Functions . . .

ments Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

– Caution: A constant function Home Page

f (x) = 5, for all x : α Title Page

where JJ II

f : α → int J I

Page 465 of 644


is not the same as a value
5 : int Go Back

Full Screen

. Their types are different. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Data vs. Functions Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

Facilities Functions Data Lecture-wise Index to . . .

Home Page

primitive operations values Title Page

user-defined functions constructors JJ II

composition application alternative J I

recursion recursion recursion Page 466 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Data vs. Functions: Correctness, . . .


Compound Data

Recursion Higher Order Functions . . .


Imperative . . .
A large Example: . . .

Recursion Lecture-wise Index to . . .

Home Page

Basis Title Page

naming JJ II

composition J I

induction Page 467 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists as Structured
Data
Computing: The . . .

datatype ’a list = Algorithms: Design & . . .


Introducing Reals

nil | Correctness, . . .
Compound Data
Higher Order Functions . . .
cons of ’a * ’a list Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Every α list is either Title Page

nil : (Basis, name) JJ II

J I
| : or (alternative) Page 468 of 644

cons : constructed inductively from Go Back

an element of type ’a and another Full Screen

list of type ’a list using the con-


Close

Quit

structor cons • •
First• • •
Prev Next Last Go Back •Full Screen •Close •Quit
Constructors Computing: The . . .
Algorithms: Design & . . .

• Inspired by the list constructors Introducing Reals


Correctness, . . .
Compound Data

nil : α list Higher Order Functions . . .


Imperative . . .

cons : α × α list → α list A large Example: . . .


Lecture-wise Index to . . .

• combine heterogeneous types: α Home Page

Title Page

and α list JJ II

• allows recursive definition by a form J I

of induction Page 469 of 644

Go Back

Basis : nil Full Screen

Induction : cons Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

A non-recursive data type Compound Data


Higher Order Functions . . .
Imperative . . .

datatype shape = A large Example: . . .


Lecture-wise Index to . . .

Home Page

CIRCLE of real Title Page

| RECTANGLE of real * real JJ II

J I
| TRIANGLE of
Page 470 of 644

real * real * real Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes: Triangle Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Inequality Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

fun isTriangle A large Example: . . .


Lecture-wise Index to . . .

(TRIANGLE (a, b, c)) = Home Page

(a+b>c) andalso Title Page

JJ II
(b+c>a) andalso
J I
(c+a>b) Page 471 of 644

| isTriangle _ = false Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes: Area Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
exception notShape; Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

fun area (CIRCLE (r)) = Lecture-wise Index to . . .

Home Page
3.14159 * r * r Title Page

| area (RECTANGLE (l,b)) = JJ II

l*b J I

| area (s as Page 472 of 644

TRIANGLE (a, b, c)) = Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Shapes: Area
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

if isTriangle (s) then Imperative . . .


A large Example: . . .

let val s = (a+b+c)/2.0 Lecture-wise Index to . . .

Home Page
in Math.sqrt Title Page

(s*(s-a)*(s-b)*(s-c)) JJ II

end J I

else raise notShape; Page 473 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: Try out
- use "shapes.sml"; Computing: The . . .
Algorithms: Design & . . .

[opening shapes.sml] Introducing Reals


Correctness, . . .
Compound Data
datatype shape Higher Order Functions . . .
Imperative . . .

= CIRCLE of real A large Example: . . .


Lecture-wise Index to . . .

| RECTANGLE of real * real Home Page

| TRIANGLE of Title Page

real * real * real JJ II

J I
val isTriangle =
Page 474 of 644

fn : shape -> bool Go Back

exception notShape Full Screen

val area = fn : shape -> real Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: Try out (contd.)
Computing: The . . .

val it = () : unit Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

- area (TRIANGLE (2.0,1.0,3.0)); Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

uncaught exception notShape Lecture-wise Index to . . .

Home Page

raised at: shapes.sml:22.17-22.25 Title Page

- area JJ II

(TRIANGLE (3.0, 4.0, 5.0)); J I

val it = 6.0 : real Page 475 of 644

- Go Back

Full Screen

Close
Back to User defined types
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Enumeration Types Computing: The . . .

• Enumeration types are non- Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

recursive datatypes with Compound Data


Higher Order Functions . . .
Imperative . . .
• 0-ary constructors A large Example: . . .
Lecture-wise Index to . . .

datatype working = MON | TUE Home Page

Title Page
| WED | THU | FRI;
JJ II
datatype weekends = SAT | SUN J I

datatype weekdays = working Page 476 of 644

| weekends; Go Back

Full Screen

Back to User defined types Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursive Data Types Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• But the really interesting types are Correctness, . . .


Compound Data

the recursive data types Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Back to Lists Home Page

• As with lists proofs of correctness Title Page

on recursive data types depend on JJ II

J I
a case-analysis of the structure (ba- Page 477 of 644

sis and inductive constructors) Go Back

Correctness on lists Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Resistors: Datatype Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .

datatype resist = A large Example: . . .


Lecture-wise Index to . . .

RES of real | Home Page

SER of resist * resist | Title Page

PAR of resist * resist JJ II

J I

Page 478 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Equivalent Computing: The . . .
Algorithms: Design & . . .

fun value (RES (r)) = r Introducing Reals


Correctness, . . .
Compound Data
| value (SER (R1, R2)) = Higher Order Functions . . .
Imperative . . .

value (R1) + value (R2) A large Example: . . .


Lecture-wise Index to . . .

| value (PAR (R1, R2)) = Home Page

let val r1 = value (R1); Title Page

val r2 = value (R2) JJ II

J I
in (r1*r2)/(r1+r2)
Page 479 of 644

end; Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors
5.0 Computing: The . . .
Algorithms: Design & . . .

5.0 2.0
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

4.0
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

3.0 Page 480 of 644

Go Back

Full Screen

+ − Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Example
val R = PAR(
SER( Computing: The . . .
Algorithms: Design & . . .

PAR( Introducing Reals


Correctness, . . .

RES (5.0), Compound Data


Higher Order Functions . . .
Imperative . . .
RES (4.0) A large Example: . . .
Lecture-wise Index to . . .

), Home Page

SER( Title Page

RES (5.0), JJ II

RES (2.0) J I

Page 481 of 644


)
Go Back

), Full Screen

RES(3.0) Close

); Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: ML session
- use "resistors.sml";
Computing: The . . .
[opening resistors.sml] Algorithms: Design & . . .
Introducing Reals

datatype resist = PAR of resist * res Correctness, . . .


Compound Data

| RES of real Higher Order Functions . . .


Imperative . . .

| SER of resist * res A large Example: . . .


Lecture-wise Index to . . .

val value = fn : resist -> real Home Page

val R = PAR (SER (PAR #,SER #),RES 3. Title Page

JJ II
val it = () : unit
J I
- value R; Page 482 of 644

val it = 2.26363636364 : real Go Back

- Full Screen

Close

Quit
Resistance Expressions
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6.3. User Defined Structured Data Types
Computing: The . . .
1. User Defined Types
Algorithms: Design & . . .
2. Resistors: Grouping Introducing Reals
Correctness, . . .
3. Resistors: In Pairs
Compound Data
4. Resistor: Values Higher Order Functions . . .
Imperative . . .
5. Resistance Expressions
A large Example: . . .
6. Resistance Expressions Lecture-wise Index to . . .

7. Arithmetic Expressions Home Page

8. Arithmetic Expressions: 0
Title Page
9. Arithmetic Expressions: 1
JJ II
10. Arithmetic Expressions: 2
11. Arithmetic Expressions: 3 J I

12. Arithmetic Expressions: 4


Page 483 of 644

13. Arithmetic Expressions: 5


Go Back
14. Arithmetic Expressions: 6
15. Arithmetic Expressions: 7 Full Screen

16. Arithmetic Expressions: 8 Close

17. Binary Trees


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Arithmetic Expressions: 0
19. Trees: Traversals
20. Recursive Data Types: Correctness
Computing: The . . .

21. Data Types: Correctness Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 484 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Defined Types
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Records Compound Data


Higher Order Functions . . .
Imperative . . .

• Structural Types A large Example: . . .


Lecture-wise Index to . . .

– Constructors Home Page

Title Page

∗ Non-recursive JJ II

∗ Enumeration Types J I

– Recursive datatypes Page 485 of 644

Go Back

∗ Resistance circuits Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Grouping
Computing: The . . .

R3 Algorithms: Design & . . .


Introducing Reals

5.0
Correctness, . . .
Compound Data

5.0 2.0
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

4.0 R2 Title Page

R1 JJ II

J I

3.0 R4
Page 486 of 644

Go Back

Full Screen

+ − Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Resistors: In Pairs Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

val R1 = PAR (RES 5.0,RES 4.0) : resi Lecture-wise Index to . . .

val R2 = SER (RES 5.0,RES 2.0) : resi Home Page

Title Page
val R3 = SER (R1, R2); JJ II
val R4 = PAR (R3, RES(3.0)); J I

Page 487 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistor: Values Computing: The . . .
Algorithms: Design & . . .

- value R1; Introducing Reals


Correctness, . . .

val it = 2.22222222222 : real Compound Data


Higher Order Functions . . .
Imperative . . .
- value R2; A large Example: . . .
Lecture-wise Index to . . .

val it = 7.0 : real Home Page

- value R3; Title Page

val it = 9.22222222222 : real JJ II

- value R4; J I

Page 488 of 644


val it = 2.26363636364 : real
Go Back

- Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistance
Expressions
Computing: The . . .
A resistance expression Algorithms: Design & . . .
Introducing Reals

PAR Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

SER
Lecture-wise Index to . . .

Home Page

Title Page

PAR SER JJ II

J I

Page 489 of 644

RES RES RES RES RES Go Back

Full Screen

5 4 5 2 3 Close

Quit
Circuit Diagram
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistance
Expressions
A resistance expression Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

R4 PAR Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

R3 SER
Lecture-wise Index to . . .

Home Page

Title Page

R1 PAR R2 SER JJ II

J I

Page 490 of 644

RES RES RES RES RES Go Back

Full Screen

5 4 5 2 3 Close

Quit

•First Circuit
•Prev •NextDiagram
•Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Arithmetic Expressions Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

ML arithmetic expressions: Home Page

Title Page
((5 * ˜4) + ˜(5 - 2)) div 3
JJ II
are represented as trees J I

Page 491 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 0 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

div Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

+
Lecture-wise Index to . . .

Home Page

Title Page

JJ II
* ~
J I

− Page 492 of 644

5 ~ 3 Go Back

Full Screen

4 5 2 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

div Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

+ Lecture-wise Index to . . .

Home Page

Title Page

JJ II
* ~
J I

− Page 493 of 644

5 ~ 3 Go Back

Full Screen

4 5 2 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .

Expressions: 2 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

div Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

+
Home Page

Title Page

JJ II

J I
* ~
Page 494 of 644

3
Go Back

5 −4 3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .

Expressions: 3 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
div Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

+ Home Page

Title Page

JJ II

~
J I
*
Page 495 of 644

3 Go Back

5 −4 3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .

Expressions: 4 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

div Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

+
Home Page

Title Page

JJ II

J I
−20 −3
Page 496 of 644

Go Back

3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .

Expressions: 5 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

div Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

+
Home Page

Title Page

JJ II

J I
−20 −3
Page 497 of 644

Go Back

3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .

Expressions: 6 Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

div Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

−23
Title Page

JJ II

J I

Page 498 of 644

Go Back

3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 7
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

div Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

−23
Title Page

JJ II

J I

Page 499 of 644

Go Back

3 Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Arithmetic A large Example: . . .


Lecture-wise Index to . . .

Expressions: 8 Home Page

Title Page

−8 JJ II

J I

Page 500 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Binary Trees
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

datatype ’a bintree = Imperative . . .


A large Example: . . .

Empty | Lecture-wise Index to . . .

Home Page
Node of ’a * Title Page

’a bintree * JJ II

’a bintree J I

Page 501 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 0 Computing: The . . .
Algorithms: Design & . . .
Arithmetic Expressions Introducing Reals
Correctness, . . .

div Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
+
Home Page

Title Page

* ~ JJ II

J I

~ − 3
Page 502 of 644
5
Go Back

Full Screen

4 5 2 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Trees: Traversals
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• preorder Home Page

• inorder
Title Page

JJ II

• postorder J I

Page 503 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Recursive Data Types: Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

Correctness Home Page

Title Page
Correctness on lists by cases
JJ II
P is proved by case analysis. J I

Page 504 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Data Types: Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Correctness Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Basis Prove P (c) for each non- A large Example: . . .


Lecture-wise Index to . . .

recursive constructor c Home Page

Induction hypothesis (IH) Assume Title Page

JJ II
P (T ) for all elements of the data
J I
type less than a certain depth Page 505 of 644

Induction Step Prove P (r(T1, . . . , Tn)) Go Back

for each recursive constructor r Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Imperative Programming: An Introduction
7.1. Introducing a Memory Model
1. Summary: Functional Model Computing: The . . .
Algorithms: Design & . . .
2. CPU & Memory: Simplified
Introducing Reals

3. Resource Management Correctness, . . .


Compound Data
4. Shell: User Interface Higher Order Functions . . .

5. GUI: User Interface Imperative . . .


A large Example: . . .
6. Memory Model: Simplified Lecture-wise Index to . . .

7. Memory
Home Page
8. The Imperative Model
Title Page
9. State Changes: σ
10. State JJ II

11. State Changes J I


12. State Changes: σ
Page 506 of 644
13. State Changes: σ1
Go Back
14. State Changes: σ2
15. Languages Full Screen

16. User Programs


Close
17. Imperative Languages
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Imperative vs Functional Variables
19. Assignment Commands
20. Assignment Commands
Computing: The . . .

21. Assignment Commands Algorithms: Design & . . .


Introducing Reals
22. Assignment Commands Correctness, . . .

23. Assignment Commands Compound Data


Higher Order Functions . . .
24. Assignment Commands: Swap Imperative . . .

25. Swap A large Example: . . .


Lecture-wise Index to . . .
26. Swap
Home Page
27. Swap
Title Page

JJ II

J I

Page 507 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Summary: Functional
Model Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Stateless (as is most mathematics) Correctness, . . .


Compound Data
Higher Order Functions . . .

• Notion of value is paramount Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

– Integers, reals, booleans, strings Home Page

and characters are all values Title Page

– Every function is also a value JJ II

J I
– Every complex piece of data is Page 508 of 644

also a value Go Back

• No concept of storage (except for Full Screen

Close

space complexity calculations) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CPU & Memory:
Simplified Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Memory A large Example: . . .


Lecture-wise Index to . . .

Home Page

Title Page

Peripherals JJ II

CPU
J I
Printer
Disk
Page 509 of 644

Go Back

Keyboard Full Screen

Screen Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resource
Management Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Memory A large Example: . . .


Lecture-wise Index to . . .

Home Page

Peripherals
Title Page

JJ II
CPU Printer
J I
Disk
Page 510 of 644

Keyboard
Go Back
Screen
Full Screen

Operating System Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shell: User Interface Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Memory Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Peripherals
Home Page

Title Page
CPU Printer
Disk JJ II

Keyboard J I
Screen
Page 511 of 644

Operating System Go Back

Full Screen

Shell Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
GUI: User Interface Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Memory Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Peripherals Home Page

CPU Printer Title Page


Disk

Keyboard JJ II
Screen
J I

Operating System Page 512 of 644

Shell Go Back

Full Screen
Graphical User Interface (GUI)
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Memory Model:
Simplified Computing: The . . .
Algorithms: Design & . . .

1. A sequence of storage cells Introducing Reals


Correctness, . . .
Compound Data

2. Each cell is a container of a single Higher Order Functions . . .


Imperative . . .

unit of information. A large Example: . . .


Lecture-wise Index to . . .

• integer, real, boolean, character


Home Page

Title Page

or string JJ II

3. Each cell has a unique name, called J I

its address Page 513 of 644

Go Back

4. The memory cell addresses range Full Screen

from 0 to (usually) 2k − 1 (for some Close

k) Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Memory
0 1 2 3 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 514 of 644

Go Back

Full Screen

Close

32K−1 Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

The Imperative Model Introducing Reals


Correctness, . . .
Compound Data

• Memory or Storage made explicit Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Notion of state (of memory) Lecture-wise Index to . . .

Home Page

– State is simply the value con- Title Page

tained in each cell. JJ II

J I
– state : Addresses → V alues
Page 515 of 644

• State changes Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

4
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

3.1 Home Page

Title Page

true JJ II

J I

Page 516 of 644

"#a" Go Back

Full Screen

Close

Assume all other cells are filled with null Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

State
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

The state σ Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• σ(12) = 4 : int Home Page

• σ(20) = null
Title Page

JJ II

• σ(43) = true : bool J I

• σ(66) = ”#a” : char Page 517 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

State Changes Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• A state change takes place when


Lecture-wise Index to . . .

Home Page

the value in some cell changes Title Page

• The contents of only one cell may JJ II

be changed at a time. J I

Page 518 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

4
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

3.1 Home Page

Title Page

true JJ II

J I

Page 519 of 644

"#a" Go Back

Full Screen

Close

Assume all other cells are filled with null Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ1
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

5 Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

3.1 Home Page

Title Page

true JJ II

J I

Page 520 of 644

"#a" Go Back

Full Screen

Close

Assume all other cells are filled with null Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ2
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

5 Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

3.1 Home Page

Title Page

true JJ II

J I

Page 521 of 644

12 "#a" Go Back

Full Screen

Close

Assume all other cells are filled with null Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Languages
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Memory Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Peripherals
Home Page

Title Page

CPU Printer
JJ II
Disk
J I
Keyboard
Page 522 of 644
Screen
Go Back

Operating System Full Screen

Close

Programming Language Interface Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Programs
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Memory Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Peripherals Home Page

CPU Printer Title Page

Disk
JJ II
Keyboard
J I
Screen
Page 523 of 644

Operating System Go Back

Full Screen
Programming Language Interface
Close

User Programs Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative Languages Computing: The . . .

• How is the memory accessed? Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

– Through system calls to the OS. Compound Data


Higher Order Functions . . .
Imperative . . .

• How are memory cells identified? A large Example: . . .


Lecture-wise Index to . . .

– Use Imperative variables. Home Page

Title Page

– Each such variable is a name JJ II

mapped to an address . J I

• How are state changes accom- Page 524 of 644

Go Back

plished? Full Screen

– By the assignment command. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Imperative vs Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Functional Variables Compound Data


Higher Order Functions . . .
Imperative . . .

Functional Imperative A large Example: . . .


Lecture-wise Index to . . .

name of a value name of an address Home Page

constant could change with time Title Page

JJ II

J I

Page 525 of 644

The value contained in an imperative Go Back


variable x is denoted !x. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Commands Correctness, . . .
Compound Data
Higher Order Functions . . .

Let x and y be imperative variables.


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Consider the following commands. Home Page

Assuming !x = 1 and !y = 2. Title Page

JJ II

J I

Page 526 of 644

x 1 y 2 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Commands Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Store the value 5 in x. A large Example: . . .
Lecture-wise Index to . . .
x := 5 Home Page

Title Page

JJ II

J I

x 5 y 2
Page 527 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Commands Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Copy the value contained in y into x. A large Example: . . .
Lecture-wise Index to . . .
x :=!y Home Page

Title Page

JJ II

J I

x 2 y 2
Page 528 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .

Commands Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

Increment the value contained in x by 1. Imperative . . .


A large Example: . . .

x :=!x + 1 Lecture-wise Index to . . .

Home Page

. Title Page

JJ II

J I

Page 529 of 644

x 3 y 2 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .

Commands
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Store the product of the values in x and y Imperative . . .


A large Example: . . .

in y. Lecture-wise Index to . . .

Home Page

y :=!x∗!y Title Page

JJ II

J I

Page 530 of 644

x 3 y 6 Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment
Commands: Swap Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Swap the values in x and y. Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Swapping values implies trying to


Home Page

Title Page

make two state changes simultane- JJ II

ously! J I

Page 531 of 644

Go Back

Requires a new memory cell t to tem- Full Screen

porarily store one of the values. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Swap Computing: The . . .
Algorithms: Design & . . .

How does one get a new memory Introducing Reals


Correctness, . . .
Compound Data

cell? Higher Order Functions . . .


Imperative . . .

val t = ref 0 A large Example: . . .


Lecture-wise Index to . . .

Then the rest is easy Home Page

Title Page

val t = ref 0; JJ II

t := !x; J I

x := !y; Page 532 of 644

Go Back

y := !t; Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Swap
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

Could be made simpler! A large Example: . . .


Lecture-wise Index to . . .

Home Page

val t = ref (!x); Title Page

x := !y; JJ II

y := !t; J I

Page 533 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Swap
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Could use a temporary functional vari- Higher Order Functions . . .


Imperative . . .

able t instead of an imperative vari-


A large Example: . . .
Lecture-wise Index to . . .

able Home Page

Title Page

val t = !x; JJ II

x := !y; J I

y := t; Page 534 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7.2. Imperative Programming:
Computing: The . . .
1. Imperative vs Functional
Algorithms: Design & . . .
2. Features of the Store Introducing Reals
Correctness, . . .
3. References: Experiments
Compound Data
4. References: Experiments Higher Order Functions . . .
Imperative . . .
5. References: Experiments
A large Example: . . .
6. Aliases Lecture-wise Index to . . .

7. References: Experiments Home Page

8. References: Aliases
Title Page
9. References: Experiments
JJ II
10. After Garbage Collection
11. Side Effects J I

12. Imperative ML
Page 535 of 644

13. Imperative ML
Go Back
14. Imperative ML
15. Imperative ML Full Screen

16. Nasty Surprises Close

17. Imperative ML
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Imperative ML
19. Common Errors
20. Aliasing & References
Computing: The . . .

21. Dangling References Algorithms: Design & . . .


Introducing Reals
22. New Reference Correctness, . . .

23. Imperative Commands: Basic Compound Data


Higher Order Functions . . .
24. Imperative Commands: Compound Imperative . . .

25. Predefined Compound Commands A large Example: . . .


Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 536 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Imperative vs Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

Functional Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• Functional Model Lecture-wise Index to . . .

Home Page

• Memory/Store Model Title Page

• Imperative Model JJ II

J I
• State Changes Page 537 of 644

• Accessing the store Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Features of the Store


Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Memory is treated as a datatype with Higher Order Functions . . .


Imperative . . .
A large Example: . . .

constructors Lecture-wise Index to . . .

Home Page

Allocation ref : α → α ref Title Page

Dereferencing ! : α ref → α JJ II

J I
Updation :=: α ref ∗ α → unit Page 538 of 644

Deallocation of memory is automatic! Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .

- val a = ref 0; Algorithms: Design & . . .


Introducing Reals

val a = ref 0 : int ref Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
0 1 2 3
Lecture-wise Index to . . .
0
Home Page

Title Page

JJ II

J I

Page 539 of 644

Go Back

Full Screen

Close

Quit

a •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .

- val b = ref 0; Algorithms: Design & . . .


Introducing Reals

val b = ref 0 : int ref Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
0 1 2 3
Lecture-wise Index to . . .
0
Home Page

Title Page

JJ II

0 J I

Page 540 of 644

Go Back

Full Screen

Close

Quit

a b •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

References: Correctness, . . .
Compound Data

Experiments Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

- a = b; Home Page

val it = false : bool Title Page

- !a = !b; JJ II

J I
val it = true : bool
Page 541 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Aliases
- val x = ref 0; Computing: The . . .
Algorithms: Design & . . .

val x = ref 0 : int ref Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
0 1 2 3 Imperative . . .

0 A large Example: . . .
Lecture-wise Index to . . .

Home Page

0 Title Page

0 JJ II

J I

Page 542 of 644

Go Back

Full Screen

Close

a b x Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .

- val y = x; Algorithms: Design & . . .


Introducing Reals

val y = ref 0 : int ref Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
0 1 2 3
Lecture-wise Index to . . .
0
Home Page

Title Page

0 JJ II

0 J I

Page 543 of 644

Go Back

Full Screen

Close

Quit

a b x y •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References: Aliases
- x := !x + 1; Computing: The . . .
Algorithms: Design & . . .

val it = () : unit Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
0 1 2 3 Imperative . . .

0 A large Example: . . .
Lecture-wise Index to . . .

Home Page

1 Title Page

0 JJ II

J I

Page 544 of 644

Go Back

Full Screen

Close

a b x y Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

References: Correctness, . . .
Compound Data

Experiments Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

- !y; Home Page

val it = 1 : int Title Page

- x = y; JJ II

J I
val it = true : bool
Page 545 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
After Garbage
Collection Computing: The . . .
Algorithms: Design & . . .

GC #0.0.0.0.2.45: (0 ms) Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
0 1 2 3
A large Example: . . .
0 0 1 Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 546 of 644

Go Back

Full Screen

Close

a b x y Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Side Effects Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• Assignment does not produce a Compound Data


Higher Order Functions . . .

value Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• It produces only a state change Home Page

(side effect) Title Page

JJ II
• But side-effects are compatible with
J I
functional programming since it is Page 547 of 644

provided as a new data type with Go Back

constructors and destructors. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Does not provide direct access to Correctness, . . .


Compound Data
Higher Order Functions . . .
memory addresses Imperative . . .
A large Example: . . .

• Does not allow for uninitialized im-


Lecture-wise Index to . . .

Home Page

perative variables Title Page

• Provides a type with every memory JJ II

location J I

Page 548 of 644

• Manages the memory completely Go Back

automatically Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .

memory addresses Introducing Reals


Correctness, . . .
Compound Data

– Prevents the use of memory ad- Higher Order Functions . . .


Imperative . . .

dresses as integers that can be A large Example: . . .


Lecture-wise Index to . . .

manipulated by the user program Home Page

• Does not allow for uninitialized im-


Title Page

JJ II
perative variables J I

• Provides a type with every memory Page 549 of 644

location Go Back

Full Screen

• Manages the memory completely Close

automatically Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .

memory addresses Introducing Reals


Correctness, . . .
Compound Data

• Does not allow for uninitialized im- Higher Order Functions . . .


Imperative . . .

perative variables A large Example: . . .


Lecture-wise Index to . . .

– Most imperative languages keep Home Page

declarations separate from initial-


Title Page

JJ II
izations J I

• Provides a type with every memory Page 550 of 644

cell Go Back

Full Screen

• Manages the memory completely Close

automatically Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .

memory addresses Introducing Reals


Correctness, . . .
Compound Data

• Does not allow for uninitialized im- Higher Order Functions . . .


Imperative . . .

perative variables A large Example: . . .


Lecture-wise Index to . . .

– A frequent source of surprising Home Page

results in most imperative lan-


Title Page

JJ II
guage programs J I

• Provides a type with every memory Page 551 of 644

cell Go Back

Full Screen

• Manages the memory completely Close

automatically Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Nasty Surprises
Separation of declaration from initial- Computing: The . . .
Algorithms: Design & . . .

ization Introducing Reals


Correctness, . . .
Compound Data

• Uninitialized variables Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Execution time errors if not de- Lecture-wise Index to . . .

tected by compiler, since every Home Page

Title Page
memory location contains some JJ II
data J I

• Might use a value stored previously Page 552 of 644

in that location by some imperative Go Back

Full Screen

variable that no longer exists. Close

• Errors due to type violations. Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• Does not provide direct access to Correctness, . . .


Compound Data
Higher Order Functions . . .
memory addresses Imperative . . .
A large Example: . . .

• Does not allow for uninitialized im-


Lecture-wise Index to . . .

Home Page

perative variables Title Page

• Provides a type with every memory JJ II

cell J I

Page 553 of 644

• Manages the memory completely Go Back

automatically and securely. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to
Computing: The . . .

memory addresses Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

• Does not allow for uninitialized im- Compound Data


Higher Order Functions . . .

perative variables Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• Provides a type with every memory Home Page

cell Title Page

• Manages the memory completely JJ II

J I
automatically and securely Page 554 of 644

– Memory has to be managed by Go Back

the user program in most lan- Full Screen

guages Close

Quit

– Prone to various errors


• • •
First • •
Prev Next Last Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Common Errors
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• Memory access errors due to inte- A large Example: . . .


Lecture-wise Index to . . .

ger arithmetic, especially in large Home Page

structures (arrays) Title Page

JJ II

• Dangling references on dealloca- J I

tion of aliased memory Page 555 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Aliasing & References Computing: The . . .
Algorithms: Design & . . .

Before deallocation:
0 1 2 3
Introducing Reals
Correctness, . . .
Compound Data
0 0 1 Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 556 of 644

Go Back

Full Screen

a b x y
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Dangling References
Deallocate x through a system call Computing: The . . .
0 1 2 3
Algorithms: Design & . . .

0 0 Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 557 of 644

a b y Go Back

Full Screen

Close

y is left dangling! Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
New Reference
val z = ref 12;
Computing: The . . .
Algorithms: Design & . . .
0 1 2 3 Introducing Reals

0 0 12 Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 558 of 644

Go Back

a b y z Full Screen

Close

Quit

By sheer coincidence !y =• 12• First Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Imperative Commands: Introducing Reals


Correctness, . . .
Compound Data

Basic Higher Order Functions . . .


Imperative . . .
A large Example: . . .

A Command is an ML expression that Lecture-wise Index to . . .

Home Page

creates a side effect and returns an Title Page

empty tuple (() : unit). JJ II

J I
Assignment
Page 559 of 644

print Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative Commands: Computing: The . . .

Compound Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

Any complex ML expression or func- Higher Order Functions . . .


Imperative . . .

tion definition whose type is of the A large Example: . . .


Lecture-wise Index to . . .

form α → unit is a compound com- Home Page

mand. Title Page

JJ II

• Predefined ML compound com- J I

mands Page 560 of 644

Go Back

• Could be user-defined. After all, ev- Full Screen

erything is a value! Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Predefined Compound
Commands Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

branching if e then c1 else c0. Compound Data


Higher Order Functions . . .
Imperative . . .

cases case e of p1 ⇒ c1| · · · |pn ⇒ cn A large Example: . . .


Lecture-wise Index to . . .

Sequencing (c1; c2; . . . ; cn). Sequenc- Home Page

ing is associative Title Page

JJ II

looping while e do c1 is defined recur- J I

sively as Page 561 of 644

Go Back

if e then (c1; while e do c1) else () Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7.3. Arrays
Computing: The . . .
1. Why Imperative
Algorithms: Design & . . .
2. Arrays Introducing Reals
Correctness, . . .
3. Indexing Arrays
Compound Data
4. Indexing Arrays Higher Order Functions . . .
Imperative . . .
5. Indexing Arrays
A large Example: . . .

6. Physical Addressing Lecture-wise Index to . . .

7. Arrays Home Page

8. 2D Arrays
Title Page
9. 2D Arrays: Indexing
JJ II
10. Ordering of indices
11. Arrays vs. Lists J I

12. Arrays: Physical Page 562 of 644

13. Lists: Physical


Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Why Imperative Computing: The . . .
Algorithms: Design & . . .

• Historical reasons: Early machine Introducing Reals


Correctness, . . .

instruction set. Compound Data


Higher Order Functions . . .
Imperative . . .

• Programming evolved from the ma- A large Example: . . .


Lecture-wise Index to . . .

chine architecture. Home Page

• Legacy software: Title Page

JJ II

– numerical packages J I

– operating systems Page 563 of 644

Go Back

• Are there any real benefits of imper- Full Screen

ative programming? Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays
An array of length n is a contiguous
Computing: The . . .

sequence of n memory cells Algorithms: Design & . . .


Introducing Reals
Correctness, . . .

C0, C1, . . . , Cn−1 Compound Data


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page
C0 Ci
JJ II

Cn−1 J I

Page 564 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Indexing Arrays
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

For any array Lecture-wise Index to . . .

Home Page

• i, 0 ≤ i < n is the index of cell Ci. Title Page

• Ci is at a distance of i cells away JJ II

J I
from C0 Page 565 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Indexing Arrays Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

C0 Ci
A large Example: . . .
Lecture-wise Index to . . .

Cn−1 Home Page

Title Page

JJ II

J I

Page 566 of 644

Go Back

Full Screen

a0 a n−1 ai Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Indexing Arrays Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .

• The start address of the array and A large Example: . . .


Lecture-wise Index to . . .

the address of C0 are the same (say Home Page

a0) Title Page

JJ II
• The address ai of cell Ci is J I
ai = a0 + i Page 567 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Physical Addressing
If each element occupies s physical Computing: The . . .
Algorithms: Design & . . .

memory locations, then Introducing Reals


Correctness, . . .

ai = a0 + i × s Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 568 of 644

Go Back

Full Screen

Close

a0 a n−1 ai Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

A 2-dimensional array of Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• r rows numbered 0 to r − 1 Lecture-wise Index to . . .

Home Page

• each row containing c elements Title Page

numbered 0 to c − 1 JJ II

J I
is also a contiguous sequence of rc
Page 569 of 644

memory cells Go Back

Full Screen

C0,0, C0,1, . . . , C0,c−1, C1,0, . . . , Cr−1,c−1 Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2D Arrays
A 2 dimensional-array is represented Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

as an array of length r × c, where Correctness, . . .


Compound Data
Higher Order Functions . . .

• a00 is the start address of the array, Imperative . . .


A large Example: . . .

and Lecture-wise Index to . . .

Home Page

• the address of the (i, j)-th cell is Title Page

given by JJ II

aij = a00 + (c × i + j) J I

Page 570 of 644

• the physical address of the (i, j)-th Go Back

cell is given by Full Screen

aij = a00 + (c × i + j) × s Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2D Arrays: Indexing Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

• The index (i, j) of a 2D array may


Correctness, . . .
Compound Data
Higher Order Functions . . .

be thought of as being similar to a Imperative . . .


A large Example: . . .

2-digit number in base c Lecture-wise Index to . . .

Home Page

• The successor of index (i, j) is the Title Page

successor of a number in base c i.e. JJ II

J I

(i + 1, 0) if j = n − 1 Page 571 of 644

succ(i, j) =
(i, j + 1) else Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Ordering of indices Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .
There is a natural “<” ordering on in- A large Example: . . .
Lecture-wise Index to . . .

dices given by Home Page

(i, j) < (k, l) ⇐⇒ Title Page

JJ II
(i < k) or J I
(i = k and j < l) Page 572 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Arrays vs. Lists Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Lists Arrays Home Page

Unbounded lengths Fixed length Title Page

Insertions possible Very complex JJ II

Indirect access Direct access J I

Page 573 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays: Physical
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 574 of 644

Go Back

Full Screen

a0 a n−1 ai
Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Physical
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 575 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8. A large Example: Tautology Checking
Computing: The . . .
Algorithms: Design & . . .
8.1. Large Example: Tautology Checking
Introducing Reals
1. Logical Arguments Correctness, . . .
Compound Data
2. Saintly and Rich
Higher Order Functions . . .
3. About Cats Imperative . . .
A large Example: . . .
4. About God
Lecture-wise Index to . . .
5. Russell’s Argument
Home Page
6. Russell’s Argument
7. Russell’s Argument Title Page

8. Russell’s Argument JJ II
9. Propositions
J I
10. Compound Propositions
Page 576 of 644
11. Valuations
12. Valuations Go Back

13. Tautology Full Screen

14. Properties
Close
15. Negation Normal Form
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Literals & Clauses
17. Conjunctive Normal Form
18. Validity
Computing: The . . .

19. Logical Validity Algorithms: Design & . . .


Introducing Reals
20. Validity & Tautologies Correctness, . . .

21. Problem Compound Data


Higher Order Functions . . .
22. Tautology Checking Imperative . . .

23. Falsifying A large Example: . . .


Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 577 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Logical Arguments
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Examples. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

• Saintly and Rich Home Page

• About cats
Title Page

JJ II

• About God J I

• Russell’s argument
Page 578 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

Saintly and Rich


Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

hy1 The landed are rich. Home Page

Title Page
hy2 One cannot be both saintly and rich.
JJ II

conc The landed are not saintly J I

Page 579 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

About Cats Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
hy1 Tame cats are non-violent and vege- Imperative . . .
A large Example: . . .

tarian. Lecture-wise Index to . . .

Home Page

hy2 Non-violent cats would not kill mice. Title Page

hy3 Vegetarian cats are bottle-fed. JJ II

J I
hy4 Cats eat meat. Page 580 of 644

conc Cats are not tame. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
About God
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

hy1 God is omniscient and omnipotent. Compound Data


Higher Order Functions . . .
Imperative . . .

hy2 An omniscient being would know A large Example: . . .


Lecture-wise Index to . . .

there is evil. Home Page

Title Page

hy3 An omnipotent being would prevent JJ II

evil. J I

hy4 There is evil. Page 581 of 644

Go Back

conc There is no God Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .
Algorithms: Design & . . .
hy1 If we can directly know that God ex- Introducing Reals
Correctness, . . .

ists, then we can know God exists by Compound Data


Higher Order Functions . . .

experience. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

hy2 If we can indirectly know that God ex- Home Page

ists, then we can know God exists by Title Page

logical inference from experience. JJ II

J I
hy3 If we can know that God exists, then Page 582 of 644

we can directly know that God exists, Go Back

or we can indirectly know that God ex- Full Screen

ists. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .

hy4 If we cannot know God empirically, Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
then we cannot know God by experi- Compound Data
Higher Order Functions . . .

ence and we cannot know God by logi- Imperative . . .


A large Example: . . .

cal inference from experience. Lecture-wise Index to . . .

Home Page

hy5 If we can know God empirically, then Title Page

“God exists” is a scientific hypothesis JJ II

and is empirically justifiable. J I

Page 583 of 644

hy6 “God exists” is not empirically justi- Go Back

fiable. Full Screen

conc We cannot know that God exists. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .

hy1 If we can directly know that God ex- Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
ists, then we can know God exists by Compound Data
Higher Order Functions . . .

experience. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

hy2 If we can indirectly know that God ex- Home Page

ists, then we can know God exists by Title Page

logical inference from experience. JJ II

J I
hy3 If we can know that God exists, then Page 584 of 644

(we can directly know that God exists, Go Back

or we can indirectly know that God ex- Full Screen

ists). Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .

hy4 If we cannot know God empirically, Algorithms: Design & . . .


Introducing Reals

then (we cannot know God by experi- Correctness, . . .


Compound Data
Higher Order Functions . . .

ence and we cannot know God by log- Imperative . . .


A large Example: . . .

ical inference from experience.) Lecture-wise Index to . . .

Home Page

hy5 If we can know God empirically, then Title Page

(“God exists” is a scientific hypothesis JJ II

and is empirically justifiable.) J I

Page 585 of 644

hy6 “God exists” is not empirically justi- Go Back

fiable. Full Screen

conc We cannot know that God exists. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Propositions Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

A proposition is a sentence to which a Higher Order Functions . . .


Imperative . . .

truth value may be assigned. A large Example: . . .


Lecture-wise Index to . . .

Home Page

In any real or imaginary world of facts Title Page

a proposition has a truth value, true or JJ II

false. J I

An atom is a simple proposition that Page 586 of 644

has no propositions as components. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compound
Propositions Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound propositions may be Compound Data
Higher Order Functions . . .

formed from atoms by using the fol- Imperative . . .


A large Example: . . .

lowing operators/constructors. Lecture-wise Index to . . .

Home Page

operator symbol Title Page

JJ II
not ¬ J I

and ∧ Page 587 of 644

or ∨ Go Back

if. . . then. . . ⇒ Full Screen

equivalent ⇐⇒ Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Valuations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

Given truth values to individual atoms Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .
the truth values of compound proposi- Home Page

tions are evaluated as follows: Title Page

p ¬p JJ II

true false J I

false true Page 588 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Valuations Compound Data


Higher Order Functions . . .
Imperative . . .

p q p∧q p∨q p⇒q p ⇐⇒ q A large Example: . . .


Lecture-wise Index to . . .

true true true true true true Home Page

true false false true false false Title Page

JJ II
false true false true true false
J I
false false false false true true Page 589 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

A (compound) proposition is a tautol- Compound Data


Higher Order Functions . . .

ogy if it is true regardless of what truth


Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

values are assigned to its atoms. Home Page

Examples. Title Page

JJ II
• p∨¬p J I

• (p∧q)⇒p Page 590 of 644

Go Back

• (p∧¬p)⇒q Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Properties
• Every proposition may be ex- Computing: The . . .
Algorithms: Design & . . .

pressed in a logically equivalent Introducing Reals


Correctness, . . .

form using only the operators ¬, ∧


Compound Data
Higher Order Functions . . .
Imperative . . .

and ∨ A large Example: . . .


Lecture-wise Index to . . .

(p ⇐⇒ q) = (p⇒q)∧(q⇒p) Home Page

(p⇒q) = (¬p∨q) Title Page

JJ II

• De Morgan’s laws may be applied J I

to push ¬ inward
Page 591 of 644

Go Back

¬(p∧q) = ¬p∨¬q Full Screen

¬(p∨q) = ¬p∧¬q Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Negation Normal Form Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Double negations may be removed Higher Order Functions . . .


Imperative . . .

since A large Example: . . .


Lecture-wise Index to . . .

¬¬p = p Home Page

Title Page

• Every proposition may be ex- JJ II

pressed in a form containing only ∧ J I

and ∨ with ¬ appearing only in front Page 592 of 644

of atoms. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Literals & Clauses Correctness, . . .


Compound Data
Higher Order Functions . . .
Imperative . . .

• A literal is either an atom or ¬ ap- A large Example: . . .


Lecture-wise Index to . . .

plied to an atom Home Page

• ∨ is commutative and associative Title Page

Wm JJ II

• A clause is of the form j=1lj , J I

where each lj is a literal. Page 593 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Conjunctive Normal
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Form
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .

• ∨ may be distributed over ∧ A large Example: . . .


Lecture-wise Index to . . .

p∨(q∧r) = (p∨q)∧(p∨r) Home Page

Title Page

• ∧ is commutative and associative. JJ II

• Every proposition V may be ex- J I

pressed in the form ni=1qi, where Page 594 of 644

Go Back

each qi is a clause. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Validity
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .

• A logical argument consists of a Imperative . . .


A large Example: . . .

number of hypotheses and a single


Lecture-wise Index to . . .

Home Page

conclusion ([h1, . . . , hn]|c) Title Page

• A logical argument is valid if the JJ II

conclusion logically follows from the J I

Page 595 of 644

hypotheses. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Logical Validity Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data
The conclusion logically follows Higher Order Functions . . .
Imperative . . .

from the given hypotheses if for A large Example: . . .


Lecture-wise Index to . . .

any truth assignment to the atoms, Home Page

either some hypothesis hi is false Title Page

JJ II

or whenever every one of the hypothe- J I

ses is true the conclusion is also Page 596 of 644

Go Back

true Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Validity & Tautologies Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

• A tautology is a valid argument Compound Data


Higher Order Functions . . .

in which there is a conclusion Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

without any hypothesis. Home Page

• A logical argument [h1, . . . , hn]|c, is Title Page

valid if and only if JJ II

J I

(h1∧ . . . ∧hn)⇒c Page 597 of 644

Go Back

is a tautology Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Problem Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .

Given an argument [h1, . . . , hn]|c, Imperative . . .


A large Example: . . .
Lecture-wise Index to . . .

• determine whether (h1∧ . . . ∧hn)⇒c Home Page

is a tautology, and Title Page

JJ II
• If it is not a tautology, to deter-
J I
mine what truth assignments to the Page 598 of 644

atoms make it false. Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology Checking Computing: The . . .
Algorithms: Design & . . .
Vn
A proposition in CNF ( i=1pi) Introducing Reals
Correctness, . . .
Compound Data

• is a tautology if and only if every Higher Order Functions . . .


Imperative . . .

proposition pi , 1 ≤ i ≤ m, is a tau-
A large Example: . . .
Lecture-wise Index to . . .

tology. Home Page

Title Page

• otherwise at least one clause pi JJ II

must be false J I
Wm
• Clause pi = j=1lij is false
Page 599 of 644

Go Back

if and only if every literal lij , 1 ≤ j ≤ Full Screen

m is false Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Falsifying Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Vn
For a proposition in CNF ( i=1pi) that Correctness, . . .
Compound Data

is not a tautology Higher Order Functions . . .


Imperative . . .
A large Example: . . .
Wm
• A clause pi = j=1lij is false Lecture-wise Index to . . .

Home Page

• A truth assignment that falsifies the Title Page

argument JJ II

J I
– sets the atoms that occur nega- Page 600 of 644

tively in pi to true, Go Back

– sets every other atom to false Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8.2. Tautology Checking Contd.
Computing: The . . .
1. Tautology Checking
Algorithms: Design & . . .
2. Normal Forms Introducing Reals
Correctness, . . .
3. Top-down Development
Compound Data
4. The Signature Higher Order Functions . . .
Imperative . . .
5. The Core subproblem
A large Example: . . .

6. The datatype Lecture-wise Index to . . .

7. Convert to CNF Home Page

8. Rewrite into NNF


Title Page
9. ⇐⇒ and ⇒ Elimination
JJ II
10. Push ¬ inward
11. Push ¬ inward J I

12. conj of disj Page 601 of 644

13. Push ∨ inward


Go Back
14. Tautology & Falsification
Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Tautology Checking Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Logical arguments Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Propositional forms Lecture-wise Index to . . .

Home Page

• Propositions Title Page

• Compound Propositions JJ II

J I
• Truth table Page 602 of 644

• Tautologies Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

Normal Forms
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data

• Properties Higher Order Functions . . .


Imperative . . .
A large Example: . . .

• Negation Normal Form Lecture-wise Index to . . .

Home Page

• Conjunctive Normal Forms Title Page

• Valid Propositional Arguments as JJ II

J I
tautologies Page 603 of 644

• The problem Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Top-down
Development Computing: The . . .
Algorithms: Design & . . .

• Transform the argument into a Introducing Reals


Correctness, . . .

single proposition. Compound Data


Higher Order Functions . . .
Imperative . . .

• Transfom the single proposition into A large Example: . . .


Lecture-wise Index to . . .

one in CNF Home Page

Title Page

JJ II

• Check whether every clause is a J I

tautology
Page 604 of 644

Go Back

• If any clause is not a tautology, find Full Screen

the truth assignment(s) that make it Close

false Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Signature
signature PropLogic = Computing: The . . .
Algorithms: Design & . . .

sig datatype Prop = ?? Introducing Reals


Correctness, . . .
Compound Data

type Argument = Higher Order Functions . . .


Imperative . . .

Prop list * Prop A large Example: . . .


Lecture-wise Index to . . .

val falsifyArg : Home Page

Argument -> Prop list list Title Page

val Valid: JJ II

J I
Argument -> bool *
Page 605 of 644

Prop list list Go Back

... Full Screen

end; Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .

The Core subproblem Algorithms: Design & . . .


Introducing Reals
Correctness, . . .
Compound Data

• Representing propositions Higher Order Functions . . .


Imperative . . .

• Transformation of propositions into


A large Example: . . .
Lecture-wise Index to . . .

CNF Home Page

Title Page

– Transform into Negation Normal JJ II

Form (NNF) J I

– Transform NNF into Conjunctive Page 606 of 644

Normal Form (CNF) Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The datatype Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
datatype Prop = Compound Data
Higher Order Functions . . .

ATOM of string | Imperative . . .


A large Example: . . .

NOT of Prop | Lecture-wise Index to . . .

Home Page
AND of Prop * Prop |
Title Page

OR of Prop * Prop | JJ II

IMP of Prop * Prop | J I

EQL of Prop * Prop Page 607 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Convert to CNF Computing: The . . .
Algorithms: Design & . . .

Convert a given proposition into CNF Introducing Reals


Correctness, . . .
Compound Data
fun cnf (P) = Higher Order Functions . . .
Imperative . . .

conj_of_disj ( A large Example: . . .


Lecture-wise Index to . . .

nnf (rewrite (P))); Home Page

Title Page

where JJ II

J I

• rewrite eliminates ⇐⇒ and ⇒ Page 608 of 644

• nnf converts into NNF Go Back

Full Screen

• conj of disj converts into CNF Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Rewrite into NNF


Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .

• Eliminate ⇐⇒ and then ⇒ Lecture-wise Index to . . .

Home Page

• Push ¬ inward using De Title Page

Morgan’s laws and eliminate JJ II

double negations. J I

Page 609 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
⇐⇒ and ⇒ Elimination Computing: The . . .
Algorithms: Design & . . .

fun rewrite (ATOM a) = ATOM a Introducing Reals


Correctness, . . .
Compound Data
| rewrite (IMP (P, Q)) = Higher Order Functions . . .
Imperative . . .

OR (NOT (rewrite(P)), A large Example: . . .


Lecture-wise Index to . . .

rewrite(Q)) Home Page

| rewrite (EQL (P, Q)) = Title Page

rewrite (AND (IMP(P, Q), JJ II

J I
IMP (Q, P)))
Page 610 of 644

| ... Go Back

Proposition made up of only ¬, ∧ and Full Screen

∨. Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .

Push ¬ inward Introducing Reals


Correctness, . . .
Compound Data
Higher Order Functions . . .
fun nnf (ATOM a) = Imperative . . .
A large Example: . . .

ATOM a Lecture-wise Index to . . .

| nnf (NOT (ATOM a)) = Home Page

Title Page
NOT (ATOM a) JJ II
| nnf (NOT (NOT (P))) = J I

nnf (P) Page 611 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Push ¬ inward Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

| nnf (NOT (AND (P, Q))) = Correctness, . . .


Compound Data
Higher Order Functions . . .
nnf (OR (NOT (P), Imperative . . .
A large Example: . . .

NOT (Q))) Lecture-wise Index to . . .

| nnf (NOT (OR (P, Q))) = Home Page

Title Page
nnf (AND (NOT (P),
JJ II
NOT (Q))) J I

| ... Page 612 of 644

Proposition made up of only ∧ and ∨ Go Back

applied to positive or negative literals. Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
conj of disj Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
fun conj_of_disj (AND (P, Q)) = Compound Data
Higher Order Functions . . .

AND (conj_of_disj (P), Imperative . . .


A large Example: . . .

conj_of_disj (Q)) Lecture-wise Index to . . .

Home Page
| conj_of_disj (OR (P, Q)) =
Title Page

distOR (conj_of_disj (P), JJ II

conj_of_disj (Q)) J I

| conj_of_disj (P) = P Page 613 of 644

Go Back

where distOR is Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Push ∨ inward Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .

Use distributivity of ∨ over ∧ Compound Data


Higher Order Functions . . .
Imperative . . .
fun distOR (P, AND (Q, R)) = A large Example: . . .
Lecture-wise Index to . . .

AND (distOR (P, Q), Home Page

distOR (P, R)) Title Page

| distOR (AND (Q, R), P) = JJ II

AND (distOR (Q, P), J I

Page 614 of 644


distOR (R, P))
Go Back

| distOR (P, Q)= OR (P, Q) Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology &
Falsification Computing: The . . .
Algorithms: Design & . . .
Introducing Reals

Falsifying a proposition Correctness, . . .


Compound Data
Higher Order Functions . . .

• A proposition Q in CNF, not a tau- Imperative . . .


A large Example: . . .

tology if and only if at least one of Lecture-wise Index to . . .

Home Page

the clauses can be made false, by Title Page

a suitable truth assignment JJ II

• The list of atoms which are set true J I

to falsify a clause is called a falsifier.


Page 615 of 644

Go Back

• A proposition is a tautology Full Screen

if and only if there is no falsifier! Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Lecture-wise Index to Slides Computing: The . . .

Algorithms: Design & . . .


Index
Introducing Reals
Introduction to Computing (3-8) Correctness, . . .

Compound Data
1. Introduction
Higher Order Functions . . .
2. Computing tools Imperative . . .
3. Ruler and Compass A large Example: . . .

4. Computing and Computers Lecture-wise Index to . . .

5. Primitives Home Page


6. Algorithm
Title Page
Our Computing Tool (9-33)
JJ II
1. The Digital Computer: Our Computing Tool
J I
2. Algorithms
3. Programming Language Page 616 of 644

4. Programs and Languages


Go Back
5. Programs
Full Screen
6. Programming
7. Computing Models Close

8. Primitives
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Primitive expressions
10. Methods of combination
11. Methods of abstraction
Computing: The . . .
12. The Functional Model Algorithms: Design & . . .

13. Mathematical Notation 1: Factorial Introducing Reals

14. Mathematical Notation 2: Factorial Correctness, . . .

Compound Data
15. Mathematical Notation 3: Factorial
Higher Order Functions . . .
16. A Functional Program: Factorial
Imperative . . .
17. A Computation: Factorial A large Example: . . .

18. A Computation: Factorial Lecture-wise Index to . . .

19. A Computation: Factorial Home Page


20. A Computation: Factorial
Title Page
21. A Computation: Factorial
22. A Computation: Factorial JJ II
23. A Computation: Factorial J I
24. Standard ML
Page 617 of 644
25. SML: Important Features
Go Back
Primitives: Integer & Real (34-60)
1. Algorithms & Programs Full Screen

2. SML: Primitive Integer Operations 1 Close

3. SML: Primitive Integer Operations 1


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. SML: Primitive Integer Operations 1
5. SML: Primitive Integer Operations 1
6. SML: Primitive Integer Operations 1
Computing: The . . .
7. SML: Primitive Integer Operations 1 Algorithms: Design & . . .
8. SML: Primitive Integer Operations 2 Introducing Reals

9. SML: Primitive Integer Operations 2 Correctness, . . .

10. SML: Primitive Integer Operations 2 Compound Data

Higher Order Functions . . .


11. SML: Primitive Integer Operations 2
Imperative . . .
12. SML: Primitive Integer Operations 2
A large Example: . . .
13. SML: Primitive Integer Operations 3 Lecture-wise Index to . . .
14. SML: Primitive Integer Operations 3
Home Page
15. SML: Primitive Integer Operations 3
16. SML: Primitive Integer Operations 3 Title Page

17. SML: Primitive Integer Operations 3 JJ II


18. Quotient & Remainder
J I
19. SML: Primitive Real Operations 1
20. SML: Primitive Real Operations 1 Page 618 of 644

21. SML: Primitive Real Operations 1 Go Back


22. SML: Primitive Real Operations 2
Full Screen
23. SML: Primitive Real Operations 3
24. SML: Primitive Real Operations 4 Close

25. SML: Precision


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
26. Fibonacci Numbers
27. Euclidean Algorithm
Technical Completeness & Algorithms (61-92) Computing: The . . .

Algorithms: Design & . . .


1. Recapitulation: Integers & Real
Introducing Reals
2. Recap: Integer Operations
Correctness, . . .
3. Recapitulation: Real Operations Compound Data
4. Recapitulation: Simple Algorithms Higher Order Functions . . .

5. More Algorithms Imperative . . .

A large Example: . . .
6. Powering: Math
Lecture-wise Index to . . .
7. Powering: SML
8. Technical completeness Home Page

9. What SML says Title Page

10. Technical completeness


JJ II
11. What SML says ... contd
12. Powering: Math 1 J I

13. Powering: SML 1 Page 619 of 644

14. Technical Completness


Go Back
15. What SML says
16. Powering: Integer Version Full Screen

17. Exceptions: A new primitive Close

18. Integer Power: SML


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Integer Square Root 1
20. Integer Square Root 2
21. An analysis
Computing: The . . .
22. Algorithmic idea Algorithms: Design & . . .

23. Algorithm: isqrt Introducing Reals

24. Algorithm: shrink Correctness, . . .

Compound Data
25. SML: shrink
Higher Order Functions . . .
26. SML: intsqrt
Imperative . . .
27. Run it! A large Example: . . .

28. SML: Reorganizing Code Lecture-wise Index to . . .

29. Intsqrt: Reorganized Home Page


30. shrink: Another algorithm
Title Page
31. Shrink2: SML
32. Shrink2: SML ... contd JJ II

Algorithm Refinement (93-122) J I

1. Recap: More Algorithms Page 620 of 644

2. Recap: Power
Go Back
3. Recap: Technical completeness
4. Recap: More Algorithms Full Screen

5. Intsqrt: Reorganized Close

6. Intsqrt: Reorganized
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Some More Algorithms
8. Combinations: Math
9. Combinations: Details
Computing: The . . .
10. Combinations: SML Algorithms: Design & . . .
11. Perfect Numbers Introducing Reals

12. Refinement Correctness, . . .

13. Perfect Numbers: SML Compound Data


Pu Higher Order Functions . . .
14. l if divisor(k)
Imperative . . .
15. SML: sum divisors
A large Example: . . .
16. if divisor and ifdivisor Lecture-wise Index to . . .
17. SML: Assembly 1
Home Page
18. SML: Assembly 2
19. Perfect Numbers . . . contd. Title Page

20. Perfect Numbers . . . contd. JJ II


21. SML: Assembly 3
J I
22. Perfect Numbers: Run
23. Perfect Numbers: Run Page 621 of 644

24. SML: Code variations Go Back


25. SML: Code variations
Full Screen
26. SML: Code variations
27. Summation: Generalizations Close

28. Algorithmic Improvements:


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
29. Algorithmic Variations
30. Algorithmic Variations
Variations: Algorithms & Code (123-149) Computing: The . . .

Algorithms: Design & . . .


1. Recap
Introducing Reals
2. Recap: Combinations
Correctness, . . .
3. Combinations 1 Compound Data
4. Combinations 2 Higher Order Functions . . .

5. Combinations 3 Imperative . . .

A large Example: . . .
6. Perfect 2
Lecture-wise Index to . . .
7. Power 2
8. A Faster Power Home Page

9. Power2: Complete Title Page

10. Power2: SML


JJ II
11. Power2: SML
12. Computation: Issues J I

13. General Correctness Page 622 of 644

14. Code: Justification


Go Back
15. Recall
16. Features: Definition before Use Full Screen

17. Run ifdivisor Close

18. Diagnosis: Features of programs


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Back to Math
20. Incorrectness
21. ifdivisor3
Computing: The . . .
22. Run it! Algorithms: Design & . . .

23. Try it! Introducing Reals

24. Hey! Wait a minute! Correctness, . . .

Compound Data
25. The n’s
Higher Order Functions . . .
26. Scope
Imperative . . .
27. Scope Rules A large Example: . . .

Names, Scopes & Recursion (150-181) Lecture-wise Index to . . .

1. Disjoint Scopes Home Page

2. Nested Scopes Title Page

3. Overlapping Scopes
JJ II
4. Spannning
5. Scope & Names J I

6. Names & References Page 623 of 644

7. Names & References


Go Back
8. Names & References
9. Names & References Full Screen

10. Names & References Close

11. Names & References


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
12. Names & References
13. Names & References
14. Names & References
Computing: The . . .
15. Names & References Algorithms: Design & . . .

16. Definition of Names Introducing Reals

17. Use of Names Correctness, . . .

Compound Data
18. local...in...end
Higher Order Functions . . .
19. local...in...end
Imperative . . .
20. local...in...end A large Example: . . .

21. local...in...end Lecture-wise Index to . . .

22. Scope & local Home Page


23. Computations: Simple
Title Page
24. Simple computations
25. Computations: Composition JJ II
26. Composition: Alternative J I
27. Compositions: Compare
Page 624 of 644
28. Compositions: Compare
29. Computations: Composition Go Back

30. Recursion Full Screen


31. Recursion: Left
Close
32. Recursion: Right
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Floating Point (182-201)
1. So Far-1: Computing
2. So Far-2: Algorithms & Programs Computing: The . . .

3. So far-3: Top-down Design Algorithms: Design & . . .

4. So Far-4: Algorithms to Programs Introducing Reals

Correctness, . . .
5. So far-5: Caveats
Compound Data
6. So Far-6: Algorithmic Variations Higher Order Functions . . .
7. So Far-7: Computations Imperative . . .

8. Floating Point A large Example: . . .

9. Real Operations Lecture-wise Index to . . .

10. Real Arithmetic Home Page

11. Numerical Methods


Title Page
12. Errors
JJ II
13. Errors
14. Infinite Series J I
15. Truncation Errors
Page 625 of 644
16. Equation Solving
Go Back
17. Root Finding-1
18. Root Finding-2 Full Screen

19. Root Finding-3


Close
20. Root Finding-4
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding, Composition and Recursion (202-229)
1. Root Finding: Newton’s Method
2. Root Finding: Newton’s Method Computing: The . . .

3. Root Finding: Newton’s Method Algorithms: Design & . . .

4. Root Finding: Newton’s Method Introducing Reals

Correctness, . . .
5. Root Finding: Newton’s Method
Compound Data
6. Root Finding: Newton’s Method Higher Order Functions . . .
7. Newton’s Method: Basis Imperative . . .

8. Newton’s Method: Basis A large Example: . . .

9. Newton’ Method: Algorithm Lecture-wise Index to . . .

10. What can go wrong!-1 Home Page

11. What can go wrong!-2


Title Page
12. What can go wrong!-2
JJ II
13. What can go wrong!-3
14. What can go wrong!-4 J I
15. Real Computations & Induction: 1
Page 626 of 644
16. Real Computations & Induction: 2
Go Back
17. What’s it good for? 1
18. What’s it good for? 2 Full Screen

19. newton: Computation


Close
20. Generalized Composition
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
21. Two Computations of h(1)
22. Two Computations of h(−1)
23. Recursive Computations
Computing: The . . .
24. Recursion: Left Algorithms: Design & . . .

25. Recursion: Right Introducing Reals

26. Recursion: Nonlinear Correctness, . . .

Compound Data
27. Some Practical Questions
Higher Order Functions . . .
28. Some Practical Questions
Imperative . . .

Termination and Space Complexity (230-266) A large Example: . . .

Lecture-wise Index to . . .
1. Recursion Revisited
2. Linear Recursion: Waxing Home Page

3. Recursion: Waning Title Page

4. Nonlinear Recursions
JJ II
5. Fibonacci: contd
6. Recursion: Waxing & Waning J I

7. Unfolding Recursion Page 627 of 644

8. Non-termination
Go Back
9. Termination
10. Proofs of termination Full Screen

11. Proofs of termination: Induction Close

12. Proof of termination: Factorial


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
13. Proof of termination: Factorial
14. Fibonacci: Termination
15. GCD computations
Computing: The . . .
16. Well-foundedness: GCD Algorithms: Design & . . .
17. Well-foundedness Introducing Reals

18. Induction is Well-founded Correctness, . . .

19. Induction is Well-founded Compound Data

Higher Order Functions . . .


20. Where it doesn’t work
Imperative . . .
21. Well-foundedness is inductive
A large Example: . . .
22. Well-foundedness is inductive Lecture-wise Index to . . .
23. GCD: Well-foundedness
Home Page
24. Newton: Well-foundedness
25. Newton: Well-foundedness Title Page

26. Example: Zero JJ II


27. Questions
J I
28. The Collatz Problem
29. Questions Page 628 of 644

30. Space Complexity Go Back


31. Newton & Euclid: Absolute
Full Screen
32. Newton & Euclid: Relative
33. Deriving space requirements Close

34. GCD: Space


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
35. Factorial: Space
36. Fibonacci: Space
37. Fibonacci: Space
Computing: The . . .

Efficiency Measures and Speed-ups (267-295) Algorithms: Design & . . .

Introducing Reals
1. Recapitulation
Correctness, . . .
2. Recapitulation Compound Data
3. Time & Space Complexity Higher Order Functions . . .

4. isqrt: Space Imperative . . .

A large Example: . . .
5. Time Complexity
Lecture-wise Index to . . .
6. isqrt: Time Complexity
7. isqrt2: Time Home Page

8. shrink vs. shrink2: Times Title Page

9. Factorial: Time Complexity


JJ II
10. Fibonacci: Time Complexity
11. Comparative Complexity J I

12. Comparisons Page 629 of 644

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

16. Speeding Up: 1 Close

17. Speeding Up: 2


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Factoring out calculations
19. Tail Recursion: 1
20. Tail Recursion: 2
Computing: The . . .
21. Factorial: Tail Recursion Algorithms: Design & . . .

22. Factorial: Tail Recursion Introducing Reals

23. A Computation Correctness, . . .

Compound Data
24. Factorial: Issues
Higher Order Functions . . .
25. Fibonacci: Tail Recursion
Imperative . . .
26. Fibonacci: Tail Recursion A large Example: . . .

27. fibTR: SML Lecture-wise Index to . . .

28. State in Tail Recursion Home Page


29. Invariance
Title Page
Invariance & Correctness (296-319)
JJ II
1. Recap
2. Recursion Transformation J I

3. Tail Recursion: Examples Page 630 of 644

4. Comparisons
Go Back
5. Transformation Issues
6. Correctness Issues 1 Full Screen

7. Correctness Issues 2 Close

8. Correctness Theorem
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Invariants & Correctness 1
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr
Computing: The . . .
12. Invariance: Example Algorithms: Design & . . .

13. Invariance: Example Introducing Reals

14. Proof Correctness, . . .

Compound Data
15. Invariance Lemma: f ib iter
Higher Order Functions . . .
16. Proof
Imperative . . .
17. Correctness: Fibonacci A large Example: . . .

18. Variants & Invariants Lecture-wise Index to . . .

19. Variants & Invariants Home Page


20. More Invariants
Title Page
21. Fast Powering 1
22. Fast Powering 2 JJ II
23. Root Finding: Bisection J I
24. Advantage Bisection
Page 631 of 644
Tuples, Lists & the Generation of Primes (320-341)
Go Back
1. Recap: Tail Recursion
2. Examples: Invariants Full Screen

3. Tuples Close

4. Lists
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5. New Lists
6. List Operations
7. List Operations: cons
Computing: The . . .
8. Generating Primes upto n Algorithms: Design & . . .

9. More Properties Introducing Reals

10. Composites Correctness, . . .

Compound Data
11. Odd Primes
Higher Order Functions . . .
12. primesU pto(n)
Imperative . . .
13. generateF rom(P, m, n, k) A large Example: . . .

14. generateF rom Lecture-wise Index to . . .

15. primeW RT (m, P ) Home Page


16. primeW RT (m, P )
Title Page
17. primeW RT
18. Density of Primes JJ II
19. The Prime Number Theorem J I
20. The Prime Number Theorem
Page 632 of 644
21. Complexity
22. Diagnosis Go Back

Compound Data & Lists (342-367) Full Screen

1. Compound Data Close

2. Recap: Tuples
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3. Tuple: Formation
4. Tuples: Selection
5. Tuples: Equality
Computing: The . . .
6. Tuples: Equality errors Algorithms: Design & . . .
7. Lists: Recap Introducing Reals

8. Lists: Append Correctness, . . .

9. cons vs. @ Compound Data

Higher Order Functions . . .


10. Lists of Functions
Imperative . . .
11. Lists of Functions
A large Example: . . .
12. Arithmetic Sequences Lecture-wise Index to . . .
13. Tail Recursion
Home Page
14. Tail Recursion Invariant
15. Tail Recursion Title Page

16. Another Tail Recursion: AS3 JJ II


17. Another Tail Recursion: AS3 iter
J I
18. AS3: Complexity
19. Generating Primes: 2 Page 633 of 644

20. primes2U pto(n) Go Back


21. generate2F rom(P, m, n, k)
Full Screen
22. generate2F rom
23. prime2W RT (m, P ) Close

24. prime2W RT
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
25. primes2: Complexity
26. primes2: Diagnosis
Compound Data & List Algorithms (368-394) Computing: The . . .

Algorithms: Design & . . .


1. Compound Data: Summary
Introducing Reals
2. Records: Constructors
Correctness, . . .
3. Records: Example 1 Compound Data
4. Records: Example 2 Higher Order Functions . . .

5. Records: Destructors Imperative . . .

A large Example: . . .
6. Records: Equality
Lecture-wise Index to . . .
7. Tuples & Records
8. Back to Lists Home Page

9. Lists: Correctness Title Page

10. Lists: Case Analysis


JJ II
11. Lists: Correctness by Cases
12. List-functions: length J I

13. List Functions: search Page 634 of 644

14. List Functions: search2


Go Back
15. List Functions: ordered
16. List Functions:insert Full Screen

17. List Functions: reverse Close

18. List Functions: reverse2


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. List Functions:merge
20. List Functions:merge
21. List Functions:merge contd.
Computing: The . . .
22. ML: merge Algorithms: Design & . . .

23. Sorting by Insertion Introducing Reals

24. Sorting by Merging Correctness, . . .

Compound Data
25. Sorting by Merging
Higher Order Functions . . .
26. Functions as Data
Imperative . . .
27. Higher Order Functions A large Example: . . .

Higher Order Functions (395-419) Lecture-wise Index to . . .

1. Summary: Compound Data Home Page

2. List: Examples Title Page

3. Lists: Sorting
JJ II
4. Higher Order Functions
5. An Example J I

6. Currying Page 635 of 644

7. Currying: Contd
Go Back
8. Generalization
9. Generalization: 2 Full Screen

10. Applying a list Close

11. Trying it out


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
12. Associativity
13. Apply to a list
14. Sequences
Computing: The . . .
15. Further Generalization Algorithms: Design & . . .

16. Further Generalization Introducing Reals

17. Sequences Correctness, . . .

Compound Data
18. Efficient Generalization
Higher Order Functions . . .
19. Sequences: 2
Imperative . . .
20. More Generalizations A large Example: . . .

21. More Summations Lecture-wise Index to . . .

22. Or Maybe . . . Products Home Page


N
23. Or Some Other
N Title Page
24. Other
25. Examples of ⊗, e JJ II

Structured Data (420-451) J I

1. Transpose of a Matrix Page 636 of 644

2. Transpose: 0
Go Back
3. Transpose: 10
4. Transpose: 01 Full Screen

5. Transpose: 20 Close

6. Transpose: 02
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Transpose: 30
8. Transpose: 03
9. trans
Computing: The . . .
10. is2DM atrix Algorithms: Design & . . .
11. User Defined Types Introducing Reals

12. Enumeration Types Correctness, . . .

13. User Defined Structural Types Compound Data

Higher Order Functions . . .


14. Functions vs. data
Imperative . . .
15. Data as 0-ary Functions
A large Example: . . .
16. Data vs. Functions Lecture-wise Index to . . .
17. Data vs. Functions: Recursion
Home Page
18. Lists
19. Constructors Title Page

20. Shapes JJ II
21. Shapes: Triangle Inequality
J I
22. Shapes: Area
23. Shapes: Area Page 637 of 644

24. ML: Try out Go Back


25. ML: Try out (contd.)
Full Screen
26. Enumeration Types
27. Recursive Data Types Close

28. Resistors: Datatype


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
29. Resistors: Equivalent
30. Resistors
31. Resistors: Example
Computing: The . . .
32. Resistors: ML session Algorithms: Design & . . .

User Defined Structured Data Types (452-472) Introducing Reals

Correctness, . . .
1. User Defined Types Compound Data
2. Resistors: Grouping Higher Order Functions . . .

3. Resistors: In Pairs Imperative . . .

A large Example: . . .
4. Resistor: Values
Lecture-wise Index to . . .
5. Resistance Expressions
6. Resistance Expressions Home Page

7. Arithmetic Expressions Title Page

8. Arithmetic Expressions: 0
JJ II
9. Arithmetic Expressions: 1
10. Arithmetic Expressions: 2 J I

11. Arithmetic Expressions: 3 Page 638 of 644

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


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
17. Binary Trees
18. Arithmetic Expressions: 0
19. Trees: Traversals
Computing: The . . .
20. Recursive Data Types: Correctness Algorithms: Design & . . .

21. Data Types: Correctness Introducing Reals

Correctness, . . .
Introducing a Memory Model (473-499)
Compound Data
1. Summary: Functional Model Higher Order Functions . . .

2. CPU & Memory: Simplified Imperative . . .

A large Example: . . .
3. Resource Management
Lecture-wise Index to . . .
4. Shell: User Interface
5. GUI: User Interface Home Page

6. Memory Model: Simplified Title Page

7. Memory
JJ II
8. The Imperative Model
9. State Changes: σ J I

10. State Page 639 of 644

11. State Changes


Go Back
12. State Changes: σ
13. State Changes: σ1 Full Screen

14. State Changes: σ2 Close

15. Languages
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. User Programs
17. Imperative Languages
18. Imperative vs Functional Variables
Computing: The . . .
19. Assignment Commands Algorithms: Design & . . .

20. Assignment Commands Introducing Reals

21. Assignment Commands Correctness, . . .

Compound Data
22. Assignment Commands
Higher Order Functions . . .
23. Assignment Commands
Imperative . . .
24. Assignment Commands: Swap A large Example: . . .

25. Swap Lecture-wise Index to . . .

26. Swap Home Page


27. Swap
Title Page
Imperative Programming: (500-524)
JJ II
1. Imperative vs Functional
2. Features of the Store J I

3. References: Experiments Page 640 of 644

4. References: Experiments
Go Back
5. References: Experiments
6. Aliases Full Screen

7. References: Experiments Close

8. References: Aliases
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. References: Experiments
10. After Garbage Collection
11. Side Effects
Computing: The . . .
12. Imperative ML Algorithms: Design & . . .

13. Imperative ML Introducing Reals

14. Imperative ML Correctness, . . .

Compound Data
15. Imperative ML
Higher Order Functions . . .
16. Nasty Surprises
Imperative . . .
17. Imperative ML A large Example: . . .

18. Imperative ML Lecture-wise Index to . . .

19. Common Errors Home Page


20. Aliasing & References
Title Page
21. Dangling References
22. New Reference JJ II
23. Imperative Commands: Basic J I
24. Imperative Commands: Compound
Page 641 of 644
25. Predefined Compound Commands
Go Back
Arrays (525-537)
1. Why Imperative Full Screen

2. Arrays Close

3. Indexing Arrays
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. Indexing Arrays
5. Indexing Arrays
6. Physical Addressing
Computing: The . . .
7. Arrays Algorithms: Design & . . .

8. 2D Arrays Introducing Reals

9. 2D Arrays: Indexing Correctness, . . .

Compound Data
10. Ordering of indices
Higher Order Functions . . .
11. Arrays vs. Lists
Imperative . . .
12. Arrays: Physical A large Example: . . .

13. Lists: Physical Lecture-wise Index to . . .

Large Example: Tautology Checking (538-560) Home Page

1. Logical Arguments Title Page

2. Saintly and Rich


JJ II
3. About Cats
4. About God J I

5. Russell’s Argument Page 642 of 644

6. Russell’s Argument
Go Back
7. Russell’s Argument
8. Russell’s Argument Full Screen

9. Propositions Close

10. Compound Propositions


Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
11. Valuations
12. Valuations
13. Tautology
Computing: The . . .
14. Properties Algorithms: Design & . . .

15. Negation Normal Form Introducing Reals

16. Literals & Clauses Correctness, . . .

Compound Data
17. Conjunctive Normal Form
Higher Order Functions . . .
18. Validity
Imperative . . .
19. Logical Validity A large Example: . . .

20. Validity & Tautologies Lecture-wise Index to . . .

21. Problem Home Page


22. Tautology Checking
Title Page
23. Falsifying
JJ II
Tautology Checking Contd. (561-574)
1. Tautology Checking J I

2. Normal Forms Page 643 of 644

3. Top-down Development
Go Back
4. The Signature
5. The Core subproblem Full Screen

6. The datatype Close

7. Convert to CNF
Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8. Rewrite into NNF
9. ⇐⇒ and ⇒ Elimination
10. Push ¬ inward
Computing: The . . .
11. Push ¬ inward Algorithms: Design & . . .
12. conj of disj Introducing Reals
Correctness, . . .
13. Push ∨ inward
Compound Data
14. Tautology & Falsification Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .

Home Page

Title Page

JJ II

J I

Page 644 of 644

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

You might also like