-
Notifications
You must be signed in to change notification settings - Fork 0
18. Program Style Guide
A recommended set of conventions, patterns, idioms, and best practices for writing clear, maintainable, and correct UNS programs.
This guide is designed for:
- authors of examples,
- contributors adding new documentation,
- researchers exploring UNS numerics, and
- anyone who wants their
.unsecode to be readable and consistent.
These are strong recommendations, not hard rules—but they will make your work significantly easier.
UNS is an expression language—nesting can become unreadable quickly.
Favor stepwise construction using multiple let bindings.
Use window, region, wave, signal, maskA, localPsi rather than x1, tmp, etc.
state is for measurement, not computation.
let is for building UValues.
Do not mix the two roles.
Example:
// 1. Construct base signals
let idx = lift1(index, const(0))
let wave = lift1(sin, idx)
// 2. Build masks or windows
let taper = lift1(windowWeight_3_4, idx)
// 3. Convert to a state
state psi = state(taper)
// 4. Measurements (final)
let value = read(wave | psi)
plotU(wave)
let wave_sin = ...
let region_mask = ...
let noise_field = ...
let weight_soft = ...
state psi = ...
state psi_local = ...
state psiBand = ...
lift1(SIN, idx) → ❌ incorrect
lift1(sin, idx) → ✔ lowercase functions
let modulated_signal = base *u taper
Bad:
// multiply wave by window
let w = wave *u window
Better:
// Apply soft taper to prevent edge artifacts
let w = wave *u window
base +u mod *s 0.5 // ❌ messy
(base +u mod) *s 0.5 // ✔ clearer
This helps visually scan program structure.
let idx = lift1(index, const(0))
let wave = lift1(sin, idx)
let mask = mask_range(0,128)
state psi = state_from_mask(mask)
let result = read(wave | psi)
Avoid:
let w = mask_range(0,128)
Prefer to space parameters and include descriptive comments:
// Window covering the first quarter of the domain
let w = mask_range(0, 128)
Bad:
read(lift1(sin, lift1(index, const(0))) *u someMask | psi)
Good:
let idx = lift1(index, const(0))
let wave = lift1(sin, idx)
let masked = wave *u someMask
read(masked | psi)
Bad:
state psi = mask_range(0,150) // ❌ normalizes incorrectly
Good:
let m = mask_range(0, 150)
state psi = state_from_mask(m)
let mask = mask_range(100,150)
let weight = lift1(windowWeight_96_48, idx)
state psi = state(mask *u weight)
state psi = ...
state psi_local = ...
state psi_band_soft = ...
Avoid single giant expressions. Prefer:
let idx = lift1(index, const(0))
let base = lift1(sin, idx)
let mod = lift1(cos, idx)
let wave = base +u (mod *s 0.5)
Build the signal, then shape it.
Bad:
let r = read(wave *u window | psi)
Better:
let windowed = wave *u window
let r = read(windowed | psi)
let local_mean = read(signal | psi_local)
Bad:
let r = read(signal | state_range(0, 100))
Good:
state psi = state_range(0, 100)
let r = read(signal | psi)
They are essential for debugging.
Always separate diagnostics at the bottom:
plotU(signal)
plotU(psi)
printU(signal)
Bad:
let ratio = lift2(divide, a, b)
Good:
let ratio = lift2(divide, a, b +u const(0.01))
This prevents undefined values from contaminating readout.
let m_range = mask_range(0,128)
let m_peak = mask_threshold(signal, 0.5)
let m = m_range *u m_peak
plotU(m)
Hard regions create digital artifacts.
let taper = lift1(windowWeight_3_4, idx)
let windowed = signal *u taper
let mask = mask_range(100,150)
let tapered = mask *u taper
Example:
let structured = assemble {
10: 1,
11: 0.5,
12: -0.25
}
// ==== BUILD INDEX ====
let idx = lift1(index, const(0))
// ==== BUILD SIGNAL ====
let base = lift1(sin, idx)
let mod = lift1(cos, idx)
let wave = base +u (mod *s 0.5)
// ==== BUILD WINDOW ====
let taper = lift1(windowWeight_3_4, idx)
let band = mask_range(0, 128)
let window = taper *u band
// ==== CONVERT TO STATE ====
state psi = state(window)
// ==== READOUT ====
let value = read(wave | psi)
// ==== DIAGNOSTICS ====
plotU(wave)
plotU(psi)
- Use many small
letbindings - Keep
statebindings separate and minimal - Avoid inlining huge expressions
- Use consistent naming patterns
- Plot and print intermediate signals
- Guard divide operations
- Prefer smooth windows over hard cutoffs
- Use descriptive comments explaining intent