Examples

abundant.mul

The abundant program written in MUL, Go, PHP, C and Rust, with the same output and exit code, and the token count of each version.

Source

examples/compare/abundant.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 compare.abundant fn divisorSum(n:i32)->i32{var sum=0 var d=1 while d<n{if n%d==0{sum=sum+d}d=d+1}return sum}fn isAbundant(n:i32)->bool{return divisorSum(n)>n}pub fn main()->i32 io{var count=0 var n=1 while n<100{if isAbundant(n){count=count+1}n=n+1}print("abundant below 100")return count}

Verified IR

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

module compare.abundant

fn @divisorSum(%0: i32) -> i32 {
b0:
  %1 = const.i32 0
  %2 = const.i32 1
  jump b1(%1, %2)
b1(%3: i32, %4: i32):
  %5 = lt.i32 %4, %0
  branch %5, b2, b3
b2:
  %6 = rem.i32 %0, %4
  %7 = const.i32 0
  %8 = eq.i32 %6, %7
  branch %8, b4, b5
b3:
  return %3
b4:
  %9 = add.i32 %3, %4
  jump b6(%9)
b5:
  jump b6(%3)
b6(%10: i32):
  %11 = const.i32 1
  %12 = add.i32 %4, %11
  jump b1(%10, %12)
}

fn @isAbundant(%0: i32) -> bool {
b0:
  %1 = call.i32 @divisorSum(%0)
  %2 = gt.i32 %1, %0
  return %2
}

fn @main() -> i32 io {
b0:
  %0 = const.i32 0
  %1 = const.i32 1
  jump b1(%0, %1)
b1(%2: i32, %3: i32):
  %4 = const.i32 100
  %5 = lt.i32 %3, %4
  branch %5, b2, b3
b2:
  %6 = call.bool @isAbundant(%3)
  branch %6, b4, b5
b3:
  %12 = const.string "abundant below 100"
  print %12
  return %2
b4:
  %7 = const.i32 1
  %8 = add.i32 %2, %7
  jump b6(%8)
b5:
  jump b6(%2)
b6(%9: i32):
  %10 = const.i32 1
  %11 = add.i32 %3, %10
  jump b1(%9, %11)
}

Run

Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from examples/compare/evidence/abundant/run.txt. The value main returns is the exit code.

$ mul run examples/compare/abundant.mul
abundant below 100
exit code 21

$ mul run --backend cranelift examples/compare/abundant.mul
abundant below 100
exit code 21

$ go build -o abundant examples/compare/abundant.go && ./abundant
abundant below 100
exit code 21

$ php examples/compare/abundant.php
abundant below 100
exit code 21

$ cc -o abundant examples/compare/abundant.c && ./abundant
abundant below 100
exit code 21

$ rustc --edition 2021 -O -o abundant examples/compare/abundant.rs && ./abundant
abundant below 100
exit code 21

Tokens

Token count of each file, MUL as the base: first the offline estimate of mul tokens, then the same files counted with the real BPE vocabularies (--tokenizer-cmd). The estimate applies fixed rules fitted to the pilot study (design document, chapter 23) instead of a learned vocabulary of merges, so it is a few percent off on each file; quote the BPE numbers.

$ mul tokens examples/compare/abundant.mul --compare examples/compare/abundant.go examples/compare/abundant.php examples/compare/abundant.c examples/compare/abundant.rs
tokenizer: estimate
note: offline estimate, not a real tokenizer (about 5% per file on GPT-style BPE); use --tokenizer-cmd for exact counts
file                           layout        tokens      diff         %
examples/compare/abundant.mul  compact          113      base
examples/compare/abundant.go   as written       153       +40    +35.4%
examples/compare/abundant.php  as written       163       +50    +44.2%
examples/compare/abundant.c    as written       175       +62    +54.9%
examples/compare/abundant.rs   as written       143       +30    +26.5%
$ mul tokens examples/compare/abundant.mul --compare examples/compare/abundant.go examples/compare/abundant.php examples/compare/abundant.c examples/compare/abundant.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file                           layout        tokens      diff         %
examples/compare/abundant.mul  compact          112      base
examples/compare/abundant.go   as written       163       +51    +45.5%
examples/compare/abundant.php  as written       168       +56    +50.0%
examples/compare/abundant.c    as written       187       +75    +67.0%
examples/compare/abundant.rs   as written       155       +43    +38.4%
$ mul tokens examples/compare/abundant.mul --compare examples/compare/abundant.go examples/compare/abundant.php examples/compare/abundant.c examples/compare/abundant.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file                           layout        tokens      diff         %
examples/compare/abundant.mul  compact          108      base
examples/compare/abundant.go   as written       160       +52    +48.1%
examples/compare/abundant.php  as written       165       +57    +52.8%
examples/compare/abundant.c    as written       184       +76    +70.4%
examples/compare/abundant.rs   as written       152       +44    +40.7%

The same program in other languages

make compare checks that each of them prints the same output and exits with the same code.

Go

examples/compare/abundant.go

package main

import (
	"fmt"
	"os"
)

func divisorSum(n int32) int32 {
	sum := int32(0)
	d := int32(1)
	for d < n {
		if n%d == 0 {
			sum = sum + d
		}
		d = d + 1
	}
	return sum
}

func isAbundant(n int32) bool {
	return divisorSum(n) > n
}

func main() {
	count := int32(0)
	n := int32(1)
	for n < 100 {
		if isAbundant(n) {
			count = count + 1
		}
		n = n + 1
	}
	fmt.Println("abundant below 100")
	os.Exit(int(count))
}

PHP

examples/compare/abundant.php

<?php

function divisorSum(int $n): int
{
    $sum = 0;
    $d = 1;
    while ($d < $n) {
        if ($n % $d == 0) {
            $sum = $sum + $d;
        }
        $d = $d + 1;
    }
    return $sum;
}

function isAbundant(int $n): bool
{
    return divisorSum($n) > $n;
}

$count = 0;
$n = 1;
while ($n < 100) {
    if (isAbundant($n)) {
        $count = $count + 1;
    }
    $n = $n + 1;
}
echo "abundant below 100\n";
exit($count);

C

examples/compare/abundant.c

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

static int32_t divisorSum(int32_t n) {
    int32_t sum = 0;
    int32_t d = 1;
    while (d < n) {
        if (n % d == 0) {
            sum = sum + d;
        }
        d = d + 1;
    }
    return sum;
}

static bool isAbundant(int32_t n) {
    return divisorSum(n) > n;
}

int main(void) {
    int32_t count = 0;
    int32_t n = 1;
    while (n < 100) {
        if (isAbundant(n)) {
            count = count + 1;
        }
        n = n + 1;
    }
    puts("abundant below 100");
    return count;
}

Rust

examples/compare/abundant.rs

fn divisor_sum(n: i32) -> i32 {
    let mut sum = 0;
    let mut d = 1;
    while d < n {
        if n % d == 0 {
            sum += d;
        }
        d += 1;
    }
    sum
}

fn is_abundant(n: i32) -> bool {
    divisor_sum(n) > n
}

fn main() {
    let mut count = 0;
    let mut n = 1;
    while n < 100 {
        if is_abundant(n) {
            count += 1;
        }
        n += 1;
    }
    println!("abundant below 100");
    std::process::exit(count);
}