Grades and capture: values at let

Only a binding — a let, or a guard’s acquisition — implicitly turns a command’s output into a value. A suspended match waits for the right-hand side’s computation head and producer grade; successful unit readback inserts the chosen adaptation. It works through blocks, calls and names: let f = { hostname }; let host = f binds the host name.

Grades classify producers, not effects

A grade says how a computation produces its result, not whether it writes.

  • F^p A produces a value; F^w Unit is a command and produces output. Unit values are written () in programs.
  • F^w A ⇒ A = Unit. Each grade is paired with one value type; rules introduce w only at Unit, and copy the pair together.
  • A grade variable can become w only beside Unit. Before binding either grade, unification refuses an output pair against a type other than Unit or a variable whose kind admits it. Returners and handles share this check.
  • A sequence takes its tail’s type, not a combination of its effects: { echo pre; return 5 } returns an integer while writing pre.
  • There is no grade multiplication or permissiveness order. Grades are producer kinds, not an effect monoid (CBPVE).

Where grades come from

  • w: commands. Externals, base frames such as echo, and builtin results declared Command.
  • p: value demands and boundaries. A let defaults an undecided grade when its owning region closes; builtin parameters such as filter’s predicate demand a value. Checked-boundary decoders (JSON/CSV), use and other checked boundaries supply data, so their result grade is p: joining with a command must not turn that supplied data into ().
  • Fresh grades: other producers. return, primitives, ~, capture, spawn and audit; ordinary builtin results, except commands, boundaries and already graded results such as fail, raise and await; and stdout redirects, M > f : F^ν A for M : F^g A.
  • Ordinary text/byte decoders (from-bytes, from-string, from-line, from-lines) have fresh result grades too; their non-unit payload types hide those grades when printed as Returns.
  • Stdin/stderr redirects preserve their input grade. Scope forms such as within and gather preserve their body’s type. guard A K R has K’s producer type and grade; acquisition and release contribute neither. let x = guard { () } { |_| echo hi } { |_ _| () } captures hi.
  • A guard’s acquisition binds its resource as a let does, through the same suspended match minus boxing: guard { mktemp -d } { |d| … } … gives d the path, and the checked core’s acquire is always F^p R.

A free unit producer can therefore stand as a command by unification, with no runtime conversion. A let-fixed p producer cannot. The rule reads types: return () and return $u, for u : (), type alike.

The bind rule is the one coercion

The same rule governs local binds, top-level definitions and hoisted !{…}.

  • Function: run M; bind its residual block at U C when it finds no argument. Every let runs its right-hand side.
  • F^w Unit: insert capture M to d. decode d; bind a String.
  • F^p A: bind A.
  • Free grade or computation: suspend. A payload excluding Unit entails grade p; otherwise wait for the scrutinee’s region to close, then default F^ν A to F^p A, or the computation variable to F^p α, and bind its value.

The binder’s demanded type does not select capture. bound τ C reads C, not τ; a use demanding String cannot solve the grade backwards. A match on an enclosing parameter remains suspended across inner lets: everything it mentions is lowered to that parameter’s rank. Defaults at one rank are chosen together. An unresolved apply defaults to an arrow without a split; a shared unknown with both binding and application defaults is refused, not prioritised (suspended matches).

Nothing else coerces: arguments and handler arms unify with their demands; if, case, try and ? arms each meet a fresh result, then those results unify with the join. Source order selects diagnostic blame, not an adaptation. A tail Run is never captured.

Blocks run as written

  • map : ∀α β ν. {α → Command[ν] β} → [α] → Returns [β] accepts either callback grade. map { |f| echo $f } [1, 2] writes 1 and 2, returning [(), ()]; it does not collect their output.
  • To collect text, write map { |f| return !{echo $f} } [1, 2].
  • cross and par also accept command callbacks; parallel workers’ output is replayed in input order. flat-map never implicitly captures, but its callback must return a list: it maps, then concatenates the returned lists.
  • filter requires a returned Bool; sort-list-by requires a returned comparable key. both requires value-returning blocks: both { return !{curl $a} } { return !{curl $b} }.
  • each, fold, fold-lines and audit run either body grade and keep its value. spawn/watch keep the body’s grade in the handle; await restores it, so let out = await $h captures a command worker’s output.

