Examples
fizzbuzz.mul
The fizzbuzz 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/fizzbuzz.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.fizzbuzz fn word(n:i32)->string{if n%15==0{return"fizzbuzz"}else if n%3==0{return"fizz"}else if n%5==0{return"buzz"}return"number"}fn isWord(n:i32)->bool{return n%3==0||n%5==0}pub fn main()->i32 io{var words=0 var n=1 while n<=15{print(word(n))if isWord(n){words=words+1}n=n+1}return words}
Verified IR
What mul ir prints after the verifier has accepted the program.
module compare.fizzbuzz
fn @word(%0: i32) -> string {
b0:
%1 = const.i32 15
%2 = rem.i32 %0, %1
%3 = const.i32 0
%4 = eq.i32 %2, %3
branch %4, b1, b2
b1:
%5 = const.string "fizzbuzz"
return %5
b2:
%6 = const.i32 3
%7 = rem.i32 %0, %6
%8 = const.i32 0
%9 = eq.i32 %7, %8
branch %9, b3, b4
b3:
%10 = const.string "fizz"
return %10
b4:
%11 = const.i32 5
%12 = rem.i32 %0, %11
%13 = const.i32 0
%14 = eq.i32 %12, %13
branch %14, b5, b6
b5:
%15 = const.string "buzz"
return %15
b6:
jump b7
b7:
%16 = const.string "number"
return %16
}
fn @isWord(%0: i32) -> bool {
b0:
%1 = const.i32 3
%2 = rem.i32 %0, %1
%3 = const.i32 0
%4 = eq.i32 %2, %3
branch %4, b2(%4), b1
b1:
%5 = const.i32 5
%6 = rem.i32 %0, %5
%7 = const.i32 0
%8 = eq.i32 %6, %7
jump b2(%8)
b2(%9: bool):
return %9
}
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 15
%5 = le.i32 %3, %4
branch %5, b2, b3
b2:
%6 = call.string @word(%3)
print %6
%7 = call.bool @isWord(%3)
branch %7, b4, b5
b3:
return %2
b4:
%8 = const.i32 1
%9 = add.i32 %2, %8
jump b6(%9)
b5:
jump b6(%2)
b6(%10: i32):
%11 = const.i32 1
%12 = add.i32 %3, %11
jump b1(%10, %12)
}
Run
Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from examples/compare/evidence/fizzbuzz/run.txt. The value main returns is the exit code.
$ mul run examples/compare/fizzbuzz.mul
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
$ mul run --backend cranelift examples/compare/fizzbuzz.mul
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
$ go build -o fizzbuzz examples/compare/fizzbuzz.go && ./fizzbuzz
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
$ php examples/compare/fizzbuzz.php
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
$ cc -o fizzbuzz examples/compare/fizzbuzz.c && ./fizzbuzz
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
$ rustc --edition 2021 -O -o fizzbuzz examples/compare/fizzbuzz.rs && ./fizzbuzz
number
number
fizz
number
buzz
fizz
number
number
fizz
buzz
number
fizz
number
number
fizzbuzz
exit code 7
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/fizzbuzz.mul --compare examples/compare/fizzbuzz.go examples/compare/fizzbuzz.php examples/compare/fizzbuzz.c examples/compare/fizzbuzz.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/fizzbuzz.mul compact 120 base
examples/compare/fizzbuzz.go as written 156 +36 +30.0%
examples/compare/fizzbuzz.php as written 165 +45 +37.5%
examples/compare/fizzbuzz.c as written 179 +59 +49.2%
examples/compare/fizzbuzz.rs as written 155 +35 +29.2%
$ mul tokens examples/compare/fizzbuzz.mul --compare examples/compare/fizzbuzz.go examples/compare/fizzbuzz.php examples/compare/fizzbuzz.c examples/compare/fizzbuzz.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file layout tokens diff %
examples/compare/fizzbuzz.mul compact 115 base
examples/compare/fizzbuzz.go as written 166 +51 +44.3%
examples/compare/fizzbuzz.php as written 171 +56 +48.7%
examples/compare/fizzbuzz.c as written 191 +76 +66.1%
examples/compare/fizzbuzz.rs as written 165 +50 +43.5%
$ mul tokens examples/compare/fizzbuzz.mul --compare examples/compare/fizzbuzz.go examples/compare/fizzbuzz.php examples/compare/fizzbuzz.c examples/compare/fizzbuzz.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file layout tokens diff %
examples/compare/fizzbuzz.mul compact 117 base
examples/compare/fizzbuzz.go as written 166 +49 +41.9%
examples/compare/fizzbuzz.php as written 171 +54 +46.2%
examples/compare/fizzbuzz.c as written 191 +74 +63.2%
examples/compare/fizzbuzz.rs as written 165 +48 +41.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/fizzbuzz.go
package main
import (
"fmt"
"os"
)
func word(n int32) string {
if n%15 == 0 {
return "fizzbuzz"
} else if n%3 == 0 {
return "fizz"
} else if n%5 == 0 {
return "buzz"
}
return "number"
}
func isWord(n int32) bool {
return n%3 == 0 || n%5 == 0
}
func main() {
words := int32(0)
n := int32(1)
for n <= 15 {
fmt.Println(word(n))
if isWord(n) {
words = words + 1
}
n = n + 1
}
os.Exit(int(words))
}
PHP
examples/compare/fizzbuzz.php
<?php
function word(int $n): string
{
if ($n % 15 == 0) {
return "fizzbuzz";
} elseif ($n % 3 == 0) {
return "fizz";
} elseif ($n % 5 == 0) {
return "buzz";
}
return "number";
}
function isWord(int $n): bool
{
return $n % 3 == 0 || $n % 5 == 0;
}
$words = 0;
$n = 1;
while ($n <= 15) {
echo word($n), "\n";
if (isWord($n)) {
$words = $words + 1;
}
$n = $n + 1;
}
exit($words);
C
examples/compare/fizzbuzz.c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
static const char *word(int32_t n) {
if (n % 15 == 0) {
return "fizzbuzz";
} else if (n % 3 == 0) {
return "fizz";
} else if (n % 5 == 0) {
return "buzz";
}
return "number";
}
static bool isWord(int32_t n) {
return n % 3 == 0 || n % 5 == 0;
}
int main(void) {
int32_t words = 0;
int32_t n = 1;
while (n <= 15) {
puts(word(n));
if (isWord(n)) {
words = words + 1;
}
n = n + 1;
}
return words;
}
Rust
examples/compare/fizzbuzz.rs
fn word(n: i32) -> &'static str {
if n % 15 == 0 {
return "fizzbuzz";
} else if n % 3 == 0 {
return "fizz";
} else if n % 5 == 0 {
return "buzz";
}
"number"
}
fn is_word(n: i32) -> bool {
n % 3 == 0 || n % 5 == 0
}
fn main() {
let mut words = 0;
let mut n = 1;
while n <= 15 {
println!("{}", word(n));
if is_word(n) {
words += 1;
}
n += 1;
}
std::process::exit(words);
}