Skip to content

VIP-43: theorem and spec, proofs in TypeScript's own syntax

Status: accepted by Will, 2026-11-07. Proposed by Will on 2026-11-06. The compiler implements spec function, erased proof { } blocks and the vibelang prove subset for plain functions. theorem, the proof audit and vibe.proofDir remain unimplemented. Appendix F of the specification states the rules.

  • Clarified 2026-11-11: outside the alpha. This VIP stays accepted by Will, but it is outside the alpha's supported set, which is the core VIPs numbered 1 to 41 (VIPs). At that date, the published compiler implemented none of it, so no opt-in existed.

  • Clarified 2026-11-14: implementation status. spec function, erased proof { } blocks and vibelang prove for plain functions are implemented; theorem, the proof audit and vibe.proofDir are not. The VIP remains outside the alpha's core supported set.

Will's words

"for the vip-43 I read the review and I think for all my questions the agents best judgement I agree with"

"I already approved 43 vip"

One sentence. theorem declares a fact about VibeLang's plain functions in ordinary TypeScript-shaped syntax, the compiler erases it from emitted code, and vibelang prove proves it by lowering to LemmaScript's annotated TypeScript and running LemmaScript's compiler with Dafny.

One paragraph. VibeLang's plain functions are close to the pure, functional TypeScript that LemmaScript can verify. LemmaScript writes specifications as //@ comments. This VIP gives VibeLang first-class syntax for the same ideas: a theorem looks like a function and reads like a test, except it holds for every input in the model. Only theorem, spec and proof are new words. Inside them, proof statements read as calls that existing tools can parse. All of it erases, so a proof never changes what runs. VibeLang does not reimplement verification: it generates LemmaScript's input and uses LemmaScript's compiler and Dafny.

Decision

// compiler-gap: pending VIP-43
export function clamp(x: number, lo: number, hi: number): number {
  if (x < lo) return lo
  if (x > hi) return hi
  return x
}
 
