Examples
fib.mul
The fib 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/fib.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.fib fn fib(n:i32)->i32{if n<2{return n}return fib(n-1)+fib(n-2)}pub fn main()->i32 io{print("fib 12")return fib(12)}
Verified IR
What mul ir prints after the verifier has accepted the program.
module compare.fib
fn @fib(%0: i32) -> i32 {
b0:
%1 = const.i32 2
%2 = lt.i32 %0, %1
branch %2, b1, b2
b1:
return %0
b2:
jump b3
b3:
%3 = const.i32 1
%4 = sub.i32 %0, %3
%5 = call.i32 @fib(%4)
%6 = const.i32 2
%7 = sub.i32 %0, %6
%8 = call.i32 @fib(%7)
%9 = add.i32 %5, %8
return %9
}
fn @main() -> i32 io {
b0:
%0 = const.string "fib 12"
print %0
%1 = const.i32 12
%2 = call.i32 @fib(%1)
return %2
}
Run
Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from examples/compare/evidence/fib/run.txt. The value main returns is the exit code.
$ mul run examples/compare/fib.mul
fib 12
exit code 144
$ mul run --backend cranelift examples/compare/fib.mul
fib 12
exit code 144
$ go build -o fib examples/compare/fib.go && ./fib
fib 12
exit code 144
$ php examples/compare/fib.php
fib 12
exit code 144
$ cc -o fib examples/compare/fib.c && ./fib
fib 12
exit code 144
$ rustc --edition 2021 -O -o fib examples/compare/fib.rs && ./fib
fib 12
exit code 144
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/fib.mul --compare examples/compare/fib.go examples/compare/fib.php examples/compare/fib.c examples/compare/fib.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/fib.mul compact 49 base
examples/compare/fib.go as written 65 +16 +32.7%
examples/compare/fib.php as written 58 +9 +18.4%
examples/compare/fib.c as written 71 +22 +44.9%
examples/compare/fib.rs as written 58 +9 +18.4%
$ mul tokens examples/compare/fib.mul --compare examples/compare/fib.go examples/compare/fib.php examples/compare/fib.c examples/compare/fib.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file layout tokens diff %
examples/compare/fib.mul compact 50 base
examples/compare/fib.go as written 66 +16 +32.0%
examples/compare/fib.php as written 59 +9 +18.0%
examples/compare/fib.c as written 71 +21 +42.0%
examples/compare/fib.rs as written 62 +12 +24.0%
$ mul tokens examples/compare/fib.mul --compare examples/compare/fib.go examples/compare/fib.php examples/compare/fib.c examples/compare/fib.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file layout tokens diff %
examples/compare/fib.mul compact 50 base
examples/compare/fib.go as written 66 +16 +32.0%
examples/compare/fib.php as written 59 +9 +18.0%
examples/compare/fib.c as written 71 +21 +42.0%
examples/compare/fib.rs as written 62 +12 +24.0%
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/fib.go
package main
import (
"fmt"
"os"
)
func fib(n int32) int32 {
if n < 2 {
return n
}
return fib(n-1) + fib(n-2)
}
func main() {
fmt.Println("fib 12")
os.Exit(int(fib(12)))
}
PHP
examples/compare/fib.php
<?php
function fib(int $n): int
{
if ($n < 2) {
return $n;
}
return fib($n - 1) + fib($n - 2);
}
echo "fib 12\n";
exit(fib(12));
C
examples/compare/fib.c
#include <stdint.h>
#include <stdio.h>
static int32_t fib(int32_t n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
int main(void) {
puts("fib 12");
return fib(12);
}
Rust
examples/compare/fib.rs
fn fib(n: i32) -> i32 {
if n < 2 {
return n;
}
fib(n - 1) + fib(n - 2)
}
fn main() {
println!("fib 12");
std::process::exit(fib(12));
}