0% found this document useful (0 votes)
78 views397 pages

Programming Language Abstractions Overview

The document outlines the course CS339: Abstractions and Paradigms for Programming, taught by Manas Thakur at IIT Bombay in Autumn 2025. It covers various programming languages, models of computation, and emphasizes the importance of abstractions and paradigms in programming. The course also includes logistics, evaluation criteria, and resources for learning, aiming to equip students with skills to design their own programming languages.

Uploaded by

silentbang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views397 pages

Programming Language Abstractions Overview

The document outlines the course CS339: Abstractions and Paradigms for Programming, taught by Manas Thakur at IIT Bombay in Autumn 2025. It covers various programming languages, models of computation, and emphasizes the importance of abstractions and paradigms in programming. The course also includes logistics, evaluation criteria, and resources for learning, aiming to equip students with skills to design their own programming languages.

Uploaded by

silentbang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS339: Abstractions and Paradigms for Programming

Introduction and Logistics

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Natural Languages
➤ Name the languages that you can speak/read/write.
➤ A unique advantage of living in India?
➤ The APP course in no other country can start with this
slide!

➤ Have you tried learning a new language?


➤ How di erent was that language from those you
already knew?
➤ What was your strategy?

Manas Thakur 2
ff
Programming Languages
➤ Name the PLs that you know.
➤ What do we mean by know?
➤ Name an English letter that’s not a PL.
➤ Why do we have so many PLs?
➤ How did so many PLs evolve?

➤ Or aren’t they so many, but just syntactic sugars


over a set of fundamental computational constructs?

Manas Thakur 3
Standing on the shoulders of giants
➤ Who are these people?

➤ Hints:
➤ Both were computer scientists.
➤ The most famous award in CS
is named after the LHS.
➤ RHS was the PhD advisor of
the LHS.
Alan Turing Alonzo Church

Manas Thakur 4
Two models of computation
➤ The Turing Machine (1936) performs computations by:
If you don’t remember TOC,
➤ Reading input
think of a computer that you are used to!!
➤ Modifying an internal memory using instructions
➤ Producing output
➤ The Lambda Calculus (1936) performs computations by:
➤ Evaluating expressions We would learn more of it in this course.

➤ Church-Turing Thesis (1937): Both the models, and all other reasonable
computation models, are equivalent!

Manas Thakur 5
Abstractions and Paradigms for Programming
➤ WHAT is the square root of a number X?

A number Y such that Y * Y == X.

➤ HOW to compute the square root of a number X?

- Make a guess Y.
- Improve the guess Y until its square is near enough to X.

A SQRT “procedure”

Manas Thakur 6
1. Procedural Abstraction
➤ Can we reuse the SQRT procedure as a blackbox?

Compute SQRT(A) + SQRT(B).


Functional
➤ Can we consume and produce procedures? Paradigm

Procedure Procedure Procedure


Y —> AVG(Y, X/Y) FIXED-POINT (F) SQRT(X)

➤ Functions become real entities in our programs!

Manas Thakur 7
2. Data Abstraction
➤ Say we have a general procedure to add two things and multiply by another:

Procedure
(X + Y) * Z
Object-Oriented
Can X, Y, Z be
Paradigm

➤ numbers?
Types &
vectors?
Proofs

➤ polynomials?
Stream
➤ signals? Processing

Manas Thakur 8
3. Language Abstraction
➤ Can we design a language that gives us higher level primitives?
vectors? matrices? tensors?
Logic

➤ images? videos?
➤ Can we modify an existing language to work di erently? Paradigm
➤ lazy evaluation instead of eager?
automatic allocation instead of explicit types?
Interpreter

➤ Can we extend a language to support something more?


➤ C to C++?
Design

Manas Thakur
ff
9
Abstractions and Paradigms for Programming
➤ Computation is much more fundamental (and older) than computers or
computer science, and is very much about specifying the right abstractions.
➤ There are several paradigms (aka ways) of specifying computations.
➤ But not as many as there are programming languages.

➤ There is no way we can teach you the PL that you can keep using for the
next 30-40 years. But if we teach you the ways of specifying and abstracting
computations, and equip you with the skills to design your own PLs, you
would have learnt enough to become a successful computing engineer.

Manas Thakur 10
The APP Course
Side learnings:
➤ Ability to comprehend large programs
➤ Empowers us to choose the right
PL for the task at hand; ➤ Language technologies that everyone
uses
➤ Makes learning a new PL much
➤ Crux of future programming languages
easier;
➤ Di erent right ways of thinking about
➤ Equips us in designing a new PL
the same thing
and its interpreter;
➤ Motivation to learn the art of cooking
➤ Is huge fun! (CS302) and processing (CS614,
CS618, CS6004) the food that
everyone (CSXYZ) eats

Manas Thakur 11
ff
Logistics, Evaluation, Rules

Manas Thakur 12
Logistics (CS339+CS355)
➤ Three classes per week in Slot 2: Mon 9:30am; Tue 10:35am; Thu 11:35am
➤ One lab per week in Slot L1: Mon 2pm, starting next week
➤ Instructor: Manas Thakur
➤ TAs: Aditya, Meetesh, Preet, Kush, Samarth, and many more to come…
➤ O ce hour: Wed 4:00-5:00 pm (CC 308) — this week only 30 mins
➤ Course webpage (schedule, deadlines): [Link]
➤ Moodle (material, submissions, marks)
➤ Grading would be relative, but you would de nitely pass if you score >30

Manas Thakur 13
ffi
fi
Logistics (CS355)
➤ You can come to the lab every week for practice
➤ But you will be evaluated in alternate weeks:
➤ 23B0901–23B0996: A batch (When? …look at the course plan)
➤ 23B0997–everybody else: B batch (When? …look at the course plan)
➤ As and Bs, as well as the TAs, will be rotated for fairness
➤ Stick to your batch unless you have a pink slip
➤ Di erent questions per batch, which means you will get double the number
of questions for practice!

Manas Thakur 14
ff
Evaluation (CS339)
➤ Exams
➤ Quizzes (10 * 2)
➤ Midsem (30)
➤ Endsem (40)
➤ Participation (10)
➤ Attendance
➤ Activities (e.g. Pop Quizzes)
➤ PCs (what are they?)

Manas Thakur 15
Evaluation (CS355)
➤ Practice (15)
➤ 5 out of 6 labs * 3
➤ Attendance (5)
➤ 5 out of 6 labs * 1
➤ Yes, you can miss any one lab without any problem;
if you don’t, we would take the best 5 out of 6
➤ Exams (80)
➤ Midsem: 40
➤ Endsem: 40
➤ Auto-graded; no re-evaluation; practice well

Manas Thakur 16
Missed-Evaluation Policy (CS339+CS355)
➤ All the below considerations would require prior permission
➤ CS339:
➤ Missed Quiz: Scale from next Big Exam
➤ Missed Midsem: Scale from Endsem
➤ Missed Endsem: Ideally none, but can consider a
supplementary exam in emergency situations
➤ CS355:
➤ Missed Midsem: Scale from Endsem
➤ Missed Endsem: Scale from Midsem

Manas Thakur 17
The Constitution
➤ Rights:
➤ An in-depth understanding of the various topics
➤ Acceptable answers to doubts/queries
➤ Timely and complete evaluations
➤ Duties:
➤ Sincerity and honesty
➤ No hesitation in reaching out to the instructor/TAs
➤ No plagiarism in anything

Manas Thakur 18
Reaching Out
➤ All conversation (doubts, queries, musings) encouraged either in the lab (SL2) or in the
o ce hour (recall: Wed 4-5 PM, CC 308)
➤ Email the instructor only if unavoidable, with the following norms:
➤ Subject must start with [CS339] or [CS355]
➤ Responses usually sent within 48 hours
➤ Don’t expect responses to DOS attacks
➤ No emails/DMs to TAs unless told by the instructor for something particular
➤ No deadlines/evaluation dates can be changed except in an emergency; make sure you keep
following the course-plan page
➤ Be polite and considerate in all conversations with the teaching team (and even classmates)

Manas Thakur 19
ffi
Learning Resources
Most
Lambda Haskell,
concepts and
calculus types, proofs
Scheme

➤ Textbooks (SICP, PLCC, TFWH)


➤ Some videos and papers
➤ Some online resources
➤ Take notes in class (slides won’t always be enough/available)
➤ Practice programs in the lab and at home

Manas Thakur 20
First Homework!
➤ Activity 1:
➤ Read rst seven pages of John Backus’s Turing Award Lecture
➤ Try nding out why was that lecture very interesting
➤ Answer a pop quiz in the next class
➤ Install DrRacket with sicp package on your computer
➤ [Link]

➤ Next class: Back to high-school maths!

Manas Thakur 21
fi
fi
The spirit of this course
Dedication text of Structure and Interpretation of Computer Programs (SICP)
Authors: Harold Abelson and Gerald Jay Sussman with Julie Sussman (MIT)

This book [course] is dedicated, in respect and admiration, to the spirit that lives in the computer:

“I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started
out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while
we began to take their complaints seriously. We began to feel as if we really were responsible for the successful,
error-free perfect use of these machines. I don’t think we are. I think we’re responsible for stretching them,
setting them o in new directions, and keeping fun in the house. I hope the eld of computer science never loses
its sense of fun. Above all, I hope we don’t become missionaries. Don’t feel as if you’re Bible salesmen. The world
has too many of those already. What you know about computing other people will learn. Don’t feel as if the key to
successful computing is only in your hands. What’s in your hands, I think and hope, is intelligence: the ability to
see the machine as more than when you were rst led up to it, that you can make it more.”
—Alan J. Perlis (April 1, 1922 – February 7, 1990), rst Turing Award winner

Manas Thakur 22
ff
fi
fi
fi
CS339: Abstractions and Paradigms for Programming
Bindings and Environment

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Manas Thakur 2
Programming Languages
➤ Syntax
➤ De ned by a (context-free) grammar

Design
➤ Semantics
➤ Meaning of each construct that is enabled by the grammar

➤ Evaluator/Interpreter
➤ Execute the programs written using the syntax based on Implementation
the de ned semantics

Manas Thakur 3
fi
fi
Our primary vehicle for APP: Scheme
➤ A dialect of LISP (for LISt Processing)
➤ The second oldest PL that is still in use today
➤ Which is the rst one?

➤ Our evaluator: DrRacket


➤ A popular cross-platform tool to learn and design Lisp-like languages
➤ Extremely popular in academia

Manas Thakur 4
fi
Recalling another giant

➤ Coined the term Arti cial Intelligence


➤ Designed several PLs
➤ Invented LISP
➤ In uenced the design of ALGOL
➤ Invented garbage collection
➤ Turing Award in 1971
John McCarthy (1927-2011)

Manas Thakur 5
fl
fi
The Elements of a PL
➤ Primitive expressions
➤ Things whose semantics are pre-de ned in the language

➤ Means of combination
➤ Building compound elements from primitive ones

➤ Means of abstraction
➤ Ability to use compound elements as primitives themselves

Manas Thakur 6
fi
Primitive expressions in Scheme
➤ An expression that cannot be evaluated further
➤ The language implementation (evaluator or interpreter) is “born” with
these expressions

➤ How do interpreters work?


Read Eval Print

REPL
Loop

Manas Thakur 7
Combining Expressions
➤ Primitive procedures can be used to combine primitive expressions
➤ Result is a compound expression

➤ Syntax: (operator operand1 operand2 ...)

Pre x notation

➤ Advantages of pre x notation?

Manas Thakur 8
fi
fi
De ining Abstractions
➤ Associate names with values
Evaluate:
> (define x 2) > x

➤ Associate names with expressions


Evaluate:
> (define y (+ x 1)) > y

➤ Associate names with procedures


Evaluate:
> (define (add x y) > (add op1 op2)
(+ x y))

Manas Thakur 9
f
Procedures as Entities
➤ What did we associate the name add with here?
> (define (add x y)
(+ x y))
➤ Can be equivalently written as:
> (define add
(lambda (x y) add belongs as much to
(+ x y)) the vocabulary now as + did!
➤ We had named a procedure (a lambda).
➤ Procedures/functions/lambdas are rst-class entities even
without names, in the exact same way as numbers — they exist!

Manas Thakur 10
fi
Bindings and Environment
➤ (define x 20) creates a binding from x to 20
➤ Where is it created/remembered?
➤ The ENVIRONMENT
➤ Default: The GLOBAL ENVIRONMENT

➤ Lookup involves searching in the current environment


➤ We shall see how do we work with multiple environments in future.

Manas Thakur 11
Applying Procedures
➤ Substitute actual arguments in place of formal parameters (at once) in the
body of the procedure, and then evaluate the body of the procedure.

> (define (add x y) (+ 4 (+ 7 5))


Next class:
(+ x y)) (+ 4 12)
Orders of Evaluation
> (add 4 (+ 7 5)) 16

Called the Substitution Model of Evaluation

Manas Thakur 12
CS339: Abstractions and Paradigms for Programming
The Procedural Paradigm

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recall Substitution Model of Procedure Application
To apply a compound procedure to arguments, evaluate the body of the
procedure with each formal parameter replaced by the corresponding argument.

(* 4 (+ (* 4 2) 5))
> (define (foo x y)
(* 4 (+ 8 5))
(* x (+ (* x 2) y)))
(* 4 13)
> (foo 4 5)
52

Manas Thakur 2
A “Procedural” Example

> (define (square x) (* x x))

> (define (sum-of-squares x y)


(+ (square x) (square y)))

> (define (f a)
(sum-of-squares (+ a 1) (* a 2)))

Manas Thakur 3
Solve using substitution
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (square x) (* x x))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))

