Quickstart

CleverLang isn’t public yet, so you can’t follow the build steps today. The Run buttons work right here. In this book, Clever is short for CleverLang.

This chapter takes you from a fresh checkout to compiling, running, proving, and auditing your first Clever program across C, WebAssembly, and JavaScript.

Prerequisites & Building clever

Clever's compiler is written in Lean 4.34.0 (core Lean only — no Mathlib). You will need:

Build the compiler:

export PATH="$HOME/.elan/bin:$PATH"
lake build

This produces .lake/build/bin/clever. For convenience in this book, we assume:

alias clever="$PWD/.lake/build/bin/clever"

Your First Verified Program

Save the following as clamp.clv. Notice three things immediately:

  1. Arithmetic (+, -, *) is checked by default — overflow traps rather than silently wrapping.
  2. export fun exposes a function to C, Wasm, and JS hosts.
  3. law states a machine-checked property right next to the code, verified by Clever's own kernel (proof by bv).
Proven
export fun clamp(x: U64, lo: U64, hi: U64): U64 {
  if (lo > hi) { lo }
  else if (x < lo) { lo }
  else if (x > hi) { hi }
  else { x }
}

export fun midpoint(a: U64, b: U64): U64 {
  // Overflow-free midpoint: never traps even when a + b >= 2^64
  val lo = if (a < b) { a } else { b }
  val hi = if (a < b) { b } else { a }
  lo + ((hi - lo) >> 1)
}

law clamp_in_bounds(x: U64, lo: U64, hi: U64):
  lo > hi || (clamp(x, lo, hi) >= lo && clamp(x, lo, hi) <= hi)
  proof by bv

law clamp_idempotent(x: U64, lo: U64, hi: U64):
  clamp(clamp(x, lo, hi), lo, hi) == clamp(x, lo, hi)
  proof by bv

law midpoint_in_range(a: U64, b: U64):
  (a <= b && midpoint(a, b) >= a && midpoint(a, b) <= b) ||
  (b < a && midpoint(a, b) >= b && midpoint(a, b) <= a)
  proof by bv
midpoint()
clamp()

The Core Workflow

1. Type-check, ownership-check, and secrecy-check

clever check clamp.clv
# ok: 2 functions, 3 laws

2. Run in the reference interpreter

clever run clamp.clv clamp 42 10 20
# 20

clever run clamp.clv midpoint 18446744073709551614 18446744073709551615
# 18446744073709551614

If an expression traps (overflow, division by zero, out-of-bounds index, failed requires), clever run prints trap: <reason> and exits with code 3.

3. Prove laws and emit a proof manifest

clever prove clamp.clv -o clamp.proofs.json

Each proof by bv law is translated from Core, bit-blasted, refuted by SAT, and its LRAT certificate is verified by Clever's kernel (proven (clever kernel)). The manifest clamp.proofs.json records each law's proof status keyed by the cryptographic hash of the code.

4. Inspect the trust report

clever trust clamp.clv --use-proofs clamp.proofs.json

clever trust lists five sections:

Use --json for machine-readable output, or pass --trust-report report.json directly to clever c or clever wasm.

Compiling to Every Target

From the same clamp.clv source:

Native C (library or standalone executable)

# Emit C11 source (build with clang -std=c11 -O3)
clever c clamp.clv -o clamp.c --use-proofs clamp.proofs.json

# Generate a matching C header or Rust FFI module with layout assertions
clever header clamp.clv -o clamp.h
clever rust clamp.clv -o clamp.rs

For programs with fun main(io: Io) (see Chapter 5), --exe compiles and links a native executable directly:

clever c hello.clv --exe -o hello
./hello

Here is a minimal standalone main(io: Io) program:

Checked
fun main(io: Io) {
  io.out.println(b"Hello from Clever!")
}

Run it in the interpreter without even invoking a C compiler:

clever run hello.clv main
# Hello from Clever!

WebAssembly (clever wasm)

Compiles through freestanding wasm32 C and clang/wasm-ld (the consensus route):

clever wasm clamp.clv -o clamp.wasm

JavaScript (clever js)

Emits a zero-dependency ES module (64-bit and 128-bit arithmetic automatically legalized to 32-bit limbs):

clever js clamp.clv -o clamp.mjs

Lean 4 Model (clever lean)

Emits a core Lean 4.34 module with every function and law as a Lean theorem:

clever lean clamp.clv -o ClampModel.lean