All languages

LemmaScript notationexperimental

A contract notation for TypeScript: `//@` annotations state what a function must do, and the `lsc` toolchain translates the annotated code to Dafny or Lean 4 to prove it, leaving the shipped JavaScript untouched.

Overview

LemmaScript is a contract notation for TypeScript: //@ annotations state what a function must do, and the lsc toolchain translates the annotated code to Dafny or Lean to prove it, without a new language because the TypeScript is what ships. It exists to close the JavaScript verification gap: assertions catch bugs, proofs exclude them.

Code

//@ requires lo <= hi
//@ ensures \result >= lo && \result <= hi   // \result: the returned value
export function clamp(x: number, lo: number, hi: number): number {
  return x < lo ? lo : x > hi ? hi : x;
}
Contracts are spec expressions over the code's own values — comparisons, ==>, the forall quantifier, \result — and a //@ pure callee may be invoked inside one; the union's tag test lowers to a match arm; invariant/decreases are what make the loop checkable. None of it reaches the shipped JavaScript.

Distinctive traits

  • Contracts are comments, as in ACSL: invisible to tsc, bundlers and the runtime, so adoption is per-function and free.
  • Shallow embedding, not deep: each TypeScript construct becomes the prover's own, so the generated Dafny reads like code a person could have written.
  • One source, two backends: Dafny is primary; Lean 4 via Velvet 2 adds inductive proofs but needs a proof file written separately.
  • Ghost state, //@ assert, and //@ havoc keep out-of-model code (regex, I/O, JSON.parse) in the model as arbitrary typed values the prover assumes nothing about.
  • The arithmetic is idealized: number becomes a mathematical integer, so a proof says nothing about doubles, NaN, or 2⁵³.
  • A runtime guard re-checks preconditions where verified and unverified code meet, so an annotated core ships inside an ordinary TypeScript app.

Status

Tech Preview: MIT-licensed, published on npm as lemmascript with the lsc CLI, and documented at docs.lemmascript.org. Dafny is the primary backend; support, semantics and ergonomics are still evolving.

Sources & references