(f 5)
(sum-of-squares (+ 5 1) (* 5 2))
(+ (square (+ 5 1)) (square (* 5 2)))
(+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
(+ (* 6 6) (* 10 10))
(+ 36 100)
136
Manas Thakur 4
Could we have substituted this way?
(define (sum-of-squares x y)
(+ (square x) (square y)))

Applicative Order of Evaluation (define (square x) (* x x))


(define (f a)
(sum-of-squares (+ a 1) (* a 2)))

(f 5)
(sum-of-squares (+ 5 1) (* 5 2))
(sum-of-squares 6 10)
(+ (square 6) (square 10)) Evaluate arguments then apply
(+ (* 6 6) (* 10 10))
(+ 36 100)
136
Manas Thakur 5
Solve using substitution
(define (sum-of-squares x y)
(+ (square x) (square y)))

Normal Order of Evaluation (define (square x) (* x x))


(define (f a)
(sum-of-squares (+ a 1) (* a 2)))

(f 5)
(sum-of-squares (+ 5 1) (* 5 2))
(+ (square (+ 5 1)) (square (* 5 2)))
Fully substitute
(+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
then reduce
(+ (* 6 6) (* 10 10))
(+ 36 100)
136
Manas Thakur 6
Applicative vs Normal Orders of Evaluation
➤ Applicative order Call by value Call by name (need)

avoids redundant computation


(f 5) (f 5)
➤ (sum-of-squares (+ 5 1) (* 5 2)) (sum-of-squares (+ 5 1) (* 5 2))
(sum-of-squares 6 10) (+ (square (+ 5 1)) (square (* 5 2)))
(+ (square 6) (square 10)) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
➤ takes lesser memory (+ (* 6 6) (* 10 10))
(+ 36 100)
(+ (* 6 6) (* 10 10))
(+ 36 100)
136 136

➤ However, there are advantages of normal order too, and there are ways to
make it more efficient (post mid-sem).

➤ Which one does Scheme use?


➤ How can you find out for any language?

Manas Thakur 7
Conditionals
➤ Absolute value of x:

(define (abs x)
(cond ((< x 0) (- x))
(define (abs x) (else x)))
(cond ((> x 0) x) Clause OR
((= x 0) 0) Predicate (define (abs x)
((< x 0) (- x))))
Action
(if (< x 0) Predicate
(- x) Consequent
x)) Alternate

Manas Thakur 8
Example: Newton’s method for computing square roots
➤ Start with a guess and “improve” the guess until it is “good enough”
➤ Square root of 2:

Guess [y] Quotient [x/y] Average [(y+x/y)/2]

1 2/1 = 2 (1+2)/2 = 1.5

1.5 2/1.5 = 1.3333 1.4167

1.4167 1.4118 1.4142

1.4142 … …

➤ Say we stop when the square of the guess is equal to the number up to three
decimal places.

Manas Thakur 9
CS339: Abstractions and Paradigms for Programming
Recursion and Iteration

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Example: Newton’s method for computing square roots
➤ Start with a guess and “improve” the guess until it is “good enough”
➤ Square root of 2:

Guess [y] Quotient [x/y] Average [(y+x/y)/2]

1 2/1 = 2 (1+2)/2 = 1.5

1.5 2/1.5 = 1.3333 1.4167

1.4167 1.4118 1.4142

1.4142 … …

➤ Say we stop when the square of the guess is equal to the number up to three
decimal places.

Manas Thakur 2
Example: Newton’s square root [Cont.]

(define (sqrt-iter guess x) (define (abs x)


(if (good-enough? guess x) (if (< x 0)
guess (- x)
(sqrt-iter (improve guess x) x))) x))
(define (improve guess x) (define (square x)
(average guess (/ x guess))) (* x x))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x) ➤ Can you identify two “bad”
(< (abs (- (square guess) x)) 0.001)) things about this code?
(define (sqrt x)
(sqrt-iter 1.0 x))

Manas Thakur 3
Example: Newton’s square root [Cont.]
Packaged
Namespace Abstraction
(define (average x y) ...)
together
(define (square x) ...)

(define (abs x) ...)


Unnecessary
(define (sqrt x) arguments gone
(define (improve guess)
(average guess (/ x guess))) Lexical
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
Scoping
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess)))) What’s the secret behind sqrt-“iter”?
(sqrt-iter 1.0))

Manas Thakur 4
Let’s look at the processes generated by procedures
➤ Factorial of a number:

➤ A procedure to compute the same:

(define (fact n)
(if (= n 1)
1
(* n (fact (- n 1)))))

Manas Thakur 5
The generated process for fact(5)
(define (fact n)
(if (= n 1) (fact 5)
1
(* n (fact (- n 1))))) (* 5 (fact 4))
(* 5 (* 4 (fact 3)))
(* 5 (* 4 (* 3 (fact 2))))
(* 5 (* 4 (* 3 (* 2 (fact 1)))))
(* 5 (* 4 (* 3 (* 2 1))))
Time: O(n)
(* 5 (* 4 (* 3 2)))
Space: O(n)
(* 5 (* 4 6))
(* 5 24)
Recursive Process 120

Manas Thakur 6
How about this one?
➤ Another way to compute factorial:

(define (fact n)
(define (fact-iter prod ctr n)
(if (> ctr n)
prod
(fact-iter (* ctr prod)
(+ ctr 1)
n)))
(fact-iter 1 1 n))

Manas Thakur 7
The generated process for fact(5)
(define (fact n)
(define (fact-iter prod ctr n)
(if (> ctr n)
prod
(fact 5)
(fact-iter (* ctr prod)
(+ ctr 1) (fact-iter 1 2 5)
n)))
(fact-iter 1 1 n)) (fact-iter 2 3 5)
(fact-iter 6 4 5)
(fact-iter 24 5 5)
Time: O(n)
(fact-iter 120 6 5)
Space: O(1) Iterative Process 120

Manas Thakur 8
Another recursive process
➤ Fibonacci numbers:

(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))

Tree-Recursive Process

Manas Thakur 9
Recursive vs Iterative Processes

(fact 5) ➤ Recursive: Grow then shrink.


(* 5 (fact 4)) ➤ Recursive: Require more space. (fact 5)
(* 5 (* 4 (fact 3))) (fact-iter 1 2 5)
(* 5 (* 4 (* 3 (fact 2))))
➤ Iterative: State variables. (fact-iter 2 3 5)
(* 5 (* 4 (* 3 (* 2 (fact 1))))) ➤ Iterative: Can be resumed easily. (fact-iter 6 4 5)
(* 5 (* 4 (* 3 (* 2 1)))) (fact-iter 24 5 5)
(* 5 (* 4 (* 3 2)))
➤ Recursive: More bureaucratic.
(fact-iter 120 6 5)
(* 5 (* 4 6)) ➤ But even an iterative process 120
(* 5 24) generated by a recursive
120 procedure requires more space!

Manas Thakur 10
Tail-Call Optimization
➤ Iteration without looping constructs is expensive in space.
➤ But we can avoid returning when the recursive call is the tail!
➤ Saves stack space and makes iteration (nearly) as e cient as imperative
languages with looping constructs.

(define (fact n)
(define (fact-iter prod ctr n)
(if (> ctr n)
prod
(fact-iter (* ctr prod)
(+ ctr 1)
n)))
(fact-iter 1 1 n))

Manas Thakur 11

ffi
Lab Modus Operandi
➤ Each lab has to be done individually.
➤ Only lab desktops. Fixed seat. No mobile phones.
➤ TAs would clarify your doubts and evaluate by seeing your code
as well as asking questions. Their judgment would be nal. We
would rotate TAs.
➤ You can skip one lab; more than that would cause loss of marks.
➤ Maintain a silent atmosphere in the lab.
➤ DO NOT CHEAT.
➤ Your day would start by installing the sicp package in DrRacket.
➤ Batch 1A (23B0901–23B0996) today!

Manas Thakur
fi
12
CS339: Abstractions and Paradigms for Programming
Higher Order Functions

Manas Thakur
CSE, IIT Bombay

Autumn 2025
The Blackbox [Procedural] Abstraction
➤ Allows procedures to be reused similar to primitive operators
➤ Enhances the vocabulary of the language
➤ Provides a namespace for variables

(define (bar y)
? (+ x y))

Free variable Bound variable

Manas Thakur 2
Scoping
➤ Determines which values are free variables bound to.
➤ Lexical/Static scoping: Look into the environment in which the procedure was
de ned.
➤ Dynamic scoping: Look into the environment in which the procedure was called.

> (define x 20) > (define x 20) > (define x 20)

Dynamic Scoping
Static Scoping

Static Scoping
> (define (foo) > (define (bar y) > (define (bar y)
(define x 30) (+ x y)) (+ x y))
(define (bar y) > (define (foo) > (define (foo)
(+ x y)) (define x 30) (define x 30)
(bar 40)) (bar 40)) (bar 40))
> (foo) 70 > (foo) 60 > (foo) 70

Manas Thakur 3
fi
Summation in Mathematics

Manas Thakur 4
Summing a series of numbers
➤ Sum the integers from a to b: Do they look similar?

(define (sum-ints a b)
(if (> a b)
0
(+ a (sum-ints (+ a 1) b))))

➤ Sum the cubes of integers from a to b:

(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a) (sum-cubes (+ a 1) b))))

Manas Thakur 5
How about this one?
➤ Sum to obtain π/8:

(define (sum-pi a b)
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2)))
(sum-pi (+ a 4) b))))

Manas Thakur 6
Now why do we have so many of them!
(define (sum-ints a b) (define (sum-cubes a b)
(if (> a b) (if (> a b)
0 0
(+ a (+ (cube a)
(sum-ints (+ a 1) b)))) (sum-cubes (+ a 1) b))))

(define (sum-pi a b)
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2)))
(sum-pi (+ a 4) b))))

(define (<name> a b)
(if (> a b)
➤ What is the common structure? 0
(+ <term(a)>
(<name> <next(a)> b))))

Manas Thakur 7
The HOF Abstraction
(define (sum-ints a b) (define (sum-series term a next b)
(sum-series (if (> a b)
(lambda (x) x) 0
a Procedures
(lambda (x) (+ x 1)) (+ (term a)
b)) (sum-series term (next a) b))))

OR
➤ Sum the integers from a to b: ➤ Sum the cubes of integers from a to b:
(define (sum-ints a b)
(define (cube x) (* x x x))
(sum-series id a inc b))
(define (sum-cubes a b)
(define (id x) x)
(sum-series cube a inc b))
(define (inc x) (+ x 1))
(sum-ints 1 10)
➤ Sum to obtain π/8?

Manas Thakur 8
Procedure Procedure Procedure

Another example Y —> AVG(Y, X/Y) FIXED-POINT (F) SQRT(X)

We have started consuming


(define (fixed-point f start) and producing procedures!
(define tolerance 0.001) ➤ How does it work?
(define (close-enough? u v) Lexical
<proc>:
(< (abs (- u v)) tolerance)) Scope
(lambda (y)
(define (iter old new) (avg y (/ 2 y)))
(if (close-enough? old new)
new (sqrt 2)
(iter new (f new))))
(fixed-point <proc> 1.0)
(iter start (f start)))
(iter 1.0 (<proc> 1.0))
(define (avg x y) (/ (+ x y) 2)) (iter 1.0 (avg 1.0 (/ 2 1.0)))
(define (sqrt x) (iter 1.0 1.5)
(fixed-point (lambda (y) (avg y (/ x y)))
1.0)) ...

Manas Thakur 9
First-class values
First example. Passing the term and the next functions as arguments allowed us to express summation as
a general concept (in the PL), and abstracted the speci c logic for given series.

Second example. We could create an abstraction to express the general notion of xed points by being able
to consume procedures and produce procedures.

➤ In a PL, a value is rst-class if it can be:


➤ named
➤ taken as an argument by a procedure
➤ returned back from a procedure
➤ stored into data structures
➤ In the next class, we would return functions from higher order procedures, and see that the
real magic begins!

Manas Thakur 10
fi
fi
fi
CS339: Abstractions and Paradigms for Programming
Closures and Let Bindings

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recall irst-class values
➤ In a PL, a value is rst-class if it can be:
➤ named
➤ taken as an argument by a procedure
➤ returned back from a procedure
➤ stored into data structures

➤ We had seen examples of passing procedures as arguments.

Manas Thakur 2
f
fi
Let’s return functions
; Equivalent:
> (define (make-inc)
(define (foo x) (+ x 1))
foo)
> (define (make-inc)
(lambda (x) (+ x 1))) A function that returns a function.

> ((make-inc) 15) Apply the returned function.

> (define inc (make-inc))


Name and use the returned function.
> (inc 15)

Manas Thakur 3
Now let’s see a MAGIC
> (define (make-addx x)
(lambda (y) (+ x y)))
➤ What was the magic?

> (define magic (make-addx 3))

> (magic 4)
Where did the ‘3’ come from?
7

You might for now say “Substitution”. But…

Manas Thakur 4
MAGIC continues…
> (define x 4) > (define x 100)
> (define (foo y)
(define x 4) > ((foo 5) 40)
(define (bar z)
49
(+ x y z))
bar)
> ((foo 5) 40)
Now where is the ‘4’ coming from?
49

Procedures in Scheme are not simply functions; they are CLOSURES.

Manas Thakur 5
Closure = Lambda + Environment
➤ Along with the body of a lambda, closures encapsulate bindings from the environment
in which the lambda was de ned.
➤ When we apply a closure, after substituting arguments for parameters, we evaluate the
body in the enclosed environment.
➤ This also happens when we pass a procedure:
> (define (foobar f x) (f x))
> (define (foo y)
(define x 4)
(define (bar z)
(+ x y z))
(foobar bar 40))
> (foo 5)
49

Manas Thakur 6
fi
Abstraction with Procedures as Return Values
➤ Recall how we took averages while computing square root to converge and
reach the answer. Average damping is a general technique:

(define (avg-damp f) ; Square root again:


(lambda (z) (avg z (f z))))
(define (sqrt x)
(fixed-point (avg-damp (lambda (y) (/ x y)))
(define (fixed-point f start)
(define tolerance 0.001) 1.0))
(define (close-enough? u v)
(< (abs (- u v)) tolerance))
(define (iter old new) How does this work?
(if (close-enough? old new)
new
(iter new (f new)))) How is this di erent from last week’s sqrt?
(iter start (f start)))

Manas Thakur 7
ff
Abstraction with Procedures as Return Values [Cont.]
; Last class
(define (avg-damp f)
(define (sqrt x)
(lambda (z) (avg z (f z))))
(fixed-point (lambda (y) (avg y (/ x y)))
1.0))

; Today
(define (sqrt x)
(fixed-point (avg-damp (lambda (y) (/ x y)))
1.0))

Returning a function allowed us to express average-damping as a general


concept, and abstracted away the speci c logic for sqrt.

Manas Thakur 8
fi
Local Names
f(x,y) = x(1+xy)2 + y(1-y) + (1+xy)(1-y)

Let a be (1+xy) and b be (1-y). Thus:

f(x,y) = xa2 + yb + ab

(define (f x y)
(let ((a (+ 1 (* x y))) let bindings are created simultaneously,
let bindings
(b (- 1 y)))
and let body is evaluated with respect to
(+ (* x (square a))
(* y b) let body those bindings.
(* a b))))

Manas Thakur 9
Let’s Practice

➤ 15? 12? Error? 6?


➤ 12
> (define x 2)
> (let ((x 3)
(y (+ x 2)))
(* x y))
➤ When will there be an error?
➤ Remove (define x 2)

let bindings are created simultaneously, and let body is evaluated with respect to
those bindings.

Manas Thakur 10
Which syntax are we sugar for?
let bindings are created simultaneously, and let body is evaluated with respect to
those bindings.

> (define (f x y)
> (define (f x y)
((lambda (a b)
(let ((a (+ 1 (* x y))) (+ (* x (square a))
(b (- 1 y))) (* y b)
(+ (* x (square a))
(* y b) (* a b)))
(+ 1 (* x y))
(* a b))))
(- 1 y)))
> (f 2 3)
> (f 2 3)

Manas Thakur 11
Lambda-Lambda Everywhere!
> (define (f x y) > (define (f x y)
((lambda (a b) ((lambda (a b)
(+ (* x (square a)) (+ (* x ((lambda (x) (* x x)) a))
(* y b) (* y b)
(* a b))) (* a b)))
(+ 1 (* x y)) (+ 1 (* x y))
(- 1 y))) (- 1 y)))
> (f 2 3) > (f 2 3)

