The whole, before the parts

A programming language is a notation for computation precise enough that a machine can run it, and this topic studies the notation itself: what a program's text means, and what can be known about a program before it ever runs. The single idea that carries it is that meaning and guarantees can both be given by rules as exact as the program's own — rules for evaluating an expression, and rules for assigning it a type — so claims like "this program cannot crash that way" become things to prove rather than to hope. Citation needed

Terminology and core concepts

  1. Syntax and semantics

    A language's syntax is which strings of symbols count as programs, fixed by a grammar — rules for building bigger phrases from smaller ones; its semantics is what those programs mean, and the two are separate: a sentence can be grammatical and still nonsense. Citation needed

    What does a grammar decide about a string?
    whether it counts as a program at all Citation needed
    What does semantics assign to a syntactically valid program?
    its meaning — what running it does Citation needed
    Can a program be grammatical yet meaningless?
    yes — syntax does not fix sense Citation needed
    What is parsing?
    recovering a program's phrase structure from its text Citation needed
    Why do grammars define big phrases from smaller ones?
    so meaning can be assigned piece by piece, by structure Citation needed
  2. Operational semantics

    Operational semantics gives meaning by rules of evaluation: small steps that rewrite an expression toward a value, an expression that cannot step further; a program's meaning is the sequence of steps its text forces — the same step-by-step rule-following that defines computation, now applied to notation. Citation needed

    What is a value in operational semantics?
    an expression that cannot be evaluated further Citation needed
    What does one small step do?
    rewrites the expression by exactly one rule Citation needed
    What is an expression that is not a value but has no applicable rule?
    stuck — the formal picture of a crash Citation needed
    How does (2 + 3) × 4 evaluate in small steps?
    (2+3)×4 → 5×4 → 20 Citation needed
  3. The lambda calculus

    The lambda calculus is the minimal language under them all: nothing but variables, function-building (λx.body), and function application, with one evaluation rule — beta reduction, substituting the argument for the bound variable — yet it computes everything a Turing machine can. Citation needed

    What are the lambda calculus's three expression forms?
    variable, abstraction λx.e, application Citation needed
    What does beta reduction do?
    substitutes the argument for the bound variable in the body Citation needed
    What does (λx.x+1) 5 reduce to?
    6 Citation needed
    How much can the lambda calculus compute?
    exactly what a Turing machine can Citation needed
    How does a two-argument function appear in lambda calculus?
    as a function returning a function — λx.λy.e Citation needed
  4. Binding and scope

    A variable's scope is the region of text where its name refers to a given binding; under lexical scope that region is read off the program's structure, so a name's meaning is fixed where the function is written, not where it is called. Citation needed

    What fixes a name's meaning under lexical scope?
    where the function is written Citation needed
    What is a free variable?
    one not bound within the expression at hand Citation needed
    In λx.x + y, which variable is free?
    y Citation needed
    What is a closure?
    a function packaged with the bindings it was written among Citation needed
    Why does substitution rename bound variables when needed?
    to keep a free variable from being captured by a binder Citation needed
  5. Types as predictions

    A type classifies expressions by the kind of value they will produce, and a type system is a set of rules assigning types from structure alone — a prediction about every run of the program, made without running it even once. Citation needed

    What does an expression's type predict?
    the kind of value every run will produce Citation needed
    What input does type checking need that testing does not?
    none extra — only the program's text Citation needed
    What does the function type A → B say?
    given an A, it returns a B Citation needed
    How does checking differ from testing in coverage?
    checking covers all runs; a test covers one Citation needed
    What is type inference?
    the checker deducing types never written down Citation needed
  6. Soundness: well-typed programs do not get stuck

    A type system is sound when its predictions are never wrong, proved in two halves — progress: a well-typed expression is a value or can step; preservation: a step keeps the type — which together rule out ever reaching a stuck expression. Citation needed

    What does progress guarantee of a well-typed expression?
    it is a value or can take a step Citation needed
    What does preservation guarantee across a step?
    the type stays the same Citation needed
    What do the two lemmas jointly exclude?
    a well-typed program ever getting stuck Citation needed
    Does soundness promise a program halts?
    no — only that it never gets stuck Citation needed
    Does soundness promise the answer is the one you wanted?
    no — types constrain kind, not correctness Citation needed
  7. Static and dynamic checking

    Static checking rejects type-incorrect programs before any run; dynamic checking tags values and faults at the moment of misuse — a trade of early certainty against flexibility, since every static checker must reject some programs that would in fact run fine. Citation needed

    When does a static checker catch a type error?
    before the program runs Citation needed
    When does a dynamic check catch it?
    at the moment the value is misused, mid-run Citation needed
    Why must a static checker reject some fine programs?
    exact behavioural questions are undecidable, so it approximates safely Citation needed
    What does a dynamic language carry at run time that a static one can shed?
    a type tag on every value Citation needed
  8. Polymorphism

    Polymorphism lets one definition carry many types: a function like reverse works for all element types because it never looks inside an element — and that very ignorance constrains its behaviour, since a function of type "for all A, A → A" can only be the identity. Citation needed

    What makes reverse work on lists of anything?
    it never inspects the elements Citation needed
    What is the only function of type "for all A, A → A"?
    the identity Citation needed
    Why can a polymorphic type constrain behaviour so tightly?
    the function must work identically for every type Citation needed
    What does polymorphism save over writing one version per type?
    one definition, checked once, reused everywhere Citation needed
  9. The Curry-Howard correspondence

    Types line up with propositions and programs with proofs: a value of type A → B is a method for turning evidence of A into evidence of B, which is exactly what a proof of an implication is — so type checking a program is checking a proof, and proof assistants are built on precisely this. Citation needed

    What does a type correspond to under Curry-Howard?
    a proposition Citation needed
    What does a program of that type correspond to?
    a proof of the proposition Citation needed
    Which logical connective matches the function type?
    implication Citation needed
    Which matches the pair type?
    conjunction Citation needed
    What does an empty type — no values — correspond to?
    an unprovable proposition Citation needed

