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:
- elan (reads
lean-toolchainautomatically) clang(for C compilation; forclever wasm, an LLVMclangwithwasm32target +wasm-ld, discovered automatically or set viaCLEVER_CC)- Node.js 24+ (to run Wasm / JS output)
- Python 3.9+ (for test and doc-test tools)
Build the compiler:
export PATH="$HOME/.elan/bin:$PATH"
lake buildThis 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:
- Arithmetic (
+,-,*) is checked by default — overflow traps rather than silently wrapping. export funexposes a function to C, Wasm, and JS hosts.lawstates a machine-checked property right next to the code, verified by Clever's own kernel (proof by bv).
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 bvThe Core Workflow
1. Type-check, ownership-check, and secrecy-check
clever check clamp.clv
# ok: 2 functions, 3 laws2. Run in the reference interpreter
clever run clamp.clv clamp 42 10 20
# 20
clever run clamp.clv midpoint 18446744073709551614 18446744073709551615
# 18446744073709551614If 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.jsonEach 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.jsonclever trust lists five sections:
proven— verified laws, kernel translation theorems, and checks removed by proofchecked— remaining runtime trap sites and translation-validated optimization resultstested— compiler passes validated differentially rather than formally provenassumed— unproven laws,externcontracts, host FFI obligations, intrinsicstrusted— Lean/Clever kernels, C compiler, runtime, CPU
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.rsFor programs with fun main(io: Io) (see Chapter 5), --exe compiles and links a native executable directly:
clever c hello.clv --exe -o hello
./helloHere is a minimal standalone main(io: Io) program:
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.wasmJavaScript (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.mjsLean 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