Substrate 发布面向 WebAssembly 的 Lisp 方言
Substrate 是一门面向 WebAssembly 的 Lisp 方言,融合了 C 的底层控制力与 Racket 风格卫生宏,可直接在语言内定义新语言与 DSL。其编译器自托管、运行于浏览器,官网与后端均由 Substrate 自身编写,展示了语言自举与沙箱化 Web 开发的可行性。
报道正文
本文目录14 个章节
Substrate combines the low-level power of C with Racket-style hygienic macros for defining new languages and DSLs. Its compiler is self-hosted, fast, and runs in the browser.
Try SubstrateCompile the compiler!counter.subSubstrate
(require-unqualified web-ui)
(struct state [count i32])
(variant message [increment] [decrement] [reset])
(define (update [s (ptr state)] [m (ptr message)])
(match m
[.increment (+= (-> s count) 1)]
[.decrement (-= (-> s count) 1)]
[.reset (set! (-> s count) 0)]))
(component (counter)
#:state [s state]
#:messages message
#:update update
(div ([class "counter"])
(button ([on click .decrement]) "-")
(output (int (-> s count)))
(button ([on click .increment]) "+")
(when (!= (-> s count) 0)
(button ([class "reset"] [on click .reset]) "reset"))))live0This webpage and the server it runs on are written in Substrate using the framework above. Its source ↗
Domain-specific abstractions.
Sandboxed security.
01
Languages as libraries
Hygienic macros and module hooks allow you to use Substrate to define new languages and DSLs as importable libraries.
;; bend2 gives new syntax for a dependently typed language
(language bend2)
(type N Data [Z] [S [p : N]]) ; natural number
;; a law states what its def must prove
(law (add [a : N] [b : N]) N)
(def (add a b)
(match a
[Z b]
[(S p) (S (add p b))]))
;; prove that 0 + b = b for every b
(law (zero_add [b : N]) (== (add Z b) b N))
(def (zero_add b) refl)02
Flexible memory model
Substrate has simple C-like memory management. Languages or DSLs can build memory-safe runtimes on top of it.
;; a reference-counted Scheme, no tracing GC
(language scheme)
(define (numbers n)
(let loop ([i 0] [acc '()])
(if (= i n) acc (loop (+ i 1) (cons i acc)))))
(define (demo)
(let* ([before (live-nodes)] [xs (numbers 1000)])
(printf "~a live, " (- (live-nodes) before))
(printf "length ~a, " (length xs))
(printf "~a live\n" (- (live-nodes) before))))
(demo)
;; 1000 live, length 1000, 0 live03
Run code safely
Substrate programs are sandboxed by default and can only access your filesystem and network when and how you allow them. The build system and compiler are also sandboxed.
$ substrate run upload.sub
fetch: example.com is not granted (--allow-net)04
Fast compilation and execution
Substrate compiles code at ~47k lines per second, and its programs run within about 1.4× of C compiled to Wasm. The compiler builds itself in about a quarter of a second.
run time, lower is faster
Substrate1.43×C -O2 (Wasm)1.00×C -O2 (native)0.70×
lines compiled per second
Substrate46.5kC -O214.3k
Examples
Score 0 Best 0
Click the board to play.
Snake
The game, written in Substrate.
Bend 2
A Substrate port of Bend 2, a dependently typed language.
web-ui
A UI library for web programs, written in Substrate.
substrate-sqlite
SQLite compiled to WebAssembly, with an API in Substrate.
(from orders
(group (month date))
(select (month date) (sum total)))SQLite explorer
An example app that builds a dashboard using substrate-sqlite, illustrating how native code can be linked with Substrate.
Open the explorerSource on GitHub ↗
Playground
Edit the program and press Run. It is compiled and run in your browser, in a sandboxed worker beside this page.
main.sub
(define (fib [n i32]) -> i32
(if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))
(define (main)
(printf "Hello, Substrate!\n")
(for ([i 0]) (<= i 10) (+= i 1)
(printf "fib ~a = ~a\n" i (fib i))))(define (fib [n i32]) -> i32 (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (define (main) (printf "Hello, Substrate!\n") (for ([i 0]) (<= i 10) (+= i 1) (printf "fib ~a = ~a\n" i (fib i)))) Console
Press Run, or Ctrl/⌘ + Enter, to compile and run the program.Loading compiler…Ctrl/⌘ + Enter to run
Install
Install Substrate with npm (Wasm runs on V8), or build it from source on GitHub.
# requires Node 22.22.3+ or 24.15+
$ npm install -g substrate-langFAQ
Why use Substrate over C, Rust, or Zig?
These languages are great and compile to WebAssembly, but Wasm is not their primary target. Substrate makes the most sense for domains that need a small and fast Wasm-based toolchain that runs anywhere (including the browser), or powerful compile-time metaprogramming (where macros can build entire languages). Substrate also provides dedicated tooling for deploying and testing code intended to run in the browser.
Are LLMs good at using Substrate?
Frontier models are very familiar with C semantics and write performant S-expression-style C with few errors. They also understand Racket-style hygienic macros for building languages. One dependently typed language was built with LLMs in more or less a single prompt on Opus 5.5, with further iteration only necessary to improve compile-time performance and efficiency. It helps that the entire Substrate compiler fits into the context window of a large model.
Is Substrate ready for production?
Substrate is still evolving and its APIs may change before 1.0. The compiler core is small (<12k LOC) and has been extensively tested and dogfooded, but other parts of the language and libraries are less battle-tested.
Why a language like Substrate now?
Substrate is an answer to a broader question: how will programming change in the era of LLMs? We believe WebAssembly will play an increasingly important role in infrastructure due to the security properties it provides, and one way to see Substrate is as a meta-language that enables rapid exploration of what is possible on WebAssembly. Whether the future of programming becomes more oriented towards high-level verification, or moves closer to the metal, Substrate is a platform that can empower both.