Lean into JavaScript
LeanScript compiles Lean code into JavaScript. Run your code in the browser, Node.js, Cloudflare Workers, or anywhere else you can run JavaScript.
01
All the power of Lean
LeanScript picks up where the Lean compiler leaves off, so your code is checked and optimized just as it is for a native build. Dependent types, proofs, and macros all work, and proofs are erased before any JavaScript is emitted.
02
Coming soon03
Coming soon04
Coming soon
Example 01
Lean in Cloudflare Workers
Deploy your Lean code to Cloudflare's global edge network with Cloudflare Workers.
Worker.lean
import Js
open Std . Async
open Js . Fetch
/-- Responds to `req` with a greeting, echoing the body of a `POST` request. -/
def handle ( req : Request ) : Async Response := do
let body ←
if req . method == "POST" then
pure s!"Hello from Lean! You sent: { ← req . text } \n "
else
pure s!"Hello from Lean! You requested { req . url } \n "
Response . new body { headers := #[( "Content-Type" , "text/plain; charset=utf-8" )] }
/-- The Worker's `fetch` handler. -/
@[ export default . fetch ]
def fetch ( req : Request ) ( _env _ctx : JsValue ) : BaseIO JsValue :=
JsValue . ofAsync ( handle req ) Getting started
Set up LeanScript in your project
LeanScript fits into your existing Lean project and JavaScript package manager.
1Require LeanScript in your Lake package
lakefile.toml
[[ require ]] name = "leanscript" git = "https://github.com/maxdeviant/leanscript" rev = "v0.1.0" 2Install the Vite plugin
Terminal
pnpm add-D @leanscript/[email protected] vite3Add the plugin to your Vite config
vite.config.js
import { defineConfig } from "vite" ; import leanscript from "@leanscript/vite-plugin" ; export default defineConfig ({ plugins :[ leanscript ()], }); 4Load a Lean file on your page
index.html
<!doctype html > < html lang ="en "> < head > < meta charset ="utf-8 "/> < title > My app</ title > </ head > < body > < div id ="app "></ div > < script type ="module "src ="./Main.lean "></ script > </ body > </ html > 5Write some Lean
Main.lean
import Js open Js . Dom def main : IO Unit := do let some app ← ( ← document ) . getElementById "app" | return app . setTextContent "Hello from Lean!"
Check out the leanscript-template-web repository for a full example.