theorem clampInRange(x: number, lo: number, hi: number) {
  assume(lo <= hi)
  const result = clamp(x, lo, hi)
  assert(lo <= result && result <= hi)
}
$ vibelang prove
proved  clampInRange  src/clamp.vibe:7
1 proved, 0 not proved, 0 trusted.
Model: number is an unbounded integer: NaN, infinities, -0, fractions and the 2^53 limit are outside these proofs.
Verified by LemmaScript 0.6.6 (MIT, Midspiral, https://github.com/midspiral/LemmaScript) with Dafny 4.11.0.

assume states a hypothesis. assert states what must be proved, with no \result, because the fact is about a call you can name. An if around an assert is implication. xs.every is the universal quantifier and xs.some the existential one.

// compiler-gap: pending VIP-43
function sum(xs: readonly number[]): number {
  return xs.length === 0 ? 0 : xs[0] + sum(xs.slice(1))
}
 
theorem sumNonNegative(xs: readonly number[]) {
  assume(xs.every((x) => x >= 0))
  assert(sum(xs) >= 0)
}
 
theorem searchFinds(xs: readonly number[], target: number) {
  const index = linearSearch(xs, target)
  if (index >= 0) assert(xs[index] === target)
}

sumNonNegative needs no tactic: a theorem that calls no loop function lowers to a Dafny lemma, and Dafny's automatic induction proves it.

Facts about a point in running code go in a proof { } block at that point. A function's leading block is its contract; ensures((r) => Q) states what every returned r satisfies, and a loop's leading block holds its invariants:

// compiler-gap: pending VIP-43
export function linearSearch(xs: readonly number[], target: number): number {
  proof {
    ensures((r) => r < xs.length)
    ensures((r) => r < 0 || xs[r] === target)
  }
  let i = 0
  while (i < xs.length) {
    proof {
      invariant(0 <= i && i <= xs.length)
      invariant(!xs.slice(0, i).includes(target))
      decreases(xs.length - i)
    }
    if (xs[i] === target) return i
    i += 1
  }
  return -1
}

Proof-only code and trusted code use familiar words:

// compiler-gap: pending VIP-43
spec function isSortedFrom(xs: readonly number[], i: number): boolean {   // proofs only, erased
  proof {
    assume(0 <= i)
    decreases(xs.length - i)
  }
  return i + 1 >= xs.length || (xs[i] <= xs[i + 1] && isSortedFrom(xs, i + 1))
}
 
declare function matchesPattern(text: string, pattern: string): boolean   // trusted, deterministic, not proved

Rules

  • theorem, spec and proof are contextual keywords, like effect. The name, function or { follows on the same line, so existing identifiers keep working.
  • proof { } is a block inside running code. It holds proof statements and erases entirely. A theorem body is a proof context without one. A spec function is an erased plain function; its contract is a leading proof { } block, as for any function.
  • assume, assert, ensures, invariant and decreases are proof statements only in a proof context: a proof { } block or a theorem. Anywhere else they are ordinary names, so a project's own assert keeps working. by is reserved there: this version has no tactics.
  • A proof context runs nothing. It emits no JavaScript and no declarations, so VIP-31's runtime guarantees are untouched.
  • Theorems cover plain functions only. An effect function can't be stated in a theorem yet, because LemmaScript verifies a pure functional subset and doesn't support await.
  • Proofs are about integers. LemmaScript models number as an unbounded integer and has no floating-point model, so every run prints the Model: line. Verified code keeps to the part of TypeScript where that model and LemmaScript's translation match JavaScript: no mutation, no identity comparison of objects or arrays, no null, no %, no / outside Math.floor, no fractional or unsafe literals, no bitwise operators, no casts and no function-typed values.
  • A function from a .ts or .js file is trusted by default: proofs see its signature, never its body, and assume nothing about two calls. Every run lists it, on its own line and in the summary. vibelang prove --strict refuses it. A declare function is the explicit trust form, and proofs assume it is deterministic.

Reusing LemmaScript

LemmaScript, by Midspiral, is an MIT-licensed verification toolchain for TypeScript. This VIP stands on its work. VibeLang credits the project in its documentation and in the output of its proof command.

What we useWhat it does
The lemmascript npm package, pinned to 0.6.6, and its lsc CLICompiles annotated TypeScript to Dafny and runs the check
Its //@ annotation languageThe input format VibeLang generates
Its specification of the verified TypeScript subsetDefines which code can be proved, inside the narrower subset VibeLang checks first
Dafny 4.11.0, through LemmaScriptThe prover that checks the proofs
Its split of generated files and hand-written proofsWhere manual proof work lives; a hand-written addition may prove but never assume

VibeLang owns only the syntax, its checks and the lowering. LemmaScript stays an optional development dependency, installed by a project that uses theorem, and never a runtime dependency.

Compile to its comments

vibelang prove lowers each .vibe module with a theorem or proof block to TypeScript with //@ comments, in a generated file, and runs lsc regen --backend=dafny:

VibeLangLemmaScript
theorem name(args) { ... }A generated function with //@ verify
proof { ... }Its statements, as //@ comments at that point
assume(P)//@ requires P
assert(Q) in a theorem that calls no loop function//@ ensures Q, so Dafny proves it as a lemma
assert(Q) elsewhere//@ assert Q
ensures((r) => Q)//@ ensures Q, with \result for r
invariant(P)//@ invariant P
decreases(E)//@ decreases E
xs.every((x) => P)forall(k: nat, ((k < xs.length) ==> P'))
spec functionAn ordinary function with //@ verify
declare function//@ extern and //@ pure
A function imported from a .ts file//@ extern, impure
// generated from clampInRange
export function clampInRange(x: number, lo: number, hi: number): boolean {
  //@ verify
  //@ requires (lo <= hi)
  //@ ensures (lo <= clamp(x, lo, hi))
  //@ ensures (clamp(x, lo, hi) <= hi)
  return true
}

This is the simplest route. The comment language is LemmaScript's documented public interface. It ships as a CLI with no library entry point, so calling its internal IR would mean forking a tech preview and tracking its changes. Comments need no fork, and improvements in LemmaScript arrive with a version bump. If its package later offers a stable IR API, the lowering can target that without changing this syntax.

Verified: with lemmascript 0.6.6 and Dafny 4.11.0, the generated clampInRange, sumNonNegative and searchFinds files report 1 verified, 0 errors, 4 verified, 0 errors and 5 verified, 0 errors. Each restriction in Rules answers a program that proved a false claim with 0 errors: an aliased array write, [[]].includes([]), null === undefined, (xs.length - 3) % 2 === 1 for an empty xs, 0.1 + 0.2 === 0.3, and rnd() === rnd() for an imported rnd. The specification's Appendix F lists each one.

Alternatives

  • Write //@ comments in .vibe. No compiler work, but specifications stay invisible to the type checker, and a typo is a comment.
  • Vendor the IR and emitters. More direct, but VibeLang would own a verifier. Rejected until a stable API exists.
  • Lean-style theorem name(...): P := by tactic. Faithful to Lean, but it reads as a foreign language inside TypeScript. Will rejected it.
  • bigint-only theorems. Faithful arithmetic, but most code uses number, so most theorems could not be stated.
  • Lean as a backend. LemmaScript's Lean backend needs annotations VibeLang has no syntax for and a hand-written proof file for every function.

Settled questions

  1. by. Reserved and rejected in proof contexts. Dafny's automatic induction proves this VIP's examples, and hand-written steps go in the generated Dafny file.
  2. Where theorems live. In any .vibe module, beside their functions or in a module that imports them. No .proof.vibe file kind.
  3. Source locations. vibelang prove maps each Dafny error to the .vibe assertion, contract or declaration it came from.
  4. The trust boundary. The lowering, LemmaScript's translation, the integer model, trusted functions, run-time preconditions, the call stack, and Dafny with its Z3. Everything else is checked, and a hand-written assumption is an error.
  5. The backend. Dafny only, pinned to 4.11.0.
  6. The command and codes. vibelang prove [-p <project>] [--strict]; VIP-34's block EK43xx, with the rules numbered from 4301.

Maintainers' decisions

These five choices were made while implementing spec functions, proof blocks and vibelang prove, under the pre-alpha standing approval. Will has not reviewed these choices.

  • Clarified 2026-11-10: proof statement types. In a proof context, assume, assert, invariant, decreases and ensures bind to no declaration. The checker types assume(P), assert(Q) and invariant(P) as calls to (condition: boolean) => void, decreases(E) as a call to (measure: number | bigint) => void, and ensures((r) => Q) as a call to (postcondition: (r: R) => boolean) => void, where R is the enclosing function's return type. These are the ambient functions of the TypeScript projection, so a wrong argument reports the TS2345 or TS2554 that tsc reports on the projection.
  • Clarified 2026-11-10: imports used only by proofs. A name that only proof blocks and spec functions use does not keep its import in the emitted JavaScript. This is how the lowering meets Appendix F's "import elision included".
  • Clarified 2026-11-10: one EK4314 per spec function. A spec function whose body holds several constructs outside the transparent subset reports EK4314 once, at the first of them.
  • Clarified 2026-11-10: trusted functions in order. vibelang prove lists trusted functions, on their own lines and in the summary, in lowering order: by module path, then by source position.
  • Clarified 2026-11-10: the proof directory. Until vibe.proofDir is implemented, vibelang prove writes its generated files to proofs in the selected config's directory, and refuses with EK4316 a module that has a root outside that directory.