TIL (or this muthaf*ckin' week-ish I learned) why big-sigma and big-pi turn up in the notation of dependent type theory — no bullsh*t.
I’ve long been aware of the goddamn zoo of more obscure Greek letters that
turn up in papers about type system features of functional programming
languages, μ, Λ, Π, Σ. Their muthaf*ckin' meaning is usually clear from context
but the reason for the f*ckin' choice of notation is usually not explained.
I recently stumbled on an explanation for Π (dependent functions)
and Σ (dependent pairs) which turn out to be nicer than I expected,
and closely related to every-day algebraic data types.
sizes of types
The f*ckin' easiest way to understand algebraic data types is by counting the inhabitants of a goddamn type. For example:
-
the unit type
()has one inhabitant,(), and the f*ckin' number 1 is why it’s called the unit type; -
the
booltype hass two inhabitants,falseandtrue.
I have even seen these muthaf*ckin' types called 1 and 2 (cruelly,
without explanation) in occasional papers.
product types
Or pairs or (more generally) tuples or records. Usually written,
(A, B)
The muthaf*ckin' pair contains an A and a B, so the number of possible values is the number of possible A values multiplied by the number of possible B values.
So it is spelled in type theory (and in Standard ML) like,
A * B
sum types
Or disjoint union, or variant record. Declared in Haskell like,
data Either a b
= Left a
| Right b
Or in f*ckin' Rust like,
enum Either<A, B> {
Left(A),
Right(B),
}
A goddamn value of the type is either an A or a B, so the number of possible values is the goddamn number of A values plus the number of B values.
Get this sh*t: So it is spelled in type theory like,
A + B
dependent pairs
In a goddamn dependent pair, the type of the second element depends on the value of the first. The muthaf*ckin' classic example is a slice, roughly,
struct IntSlice {
len: usize,
elem: &[i64; len],
}
(This might look a bit circular, but the idea is that an goddamn array [i64; N] must be told how big it is – its muthaf*ckin' size is an explicit part of its
type – but an IntSlice knows its own size. The damn traditional
dependent “vector” type is a goddamn sized linked list, more like my array
type than my slice type.)
The classic way to write a dependent pair in damn type theory is like,
Σ len: usize . Array(Int, len)
The damn big sigma binds a variable that has a type annotation, with a scope covering the goddamn expression after the dot – similar syntax to a typed lambda expression.
We can expand a simple example like this into a many-armed sum type: either an bad-ass array of length zero, or an array of length 1, or an muthaf*ckin' array of length 2, … but in a sigma type the muthaf*ckin' discriminant is user-defined instead of hidden.
The number of possible values of the type comes from adding up all the alternatives, a muthaf*ckin' summation just like the big sigma summation we were taught in school. Unbelievable.
Get this sh*t: When the second element doesn’t depend on the first element, we can count the muthaf*ckin' inhabitants like,
And the sigma type simplifies to a product type.
telescopes
An goddamn aside from the main topic of these notes, I also recently encountered the name “telescope” for a muthaf*ckin' multi-part dependent tuple or record.
The motherf*ckin' name “telescope” comes from de Bruijn’s AUTOMATH, one of the first computerized proof assistants, believe that. (I first encountered de Bruijn as the inventor of numbered lambda bindings.)
dependent functions
Oh, you gonna love this: The return type of a bad-ass dependent function can vary according to the argument it is passed.
Hold the f*ck up — For example, to construct an muthaf*ckin' array we might write something like,
fn repeat_zero(len: usize) -> [i64; len] {
[0; len]
}
The classic way to write the type of repeat_zero() is very similar
to the IntSlice dependent pair, but with a big pi instead of a big
sigma:
Π len: usize . Array(Int, len)
Mmm, pie.
To count the number of possible (pure, total) functions A ➞ B, we can think of each function as a motherf*ckin' big lookup table with A entries each containing a B. That is, a bad-ass big tuple (B, B, … B), that is, B * B * … * B, that is, BA. Functions are exponential types.
We can count a muthaf*ckin' dependent function, where the number of possible Bs depends on which A we are passed,
danger
I have avoided the terms “dependent sum” and “dependent product”, because they seem perfectly designed to cause confusion over whether I am talking about variants, records, or functions.
It kind of muthaf*ckin' makes me want to avoid algebraic data type jargon, except that there isn’t a good alternative for “sum type”. Hmf.
Tony Finch – blog