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 Aproduces a value;F^w Unitis 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 introducewonly atUnit, and copy the pair together.- A grade variable can become
wonly besideUnit. Before binding either grade, unification refuses an output pair against a type other thanUnitor 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 writingpre. - 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 asecho, and builtin results declaredCommand.p: value demands and boundaries. Aletdefaults an undecided grade when its owning region closes; builtin parameters such asfilter’s predicate demand a value. Checked-boundary decoders (JSON/CSV),useand other checked boundaries supply data, so their result grade isp: joining with a command must not turn that supplied data into().- Fresh grades: other producers.
return, primitives,~,capture,spawnandaudit; ordinary builtin results, except commands, boundaries and already graded results such asfail,raiseandawait; and stdout redirects,M > f : F^ν AforM : 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 asReturns. - Stdin/stderr redirects preserve their input grade. Scope forms such as
withinandgatherpreserve their body’s type.guard A K RhasK’s producer type and grade; acquisition and release contribute neither.let x = guard { () } { |_| echo hi } { |_ _| () }captureshi. - A
guard’s acquisition binds its resource as aletdoes, through the same suspended match minus boxing:guard { mktemp -d } { |d| … } …givesdthe path, and the checked core’s acquire is alwaysF^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 atU Cwhen it finds no argument. Everyletruns its right-hand side. F^w Unit: insertcapture M to d. decode d; bind aString.F^p A: bindA.- Free grade or computation: suspend. A payload excluding
Unitentails gradep; otherwise wait for the scrutinee’s region to close, then defaultF^ν AtoF^p A, or the computation variable toF^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]writes1and2, returning[(), ()]; it does not collect their output.- To collect text, write
map { |f| return !{echo $f} } [1, 2]. crossandparalso accept command callbacks; parallel workers’ output is replayed in input order.flat-mapnever implicitly captures, but its callback must return a list: it maps, then concatenates the returned lists.filterrequires a returnedBool;sort-list-byrequires a returned comparable key.bothrequires value-returning blocks:both { return !{curl $a} } { return !{curl $b} }.each,fold,fold-linesandauditrun either body grade and keep its value.spawn/watchkeep the body’s grade in the handle;awaitrestores it, solet out = await $hcaptures a command worker’s output.
Reading types
Command[g] A names a producer. Constants and impossible command grades have
short spellings.
wprintsCommand;pprintsReturns A.- A free grade prints
Returns Awhen its paired type cannot beUnit: 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
| program | result |
|---|---|
let h = hostname; let f = { hostname }; let h = f | captures |
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 $g | writes 1 and 2; binds [(), ()] |
cross [1, 3] [2] { |x y| echo "$x$y" } | writes 12 and 32; value [(), ()] |
par { |u| curl $u } $us 4 | output 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 key | refused, 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::CapturereturnsBytes;Prim::Decodedrops one trailing LF or CRLF and strictly decodes UTF-8. Invalid text fails; usefrom-bytesto 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::Capturerestores its previous sink and drops the buffer: a capture that does not become its value commits nothing, solet x = try { return !{ cmd } } { |_| return 'x' }writes nothing to stdout. On error, each capture adds its actual byte count toUnwound.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 surviveraiseof that record; escapes carry no unwind facts.auditkeeps a command’s output (the error record).
See types, codecs,
output capture,
typecheck. Cite: docs/SPEC.md §§7.2, 17.3–17.7.