Examples

effects.mul

Effects: pure (the default) and io (RFC-009). A pure function cannot call an io one; mul check rejects that with MUL-T013.

Source

examples/effects.mul is stored in the compact layout, as AI agents write it: a space only between two words, no other whitespace. The page lays it out for reading; the Compact button shows the stored text, and Copy always copies it.

// Effects: `pure` (the default) and `io` (RFC-009).
// A pure function cannot call an io one; `mul check` rejects that with MUL-T013.
module demo.effects
// Pure: the result depends only on the arguments.
fn square(n:i32)->i32 pure{return n*n}
// No effect written, so this is pure too.
fn sumOfSquares(n:i32)->i32{var total=0 var i=1 while i<=n{total=total+square(i)i=i+1}return total}
// Io may call pure functions, and only io functions may call `print`.
fn report(n:i32)->i32 io{return sumOfSquares(n)}pub fn main()->i32 io{return report(10)}

Verified IR

What mul ir prints after the verifier has accepted the program.

module demo.effects

fn @square(%0: i32) -> i32 {
b0:
  %1 = mul.i32 %0, %0
  return %1
}

fn @sumOfSquares(%0: i32) -> i32 {
b0:
  %1 = const.i32 0
  %2 = const.i32 1
  jump b1(%1, %2)
b1(%3: i32, %4: i32):
  %5 = le.i32 %4, %0
  branch %5, b2, b3
b2:
  %6 = call.i32 @square(%4)
  %7 = add.i32 %3, %6
  %8 = const.i32 1
  %9 = add.i32 %4, %8
  jump b1(%7, %9)
b3:
  return %3
}

fn @report(%0: i32) -> i32 io {
b0:
  %1 = call.i32 @sumOfSquares(%0)
  return %1
}

fn @main() -> i32 io {
b0:
  %0 = const.i32 10
  %1 = call.i32 @report(%0)
  return %1
}

Run

The value main returns is the exit code of the process.

$ mul run examples/effects.mul; echo "exit code $?"
exit code 129