Maned

Stack operators

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

dup, drop, swap, over, rot, -rot — reshaping the operand stack directly, without inventing a name for every intermediate value.

The program#

# ============================================================================
# example_stack_ops.mnd
# ----------------------------------------------------------------------------
# FUNCTIONALITY: dup, drop, swap, over, rot, -rot -- reshaping the operand
# stack directly, without inventing a name for every intermediate value.
#
# These are resolved by the PARSER while it reduces the RPN token stream, so
# they cost nothing at run time: `x dup matmul` produces exactly the graph
# `matmul(x, x)`, with x referenced twice. They are notation, not instructions.
#
#   dup   [.. a]        -> [.. a a]        duplicate the top
#   drop  [.. a]        -> [..]            discard the top
#   swap  [.. a b]      -> [.. b a]        exchange the top two
#   over  [.. a b]      -> [.. a b a]      copy the second over the top
#   rot   [.. a b c]    -> [.. b c a]      rotate the third to the top
#   -rot  [.. a b c]    -> [.. c a b]      rotate the top down to third
#
# STYLE: reach for a named binding the moment a stack sequence stops being
# obvious at a glance. These exist to remove *noise*, not to win at golf.
#
# RUN: ../maned-run examples/example_stack_ops.mnd
# ============================================================================
mnd::quantmax=1000;
mnd::quantmin=-1000;
mnd::quantres=0;

in::m = [[1,2],[3,4]];
in::p = 10;
in::q = 3;
in::r = 100;

calc::lambda_flow stack_shapes(m, p, q, r) {
    # dup: use one value as BOTH operands. m * m without naming m twice.
    m dup matmul squared =

    # over: reuse the SECOND operand without naming it again.
    # [p q] -over-> [p q p], then sub pops the top two (q, p) and computes
    # q - p = 3 - 10 = -7, with the original p still on the stack beneath it.
    # A leftover operand is silently discarded by the assignment (and
    # maned-lint warns about it since lang_050), so drop it explicitly.
    p q over sub swap drop over_result =

    # swap: reverse operand order for a non-commutative operation.
    p q sub p_minus_q =
    p q swap sub q_minus_p =

    # drop: evaluate something and deliberately keep the OTHER value. This is
    # the K combinator (see example_lambda_birds.mnd).
    p q drop kept_first =

    # rot / -rot: reorder three operands. [p q r] -rot-> [q r p], so `add`
    # pops (r, p) and yields r + p = 110, with q still underneath (dropped
    # explicitly, as above).
    p q r rot add swap drop rotated =

    # -rot is the inverse rotation: [p q r] --rot-> [r p q], `add` gives p + q
    # with r left beneath it.
    p q r -rot add swap drop unrotated =
} return squared, over_result, p_minus_q, q_minus_p,
         kept_first, rotated, unrotated;

The run#

$ maned-run mnd_scripts/examples/example_stack_ops.mnd
flow 'stack_shapes' outputs:
squared : [2x2] = [7, 10, 15, 22]
over_result : scalar = -7
p_minus_q : scalar = 7
q_minus_p : scalar = -7
kept_first : scalar = 10
rotated : scalar = 110
unrotated : scalar = 13

Next#

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