Experiment: Closure Sharing and Isolation(v1 · en)

"Sharing" and "isolation" are not two mechanisms but two observations of one capture rule: closures born of one factory call share a binding; closures born of different calls are isolated. This experiment runs both paths side by side on the same code; observed values come from real zioge CLI runs (see the concept bodies Closures and Shared State).

Seed code

(defun make-counter []
  (let [n 0]
    (fn [] (set! n (+ n 1)) n)))
(defun make-pair []
  (let [n 0]
    [(fn [] (set! n (+ n 1)) n)
     (fn [] n)]))

Steps

Step 1: Closures from one factory call share one binding cell

Take the incrementer and the observer out of a single (make-pair) call and alternate writes and reads:

(def pair (make-pair))
(def tick (get pair 0))
(def peek (get pair 1))
(println (tick))
(println (tick))
(println (peek))

Expected observation: stdout is 1, 2, 2 in turn. tick and peek captured the same n established by that one call: after two increments the observer sees 2 from the same cell — a write is immediately visible to the other closure of the same factory call.

Step 2: Different factory calls are isolated

Mint a second account and observe both ledgers interleaved:

(def other (make-pair))
(def otick (get other 0))
(def opeek (get other 1))
(println (opeek))
(println (otick))
(println (opeek))
(println (peek))

Expected observation: stdout is 0, 1, 1, 2 in turn. other's ledger starts at 0 (its own n) and its increment only reaches 1; the original account's peek still reports 2. Isolation needs no explicit action — the call that performed the capture owns the state.

Step 3: Cross-calling counters (independent increments)

Cross-check two closures' private memories with make-counter:

(def c (make-counter))
(def d (make-counter))
(println (c))
(println (c))
(println (d))
(println (c))

Expected observation: stdout is 1, 2, 1, 3 in turn. c increments continuously (1→2→3) while d, interleaved between its calls, still starts at 1 — the two counters hold different binding cells and do not perceive each other. Sharing requires the same capture; for isolation, call the factory again.

步骤总览

步骤预期观察
Step 1: Closures from one factory call share one binding cellstdout is =1=, =2=, =2= in turn. =tick= and =peek= captured the same =n= established by that one call: after two increments the observer sees 2 from the same cell — a write is immediately visible to the other closure of the same factory call.
Step 2: Different factory calls are isolatedstdout is =0=, =1=, =1=, =2= in turn. =other='s ledger starts at 0 (its own =n=) and its increment only reaches 1; the original account's =peek= still reports 2. Isolation needs no explicit action — the call that performed the capture owns the state.
Step 3: Cross-calling counters (independent increments)stdout is =1=, =2=, =1=, =3= in turn. =c= increments continuously (1→2→3) while =d=, interleaved between its calls, still starts at 1 — the two counters hold different binding cells and do not perceive each other. Sharing requires the same capture; for isolation, call the factory again.

无 JS 执行会话

下面的原生表单创建一个执行会话(服务端 P6-b runner 子进程求值,本页进程不执行用户代码);会话页内逐步运行、查看观察、重置或关闭。

JSON API 同一能力走 POST /api/experiments;两条路径共用同一 runner 合同。