ɛ∞ɜɛ∞ɜ

Learn · Lasm · Tutorials · 2026-11-02

AI-generated. Written by AI (Claude) from the Lasm repository and published automatically. Source · Report a mistake

A Lean function with a proof, deployed

Lean is a programming language and a proof assistant. With Lasm, the function you prove things about is the function your server runs: Lasm compiles that same Lean definition to WebAssembly.

In this tutorial you write a rule for the ?limit= parameter of an HTTP endpoint, prove that it keeps the page size between 1 and 100, and serve it. Then you break the rule and watch the build refuse it.

You need Node.js 24 or newer and npm. CI runs every command on this page and checks every output it shows (how). The finished project is in content/tutorials/proof-in-a-fetch-handler/project.

1. Create the project

npm create @lasm@latest verified-limit -- --template fetch --deploy workers
cd verified-limit

This is the same template as in From zero to Lean on Cloudflare Workers: a handler on the web's Request and Response in Handler.lean, served on Node by Main.lean and on Workers by Export.lean, pure logic in App.lean, and the files for Workers.

2. The rule and its proofs

Replace App.lean with this:

/-! Pure Lean: no JavaScript, so `lake build App` can also check it natively. -/

namespace App

/-- Keep a requested page size between 1 and 100. -/
def clampLimit (n : Nat) : Nat := min 100 (max 1 n)

/-- The page size for `?limit=`: 20 when it is absent or not a number. -/
def pageSize (limit? : Option String) : Nat :=
  match limit?.bind String.toNat? with
  | none => 20
  | some n => clampLimit n

/-- Whatever a client sends, the page size is between 1 and 100. -/
theorem pageSize_bounds (limit? : Option String) :
    1 ≤ pageSize limit? ∧ pageSize limit? ≤ 100 := by
  unfold pageSize clampLimit
  split <;> omega

/-- A size that is already in range is kept as it is. -/
theorem clampLimit_keeps (n : Nat) (h₁ : 1 ≤ n) (h₂ : n ≤ 100) : clampLimit n = n := by
  unfold clampLimit
  omega

-- The build fails if either proof ever depends on `sorry` (or any new axiom).
/-- info: 'App.pageSize_bounds' depends on axioms: [propext, Classical.choice, Quot.sound] -/
#guard_msgs in #print axioms App.pageSize_bounds

/-- info: 'App.clampLimit_keeps' depends on axioms: [propext, Quot.sound] -/
#guard_msgs in #print axioms App.clampLimit_keeps

end App

3. Use it in the handler

Replace Handler.lean with this:

/-
`GET /items?limit=N` answers with the first N items, where N is
`App.pageSize`: the function the theorems in App.lean are about.
-/
import App
import Lasm.JS.Web.Fetch
import Lasm.JS.Web.Url
import Lean.Data.Json
open Lasm.JS Lasm.JS.Web Lean

namespace Handler

def handle (request : Request) : Async Response := do
  let url ← URL.new (← request.url)
  match (← request.method), (← url.pathname) with
  | "GET", "/items" =>
    let size := App.pageSize (← (← url.searchParams).get "limit")
    let items := (List.range size).map (· + 1)
    let body := (Json.mkObj [("size", toJson size), ("items", toJson items)]).compress
    Response.new (some (.usvString body))
      (some { headers := some (.record ⟨#[("content-type", "application/json")]⟩) })
  | _, _ => Response.new (some (.usvString "Not Found\n")) (some { status := some 404 })

end Handler

The handler reads limit from the URL's query (URL.searchParams, as in JavaScript) and calls App.pageSize, the function the theorems are about. There is no second copy of the rule to keep in sync. Main.lean and Export.lean stay as the template wrote them.

4. Build and run it on Node

npm install
npm run build
npm start

Lean checks every proof while it compiles App.lean, so npm run build succeeds only if they all hold. In another terminal:

curl 'http://localhost:3000/items?limit=3'
curl 'http://localhost:3000/items?limit=0'
curl 'http://localhost:3000/items?limit=1000000'
{"items":[1,2,3],"size":3}
{"items":[1],"size":1}

The third answer lists the numbers 1 to 100 and ends like this:

99,100],"size":100}

A client can't make the server build a longer list. Stop the server with Ctrl-C.

5. Break the rule

Suppose someone drops the upper bound. In App.lean, change clampLimit to:

def clampLimit (n : Nat) : Nat := max 1 n

and build again:

npm run build

The build fails. omega reports that it could not prove the goal, and gives a counterexample:

App.lean:18:12: error: omega could not prove the goal:
   18 |   split <;> omega
      |             ^
a possible counterexample may satisfy the constraints
  a ≥ 101
where
 a := ↑n✝

Any size of 101 or more breaks the theorem, so no program is built. Put the line back as it was.

6. Skip the proof

What if someone replaces the proof with sorry instead, which tells Lean to accept the theorem without proof? In pageSize_bounds, replace the line split <;> omega with:

  sorry

and build again:

npm run build

Lean only warns about a sorry:

App.lean:15:8: warning: declaration uses `sorry`

But #print axioms now lists sorryAx, so #guard_msgs fails the build:

App.lean:27:15: error: 'App.pageSize_bounds' depends on axioms: [propext, sorryAx, Classical.choice, Quot.sound]

Put the proof back.

7. Deploy it to Workers

npm run build:workers
npx wrangler dev

wrangler dev serves the Worker locally on port 8787, in workerd. In another terminal:

curl 'http://localhost:8787/items?limit=3'
curl 'http://localhost:8787/items?limit=1000000'

The answers are the same as on Node:

{"items":[1,2,3],"size":3}

and the list of 1 to 100, ending with 99,100],"size":100}. CI runs this step with wrangler 4.152.0 on local workerd, with no account. npx wrangler deploy would upload workers/ to your Cloudflare account. We don't test deployments.

What is proved, and what is trusted

The theorems are about App.pageSize, and the handler runs App.pageSize. What they rely on, and don't prove, is the toolchain: Lean's compiler, Lasm, and the JavaScript or WebAssembly runtime that runs the result. The same split applies to the 294 theorems of Conduit, the RealWorld app written in Lean (PROOFS.md).

Sources

More from Learn LasmAbout Lasm