Maned

Bounded repetition

Generated by tools/gen_example_pages.py from mnd_scripts/examples/example_repeat_loops.mnd — the output below is a real run, not a transcription. Regenerating the page reruns it.

repeat(n): ... end - bounded, compile-time iteration.

The program#

# ============================================================================
# example_repeat_loops.mnd (gap_023)
# ----------------------------------------------------------------------------
# FUNCTIONALITY: `repeat(n): ... end` - bounded, compile-time iteration.
#
# The count is a literal in 1..256 and the parser EXPANDS the body n times,
# so there is no loop in the IR, no dynamic trip count, and no break: the
# program stays total and its cost is visible before it runs. A name the
# body rebinds is loop-carried (the language's last-wins rule); each
# iteration writes a fresh SSA name internally and the original name holds
# the final value.
#
# This is sugar over the same machinery @unroll(n) bounded recursion uses -
# the two forms produce identical results (the gap_023 tests pin that).
#
# RUN: ../maned-run examples/example_repeat_loops.mnd
# ============================================================================
mnd::quantmax=100000;
mnd::quantmin=-100000;
mnd::quantres=0;

in::w = [1];
in::x = [[1,2],[3,4]];

calc::lambda_flow doubling(w) {
    # five doublings: 1 -> 32
    repeat(5):
      w 2 mul w =
    end
} return w;

calc::lambda_flow power_iterate(x) {
    # X^4 by repeated squaring-free multiplication (the ML campaign's
    # "apply the same layer n times" shape, without the recursion boilerplate)
    repeat(3):
      x x matmul x =
    end
} return x;

The run#

$ maned-run mnd_scripts/examples/example_repeat_loops.mnd
flow 'doubling' outputs:
w : [1] = [32]
flow 'power_iterate' outputs:
x : [2x2] = [165751, 241570, 362355, 528106]

Next#

© 2026 Fabiano Dicheti · Found a problem on this page? Open an issue