Examples
collatz.mul
The collatz 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/collatz.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.collatz fn steps(n:i32)->i32{if n==1{return 0}else if n%2==0{return 1+steps(n/2)}return 1+steps(3*n+1)}fn label(count:i32)->string{if count>100{return"long"}return"short"}pub fn main()->i32 io{let count=steps(27)print(label(count))return count}
Verified IR
What mul ir prints after the verifier has accepted the program.
module compare.collatz
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:
%4 = const.i32 2
%5 = rem.i32 %0, %4
%6 = const.i32 0
%7 = eq.i32 %5, %6
branch %7, b3, b4
b3:
%8 = const.i32 1
%9 = const.i32 2
%10 = div.i32 %0, %9
%11 = call.i32 @steps(%10)
%12 = add.i32 %8, %11
return %12
b4:
jump b5
b5:
%13 = const.i32 1
%14 = const.i32 3
%15 = mul.i32 %14, %0
%16 = const.i32 1
%17 = add.i32 %15, %16
%18 = call.i32 @steps(%17)
%19 = add.i32 %13, %18
return %19
}
fn @label(%0: i32) -> string {
b0:
%1 = const.i32 100
%2 = gt.i32 %0, %1
branch %2, b1, b2
b1:
%3 = const.string "long"
return %3
b2:
jump b3
b3:
%4 = const.string "short"
return %4
}
fn @main() -> i32 io {
b0:
%0 = const.i32 27
%1 = call.i32 @steps(%0)
%2 = call.string @label(%1)
print %2
return %1
}
Run
Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from examples/compare/evidence/collatz/run.txt. The value main returns is the exit code.
$ mul run examples/compare/collatz.mul
long
exit code 111
$ mul run --backend cranelift examples/compare/collatz.mul
long
exit code 111
$ go build -o collatz examples/compare/collatz.go && ./collatz
long
exit code 111
$ php examples/compare/collatz.php
long
exit code 111
$ cc -o collatz examples/compare/collatz.c && ./collatz
long
exit code 111
$ rustc --edition 2021 -O -o collatz examples/compare/collatz.rs && ./collatz
long
exit code 111
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/collatz.mul --compare examples/compare/collatz.go examples/compare/collatz.php examples/compare/collatz.c examples/compare/collatz.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/collatz.mul compact 91 base
examples/compare/collatz.go as written 112 +21 +23.1%
examples/compare/collatz.php as written 121 +30 +33.0%
examples/compare/collatz.c as written 130 +39 +42.9%
examples/compare/collatz.rs as written 116 +25 +27.5%
$ mul tokens examples/compare/collatz.mul --compare examples/compare/collatz.go examples/compare/collatz.php examples/compare/collatz.c examples/compare/collatz.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file layout tokens diff %
examples/compare/collatz.mul compact 89 base
examples/compare/collatz.go as written 114 +25 +28.1%
examples/compare/collatz.php as written 120 +31 +34.8%
examples/compare/collatz.c as written 135 +46 +51.7%
examples/compare/collatz.rs as written 120 +31 +34.8%
$ mul tokens examples/compare/collatz.mul --compare examples/compare/collatz.go examples/compare/collatz.php examples/compare/collatz.c examples/compare/collatz.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file layout tokens diff %
examples/compare/collatz.mul compact 89 base
examples/compare/collatz.go as written 114 +25 +28.1%
examples/compare/collatz.php as written 120 +31 +34.8%
examples/compare/collatz.c as written 135 +46 +51.7%
examples/compare/collatz.rs as written 120 +31 +34.8%
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/collatz.go
package main
import (
"fmt"
"os"
)
func steps(n int32) int32 {
if n == 1 {
return 0
} else if n%2 == 0 {
return 1 + steps(n/2)
}
return 1 + steps(3*n+1)
}
func label(count int32) string {
if count > 100 {
return "long"
}
return "short"
}
func main() {
count := steps(27)
fmt.Println(label(count))
os.Exit(int(count))
}
PHP
examples/compare/collatz.php
<?php
function steps(int $n): int
{
if ($n == 1) {
return 0;
} elseif ($n % 2 == 0) {
return 1 + steps(intdiv($n, 2));
}
return 1 + steps(3 * $n + 1);
}
function label(int $count): string
{
if ($count > 100) {
return "long";
}
return "short";
}
$count = steps(27);
echo label($count), "\n";
exit($count);
C
examples/compare/collatz.c
#include <stdint.h>
#include <stdio.h>
static int32_t steps(int32_t n) {
if (n == 1) {
return 0;
} else if (n % 2 == 0) {
return 1 + steps(n / 2);
}
return 1 + steps(3 * n + 1);
}
static const char *label(int32_t count) {
if (count > 100) {
return "long";
}
return "short";
}
int main(void) {
int32_t count = steps(27);
puts(label(count));
return count;
}
Rust
examples/compare/collatz.rs
fn steps(n: i32) -> i32 {
if n == 1 {
return 0;
} else if n % 2 == 0 {
return 1 + steps(n / 2);
}
1 + steps(3 * n + 1)
}
fn label(count: i32) -> &'static str {
if count > 100 {
return "long";
}
"short"
}
fn main() {
let count = steps(27);
println!("{}", label(count));
std::process::exit(count);
}