Foundations of ComputingDeeper

Kayavak / Foundations of Computing / CS·02 Programming Fundamentals & Paradigms / Deeper

Deeper · Foundations of Computing

Programming Fundamentals & Paradigms

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Early programs were one long list of machine instructions with jumps. Two problems grew with their size: the same piece of work was written out again and again, and nobody could follow where control went. The first answer was the subroutine: David Wheeler's "Wheeler jump" on Cambridge's EDSAC (around 1949–51) let a program call a shared piece of code and come back. Everything in this chapter called a function descends from it.

The second answer came in the 1960s. Corrado Böhm and Giuseppe Jacopini proved in 1966 that sequence, selection and iteration are enough to express any computation, and Edsger Dijkstra's 1968 letter Go To Statement Considered Harmful argued that programs should be built from those shapes so their text mirrors what happens when they run. That is structured programming, and it's why every language you'll meet has if, while and functions rather than only jumps.

The paradigms came from different problems. Lisp (John McCarthy, 1958) treated programs as mathematical functions over symbols. Simula 67 (Ole-Johan Dahl and Kristen Nygaard), built for simulations, bundled data with the procedures that act on it: the first objects. Smalltalk (Alan Kay's group at Xerox PARC, 1970s) made "everything is an object sending messages" a whole language. ML (Robin Milner, 1973) added types the compiler infers for you. Modern languages borrow from all of them.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

A program is a set of names bound to values, and rules for computing new values from old ones. Every feature in this chapter is a way of controlling which names exist where, what they can hold, and who may change them.

Question Answer
What is it? Values (data), names for them (variables), operations, and ways to combine operations (control flow, functions)
Problem it solves Saying a procedure precisely enough for a machine, and clearly enough for the next person
Parts Values and types; bindings and scope; expressions and statements; control flow; functions; modules; errors
How it works Each function call gets a fresh frame of local names; returning discards it; data that must outlive the call lives elsewhere (the heap)
Assumptions Names mean one thing within a scope; a call returns to where it came from; types describe the bits correctly
When they change Shared mutable state breaks "a name means one thing": two parts of the program change the same value and each surprises the other
Where it fails Concurrency (chapter 8), errors that cross many layers, and programs too large to hold in one head (software architecture)
Related ideas Stack frames are real memory (ch07, ch08); types are what compilers check (ch10); recursion is induction (ch05)

What if?

What if nothing could ever change once created?

Reason it through, then open

Every "change" would create a new value: appending to a list returns a new list. Whole classes of bugs disappear: nobody can modify a value you're still using, so sharing it between functions or threads is safe. The cost moves to memory and copying, which is why purely functional languages use persistent data structures that share unchanged parts between old and new versions (chapter 3). You'd also need another way to talk to the outside world, which does change: that is what Haskell's I/O types are about.

What if a language had functions but no recursion?

Reason it through, then open

FORTRAN up to the 1977 standard was like this: it didn't support recursive calls, partly because it gave each subroutine one fixed place for its variables instead of a fresh frame per call. You can still compute anything with loops and an explicit stack, but problems that are naturally recursive (walking a tree, parsing nested brackets) become bookkeeping. The fresh-frame-per-call design is what makes recursion work, and also what makes very deep recursion run out of stack.

Where the boundary is

Aspect Value semantics Reference semantics
Assignment copies The value itself A pointer to the same object
After b = a; change b a is unchanged a sees the change
Typical in Numbers everywhere; structs in C, Go, Rust, Zig; Java primitives Java and Python objects, JS objects, Go maps and slices
Cost Copying large values Aliasing: two names, one object
Confused because Python and Java pass references by value: the reference is copied, the object isn't

Derive it

Why a recursive function terminates. Take a function that calls itself only on smaller inputs, with a base case.

  1. Give each input a measure: a natural number, like the length of a list or the value of n.
  2. Every recursive call is made on an input whose measure is strictly smaller. (This is what "progress toward the base case" means; check it in the code.)
  3. Natural numbers can't decrease forever: any strictly decreasing sequence of them is finite.
  4. So every chain of calls reaches an input with no recursive call, the base case, and returns.

The same argument, read the other way, is mathematical induction (chapter 5): if the base case is right and each call is right assuming the smaller calls are, the function is right for every input.

How sure are we?

Claim How it's known Where from
Sequence, selection and iteration suffice for any program Theorem Böhm and Jacopini, 1966
Avoiding unstructured jumps makes programs easier to understand Model Dijkstra's argument (1968); widely accepted, hard to measure precisely
Static typing reduces defects Active research Studies disagree: a 2014 study of GitHub projects linked some languages, statically typed ones among them, to slightly fewer defects; a 2019 reproduction found most of those effects disappeared
Paradigms are separate kinds of language Simplification Most modern languages mix object, functional and procedural features

Still open

Whether static types make teams more productive overall is still argued about, with evidence on both sides; so is how much functional style should be the default for everyday programs. Treat confident claims either way with care.

What would change your mind? If a large, well-controlled study showed that code with pervasive shared mutable state had fewer defects than code built from immutable values, the chapter's advice to prefer immutability would need revisiting.

Recall

What does structured programming replace, and with what?

Arbitrary jumps (goto), with sequence, selection, iteration and functions.

Why can recursion work at all?

Each call gets its own fresh frame of local variables, so calls don't overwrite each other.

In Java, if a method assigns a new object to its parameter, does the caller see it?

No: the reference is passed by value; the caller's variable still points to the old object (changes made to that object are seen).

How do you show a recursive function terminates?

Find a natural-number measure that strictly decreases on every recursive call.

What's the cost of never mutating values?

More copying and memory, which persistent data structures reduce by sharing unchanged parts.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.