Reasoning About Mutual Recursion

Mutually recursive functions call one another. Their combined transitions must reach a base case for every allowed input.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace complementary questions
  2. Check shared progress
  3. Follow the alternating contracts down to zero
  4. Find one progress measure for every cross-call

Trace complementary questions

For a nonnegative integer, even at zero returns true and odd at zero returns false. For a positive integer, even can ask odd about one less, and odd can ask even about one less. Calling even at three reaches odd at two, even at one, and odd at zero.

Check shared progress

Each call decreases the same nonnegative number. An argument that counts only calls within one function can miss part of the chain. Shrinking input measures and the call stack make both paths visible.[1]

Follow the alternating contracts down to zero

For nonnegative integers, define even at zero as true and odd at zero as false. At a positive argument, each function can ask the other about one less. Subtracting one changes parity, so these two rules must agree at the shared base. See choosing a recursion base case.

even(3) → odd(2) → even(1) → odd(0) → false
odd(3)  → even(2) → odd(1)  → even(0) → true

Find one progress measure for every cross-call

Both call directions strictly reduce the same nonnegative argument. If one direction leaves the value unchanged, check whether any complete cycle still ensures descent or instead repeats a state. Test zero directly in both functions, then one and two to catch swapped base results.

References

  1. Jeff Erickson, Algorithms ↩