Examples

primes.mul

The primes 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/primes.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.primes fn isPrime(n:i32)->bool{var d=2 while d*d<=n{if n%d==0{return false}d=d+1}return true}fn primesBelow(limit:i32)->i32{var count=0 var n=2 while n<limit{if isPrime(n){count=count+1}n=n+1}return count}pub fn main()->i32 io{print("primes below 100")return primesBelow(100)}

Verified IR

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

module compare.primes

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

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

fn @main() -> i32 io {
b0:
  %0 = const.string "primes below 100"
  print %0
  %1 = const.i32 100
  %2 = call.i32 @primesBelow(%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/primes/run.txt. The value main returns is the exit code.

$ mul run examples/compare/primes.mul
primes below 100
exit code 25

$ mul run --backend cranelift examples/compare/primes.mul
primes below 100
exit code 25

$ go build -o primes examples/compare/primes.go && ./primes
primes below 100
exit code 25

$ php examples/compare/primes.php
primes below 100
exit code 25

$ cc -o primes examples/compare/primes.c && ./primes
primes below 100
exit code 25

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

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/primes.mul --compare examples/compare/primes.go examples/compare/primes.php examples/compare/primes.c examples/compare/primes.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/primes.mul  compact          106      base
examples/compare/primes.go   as written       143       +37    +34.9%
examples/compare/primes.php  as written       153       +47    +44.3%
examples/compare/primes.c    as written       164       +58    +54.7%
examples/compare/primes.rs   as written       134       +28    +26.4%
$ mul tokens examples/compare/primes.mul --compare examples/compare/primes.go examples/compare/primes.php examples/compare/primes.c examples/compare/primes.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file                         layout        tokens      diff         %
examples/compare/primes.mul  compact          102      base
examples/compare/primes.go   as written       147       +45    +44.1%
examples/compare/primes.php  as written       155       +53    +52.0%
examples/compare/primes.c    as written       169       +67    +65.7%
examples/compare/primes.rs   as written       141       +39    +38.2%
$ mul tokens examples/compare/primes.mul --compare examples/compare/primes.go examples/compare/primes.php examples/compare/primes.c examples/compare/primes.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file                         layout        tokens      diff         %
examples/compare/primes.mul  compact          102      base
examples/compare/primes.go   as written       147       +45    +44.1%
examples/compare/primes.php  as written       155       +53    +52.0%
examples/compare/primes.c    as written       169       +67    +65.7%
examples/compare/primes.rs   as written       143       +41    +40.2%

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/primes.go

package main

import (
	"fmt"
	"os"
)

func isPrime(n int32) bool {
	d := int32(2)
	for d*d <= n {
		if n%d == 0 {
			return false
		}
		d = d + 1
	}
	return true
}

func primesBelow(limit int32) int32 {
	count := int32(0)
	n := int32(2)
	for n < limit {
		if isPrime(n) {
			count = count + 1
		}
		n = n + 1
	}
	return count
}

func main() {
	fmt.Println("primes below 100")
	os.Exit(int(primesBelow(100)))
}

PHP

examples/compare/primes.php

<?php

function isPrime(int $n): bool
{
    $d = 2;
    while ($d * $d <= $n) {
        if ($n % $d == 0) {
            return false;
        }
        $d = $d + 1;
    }
    return true;
}

function primesBelow(int $limit): int
{
    $count = 0;
    $n = 2;
    while ($n < $limit) {
        if (isPrime($n)) {
            $count = $count + 1;
        }
        $n = $n + 1;
    }
    return $count;
}

echo "primes below 100\n";
exit(primesBelow(100));

C

examples/compare/primes.c

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

static bool isPrime(int32_t n) {
    int32_t d = 2;
    while (d * d <= n) {
        if (n % d == 0) {
            return false;
        }
        d = d + 1;
    }
    return true;
}

static int32_t primesBelow(int32_t limit) {
    int32_t count = 0;
    int32_t n = 2;
    while (n < limit) {
        if (isPrime(n)) {
            count = count + 1;
        }
        n = n + 1;
    }
    return count;
}

int main(void) {
    puts("primes below 100");
    return primesBelow(100);
}

Rust

examples/compare/primes.rs

fn is_prime(n: i32) -> bool {
    let mut d = 2;
    while d * d <= n {
        if n % d == 0 {
            return false;
        }
        d += 1;
    }
    true
}

fn primes_below(limit: i32) -> i32 {
    let mut count = 0;
    let mut n = 2;
    while n < limit {
        if is_prime(n) {
            count += 1;
        }
        n += 1;
    }
    count
}

fn main() {
    println!("primes below 100");
    std::process::exit(primes_below(100));
}