Examples

digits.mul

The digits 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/digits.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.digits fn digitSum(n:i32)->i32{var sum=0 var rest=n while rest>0{sum=sum+rest%10 rest=rest/10}return sum}fn isHarshad(n:i32)->bool{return n%digitSum(n)==0}pub fn main()->i32 io{if isHarshad(98765){print("harshad")}else{print("not harshad")}return digitSum(98765)}

Verified IR

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

module compare.digits

fn @digitSum(%0: i32) -> i32 {
b0:
  %1 = const.i32 0
  jump b1(%1, %0)
b1(%2: i32, %3: i32):
  %4 = const.i32 0
  %5 = gt.i32 %3, %4
  branch %5, b2, b3
b2:
  %6 = const.i32 10
  %7 = rem.i32 %3, %6
  %8 = add.i32 %2, %7
  %9 = const.i32 10
  %10 = div.i32 %3, %9
  jump b1(%8, %10)
b3:
  return %2
}

fn @isHarshad(%0: i32) -> bool {
b0:
  %1 = call.i32 @digitSum(%0)
  %2 = rem.i32 %0, %1
  %3 = const.i32 0
  %4 = eq.i32 %2, %3
  return %4
}

fn @main() -> i32 io {
b0:
  %0 = const.i32 98765
  %1 = call.bool @isHarshad(%0)
  branch %1, b1, b2
b1:
  %2 = const.string "harshad"
  print %2
  jump b3
b2:
  %3 = const.string "not harshad"
  print %3
  jump b3
b3:
  %4 = const.i32 98765
  %5 = call.i32 @digitSum(%4)
  return %5
}

Run

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

$ mul run examples/compare/digits.mul
not harshad
exit code 35

$ mul run --backend cranelift examples/compare/digits.mul
not harshad
exit code 35

$ go build -o digits examples/compare/digits.go && ./digits
not harshad
exit code 35

$ php examples/compare/digits.php
not harshad
exit code 35

$ cc -o digits examples/compare/digits.c && ./digits
not harshad
exit code 35

$ rustc --edition 2021 -O -o digits examples/compare/digits.rs && ./digits
not harshad
exit code 35

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/digits.mul --compare examples/compare/digits.go examples/compare/digits.php examples/compare/digits.c examples/compare/digits.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/digits.mul  compact           95      base
examples/compare/digits.go   as written       124       +29    +30.5%
examples/compare/digits.php  as written       131       +36    +37.9%
examples/compare/digits.c    as written       144       +49    +51.6%
examples/compare/digits.rs   as written       119       +24    +25.3%
$ mul tokens examples/compare/digits.mul --compare examples/compare/digits.go examples/compare/digits.php examples/compare/digits.c examples/compare/digits.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file                         layout        tokens      diff         %
examples/compare/digits.mul  compact           98      base
examples/compare/digits.go   as written       134       +36    +36.7%
examples/compare/digits.php  as written       135       +37    +37.8%
examples/compare/digits.c    as written       149       +51    +52.0%
examples/compare/digits.rs   as written       130       +32    +32.7%
$ mul tokens examples/compare/digits.mul --compare examples/compare/digits.go examples/compare/digits.php examples/compare/digits.c examples/compare/digits.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file                         layout        tokens      diff         %
examples/compare/digits.mul  compact           98      base
examples/compare/digits.go   as written       134       +36    +36.7%
examples/compare/digits.php  as written       135       +37    +37.8%
examples/compare/digits.c    as written       149       +51    +52.0%
examples/compare/digits.rs   as written       130       +32    +32.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/digits.go

package main

import (
	"fmt"
	"os"
)

func digitSum(n int32) int32 {
	sum := int32(0)
	rest := n
	for rest > 0 {
		sum = sum + rest%10
		rest = rest / 10
	}
	return sum
}

func isHarshad(n int32) bool {
	return n%digitSum(n) == 0
}

func main() {
	if isHarshad(98765) {
		fmt.Println("harshad")
	} else {
		fmt.Println("not harshad")
	}
	os.Exit(int(digitSum(98765)))
}

PHP

examples/compare/digits.php

<?php

function digitSum(int $n): int
{
    $sum = 0;
    $rest = $n;
    while ($rest > 0) {
        $sum = $sum + $rest % 10;
        $rest = intdiv($rest, 10);
    }
    return $sum;
}

function isHarshad(int $n): bool
{
    return $n % digitSum($n) == 0;
}

if (isHarshad(98765)) {
    echo "harshad\n";
} else {
    echo "not harshad\n";
}
exit(digitSum(98765));

C

examples/compare/digits.c

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

static int32_t digitSum(int32_t n) {
    int32_t sum = 0;
    int32_t rest = n;
    while (rest > 0) {
        sum = sum + rest % 10;
        rest = rest / 10;
    }
    return sum;
}

static bool isHarshad(int32_t n) {
    return n % digitSum(n) == 0;
}

int main(void) {
    if (isHarshad(98765)) {
        puts("harshad");
    } else {
        puts("not harshad");
    }
    return digitSum(98765);
}

Rust

examples/compare/digits.rs

fn digit_sum(n: i32) -> i32 {
    let mut sum = 0;
    let mut rest = n;
    while rest > 0 {
        sum += rest % 10;
        rest /= 10;
    }
    sum
}

fn is_harshad(n: i32) -> bool {
    n % digit_sum(n) == 0
}

fn main() {
    if is_harshad(98765) {
        println!("harshad");
    } else {
        println!("not harshad");
    }
    std::process::exit(digit_sum(98765));
}