> ((lambda (x y)
((lambda (a b)
(+ (* x ((lambda (x) (* x x)) a))
(* y b)
(* a b))) With Lambda Calculus, we can even
(+ 1 (* x y))
(- 1 y))) remove the numbers and operators :-)
2 3)

Manas Thakur 12
CS339: Abstractions and Paradigms for Programming
Data Abstraction: Pairs and Lists

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Quiz 1
➤ Wednesday, August 20, 8:30-9:15 AM
➤ Syllabus: Everything until Tuesday, August 19th
➤ Last year’s QP would be uploaded, but no guarantee
that the pattern would be similar (except that it
would be a short exam to test progress)
➤ Closed book, closed smart devices (bring ID cards)
➤ Seating arrangement to be informed a day before
➤ Quiz 2 date also available on the course webpage

Manas Thakur 2
Let’s work with “rational” numbers
➤ Say we have the following procedures available (wishful thinking):
➤ (make-rat n d), which gives a rational number with numerator n and
denominator d
➤ (numer x), which gives the numerator of a rational number x
➤ (denom x), which gives the denominator of a rational number x

➤ Now we can play with rational numbers!


➤ PC: What would be the problem if we didn’t make an explicit rat?

Manas Thakur 3
Operations with rational numbers
➤ How do we add two rational numbers ➤ Print them nicely:
x and y?
(define (print-rat x)
(define (add-rat x y) (newline)
(make-rat (+ (* (numer x) (denom y)) (display (numer x))
(* (numer y) (denom x))) (display “/“)
(* (denom x) (denom y)))) (display (denom x)))

➤ Multiply them?
Interestingly, we don’t yet know
(define (mult-rat x y) how to represent them!
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))

Manas Thakur 4
One way to represent rational numbers

➤ Let’s check if this works:


(define (make-rat n d)
(lambda (select)
(if (= select 0)
n
d)))
(define (numer x)
(x 0))
(define (denom x)
(x 1))

Manas Thakur 5
But pairing data is quite a popular operation!
➤ Scheme provides the following special forms to work with pairs:

➤ (cons x y) puts x and y together and returns a pair

➤ (car x) returns the rst element of a pair x

➤ (cdr x) returns the second element of a pair x

Manas Thakur 6
fi
Data Abstraction
➤ Representation is separate from usage; designing/changing the former
should not a ect the latter.

