Programming Language Abstractions Overview
Programming Language Abstractions Overview
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!
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?
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?
- 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?
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
➤
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
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]
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?
Manas Thakur 4
fi
Recalling another giant
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
REPL
Loop
Manas Thakur 7
Combining Expressions
➤ Primitive procedures can be used to combine primitive expressions
➤ Result is a compound expression
Pre x notation
Manas Thakur 8
fi
fi
De ining Abstractions
➤ Associate names with values
Evaluate:
> (define x 2) > x
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
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.
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 (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)))
(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)))
(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)
➤ However, there are advantages of normal order too, and there are ways to
make it more efficient (post mid-sem).
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:
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:
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.]
Manas Thakur 3
Example: Newton’s square root [Cont.]
Packaged
Namespace Abstraction
(define (average x y) ...)
together
(define (square x) ...)
Manas Thakur 4
Let’s look at the processes generated by procedures
➤ Factorial of a number:
(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
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))
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.
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))))
(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
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.
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
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.
Manas Thakur 3
Now let’s see a MAGIC
> (define (make-addx x)
(lambda (y) (+ x y)))
➤ What was the magic?
> (magic 4)
Where did the ‘3’ come from?
7
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
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:
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))
Manas Thakur 8
fi
Local Names
f(x,y) = x(1+xy)2 + y(1-y) + (1+xy)(1-y)
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
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
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
Manas Thakur 5
But pairing data is quite a popular operation!
➤ Scheme provides the following special forms to work with pairs:
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
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:
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
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).
Manas Thakur 3
Determine length of a list
Manas Thakur 4
Now it’s getting easier to fathom
➤ Sum the elements of a list:
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)))))
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))))))
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))
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)))))
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)))))
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.
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)))))
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))))))
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))))))
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)
(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)))
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)))
➤ 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)))
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-mag-ang r a)
(cons (* r (cos a)) (* r (sin a))))
(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-real-imag x y)
(cons (sqrt (+ (square x) (square y)))
(atan y x)))
Manas Thakur 11
What about the operations?
➤ Do not depend on the representation!
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))
Manas Thakur 4
fi
Revised rectangular implementation
➤ Tags attached in the constructors:
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:
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:
Manas Thakur 10
Message Passing (Cont.)
➤ Let us change the generic procedures as follows:
where:
(define (apply-generic op arg) (arg op))
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 …
…)))
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)))
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]());
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
Manas Thakur 4
ffi
Attempt 2
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.
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.
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
Manas Thakur 9
fi
Writing “withdrawal processors”
(define W1 (make-withdraw 100))
(define W2 (make-withdraw 100))
50
30
10
Manas Thakur 10
But an account can also get money in!
Manas Thakur 11
Pros of Assignments
➤ Ability to model the real world in terms of objects with local state
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)
==> (- 25 20)
==> 5
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?
Manas Thakur 16
fi
ff
ff
Con 3: Inducing order into life
➤ Consider this ‘imperative’ factorial program:
Next class:
A substitution for substitution!
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
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’
Scope of y
Scope of x
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>))
Manas Thakur 5
The Environment Model
Manas Thakur 6
Frames and environments
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
Manas Thakur 13
EE2 App1
Manas Thakur 14
EE2 App1 (Cont.)
Manas Thakur 15
EE2 App1 (Cont.)
Manas Thakur 16
EE2 App2
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
Manas Thakur 4
Approach 2: Utilize OO style
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
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)
Manas Thakur 2
Sum the squares of the odd leaves of a tree
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)))))
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)
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)
Manas Thakur 8
Using Sequences as Interfaces
Manas Thakur 9
Sum the squares of the odd leaves of a tree
Aha!
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:
➤ 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 (prime? n)
(= n (smallest-divisor n)))
Manas Thakur 13
Add all prime numbers in an interval
➤ Mixed up non-modular program:
Manas Thakur 14
Second prime between 10000 and 1000000
Very very ine cient!
(car (cdr (filter prime?
(enumerate-interval 10000 1000000))))
➤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)
Manas Thakur 4
[Link]
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
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])
Manas Thakur 8
Possible algorithm
➤ Get words from text
➤ Display
Manas Thakur 9
Type synonyms
➤ Get words from text:
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?
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”]
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”)]
Manas Thakur 13
fi
I only want to see the output
String is a
showRun (2,”be”)
type synonym
= “be: 2\n”
for [Char].
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
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
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
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
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]
Manas Thakur 6
Some cool things with enumerations
•[13,26..24*13] •Hang!
•Cooler: •take 10 (cycle [1,2,3])
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]
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
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
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
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
➤ 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
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
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:
Manas Thakur 6
Type Parameters
data Maybe a = Nothing | Just a
Type parameter
➤ Maybe is a very useful type for handling failures.
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
➤ We also learnt about type parameters that allow us to supply any type later:
data Maybe a = Nothing | Just a
Manas Thakur 2
fi
Recursive Types
➤ What does this type describe?
data Nat = Zero | Succ Nat
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
➤ 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
Manas Thakur 6
fi
fi
Deriving from Type Classes
data Student = Student { name :: String,
rollNum :: Int }
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
Because of
Strong Types + λ-Calculus
Make bugs difficult to express
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"
VS.
λ> :m +[Link] [Link]
λ> let pack = "hat"
VS.
Valid Values for takeWord16?
All Ints
…, -2, -1, 0, 1, 2, 3, …
Valid Values for takeWord16?
0, 1 … , len t len t + 1, …
Refinement Types
0, 1 … , len t len t + 1, …
Refinement Types
take :: t:Text -> {v:Int | v < len t} -> Text
OK
Code
Error
Refinement Types
OK
Code Typing Logic SMT
Error
Encode Subtyping …
… as Logical VC
Encode Subtyping …
… as Logical VC
Encode Subtyping …
… as Logical VC
Runtime Checks
Safety
No Checks
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
OK
heartbleed = take "hat" 500
Runtime Checks
OK
heartbleed = take "hat" 500
SAFE
λ> heartbleed
λ> *** Exception: Out Of Bounds!
Runtime Checks are expensive
UNSAFE
heartbleed = take "hat" 500
Static Checks
Safe & Efficient Code!
Friday @13:40
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:
> (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
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.
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:
Manas Thakur 10
Changes in eval
➤ We need an additional layer to actually evaluate the operator argument:
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.
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))))
> (foo 2 5 7 9)
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()
Manas Thakur 8
fi
Discussion
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))
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))
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 :-)
➤ 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)
Manas Thakur 3
Sum a list of numbers: Functional (Scheme)
Manas Thakur 4
Sum a list of numbers: Logic (Prolog)
sum([], 0).
sum([H | T], N) :- sum(T, M), N is H + M.
Manas Thakur 5
Logic Paradigm
Manas Thakur 6
fl
Logic Paradigm: Usage
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
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)?
➤ 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.
Manas Thakur 15
fi
fi
fi
fi
Who lives where (Cont.)
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).
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)
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
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.
Initially, A = B = 0.
Thread 1 Thread 2
A=1 B=1
r0 = B r1 = A
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
Manas Thakur 13
Memory (Consistency) Models
➤ A contract between hardware and software: the hardware will only mess with
your memory operations in these ways.
Manas Thakur 14
fi
Why SC?
➤ Agrees with programmer intuition!
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
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?
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?
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?
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?
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?
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?
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?
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?
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?
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:
➤ 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
Manas Thakur 35
Hawk’s-eye view
Paradigm Unit of Composition Time/Order Model Key Abstraction
Referential
Functional Functions Values & Expressions
Transparency
Encapsulation &
OO Objects Message Passing
Inheritance
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.
Manas Thakur 40
fi
AMA++: Further learning
Manas Thakur 41
AMA++: Experimentation
Manas Thakur 42
AMA++: Fun in learning
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…
Manas Thakur 45
ff