Project briefing illustration for recursive numbers: a flat tactical intelligence sheet in the style of Star Fox Assault.
ACADEMIC PROJECT / Computer Science Course

Haskell Natural Number Converter

I restored a small Zero/Succ conversion fragment by reading the structure already present in its patterns, then checking the base case and two recursive examples.

Haskellalgebraic data typesrecursioncoursework fragmentrestoration

A small fragment with a useful missing piece

In October 2026, I returned to an April 2025 Haskell conversion fragment that referred to Nat and matched on Zero and Succ, but lacked its type declaration. I recovered the structure required by those equations, preserved the conversion, and verified a few values. The fragment does not establish a larger application or complete assignment.

What interested me was how much the function itself could tell me. Its patterns connect a recursive representation to a recursive calculation. Keeping the surviving equations separate from the new scaffolding also gave me a manageable restoration whose scope another reader could inspect.

THE CHALLENGE

The challenge: infer only what the patterns support

PROJECT STEPS

Following the shape of a natural number

Project briefing illustration for recursive numbers: a flat tactical intelligence sheet in the style of Star Fox Assault.
STEP 01 OF 04
Constructor chain

I trace Succ (Succ (Succ Zero)) as three nested successor values, each representing one unit, ending at Zero. Those forms already appear in the surviving Haskell code and justify the minimal Nat number type I restored in 2026. This makes the small fragment’s number representation understandable without claiming a recovered arithmetic library or complete assignment.

Select a step to follow the project.
Read all step descriptions
  1. Constructor chain

    I trace Succ (Succ (Succ Zero)) as three nested successor values, each representing one unit, ending at Zero. Those forms already appear in the surviving Haskell code and justify the minimal Nat number type I restored in 2026. This makes the small fragment’s number representation understandable without claiming a recovered arithmetic library or complete assignment.

  2. Recursive descent

    For each Succ n, I have the preserved nat2Int conversion call itself on the smaller value n, leaving one addition to perform when that call returns. This is recursion: the function repeats its work on a smaller part of the same structure, making the path down to zero easy to follow. Each represented unit requires another layer of work, and my restoration checks did not investigate extremely deep values.

  3. Base case

    I keep the preserved rule nat2Int Zero = 0 as the stopping point, giving the returning calls a definite value to build on. In the 2026 verification, I check this zero case separately from the successor examples. The three recorded checks support this small restoration, while exhaustive correctness and the historical classroom result remain unestablished.

  4. Unwind

    After reaching Zero, I add one for each returning successor call, producing three for this illustrative chain without changing the original conversion equations. The type declaration and module that organize the fragment were added during my 2026 restoration. I return Haskell’s bounded Int integer type, so values exceeding its range remain outside the three verified examples.

Evaluation trace: each Succ contributes one until recursion reaches Zero; the type declaration was restored in 2026.

I started with the constructors. Zero has no argument; Succ contains the next smaller natural-number value. Those two patterns support the minimal declaration data Nat = Zero | Succ Nat. I did not need to invent a more elaborate arithmetic model to make the conversion meaningful.

This bounded example shows the preserved conversion equations with the restored declaration:

Illustrative haskell example
data Nat = Zero | Succ Nat

nat2Int :: Nat -> Int
nat2Int Zero = 0
nat2Int (Succ n) = 1 + nat2Int n

The declaration gives me two ways to construct a value. Zero represents zero; one application of Succ represents one, and another represents two. Every finite constructor chain ends in Zero. The function follows the same shape, returning zero at the base and adding one as each successor unwinds.

Tracing Succ (Succ (Succ Zero)) made that correspondence easy to check. The function descends through three successor layers, reaches Zero, then evaluates outward to one, two, and three. The representation is doing part of the explanatory work: counting is visible in the structure of the value.

Keeping the restoration easy to review

In October 2026, I added the Nat declaration and a module named Naturals. Eq and Show derivations make values convenient to compare and inspect. I kept the original conversion equations unchanged while adding the declaration, derivations, and wrapper around them.

I kept the new checks in a separate executable that imports Naturals. That separation lets a reader see the restored definition and the verification independently. The result remains a supporting fragment, with no reconstructed assignment prompt, larger arithmetic library, or inferred submission history.

Checking the base case and the recursive path

The recorded GHC verification covers these three values:

  • Zero produces 0
  • Succ Zero produces 1
  • Succ (Succ (Succ Zero)) produces 3

All three checks passed during the October 1, 2026 restoration. They gave me direct evidence for the base case and two recursive examples, without establishing exhaustive correctness or a historical classroom result. The same expressions can be evaluated in a Haskell module or interpreter session to reproduce those observations.

What the small example taught me about representation

The same structure that makes unary naturals easy to understand also makes them expensive for large values. Every unit needs another Succ layer, and this direct conversion visits every layer. Its work grows linearly with the represented value. I see it as a useful model for learning recursion, with very different costs from ordinary machine-integer representation.

The output type adds another boundary: Int is bounded. An arbitrarily long mathematical natural number does not imply an arbitrarily large representable result from this function. My three checks did not investigate overflow or very deep constructor chains.

I recovered the type from the existing patterns and checked the base case and two recursive values. The result reinforced a useful habit for me: let the existing code justify the repair, then keep verification proportionate and explicit.

PROJECT WALKTHROUGH

I recovered the missing type and module structure for the supporting snippet; three October 2026 checks passed.

Validation & limitations

  • I recovered the missing type and module structure for the supporting snippet; three October 2026 checks passed.

Project records

  • Project artifacts and historical records, April 2025, approximate month
  • Restoration and public documentation, October 1, 2026
END OF PROJECT FILE RETURN TO ARCHIVE