I am asking a soft question.
For me it feels like induction and recursion are dual to each other (in somewhat category-theory-like sense, which I can not define rigorously):
- both do have at least one base case (yeah, some recursions do not have, I am aware of that and this fact seems to break some of my assumptions);
- both can be seen as a "sequence of consequences", where: a) induction defines $n + 1$ consequence in term of $n$ consequence (i.e. next state in terms of previous one) and b) recursion defines $n$ in terms of $n + 1$ (dependency arrow turned around: once again, reminds category theory's duality).
I'd like to now how deep the rabbit hole goes.
They are not 'dual' to each other. They are essentially the same thing.
In dependent type theories, the induction principle on the natural numbers looks something like:
This says that for a family of types indexed by the naturals
T, if we can exhibit a value with typeT 0and a function for constructing aT (suc n)from aT nfor alln, then we can construct aT nfor every choice ofn. However, if we simply use a constant family of typesT n = R, we instead get the type:And this is the type of primitive recursion on the natural numbers. So, one of the fundamental ideas in type theory is that the computational behavior of an induction principle is recursion. Or, the induction principle is recursion with a fancier type.
Over in category theory, one talks about these inductively defined types as free algebras. If you have a functor $F$, then an algebra is a pair $(A, α : F A → A)$. Here $A$ might be called the 'carrier' and $α$ the 'action'. Homomorphisms between algebras are maps between carriers that form a commutative square with the actions in an 'obvious' way. Then a free (or initial) algebra is one $(μF, ι : F μF → μF)$ such that there is a unique homomorphism from it to every other algebra.
In the case of the naturals, the functor is $F A = 1 + A$, and an algebra action $1 + A → A$ is actually like two of the arguments to structural recursion $(1+A→A) \cong A × (A → A)$. Given these arguments, we get a homomorphism $\mathsf{rec} : ℕ → A$, and the commutative property it has means that it behaves like recursion.
Now, in the type theories above we can define a type
Σ ℕ T, which has values(n, t)such thatn : ℕandt : T n. Giventz : T 0we can constructsz = (0, tz) : Σ ℕ T, and withts : ∀ n. T n -> T (suc n)we can define:With
szandss, we can define an algebra with carrierΣ ℕ T. So this gives us a functionℕ -> Σ ℕ T. But, it turns out that the uniqueness property of $\mathsf{rec}$ above (along with the definition ofssandsz) allows us to deduce that the first component of the result of $\mathsf{rec}$ is the number we fed in. And this means that if we gave itn, then the second component of the result has typeT n. So this means that this use of $\mathsf{rec}$ gives usinductionabove.Depending on the exact setting, more machinery might be involved. But induction and recursion are indeed closely related.