flan/conditions-play.flan
Joseph Ferano 96ab4c9cf0 Retire the per-type printers, since print says all of it
print-str, print-i64, print-f64, print-bytes, print-line and newline leave
the prelude. print and println are the whole printing surface now, and print
is the better call at every one of the sites that used them: it is the same
structural walk without the newline, so the no-newline case the family was
kept for is covered, and it takes the value as it is. The old print-i64
forced an explicit (i64 x) at every call site, because this language widens
nothing implicitly; that cast is gone from 127 places.

Dropping it moves one answer. hash-grid returns u64, and the cast through
the signed printer showed sand-headless's hash as -2851001042534928384.
print routes a u64 through flan_u64_to_bytes, so it now prints
15595743031174623232 — the same 64 bits, read as the unsigned number they
are. The pinned expectation follows the correction.

test-flan-dev.el and test_session.ml both reached for print-line as "a name
the prelude has"; they reach for rand-seed instead.
2026-09-12 05:32:25 +07:00

52 lines
1.5 KiB
Plaintext

;;;; A program to poke at conditions and restarts with — see conditions.org.
;;;;
;;;; It loops instead of exiting, so `flan dev conditions-play.flan` can attach
;;;; and you can redefine `probe` and `run-once` from Emacs while it runs. The
;;;; socket written here is only a fallback; the daemon overrides it.
(import agent "vendor:agent")
(defstruct AssetMissing [id i32])
(defstruct Corrupt [id i32])
;;; Handlers cannot see the establishing function's locals yet, so anything a
;;; handler accumulates into has to be a global.
(defvar seen i64)
(defvar ticks i64)
;;; Two frames under the restart-case, so a transfer has something to cross.
(defn probe [n i32] i32
(signal (AssetMissing {:id n}))
100)
(defn middle [n i32] i32
(defer (set ticks (+ ticks 1))) ; runs on the way out, transfer or not
(+ (probe n) 1))
;;; The shape spec-conditions.md §1 uses: a restart-case in value position,
;;; whose fall-through has to produce the type too.
(defn fetch [n i32] i32
(restart-case (middle n)
(use-placeholder [] -1)
(retry [] 7)))
(defn run-once []
(print (fetch 1)) (println "")
(handler-bind [(AssetMissing [c] (set seen (+ seen (i64 (.id c)))))]
(print (fetch 2)) (println ""))
(handler-bind [(AssetMissing [c] (invoke-restart 'use-placeholder))]
(print (fetch 3)) (println ""))
(print seen) (println "")
(print ticks) (println ""))
(defn main [] i32
(agent/start "/tmp/flan-conditions.sock")
(run-once)
(while (= (agent/wait 200) 0) 0)
(run-once)
(while (= (agent/wait 200) 0) 0)
(run-once)
0)