One lesson may be that not all proofs are meant to be formalized. The gap between an informal proof and a formal proof is often substantial, and you could reasonably treat this as an extreme instance of that phenomenon. Counting in two ways gives you an informal starting point. You can gradually refine it into a fully algebraic proof that is then straightforward to formalize, rather than come up with that latter proof from scratch.
But maybe you don't trust what random strangers on the internet tell you. You have to suffer through the pain of doing combinatorics in type theory yourself to learn a lesson. Let's start by saying that "counting" the number of elements in a set $A$ is to establish an equivalence of types, or pardon my French, a bijection $A = \overline{n}$, where $\overline{n}$ is the prototypical set with $n$ elements, $\overline{n} {\;:=} \left\{m \in \mathbb{N} \mid m < n \right\}$. Thus, counting $A$ is to establish a correspondence between $A$ and some reference set of numbers $0\dots n-1$. We can pun bijections and equality thanks to univalence, but all of this is also formalizable in non-univalent type theories by not interpreting the $=$ between types literally as equality.
Counting $A$ in two ways is to find two equivalences $A = \overline{n}$ and $A = \overline{m}$, which lets us conclude the equality between natural numbers $n = m$ via transitivity and a theorem which is really fun to prove:
$${\textrm{inj-fin}} : \overline{n} = \overline{m} \to n = m$$
Refining the idea further, we could also say that counting the same set in two ways is to give two equivalent descriptions of it, as different sequences of choices, each corresponding to a different type leading to different expressions for their cardinalities. $A = B$, $A = \overline{n}$, and $B = \overline{m}$, implying $n = m$.
Here the power $2^n$ is encoded by vectors of $n$ booleans $\mathbf{2}$:
$${\textrm{card-pow}} : \mathbf{2}^n = \overline{2^n}$$
and the binomial coefficient ${n}\choose{k}$ is encoded by those vectors with $k$ ones:
$${\textrm{card-choose}} : \left\{v \in \mathbf{2}^n \mid \left|v\right|_1 = k \right\} = \overline{{n}\choose{k}}$$
So we can count the powerset either via its canonical cardinality $2^n$, or via a bijection to the sum of binomial coefficients:
$${\textrm{power2choose}} : \mathbf{2}^n = \sum_{k \leq n} \left\{v \in \mathbf{2}^n \mid \left|v\right|_1 = k \right\}$$
The final theorem follows from the above facts and the equivalence between sums of types and sums of cardinalities:
$${\textrm{card-sum}} : (\forall i\leq n, f(i) = \overline{g(i)}) \to \sum_{k\leq n} f(k) = \overline{\sum_{k \leq n} g(k)}$$
yielding
$${\textrm{binomial-sum}} : 2^n = \sum_{k\leq n} {n\choose k}$$
Now we have all the pieces in place, we can see that most of the work in the informal "count in two ways" proof of that final identity really happens in the theorem $\textrm{power2choose}$, a bijection between two types. The direct construction is left as an exercise; it either ends up being a mess or as a disguised version of the purely algebraic proof.
But there is one more idea in the informal proof that we can formalize, which is that the binomial coefficients represent a partition of the powerset, indexed by the size of the subsets. We can refactor that idea into one more theorem, equating a set with the sum of its fibers:
$$\textrm{partition} : \forall A, \forall B, \forall f : A \to B, A = \sum_{b : B} \left\{ a : A \mid f(a) = b \right\}$$
Contemplate the resemblance with $\textrm{power2choose}$.
To summarize everything, a lot of sightseeing, but maybe not the shortest path from point A to point B.
But if we have a short path and a long path, why not consider the short one as as shortcut over the longer one? By turning the "count in two ways" argument on its head, we can use an algebraic identity in $\mathbb{N}$ to construct an otherwise complex bijection, via the converse of $\textrm{inj-fin}$:
$$\textrm{cong} : n = m \to \overline{n} = \overline{m}$$
This is perhaps another lesson. The $\textrm{binomial-sum}$ gives us the $\textrm{power2choose}$.
filter
for vectors?", which I've seen several times this week already. I counsel the community to think about it. $\endgroup$