Language Tour
Clever's surface syntax reads like Kotlin, while its semantics follow strict systems-programming rules: deterministic left-to-right evaluation, explicit mutability, checked arithmetic by default, and no hidden exceptions or garbage collector.
Variables, Types & Checked Arithmetic
valintroduces an immutable binding;varintroduces a mutable local variable.- Integer types are fixed-width: unsigned
U8,U16,U32,U64,U128and signedI8,I16,I32,I64,I128. - Floating-point types are
F32,F64, and opt-inFastF32(disallowed under--consensus). - Standard
+,-,*,/,%,<<,>>trap on overflow, division by zero, or out-of-range shift. - Wrapping arithmetic is always explicit:
+%,-%,*%.
export fun checkedAndWrapping(a: U64, b: U64): (U64, U64) {
val sum = a +% b // wraps mod 2^64 (never traps)
val hi = mulHi(a, b) // upper 64 bits of exact a * b
(sum, hi)
}
export fun safeAverage(a: U32, b: U32): U32 {
val wide = a.toU64() + b.toU64() // checked + in U64 cannot overflow U32 inputs
(wide / 2u64).truncU32()
}
law wrapping_comm(a: U64, b: U64): a +% b == b +% a
proof by bvClever also provides carry/borrow primitives (addCarry, subBorrow), widening multiply (mulWide, mulHi), bit rotations (rotl, rotr), and bit counts (clz, ctz, popcnt).
Records (data), Enums & Exhaustive when
Records are value types declared with data. Fields are immutable (val); updates produce a new value (often via .copy(...)). Algebraic data types are declared with enum and matched exhaustively with when (no fallthrough, smart-cast access to variant fields after is Variant):
@derive(Eq, Ord)
data Point(val x: I32, val y: I32)
@derive(Eq)
enum Shape {
Dot,
Circle(val radius: U32),
Rect(val w: U32, val h: U32)
}
export fun translateX(p: Point, dx: I32): Point = p.copy(x = p.x + dx)
export fun boundingArea(s: &Shape): U64 = when (s) {
Shape.Dot -> 0u64
is Shape.Circle -> {
val d = s.radius.toU64() * 2u64
d * d
}
is Shape.Rect -> s.w.toU64() * s.h.toU64()
}Option (T?) and Result<T, E>
Clever has no exceptions. Absent values use Option<T> (written T?), and recoverable errors use Result<T, E> (Ok(v) / Err(e)).
- Elvis operator (
?:): fallback value, or earlyreturn/breakonnull. - Safe call (
?.): chains field/method access acrossT?. - Smart casts: after
if (x != null)orif (x == null) return,xhas typeT. - Postfix
?: propagatesnull(in functions returningU?) orErr(e)(in functions returningResult<U, E>). - Explicit unwrap (
!!): traps withtrap: unwraponnullorErr.
enum ParseErr { Empty, BadDigit(val pos: U32, val byte: U8) }
fun findFirst(xs: &Array<U64, 4>, target: U64): U32? {
for (i in 0u32 until 4u32) {
if (xs[i] == target) { return i }
}
null
}
export fun findOrDefault(a: U64, b: U64, target: U64): U32 {
val table = [a, b, a +% b, 0u64]
val idx = findFirst(table, target) ?: return 99u32
idx + 1u32
}
fun parseDigit(b: U8, pos: U32): Result<U32, ParseErr> {
if (b < 48u8 || b > 57u8) { return Err(ParseErr.BadDigit(pos, b)) }
Ok((b - 48u8).toU32())
}
export fun parseTwoDigits(b0: U8, b1: U8): Result<U32, ParseErr> {
val d0 = parseDigit(b0, 0u32)?
val d1 = parseDigit(b1, 1u32)?
Ok(d0 * 10u32 + d1)
}Generics, Traits & @derive
Generics monomorphize cleanly with no backtracking search — trait resolution is local and coherent. Traits can carry laws that every impl inherits (for example, Eq requires reflexive, symmetric, and transitive; Ord requires consistent, antisymmetric, and leTransitive).
@derive(Eq, Ord) generates the trait implementations automatically, and for plain records/enums Clever's Lean backend even proves Eq.lawful automatically.
@derive(Eq, Ord)
data Pair<T>(val first: T, val second: T)
trait Summarizable {
fun score(&this): U64
}
impl Summarizable for Pair<U64> {
fun score(&this): U64 = this.first +% this.second
}
fun <T: Ord> clampOrd(x: T, lo: T, hi: T): T =
if (x < lo) lo else if (hi < x) hi else x
export fun bestScore(a: U64, b: U64): U64 {
val p = Pair(clampOrd(a, 10u64, 100u64), clampOrd(b, 10u64, 100u64))
p.score()
}Loops & Termination Measures
- Bounded loops use
for (i in start until end)(exclusive upper bound). - General
while (cond) decreases (expr)loops carry a termination measure so they remain inside the total logic fragment;partial funallows unmeasured loops in the program fragment.
export fun gcd(a0: U64, b0: U64): U64 {
var a = a0
var b = b0
while (b > 0u64) decreases (b) {
val r = a % b
a = b
b = r
}
a
}Collections: Array, Slice, Buf, Bytes, String, Map, Set
Clever's built-in collections are value types backed by copy-on-write heap buffers (Buf<T>, Bytes = Buf<U8>, UTF-8 String, and deterministic ordered B+tree Map<K, V> / Set<K>):
export fun collectionTour(n: U64): U64 {
// Growable buffer
var nums = Buf<U64>()
for (i in 0u64 until n) {
nums.push(i *% i)
}
// Ordered B+tree map (deterministic iteration order)
var counts = Map<U64, U64>()
for (i in 0u64 until nums.len) {
val key = nums[i] % 7u64
val prev = counts.get(key) ?: 0u64
counts.set(key, prev + 1u64)
}
var total = 0u64
for ((k, v) in counts) {
total = total +% (k *% v)
}
// Byte literals & UTF-8 String templates
val tag = b"sum"
val label = "total=$total"
total +% tag.len +% label.len
}Additional standard library collections (HashMap, Deque, MinHeap, sorting, binary search, JSON, Base64, formatting/parsing) live in lib/std/ (package std).