Reading types

Command[g] A names a producer. Constants and impossible command grades have short spellings.

  • w prints Command; p prints Returns A.
  • A free grade prints Returns A when its paired type cannot be Unit: a concrete non-unit type, or a quantified variable whose kind excludes unit.
  • Otherwise it prints Command[ν] A, using ν ξ ο π. A scheme quantifies only the grades it prints.
  • cd : ∀ν. String → Command[ν] Unit; length : ∀α. [α] → Returns Integer; echo : [String] → Command.
  • { |x| return $x } : ∀α ν. {α → Command[ν] α}.
  • await : ∀α ν. Handle (Command[ν] α) → Command[ν] α.

Programs

programresult
let h = hostname; let f = { hostname }; let h = fcaptures
let x = if $c { echo a } else { warn b }captures
try { mkdir d } { |e| warn no }one Command
within [handlers: [:, curl: { |a| return () }]] { let x = curl foo }captures ""
try { curl $u > out } { |e| echo failed }one Command
try { each-line { |l| warn $l } } { |e| echo failed }one Command
let u = (); if $c { return $u } else { echo a }one Command
each { |b| !$b } [{ echo hi }, { return () }]writes hi
map { |f| echo $f } [1, 2]writes 1 and 2; value [(), ()], which the REPL then prints
let x = map { |f| echo $f } [1, 2]; likewise map $g for a command block $gwrites 1 and 2; binds [(), ()]
cross [1, 3] [2] { |x y| echo "$x$y" }writes 12 and 32; value [(), ()]
par { |u| curl $u } $us 4output replayed in order; value [(), …]
let x = try { echo a } { |e| return 'none' }refused, join hint
let x = if $c { hostname } else { return 'b' }refused, join hint
both { echo a } { echo b }refused, argument hint
let x = if $c { echo a } else { from-json < f }refused, join hint
sort-list-by { |f| echo $f } $xs; group-by, median-by with a command keyrefused, argument hint
let g = { |f| let y = !$f; return $y }; g { echo hi }refused, argument hint
let twice = { |f| let _ = !$f; !$f }; if $c { twice { return () } } else { echo a }refused, join hint
let g = { |f| let y = !$f; if true { !$f } else { echo a } }y captures; transposing the statements selects the same adaptation
let g = { |f| f 1; if true { !$f } else { return { |n| echo $n } } }one returned-thunk split; likewise with the conditional bound before the call
let g = { |f| let a = !$f; let b = f 1; return $b }refused, conflicting defaults in either order

For a command where a value is required, return an explicit capture ({ return !{…} }) or bind it first. Use succeeds { … } for a yes/no answer, or decode output to the required type. In the twice example, end its arm with return () to supply a fresh unit producer.

The whole computation is the buffer

Capture preserves exactly the bytes written by its body. An inner let and a command guard acquisition capture for themselves; every other write, including a value-producing acquisition or any release, goes to the enclosing sink. let x = !{ echo a; echo b } binds "a\nb", while let x = !{ let y = echo a; echo b } binds "b" and y is "a".

  • CompKind::Capture returns Bytes; Prim::Decode drops one trailing LF or CRLF and strictly decodes UTF-8. Invalid text fails; use from-bytes to keep binary data. Neither term has surface syntax.
  • The solver records a binding’s Mode::{Run, Capture} from the RHS head; readback applies it at the binding hole only after unit closure.
  • A stand-in runs whole under its caller’s capture. A fresh unit-returning stub for an external is a command and captures "".
  • A capture reaching the 16 MiB buffer cap fails rather than bind a prefix. On a halt, Frame::Capture restores its previous sink and drops the buffer: a capture that does not become its value commits nothing, so let x = try { return !{ cmd } } { |_| return 'x' } writes nothing to stdout. On error, each capture adds its actual byte count to Unwound.discarded, summed saturatingly across nesting. One renderer shows one counted note, after the hints and advice (docs/SPEC.md §13.1) and before any eligible abandoned-tail note. The count is report-only and does not enter [frame, message, status, site] or survive raise of that record; escapes carry no unwind facts. audit keeps a command’s output (the error record).

See types, codecs, output capture, typecheck. Cite: docs/SPEC.md §§7.2, 17.3–17.7.