Manas Thakur 7
ff
Updated rational-number implementation
(define (make-rat n d)
➤ Notice that the usage need not change:
(cons n d))
(define (numer x) (define (add-rat x y)
(car x)) (make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(define (denom x) (* (denom x) (denom y))))
(cdr x))
(define (mult-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (print-rat x)
(newline)
(display (numer x))
(display “/“)
(display (denom x)))

Manas Thakur 8
Box-Pointer Notation of Pairs

➤ Advantage: Implicit that pairs can have pairs as elements!

Manas Thakur 9
But chaining pairs to form a list is quite a popular operation!
➤ Scheme provides the following special forms to work with lists:

➤ (list e1 e2 e3) is a syntactic sugar for


(cons (e1 (cons e2 (cons e3 nil))))

➤ nil represents an empty list

➤ (null? l) checks whether a list l is empty and returns a boolean

Manas Thakur 10
And we often overdo stuff :D

Manas Thakur 11
CS339: Abstractions and Paradigms for Programming
Programming with Lists

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Quiz 1

➤ When: Friday, August 22, 8:30-9:15 AM


➤ Syllabus: Everything until Thursday, August 21st
➤ Last year’s QP would be uploaded, but no guarantee that
the pattern would be similar (except that it would be a
short exam to test progress)
➤ Closed book, closed smart devices (bring ID cards)
➤ Seating arrangement to be informed a day before
➤ Quiz 2 date also available on the course webpage

Manas Thakur 2
Index a list like an array
Getting the nth element from l is same as getting the (n-1)th element from (cdr l).

(define (get n lst)


(if (= n 0) But this throws an error with
(car lst) (get 3 (list 1 2))
(get (- n 1) (cdr lst))))

(define (get n lst)


(cond ((null? lst) nil)
((= n 0) (car lst))
This returns an empty list.
(else (get (- n 1) (cdr lst)))))

Which one is better? A design decision.

Manas Thakur 3
Determine length of a list

(define (length lst)


(if (null? lst) Length of a list l is one plus
0 the length of (cdr l).
(+ 1 (length (cdr list)))))

(define (length lst)


(define (length-iter n lst)
(if (null? lst)
n
Iterative version.
(length-iter (+ 1 n) (cdr lst))))
(length-iter 0 lst))

Manas Thakur 4
Now it’s getting easier to fathom
➤ Sum the elements of a list:

Summing a list (l) is same as adding the rst element of


the list (car l) to the sum of the remaining list (cdr l).

(define (sum lst)


(if (null? lst)
0
(+ (car lst) (sum (cdr list)))))

Recursive de nitions are often easier to spell out and understand!

Manas Thakur 5
fi
fi
Form sublists
➤ Take the rst n elements of a list:
(define (take n lst)
(if (= n 0)
nil
(cons (car lst) (take (- n 1) (cdr lst)))))

Get used to “spelling out” the recursive de nitions.

➤ Drop the rst n elements of a list:


(define (drop n lst)
(if (= n 0)
lst
(drop (- n 1) (cdr lst))))

Manas Thakur 6
fi
fi
fi
Let’s get complicated! How about Insertion Sort?
➤ Let’s insert a number at its right place in a sorted list:
(define (insert num lst)
(cond ((null? lst) (cons num nil))
((<= num (car lst)) (cons num lst))
(else (cons (car lst) (insert num (cdr lst))))))

➤ Job’s almost done:


(define (isort lst)
(if (null? lst)
nil
(insert (car lst) (isort (cdr lst))))) Interested in
➤ Test: comparing this with
> (define l (list 56 47 89 23 100 27 38)) other languages?
> (isort l)

Manas Thakur 7
But I like trees more!
➤ Let’s make a binary search tree (BST) node:
(define (make-tree datum left right)
(list datum left right))

➤ Once we got a “constructor”, we need some “selectors”:

(define (datum t) (car t))


(define (left-tree t) (cadr t))
(define (right-tree t) (caddr t) What if this was cddr?

Manas Thakur 8
Our tree is currently empty!
➤ Inserting an element in a BST:
(define (insert e t)
(cond ((null? t) (make-tree e nil nil))
((= e (datum t)) t)
((< e (datum t)) (make-tree (datum t)
(insert e (left-tree t))
(right-tree t)))
(else (make-tree (datum t)
(left-tree t)
(insert e (right-tree t))))))

➤ Test:
> (define t (make-tree 4 nil nil))
> (define t1 (insert 6 (insert 3 (insert 5 t))))

Manas Thakur 9
Practice makes a tree perfect
➤ Checking for presence of an element
(define (elem? e t)
(cond ((null? t) #f)
((= e (datum t)) #t)
((< e (datum t)) (elem? e (left-tree t)))
(else (elem? e (right-tree t)))))

➤ Inorder traversal:
(define (inorder-try t)
(if (null? t)
nil What’s the
(cons (inorder-try (left-tree t)) problem with this?
(cons (datum t)
(inorder-try (right-tree t))))))

Manas Thakur 10
Properly formatted inorder traversal of a tree
➤ Concatenate two lists:
(define (concat l1 l2)
(if (null? l1)
l2
(cons (car l1) (concat (cdr l1) l2))))

➤ Inordo perfectum:
(define (inorder t)
(if (null? t)
nil
(concat (inorder (left-tree t))
Why this?
(concat (list (datum t))
(inorder (right-tree t))))))

Manas Thakur 11
Can we now have a nice tree-sort procedure?
➤ Form a BST out of a list, and then print its inorder traversal!

(define (list2tree l)
(define (l2t-iter t l)
(if (null? l)
t
(l2t-iter (insert (car l) t) (cdr l))))
(l2t-iter (make-tree (car l) nil nil) (cdr l)))
(define (tree-sort l)
(inorder (list2tree l)))
> (define l (list 56 47 89 23 100 27 38)) Even more fun in
> (tree-sort l)
the next class :-)

Manas Thakur 12
CS339: Abstractions and Paradigms for Programming
Higher Order List Functions

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Repetitions galore
➤ Recall the program to sum a list: ➤ Length?
(define (sum l) (define (length l)
(if (null? l) (if (null? l)
0 0
(+ (car l) (sum (cdr l))))) (+ 1 (length (cdr l)))))

➤ What about product? Along with similar syntax, what


(define (prod l) are we doing in each of these?
(if (null? l)
1
(* (car l) (prod (cdr l))))) Folding a list to a single value!

Manas Thakur 2
The fold higher-order operation
(define (foldr f v l)
(if (null? l)
v
Also called reduce.
(f (car l) (foldr f v (cdr l)))))

➤ Updated sum: ➤ Length?

(define (sum l) (define (length l)


(foldr + 0 l) ➤ Product: (foldr (lambda (x y) (+ 1 y))
0 l)
(define (prod l)
(foldr * 1 l)

Manas Thakur 3
What was the ‘r’ in “foldr”?
(define (foldr f v l) > (define l (list 1 2 3 4))
(if (null? l)
> (foldr + 0 l)
v
(f (car l) (foldr f v (cdr l)))))
(+ 1 (+ 2 (+ 3 (+ 4 0))))
Folds from right.

➤ We also have a foldl:


(define (foldl f v l) > (foldl + 0 l)
(if (null? l)
v (+ 0 1) ==> (+ 1 2) ==>
(foldl f (f v (car l)) (cdr l))))
(+ 3 3) ==> (+ 6 4) ==> 10
Folds from left.

Manas Thakur 4
Transforming lists
➤ Add 10 to each element of a list:

(define (add10 l)
(if (null? l) We are mapping a list to
nil
(cons (+ 10 (car l)) (add10 (cdr l)))))
another list by applying
a common function to
➤ How about squaring each element? all its elements!

(define (sqr-list l)
(if (null? l)
nil
(cons (square (car l)) (sqr-list (cdr l)))))

Manas Thakur 5
The map higher-order operation
(define (map f l)
(if (null? l)
nil
(cons (f (car l)) (map f (cdr l)))))

➤ What’s squaring a list now? ➤ The new add10 function:


(define (sqr-list l) (define (add10 l)
(map square l)) (map (lambda (x) (+ x 10)) l))

Map-based functions usually are also


great candidates for parallelization!

Manas Thakur 6
Something complex (for now!)
➤ Compute the sum of the squares of the even numbers of a list:
(define (sum-sqr-evn l)
(if (null? l)
0
(let ((x (car l)))
(if (even? x)
(+ (square x) (sum-sqr-evn (cdr l)))
(sum-sqr-evn (cdr l))))))

Now keep this aside for some time.

Manas Thakur 7
Filtering lists
➤ Return numbers from a list that are >10:
(define (gt10 l)
(if (null? l)
nil
(if (> (car l) 10)
(cons (car l) (gt10 (cdr l)))
(gt10 (cdr l))))) You know what we’re
➤ Return even numbers from a list: going to do next!

(define (get-evn l)
(if (null? l)
nil
(if (even? (car l))
(cons (car l) (get-evn (cdr l)))
(get-evn (cdr l)))))

Manas Thakur 8
The ilter higher-order operation
(define (filter pred l)
(if (null? l)
nil
(let ((x (car l)))
(if (pred x)
(cons x (filter pred (cdr l)))
(filter pred (cdr l))))))

➤ The new get-evn: ➤ The new gt10:


(define (get-evn l) (define (gt10 l)
(filter even? l)) (filter (lambda (x) (> x 10)) l))

Manas Thakur 9
f
Now we can solve complex problems so elegantly!
➤ Get a list containing squares of the even numbers of a list:
(define (sqr-evn l) Next Class
(map square (filter even? l))) We begin with OO (ooo)

➤ Compute the sum of the squares of the even numbers of a list:


(define (sum-sqr-evn l)
(foldr + 0 (map square (filter even? l))))

(define (sum-sqr-evn l)
Programming this way is called using (if (null? l) Comparison
sequences as conventional interfaces 0
(let ((x (car l)))
— a topic we would revisit again (that (if (even? x)
(+ (square x) (sum-sqr-evn (cdr l)))
time, for e ciency) and enjoy a lot! (sum-sqr-evn (cdr l))))))

Manas Thakur 10
ffi
CS339: Abstractions and Paradigms for Programming
Overview of Object-Oriented Programming

Manas Thakur
CSE, IIT Bombay

Autumn 2025
What all do you associate with OOP?
➤ Objects!
➤ Ability to group multiple items into a single abstraction
➤ Ability to create multiple objects of a certain kind
➤ Ability to perform operations on the object abstraction
➤ Ability to say that some objects are like others in some sense but
di erent in their own ways
➤ Let’s handle these abilities one by one.

Manas Thakur 2
ff
1. Ability to group multiple items into a single abstraction
➤ Structures in C; Classes in C++/Java
➤ What about in Scheme?

(define (make-rat x y)
(lambda (select)
(if (= select 0) x y)))

➤ We already know how to create classes in Scheme!

Manas Thakur 3
2. Ability to create multiple objects of a certain kind
➤ Constructors in C++/Java
➤ Yes sir, we’ve already got ’em!

(define (make-rat x y)
(lambda (which)
(if (= which 0) x y)))

(define n1 (make-rat 2 3))

(define n2 (make-rat 3 4))

➤ Reckon that both n1 and n2 hold the values of di erent “ elds” (again due to
the existence of closures!).

Manas Thakur 4
ff
fi
3. Ability to perform operations on the object abstraction
➤ Functions/Methods in C++/Java
➤ In Scheme?
➤ Yeh to pehli class se padha rahe hain!
(define (make-rat x y)
(lambda (which)
(if (= which 0) x y)))

(define (numer n) (n 0))


(define (denom n) (n 1))

(define (mult-rat n1 n2)


(make-rat (* (numer n1) (numer n2))
(* (denom n1) (denom n2))))
Manas Thakur 5
class Rational {
Let’s compare… int x; int y;

Rational(int x, int y) {
(define (make-rat x y)
this.x = x; this.y = y;
(lambda (which)
}
(if (= which 0) x y)))
int numer() { return x; }
(define (numer n) (n 0))
int denom() { return y; }
(define (denom n) (n 1))
Rational mult-rat(Rational other) {
(define (mult-rat n1 n2) return new Rational(
(make-rat (* (numer n1) [Link]() * [Link](),
(numer n2)) [Link]() * [Link]());
(* (denom n1) }
(denom n2)))) }
(define n1 (make-rat 2 3))
Rational n1 = new Rational(2,3);
(define n2 (make-rat 3 4))
Rational n2 = new Rational(3,4);
(define n3 (mult-rat n1 n2))
Rational n3 = [Link]-rat(n2);
Manas Thakur 6
What all does our Scheme version lack?
(define (make-rational x y)
(lambda (which)
➤ Packaging (if (= which 0) x y)))
(define (numer n) (n 0))
(define (denom n) (n 1))
➤ Encapsulation of the de ned functions/ (define (mult-rat n1 n2)
(make-rat (* (numer n1)
methods into a module (numer n2))
(* (denom n1)
(denom n2))))
➤ Dispatch on objects (define n1 (make-rat 2 3))
(define n2 (make-rat 3 4))
(define n3 (mult-rat n1 n2))
➤ Aka message passing ([Link] or class Rational {
int x; int y;
[Link]) Rational(int x, int y) {
this.x = x; this.y = y;
}
➤ And the complete miss on the 4th bullet from int numer() { return x; }
int denom() { return y; }
Slide 2! Rational mult-rat(Rational other) {
return new Rational(
[Link]() * [Link](),
➤ We’ll learn how to address all of these and }
[Link]() * [Link]());

}
more — in Scheme! Rational n1 = new Rational(2,3);
Rational n2 = new Rational(3,4);
Rational n3 = [Link]-rat(n2);

Manas Thakur 7
fi
The rabbit asked the king, “Where shall I begin, please your Majesty?”.
The king replied gravely, “Begin at the beginning.”

Manas Thakur 8
Let’s begin with complex numbers
➤ Two representations:
➤ Rectangular (real and imaginary)
➤ Polar (magnitude and angle)
➤ Which one to choose?
➤ Addition/subtraction easier using rectangular representation:
real-part(z1+z2) = real-part(z1) + real-part(z2)
img-part(z1+z2) = img-part(z1) + img-part(z2)
➤ Multiplication/division easier using polar representation:
magnitude(z1*z2) = magnitude(z1) * magnitude(z2)
angle(z1*z2) = angle(z1) + angle(z2)

Manas Thakur 9
Let’s choose both the representations
➤ Rectangular complex numbers:

(define (make-from-real-imag x y) (cons x y))

(define (make-from-mag-ang r a)
(cons (* r (cos a)) (* r (sin a))))

(define (real-part z) (car z))


(define (imag-part z) (cdr z))

(define (magnitude z)
(sqrt (+ (square (real-part z))
(square (imag-part z)))))
(define (angle z)
(atan (imag-part z) (real-part z)))

Manas Thakur 10
Let’s choose both the representations
➤ Polar complex numbers:

(define (make-from-mag-ang r a) (cons r a))

(define (make-from-real-imag x y)
(cons (sqrt (+ (square x) (square y)))
(atan y x)))

(define (magnitude z) (car z))


(define (angle z) (cdr z))

(define (real-part z) (* (magnitude z) (cos (angle z))))


(define (imag-part z) (* (magnitude z) (sin (angle z))))

Manas Thakur 11
What about the operations?
➤ Do not depend on the representation!

➤ Principle of data abstraction: Separate usage from representation.


Manas Thakur 12
But now we have a problem!
➤ We have two di erent selectors with the same name:
(define (real-part z) (car z))

(define (real-part z) (* (magnitude z) (cos (angle z))))

➤ Which one should be called?


(define (add-complex z1 z2)
(make-from-real-imag (+ (real-part z1) (real-part z2))
(+ (imag-part z1) (imag-part z2))))

➤ Topic for the next class!

Manas Thakur 13
ff
Quiz 1
➤ When: Tomorrow, August 22, 8:30-9:15 AM
➤ Syllabus: Everything until Today, August 21
➤ Last year’s QP uploaded, but no guarantee that the pattern would be
similar (except that it would be a short exam to test progress)
➤ Closed book, closed smart devices (bring ID cards)
➤ Venue:
➤ LH 302: 21*, 22B*, 23B0901--23B0968
➤ LH 301: 23B0969--23B1039
➤ LH 101: 23B1040--23B1085, 24*, 25*
➤ Quiz 2 date also available on the course webpage

Manas Thakur 14
CS339: Abstractions and Paradigms for Programming
Tagging and Message Passing

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recall the problem we discussed in the last class
➤ We had two representations for complex numbers (rectangular and polar).
➤ Each representation seemed more natural for certain operations
(rectangular for addition/subtraction, and polar for multiplication/
division).
➤ But name-con ict issues in our scheme forced only one to be used at a
time.
➤ Can we use both the representations together?

Manas Thakur 2
fl
Tagging
➤ Same procedure names for di erent representations.
➤ Easy solution: Rename the procedures; su x ‘-rectangular’ with rectangular
procedures and ‘-polar’ with polar procedures.
➤ A bigger problem is that both rectangular and polar complex numbers may be
roaming around in an indistinguishable manner.
➤ Let us introduce a mechanism to distinguish rectangular and polar representations
of complex numbers.
➤ Tag data items:
➤ Rectangular data with ‘rectangular
➤ Polar data with ‘polar

Manas Thakur 3
ff
ffi
Attaching tags
➤ How do we add a tag to a complex number?
➤ cons the tag with the existing pair!
(define (attach-tag type-tag contents)
(cons type-tag contents))

(define (type-tag datum) (car datum))

(define (contents datum) (cdr datum))


➤ How could we nd if a given complex number representation is rectangular
or polar?
(define (rectangular? z) (eq? (type-tag z) ‘rectangular))

(define (polar? z) (eq? (type-tag z) ‘polar))

Manas Thakur 4
fi
Revised rectangular implementation
➤ Tags attached in the constructors:

➤ Name con icts resolved by renaming:

Manas Thakur 5
fl
Similarly, the revised polar representation

Manas Thakur 6
Tagging solves the problem
➤ Now we can decide which procedure to call using tags:

➤ Basically, we are dispatching a procedure based on the type-tag associated


with the data object.
➤ Similar changes can be made for other con icting procedures.
➤ Both representations can co-exist peacefully.

Manas Thakur 7
fl
Abstractions in our complex number library

Manas Thakur 8
Is there anything bad in our scheme?
➤ What if I want to add 10 more representations?
➤ De ne new make-* procedures and selectors while making sure that the
names don’t match — umm, painful but obvious.
➤ De ne a new tag for each representation — looks ne.
➤ What about the generic procedures?

➤ Need to add a case for each representation in each generic procedure — ouch!

Manas Thakur 9
fi
fi
fi
Message Passing
➤ Instead of making operations intelligent (read bothersome), make the data
objects intelligent:

➤ What does make-from-real-imag return?


➤ A procedure that takes an argument ‘op’ to decide what computation to perform.
➤ And why is it so interesting? The picture is not yet over!

Manas Thakur 10
Message Passing (Cont.)
➤ Let us change the generic procedures as follows:

where:
(define (apply-generic op arg) (arg op))

➤ Thus, our make-* procedure returns another procedure representing an


object that dispatches the correct procedure based on the message passed to
the object (receiver).

Manas Thakur 11
Putting it all together
(define (make-from-mag-ang x y)
(lambda (op)
(cond ((eq? op ‘magnitude) x)
((eq? op ‘angle) y)
((eq? op ‘real-part …
…)))

(define n1 (make-from-real-imag 2 3))


(define n2 (make-from-real-imag 3 4))
(define n3 (add-complex n1 n2))

The right procedures will


(define (apply-generic op arg) (arg op))
be called automagically
(define (add-complex z1 z2) based on which dispatch
(make-from-real-imag (+ (real-part z1) (real-part z2)) procedure lies inside ‘z’!
(+ (imag-part z1) (imag-part z2))))

Manas Thakur 12
Back to Object-Oriented Programming
➤ Each OOL provides a mechanism to abstract out objects belonging to a
certain kind, such that:
➤ the object encapsulates constituent data items as elds
➤ and the procedures to operate on objects as methods

➤ You create objects and assign values to their elds using constructors, and
dispatch methods by passing messages to receivers.

Manas Thakur 13
fi
fi
(define (make-rat x y)
(lambda (which)
(if (= which 0) x y)))

(define (numer n) (n 0))


(define (denom n) (n 1)) Abra-ca-dabra!
(define (mult-rat n1 n2)
(make-rat (* (numer n1) (numer n2))
(* (denom n1) (denom n2))))

(define n1 (make-rat 2 3)) (define (Rational x y)


(define n2 (make-rat 3 4)) (lambda (msg)
(define n3 (mult-rat n1 n2)) (cond ((eq? msg ‘numer) x)
((eq? msg ‘denom) y)
((eq? msg ‘mult-rat)
(lambda (other)
(Rational (* x (other ‘numer))
(* y (other ‘denom))))))))

(define n1 (Rational 2 3))


(define n2 (Rational 3 4))
(define n3 ((n1 ‘mult-rat) n2))

Manas Thakur 14
Now ain’t they similar?
(define (Rational x y)
(lambda (msg)
Notice we got both:
(cond ((eq? msg ‘numer) x) •Packaging
•Dispatch
((eq? msg ‘denom) y)
((eq? msg ‘mult-rat)
(lambda (other)
(Rational (* x (other ‘numer)) class Rational {
(* y (other ‘denom)))))))) int x; int y;
Rational(int x, int y) {
(define n1 (Rational 2 3)) this.x = x; this.y = y;
(define n2 (Rational 3 4)) }
(define n3 ((n1 ‘mult-rat) n2)) int numer() { return x; }
int denom() { return y; }
Rational mult-rat(Rational other) {
return new Rational(
[Link]() * [Link](),
•Preserve this class to understand the crux of }
[Link]() * [Link]());

some of the fundamentals of the OO paradigm. }


Rational n1 = new Rational(2,3);
Rational n2 = new Rational(3,4);
Rational n3 = [Link]-rat(n2);

Manas Thakur 15
CS339: Abstractions and Paradigms for Programming
Imperative Programming

Manas Thakur
CSE, IIT Bombay

Autumn 2025
An objective world…
➤ The world consists of independent objects, whose behaviour changes over
time.
➤ The changing behaviour can be captured using a snapshot of the state of
individual objects.
➤ The objects interact with each other and in uence each other’s states.
➤ In programming terms, the state of an object is modelled using local state
variables.
➤ We next need to see how could we model such time-varying states.

Manas Thakur 2
fl
A bank account
➤ Classwork:
➤ Write a function to ‘withdraw’ a given amount if an account has
enough balance.
➤ Another function should simulate the following over an account:
➤ Initial balance: 100
➤ Amounts to withdraw: 20, 90, 30

Manas Thakur 3
Attempt 1

➤ Output: “Insu cient balance”

Manas Thakur 4
ffi
Attempt 2

Problem: We need to maintain the


balance of each account explicitly
while writing our operations.
➤ Output: 50
➤ What if we had two accounts? 1000 accounts?

Manas Thakur 5
What we want
➤ An account that maintains its balance by itself.
➤ Users should not have to “remember” the balance of each account and
supply it to the withdraw procedure.

➤ What does it need:


➤ A way to remember the balance based on the history of transactions.
➤ Which needs a way to update the balance after each transaction.
➤ Which needs mutation!

Manas Thakur 6
Mutations, assignments, states
➤ Scheme provides a special form set! to update the value of a variable:
➤ set! <name> <new-value>
Pronounced as
“set bang”
➤ Traditionally, this is called an assignment.

➤ Mutations enabled by assignments lead to the notion of a state after each


assignment.

Manas Thakur 7
Imperative Programming
➤ An assignment updates the state.
➤ A sequence of assignments leads to a sequence of states.
➤ Hence, assignments are statements (against expressions).
➤ Each assignment is like a command to change the state.
➤ (Apple Dictionary) Imperative. giving an authoritative command.
➤ The practice of giving a sequence of commands to update states, in the
form of assignment statements, de nes the paradigm of imperative
programming.

Manas Thakur 8
fi
Account withdrawals in an imperative world

➤ Notice the begin keyword for sequencing multiple statements


➤ PC Question:
➤ Can you recall a place where we have used ‘begin’ implicitly?
➤ Answer: What about multiple de nes in a procedure?

Manas Thakur 9
fi
Writing “withdrawal processors”
(define W1 (make-withdraw 100))
(define W2 (make-withdraw 100))

> (W1 50)

50

> (W2 70)

30

> (W2 40)

“Insufficient funds” Multiple accounts without any hassle!

> (W1 40)

10

Manas Thakur 10
But an account can also get money in!

A pakka object-oriented account :-)

Manas Thakur 11
Pros of Assignments

➤ Ability to model the real world in terms of objects with local state

➤ Proper modularity: independence of objects and users

➤ Simpler bank account code!

Manas Thakur 12
Cons of Assignments
➤ Our nice, simple substitution model of procedure application goes away for
a toss!
➤ Consider the following two procedures:

Imperative Functional

Manas Thakur 13
Con 1: Good-bye substitution model
((make-decrementer 25) 20)

==> ((lambda (amount) (- 25 amount)) 20)

==> (- 25 20)

==> 5

((make-simplified-withdraw 25) 20)

==> ((lambda (amount) (set! balance (- 25 amount) 25) 20)

==> (set! balance (- 25 20)) 25

==> Set balance to 5 and return 25

Consequence: Simple substitution may lead to unexpected results.


Manas Thakur 14
Con 2a: Bye-bye referential transparency
Are D1 and D2 the same?
> Arguably yes, because each could be
substituted for the other.
(define D1 (make-decrementer 25))
(define D2 (make-decrementer 25))
Are W1 and W2 the same?
> They look to be, but aren’t:
(W1 20)
5
(W1 20)
-15 (define W1 (make-withdraw 25))
(W2 20) (define W2 (make-withdraw 25))
5

Consequence: Can’t reason about correctness by “looking” at the program.

Manas Thakur 15
Con 2b: Identity crisis
➤ Two bank accounts are di erent even if they have the same balance.
➤ A bank account remains to be the same even if its constituent data items or
elds (i.e., the balance) changes.
➤ A rational number 2/3 is not the same if its constituent data items (either
the numerator or the denominator) change.
➤ What’s the identity of a bank account then?

Consequence: Extra e orts required for implementing ‘equals’ methods!

Manas Thakur 16
fi
ff
ff
Con 3: Inducing order into life
➤ Consider this ‘imperative’ factorial program:

Next class:
A substitution for substitution!

➤ What if we wrote or performed the assignments in opposite order:

Consequence: Di cult optimization/parallelization.


Manas Thakur 17
ffi
CS339: Abstractions and Paradigms for Programming
Environment Model

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Problems brought by assignments
(define count 1) > (f 2)
(define (f v) > 4
(set! count (+ 1 count)) > (f 2)
(+ v count)) > 5

➤ Procedures are no longer mathematical functions!

((make-simplified-withdraw 25) 20)


==> ((lambda (amount) (set! balance (- 25 amount) 25) 20)
==> (set! balance (- 25 20)) 25
==> Set balance to 5 and return 25

➤ Substitution model of procedure application no longer works!


➤ Reason: Variables are no longer simply names for expressions!

Manas Thakur 2
Substitution model revisited
➤ Evaluating a combination:
➤ Evaluate the subexpressions of the combination.
➤ Apply the value of the operator subexpression to the values of the operand
subexpressions.

➤ Applying a procedure:
➤ To apply a compound procedure to arguments, evaluate the body of the
procedure with each formal parameter replaced by the corresponding actual
argument.

Manas Thakur 3
Bindings and scope revisited
This lambda This lambda
binds ‘x’ binds ‘y’

(lambda (x) (lambda (y) (* x y))

Scope of y
Scope of x

(lambda (x) (lambda (y) (* x (+ z y)))

x and y are bound;


z is free

The only way to create a binding is via a lambda!

Manas Thakur 4
Lambda — The Ultimate Binder
(let ((var1 expr1) ((lambda (var1 var2)
(var2 expr2)) <body>)
<body>) expr1 expr2)

(define foo
(define (foo x)
(lambda (x)
<body>)
<body>))

(define (foo x) (define (foo x)


(define (var expr)) (let ((var expr))
<body>) <body>))

Manas Thakur 5
The Environment Model

Manas Thakur 6
Frames and environments

I, II, III: Frames A, B, C, D: Environments

Environment ‘A’: Environment ‘B’:


•x: 7, y: 5, z: 6 •x: 3, y: 2, m: 1
•m: unbound Environments as chains of frames
•z: unbound

Environments ‘C’, ‘D’:


•x: 3, y: 5
•z, m: unbound

Manas Thakur 7
Evaluating procedure de initions
(define (square x) (define square
(* x x)) (lambda (x) (* x x)))

Manas Thakur 8
f
Evaluating procedure applications
(define (square x)
(* x x))

> (square 5)

Manas Thakur 9
The Environment Model: Summary
➤ Evaluating a combination: Same as the substitution model
➤ Evaluate the subexpressions of the combination.
➤ Apply the value of the operator subexpression to the values of the operand
subexpressions.
➤ Evaluating a procedure de nition:
➤ Create a procedure object by evaluating a lambda-expression relative to a given
environment.
➤ Evaluating a procedure application:
➤ Create a new environment containing a frame that binds the parameters to the
arguments, and then evaluate the body of the procedure in the new environment.

Manas Thakur 10
fi
Extended example 1: De inition

(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))

Manas Thakur 11
f
Extended example 1: Application

(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))

> (f 5)

Manas Thakur 12
EE2 Def: Local states in frames

(define (make-withdraw balance)


(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
“insufficient funds”)))

Manas Thakur 13
EE2 App1

(define W1 (make-withdraw 100))

Manas Thakur 14
EE2 App1 (Cont.)

> (W1 50)

Manas Thakur 15
EE2 App1 (Cont.)

After (W1 50)

Manas Thakur 16
EE2 App2

(define W2 (make-withdraw 100))

Manas Thakur 17
Internal De initions

> (sqrt 2)

Manas Thakur 18
f
CS339: Abstractions and Paradigms for Programming
OO: Visitor Pattern

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recap: Java Primer
➤ Variables and methods can be de ned only inside classes
➤ Classes can be instantiated to create objects
➤ A class can extend another class and inherit its elds
➤ A class can extend another class and override its methods
➤ Parent class references can point to child class objects, but not vice versa
➤ A class can extend only one class but implement any number of interfaces
➤ All methods are virtual by default — resolved based on the “receiver” object
➤ Java class libraries provide useful data-structure implementations such as Vector,
ArrayList, HashSet, HashMap, etc.

Manas Thakur 2
fi
fi
Let’s look at an OO problem
➤ Goal: We want to sum the elements of an integer linked list:

Manas Thakur 3
Approach 1: Instance checks and Typecasts

➤ Bad: Typecasts and instanceof checks are ugly (and expensive).


➤ Good: No need to touch Nil and Cons (abstraction principles respected).

Manas Thakur 4
Approach 2: Utilize OO style

➤ Good: No typecasts and instanceofs.


➤ Bad: Original classes to be recompiled for each new operation.

Manas Thakur 5
Approach 3: Visitor Pattern
➤ Divide code into an object structure and a Visitor.
➤ Insert an accept method in each structure class. Each accept method takes a
Visitor as an argument.
➤ A Visitor contains a visit method for each structure class.

Manas Thakur 6
Approach 3: Visitor Pattern
➤ The purpose of accept methods is to invoke the visit method in the Visitor
that can handle the current object.

Manas Thakur 7
Approach 3: Visitor Pattern
➤ The control ows back and forth between the visit methods in the Visitor and
the accept methods in the object structure.

Manas Thakur 8
fl
Visitor Pattern: Everything together

Several compilers use


visitor pattern to perform
syntax-directed translation.

Manas Thakur 9
For some other day
➤ There are several more interesting OO patterns that help solve problems elegantly
➤ Particularly notable ones ( nd out as a homework):
➤ Strategy Pattern
➤ Decorator Pattern
➤ Factory-method Pattern
➤ Learn more (not in this course):

Manas Thakur 10
fi
CS339: Abstractions and Paradigms for Programming
Sequences as Conventional Interfaces

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Let’s get back to lists
➤ Our lists look very similar to trees:
(list (list 1 2) 3 4)

➤ What’s the corresponding tree?

Manas Thakur 2
Sum the squares of the odd leaves of a tree

List the even ibonacci numbers till ib(n)


Is there any similarity between
these two programs?

Manas Thakur 3
f
f
Recall our higher order list procedures
(define (map f l)
(if (null? l)
‘()
(cons (f (car l)) (define (filter pred l)
(map f (cdr l))))) (cond ((null? l) ‘())
((pred (car l))
> (define l ‘(1 2 3 4)) (cons (car l)
> (map (lambda (x) (* x x)) l) (filter pred (cdr l)))
> (1 4 9 16) (else (filter pred (cdr l)))))

(define (accumulate op v l) > (filter odd? ‘(1 2 3 4 5))


(if (null? l) > (1 3 5)
v
(op (car l)
(accumulate op v (cdr l)))))

> (accumulate + 0 ‘(1 2 3 4)) Same as foldr


> 10
Manas Thakur 4
Sum the squares of the odd leaves of a tree

List the even ibonacci numbers till ib(n)

Manas Thakur 5
f
f
Sum the squares of the odd leaves of a tree

Don’t match!

Manas Thakur 6
List the even ibonacci numbers till ib(n)

Don’t match again!

Manas Thakur 7
f
f
Enumerators
(define (enumerate-interval low high)
(if (> low high)
‘()
(cons low (enumerate-interval (+ low 1) high))))

> (enumerate-interval 1 5)
> (1 2 3 4 5)

(define (enumerate-tree tree)


(cond ((null? tree) ‘())
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))

> (enumerate-tree (list 1 (list 2 (list 3 4)) 5))


> (1 2 3 4 5)

Manas Thakur 8
Using Sequences as Interfaces

Manas Thakur 9
Sum the squares of the odd leaves of a tree

Aha!

List the even ibonacci numbers till ib(n)

Manas Thakur 10
f
f
A PL for specifying computations using sequences
➤ What constitutes a programming language?
➤ Primitive expressions; means of combination; means of abstraction
➤ We have created a new programming language that de nes
sequences as conventional interfaces:
➤ Sequences (implemented using lists) are like signals owing from one stage to
another
➤ map, filter and accumulate (the new vocabulary of our language) represent
general patterns of processing sequences
➤ Advantage: Largely decoupled modules de ned in terms of operations on
sequences
➤ Key to reducing complexity of large software

Manas Thakur 11
fi
fl
fi
Modularity in full glow
➤ List the squares of the rst n+1 bonacci numbers:

➤ Multiply the squares of the odd integers in a sequence:

➤ Can you see a BIG problem with our elegant modular programs?

Manas Thakur 12
fi
fi
Checking if a number is prime
➤ Find the smallest divisor (>1) of a given number n
➤ If the divisor is the same as n, then n is prime
(define (smallest-divisor n) (find-divisor n 2))

(define (find-divisor n test-divisor)


(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))

(define (square x) (* x x))

(define (divides? a b) (= (remainder b a) 0))

(define (prime? n)
(= n (smallest-divisor n)))

Manas Thakur 13
Add all prime numbers in an interval
➤ Mixed up non-modular program:

➤ Nicely separable modular program:

Manas Thakur 14
Second prime between 10000 and 1000000
Very very ine cient!
(car (cdr (filter prime?
(enumerate-interval 10000 1000000))))

➤ Where are we headed next?


➤ A way to make our nice and elegant modular programs as e cient as their
non-modular counterparts!
➤ A way to model the real world without the cons of assignments!
➤ A world in which substitution model still works!
➤ A new way of viewing life!

➤Stream processing.
Manas Thakur 15
ffi
ffi
CS339: Abstractions and Paradigms for Programming
Lazy Typed Programming with Haskell

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Case Study on Typed Functional Programming
➤ Recall this slide?

Manas Thakur 2
What’s a function?
➤ A map from values in a domain D to values in a range R.
f :: D -> R

➤ Examples:
sin :: Float -> Float
age :: Person -> Int
add :: (Integer,Integer) -> Integer

➤ Applying a function:
f(x)
sin θ OR sin(θ)?
Haskell: f x

Manas Thakur 3
Function composition
f :: Y -> Z g :: X -> Y

➤ What is f . g?

f . g :: X -> Z

(f . g) x :: f (g x)

➤ All this is Haskell code!

Manas Thakur 4
[Link]

For times when getting


things right is important!

Manas Thakur 5
➤ Scheme: Explore ideas freely (we would come back to this once more)
➤ Java: Organize large programs in disciplined ways
➤ Haskell: Combine mathematical clarity with safety and abstraction

➤ Scheme: Structure programs around lists and functions


➤ Java: Structure programs around classes and objects
➤ Haskell: Structure programs around algebraic data types

Manas Thakur 6
Common Words

the: 154
of : 50
a : 18
and: 12
in : 11


n entries

Manas Thakur 7
Type declaration
➤ What’s the type of input?
➤ An integer
Int, [Char]
➤ A list of characters
➤ What’s the type of output?
[Char]
➤ List of characters again!
➤ What’s the type of commonWords?
commonWords :: Int -> ([Char] -> [Char])

Functions in Haskell are curried!

Manas Thakur 8
Possible algorithm
➤ Get words from text

➤ Count occurrence of each word

➤ Sort in decreasing order

➤ Display

Manas Thakur 9
Type synonyms
➤ Get words from text:

words :: [Char] -> [[Char]]

type Text = [Char]


words :: Text -> [Word]
type Word = [Char]

Manas Thakur 10
Higher order functions
➤ Should “The” and “the” be counted as same or di erent?
➤ Solution: Convert each letter to lowercase!
➤ Can you identify a function just by looking at its type signature?

What? :: (a -> b) -> [a] -> [b]

map :: (a -> b) -> [a] -> [b]

map toLower :: Text -> Text

Currying again!

Manas Thakur 11

ff
Sort out all your problems
➤ Count occurrence of each word
➤ Sort rst?
sortWords [“to”, “be”, “or”, “not”, “to”, “be”]
= [“be”, “be”, “not”, “or”, “to”, “to”]

sortWords :: [Word] -> [Word]

➤ Count adjacent runs of each word:


countRuns [“be”, “be”, “not”, “or”, “to”, “to”]
= [(2,“be”), (1,“not”), (1,“or”), (2,“to”)]

countRuns :: [Word] -> [(Int,Word)]

Manas Thakur 12
fi
Told you sorting is remarkable
➤ Sort in decreasing order:
sortRuns [(2,“be”), (1,“not”), (1,“or”), (2,“to”)]
= [(2,“be”), (2,“to”), (1,“not”), (1,“or”)]

sortRuns :: [(Int,Word)] -> [(Int,Word)]

➤ But we wanted to take only rst n entries:


take 2 [(2,“be”), (2,“to”), (1,“not”), (1,“or”)]
= [(2,“be”), (2,“to”)]

take :: Int -> [a] -> [a]

Reminder: Every function is curried by default in Haskell!

Manas Thakur 13
fi
I only want to see the output
String is a
showRun (2,”be”)
type synonym
= “be: 2\n”
for [Char].

showRun :: (Int,Word) -> String

➤ We want to do this for each entry in the list:

map showRun :: [(Int,Word)] -> [String]

➤ Finally, concatenate the list of Strings into one:

concat :: [[a]] -> [a]

Manas Thakur 14
Common Words in a Nutshell
commonWords :: Int -> Text -> String
commonWords n = concat . map showRun . take n .
sortRuns . countRuns . sortWords .
words . map toLower

➤ This is how typical Haskell programs look!!


➤ Mathematics is de nitely something you didn’t study unnecessarily in
Homework: school!
➤ But none of those functions was de ned?
➤ Some of them are prede ned (concat, words, take, map, toLower).
➤ Rest (showRun, sortRuns, countRuns, sortWords) we will de ne.
➤ Does the implementation matter much? :-)

Manas Thakur 15
fi
fi
fi
fi
CS339: Abstractions and Paradigms for Programming
Lists in Haskell

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recall the Common Words program
commonWords :: Int -> Text -> String
commonWords n = concat . map showRun . take n .
sortRuns . countRuns . sortWords .
words . map toLower

➤ We are yet to de ne showRun, sortRuns, countRuns and sortWords.


➤ showRun is easy:
showRun :: (Int, Word) -> String
showRun (n,w) = w ++ “: ” ++
show n ++ “\n”

➤ The remaining will allow us learn some more Haskell.

Manas Thakur 2
fi
Pattern Matching in Haskell
➤ Lists in Haskell are denoted using square brackets: [1,2,3]
➤ Empty list (our favorite base case) is just empty brackets: []
➤ cons in Haskell is denoted using :
x:xs, where x is car and xs is cdr

cons is non-strict in both arguments.


➤ Check if a list is empty:
null :: [a] -> Bool null :: [a] -> Bool
null [] = True OR null [] = True
null (x:xs) = False null _ = False

Manas Thakur 3
An example is better than 10 de initions
➤ We can de ne map as follows:
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs

➤ What about lter: We can bind variables, and use them.

filter :: (a -> Bool) -> [a] -> [a]


filter p [] = []
filter p (x:xs) = if p x
then x : filter p xs
else filter p xs

➤ Notice the indentation below the if.

Manas Thakur 4
fi
fi
f
Lists: The omnipresent data structure in APP

Manas Thakur 5
List Enumerations
•[1..10]
•[1,2,3,4,5,6,7,8,9,10] •[‘a’..‘z’]
•[1..] •[a,b,c,d,e,f,g,h,i,j,k,
l,m,n,o,p,q,r,s,t,u,v,w
•[1,2,3,4 {C-c} ,x,y,z]

•[0,2..11] •Smart enough, but not so much!


•[0,2,4,6,8,10] •[20..1] vs [20,19..1]

•[1,3..] •[1,2,4,8..100] won’t work


•[1,3,5,7 {C-c}

Manas Thakur 6
Some cool things with enumerations

•Get rst 24 multiples of 13: •cycle [1,2,3]

•[13,26..24*13] •Hang!
•Cooler: •take 10 (cycle [1,2,3])

•take 24 [13,26..] •[1,2,3,1,2,3,1,2,3,1]


•Why does it work? •take 11 (cycle “LOL ”)

•Lazy evaluation! •“LOL LOL LOL”

Manas Thakur 7
fi
List Comprehensions
➤ Describe the set of rst 10 even natural numbers:
➤ Math: S = {x*2 | x ∈ N, x ≤ 10}
Told you they are close!
➤ Haskell: [x*2 | x <- [1..10]]

➤ Elements between 1 and 10 which when doubled are greater than 12 but
less than 80 when multiplied by 3:
➤ [x | x <- [1..10], x*2 > 12, x*3 < 80]

Manas Thakur 8
fi
Cooler things with list comprehensions
•Prime numbers between 1 and 100:
•map:
•[x | x <- [1..100],
isPrime x] •map f xs = [f x | x <- xs]

•First 100 prime numbers: • lter:

•take 100 [x | x <- [1..], •filter p xs = [x | x <- xs,


isPrime x] p x]
Order order
•All iteration vectors for summing two •concat:
nxn matrices:
•concat xss = [x | xs <- xss,
•[(i,j) | i <- [1..n], x <- xs]
j <- [1..n]] Notice the nested loops here!

Manas Thakur 9
fi
Back to Common Words
➤ Here is a possible de nition of countRuns:
countRuns :: [Word] -> [(Int,Word)]
countRuns [] = []
countRuns (w:ws) = (1+length us,w) : countRuns vs
where (us,vs) = span (==w) ws

length :: [a] -> Int


length [] = 0
length (_:xs) = 1 + length xs

span :: (a -> Bool) -> [a] -> ([a], [a])


span p [] = ([], [])
span p (x:xs) = if p x then (x:ys,zs)
else ([],x:xs)
where (ys,zs) = span p xs

Manas Thakur 10
fi
Guards in Haskell
➤ Now we’re left with sortWords and sortRuns, but let’s rst de ne a
merge sort:
sort [] = []
sort [x] = [x]
sort xs = merge (sort ys) (sort zs)
where (ys,zs) = half xs

half xs = (take n xs, drop n xs)


where n = length xs `div` 2

merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
| x <= y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys

Manas Thakur 11

fi
fi
It’s an easy inisher now

sortWords :: [Word] -> [Word]


sortWords = sort

sortRuns :: [(Int,Word)] -> [(Int,Word)]


sortRuns = reverse . sort

commonWords :: Int -> Text -> String


commonWords n = concat . map showRun . take n .
sortRuns . countRuns . sortWords .
words . map toLower

Manas Thakur 12
f
Summary of case constructs in Haskell
Next class:
Typed Programming with Haskell
➤ If-then-else
➤ Guards
➤ Pattern matching
➤ Case analysis
➤ There are even more :p
Syntactic sugar at its best!
head xs = case xs of
[] -> error “Empty list”
(x:_) -> x

Manas Thakur 13
CS339: Abstractions and Paradigms for Programming
ADTs and Type Classes

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Types Types Types
➤ What is a type? Duck typing!
If it walks like a duck
➤ What is it useful for?
and it quacks like a duck,
➤ What is type checking? then it must be a duck.
➤ Difference between a strongly and a weakly typed language?
➤ Why shouldn’t we always use the most general type?
➤ Static vs dynamic typing?
➤ Untyped languages?
➤ Haskell tries to offer the best of all worlds: Type inference!

Manas Thakur 2
Algebraic Data Types (ADTs)
➤ Types can either be combinations as well as choices.
➤ Definition of Bool in Haskell:
data Bool = False | True

Data declaration Data constructors

➤ This is a sum type; it presents two choices for constructing a Bool value.

Manas Thakur 3
Examples of ADTs
data Move = North | South | East | West Named constructors

➤ What are constructors in OO languages?


➤ Check the types of North, South, East and West.
➤ Now we can use these types in functions:

type Pos = (Int,Int)


move :: Move -> Pos -> Pos
moves :: [Move] -> Pos -> Pos
move North (x,y) = (x,y+1)
moves [] p = p
move South (x,y) = (x,y-1)
moves (m:ms) p = moves ms (move m p)
move East (x,y) = (x+1,y)
move West (x,y) = (x-1,y)

Manas Thakur 4
Algebraic Data Types (ADTs)
➤ Types can either be combinations as well as choices.
➤ Definition of a Point data type:
data Point = Point Int Int

➤ This is a product type; it is constructed by combining two Int values.


➤ We can also use sum and product characteristics together:
data Shape = Circle Float
| Rectangle Float Float

➤ Do you understand the term algebraic data types now?

Manas Thakur 5
Pattern Matching with Types
data Shape = Circle Float
| Rectangle Float Float

➤ These constructors are essentially functions, which return a value of type Shape.
Check their types again!
➤ We can again define functions using them:

Notice area :: Shape -> Float


area (Circle r) = pi * r * r
pattern matching! area (Rectangle ...

➤ Why won’t area :: Circle -> Float work?


➤ Same reason why foo :: True -> Int won’t work!

Manas Thakur 6
Type Parameters
data Maybe a = Nothing | Just a

Type parameter
➤ Maybe is a very useful type for handling failures.

head :: [a] -> a safeHead :: [a] -> Maybe a


head [] = error “Empty list” safeHead [] = Nothing
head (x:_) = x safeHead (x:_) = Just x

Can’t return this!


safediv :: Int -> Int -> Maybe Int
safediv _ 0 = Nothing
safediv m n = Just (m `div` n)

Manas Thakur 7
CS339: Abstractions and Paradigms for Programming
ADTs and Type Classes (Cont.)

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Recap
➤ We learnt how could we create new algebraic data types in Haskell:
data Shape = Circle Float | Rectangle Float Float

➤ Pattern matching helps de ne operations:


area :: Shape -> Float
area (Circle r) = pi * r * r
area (Rectangle ...

➤ We also learnt about type parameters that allow us to supply any type later:
data Maybe a = Nothing | Just a

➤ Question: Can we create recursive types?

Manas Thakur 2
fi
Recursive Types
➤ What does this type describe?
data Nat = Zero | Succ Nat

➤ Does this remind you of something from pre-Quiz1 time?


➤ Church numerals!

nat2int :: Nat -> Int


nat2int Zero = 0
nat2int (Succ n) = 1 + nat2int n

add :: Nat -> Nat -> Nat


add m n = int2nat (nat2int m + nat2int n)

Classwork

Manas Thakur 3
Type Classes
➤ What’s the problem with the following types for +:
(+) :: Int -> Int -> Int
Too speci c (+) :: Float -> Float -> Float
Too generic (+) :: a -> a -> a

➤ We would like to de ne (+) for only number-like types.


➤ Check the type of Haskell’s built-in sort function:
> import [Link]
> :t sort
> sort :: Ord a => [a] -> [a]

➤ It sorts lists containing elements of any type a that belongs to the type
class Ord (“Ord a” being a type constraint).

Manas Thakur 4
fi
fi
Type Classes (Cont.)
➤ Check the type of + now:
> :t (+)
> (+) :: Num a => a -> a -> a
a is a type variable

➤ Here is the declaration of the type class Num from Haskell:


class (Eq a, Show a) => Num a where
(+),(-),(*) :: a -> a -> a
negate :: a -> a
abs, signum :: a -> a
fromInteger :: Integer -> a

➤ Class Num is a subclass of type classes Eq and Show.


➤ Any subclass of Num would need to de ne (+), (-), negate, etc.
Manas Thakur 5
fi
Instantiating Type Classes
➤ A type can be made an instance of a type class by instantiating that class:
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)

➤ Now any type can instantiate Eq by de ning == (/= is already de ned):

data Bool = False | True


instance Eq Bool where
False == False = True
True == True = True
_ == _ = False

Manas Thakur 6
fi
fi
Deriving from Type Classes
data Student = Student { name :: String,
rollNum :: Int }

➤ We can create named values as follows:


stud = Student { name = “harry”, rollNum = 123 }

➤ But we can’t “show” stud yet! (Notice the error.)


➤ Derive from something that knows how to show:
data Student = Student { name :: String,
rollNum :: Int
} deriving Show

➤ Now typing stud on the prompt works!

Manas Thakur 7
Deriving from Type Classes
➤ Could we have done the following instead?
data Student = Student { name :: String,
rollNum :: Int }
instance Show Student

➤ The “deriving” version generates a de nition; the “instance” one does not.
➤ The following would work:
data Student = Student { name :: String,
rollNum :: Int }
instance Show Student where
show (Student n r) = "Student { name = " ++ show n ++
", rollNum = " ++ show r ++ " }"

Manas Thakur 8
fi
The Expression Problem
➤ ADTs: The Functional Style
➤ Adding new operations (functions) is easy, particularly with pattern matching
➤ Adding new types is di cult (have to modify the top-level data declaration —
breaks abstraction barrier)
➤ Typeclasses: The OO Style
➤ Adding new types (classes) is easy — just create a new instance
➤ Adding new operations is di cult (have to modify the top-level typeclass —
breaks abstraction barrier)
➤ Typeclasses are like Java interfaces (Haskell has everything!)

Manas Thakur 9
ffi
ffi
Refinement Types for Haskell

Niki Vazou
University of Maryland
Software bugs are everywhere

Airbus A400M crashed due to a software bug.


— May 2015
Software bugs are everywhere

The Heartbleed Bug.


Buffer overflow in OpenSSL. 2015
Make bugs difficult to express

Using Modern Programming Languages


F#, Ocaml, Erlang, Scala, Haskell

Because of
Strong Types + λ-Calculus
Make bugs difficult to express

Using Well Typed Programs


Modern Programming Languages
cannot go wrong!
F#, Ocaml, Erlang, Scala, Haskell

Because of
Strong Types + λ-Calculus
VS.
λ> :m +[Link] [Link]
λ> let pack = "hat"

λ> :t takeWord16
takeWord16 :: Text -> Int -> Text

VS.
λ> :m +[Link] [Link]
λ> let pack = "hat"

λ> takeWord16 pack True


Type Error: Cannot match Bool vs Int

VS.
λ> :m +[Link] [Link]
λ> let pack = "hat"

λ> takeWord16 pack 500


“hat\58456\2594\SOH\NUL…

VS.
Valid Values for takeWord16?

takeWord16 :: t:Text -> i:Int -> Text

All Ints

…, -2, -1, 0, 1, 2, 3, …
Valid Values for takeWord16?

takeWord16 :: t:Text -> i:Int -> Text

Valid Ints Invalid Ints

0, 1 … , len t len t + 1, …
Refinement Types

take :: t:Text -> {v:Int | v < len t} -> Text

Valid Ints Invalid Ints

0, 1 … , len t len t + 1, …
Refinement Types
take :: t:Text -> {v:Int | v < len t} -> Text

λ> :m +[Link] [Link]


λ> let pack = "hat"

λ> take pack 500


Refinement Type Error
Refinement Types

OK
Code
Error
Refinement Types

OK
Code Typing Logic SMT
Error

1. Source Code to Type constraints

2. Type Constraints to Verification Condition (VC)

3. Check VC validity with SMT Solver


OK
Code Typing Logic SMT
Error

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8
Code Typing

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}


Code Typing

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}


Code Typing

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}


Code Typing

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}


Code Typing

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}


Typing Logic

Encode Subtyping …

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}

… as Logical VC

len x = 3 => (v = 8) => (v <= len x)


Typing Logic

Encode Subtyping …

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}

… as Logical VC

len x = 3 => (v = 8) => (v <= len x)


Typing Logic

Encode Subtyping …

x:{v|len v = 3} |- {v|v = 8} <: {v|v <= len x}

… as Logical VC

len x = 3 => (v = 8) => (v <= len x)


Logic SMT

len x = 3 => (v = 8) => (v <= len x)


Logic SMT Invalid

len x = 3 => (v = 8) => (v <= len x)


Code Typing Logic SMT Error

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 8

len x = 3 => (v = 8) => (v <= len x)


Code Typing Logic SMT OK

take :: t:Text -> {v | v <= len t} -> Text


heartbleed = let x = "hat"
in take x 2

len x = 3 => (v = 2) => (v <= len x)


OK
Code
Error

Checks valid arguments, under facts.


Static Checks
No Checks Static Checks
Efficiency

Runtime Checks

Safety
No Checks

take :: t:Text -> i:Int -> Text


take i t
= Unsafe.takeWord16 i t
No Checks

take :: t:Text -> i:Int -> Text


take i t
= Unsafe.takeWord16 i t

OK
heartbleed = take "hat" 500
No Checks
take :: t:Text -> i:Int -> Text
take i t
= Unsafe.takeWord16 i t

OK
heartbleed = take "hat" 500

UNSAFE
λ> heartbleed
λ> “hat\58456\2594\SOH\NUL…
Runtime Checks

take :: t:Text -> i:Int -> Text


take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
Runtime Checks

take :: t:Text -> i:Int -> Text


take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”

OK
heartbleed = take "hat" 500
Runtime Checks

take :: t:Text -> i:Int -> Text


take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”

OK
heartbleed = take "hat" 500

SAFE
λ> heartbleed
λ> *** Exception: Out Of Bounds!
Runtime Checks are expensive

take :: t:Text -> i:Int -> Text


take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
Static Checks

take :: t:Text -> i:{i


i <->len
i:Int Textt
t} -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
Static Checks

take :: t:Text -> i:{i


i <->len
i:Int Textt
t} -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
Static Checks

take :: t:Text -> i:Int


i:{i <->len
Textt} -> Text
take i t | i < len t
Unsafe. = error “Out
= Unsafe.takeWord16 i t Of Bounds!”
Static Checks

take :: t:Text -> i:{i


i:Int<->len
Textt} -> Text
take i t | i < len t
= Unsafe.takeWord16
Unsafe. = error “Outi t Of Bounds!”

UNSAFE
heartbleed = take "hat" 500
Static Checks
Safe & Efficient Code!
Friday @13:40

Refinement Types for Theorem Proving


Theorem Proving

fib :: {i:Int|0<=i} -> {v:Int|0<=v}


fib i
| i <= 1 = i
| otherwise = fib (i-1) + fib (i-2)
Theorem Proving

fib :: {i:Int|0<=i} -> {v:Int|i<=v}


fib i
| i <= 1 = i
| otherwise = fib (i-1) + fib (i-2)
Theorem Proving

fib :: {i:Int|0<=i} -> {v:Int|i<=v}


fib i
| i <= 1 = i
| otherwise = fib (i-1) + fib (i-2)

Can we prove theorems about functions?


\forall i. 0 <= i => fib i <= fib (i+1)
Theorem Proving

Can we prove theorems about functions?


Theorem Proving

Express theorems via refinement types.


Express proofs via functions.
Check that functions prove theorems.

Known as
Curry–Howard correspondence
Theorem Proving
fibUp :: i:Nat -> {fib i ≤ fib (i+1)}
fibUp i
| i == 0
= fib 0 <. fib 1
*** QED
| i == 1
= fib 1 <=. fib 1 + fib 0 <=. fib 2
*** QED
| otherwise
= fib i
==. fib (i-1) + fib (i-2)
<=. fib i + fib (i-2) ? fibUp (i-1)
<=. fib i + fib (i-1) ? fibUp (i-2)
<=. fib (i+1)
*** QED
Theorem Proving
fibUp :: i:Nat -> {fib i ≤ fib (i+1)}
fibUp i
| i == 0
= fib 0 <. fib 1
*** QED
| i == 1
= fib 1 <=. fib 1 + fib 0 <=. fib 2
*** QED
| otherwise
= fib i
==. fib (i-1) + fib (i-2)
<=. fib i + fib (i-2) ? fibUp (i-1)
<=. fib i + fib (i-1) ? fibUp (i-2)
<=. fib (i+1)
*** QED
Theorem Proving
fibUp :: i:Nat -> {fib i ≤ fib (i+1)}
fibUp i
| i == 0
= fib 0 <. fib 1
*** QED
| i == 1
= fib 1 <=. fib 1 + fib 0 <=. fib 2
*** QED
| otherwise
= fib i
==. fib (i-1) + fib (i-2)
<=. fib i + fib (i-2) ? fibUp (i-1)
<=. fib i + fib (i-1) ? fibUp (i-2)
<=. fib (i+1)
*** QED
Theorem Proving
fibUp :: i:Nat -> {fib i ≤ fib (i+1)}
fibUp i
| i == 0
= fib 0 <. fib 1
*** QED
| i == 1
= fib 1 <=. fib 1 + fib 0 <=. fib 2
*** QED
| otherwise
= fib i
==. fib (i-1) + fib (i-2)
<=. fib i + fib (i-2) ? fibUp (i-1)
<=. fib i + fib (i-1) ? fibUp (i-2)
<=. fib (i+1)
*** QED
CS339: Abstractions and Paradigms for Programming
Metacircular Evaluator (Cont.)

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Graduate to Post-Graduate
➤ We can use the knowledge gained to experiment with programming
language technology in several ways:
1. Extend the language with new syntactic constructs
➤ What’s the limitation with using macros for this?
2. Explore various design choices in the space of programming languages
3. Design new programming languages themselves!
➤ That even introduce novel paradigms of programming!!

Manas Thakur 2
Exploring design choices:
Eager vs Lazy Evaluation

Manas Thakur 3
Applicative Order and Normal Order
➤ Applicative order: All arguments evaluated before procedure application.
➤ Normal order: Evaluation of arguments delayed until the actual argument
values are needed.
➤ What do we mean by needed?
➤ Application of a primitive procedure
➤ Printing the value on screen What about returns?
➤ As the predicate of a conditional
➤ Value of a procedure operator (recall: we’re rst-class citizens!)

Manas Thakur 4
fi
Example
➤ Applicative order: Error
(define (try a b)
(if (= a 0) 1 b))
➤ Normal order: 1
> (try 0 (/ 1 0))
➤ Scheme?
➤ Another useful example:

(define (unless condition usual-value exceptional-value)


(if condition exceptional-value usual-value)

> (unless (= b 0)
(/ a b)
(begin (display “Exception”) 0))

Manas Thakur 5
More jargon: Strict vs Non-Strict Arguments
➤ If the body of a procedure is entered before evaluating an argument, then
the procedure is non-strict in that argument, and vice-versa.
➤ Purely applicative-order language: All procedures are strict in each
argument.
➤ Purely normal-order language: All compound procedures are non-strict in
each argument, and primitive procedures may be strict or non-strict.
➤ PCQ: Example of a primitive procedure that can be non-strict?
➤ Answer: cons!

Manas Thakur 6
More more jargon: Call by Xyz

➤ Call by value: Evaluate arguments before procedure application.


➤ Applicative order!
➤ Call by name: Delay evaluation until the argument is needed!
➤ Normal order!
➤ What is lazy evaluation then?

Manas Thakur 7
Flashback
(define (foo a)
(define (square x) (* x x))
(define (sum-squares x y)
(foo 5)
(+ (square x) (square y)))
(sum-squares (+ 5 1) (* 5 2))
(sum-squares (+ a 1) (* a 2)))
(+ (square 6) (square 10))
(+ (* 6 6) (* 10 10)) Applicative-order
(+ 36 100)
136
evaluation

Normal-order (foo 5)
(sum-squares (+ 5 1) (* 5 2))
evaluation (+ (square (+ 5 1)) (square (* 5 2)))
(+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
(+ (* 6 6) (* 10 10))
(+ 36 100)
Repeated computation!
136

Manas Thakur 8
Lazy Evaluation
➤ Lazy evaluation is the way to impart e ciency to normal-order evaluation,
or in other words to non-strict parameters.
➤ Normal order + Memoization Last jargon:
The opposite of
➤ Call by need (confusing term, but acceptable) lazy evaluation is
➤ Implementation: eager evaluation.

➤ Instead of evaluation, arguments stored into a thunk.


➤ It’s just an object (aka list in APP!).
➤ Thunks are replaced with the evaluated value on rst use.

Manas Thakur 9
ffi
fi
Creating a thunk
➤ You already told it’s a list!
➤ What’s needed to evaluate (an argument) at a later point of time?
➤ The (right) environment! Which one?
➤ The one where the call-site occurs.
➤ So now we can create a thunk as simply as:

(define (create-thunk exp env)


(list ‘thunk exp env))

➤ With corresponding changes in eval/apply to decide when to create a thunk


and when to evaluate the expression in a thunk.

Manas Thakur 10
Changes in eval
➤ We need an additional layer to actually evaluate the operator argument:

➤ What is the purpose of recursion between actual-eval and eval-thunk?

Manas Thakur 11
Changes in apply
➤ We need to actually evaluate the arguments of primitive procedures and
thunkize (delay) the arguments of compound ones:

Manas Thakur 12
Last one: Conditional
➤ We need to actually evaluate the predicate:

Manas Thakur 13
But that was call by name, not need
➤ Recall memoization: Evaluate thunk on rst use and replace it with the evaluated
value.

➤ We also got rid of the environment!


➤ No change in create-thunk.
➤ Auxiliary procedures set-car! and set-cdr! mutate the car and the cdr of a list,
respectively.

Manas Thakur 14
fi
Laziness in action

Manas Thakur 15
Now Scheme can be magical!

Manas Thakur 16
Streams as lazy lists
➤ Now that we have laziness built in, do streams need to be something special any more?
➤ The only change required: Make cons lazy.
➤ An easy way: Switch to pairs being procedures!
➤ Advantages: (define (cons x y)
➤ No explicit delay/force required. (lambda (m) (m x y)))
(define (car z)
➤ Why were they bad? (z (lambda (p q) p)))
(define (cdr z)
➤ No special stream-* procedures need to be de ned. (z (lambda (p q) q)))
➤ Why is that good?
➤ Even lazier:
➤ Both car and cdr are lazy now!
➤ p.s. Quiz 2 question’s answer should be “True” :-)

Manas Thakur 17
fi
The End is Near
➤ Moving into the last week (Nov 3, 4, 6)
➤ More extensions to the metacircular interpreter
➤ A glimpse into other language paradigms
➤ Lab exam (Nov 3, 2 PM)
➤ Scheme & Haskell
➤ Only auto-graded
➤ Theory exam (Nov 11, 9 AM)
➤ Full syllabus
➤ Sheets to be shown on Nov 24; submit grievances by Nov 25;
all handled by Nov 28; grades uploaded by Dec 1

Manas Thakur 18
CS339: Abstractions and Paradigms for Programming
Metacircular Evaluator (Cont.)

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Expanding features:
Variable number of arguments

Manas Thakur 2
Lexical vs Dynamic Scoping
➤ Do we need a keyword or a symbol?
➤ ?, :, if
➤ How much would we be constraining the programmers as a result of our
choice?
➤ Underscores
if (c1) S1
➤ Can we introduce it unambiguously? else if (c2) S2
else S3
➤ Problems with ambiguity?
➤ Hyphens, if-else chaining, …

Manas Thakur 3
Syntax and semantics for varargs
(define foo
(lambda (x . y)
(+ x (sum-list y))))

(define (sum-list What happens


(lambda (l) without the ‘.’?
(foldr + l 0)))

> (foo 2 5 7 9)

➤ x takes the rst argument


➤ y takes the rest of the arguments
➤ y has to be a list.

Manas Thakur 4
fi
Changes in the interpreter
➤ Standard way of pairing up actual argument values with formal parameter
variables:
(define pair-up
(lambda (vars vals)
(cond
((eq? vars ‘())
(cond ((eq? vals ‘()) ‘())
(else (error “TMA”))))
((eq? vals ‘()) (error “TFA”))
(else
(cons (cons (car vars)
(car vals))
(pair-up (cdr vars)
(cdr vals)))))))

Manas Thakur 5
Changes in the interpreter (Cont.)
➤ Pairing up argument values with parameter variables to support varargs:
(define pair-up
(lambda (vars vals)
(cond
((eq? vars ‘())
(cond ((eq? vals ‘()) ‘())
(else (error “TMA”))))
((symbol? vars)
(cons (cons vars vals) ‘()))
((eq? vals ‘()) (error “TFA”))
(else
(cons (cons (car vars)
(car vals))
(pair-up (cdr vars)
(cdr vals)))))))

Manas Thakur 6
Exploring design choices:
Lexical vs Dynamic Scoping

Manas Thakur 7
Lexical vs Dynamic Scoping
x <- 1
➤ Lexical scoping: Free variables are bound in the
f <- function(a) x + a
environment in which the procedure was de ned (closure). g <- function() {
x <- 2
➤ Dynamic scoping: Free variables are bound the most
f(0)
recently assigned value during program execution. }
g()

➤ Learn more about various scoping considerations (and confusions):

Manas Thakur 8
fi
Discussion

➤ Which one — lexical or dynamic scoping


— is more natural?
➤ Which one should be easier to
implement?
➤ Original Lisp had dynamic scoping!
➤ Then why switch to lexical scoping in
Scheme?

Manas Thakur 9
Flashback
➤ Recall the general sum procedure from the days we learnt about higher
order functions:
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
➤ Usage: (define (sum-cubes a b)
(define (cube x) (expt x 3)) Notice the
(sum cube a inc b))
binding
➤ One more (sum of nth powers):
(define (sum-powers a b n)
(define (nth-power x) (expt x n))
(sum nth-power a inc b))

Manas Thakur 10
Why not Dynamic Scoping?

Manas Thakur 11
Problem with Dynamic Scoping
➤ Say the programmer renamed b in the procedure sum to n:
(define (sum term a next n)
(if (> a n)
0
(+ (term a)
(sum term (next a) next n))))

(define (sum-powers a b n)
(define (nth-power x) (expt x n))
(sum nth-power a inc b))

➤ Oops! Now we have a problem.


n got bound to b

➤ We broke a core abstraction barrier (that procedures could rename parameters)

Manas Thakur 12
Then why Dynamic Scoping?

Manas Thakur 13
Classwork
(define (sum-powers a b n)
(define (nth-power x) (expt x n))
➤ Write an analogous procedure to (sum nth-power a inc b))
multiply the nth powers.
(define (product-powers a b n)
(define (nth-power x) (expt x n))
(product nth-power a inc b))
(define (sum-powers a b n)
(sum nth-power a inc b))
➤ Can we abstract out nth-power?
(define (product-powers a b n)
➤ Lexical scoping: No. (product nth-power a inc b))

➤ Dynamic scoping: Yes! (define (nth-power x)


(expt x n))

Manas Thakur 14
Now comes fun!

Manas Thakur 15
Make Scheme Dynamically Scoped
➤ Remarkably simple: Just extend the correct environment!

In fact, we won’t need to bind the environment while creating procedure objects!

Manas Thakur 16
Scoping in Action

Manas Thakur 17
Insight of the Week (Semester!)
➤ How do we abstract out nth-power with lexical scoping?
(define make-exp By learning
(lambda (n)
(lambda (x) (expt x n)))) the APP course :-)

(define (sum-powers a b n) (define (product-powers a b n)


(sum (make-exp n) a inc b)) (product (make-exp n) a inc b))

➤ What’s the powerful feature that allowed you to solve this problem?
➤ Ability to return functions as values.
➤ In general, granting rst-class citizenship to functions!

Manas Thakur 18
fi
CS339: Abstractions and Paradigms for Programming
Logic Paradigm

Manas Thakur
CSE, IIT Bombay

Autumn 2025
Declarative vs Operational
➤ Square root of x:
➤ A number y such that y2 = x Declarative
➤ Newton’s method
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
Operational (define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))

Manas Thakur 2
Sum a list of numbers: Imperative (Java)

int sum(int[] list) {


int result = 0;
for (int i = 0; i < [Link]; i++) {
result += list[i];
}
return result;
}

Manas Thakur 3
Sum a list of numbers: Functional (Scheme)

(define (sum list)


(if (null? list)
0
(+ (car list) (sum (cdr list)))))

Manas Thakur 4
Sum a list of numbers: Logic (Prolog)

sum([], 0).
sum([H | T], N) :- sum(T, M), N is H + M.

Functional is sometimes close to declarative, but logic is closer.

Manas Thakur 5
Logic Paradigm

➤ Programs consist of just facts and rules.


➤ Not necessary to describe the “procedure” or the
control ow at a very low-level.
➤ Who does the computation then?
➤ In other words, who has the onus of translating the
“declarative” description to an “algorithm” that
computes on the von-Neumann architecture?
➤ The Interpreter!

Manas Thakur 6
fl
Logic Paradigm: Usage

➤ Prolog quite popular in rule-based Arti cial Intelligence.

➤ Datalog becoming very popular in program analysis, code optimization, and


type inference.

➤ SQL already the de-facto of relational databases.

Manas Thakur 7
fi
Predicates and Horn Clauses
➤ If it is precipitating in a city C and the temperature in C is freezing, then it is
snowing in C.
➤ snowing(C); precipitation(C); freezing(C) Predicates
➤ snowing(C) <- precipitation(C) AND freezing(C) Horn clauses

➤ When does it snow at Mumbai?


➤ Instantiate the variables.

Manas Thakur 8
Manas Thakur 9
Manas Thakur 10
(The Shortest?) Introduction to Prolog
➤ Two kinds of terms:
Rules of the game (aka Syntax):
➤ Facts • Constants start with small letters.
• Variables start with capital letters.
➤ father(ned, arya). • Full stop necessary after each fact/rule.
➤ mother(catelyn, bran). • No space before the opening parenthesis.
• Multiple terms with the same head
➤ Rules indicate disjunction.
• A comma between terms indicates
➤ parent(X, Y) :- father(X, Y). conjunction.
➤ parent(X, Y) :- mother(X, Y).
➤ grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
➤ Querying:
➤ ?- father(ned, sansa).
➤ ?- grandparent(rickard, Who).

Manas Thakur 11
How does the Prolog engine work (aka semantics)?

➤ Program = logic + control.


➤ Logic is speci ed by user; control is managed by runtime.

➤ Given a query:
➤ Consult the facts and rules in top-down order.
➤ Try to instantiate variables in the RHS of rules.
➤ Report instantiated values that satisfy the predicates resultant from the query.

Manas Thakur 12
fi
Resolution and Uni cation

➤ Resolution. If h is the head of a Horn clause and it matches with one of the
terms of another Horn clause, then that term can be replaced by h.
➤ Uni cation. A pattern-matching process that determines what particular
instantiations can be made to variables while making a series of simultaneous
resolutions.
➤ Which resolutions are simultaneous?
➤ Those that satisfy the given set of predicates.
➤ Example: ?- parent(brandon, bran).

Manas Thakur 13
fi
fi
Searching and Backtracking
When the path ahead is not nice,
say even mice, that backtracking is wise.
➤ Basic idea of logic paradigm:
➤ Search through the solution space while trying to unify variables with values,
till you get a solution.
➤ If no further resolution can be done, then backtrack and try a different
instantiation.
➤ Example: ?- parent(brandon, bran).
➤ Observe: Multiple solutions are possible.
➤ Example: ?- grandparent(rickard, Whoall).

Manas Thakur 14
Who lives where
➤ Adarsh, Kunj, Malay, Niral and Priyanshu live in a ve-room corridor in the
H17 hostel, starting from 1 to 5.

➤ Adarsh doesn’t live in the fth room and Kunj doesn’t live in the rst.
➤ Malay doesn’t live in the rst or the last rooms, and he is not in a room
adjacent to Priyanshu or Kunj.
➤ Niral lives in some room numbered after that of Priyanshu.

➤ Who lives in which room?

Manas Thakur 15
fi
fi
fi
fi
Who lives where (Cont.)

➤ Say the solution should look like this:


➤ [room(_,5), room(_,4), room(_,3), room(_,2), room(_,1)]
➤ The ve variables to be assigned rooms are: A, K, M, N and P.
➤ Say the structure rooms(Rooms) contains the solution list.
➤ The rst constraint can be encoded as:
➤ member(room(adarsh, A), Rooms), A \= 5.
➤ We can similarly add the other constraints to give the complete program.

Manas Thakur 16
fi
fi
Who lives where (Cont.)
rooms([room(_,5),room(_,4),room(_,3),room(_,2),room(_,1)]).
hostel(Rooms) :- rooms(Rooms),
member(room(adarsh, A), Rooms), A \= 5,
member(room(kunj, K), Rooms), K \= 1,
member(room(malay, M), Rooms), M \= 1, M \= 5,
member(room(priyanshu, P), Rooms),
not(adjacent(M, P)), not(adjacent(M, K)),
member(room(niral, N), Rooms), N > K,
print_rooms(Rooms).

where adjacent and print_rooms are de ned as follows:


adjacent(X, Y) :- X =:= Y+1. Yes, logic paradigm
adjacent(X, Y) :- X =:= Y-1.
began the times when
print_rooms([A | B]) :- write(A), nl, print_rooms(B).
print_rooms([]). computers started
replacing humans!
Solution: ?- hostel(X).
Manas Thakur 17
fi
Thursday’s is the last class
➤ We would
➤ talk about other paradigms that exist;
➤ discuss a few key learnings of the course;
➤ and answer questions collected through an
AMA form:

Manas Thakur 18
CS339: Abstractions and Paradigms for Programming
Au Revoir (until we meet again)

Manas Thakur
CSE, IIT Bombay

Autumn 2025
The amb operator from SICP
➤ (amb a b c) chooses any of the values a, b or c
➤ (amb) returns false (no choices)*
➤ Introduces ambiguity in our programs:
(define (an-integer-between low high)
(if (> low high)
(amb)
(amb low (an-integer-between (+ low 1) high))))

(an-integer-between 1 3)

(let ((x (an-integer-between 1 10))


(y (an-integer-between 1 10))) (define (require p)
(require (= (+ x y) 10)) (if (not p) (amb)))
(list x y))
*The amb obtained using #lang sicp in DrRacket is di erent.

Manas Thakur 2
ff
Ambiguity -> Non-determinism -> Concurrency
➤ Non-deterministic paradigm invented by John McCarthy in 1961
➤ What if we could explore the search space in parallel?
➤ Di erence between parallel and concurrent?
(define x 0)

(parallel-execute
(lambda () (set! x (+ x 1)))
(lambda () (set! x (* x 2))))
➤ May lead to a data race:
➤ If two accesses to the same memory location may-happen-in-parallel and at least one of them is a
write (variable level, eld level, cache level, page level, …)
➤ How do we mitigate data races?
➤ Which ones should we try to mitigate?

Manas Thakur 3
ff
fi
The Concurrency Paradigm

Manas Thakur 4
Concurrent Programs
Initially, A = B = 0.

Thread 1 Thread 2

(1) A = 1 (3) B = 1
(2) print B (4) print A

➤ What all can be printed?


➤ (1) —> (2) —> (3) —> (4): 01
➤ (3) —> (4) —> (1) —> (2): 01
➤ (1) —> (3) —> (2) —> (4): 11
➤ (1) —> (3) —> (4) —> (2): 11
➤ A few others with the same e ect.

Manas Thakur 5
ff
Things that shouldn’t happen
➤ It should not be possible to print 00.
Thread 1 Thread 2

(1) A = 1 (3) B = 1
(2) print B (4) print A
➤ In order to print 00:
➤ For line (2) to print 0, (2) should happen before (3) writes 1 to B.
➤ For line (4) to print 0, (4) should happen before (1) writes 1 to A.
➤ Each thread’s events should be in order: (1) before (2) and (3) before (4).
➤ This implies (1) should happen before (1)!
➤ A contradiction — means 00 cannot be printed by this multithreaded program.

Manas Thakur 6
Sequential Consistency
1. All operations executed in some sequential order.
➤ As if they were manipulating a single shared memory.

2. Each thread’s operations happen in program order.

Initially, A = B = 0.

Thread 1 Thread 2

A=1 B=1
r0 = B r1 = A

➤ Not allowed: r0 = 0 and r1 = 0.

Manas Thakur 7
Sequential Consistency
➤ Can be seen as a “switch” running one instruction at a time from di erent threads.

Core 1 Core 2
A=1 B=1
r0 = B r1 = A

Executed Memory
A=0
B=0

Manas Thakur 8

ff
Sequential Consistency
➤ Can be seen as a “switch” running one instruction at a time from di erent threads.

Core 1 Core 2
A=1 B=1
r0 = B r1 = A

Executed Memory
A=1
A=1
B=0

Manas Thakur 9

ff
Sequential Consistency
➤ Can be seen as a “switch” running one instruction at a time from di erent threads.

Core 1 Core 2
A=1 B=1
r0 = B r1 = A

Executed
A=1 Memory
B=1 A=1
B=1

Manas Thakur 10

ff
Sequential Consistency
➤ Can be seen as a “switch” running one instruction at a time from di erent threads.

Core 1 Core 2
A=1 B=1
r0 = B r1 = A

Executed
A=1 Memory
B=1
A=1
r1 = A (=1)
B=1

Manas Thakur 11

ff
Sequential Consistency
➤ Can be seen as a “switch” running one instruction at a time from di erent threads.

Core 1 Core 2
A=1 B=1
r0 = B r1 = A

Executed
A=1
B=1 Memory
r1 = A (=1) A=1
r0 = B (=1) B=1

Manas Thakur 12

ff
Sequential Consistency
➤ Two invariants: Order across
threads
➤ All operations executed in some sequential order.
➤ Each thread’s operations happen in program order.
Order within
a thread
➤ Says nothing about which order all operations happen in
➤ Any interleaving of threads is allowed

➤ Developed by Leslie Lamport in 1979 (Turing Award in 2013).

Manas Thakur 13
Memory (Consistency) Models

➤ A memory model de nes the permitted reorderings of memory operations during


execution.

➤ A contract between hardware and software: the hardware will only mess with
your memory operations in these ways.

➤ Sequential consistency is the strongest memory (consistency) model; allows the


fewest reorderings.

Manas Thakur 14
fi
Why SC?
➤ Agrees with programmer intuition!

Why not SC?


➤ Extremely slow to guarantee in hardware!
➤ The “switch” model is very conservative and does not allow several valid reorderings.

Manas Thakur 15
The Problem with SC

Thread 1 Thread 2
These instructions don’t con ict;
A=1 B=1
we don’t really need to
r0 = B r1 = A In addition, the whole world of
wait for the rst to nish.
compiler optimizations depends
on instruction reordering.

Executed Memory
A=1 Also, writing to memory
A=1
B=0 takes a lot of time.

Manas Thakur 16
fi
fi
fl
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1 A=1 Caches Memory
A=1 Write bu er A=0 A=0
r0 = B B=0 B=0

Manas Thakur 17
ff
ff
ff
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1
Caches Memory
A=1 Write bu er A=0 A=0
r0 = B A=1 B=0 B=0

Manas Thakur 18
ff
ff
ff
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1 r0 = B Caches Memory
A=1 Write bu er A=0 A=0
r0 = B A=1 B=0 B=0

Manas Thakur 19
ff
ff
ff
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1 C=1 Caches Memory
C=1 Write bu er C=0 C=0
r0 = C

Manas Thakur 20
ff
ff
ff
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1
Caches Memory
C=1 Write bu er C=0 C=0
r0 = C C=1

Manas Thakur 21
ff
ff
ff
Hardware-Level Optimization: Write Buffers
➤ Store writes in a processor-local bu er and proceed to next instruction immediately.
➤ The cache will pull out writes out of the write bu er when it’s ready.

Core 1
Thread 1 r0 = C Caches Memory
C=1 Write bu er C=0 C=0
r0 = C C=1

➤ Write bu ers de ne a new memory model!

Manas Thakur 22
ff
ff
fi
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A

Write bu er Write bu er

➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
B=0

Manas Thakur 23
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A

Write bu er Write bu er

➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
Executed
B=0

Manas Thakur 24
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
A=1
Write bu er Write bu er

➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
Executed
B=0

Manas Thakur 25
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A

Write bu er Write bu er
A=1
➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
Executed
B=0

Manas Thakur 26
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
B=1
Write bu er Write bu er
A=1
➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
Executed
B=0

Manas Thakur 27
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A

Write bu er Write bu er
A=1 B=1
➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0
Executed
B=0

Manas Thakur 28
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
r0 = B
Write bu er Write bu er
A=1 B=1
➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


A=0 Executed
B=0 r0 = B (=0)

Manas Thakur 29
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
r0 = B r1 = A
Write bu er Write bu er
A=1 B=1
➤ r0 = 0? and r1 = 0?

Memory ➤ SC? No.


Executed
A=0
r0 = B (=0)
B=0 r1 = A (=0)

Manas Thakur 30
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
r0 = B r1 = A
Write bu er Write bu er
B=1
➤ r0 = 0? and r1 = 0?

Memory Executed ➤ SC? No.


A=1 r0 = B (=0)
B=0 r1 = A (=0)
A=1

Manas Thakur 31
ff
ff
Write buffers change memory behaviour
Thread 1 Thread 2

(1) A = 1 (3) B = 1
Core 1 Core 2 (2) r0 = B (4) r1 = A
r0 = B r1 = A
Write bu er Write bu er

➤ r0 = 0? and r1 = 0?
Executed
Memory ➤ SC? No.
r0 = B (=0)
A=1 Write Bu ers? Yes!
r1 = A (=0) ➤
B=1 A=1
B=1

Manas Thakur 32
ff
ff
ff
Total Store Ordering (TSO)
➤ TSO allows optimizations that can make the underlying program signi cantly faster.
➤ Essentially, SC + Write Bu ers.

➤ But..
➤ TSO allows more behaviours than SC.
➤ Which means, programs are harder to reason about.

➤ Yet..
➤ x86 speci es TSO as its memory model.

Manas Thakur 33
fi
ff
fi
PL Memory Models
➤ Guess which was the rst programming language to have a memory model?
➤ Of course:

What all should I do as a programmer?


➤ And which language followed suit? ➤ Require new ways of thinking

➤ Of course: ➤ New paradigm: Concurrency

➤ Java and C++ have the most extensive memory-model descriptions till date
➤ But Java has multithreading in-built, which is good
➤ Java philosophy: Guarantee sequential consistency for data-race free programs; trust
the programmer to insert required synchronization

Manas Thakur 34
fi
Two popular ways of writing concurrent programs
➤ Shared-memory concurrency (imperative roots)
➤ Allow variable mutation; control with explicit synchronization
➤ Supported through atomic instructions, monitors, locks, etc
➤ Java synchronized blocks, OpenMP critical sections, C++ mutexes

➤ Message-passing concurrency (functional roots)


➤ Shared memory discouraged; communication through passed messages
➤ Popularly implemented as the Actor model (objects with local state and mailbox)
➤ Scala’s Akka actors, Go’s channels (CSP), Erlang’s message queues

Manas Thakur 35
Hawk’s-eye view
Paradigm Unit of Composition Time/Order Model Key Abstraction

Imperative Statements Sequential State and Control

Referential
Functional Functions Values & Expressions
Transparency

Logic Facts & Rules Search Space Proofs & Constraints

Encapsulation &
OO Objects Message Passing
Inheritance

Partial Order / Synchronization &


Concurrent Processes & Events
Interleaving Communication

Manas Thakur 36
Problem Solving as a PL Person

Manas Thakur 37
How to avoid buffer over lows?
➤ HW: Add extra circuitry; run-time and space overheads
➤ OS: Add checks in the ATU; lot of process-level interruption
➤ ML+FM: Predict based on past knowledge and then verify; extremely expensive
➤ PL:
➤ Perform static analysis and identify (un)safe accesses
➤ Design a programming language where this is not possible
➤ Add a “managed runtime” over the OS-Hardware layer for dynamic analysis
Static analysis is imprecise and dynamic analysis is expensive

😎

➤ CompL: Analyze what you can statically, and improve the results dynamically

Manas Thakur 38
f
AMA++

Manas Thakur 39
AMA++: Marks et al.

“what should be the minimum total marks adding the


quizzes midsem and endsem to pass the course
(i was not present in the rst lecture)”

Manas Thakur 40
fi
AMA++: Further learning

“Some of the next interesting courses building upon


this course”

“What to do if I absolutely loved the course (with respect


to anything extra I can do apart from coursework)?”

Manas Thakur 41
AMA++: Experimentation

“Why were Tuesday's slides blue?”

Manas Thakur 42
AMA++: Fun in learning

“Some story behind giving memes as the starting point


in exams paper”

Manas Thakur 43
The spirit of this course (from Lecture 1)
Dedication text of Structure and Interpretation of Computer Programs (SICP)
Authors: Harold Abelson and Gerald Jay Sussman with Julie Sussman (MIT)

This book [course] is dedicated, in respect and admiration, to the spirit that lives in the computer:

“I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started
out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while
we began to take their complaints seriously. We began to feel as if we really were responsible for the successful,
error-free perfect use of these machines. I don’t think we are. I think we’re responsible for stretching them,
setting them o in new directions, and keeping fun in the house. I hope the eld of computer science never loses
its sense of fun. Above all, I hope we don’t become missionaries. Don’t feel as if you’re Bible salesmen. The world
has too many of those already. What you know about computing other people will learn. Don’t feel as if the key to
successful computing is only in your hands. What’s in your hands, I think and hope, is intelligence: the ability to
see the machine as more than when you were rst led up to it, that you can make it more.”
—Alan J. Perlis (April 1, 1922 – February 7, 1990), rst Turing Award winner

Manas Thakur 44
ff
fi
fi
fi
My spirit as an instructor/advisor/friend
If I could make you feel any of it through this course,
I’d consider myself successful. You surely did.
…it’s extraordinarily important that we in computer science keep fun
in computing…

…we’re responsible for stretching them, setting them o in new


directions, and keeping fun in the house…

…the ability to see … that you can make it more…

Manas Thakur 45

ff

You might also like