Examples

conditions.mul

Collatz sequence: how many steps it takes n to reach 1. next is assigned on one path only; the IR merges it with a block parameter.

Source

examples/conditions.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.

module demo.conditions
// Collatz sequence: how many steps it takes `n` to reach 1.
// `next` is assigned on one path only; the IR merges it with a block parameter.
fn steps(n:i32)->i32{if n==1{return 0}var next=n/2 if n%2!=0{next=3*n+1}return 1+steps(next)}pub fn main()->i32{return steps(27)}

Verified IR

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

module demo.conditions

fn @steps(%0: i32) -> i32 {
b0:
  %1 = const.i32 1
  %2 = eq.i32 %0, %1
  branch %2, b1, b2
b1:
  %3 = const.i32 0
  return %3
b2:
  jump b3
b3:
  %4 = const.i32 2
  %5 = div.i32 %0, %4
  %6 = const.i32 2
  %7 = rem.i32 %0, %6
  %8 = const.i32 0
  %9 = ne.i32 %7, %8
  branch %9, b4, b5
b4:
  %10 = const.i32 3
  %11 = mul.i32 %10, %0
  %12 = const.i32 1
  %13 = add.i32 %11, %12
  jump b6(%13)
b5:
  jump b6(%5)
b6(%14: i32):
  %15 = const.i32 1
  %16 = call.i32 @steps(%14)
  %17 = add.i32 %15, %16
  return %17
}

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

Run

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

$ mul run examples/conditions.mul; echo "exit code $?"
exit code 111