Problems that prove it

  1. Beta-reduce (λf.λx.f (f x)) (λy.y+1) 3 to a value.

    Drills: substitution as the single engine of evaluation.

    Derivation steps
    1. Apply the outer abstraction: substitute λy.y+1 for f, giving λx.(λy.y+1) ((λy.y+1) x).
    2. Apply to 3: substitute 3 for x, giving (λy.y+1) ((λy.y+1) 3).
    3. Reduce the inner application: (λy.y+1) 3 → 4.
    4. Reduce the outer: (λy.y+1) 4 → 5.
    5. Value: 5. The first argument was applied twice — the term is the "do it twice" function.

    Citation needed

  2. Assign a type to the composition function λf.λg.λx.f (g x).

    Drills: reading a type off a term's structure, the checker's own reasoning.

    Derivation steps
    1. Give x an unknown type A.
    2. g is applied to x, so g : A → B for some B.
    3. f is applied to g x : B, so f : B → C.
    4. The body f (g x) has type C, so λx.… : A → C.
    5. Assembling the three binders: (B → C) → (A → B) → (A → C) — for all A, B, C.
    6. Under Curry-Howard this is the proof that implication chains: from B⇒C and A⇒B, conclude A⇒C.

    Citation needed

  3. Trace where lexical scope gets its answer: let a = 10; define f(x) = x + a; inside another function where a = 99, call f(1).

    Drills: definition site versus call site, the scope question in miniature.

    Derivation steps
    1. f's body mentions a free; lexical scope resolves it where f is written.
    2. There, a = 10, and f is packaged with that binding — a closure.
    3. At the call site a = 99, but the call site's bindings are irrelevant to f's body.
    4. f(1) = 1 + 10 = 11, not 100.
    5. Under the historical alternative — resolving names at the call site — the answer would silently depend on every caller, which is why lexical scope won.

    Citation needed

  4. Using pairs-as-conjunctions, write the program that proves "A and B implies B and A".

    Drills: Curry-Howard used in anger — a proof as a small program.

    Derivation steps
    1. The proposition reads as a type: (A × B) → (B × A), with × the pair type.
    2. A program of that type takes a pair and must return a pair, swapped.
    3. Write it: λp. (second p, first p).
    4. Check: second p : B, first p : A, so the result is B × A. Well-typed.
    5. The program type-checks, so the proposition is proved — the checker was the proof-checker.

    Citation needed

Sources

  • Benjamin Pierce, Types and Programming Languages — operational semantics, progress and preservation, and the lambda calculus, done rigorously.
  • Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs — scope, closures, and evaluation built from scratch.
  • Robert Harper, Practical Foundations for Programming Languages — the type-theoretic view carried through a whole language zoo.
  • Philip Wadler, Propositions as Types — the Curry-Howard story, told for the working programmer.

Learning path

Continue through Computer Science in order, using the concepts above to connect this topic to the next.