<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Nex</title>
  <link href="https://nexlang.org/feed.xml" rel="self"/>
  <link href="https://nexlang.org/"/>
  <id>https://nexlang.org/feed.xml</id>
  <updated>2026-05-22T19:26:20.099529896Z</updated>
  <author><name>Ed Maxedon</name></author>
  <entry>
    <title>Verification Model</title>
    <link href="https://nexlang.org/tooling/verification/"/>
    <id>https://nexlang.org/tooling/verification/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>How the interpreter and the AOT compiler are kept in lock-step — two paths, one expected output.</summary>
    <content type="html">&lt;p&gt;The Nex implementation runs two execution paths in parallel and treats their disagreement as a bug:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Tree-walking interpreter.&lt;/strong&gt; The reference semantics. Used by &lt;code&gt;nex run&lt;/code&gt; and &lt;code&gt;nex test&lt;/code&gt;. Easy to read, easy to step through, easy to extend when adding a new language feature.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AOT compiler.&lt;/strong&gt; LLVM IR text → &lt;code&gt;clang -O1&lt;/code&gt; → native binary. Used by &lt;code&gt;nex compile&lt;/code&gt;. The path users ship.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;three-layers-of-testing&quot;&gt;Three layers of testing&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Unit tests on every subsystem.&lt;/strong&gt; Lexer, parser, three elaborator stages, interpreter, codegen, prelude. Over two thousand tests on JVM today; every commit runs the full suite on all three CLI targets (JVM, Scala.js, Scala Native). Backend-shelling tests (those that drive &lt;code&gt;clang&lt;/code&gt; or the MLIR toolchain) run on JVM only; the cross-platform subset covers the parser, elaborator, and interpreter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IR pattern checks.&lt;/strong&gt; Codegen tests assert against expected LLVM IR fragments — both that the right instructions are emitted and that the fusion pass actually produced a single loop where it claimed to. This catches regressions where the IR drifts shape but still happens to execute correctly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;End-to-end interpreter/AOT parity.&lt;/strong&gt; A shared corpus of programs (see &lt;code&gt;NexProgramCorpus&lt;/code&gt;) is run twice — once through the interpreter, once through the AOT binary — and the two stdouts are compared byte-for-byte. A mutation-based fuzz harness (&lt;code&gt;NexMutations&lt;/code&gt;) applies behaviour-preserving rewrites to each corpus program and reruns the comparison so silent miscompiles surface as parity failures. The AOT path emits reals via an iterative shortest-round-trip helper (&lt;code&gt;%.&amp;lt;p&amp;gt;g&lt;/code&gt; + &lt;code&gt;strtod&lt;/code&gt; for p = 1..17) plus a Java-&lt;code&gt;Double.toString&lt;/code&gt;-style post-pass, so non-whole reals like &lt;code&gt;0.1 + 0.2&lt;/code&gt; print as &lt;code&gt;0.30000000000000004&lt;/code&gt; on both sides instead of &lt;code&gt;%g&lt;/code&gt;‘s 6-significant-figure approximation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;why-this-matters&quot;&gt;Why this matters&lt;/h2&gt;
&lt;p&gt;Numerical code is unforgiving — silent codegen bugs corrupt computation in ways that look exactly like correct output until they don’t. Running a reference interpreter alongside a compiler and demanding byte-exact agreement turns a class of “did the optimizer break it?” bugs into immediate test failures.&lt;/p&gt;
&lt;p&gt;The model came from sysl, where it has been load-bearing across seven backends. Nex inherits the same discipline.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Type System</title>
    <link href="https://nexlang.org/spec/types/"/>
    <id>https://nexlang.org/spec/types/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Primitive types, numeric promotion, arrays, tuples, structs, sum types, function types.</summary>
    <content type="html">&lt;h2 id=&quot;3-1-primitive-types&quot;&gt;3.1 Primitive types&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Size&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Boolean (&lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;1 byte&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;integer&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Signed integer&lt;/td&gt;&lt;td&gt;64 bits&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;IEEE 754 floating-point&lt;/td&gt;&lt;td&gt;64 bits&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;complex&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Pair of &lt;code&gt;real&lt;/code&gt; (re, im)&lt;/td&gt;&lt;td&gt;128 bits&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;unit&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Single value &lt;code&gt;()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;0 bits&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;string&lt;/code&gt;&lt;/td&gt;&lt;td&gt;UTF-8 byte string&lt;/td&gt;&lt;td&gt;reference-typed&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Sized variants (&lt;code&gt;integer32&lt;/code&gt;, &lt;code&gt;real32&lt;/code&gt;, &lt;code&gt;complex32&lt;/code&gt;, etc.) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;3-2-numeric-promotion&quot;&gt;3.2 Numeric promotion&lt;/h2&gt;
&lt;p&gt;The numeric types form a promotion lattice:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;integer  →  real  →  complex
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When an operator is applied to operands of different numeric types, the operand of the lower type is implicitly promoted. The result has the higher type.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// real, promotes 1 → 1.0, result 3.0&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;i     &lt;span class=&quot;hl-comment&quot;&gt;// complex, promotes 1.0 → 1.0+0.0i, result 1.0+2.0i&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;i       &lt;span class=&quot;hl-comment&quot;&gt;// complex, promotes 1 → 1.0 → 1.0+0.0i&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Promotion is implicit in expression context. Conversion in the &lt;em&gt;narrowing&lt;/em&gt; direction is never implicit — see chapter 4.&lt;/p&gt;
&lt;h2 id=&quot;3-3-array-types&quot;&gt;3.3 Array types&lt;/h2&gt;
&lt;p&gt;The array type &lt;code&gt;[T]&lt;/code&gt; denotes a &lt;strong&gt;rank-1&lt;/strong&gt; array (vector) of elements of type &lt;code&gt;T&lt;/code&gt;, with runtime-determined length. The array type &lt;code&gt;[[T]]&lt;/code&gt; denotes a &lt;strong&gt;rank-2&lt;/strong&gt; array (matrix), with runtime-determined shape (rows × columns). &lt;code&gt;T&lt;/code&gt; must be one of &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, or &lt;code&gt;complex&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// rank-1: vector of real&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// rank-1: vector of integer&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// rank-2: matrix of real&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;  &lt;span class=&quot;hl-comment&quot;&gt;// rank-2: matrix of complex&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rank-2 arrays are &lt;strong&gt;rectangular&lt;/strong&gt; — every row has the same number of columns. Constructing a jagged array (rows of differing lengths) traps at runtime. Storage is &lt;strong&gt;column-major&lt;/strong&gt;, matching Fortran and the standard LAPACK/BLAS convention so that future FFI to those libraries is layout-compatible without transposing.&lt;/p&gt;
&lt;p&gt;Higher-rank arrays (&lt;code&gt;[[[T]]]&lt;/code&gt; and beyond) and statically-shaped arrays (&lt;code&gt;[T; N]&lt;/code&gt;, &lt;code&gt;[T; M, N]&lt;/code&gt;) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;3-3-1-the-byte-buffer-type&quot;&gt;3.3.1 The &lt;code&gt;[byte]&lt;/code&gt; buffer type&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;[byte]&lt;/code&gt; is a packed buffer of 8-bit values used for file I/O and image processing. It is a separate type from &lt;code&gt;[T]&lt;/code&gt;, not &lt;code&gt;[byte] = [integer]&lt;/code&gt; with a narrower range. There is no scalar &lt;code&gt;byte&lt;/code&gt; type — bytes only exist as elements of a buffer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// zero-filled buffer of length 64&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// first: integer (0..255)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;128&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// traps if RHS is outside 0..255&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Indexed read widens to &lt;code&gt;integer&lt;/code&gt; in the range &lt;code&gt;0..255&lt;/code&gt;; indexed write requires the right-hand side to be an &lt;code&gt;integer&lt;/code&gt; in the same range, otherwise it traps. The widening is unconditional — there is no implicit “is this within 0..255?” check on read, because by construction every stored byte already is.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[byte]&lt;/code&gt; is a &lt;strong&gt;storage&lt;/strong&gt; type, not a computation type:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No element-wise arithmetic (&lt;code&gt;b1 + b2&lt;/code&gt; is a type error).&lt;/li&gt;
&lt;li&gt;No broadcasting (&lt;code&gt;b + 1&lt;/code&gt; is a type error).&lt;/li&gt;
&lt;li&gt;No views (&lt;code&gt;b.view(lo..hi)&lt;/code&gt; is not supported; slicing copies).&lt;/li&gt;
&lt;li&gt;No participation in fusion.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To do arithmetic on byte data, widen explicitly with &lt;code&gt;to_integers(b)&lt;/code&gt;, compute on the resulting &lt;code&gt;[integer]&lt;/code&gt;, and narrow back with &lt;code&gt;to_bytes(a)&lt;/code&gt; (which traps if any element is outside &lt;code&gt;0..255&lt;/code&gt;). This widen-compute-narrow pattern is the recommended shape for sRGB / HDR / linear-light pixel math, where the computation lives in &lt;code&gt;real&lt;/code&gt; or &lt;code&gt;integer&lt;/code&gt; regardless.&lt;/p&gt;
&lt;p&gt;Slicing &lt;code&gt;b[lo..hi]&lt;/code&gt; (and the inclusive / open / strided variants from §4.14) returns a fresh &lt;code&gt;[byte]&lt;/code&gt; copy. &lt;code&gt;length(b)&lt;/code&gt; returns the buffer’s element count. Structural equality (&lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;) compares lengths first and then bytes.&lt;/p&gt;
&lt;h2 id=&quot;3-4-tuple-types&quot;&gt;3.4 Tuple types&lt;/h2&gt;
&lt;p&gt;A tuple type is a comma-separated list of types denoting a fixed-size heterogeneous product (n ≥ 2). Tuples are primarily used for multi-value return.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parentheses around a tuple type are optional grouping; they are required only when the surrounding context would otherwise parse the commas as something else (parameter declarations, type arguments, array element types). Both forms are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// paren-less return type&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// explicit grouping&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In a parameter declaration the parens are mandatory because the outer parens of the parameter list use commas to separate parameters:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// two tuple parameters&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// ERROR: 4 parameters&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;3-5-struct-types&quot;&gt;3.5 Struct types&lt;/h2&gt;
&lt;p&gt;A struct type is declared with &lt;code&gt;struct&lt;/code&gt;. Fields are declared one per line, with types:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;LineSegment&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;finish&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Struct types are pure data — no methods, no inheritance, no constructors beyond the implicit field-list constructor. Struct values are constructed by naming the type followed by parenthesized arguments in declaration order:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;seg&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;LineSegment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Field access uses &lt;code&gt;.&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;end_x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;seg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;finish&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;3-6-generic-structs&quot;&gt;3.6 Generic structs&lt;/h2&gt;
&lt;p&gt;A struct declaration may introduce one or more &lt;strong&gt;type parameters&lt;/strong&gt; in square brackets after its name. Each type parameter stands for a type fixed at the use site; the field declarations may name the parameters, and the compiler generates a specialized clone of the struct for every distinct argument combination.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;fst&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;snd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;B&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bracket list follows the same shape as the one on a generic function (see &lt;a href=&quot;/spec/06-functions/#610-generic-functions&quot;&gt;§6.10&lt;/a&gt;): each entry is a bare &lt;code&gt;[T]&lt;/code&gt; (admits any type) or a bounded &lt;code&gt;[T: Kind]&lt;/code&gt; drawn from the same closed constraint set — &lt;code&gt;Any&lt;/code&gt;, &lt;code&gt;Numeric&lt;/code&gt;, &lt;code&gt;Real&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Complex&lt;/code&gt;, &lt;code&gt;Ord&lt;/code&gt;, &lt;code&gt;Eq&lt;/code&gt;. A use site whose argument type isn’t admitted by the constraint is rejected at elaboration time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Call-site inference.&lt;/strong&gt; A generic struct is constructed exactly like a monomorphic one — the type arguments are not written. The compiler infers them by unifying each field’s declared type against the actual argument:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// A := integer, B := string&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// A := real,    B := real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// T := integer&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Explicit type arguments.&lt;/strong&gt; A binding’s declared type may carry explicit arguments, which is useful when call-site inference cannot pin every parameter on its own — for instance, to pin a payload’s type ahead of the constructor or to disambiguate at a join point:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Specialization.&lt;/strong&gt; Like generic functions, generic struct templates have no executable form on their own. Each unique combination of type arguments produces a specialized struct (e.g. &lt;code&gt;Pair$integer$string&lt;/code&gt;, &lt;code&gt;Pair$real$real&lt;/code&gt;) with the concrete fields already substituted. Two specializations of the same template with different arguments are independent types — a value of one cannot stand in for the other.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Combining with generic functions.&lt;/strong&gt; A type parameter on a function can flow through to a generic struct constructor; the function’s specialization carries the right struct specialization with it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// Pair$integer$integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// Pair$real$real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same constraint vocabulary applies — &lt;code&gt;struct Sorted[T: Ord]&lt;/code&gt; is well-formed and lets the field operations rely on &lt;code&gt;&amp;lt;&lt;/code&gt; / &lt;code&gt;&amp;lt;=&lt;/code&gt; / &lt;code&gt;&amp;gt;&lt;/code&gt; / &lt;code&gt;&amp;gt;=&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;3-7-sum-types&quot;&gt;3.7 Sum types&lt;/h2&gt;
&lt;p&gt;A sum type is declared with &lt;code&gt;enum&lt;/code&gt;. Each line of the body declares a &lt;strong&gt;variant&lt;/strong&gt; — either a bare name (a single value of the enum type) or a constructor that takes named, typed fields:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Red&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Green&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Blue&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A variant’s field types are restricted to scalar types (&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;) in the current implementation. Aggregate fields (arrays, tuples, structs, complex, nested enums) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Variant references are values directly when the variant is bare (&lt;code&gt;Red&lt;/code&gt;, &lt;code&gt;Diverged&lt;/code&gt;), or callable constructors when the variant has fields (&lt;code&gt;Converged(3.14)&lt;/code&gt;, &lt;code&gt;MaxIters(100, 0.5)&lt;/code&gt;). Both forms yield a value whose static type is the enclosing enum:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Color&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Green&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sum-type values are inspected with &lt;code&gt;match&lt;/code&gt; (see &lt;a href=&quot;/spec/07-control-flow/#75-match-expressions&quot;&gt;§7.5&lt;/a&gt;). Printing a sum-type value renders bare variants as their name (&lt;code&gt;Red&lt;/code&gt;) and fielded variants as &lt;code&gt;Name(f0, f1, ...)&lt;/code&gt; (&lt;code&gt;Converged(3.14)&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&quot;3-8-generic-enums&quot;&gt;3.8 Generic enums&lt;/h2&gt;
&lt;p&gt;An enum declaration may introduce &lt;strong&gt;type parameters&lt;/strong&gt; in square brackets, exactly like a generic struct. The parameters may appear in variant payload field types; bare variants carry no payload but still participate in the enum’s specialization.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Opt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bracket list uses the same constraint vocabulary as &lt;a href=&quot;/#36-generic-structs&quot;&gt;§3.6&lt;/a&gt; and &lt;a href=&quot;/spec/06-functions/#610-generic-functions&quot;&gt;§6.10&lt;/a&gt;: a bare &lt;code&gt;[T]&lt;/code&gt; admits any type; a bound &lt;code&gt;[T: Kind]&lt;/code&gt; restricts the parameter to the closed kind set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Call-site inference.&lt;/strong&gt; A variant constructor (&lt;code&gt;Some(x)&lt;/code&gt;, &lt;code&gt;Ok(v)&lt;/code&gt;) infers its enum’s type arguments from its argument types:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// Opt$integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// E unpinned — see below&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Bare-variant inference.&lt;/strong&gt; A bare variant (&lt;code&gt;None&lt;/code&gt;, no payload) carries no information to infer its enclosing enum’s type parameters from. Bare-variant references rely on push-down from a declared type on the surrounding binding:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Opt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// T := integer via the annotation&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A bare variant in a context with no declared type to push down (e.g. an &lt;code&gt;if … then Some(1) else None&lt;/code&gt; with no outer annotation) is rejected the same way an unconstrained generic function call would be.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Partial-inference annotations.&lt;/strong&gt; When only some of a multi-parameter enum’s type parameters can be pinned from a single use, the binding must carry an explicit type. Constructing &lt;code&gt;Ok(7)&lt;/code&gt; on a &lt;code&gt;Result[T, E]&lt;/code&gt; pins &lt;code&gt;T&lt;/code&gt; from the argument but leaves &lt;code&gt;E&lt;/code&gt; open:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Without the annotation the compiler rejects the construction with a “cannot infer type for type parameter &lt;code&gt;E&lt;/code&gt;“ diagnostic — the same shape Rust reports for an analogous case.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Match patterns.&lt;/strong&gt; Patterns on a specialized enum reference the specialization’s variants. The scrutinee’s static type determines which specialization is matched; nested payload patterns bind values of the substituted field type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;got &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;nothing&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Specialization.&lt;/strong&gt; Like generic structs, each unique combination of type arguments produces a specialized enum (&lt;code&gt;Opt$integer&lt;/code&gt;, &lt;code&gt;Opt$string&lt;/code&gt;, &lt;code&gt;Result$integer$string&lt;/code&gt;, etc.) with its own per-variant constructors. Two specializations are independent types; a value of one cannot stand in for the other.&lt;/p&gt;
&lt;h2 id=&quot;3-9-function-types&quot;&gt;3.9 Function types&lt;/h2&gt;
&lt;p&gt;A function type &lt;code&gt;(T1, T2, ..., Tn) -&amp;gt; R&lt;/code&gt; denotes a function taking arguments of the given types and returning &lt;code&gt;R&lt;/code&gt;. A single-argument function may be written without parentheses:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A function type may include parameter modes (see chapter 6):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;unit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;3-10-type-expressions&quot;&gt;3.10 Type expressions&lt;/h2&gt;
&lt;p&gt;Type expressions are used in declarations (function signatures, struct field types, explicit annotations on bindings). They consist of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Primitive type names (&lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;complex&lt;/code&gt;, &lt;code&gt;unit&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Array type constructors (&lt;code&gt;[T]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Tuple type constructors (&lt;code&gt;(T1, ..., Tn)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Function type constructors (&lt;code&gt;(T1, ..., Tn) -&amp;gt; R&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;User-defined type names (referring to declared structs or enums)&lt;/li&gt;
&lt;li&gt;Applied generic type names (&lt;code&gt;Pair[integer, string]&lt;/code&gt;, &lt;code&gt;Opt[integer]&lt;/code&gt;) — see &lt;a href=&quot;/#36-generic-structs&quot;&gt;§3.6&lt;/a&gt; and &lt;a href=&quot;/#38-generic-enums&quot;&gt;§3.8&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>Tour</title>
    <link href="https://nexlang.org/examples/tour/"/>
    <id>https://nexlang.org/examples/tour/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Every language feature in small, focused snippets — literals, bindings, blocks, arithmetic, arrays, structs, lambdas, functions, modes, closures, control flow, prelude, testing, modules.</summary>
    <content type="html">&lt;h2 id=&quot;comments&quot;&gt;Comments&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Line comment.&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;/* Block comment. */&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;/* Block comments &lt;/span&gt;&lt;span class=&quot;hl-comment&quot;&gt;/* can nest */&lt;/span&gt;&lt;span class=&quot;hl-comment&quot;&gt; — convenient for commenting out code. */&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;layout-separators-and-line-continuation&quot;&gt;Layout: separators and line continuation&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// multiple statements on one line, separated by ;&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;long_expression&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// a line ending with an operator or open&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;some_function&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// delimiter continues on the next (more-&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;another&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// indented) line; no backslash needed&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;yet_another&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;;&lt;/code&gt; is rarely used; line continuation is common for expression-heavy numerical code.&lt;/p&gt;
&lt;h2 id=&quot;literals&quot;&gt;Literals&lt;/h2&gt;
&lt;h3 id=&quot;booleans-integers-reals&quot;&gt;Booleans, integers, reals&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;flag&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1_000_000&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hex&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0xFF&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 255&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;bin&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0b1010_1010&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// 170&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;oct&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0o755&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// 493&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi_ish&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14159&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sci&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.022e23&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sep&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1_234.567_89&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;strings-and-escapes&quot;&gt;Strings and escapes&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hello&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello, world&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;multi&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;line one&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;line two&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tabbed&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;col1&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;col2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;quoted&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;she said &lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;unicode&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\u{1F600}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 😀&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\xC3\xA9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// bytes for &amp;quot;é&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;interpolated-strings-s&quot;&gt;Interpolated strings (&lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt;)&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Ada&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;i

&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// hello, Ada&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;n = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, n+1 = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// n = 42, n+1 = 43&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|z| = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// |z| = 5.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;first element = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;literal dollar: &lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// literal dollar: $&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// $ident.field treats only $ident as the splice — .field is literal text.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// Use ${ident.field} to interpolate a field access:&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;.re&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                         &lt;span class=&quot;hl-comment&quot;&gt;// (3.0 + 4.0i).re&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// 3.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;complex-via-the-i-constant&quot;&gt;Complex via the &lt;code&gt;i&lt;/code&gt; constant&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// (0.0 + 1.0i)  — imaginary unit&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i                  &lt;span class=&quot;hl-comment&quot;&gt;// (0.0 + 2.0i)  — juxtaposition: 2 * i&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;i              &lt;span class=&quot;hl-comment&quot;&gt;// (3.0 + 4.0i)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// ~ (-1.0 + 0.0i)  (Euler&apos;s identity)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;i&lt;/code&gt; is the prelude name; &lt;code&gt;for i in 0..n&lt;/code&gt; shadows it normally. Pick a different index name (&lt;code&gt;j&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;m&lt;/code&gt;) if you need both in the same scope.&lt;/p&gt;
&lt;h3 id=&quot;unit&quot;&gt;Unit&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;nothing&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// the single value of type `unit`&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;bindings&quot;&gt;Bindings&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// immutable + has storage&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// mutable + has storage&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// OK&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// OK — compound assignment, same as y = y + 1&lt;/span&gt;
                            &lt;span class=&quot;hl-comment&quot;&gt;// (also: -=  *=  /=  %=)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// explicit type annotation&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[]&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// empty literal needs annotation&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `const` — compile-time constant; RHS must be a constant expression.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// May appear at any scope; compiler may inline at use sites.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;PI_OVER_2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;TOL&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0e-12&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;MAX_ITER&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1000&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `const` inside a function works too:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;newton&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;LOCAL_TOL&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0e-8&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Calls to pure functions (prelude or user-defined) returning a scalar&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// are constant expressions:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;SQRT_2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// OK — sqrt is pure&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;HYP&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hypot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// OK — 5.0&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;NINE&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// OK — square is pure&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Calls that return non-scalars (arrays, tuples, structs) still need val:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;SAMPLES&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Shadowing in a new scope is allowed:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// OK: new scope shadows outer p&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// 99&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;block-expressions&quot;&gt;Block expressions&lt;/h2&gt;
&lt;p&gt;A multi-statement block is itself an expression — the value of the block is its final expression. Block expressions arise wherever an expression is expected: &lt;code&gt;val&lt;/code&gt; RHS, function bodies, lambda bodies, branches of &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;computed&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;temp&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;scaled&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100.0&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;scaled&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// value of the block is this final expression&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `if` branches are block expressions too:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;classified&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;positive&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;got &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;non-positive&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;got &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;arithmetic&quot;&gt;Arithmetic&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 10&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 4&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 21&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 2.333...  (real division — operands promoted)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// 2        (integer division — `div` keyword)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 1        (modulo, integer)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// 1024     (exponentiation)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.5&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 1.414...&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// unary minus&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;numeric-promotion&quot;&gt;Numeric promotion&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// integer 1 → real 1.0; result real 3.0&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i     &lt;span class=&quot;hl-comment&quot;&gt;// real → complex; result (1.0 + 2.0i)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i       &lt;span class=&quot;hl-comment&quot;&gt;// integer → real → complex; result (1.0 + 2.0i)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;comparison-and-logical&quot;&gt;Comparison and logical&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// false&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// false&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Element-wise on arrays:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [true, true, false, false]&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Chained comparisons read like the math — `0 &amp;lt;= i &amp;lt; n` desugars to&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// `(0 &amp;lt;= i) and (i &amp;lt; n)` with the middle operand evaluated at most&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// once and only when the previous compare held:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;in_range&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Any number of comparisons may be chained; operators can be mixed.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tight&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;juxtaposition-multiplication&quot;&gt;Juxtaposition multiplication&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x                  &lt;span class=&quot;hl-comment&quot;&gt;// 2 * x = 10.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// 2 * (x + 1) = 12.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;pi                 &lt;span class=&quot;hl-comment&quot;&gt;// 2 * pi&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;sin&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// 3 * sin(pi/4)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i                  &lt;span class=&quot;hl-comment&quot;&gt;// 2 * i&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Identifier-prefix is NOT juxtaposition — xy is one identifier.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// explicit * required between identifiers&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Precedence: ^ &amp;gt; juxtaposition &amp;gt; * / // %&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 2 * (x^3) = 250.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x                &lt;span class=&quot;hl-comment&quot;&gt;// 1 / (2 * x) = 0.1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// (2x) * 3 = 30.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;arrays-rank-1&quot;&gt;Arrays — rank-1&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 3&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// 1.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// 3.0             — negative indices wrap from the end&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// [2.0, 3.0]      slice (owned copy)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0]      inclusive slice&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// [2.0, 3.0]      open upper bound — fills with length(v)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0]      open lower bound — fills with 0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0, 3.0] both ends open — whole-array copy&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;70.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;80.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// [10.0, 30.0, 50.0, 70.0]   — strided: every k-th element&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// [20.0, 50.0, 80.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// [10.0, 30.0, 50.0, 70.0]   — stride combines with open bounds&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// [50.0, 70.0]                 and with negative bounds&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Element-wise arithmetic with broadcasting:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// [6.0, 9.0, 12.0]  — fuses to one loop&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// View-style slicing — non-copying borrow into the source&apos;s buffer.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// Reads and writes alias the source; every rank-1 prelude function&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// (sum, length, map, dot, ...) accepts a view transparently.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;win&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// borrows src[1..4] — [20, 30, 40]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;win&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// writes through: src is [10, 99, 30, 40, 50]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;win&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                             &lt;span class=&quot;hl-comment&quot;&gt;// 99 + 30 + 40 = 169&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// 3-arg form: strided view — borrow every k-th element of the window.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;70&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;every2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// [10, 30, 50, 70]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;every3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// [20, 50, 80]&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// View-of-view composes strides: this borrows every 4th of `big`.&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;every2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// [10, 50]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;arrays-rank-2-matrices&quot;&gt;Arrays — rank-2 (matrices)&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 2×3&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 2&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 3&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// (2, 3)&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 6.0               — element access&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0, 3.0]   — i-th row&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// [2.0, 5.0]        — j-th column&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0]        — row 0, cols 0-1&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [[2.0, 3.0], [5.0, 6.0]]  — submatrix&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// [[4.0, 5.0, 6.0]]         — open lo: rows 1..rows(M)&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// [[1.0, 2.0], [4.0, 5.0]]  — open hi on both axes&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Element-wise on rank-2:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;40.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;N&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [[11.0, 22.0, 33.0], [44.0, 55.0, 66.0]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;Q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;M             &lt;span class=&quot;hl-comment&quot;&gt;// scalar broadcasts&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Matrix multiplication via @ (not element-wise):&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 2×2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;Au&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// [17.0, 39.0]   matrix-vector&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;AA&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// [[7.0, 10.0], [15.0, 22.0]]  matrix-matrix&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// 61.0           dot (rank-1 @ rank-1)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Rank-2 view: borrow a contiguous row range. Row-major layout keeps&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// the window contiguous, so no stride field is needed.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;grid&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;band&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// rows 1..3 — a 2×3 view&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;band&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// writes through: grid[1, 1] is now 99&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// 3-arg form: sub-rectangle view — borrow a window in both axes. The&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// visible rows have gaps in the underlying buffer; the descriptor&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// carries a row-stride field so flat iteration (sum, map, transpose)&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// still picks up the right elements.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// [[4, 5], [99, 8]]    — 2×2 sub-matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;byte-buffers&quot;&gt;&lt;code&gt;[byte]&lt;/code&gt; buffers&lt;/h2&gt;
&lt;p&gt;A packed byte buffer for file I/O and image processing. Bytes are &lt;em&gt;storage&lt;/em&gt; — indexing widens to an integer in &lt;code&gt;0..255&lt;/code&gt;, and writes outside that range trap.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// zero-filled buffer, length 4&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0xFF&lt;/span&gt;                     &lt;span class=&quot;hl-comment&quot;&gt;// each cell holds 0..255&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0x80&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0x00&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0x42&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 4&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                     &lt;span class=&quot;hl-comment&quot;&gt;// 255       — read widens to integer&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                        &lt;span class=&quot;hl-comment&quot;&gt;// [0xFF, 0x80, 0x00, 0x42]&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Widen, compute, narrow:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ints&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_integers&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// [255, 128, 0, 66]            : [integer]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;out&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;ints&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// back to [byte] — traps if any &amp;gt; 255&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// File I/O works on every backend (the prelude routes through&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// the cross-platform runtime, so the same program runs on JVM,&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// Node, and native).&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;write_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello.bin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;read_back&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;read_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello.bin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;read_back&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is no scalar &lt;code&gt;byte&lt;/code&gt; type — &lt;code&gt;byte&lt;/code&gt; only exists as the element of a &lt;code&gt;[byte]&lt;/code&gt; buffer, and arithmetic on bytes is &lt;em&gt;always&lt;/em&gt; done by widening to &lt;code&gt;integer&lt;/code&gt;. There is no element-wise arithmetic, broadcasting, or view form for &lt;code&gt;[byte]&lt;/code&gt;; slicing copies. The full design is in the Specification, §3.3.1 and §10.9.&lt;/p&gt;
&lt;h2 id=&quot;slice-assignment-fortran-90-array-sections&quot;&gt;Slice assignment — Fortran-90 array sections&lt;/h2&gt;
&lt;p&gt;A slice expression on the left of &lt;code&gt;=&lt;/code&gt; overwrites the corresponding sub-extent of a &lt;code&gt;var&lt;/code&gt; array in place. The buffer is mutated, not reallocated.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// xs = [10, 200, 300, 400, 50]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// xs = [1, 2, 3, 400, 50]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// open hi: xs = [1, 2, 3, 99, 100]&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// strided: ys = [1,0,1,0,1,0,1,0,1,0]&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// replace row 1&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// replace column 0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// replace a 2×2 sub-matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The right-hand side must be shape-conforming with the slice; mismatches trap at runtime. See the &lt;a href=&quot;/spec/04-expressions/#415-slice-assignment&quot;&gt;Slice assignment&lt;/a&gt; section of the Expressions chapter for the full rules.&lt;/p&gt;
&lt;h2 id=&quot;tuples&quot;&gt;Tuples&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;three&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                     &lt;span class=&quot;hl-comment&quot;&gt;// no parens needed&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 1&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 2.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// &amp;quot;three&amp;quot;&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Destructuring at binding — no parens needed&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;ignored&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Parens are only needed in contexts where commas mean something else,&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// e.g. function arguments or array elements:&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// 3 separate arguments&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 1 argument, the tuple&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// array of 2 tuples&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;ranges&quot;&gt;Ranges&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// half-open: 0, 1, 2, 3, 4&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// inclusive: 0, 1, 2, 3, 4, 5&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// prints 0, 1, 2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;nums&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// materialize range to [integer]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;structs&quot;&gt;Structs&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 3.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 4.0&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Mutate via var binding:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// OK because q is var&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// rebind whole value&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;sum-types-and-match&quot;&gt;Sum types and &lt;code&gt;match&lt;/code&gt;&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Rect&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Empty&lt;/span&gt;                                       &lt;span class=&quot;hl-comment&quot;&gt;// bare variant — no fields&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Rect&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Empty&lt;/span&gt;      &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;Circle&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)))&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// 12.566370614359172&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;Rect&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)))&lt;/span&gt;  &lt;span class=&quot;hl-comment&quot;&gt;// 12.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 0.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bare variants like &lt;code&gt;Empty&lt;/code&gt; are values; fielded variants like &lt;code&gt;Circle(radius)&lt;/code&gt; are constructors. The scrutinee sits to the left of &lt;code&gt;match&lt;/code&gt; and arms use &lt;code&gt;Pattern -&amp;gt; body&lt;/code&gt;; an optional &lt;code&gt;end match&lt;/code&gt; may close the block. &lt;code&gt;match&lt;/code&gt; is exhaustive — every variant of the scrutinee’s enum must have an arm (or you opt out explicitly with &lt;code&gt;_ -&amp;gt;&lt;/code&gt;); field patterns bind by declaration order, and &lt;code&gt;_&lt;/code&gt; discards.&lt;/p&gt;
&lt;h2 id=&quot;lambdas&quot;&gt;Lambdas&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sq&lt;/span&gt;      &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// explicit param type&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sq2&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// type inferred from context&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sum_xy&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// multi-arg&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;biggy&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt;                        &lt;span class=&quot;hl-comment&quot;&gt;// block-form&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;functions&quot;&gt;Functions&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Single-expression form&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x

&lt;span class=&quot;hl-comment&quot;&gt;// Block-body form&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Explicit return type (required for recursive functions)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Mutual recursion&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;is_even&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;is_odd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;is_odd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;is_even&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Early return&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;first_positive&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// fallback if nothing matched&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Unit-returning function&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Default parameter values + named arguments — defaults sit on the&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// trailing positions; callers can pass `name = value` in any order&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// for any parameter after the positional run.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;depth&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// (3.0, 1.0, 1.0)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                       &lt;span class=&quot;hl-comment&quot;&gt;// (3.0, 5.0, 1.0)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// (3.0, 1.0, 7.0)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;hl-comment&quot;&gt;// (2.0, 3.0, 4.0)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;parameter-modes&quot;&gt;Parameter modes&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// `read` mode is INFERRED when the body doesn&apos;t mutate the parameter.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// The function reads but never modifies v:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;sum_squares&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// inferred read&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `mut` mode is DECLARED when the body mutates the parameter:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;scale_in_place&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factor&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factor&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// A planned `@strict` attribute will disable auto-clone insertion in&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// this function body — every clone must then be written explicitly&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// with .clone(). For performance-critical paths where every allocation&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// must be visible. (Parser-reserved today; deferred.)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;higher-order-functions&quot;&gt;Higher-order functions&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;apply_twice&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;apply_twice&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 7.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;apply_twice&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;16.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// 2.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;closures&quot;&gt;Closures&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_adder&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;add5&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;make_adder&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;add10&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;make_adder&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;add5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// 8.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;add10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 13.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;generic-functions&quot;&gt;Generic functions&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;def&lt;/code&gt; may carry type parameters in &lt;code&gt;[...]&lt;/code&gt; right after the name. The caller never writes the type arguments — the compiler infers each &lt;code&gt;T&lt;/code&gt; from the actuals at the call site and emits a specialized clone per distinct instantiation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Ord&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// T := integer&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// T := string&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 7   — T := integer&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 2.5 — T := real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A bare &lt;code&gt;[T]&lt;/code&gt; admits any type; a bound &lt;code&gt;[T: Kind]&lt;/code&gt; restricts &lt;code&gt;T&lt;/code&gt; to a fixed admission list — &lt;code&gt;Numeric&lt;/code&gt; (&lt;code&gt;integer&lt;/code&gt; / &lt;code&gt;real&lt;/code&gt; / &lt;code&gt;complex&lt;/code&gt;), &lt;code&gt;Real&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Complex&lt;/code&gt;, &lt;code&gt;Ord&lt;/code&gt; (the types with &lt;code&gt;&amp;lt;&lt;/code&gt;), &lt;code&gt;Eq&lt;/code&gt; (the types with &lt;code&gt;==&lt;/code&gt;). The closed set is hard-wired in this milestone; user-defined constraints are deferred.&lt;/p&gt;
&lt;p&gt;Concrete overloads win when they apply — a generic is consulted only when no concrete &lt;code&gt;def&lt;/code&gt; of the same name fits the call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;200&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 100 — concrete integer overload&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// 200 — falls through to the generic&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Generic structs and enums use the same constraint set and specialization model — see the &lt;strong&gt;Generic structs&lt;/strong&gt; and &lt;strong&gt;Generic enums&lt;/strong&gt; sections below.&lt;/p&gt;
&lt;h2 id=&quot;generic-structs&quot;&gt;Generic structs&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;struct&lt;/code&gt; declaration may carry type parameters in &lt;code&gt;[...]&lt;/code&gt; after the name. Field types reference the parameters; the compiler emits a specialized clone per distinct argument combination.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;fst&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;snd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;B&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// T := integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// A := integer, B := string&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// A := real, B := real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The constraint vocabulary is the same closed set as generic functions (&lt;code&gt;Numeric&lt;/code&gt;, &lt;code&gt;Real&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Complex&lt;/code&gt;, &lt;code&gt;Ord&lt;/code&gt;, &lt;code&gt;Eq&lt;/code&gt;, or bare &lt;code&gt;Any&lt;/code&gt;). When call-site inference can’t pin every parameter, write an explicit type on the binding:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A type parameter on a function may thread through to a generic struct constructor — the function’s specialization carries the right struct specialization along:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// Pair$integer$integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// Pair$real$real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;generic-enums&quot;&gt;Generic enums&lt;/h2&gt;
&lt;p&gt;An &lt;code&gt;enum&lt;/code&gt; may also take type parameters. Variant payloads name them; bare variants carry no payload but still belong to the specialization.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Opt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Err&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// Opt$integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Opt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// bare variant — annotation pins T&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// pins E that the use can&apos;t infer&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A bare variant (&lt;code&gt;None&lt;/code&gt;) carries no information to infer its enum’s type parameters from — supply a declared type on the binding to push the parameter down. Multi-parameter enums where a single use pins only some parameters likewise need an explicit type (“cannot infer type for type parameter &lt;code&gt;E&lt;/code&gt;“ if you forget). Matching on a specialized enum uses ordinary patterns:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;got &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;None&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;nothing&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;control-flow&quot;&gt;Control flow&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// `if` is an expression:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;abs_x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `if` with no else has type `unit`:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;verbose&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;doing the thing&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `for` over ranges or arrays, with optional tuple destructuring:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;index &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt; = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `while` loop:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-comment&quot;&gt;// 2&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;                   &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;constants&quot;&gt;Constants&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// 3.14159265358979...&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;e&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 2.71828...&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;inf&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// +infinity&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;nan&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// a quiet NaN&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// (0.0 + 1.0i) — imaginary unit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;prelude-math&quot;&gt;Prelude — math&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 1.414...&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// e&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// 1.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;log2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;log10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;100.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tan&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;asin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;atan2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sinh&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cosh&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// 5&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// -1.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ceil&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;trunc&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;prelude-rank-1-array-operations&quot;&gt;Prelude — rank-1 array operations&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                          &lt;span class=&quot;hl-comment&quot;&gt;// 15.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                      &lt;span class=&quot;hl-comment&quot;&gt;// 120.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                  &lt;span class=&quot;hl-comment&quot;&gt;// 1.0; 5.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                       &lt;span class=&quot;hl-comment&quot;&gt;// 55.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                       &lt;span class=&quot;hl-comment&quot;&gt;// 5&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                               &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 4.0, 9.0, 16.0, 25.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 15.0  (left fold)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [3.0, 4.0, 5.0]&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;hl-comment&quot;&gt;// [(0,1.0), (1,2.0), ...]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [(1.0,10.0), ...]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                     &lt;span class=&quot;hl-comment&quot;&gt;// [0, 1, 2, 3, 4]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;prelude-rank-2-matrix-operations&quot;&gt;Prelude — rank-2 (matrix) operations&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// 3×2&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// 3; 2; (3, 2)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;hl-comment&quot;&gt;// 2×3&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;diag&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                &lt;span class=&quot;hl-comment&quot;&gt;// [[1.0, 0.0], [0.0, 2.0]]  — diagonal matrix&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// 2×3&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;flatten&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                      &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]  (column-major)&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                          &lt;span class=&quot;hl-comment&quot;&gt;// 21.0  (all elements)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum_axis&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                  &lt;span class=&quot;hl-comment&quot;&gt;// [9.0, 12.0]  per-column&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum_axis&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                  &lt;span class=&quot;hl-comment&quot;&gt;// [3.0, 7.0, 11.0]  per-row&lt;/span&gt;
&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                             &lt;span class=&quot;hl-comment&quot;&gt;// element-wise&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                         &lt;span class=&quot;hl-comment&quot;&gt;// 2×2; same as transpose(M) @ M&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;prelude-construction&quot;&gt;Prelude — construction&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                        &lt;span class=&quot;hl-comment&quot;&gt;// [0, 0, 0, 0, 0]            — integer zeros&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                         &lt;span class=&quot;hl-comment&quot;&gt;// [1, 1, 1]                  — integer ones&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;hl-comment&quot;&gt;// [7.0, 7.0, 7.0, 7.0]       — real, from x: real&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;hl-comment&quot;&gt;// [0.0, 0.0, 0.0, 0.0, 0.0]  — real zeros via fill&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// [0.0, 0.25, 0.5, 0.75, 1.0]&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;                                   &lt;span class=&quot;hl-comment&quot;&gt;// 2×3 integer zero matrix&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;                                    &lt;span class=&quot;hl-comment&quot;&gt;// 3×3 integer ones&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                 &lt;span class=&quot;hl-comment&quot;&gt;// 2×2 of integer 9&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                               &lt;span class=&quot;hl-comment&quot;&gt;// 3×2 real zero matrix&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                     &lt;span class=&quot;hl-comment&quot;&gt;// 3×3 integer identity matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rank-2 construction takes a &lt;code&gt;(rows, cols)&lt;/code&gt; tuple as the shape argument (single-integer arg = rank-1; tuple arg = rank-2). &lt;code&gt;zeros&lt;/code&gt;/&lt;code&gt;ones&lt;/code&gt;/&lt;code&gt;identity&lt;/code&gt; currently return integer-element arrays — for real-typed initial buffers use &lt;code&gt;fill(n, 0.0)&lt;/code&gt; / &lt;code&gt;fill((r, c), 0.0)&lt;/code&gt; / &lt;code&gt;linspace&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;prelude-i-o&quot;&gt;Prelude — I/O&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                  &lt;span class=&quot;hl-comment&quot;&gt;// with newline&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;                                         &lt;span class=&quot;hl-comment&quot;&gt;// newline alone&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;x = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                &lt;span class=&quot;hl-comment&quot;&gt;// plain interpolation&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;x = $x%5d, y = $y%.3f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// f-string with printf-style spec&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;alpha = $a%8.4f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// value-position is fine too&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;f&amp;quot;...&amp;quot;&lt;/code&gt; accepts the same &lt;code&gt;$ident&lt;/code&gt; / &lt;code&gt;${expr}&lt;/code&gt; interpolations as &lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt;, plus an optional Scala/printf-style spec right after each value: &lt;code&gt;%[flags][width][.precision]conv&lt;/code&gt; with conv ∈ &lt;code&gt;d f e g s x X o b&lt;/code&gt;. Flags &lt;code&gt;-&lt;/code&gt; (left-align), &lt;code&gt;0&lt;/code&gt; (zero-pad). See spec §10.6.&lt;/p&gt;
&lt;h2 id=&quot;prelude-conversions&quot;&gt;Prelude — conversions&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                     &lt;span class=&quot;hl-comment&quot;&gt;// 42.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                 &lt;span class=&quot;hl-comment&quot;&gt;// 3 (truncates toward zero)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                                 &lt;span class=&quot;hl-comment&quot;&gt;// (5.0 + 0.0i)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_addition&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_eq&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_with_message&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;expected length 2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_approx_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_approx_complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// Checks `|a - b| &amp;lt;= tol` via hypot on the componentwise differences.&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.000001&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.000001&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_approx_array&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// Element-wise rank-1 form: integer/real/complex elements are compared&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// per element with the same per-type distance as the scalar overloads.&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// A length mismatch traps.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_approx_array2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// Same shape: rank-2 element-wise. Rows or cols mismatch traps.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_traps_on_bad_division&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_traps&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_trap_message_contains_substring&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// The 2-arg form additionally checks that the trap&apos;s message&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// contains the expected substring — useful for asserting a specific&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// failure mode rather than &amp;quot;any trap fires&amp;quot;.&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_traps&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;division by zero&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A whole module can be marked test-only:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib.deep_tests&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;public_fn&lt;/span&gt;}

&lt;span class=&quot;hl-comment&quot;&gt;// helper functions live alongside tests — both are stripped from production builds&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_input&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_public_fn_monotonic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;make_input&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;public_fn&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;modules&quot;&gt;Modules&lt;/h2&gt;
&lt;p&gt;Folder layout:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;mylib&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;          ← &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;          ← &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;same&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sibling&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// in src/mylib/core.nex&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;public_fn&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;helper&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// in src/mylib/util.nex&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `helper` is visible because we&apos;re in the same module folder&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;public_use_of_helper&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;helper&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// in src/main.nex&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;public_fn&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;public_use_of_helper&lt;/span&gt;}
&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mylib&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;public_fn&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pf&lt;/span&gt;}      &lt;span class=&quot;hl-comment&quot;&gt;// rename on import&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;public_fn&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// 4.0&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 4.0&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// helper is NOT visible here — it&apos;s private to mylib&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;literate-nex&quot;&gt;Literate Nex&lt;/h2&gt;
&lt;p&gt;A file with the &lt;code&gt;.lnex&lt;/code&gt; extension is Markdown: column-0 prose, code indented one level (tab or four-plus spaces). The compiler strips the prose to blank lines (so error positions still match) and dedents the code before lexing. A module may freely mix &lt;code&gt;.nex&lt;/code&gt; and &lt;code&gt;.lnex&lt;/code&gt; files.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;A short paragraph above the function explains why it exists.

    def square(x: real): real = x * x

A second paragraph between code blocks renders normally in
documentation and is invisible to the compiler.

    def main() =
      print(square(2.5))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;See spec §2.9 for fenced-block handling and corner cases.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Dedicated @test Modules</title>
    <link href="https://nexlang.org/examples/test-modules/"/>
    <id>https://nexlang.org/examples/test-modules/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>A sibling test-only module with support code and table-driven fixtures, stripped from non-test builds.</summary>
    <content type="html">&lt;p&gt;When tests need a lot of helpers, fixtures, or table-driven data, put them in a sibling &lt;code&gt;@test&lt;/code&gt; module. The whole module is stripped from non-test builds.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats.distribution_tests&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;skewness&lt;/span&gt;}

&lt;span class=&quot;hl-comment&quot;&gt;// --- support code (not tests, but available to tests in this module) ---&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;constant_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;linear_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;constant_cases&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;constant_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;constant_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;constant_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// --- tests ---&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_mean_recovers_constants&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;constant_cases&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_stddev_of_any_constant_is_zero&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;constant_cases&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_skewness_of_symmetric_linear_dataset_is_zero&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// 0..n-1 is symmetric around (n-1)/2&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;skewness&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;linear_dataset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This style is appropriate when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The tests need lots of shared setup&lt;/li&gt;
&lt;li&gt;You want fixture data computed once and shared across tests&lt;/li&gt;
&lt;li&gt;The support code is itself testing-specific and would be noise in the regular module&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cost of a &lt;code&gt;@test&lt;/code&gt; module: it can only see &lt;em&gt;public&lt;/em&gt; members of the modules it imports. If you need to test &lt;code&gt;private&lt;/code&gt; helpers, use inline &lt;code&gt;@test&lt;/code&gt; functions in the same module instead (as in the Multi-File Modules example).&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Supported Targets</title>
    <link href="https://nexlang.org/tooling/targets/"/>
    <id>https://nexlang.org/tooling/targets/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Where the AOT path is verified, where the CLI builds, and what&apos;s planned.</summary>
    <content type="html">&lt;h2 id=&quot;aot-path-native-binaries-via-nex-compile&quot;&gt;AOT path (native binaries via &lt;code&gt;nex compile&lt;/code&gt;)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Mac arm64&lt;/strong&gt; — verified end-to-end on every commit. The &lt;code&gt;clang -O1&lt;/code&gt; invocation produces a Mach-O binary; CI runs it and diffs the stdout against the interpreter.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Other Unix targets (Linux x86_64, Linux arm64) should work given a working &lt;code&gt;clang -O1&lt;/code&gt;, but they are not yet exercised by CI.&lt;/p&gt;
&lt;h2 id=&quot;cli-builds&quot;&gt;CLI builds&lt;/h2&gt;
&lt;p&gt;The Nex compiler is cross-compiled to three targets via Scala 3, and the full unit-test suite (over two thousand tests on JVM today) passes identically on all three on every commit. (Backend-shelling tests — those that drive &lt;code&gt;clang&lt;/code&gt; or the MLIR toolchain — only run on JVM; the cross-platform tests cover the parser, elaborator, and interpreter.) Filesystem operations route through the &lt;a href=&quot;https://github.com/edadma/path&quot;&gt;&lt;code&gt;path&lt;/code&gt;&lt;/a&gt; library so the module loader runs the same code on JVM filesystems, Node’s &lt;code&gt;fs&lt;/code&gt;, and Native via &lt;code&gt;java.nio.file&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JVM&lt;/strong&gt; — primary development target. The full CLI (&lt;code&gt;tokens&lt;/code&gt;, &lt;code&gt;parse&lt;/code&gt;, &lt;code&gt;elaborate&lt;/code&gt;, &lt;code&gt;run&lt;/code&gt;, &lt;code&gt;test&lt;/code&gt;, &lt;code&gt;compile&lt;/code&gt;) lives here; the JVM is the only target that exercises the AOT path because it shells out to &lt;code&gt;clang&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scala.js (Node)&lt;/strong&gt; — the interpreter and module loader work the same as JVM. &lt;code&gt;nex run&lt;/code&gt; / &lt;code&gt;nex test&lt;/code&gt; are functional; no &lt;code&gt;compile&lt;/code&gt; subcommand because there’s no &lt;code&gt;clang&lt;/code&gt; to shell out to.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scala Native&lt;/strong&gt; — same story as Scala.js. &lt;code&gt;nex run&lt;/code&gt; / &lt;code&gt;nex test&lt;/code&gt; work; no AOT path.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;planned&quot;&gt;Planned&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;GPU / accelerator targets — &lt;em&gt;deferred&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Linux x86_64 / arm64 — likely to be folded into CI once a runner is available; no language work required.&lt;/li&gt;
&lt;li&gt;WebAssembly — possible via a future LLVM IR → wasm backend; no concrete plan yet.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>Sum Types and Match</title>
    <link href="https://nexlang.org/examples/sum-types/"/>
    <id>https://nexlang.org/examples/sum-types/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Modeling a Newton-style solver&apos;s outcomes with an enum, then dispatching with match.</summary>
    <content type="html">&lt;p&gt;A solver loop typically has three distinct outcomes: it converged on a value, the iterates diverged, or it ran out of iterations without converging. Modeling the &lt;em&gt;result&lt;/em&gt; as a flat structure (&lt;code&gt;(success: bool, x: real, iters: integer)&lt;/code&gt;) loses information: the meaning of &lt;code&gt;x&lt;/code&gt; depends on &lt;code&gt;success&lt;/code&gt;, and the meaning of &lt;code&gt;iters&lt;/code&gt; depends on both. A sum type makes the three cases — and the data each one carries — explicit.&lt;/p&gt;
&lt;p&gt;The driver below is Newton’s method for &lt;span class=&quot;math inline&quot;&gt;\(f(x) = 0\)&lt;/span&gt;,&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[x_{n+1} \;=\; x_n - \frac{f(x_n)}{f&apos;(x_n)},\]&lt;/div&gt;
&lt;p&gt;reporting &lt;code&gt;Converged(x)&lt;/code&gt; once &lt;span class=&quot;math inline&quot;&gt;\(\lvert f(x_n) \rvert &amp;lt; \text{tol}\)&lt;/span&gt;, &lt;code&gt;Diverged&lt;/code&gt; if &lt;span class=&quot;math inline&quot;&gt;\(f&apos;(x_n)\)&lt;/span&gt; approaches zero (the update step blows up), and &lt;code&gt;MaxIters(iters, last)&lt;/code&gt; if the loop exhausts its budget without either condition firing. The example function is &lt;span class=&quot;math inline&quot;&gt;\(f(x) = x^2 - 2\)&lt;/span&gt;, &lt;span class=&quot;math inline&quot;&gt;\(f&apos;(x) = 2x\)&lt;/span&gt;, so a converging run lands on &lt;span class=&quot;math inline&quot;&gt;\(\sqrt{2}\)&lt;/span&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-solver&quot;&gt;The solver&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;newton&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;max_iters&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fx&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;fx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0e-15&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fx&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;max_iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each &lt;code&gt;return&lt;/code&gt; site builds a value of the enum type. &lt;code&gt;Converged(x)&lt;/code&gt; calls the fielded constructor (one &lt;code&gt;real&lt;/code&gt; field); &lt;code&gt;Diverged&lt;/code&gt; is a bare variant, so the name itself is the value; &lt;code&gt;MaxIters(...)&lt;/code&gt; constructs the two-field variant. All three are typed &lt;code&gt;Solver&lt;/code&gt;, so the function’s declared return type is satisfied uniformly.&lt;/p&gt;
&lt;h2 id=&quot;inspecting-the-outcome-with-match&quot;&gt;Inspecting the outcome with &lt;code&gt;match&lt;/code&gt;&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;converged at x=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;             &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;diverged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;ran &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt; iters, last x=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;newton&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;hl-number&quot;&gt;1.0e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;newton&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;hl-number&quot;&gt;1.0e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)))&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// fp(0)=0 → Diverged&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;newton&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0e9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;hl-number&quot;&gt;1.0e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)))&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// 3 iters too few&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;converged at x=1.4142135623730951
diverged
ran 3 iters, last x=125000000.0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three properties to notice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Exhaustiveness.&lt;/strong&gt; Every variant of &lt;code&gt;Solver&lt;/code&gt; has its own arm. Drop one and the compiler refuses to build the program — there is no quiet “default fell through” path. A wildcard &lt;code&gt;case _ =&amp;gt;&lt;/code&gt; arm is allowed when only some variants matter, but you opt in by writing it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Field binding.&lt;/strong&gt; &lt;code&gt;Converged(x)&lt;/code&gt; extracts the &lt;code&gt;real&lt;/code&gt; field into a name visible inside the arm body. &lt;code&gt;MaxIters(iters, x)&lt;/code&gt; binds two fields in declaration order, available throughout the arm body. Use &lt;code&gt;_&lt;/code&gt; for any field you don’t need (&lt;code&gt;case Continue(_) =&amp;gt; ...&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One result type.&lt;/strong&gt; Every arm body must evaluate to the same type — here, &lt;code&gt;string&lt;/code&gt;. The whole &lt;code&gt;match&lt;/code&gt; expression is itself a &lt;code&gt;string&lt;/code&gt;, so it can flow into &lt;code&gt;print&lt;/code&gt;, into a binding, or into another expression.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;when-to-reach-for-a-sum-type&quot;&gt;When to reach for a sum type&lt;/h2&gt;
&lt;p&gt;A sum type fits when a value has a &lt;em&gt;bounded set of named cases&lt;/em&gt;, each with its own associated data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Solver outcomes (&lt;code&gt;Converged | Diverged | MaxIters&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Parsed tokens (&lt;code&gt;Number(real) | Identifier(string) | LParen | RParen | ...&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Tree shapes (&lt;code&gt;Leaf(real) | Branch(real, real)&lt;/code&gt; — once nested variant fields ship)&lt;/li&gt;
&lt;li&gt;Operation results that distinguish “succeeded with x” from “failed because of y”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the cases all carry the &lt;em&gt;same&lt;/em&gt; shape and only differ in a small tag, a struct with an integer/string tag field is simpler. If the cases are unbounded or grow at runtime (user-defined plugins, dynamic types), a sum type isn’t the right tool — that’s where traits / interfaces would fit, both of which are deferred from the current language.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Three Ways to Express the Same Computation</title>
    <link href="https://nexlang.org/examples/styles/"/>
    <id>https://nexlang.org/examples/styles/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>The same `normalize` algorithm written three ways — functional (fuses to one loop), explicit-loop with a local `var`, and slice assignment for partial in-place updates.</summary>
    <content type="html">&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// 1) Functional: fresh array, fusion-friendly. The expression `v / mag`&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    lowers to a single fused loop with no intermediate.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// 2) Explicit-loop with a local `var`: same machine code as (1), useful&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    when the loop body is more complex than a single element-wise&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    expression can capture. `out` is unique inside the function, so&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    indexed writes are in-place; the return value moves the buffer out.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize_loop&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;out&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// 3) Slice assignment: overwrite a sub-extent of an existing var array&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    in place. The assignment mutates the underlying buffer in the&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//    binding&apos;s own scope — no clone, no reallocation, no new identity.&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// 1) Functional:&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [0.6, 0.8]&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// 2) Explicit loop — same answer, same machine code (per design promise):&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;normalize_loop&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [0.6, 0.8]&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// 3) Slice assignment: replace the first two elements in place.&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;//    This is the idiom for partial in-place updates on a var array.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// open lo: writes positions 0 and 1&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [0.6, 0.8, 9.0, 9.0]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Sum of Sinusoids</title>
    <link href="https://nexlang.org/examples/sinusoids/"/>
    <id>https://nexlang.org/examples/sinusoids/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>A three-term wave function and a partial Fourier sum, sampled over [0, 1].</summary>
    <content type="html">&lt;p&gt;A pure sinusoid has the form &lt;span class=&quot;math inline&quot;&gt;\(y(t) = A \sin(2\pi f t + \varphi)\)&lt;/span&gt; — amplitude &lt;span class=&quot;math inline&quot;&gt;\(A\)&lt;/span&gt;, frequency &lt;span class=&quot;math inline&quot;&gt;\(f\)&lt;/span&gt; (cycles per unit of &lt;span class=&quot;math inline&quot;&gt;\(t\)&lt;/span&gt;), and phase &lt;span class=&quot;math inline&quot;&gt;\(\varphi\)&lt;/span&gt;. Adding several sinusoids with integer-multiple frequencies builds a periodic &lt;em&gt;waveform&lt;/em&gt;; the wave below sums three such components on &lt;span class=&quot;math inline&quot;&gt;\([0, 1]\)&lt;/span&gt;:&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[w(t) \;=\; 3\sin(2\pi t) \;+\; 2\cos(4\pi t) \;-\; \sin(6\pi t).\]&lt;/div&gt;
&lt;p&gt;The second example evaluates a partial Fourier sine series at a single &lt;span class=&quot;math inline&quot;&gt;\(t\)&lt;/span&gt;,&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[s(t) \;=\; \sum_{k=1}^{n} c_k \sin(2\pi k t),\]&lt;/div&gt;
&lt;p&gt;where &lt;code&gt;coeffs[k-1]&lt;/code&gt; holds &lt;span class=&quot;math inline&quot;&gt;\(c_k\)&lt;/span&gt;. Both functions exploit juxtaposition multiplication so the source reads like the math: &lt;code&gt;3sin(2pi*t)&lt;/code&gt;, &lt;code&gt;2cos(4pi*t)&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Three-term wave: w(t) = 3 sin(2π t) + 2 cos(4π t) - sin(6π t)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;wave&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;sin&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;pi&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;cos&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;pi&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt;pi&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Partial Fourier sum: s(t) = Σ_{k=1..n} coeffs[k-1] · sin(2π k t)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;fourier_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;coeffs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;coeffs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;coeffs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;pi &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ts&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;wave&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;wave(0.25) = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;wave&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;max sample = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;min sample = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;coeffs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.125&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;fourier(0.1) = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;fourier_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;coeffs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Standard Library / Prelude</title>
    <link href="https://nexlang.org/spec/prelude/"/>
    <id>https://nexlang.org/spec/prelude/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Constants, scalar math, complex operations, rank-1 and rank-2 array operations, construction, I/O, conversions, assertions.</summary>
    <content type="html">&lt;p&gt;The prelude is implicitly imported in every module. It contains the names available without any &lt;code&gt;import&lt;/code&gt; statement.&lt;/p&gt;
&lt;p&gt;The scalar math layer (the libm transcendentals plus their complex extensions) lives as ordinary Nex source in the sysroot’s &lt;code&gt;prelude/&lt;/code&gt; directory and is auto-imported at every program’s elaboration. The compiler still seeds a handful of names whose dispatch depends on return-type information (&lt;code&gt;abs&lt;/code&gt;, &lt;code&gt;sign&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, &lt;code&gt;conj&lt;/code&gt;, &lt;code&gt;arg&lt;/code&gt;) plus the array prelude and I/O — those will migrate to source as the relevant overload-resolution rules generalize. The split is invisible to users; either way the names are in scope.&lt;/p&gt;
&lt;h2 id=&quot;10-1-constants&quot;&gt;10.1 Constants&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;pi&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;π = 3.14159265358979…&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;e&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Euler’s number = 2.71828…&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;inf&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;positive infinity&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;nan&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;a quiet NaN&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;i&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;complex&lt;/code&gt;&lt;/td&gt;&lt;td&gt;imaginary unit = &lt;code&gt;(0.0, 1.0)&lt;/code&gt; (shadowable; see the Lexical chapter)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The boolean literals &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; (see the Lexical chapter) are globally available as keywords, not prelude names.&lt;/p&gt;
&lt;h2 id=&quot;10-2-mathematical-functions-on-scalars&quot;&gt;10.2 Mathematical functions on scalars&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;sqrt, cbrt, abs, sign
exp, log, log2, log10
sin, cos, tan, asin, acos, atan, atan2
sinh, cosh, tanh, asinh, acosh, atanh
hypot
floor, ceil, round, trunc
min, max
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each is overloaded over numeric types as appropriate. &lt;code&gt;sqrt&lt;/code&gt;, &lt;code&gt;exp&lt;/code&gt;, &lt;code&gt;log&lt;/code&gt;, &lt;code&gt;log2&lt;/code&gt;, &lt;code&gt;log10&lt;/code&gt;, &lt;code&gt;sin&lt;/code&gt;, &lt;code&gt;cos&lt;/code&gt;, &lt;code&gt;tan&lt;/code&gt; apply to both &lt;code&gt;real&lt;/code&gt; and &lt;code&gt;complex&lt;/code&gt; (the complex variants are source-level Nex &lt;code&gt;def&lt;/code&gt;s in the prelude that compose the real-libm primitives — overload resolution picks the right one by argument type at the call site).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;hypot(x, y)&lt;/code&gt; returns the Euclidean norm &lt;span class=&quot;math inline&quot;&gt;\(\sqrt{x^2 + y^2}\)&lt;/span&gt; without the intermediate overflow that a naive &lt;code&gt;sqrt(x*x + y*y)&lt;/code&gt; would suffer for large &lt;span class=&quot;math inline&quot;&gt;\(\lvert x \rvert\)&lt;/span&gt; or &lt;span class=&quot;math inline&quot;&gt;\(\lvert y \rvert\)&lt;/span&gt;. &lt;code&gt;sign(x)&lt;/code&gt; is the signum function: &lt;span class=&quot;math inline&quot;&gt;\(-1\)&lt;/span&gt;, &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt;, or &lt;span class=&quot;math inline&quot;&gt;\(+1\)&lt;/span&gt; for negative, zero, and positive real &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; respectively.&lt;/p&gt;
&lt;h2 id=&quot;10-3-complex-specific-operations&quot;&gt;10.3 Complex-specific operations&lt;/h2&gt;
&lt;p&gt;The real and imaginary parts of a &lt;code&gt;complex&lt;/code&gt; value are accessed as fields with &lt;code&gt;.&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;re&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// real part        (type: real)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;im&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// imaginary part   (type: real)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other complex operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;conj&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// complex conjugate&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// argument (angle, in radians)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// magnitude&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Equivalent method-style: &lt;code&gt;z.conj()&lt;/code&gt;, &lt;code&gt;z.arg()&lt;/code&gt;, &lt;code&gt;z.abs()&lt;/code&gt; (see the method-like notation rules in the Expressions chapter).&lt;/p&gt;
&lt;p&gt;Construction of complex values uses the prelude constant &lt;code&gt;i&lt;/code&gt; (§10.1) together with arithmetic, supported by juxtaposition multiplication (see the Expressions chapter):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;i             &lt;span class=&quot;hl-comment&quot;&gt;// (3.0 + 4.0i)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;i               &lt;span class=&quot;hl-comment&quot;&gt;// (0.0 + 2.5i)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// ≈ (-1.0 + 0.0i) — Euler&apos;s identity&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;pi &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// ≈ (1.0 + 0.0i)  — full rotation&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;10-4-array-operations&quot;&gt;10.4 Array operations&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Rank-1 operations:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// T must be numeric&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// T must be numeric&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// T must be ordered numeric&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// T must be ordered numeric&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// inner product; T numeric&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// concat results&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;lo&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// materialize range&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Definitions.&lt;/strong&gt; Indices run &lt;span class=&quot;math inline&quot;&gt;\(i = 0, \ldots, n-1\)&lt;/span&gt; where $n = $ &lt;code&gt;length(a)&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;sum(a)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_i a_i\)&lt;/span&gt; and &lt;code&gt;product(a)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \prod_i a_i\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min(a)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \min_i a_i\)&lt;/span&gt; and &lt;code&gt;max(a)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \max_i a_i\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dot(a, b)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_i a_i\, b_i\)&lt;/span&gt; — &lt;code&gt;length(a)&lt;/code&gt; must equal &lt;code&gt;length(b)&lt;/code&gt;; mismatch traps.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map(a, f)[i]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= f(a_i)\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;filter(a, p)&lt;/code&gt; keeps each &lt;span class=&quot;math inline&quot;&gt;\(a_i\)&lt;/span&gt; for which &lt;span class=&quot;math inline&quot;&gt;\(p(a_i)\)&lt;/span&gt; is &lt;code&gt;true&lt;/code&gt;, preserving the original order.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reduce(a, z, f)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= f(\ldots f(f(z, a_0), a_1) \ldots, a_{n-1})\)&lt;/span&gt; — left fold seeded with &lt;span class=&quot;math inline&quot;&gt;\(z\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;range(lo, hi)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= [lo,\; lo+1, \ldots, hi-1]\)&lt;/span&gt; — exclusive upper; empty if &lt;span class=&quot;math inline&quot;&gt;\(hi \le lo\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;enumerate(a)[i]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= (i, a_i)\)&lt;/span&gt; and &lt;code&gt;zip(a, b)[i]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= (a_i, b_i)\)&lt;/span&gt; — &lt;code&gt;zip&lt;/code&gt; truncates to &lt;span class=&quot;math inline&quot;&gt;\(\min(\text{length}(a), \text{length}(b))\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Calling convention.&lt;/strong&gt; All the higher-order array functions
(&lt;code&gt;map&lt;/code&gt; / &lt;code&gt;flatMap&lt;/code&gt; / &lt;code&gt;reduce&lt;/code&gt; / &lt;code&gt;filter&lt;/code&gt;) and the rank-1 accessors
(&lt;code&gt;length&lt;/code&gt; / &lt;code&gt;sum&lt;/code&gt; / &lt;code&gt;product&lt;/code&gt; / &lt;code&gt;dot&lt;/code&gt; / &lt;code&gt;enumerate&lt;/code&gt; / &lt;code&gt;zip&lt;/code&gt;)
are typically called via method-call sugar (§4.9). The two forms
are equivalent — the dot form just sugars &lt;code&gt;arr.name(...)&lt;/code&gt; into
&lt;code&gt;name(arr, ...)&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Preferred — reads as a pipeline:&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Same call shape, free-function form:&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Either compiles to the same AST; the dot form is the documented
convention for chains.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rank-2 operations:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// number of rows&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// number of columns&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// (rows, cols)&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// m × n → n × m&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// also available via the @ operator&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;diag&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;                     &lt;span class=&quot;hl-comment&quot;&gt;// n×n diagonal matrix with d on the diagonal&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// length(a) must equal m * n&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;flatten&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// column-major flatten&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;                        &lt;span class=&quot;hl-comment&quot;&gt;// sum over all elements&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sum_axis&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;hl-comment&quot;&gt;// axis=0 → per-column; axis=1 → per-row&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// element-wise&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Definitions.&lt;/strong&gt; For a matrix &lt;span class=&quot;math inline&quot;&gt;\(M\)&lt;/span&gt; with row index &lt;span class=&quot;math inline&quot;&gt;\(i = 0, \ldots, r-1\)&lt;/span&gt; and column index &lt;span class=&quot;math inline&quot;&gt;\(j = 0, \ldots, c-1\)&lt;/span&gt; where $(r, c) = $ &lt;code&gt;shape(M)&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;transpose(M)[i, j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= M_{j i}\)&lt;/span&gt; — an &lt;span class=&quot;math inline&quot;&gt;\(r \times c\)&lt;/span&gt; matrix becomes &lt;span class=&quot;math inline&quot;&gt;\(c \times r\)&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;matmul(A, B)[i, j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_k A_{i k}\, B_{k j}\)&lt;/span&gt; — also written &lt;code&gt;A @ B&lt;/code&gt;; &lt;code&gt;cols(A)&lt;/code&gt; must equal &lt;code&gt;rows(B)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;diag(d)[i, j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= d_i\)&lt;/span&gt; if &lt;span class=&quot;math inline&quot;&gt;\(i = j\)&lt;/span&gt;, else &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt; — an &lt;span class=&quot;math inline&quot;&gt;\(n \times n\)&lt;/span&gt; matrix from a length-&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sum(M)&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_{i, j} M_{i j}\)&lt;/span&gt; — total over every element.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sum_axis(M, 0)[j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_i M_{i j}\)&lt;/span&gt; — column sums; result length &lt;code&gt;cols(M)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sum_axis(M, 1)[i]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= \sum_j M_{i j}\)&lt;/span&gt; — row sums; result length &lt;code&gt;rows(M)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map(M, f)[i, j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= f(M_{i j})\)&lt;/span&gt; — element-wise; shape preserved.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;flatten(M)[k]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= M_{k \bmod r,\; k \div r}\)&lt;/span&gt; and &lt;code&gt;reshape(a, r, c)[i, j]&lt;/code&gt; &lt;span class=&quot;math inline&quot;&gt;\(= a[j \cdot r + i]\)&lt;/span&gt; — both follow the column-major storage convention from &lt;a href=&quot;/spec/03-types/#33-array-types&quot;&gt;§3.3&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are built-in: the compiler knows their types and lowers them with fusion-aware codegen. The user-facing surfaces for the same shape are generic functions (&lt;a href=&quot;/spec/06-functions/#610-generic-functions&quot;&gt;§6.10&lt;/a&gt;) and generic structs / enums (&lt;a href=&quot;/spec/03-types/#36-generic-structs&quot;&gt;§3.6&lt;/a&gt;, &lt;a href=&quot;/spec/03-types/#38-generic-enums&quot;&gt;§3.8&lt;/a&gt;).&lt;/p&gt;
&lt;h2 id=&quot;10-5-construction&quot;&gt;10.5 Construction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Rank-1:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;lo&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Rank-2:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// n × n identity matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;zeros&lt;/code&gt;, &lt;code&gt;ones&lt;/code&gt;, and &lt;code&gt;fill&lt;/code&gt; dispatch on shape: a single &lt;code&gt;integer&lt;/code&gt; argument produces rank-1, a &lt;code&gt;(integer, integer)&lt;/code&gt; tuple produces rank-2.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// rank-1: [0, 0, 0, 0, 0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// rank-2: [[0, 0, 0], [0, 0, 0]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// rank-1: [7.0, 7.0, 7.0, 7.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// rank-2 reals&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// rank-2 integer identity matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;zeros&lt;/code&gt;, &lt;code&gt;ones&lt;/code&gt;, and &lt;code&gt;identity&lt;/code&gt; currently return &lt;strong&gt;integer&lt;/strong&gt; element type. When you need a real-typed buffer, use &lt;code&gt;fill(n, 0.0)&lt;/code&gt;, &lt;code&gt;fill(n, 1.0)&lt;/code&gt;, or &lt;code&gt;linspace&lt;/code&gt;; for a real identity, multiply by &lt;code&gt;1.0&lt;/code&gt; (&lt;code&gt;identity(n) * 1.0&lt;/code&gt;) or fill manually. A future refinement is expected once overload-by-return-type lands.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View-style slicing.&lt;/strong&gt; Alongside the copying slice &lt;code&gt;a[lo..hi]&lt;/code&gt; (spec §4.14), a rank-1 array exposes a non-copying &lt;code&gt;view&lt;/code&gt; form:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// borrows a[1..4] — [20, 30, 40]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// writes through: a is now [10, 99, 30, 40, 50]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// 3&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// 99 + 30 + 40 = 169&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;a.view(r)&lt;/code&gt; returns a non-copying borrow into &lt;code&gt;a&lt;/code&gt; covering the index range &lt;code&gt;r&lt;/code&gt; (exclusive &lt;code&gt;lo..hi&lt;/code&gt; or inclusive &lt;code&gt;lo..=hi&lt;/code&gt;; negative bounds wrap from the end). Reads through the view see the source’s current contents; writes (&lt;code&gt;v[i] = x&lt;/code&gt;) update the source. Every &lt;code&gt;[T]&lt;/code&gt; prelude function (&lt;code&gt;sum&lt;/code&gt;, &lt;code&gt;length&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;dot&lt;/code&gt;, …) accepts a view transparently. An out-of-bounds range traps.&lt;/p&gt;
&lt;p&gt;A 3-arg form &lt;code&gt;a.view(lo..hi, k)&lt;/code&gt; borrows every &lt;code&gt;k&lt;/code&gt;-th element of the window:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;70&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;every2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// [10, 30, 50, 70]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;every3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;big&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// [20, 50, 80]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;k&lt;/code&gt; must be a positive integer; non-positive strides trap. View-of-view composes strides — &lt;code&gt;every2.view(0..4, 2)&lt;/code&gt; borrows every 4th element of &lt;code&gt;big&lt;/code&gt; — and the contiguous 2-arg form is the special case &lt;code&gt;k = 1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A rank-2 matrix exposes the same &lt;code&gt;view&lt;/code&gt; form for a contiguous row range:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// rows 1..3 — a 2×3 view&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// writes through: m[1, 1] is now 99&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 2&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// works on a view&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Row-major layout makes any row range contiguous in memory, so a row-range view shares the same flat descriptor mechanism as rank-1 — &lt;code&gt;rows&lt;/code&gt;, &lt;code&gt;cols&lt;/code&gt;, &lt;code&gt;shape&lt;/code&gt;, &lt;code&gt;transpose&lt;/code&gt;, &lt;code&gt;sum&lt;/code&gt;, &lt;code&gt;sum_axis&lt;/code&gt;, element indexing, and row indexing all accept a row-range view without copying.&lt;/p&gt;
&lt;p&gt;A 3-arg form &lt;code&gt;m.view(rRange, cRange)&lt;/code&gt; borrows a non-contiguous sub-matrix:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// [[4, 5], [7, 8]]   — 2×2 window&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;99&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// writes through: m[1, 1] is now 99&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// rank-2 ops work over the gap&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The visible rows have gaps in the underlying buffer; the descriptor carries an explicit row-stride field so flat iteration (&lt;code&gt;sum&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;transpose&lt;/code&gt;, &lt;code&gt;matmul&lt;/code&gt;, …) decomposes the linear index back into &lt;code&gt;(r, c)&lt;/code&gt; and picks up the right elements across the gap. The rank-1 and rank-2 3-arg forms disambiguate on the 3rd argument’s shape — an integer is a stride (rank-1), a range is a column window (rank-2).&lt;/p&gt;
&lt;p&gt;Spec §4.14 — &lt;code&gt;a[lo..hi]&lt;/code&gt; allocates a fresh array and copies elements in. &lt;code&gt;a.view(...)&lt;/code&gt; is the alternative for places where copying is wasteful: in-place algorithms (FFT, row pivoting), passing windows to reduction kernels, or working on a sub-range without changing the source.&lt;/p&gt;
&lt;h2 id=&quot;10-6-i-o&quot;&gt;10.6 I/O&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// print value with newline&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// print newline alone&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For string composition, Nex provides two interpolated string forms (see also the Lexical chapter):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt; — plain interpolation.&lt;/strong&gt; &lt;code&gt;$ident&lt;/code&gt; and &lt;code&gt;${expr}&lt;/code&gt; are replaced by the default printed form of the value (whole reals as &lt;code&gt;n.0&lt;/code&gt;, strings unquoted, structs as &lt;code&gt;Name { ... }&lt;/code&gt;, etc.).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;x = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// x = 42&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;sum = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// sum = 43&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;f&amp;quot;...&amp;quot;&lt;/code&gt; — formatted interpolation.&lt;/strong&gt; Same as &lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt;, but each &lt;code&gt;$ident&lt;/code&gt; or &lt;code&gt;${expr}&lt;/code&gt; may be followed by a printf-style format spec &lt;code&gt;%[flags][width][.precision]conversion&lt;/code&gt;. Conversion characters: &lt;code&gt;d&lt;/code&gt; (integer), &lt;code&gt;f e g&lt;/code&gt; (real, fixed / scientific / shortest), &lt;code&gt;s&lt;/code&gt; (string), &lt;code&gt;x X o b&lt;/code&gt; (integer in hex / upper-hex / octal / binary). Flags: &lt;code&gt;-&lt;/code&gt; left-align, &lt;code&gt;0&lt;/code&gt; zero-pad. The spec follows immediately after the value with no space.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14159&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|$n%5d|&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// |   42|&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|$n%-5d|&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// |42   |&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|$n%05d|&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// |00042|&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;$pi%.3f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 3.142&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;$pi%10.3f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;//      3.142&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hex = $n%x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// hex = 2a&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;bin = $n%08b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// bin = 00101010&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inside an &lt;code&gt;f&amp;quot;...&amp;quot;&lt;/code&gt; literal, &lt;code&gt;%&lt;/code&gt; in plain text is literal (no &lt;code&gt;%%&lt;/code&gt; escape needed — &lt;code&gt;100% sure&lt;/code&gt; works). &lt;code&gt;$$&lt;/code&gt; still emits a literal &lt;code&gt;$&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The conversion-vs-value-type compatibility is checked at runtime: &lt;code&gt;f&amp;quot;$str%d&amp;quot;&lt;/code&gt; traps because &lt;code&gt;%d&lt;/code&gt; expects an integer. &lt;code&gt;%s&lt;/code&gt; accepts any value and uses the default printed form.&lt;/p&gt;
&lt;h2 id=&quot;10-7-type-conversions&quot;&gt;10.7 Type conversions&lt;/h2&gt;
&lt;p&gt;Explicit conversion functions (narrowing or non-promoting):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// truncates toward zero&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// traps if any element is outside 0..255&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;to_integers&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// widens; always safe&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pair &lt;code&gt;to_bytes&lt;/code&gt; / &lt;code&gt;to_integers&lt;/code&gt; is the bridge between &lt;code&gt;[byte]&lt;/code&gt; storage and the integer arithmetic surface — bytes are not first-class numbers and must be widened before any computation. See §3.3.1.&lt;/p&gt;
&lt;h2 id=&quot;10-9-the-byte-buffer&quot;&gt;10.9 The &lt;code&gt;[byte]&lt;/code&gt; buffer&lt;/h2&gt;
&lt;p&gt;Construction and length:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// zero-filled, length n&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// element count&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;File I/O — both routes route through the cross-platform runtime, so the same program reads and writes on JVM, JS, and native targets:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;read_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;write_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;unit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These two are the only file-I/O entry points the prelude offers in v0; the existing text-file helpers (&lt;code&gt;readFile&lt;/code&gt; / &lt;code&gt;writeFile&lt;/code&gt;) live in the host runtime and are not surfaced to Nex code yet.&lt;/p&gt;
&lt;h2 id=&quot;10-8-assertions&quot;&gt;10.8 Assertions&lt;/h2&gt;
&lt;p&gt;For use in test functions (see the Functions chapter) and test modules (see the Modules chapter):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_eq&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;actual&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;expected&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;actual&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;hl-variable&quot;&gt;expected&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;actual&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-variable&quot;&gt;expected&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;actual&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;hl-variable&quot;&gt;expected&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;actual&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;hl-variable&quot;&gt;expected&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_traps&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;thunk&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;assert_traps&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;thunk&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;expected_substring&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All assertions trap on failure with a message naming the assertion type and (where applicable) the user-supplied &lt;code&gt;msg&lt;/code&gt;. The test runner catches the trap and reports the failure without halting the rest of the test suite.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prefer &lt;code&gt;assert_approx&lt;/code&gt; over &lt;code&gt;assert_eq&lt;/code&gt; for &lt;code&gt;real&lt;/code&gt; and &lt;code&gt;complex&lt;/code&gt; values&lt;/strong&gt; — exact floating-point equality is almost always incorrect. The check is the absolute-distance predicate &lt;span class=&quot;math inline&quot;&gt;\(\lvert a - b \rvert \le \text{tol}\)&lt;/span&gt;, where the metric depends on the operand type: &lt;span class=&quot;math inline&quot;&gt;\(\lvert a - b \rvert\)&lt;/span&gt; for &lt;code&gt;real&lt;/code&gt;, and the Euclidean distance &lt;span class=&quot;math inline&quot;&gt;\(\lvert a - b \rvert = \sqrt{(a_r - b_r)^2 + (a_i - b_i)^2}\)&lt;/span&gt; for &lt;code&gt;complex&lt;/code&gt;. The array overloads run element-wise on rank-1 and rank-2 arrays of integer, real, or complex; integers lift to real, complex elements use the same Euclidean distance, and a shape mismatch (length for rank-1, rows or cols for rank-2) traps.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;assert_traps&lt;/code&gt; takes a zero-argument closure and passes if invoking it traps; it fails if the thunk returns normally. The 2-arg form additionally checks that the trap message contains &lt;code&gt;expected_substring&lt;/code&gt; — useful for asserting a specific failure mode rather than “any trap fires”.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Power Iteration</title>
    <link href="https://nexlang.org/examples/power-iteration/"/>
    <id>https://nexlang.org/examples/power-iteration/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Dominant eigenvalue and eigenvector of a square matrix via the `@` operator and `dot`.</summary>
    <content type="html">&lt;p&gt;Power iteration finds the eigenvector &lt;span class=&quot;math inline&quot;&gt;\(v_*\)&lt;/span&gt; associated with the eigenvalue of largest magnitude (the &lt;em&gt;dominant&lt;/em&gt; eigenvalue &lt;span class=&quot;math inline&quot;&gt;\(\lambda_*\)&lt;/span&gt;) of a square matrix &lt;span class=&quot;math inline&quot;&gt;\(A\)&lt;/span&gt;. Starting from an arbitrary nonzero &lt;span class=&quot;math inline&quot;&gt;\(v_0\)&lt;/span&gt;, repeated multiplication by &lt;span class=&quot;math inline&quot;&gt;\(A\)&lt;/span&gt; amplifies the component along &lt;span class=&quot;math inline&quot;&gt;\(v_*\)&lt;/span&gt; faster than every other component; renormalizing each step keeps the vector bounded:&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[v_{k+1} = \frac{A v_k}{\lVert A v_k \rVert_2}.\]&lt;/div&gt;
&lt;p&gt;Once &lt;span class=&quot;math inline&quot;&gt;\(v_k\)&lt;/span&gt; has converged in direction, the Rayleigh quotient recovers the eigenvalue:&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[\lambda \;=\; \frac{v^\top A v}{v^\top v}.\]&lt;/div&gt;
&lt;p&gt;The denominator is &lt;span class=&quot;math inline&quot;&gt;\(1\)&lt;/span&gt; here because we renormalize &lt;span class=&quot;math inline&quot;&gt;\(v\)&lt;/span&gt; each step, so the code below evaluates the numerator &lt;span class=&quot;math inline&quot;&gt;\(v^\top (A v)\)&lt;/span&gt; directly with &lt;code&gt;dot&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Power iteration: finds the dominant eigenvalue/eigenvector of a square matrix.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// Returns (eigenvalue, eigenvector).&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;power_iteration&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// initial guess — real-typed so subsequent&lt;/span&gt;
                                     &lt;span class=&quot;hl-comment&quot;&gt;// reassignments stay [real]; `ones(n)` would&lt;/span&gt;
                                     &lt;span class=&quot;hl-comment&quot;&gt;// give [integer] and the rebind would trip&lt;/span&gt;
                                     &lt;span class=&quot;hl-comment&quot;&gt;// the type check.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;Av&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// matrix-vector multiply (rank-1 result)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;norm&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;Av&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Av&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Av&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;norm&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// element-wise scalar division&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// Rayleigh quotient&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// Symmetric matrix with eigenvalues 5 and 1&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
           &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;power_iteration&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;dominant eigenvalue ~ &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;corresponding eigenvector ~ &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_power_iteration_finds_correct_eigenvalue&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;power_iteration&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Numerical Methods</title>
    <link href="https://nexlang.org/examples/numerical-methods/"/>
    <id>https://nexlang.org/examples/numerical-methods/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Newton&apos;s method for square roots, then one Runge-Kutta 4 step for a vector ODE.</summary>
    <content type="html">&lt;h2 id=&quot;newton-s-method-for-square-roots&quot;&gt;Newton’s method for square roots&lt;/h2&gt;
&lt;p&gt;Newton’s method for &lt;span class=&quot;math inline&quot;&gt;\(f(x) = 0\)&lt;/span&gt; refines a guess by&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[x_{n+1} \;=\; x_n - \frac{f(x_n)}{f&apos;(x_n)}.\]&lt;/div&gt;
&lt;p&gt;Applied to &lt;span class=&quot;math inline&quot;&gt;\(f(x) = x_n^2 - x\)&lt;/span&gt; (whose root in &lt;span class=&quot;math inline&quot;&gt;\(x_n\)&lt;/span&gt; is &lt;span class=&quot;math inline&quot;&gt;\(\sqrt{x}\)&lt;/span&gt;), the recurrence collapses to the classical Heron / Babylonian average&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[x_{n+1} \;=\; \tfrac{1}{2}\!\left(x_n + \tfrac{x}{x_n}\right),\]&lt;/div&gt;
&lt;p&gt;which converges quadratically — each step roughly doubles the number of correct digits.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;newton_sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;delta&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;tol&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;delta&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;next&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;guess&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;newton_sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;hl-number&quot;&gt;1.0e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 1.41421356...&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;newton_sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1000.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0e-9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// 31.6227766...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rk4-one-step-of-a-vector-ode-solver&quot;&gt;RK4 — one step of a vector ODE solver&lt;/h2&gt;
&lt;p&gt;For the vector ODE &lt;span class=&quot;math inline&quot;&gt;\(\dot y = f(t, y)\)&lt;/span&gt;, the classical Runge-Kutta-4 step from &lt;span class=&quot;math inline&quot;&gt;\((t_n, y_n)\)&lt;/span&gt; to &lt;span class=&quot;math inline&quot;&gt;\((t_{n+1}, y_{n+1}) = (t_n + h, \, y_{n+1})\)&lt;/span&gt; samples the slope at four points,&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[\begin{aligned}
k_1 &amp;amp;= f(t_n,           \; y_n)              \\
k_2 &amp;amp;= f(t_n + h/2,     \; y_n + (h/2)\, k_1) \\
k_3 &amp;amp;= f(t_n + h/2,     \; y_n + (h/2)\, k_2) \\
k_4 &amp;amp;= f(t_n + h,       \; y_n + h\, k_3),
\end{aligned}\]&lt;/div&gt;
&lt;p&gt;and combines them with Simpson-like weights,&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[y_{n+1} \;=\; y_n + \tfrac{h}{6}\,(k_1 + 2k_2 + 2k_3 + k_4).\]&lt;/div&gt;
&lt;p&gt;The method is fourth-order accurate: the local truncation error is &lt;span class=&quot;math inline&quot;&gt;\(O(h^5)\)&lt;/span&gt; and the global error over a fixed interval is &lt;span class=&quot;math inline&quot;&gt;\(O(h^4)\)&lt;/span&gt;. The example below applies it to the simple harmonic oscillator &lt;span class=&quot;math inline&quot;&gt;\(\ddot p = -p\)&lt;/span&gt;, written as the first-order system &lt;span class=&quot;math inline&quot;&gt;\(\dot p = v\)&lt;/span&gt;, &lt;span class=&quot;math inline&quot;&gt;\(\dot v = -p\)&lt;/span&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// One Runge-Kutta 4 step for dy/dt = f(t, y), where y is a vector.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;rk4_step&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k4&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// The whole expression fuses to a single loop:&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;k2 &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;k3 &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Simple harmonic oscillator: dy/dt = (velocity, -position)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;harmonic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// start: position 1, velocity 0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.01&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;steps&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1000&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;steps&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;rk4_step&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;harmonic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;after &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt; steps: position=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, velocity=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Modules</title>
    <link href="https://nexlang.org/spec/modules/"/>
    <id>https://nexlang.org/spec/modules/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Folder-as-module, declaration, imports, visibility, no .mod headers, test modules.</summary>
    <content type="html">&lt;h2 id=&quot;9-1-folder-as-module&quot;&gt;9.1 Folder-as-module&lt;/h2&gt;
&lt;p&gt;In Nex, &lt;strong&gt;a module is a directory&lt;/strong&gt;. All &lt;code&gt;.nex&lt;/code&gt; files within the directory belong to the same module. Subdirectories are submodules.&lt;/p&gt;
&lt;p&gt;Given a project root, the module path is the directory path relative to the root, with &lt;code&gt;/&lt;/code&gt; replaced by &lt;code&gt;.&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;linalg&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;        ← &lt;span class=&quot;hl-variable&quot;&gt;all&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;three&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;files&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;are&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;part&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;module&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;arith&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;       ← &lt;span class=&quot;hl-variable&quot;&gt;linalg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;dense&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;decompose&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;      ← &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;linalg.sparse&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;            ← &lt;span class=&quot;hl-variable&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Files in the same module share visibility: any binding in one file is visible to all other files in the same module, unless declared &lt;code&gt;private&lt;/code&gt; (see §9.4).&lt;/p&gt;
&lt;h2 id=&quot;9-2-module-declaration&quot;&gt;9.2 Module declaration&lt;/h2&gt;
&lt;p&gt;A file may optionally declare its module at the top:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;linalg.dense&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Matrix&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Matrix&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the declaration is present, it must match the directory path. If absent, the module is determined by the directory path alone. Declaration is recommended for clarity in large codebases but never required.&lt;/p&gt;
&lt;h2 id=&quot;9-3-imports&quot;&gt;9.3 Imports&lt;/h2&gt;
&lt;p&gt;Imports use the &lt;code&gt;import&lt;/code&gt; keyword. Selective imports list the names to import; &lt;code&gt;as&lt;/code&gt; introduces a local alias:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;}
&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;linalg.dense&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-type&quot;&gt;Matrix&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;lu_decompose&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;lu&lt;/span&gt;}
&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;io.console&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;con&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Whole-module imports (&lt;code&gt;import math&lt;/code&gt;) are &lt;em&gt;not&lt;/em&gt; allowed — selective import is required. This prevents namespace pollution and makes dependencies explicit.&lt;/p&gt;
&lt;p&gt;Imports must appear at the top of a file, after the optional &lt;code&gt;module&lt;/code&gt; declaration.&lt;/p&gt;
&lt;h2 id=&quot;9-4-visibility&quot;&gt;9.4 Visibility&lt;/h2&gt;
&lt;p&gt;The default visibility of all module-level declarations (functions, structs, constants) is &lt;strong&gt;public&lt;/strong&gt; — visible to importing modules.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;private&lt;/code&gt; keyword restricts a declaration to the declaring module (the whole folder, including sibling files):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;geometry&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// public&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;helper&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// private to geometry&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Private declarations are visible to all sibling files within the same module folder but invisible to importing modules.&lt;/p&gt;
&lt;h2 id=&quot;9-5-no-mod-binary-headers&quot;&gt;9.5 No &lt;code&gt;.mod&lt;/code&gt; binary headers&lt;/h2&gt;
&lt;p&gt;Nex does not use precompiled interface files (Fortran’s &lt;code&gt;.mod&lt;/code&gt; files, C++ modules’ BMIs, etc.). The build system compiles from source in topological order based on the import graph.&lt;/p&gt;
&lt;h2 id=&quot;9-6-test-modules&quot;&gt;9.6 Test modules&lt;/h2&gt;
&lt;p&gt;An entire module may be marked as test-only by placing &lt;code&gt;@test&lt;/code&gt; before the &lt;code&gt;module&lt;/code&gt; declaration. The module’s contents — both test functions and support code — are stripped from non-test builds.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats.deep_tests&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;}

&lt;span class=&quot;hl-comment&quot;&gt;// Support code — not a test, available to tests in this module.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_constant_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;constant_cases&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;make_constant_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;make_constant_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;make_constant_sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_mean_recovers_constants&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;constant_cases&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Test modules can import freely from regular modules but see only the public surface — &lt;code&gt;private&lt;/code&gt; members of the imported modules are &lt;em&gt;not&lt;/em&gt; visible. If a test needs an internal helper, place the &lt;code&gt;@test&lt;/code&gt; function in the same module as the code (see the Functions chapter) rather than in a separate test module.&lt;/p&gt;
&lt;p&gt;A test module may itself be a folder containing multiple files; each file declares &lt;code&gt;@test module foo.bar.tests&lt;/code&gt; at the top.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Multi-File Modules</title>
    <link href="https://nexlang.org/examples/modules/"/>
    <id>https://nexlang.org/examples/modules/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Descriptive statistics across three files in one module folder, with inline tests that see private helpers.</summary>
    <content type="html">&lt;p&gt;Folder layout:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;          ← &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;distribution&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;nex&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/stats/moments.nex&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;central_moment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Private to the `stats` module; other files in stats/ can call it&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// but importers cannot. The explicit `: real` return type lets sibling&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// callers like `variance` see a concrete element type even when the&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// body&apos;s last expression composes generic prelude HOFs.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;central_moment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Inline tests sit next to the code they exercise.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// They see private members because they&apos;re in the same module.&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_mean_of_constants&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_stddev_of_constants_is_zero&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_central_moment_visible_to_test_in_module&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// central_moment is private — only visible because we&apos;re in `stats`&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;central_moment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/stats/distribution.nex&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;skewness&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;central_moment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;kurtosis&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;central_moment&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/stats/summary.nex&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Summary&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;mean_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;stddev_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;min_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;max_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;summarize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Summary&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;src/main.nex&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;{&lt;span class=&quot;hl-type&quot;&gt;Summary&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;summarize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;skewness&lt;/span&gt;}

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;summarize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;n=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;  mean=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;mean_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;  sd=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;stddev_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;range=[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;min_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;max_val&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;skewness=&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;skewness&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Mode System and Memory Management</title>
    <link href="https://nexlang.org/spec/memory/"/>
    <id>https://nexlang.org/spec/memory/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>The two array regimes, uniqueness for var arrays, auto-clone, read-mode parameters, ARC, future RAII.</summary>
    <content type="html">&lt;p&gt;This chapter describes the design that makes Nex’s array semantics safe and analyzable. Most of it is invisible to users in everyday code — the compiler handles the bookkeeping. This chapter describes what the compiler is doing on the user’s behalf.&lt;/p&gt;
&lt;h2 id=&quot;8-1-the-two-array-regimes&quot;&gt;8.1 The two array regimes&lt;/h2&gt;
&lt;p&gt;Arrays in Nex fall into two regimes based on the binding kind that owns them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;val&lt;/code&gt; arrays:&lt;/strong&gt; immutable contents, freely shareable. Multiple bindings may refer to the same underlying buffer because no mutation is possible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;var&lt;/code&gt; arrays:&lt;/strong&gt; mutable contents, uniquely owned. Only one binding may refer to a given buffer at any program point. Aliasing is prohibited by the type system.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This split is the foundation of Nex’s fusion guarantees: the compiler can rewrite array expressions without proving non-aliasing analyses because the type system has already ensured no aliasing exists for mutable arrays.&lt;/p&gt;
&lt;h2 id=&quot;8-2-uniqueness-for-var-arrays&quot;&gt;8.2 Uniqueness for &lt;code&gt;var&lt;/code&gt; arrays&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;var&lt;/code&gt; binding to an array is a &lt;strong&gt;unique owner&lt;/strong&gt;. The compiler enforces:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;After a &lt;code&gt;var&lt;/code&gt; binding’s value is moved (passed to a &lt;code&gt;mut&lt;/code&gt; parameter, returned, or assigned to another &lt;code&gt;var&lt;/code&gt; binding), the original binding is invalid and may not be used.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;var&lt;/code&gt; binding may not be assigned the same array as another live &lt;code&gt;var&lt;/code&gt; binding.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These rules ensure that for any &lt;code&gt;var&lt;/code&gt; array, there is exactly one live binding referring to it at every program point.&lt;/p&gt;
&lt;h2 id=&quot;8-3-auto-clone-insertion&quot;&gt;8.3 Auto-clone insertion&lt;/h2&gt;
&lt;p&gt;The compiler &lt;strong&gt;automatically inserts clones&lt;/strong&gt; wherever needed to satisfy the uniqueness rule. Users do not write &lt;code&gt;.clone()&lt;/code&gt; in normal code. The clone insertion follows these rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If a &lt;code&gt;var&lt;/code&gt; array is used after its value has been moved, the compiler inserts a clone of the original buffer at the point of the move so the later use sees an independent buffer.&lt;/li&gt;
&lt;li&gt;If a &lt;code&gt;val&lt;/code&gt; array is passed to a &lt;code&gt;mut&lt;/code&gt; parameter, the compiler inserts a clone so the function receives a fresh mutable buffer.&lt;/li&gt;
&lt;li&gt;The clone-placement optimization pass picks the optimal point for each clone (e.g., clones the &lt;em&gt;earlier&lt;/em&gt; use so the &lt;em&gt;later&lt;/em&gt; use can be a move), and elides clones that are provably unnecessary.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A planned &lt;code&gt;@strict&lt;/code&gt; function attribute will disable auto-clone insertion within a function body — every clone must then be written explicitly as &lt;code&gt;.clone()&lt;/code&gt;, and missing clones become compile errors. This mode is intended for library authors and performance-critical code where every allocation must be visible. The attribute is reserved at the parser level today; the enforcement pass is &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;8-4-read-mode-parameters-need-no-clones&quot;&gt;8.4 Read-mode parameters need no clones&lt;/h2&gt;
&lt;p&gt;Because read-mode parameters (the default) do not mutate or take ownership of their argument, calls to read-mode functions never require clones — even when the caller passes a &lt;code&gt;var&lt;/code&gt; array:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// read mode&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// ok: read mode, a still valid&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// ok: a still valid&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// ok: a is mutable, never moved&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;8-5-memory-management-arc&quot;&gt;8.5 Memory management: ARC&lt;/h2&gt;
&lt;p&gt;The current implementation uses &lt;strong&gt;automatic reference counting (ARC)&lt;/strong&gt; for all heap-allocated values (arrays, strings, structs containing those). Each heap-allocated value carries a reference count; increments occur when ownership is shared, decrements occur when a reference goes out of scope, and the value is freed when the count reaches zero.&lt;/p&gt;
&lt;p&gt;ARC requires no garbage collector runtime, has no pause-style overhead, and is straightforward to implement. The per-operation cost (atomic increments and decrements) is acceptable for current workloads; performance-critical paths can be revisited later.&lt;/p&gt;
&lt;h2 id=&quot;8-6-future-raii-for-var-arrays&quot;&gt;8.6 Future: RAII for &lt;code&gt;var&lt;/code&gt; arrays&lt;/h2&gt;
&lt;p&gt;The memory management for &lt;code&gt;var&lt;/code&gt; arrays will be upgraded from ARC to &lt;strong&gt;RAII&lt;/strong&gt; (deterministic deallocation at scope exit). Because uniqueness guarantees a single owner, RAII can free a &lt;code&gt;var&lt;/code&gt; array immediately when its binding goes out of scope, with no refcount overhead. This change is &lt;em&gt;invisible to user code&lt;/em&gt; — only the runtime characteristic changes. &lt;code&gt;val&lt;/code&gt; arrays continue to use ARC.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Mandelbrot Set</title>
    <link href="https://nexlang.org/examples/mandelbrot/"/>
    <id>https://nexlang.org/examples/mandelbrot/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Rank-2 matrix plus complex iteration — escape-time rendered as ASCII art. Row-vector slice assignment writes a whole row of the image with one statement.</summary>
    <content type="html">&lt;p&gt;For each complex parameter &lt;span class=&quot;math inline&quot;&gt;\(c\)&lt;/span&gt;, the Mandelbrot set studies the iteration&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[z_0 = 0, \qquad z_{n+1} = z_n^2 + c.\]&lt;/div&gt;
&lt;p&gt;The point &lt;span class=&quot;math inline&quot;&gt;\(c\)&lt;/span&gt; belongs to the set if the sequence &lt;span class=&quot;math inline&quot;&gt;\(\{z_n\}\)&lt;/span&gt; stays bounded; otherwise it escapes to infinity. A simple sufficient escape test is &lt;span class=&quot;math inline&quot;&gt;\(\lvert z_n \rvert &amp;gt; 2\)&lt;/span&gt; — once the orbit leaves the disk of radius 2, it never returns. The renderer records the first &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; at which that happens (the &lt;em&gt;escape time&lt;/em&gt;) and maps it to an ASCII gradient; pixels that never escape after &lt;code&gt;max_iter&lt;/code&gt; are drawn solid.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;escape_iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;i
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Compute one row of escape-times as a rank-1 vector. The whole row&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// is built independently of the result matrix and then written into&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// it via slice assignment — `result[py, :] = row` — in one statement.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;row_at&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;escape_iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;row&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;mandelbrot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-comment&quot;&gt;// Slice assignment replaces the entire py-th row of the image in&lt;/span&gt;
    &lt;span class=&quot;hl-comment&quot;&gt;// place. No per-pixel writes to `result`, no temporary copy.&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;row_at&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;max_iter&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mandelbrot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-comment&quot;&gt;// `print` always appends a newline, so we assemble one row of&lt;/span&gt;
    &lt;span class=&quot;hl-comment&quot;&gt;// pixels as a string and print it once per row.&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;hl-keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;hl-keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ch&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two slice idioms in one program:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Whole-row write&lt;/strong&gt; — &lt;code&gt;result[py, :] = row&lt;/code&gt; replaces the entire py-th row of &lt;code&gt;result&lt;/code&gt; in place, with no per-pixel &lt;code&gt;result[py, px] = ...&lt;/code&gt; writes and no temporary copy of the row buffer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-row read&lt;/strong&gt; — &lt;code&gt;img[py]&lt;/code&gt; (single integer on a rank-2) reads the py-th row as a freshly-owned rank-1, used implicitly above via the explicit &lt;code&gt;img[py, px]&lt;/code&gt; element access. You could also lift that with &lt;code&gt;val row = img[py]; for px in ... do ... row[px]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;</content>
  </entry>
  <entry>
    <title>Lexical Syntax</title>
    <link href="https://nexlang.org/spec/lexical/"/>
    <id>https://nexlang.org/spec/lexical/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Source encoding, identifiers, keywords, literals, comments, indentation, and statement separators.</summary>
    <content type="html">&lt;h2 id=&quot;2-1-source-encoding&quot;&gt;2.1 Source encoding&lt;/h2&gt;
&lt;p&gt;Source files are UTF-8 encoded. Files use the &lt;code&gt;.nex&lt;/code&gt; extension. Literate sources (Markdown wrappers; see &lt;a href=&quot;/#29-literate-nex&quot;&gt;§2.9&lt;/a&gt;) use &lt;code&gt;.lnex&lt;/code&gt; and are transparently preprocessed before lexing — every rule below applies to the dedented code inside a &lt;code&gt;.lnex&lt;/code&gt; file just as it does to a &lt;code&gt;.nex&lt;/code&gt; file.&lt;/p&gt;
&lt;h2 id=&quot;2-2-identifiers&quot;&gt;2.2 Identifiers&lt;/h2&gt;
&lt;p&gt;Identifiers begin with an ASCII letter or underscore and may contain ASCII letters, digits, and underscores:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;identifier  ::=  (letter | &apos;_&apos;) (letter | digit | &apos;_&apos;)*
letter      ::=  &apos;a&apos;..&apos;z&apos; | &apos;A&apos;..&apos;Z&apos;
digit       ::=  &apos;0&apos;..&apos;9&apos;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unicode identifiers are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;2-3-keywords&quot;&gt;2.3 Keywords&lt;/h2&gt;
&lt;p&gt;Reserved words (cannot be used as identifiers):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;as&lt;/span&gt;        &lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt;     &lt;span class=&quot;hl-variable&quot;&gt;def&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;div&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;      &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;       &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt;        &lt;span class=&quot;hl-variable&quot;&gt;import&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt;        &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;module&lt;/span&gt;    &lt;span class=&quot;hl-function&quot;&gt;mut&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;not&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;or&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;private&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt;    &lt;span class=&quot;hl-type&quot;&gt;then&lt;/span&gt;      &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;      &lt;span class=&quot;hl-variable&quot;&gt;val&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt;       &lt;span class=&quot;hl-variable&quot;&gt;while&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Words reserved for future use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class     extends   given     implicit  package
pub       trait     type      where     yield
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-4-literals&quot;&gt;2.4 Literals&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Boolean literals:&lt;/strong&gt; &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Integer literals:&lt;/strong&gt; decimal, hexadecimal (&lt;code&gt;0x&lt;/code&gt; prefix), binary (&lt;code&gt;0b&lt;/code&gt; prefix), or octal (&lt;code&gt;0o&lt;/code&gt; prefix). Underscores allowed as digit separators.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1_000_000&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;0xFF&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;0b1010_1010&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;0o755&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Integer literals have type &lt;code&gt;integer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Real literals:&lt;/strong&gt; decimal with required fractional part or exponent.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2.5e-3&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;6.022e23&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;1_234.567_89&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Real literals have type &lt;code&gt;real&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Complex values&lt;/strong&gt; are constructed from real values using the prelude constant &lt;code&gt;i&lt;/code&gt; (the imaginary unit, equal to &lt;code&gt;(0.0, 1.0)&lt;/code&gt;) together with arithmetic operators. Combined with juxtaposition multiplication (chapter 4), this produces math-flavored notation without a dedicated complex-literal form:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// imaginary unit: (0.0 + 1.0i)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;i                        &lt;span class=&quot;hl-comment&quot;&gt;// (0.0 + 2.0i)  — juxtaposition: 2.0 * i&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.5&lt;/span&gt;i                  &lt;span class=&quot;hl-comment&quot;&gt;// (3.0 + 4.5i)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// (-1.0 + 0.0i) — Euler&apos;s identity&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Complex values have type &lt;code&gt;complex&lt;/code&gt; (chapter 3). Nex does not provide a dedicated complex-literal token — &lt;code&gt;2i&lt;/code&gt;, &lt;code&gt;3 + 4i&lt;/code&gt;, etc. are ordinary expressions parsed under the rules in chapter 4.&lt;/p&gt;
&lt;p&gt;(The prelude name &lt;code&gt;i&lt;/code&gt; shadows freely: a &lt;code&gt;for i in 0..n&lt;/code&gt; loop variable hides the imaginary unit within the loop body. If you need both in the same scope, pick a different index name — by mathematical convention, &lt;code&gt;j&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, &lt;code&gt;m&lt;/code&gt;, or &lt;code&gt;n&lt;/code&gt;.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;String literals:&lt;/strong&gt; double-quoted, with backslash escapes (&lt;code&gt;\n&lt;/code&gt;, &lt;code&gt;\t&lt;/code&gt;, &lt;code&gt;\r&lt;/code&gt;, &lt;code&gt;\\&lt;/code&gt;, &lt;code&gt;\&amp;quot;&lt;/code&gt;, &lt;code&gt;\x41&lt;/code&gt;, &lt;code&gt;\u{1F600}&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello, world&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;line one&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;line two&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;String literals have type &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Interpolated string literals:&lt;/strong&gt; prefixed with &lt;code&gt;s&lt;/code&gt;. Values are spliced into the string at marker positions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hello, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// identifier substitution&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;x + y = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// arbitrary expression in ${}&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|z| = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// method/field access in ${}&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;first = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// indexing in ${}&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;price: &lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// literal $ via doubling&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;$identifier&lt;/code&gt; substitutes the value of the named binding.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;${expression}&lt;/code&gt; substitutes the result of an arbitrary expression.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$$&lt;/code&gt; produces a literal &lt;code&gt;$&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The form &lt;code&gt;$ident.field&lt;/code&gt; parses as &lt;code&gt;${ident}.field&lt;/code&gt; — only the bare identifier is interpolated, and &lt;code&gt;.field&lt;/code&gt; is literal text. To interpolate a field access, write &lt;code&gt;${ident.field}&lt;/code&gt;. The same rule applies to &lt;code&gt;[…]&lt;/code&gt; and &lt;code&gt;(…)&lt;/code&gt; following an interpolated identifier.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each interpolated value is converted to its string form (the same conversion &lt;code&gt;print&lt;/code&gt; uses). Interpolated strings have type &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Format-string interpolation with precision specifiers (&lt;code&gt;f&amp;quot;x = $x%.3f&amp;quot;&lt;/code&gt;) is &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Unit literal:&lt;/strong&gt; &lt;code&gt;()&lt;/code&gt;. The single value of type &lt;code&gt;unit&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;2-5-comments&quot;&gt;2.5 Comments&lt;/h2&gt;
&lt;p&gt;Line comments begin with &lt;code&gt;//&lt;/code&gt; and extend to the end of the line. Block comments are &lt;code&gt;/* ... */&lt;/code&gt; and may nest.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// this is a line comment&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;/* this is&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;   a block comment */&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;/* block &lt;/span&gt;&lt;span class=&quot;hl-comment&quot;&gt;/* nested */&lt;/span&gt;&lt;span class=&quot;hl-comment&quot;&gt; comments work */&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-6-indentation-and-structure&quot;&gt;2.6 Indentation and structure&lt;/h2&gt;
&lt;p&gt;Nex uses indentation-based syntax in the Scala 3 family. Indentation introduces a block; dedentation closes it. Blocks are introduced after &lt;code&gt;=&lt;/code&gt; (in &lt;code&gt;def&lt;/code&gt; and &lt;code&gt;val&lt;/code&gt;/&lt;code&gt;var&lt;/code&gt; bindings), after &lt;code&gt;then&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt; (in &lt;code&gt;if&lt;/code&gt;), after &lt;code&gt;do&lt;/code&gt; (in &lt;code&gt;for&lt;/code&gt; / &lt;code&gt;while&lt;/code&gt;), and after &lt;code&gt;-&amp;gt;&lt;/code&gt; in block-form lambdas.&lt;/p&gt;
&lt;p&gt;Indentation may use spaces or tabs but must be consistent within a file. The recommended convention is two spaces.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-7-end-markers&quot;&gt;2.7 End markers&lt;/h2&gt;
&lt;p&gt;Any indented block may be terminated by an optional &lt;code&gt;end&lt;/code&gt; marker for clarity. The marker may be bare (&lt;code&gt;end&lt;/code&gt;) or named with the construct it closes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;def&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;End markers are particularly useful for long blocks where the closing dedent would otherwise be far from the opening keyword. They are never required and never alter semantics.&lt;/p&gt;
&lt;h2 id=&quot;2-8-statement-separators&quot;&gt;2.8 Statement separators&lt;/h2&gt;
&lt;p&gt;A statement ends at the end of a line, unless the line ends with an operator or open delimiter, in which case the statement continues on the next (more-indented) line. Multiple statements on one line may be separated by &lt;code&gt;;&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;long_expression&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;some_function&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;another_function&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;yet_another&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-9-literate-nex&quot;&gt;2.9 Literate Nex&lt;/h2&gt;
&lt;p&gt;A file with the &lt;code&gt;.lnex&lt;/code&gt; extension is a literate source: Markdown prose interleaved with indented code blocks. The compiler preprocesses the file before lexing, so the rest of the language sees ordinary Nex source.&lt;/p&gt;
&lt;p&gt;The preprocessing rules are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prose lines&lt;/strong&gt; start at column 0 and are stripped — replaced by blank lines so that error positions still refer to the original &lt;code&gt;.lnex&lt;/code&gt; line numbers. Markdown headings, paragraphs, lists, links, inline math (&lt;code&gt;$..$&lt;/code&gt;), and display math (&lt;code&gt;$$..$$&lt;/code&gt;) all fall under this rule.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code lines&lt;/strong&gt; start with a tab or at least four spaces. Exactly one indentation level is stripped (the leading tab, or four leading spaces) and the dedented body is fed to the lexer. Any further indentation survives — Nex’s own indent-sensitive blocks work normally inside a literate file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fenced blocks&lt;/strong&gt; (&lt;code&gt;```&lt;/code&gt;) are non-Nex content (ASCII diagrams, sample output, hex dumps) and are stripped entirely. The fence delimiters and everything between them become blank lines.&lt;/li&gt;
&lt;li&gt;Lines with 1-3 leading spaces are treated as prose (Markdown list-item continuations).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-markdown&quot;&gt;# Greeting

This file demonstrates literate Nex. The compiler ignores the prose
and reads only the indented blocks below.

    def main() =
        val name = &amp;quot;world&amp;quot;
        print(name)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A module may freely mix &lt;code&gt;.nex&lt;/code&gt; and &lt;code&gt;.lnex&lt;/code&gt; files; the directory’s contents are taken as the union, and the rules of &lt;a href=&quot;/spec/09-modules/&quot;&gt;§9&lt;/a&gt; apply unchanged.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Introduction</title>
    <link href="https://nexlang.org/spec/introduction/"/>
    <id>https://nexlang.org/spec/introduction/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>What Nex is, who it&apos;s for, what&apos;s in scope and what&apos;s deferred.</summary>
    <content type="html">&lt;p&gt;&lt;strong&gt;Implementation language:&lt;/strong&gt; Scala 3.
&lt;strong&gt;Target:&lt;/strong&gt; AOT compilation via LLVM IR text → &lt;code&gt;clang -O1&lt;/code&gt; (Mac arm64 verified end-to-end on every commit). A reference tree-walking interpreter runs alongside; the two paths are required to produce byte-for-byte identical output (modulo a documented &lt;code&gt;%g&lt;/code&gt; 6-significant-figure divergence on irrational reals).&lt;/p&gt;
&lt;h2 id=&quot;1-1-what-nex-is&quot;&gt;1.1 What Nex is&lt;/h2&gt;
&lt;p&gt;Nex is a statically-typed, expression-oriented, array-first numerical programming language. It is designed for the kind of code that Fortran is still used for today: numerical simulation, scientific computing, computational mathematics, signal processing — workloads where array operations dominate, IEEE 754 behavior matters, and AOT-compiled performance is non-negotiable.&lt;/p&gt;
&lt;p&gt;The language combines:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Array-first semantics&lt;/strong&gt; with element-wise operations as the default&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value semantics&lt;/strong&gt; throughout — no shared mutable state, no object identity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Uniqueness types&lt;/strong&gt; for mutable arrays, inferred and managed by the compiler&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fusion-aware compilation&lt;/strong&gt; — array expressions compile to fused loops with no temporaries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modern syntax&lt;/strong&gt; in the Scala 3 family: indentation-based, expression-oriented, optional end markers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built-in complex numbers&lt;/strong&gt;, in the Fortran tradition&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;1-2-audience&quot;&gt;1.2 Audience&lt;/h2&gt;
&lt;p&gt;The primary audience is the Fortran-using community: HPC, computational science, numerical libraries. Secondary audiences are anyone who currently reaches for Julia or NumPy for numerical work and would prefer an AOT-compiled, statically-typed alternative with cleaner syntax.&lt;/p&gt;
&lt;h2 id=&quot;1-3-goals-and-non-goals&quot;&gt;1.3 Goals and non-goals&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Goals:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Mathematical clarity in source code&lt;/li&gt;
&lt;li&gt;Predictable, AOT-compiled performance&lt;/li&gt;
&lt;li&gt;Fusion of array operations as a semantic guarantee, not an optimization&lt;/li&gt;
&lt;li&gt;Strong static typing without ceremony&lt;/li&gt;
&lt;li&gt;Clean module system that supports growing codebases&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Non-goals:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Web programming, systems programming, application programming&lt;/li&gt;
&lt;li&gt;Object-oriented design (no classes, no inheritance, no methods in the OO sense)&lt;/li&gt;
&lt;li&gt;General-purpose dynamic dispatch&lt;/li&gt;
&lt;li&gt;Pervasive metaprogramming&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;1-4-scope&quot;&gt;1.4 Scope&lt;/h2&gt;
&lt;p&gt;The language as currently specified is the minimum coherent slice that can run real numerical programs. It includes: scalar types, rank-1 and rank-2 arrays (vectors and matrices) with slice assignment, expression-oriented control flow, multi-file folder modules, the mode system, ARC-based memory management, a source-form standard prelude (the scalar transcendentals and their complex extensions live in &lt;code&gt;prelude/*.nex&lt;/code&gt; and are auto-imported), built-in unit testing, and both a reference interpreter and an AOT compiler.&lt;/p&gt;
&lt;p&gt;Excluded for now: user-defined generics, sized numeric variants (&lt;code&gt;real32&lt;/code&gt;, &lt;code&gt;integer32&lt;/code&gt;, etc.), rank-3+ arrays / a shape language, structs with methods, sum types / pattern matching, operator definitions, modules with submodule interface/implementation split, FFI, parallel-for, and a package manager. See chapter 11 for the full deferred list.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Install</title>
    <link href="https://nexlang.org/getting-started/install/"/>
    <id>https://nexlang.org/getting-started/install/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Prerequisites and how to get the toolchain.</summary>
    <content type="html">&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JVM 17 or newer.&lt;/strong&gt; Needed to run the CLI itself. Any distribution works (Temurin, GraalVM, Zulu, Oracle).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;sbt 1.9 or newer.&lt;/strong&gt; Used as the build and runner front-end. Install via &lt;a href=&quot;https://sdkman.io/&quot;&gt;SDKMAN&lt;/a&gt; (&lt;code&gt;sdk install sbt&lt;/code&gt;) or &lt;a href=&quot;https://brew.sh/&quot;&gt;Homebrew&lt;/a&gt; (&lt;code&gt;brew install sbt&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;clang on &lt;code&gt;$PATH&lt;/code&gt;.&lt;/strong&gt; Required only by &lt;code&gt;nex compile&lt;/code&gt; (the AOT path). The interpreter (&lt;code&gt;nex run&lt;/code&gt;, &lt;code&gt;nex test&lt;/code&gt;) doesn’t need it. macOS ships clang via the Xcode Command Line Tools (&lt;code&gt;xcode-select --install&lt;/code&gt;); Linux distros bundle clang in their package manager (&lt;code&gt;apt install clang&lt;/code&gt;, &lt;code&gt;dnf install clang&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Verify each is installed:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;java   -version   # 17+
sbt    --version  # 1.9+
clang  --version  # any recent
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;get-the-source&quot;&gt;Get the source&lt;/h2&gt;
&lt;p&gt;There is no published binary yet. Clone the repository and build from source:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;git clone https://github.com/edadma/nex.git
cd nex
sbt nexJVM/compile     # first build pulls dependencies; subsequent runs are fast
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first &lt;code&gt;sbt&lt;/code&gt; invocation downloads dependencies (several minutes). After that the build is incremental and quick.&lt;/p&gt;
&lt;h2 id=&quot;supported-targets&quot;&gt;Supported targets&lt;/h2&gt;
&lt;p&gt;The AOT compile path is verified end-to-end on &lt;strong&gt;Mac arm64&lt;/strong&gt;. Linux x86_64 and Linux aarch64 should work — the codegen emits portable LLVM IR and shells out to whatever clang finds — but neither is exercised on every commit. Report any platform-specific issues.&lt;/p&gt;
&lt;p&gt;The interpreter (&lt;code&gt;nex run&lt;/code&gt;, &lt;code&gt;nex test&lt;/code&gt;) runs anywhere the JVM does, including Windows. The same code path also builds for Scala.js (Node) and Scala Native — every commit runs the cross-platform unit-test suite on all three targets — so embedding the interpreter in a browser playground or a static binary is straightforward when the need arises.&lt;/p&gt;
&lt;h2 id=&quot;next-step&quot;&gt;Next step&lt;/h2&gt;
&lt;p&gt;Once the build succeeds: &lt;a href=&quot;/getting-started/02-first-program/&quot;&gt;run your first program&lt;/a&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Writing a PPM image with `[byte]`</title>
    <link href="https://nexlang.org/examples/image/"/>
    <id>https://nexlang.org/examples/image/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Build a small gradient image as a `[byte]` buffer in P6 (binary PPM) format and dump it to disk with `write_bytes`.</summary>
    <content type="html">&lt;p&gt;The &lt;code&gt;[byte]&lt;/code&gt; type is Nex’s storage form for binary file I/O. To show it end to end, we build a tiny 64 × 32 image of a smooth red→blue gradient, pack it into the &lt;em&gt;P6&lt;/em&gt; binary PPM container, and write the result to disk. PPM is convenient here because the on-disk layout is essentially one ASCII header followed by raw RGB triples — exactly the shape &lt;code&gt;[byte]&lt;/code&gt; is for.&lt;/p&gt;
&lt;p&gt;A P6 file is&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;P6
&amp;lt;width&amp;gt; &amp;lt;height&amp;gt;
255
&amp;lt;width * height * 3 bytes of RGB pixel data&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with a single newline after each line of the header and &lt;strong&gt;no&lt;/strong&gt; terminator between header and pixel block.&lt;/p&gt;
&lt;p&gt;For a fixed 64 × 32 image the header is exactly 13 bytes: &lt;code&gt;P6\n64 32\n255\n&lt;/code&gt;. We build it as a literal &lt;code&gt;[byte]&lt;/code&gt; and concatenate the pixel block onto it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;64&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;32&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// P6\n 6 4   3 2 \n 2 5 5 \n  — 13 ASCII bytes.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;header_ints&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;header_len&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;header_ints&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pixel_count&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;header_len&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pixel_count&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;header_len&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;header_ints&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// Horizontal red→blue gradient with a fixed green channel. The&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// per-pixel index in the buffer is header_len + (y * w + x) * 3.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;header_len&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;
      &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;255.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// red&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;32&lt;/span&gt;                              &lt;span class=&quot;hl-comment&quot;&gt;// green&lt;/span&gt;
      &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;base&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;255.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// blue&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;write_bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;gradient.ppm&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;wrote &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt; bytes&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The program prints &lt;code&gt;wrote 6157 bytes&lt;/code&gt; (header + 64 × 32 × 3 pixels) and leaves a real &lt;code&gt;gradient.ppm&lt;/code&gt; on disk that any PPM viewer can open. The same source runs through &lt;code&gt;nex run&lt;/code&gt; on the interpreter on JVM, Node, and native, and through &lt;code&gt;nex compile&lt;/code&gt; on the LLVM AOT backend — &lt;code&gt;write_bytes&lt;/code&gt; routes through the cross-platform runtime so the file appears in the same place on every backend, and the &lt;code&gt;[byte]&lt;/code&gt; descriptor + ARC helpers in the AOT runtime produce a byte-identical PPM file to the interpreter’s.&lt;/p&gt;
&lt;p&gt;A real image-processing pipeline would normally widen the byte buffer to &lt;code&gt;[integer]&lt;/code&gt; (or to &lt;code&gt;[real]&lt;/code&gt; for HDR / sRGB / linear-light math) with &lt;code&gt;to_integers&lt;/code&gt;, do the computation, and narrow back to &lt;code&gt;[byte]&lt;/code&gt; with &lt;code&gt;to_bytes&lt;/code&gt; before writing. &lt;code&gt;[byte]&lt;/code&gt; exists for the storage layer; it deliberately does not participate in element-wise arithmetic or fusion. See the Specification, §3.3.1.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Higher-Order Functions and Closures</title>
    <link href="https://nexlang.org/examples/higher-order/"/>
    <id>https://nexlang.org/examples/higher-order/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>A closure captures `mean`, then `.map` fuses into the `.sum` reduction.</summary>
    <content type="html">&lt;p&gt;For a sample &lt;span class=&quot;math inline&quot;&gt;\(x_0, \ldots, x_{n-1}\)&lt;/span&gt;, the mean and the population standard deviation are&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[\bar{x} \;=\; \frac{1}{n}\sum_{i=0}^{n-1} x_i, \qquad
\sigma   \;=\; \sqrt{\,\frac{1}{n}\sum_{i=0}^{n-1} (x_i - \bar{x})^2\,}.\]&lt;/div&gt;
&lt;p&gt;The Nex realization reads as one expression per formula. The lambda inside &lt;code&gt;.map&lt;/code&gt; captures &lt;code&gt;mean&lt;/code&gt; from the enclosing scope — that’s the closure — and the intermediate array &lt;code&gt;.map&lt;/code&gt; would otherwise produce fuses into the &lt;code&gt;.sum&lt;/code&gt; reduction, so the whole variance computation runs in a single pass over &lt;code&gt;xs&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// The explicit `: real` annotation pins the binding&apos;s type — the&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// elaborator does not yet infer return types through `.map(lambda).sum()`&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// chains, so the downstream `sqrt(variance)` overload-resolution needs&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// the hint.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;variance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;mean = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, stddev = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Hello, world</title>
    <link href="https://nexlang.org/examples/hello/"/>
    <id>https://nexlang.org/examples/hello/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>The first program — print to stdout — followed by a function and a calculation.</summary>
    <content type="html">&lt;h2 id=&quot;hello-world&quot;&gt;Hello, world&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Hello, Nex!&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;a-function-and-a-calculation&quot;&gt;A function and a calculation&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;hypotenuse&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hypotenuse(3, 4) = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;hypotenuse&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Functions</title>
    <link href="https://nexlang.org/spec/functions/"/>
    <id>https://nexlang.org/spec/functions/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Declarations, return types, parameters, modes, closures, higher-order functions, recursion, early return, test functions.</summary>
    <content type="html">&lt;h2 id=&quot;6-1-function-declarations&quot;&gt;6.1 Function declarations&lt;/h2&gt;
&lt;p&gt;A function is declared with &lt;code&gt;def&lt;/code&gt;. The simplest form is single-expression:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;hypotenuse&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The block form uses an indented body after &lt;code&gt;=&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;6-2-return-types&quot;&gt;6.2 Return types&lt;/h2&gt;
&lt;p&gt;Return types are inferred by default. An explicit return type may be given after the parameter list:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Explicit return types are required for recursive functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;6-3-parameters&quot;&gt;6.3 Parameters&lt;/h2&gt;
&lt;p&gt;Each parameter is declared with &lt;code&gt;name: type&lt;/code&gt;. An optional &lt;code&gt;= default&lt;/code&gt; clause supplies a value to use when the caller omits the argument (see §6.5).&lt;/p&gt;
&lt;h2 id=&quot;6-4-parameter-modes&quot;&gt;6.4 Parameter modes&lt;/h2&gt;
&lt;p&gt;Function parameters that hold arrays (or struct types containing arrays) have a &lt;strong&gt;mode&lt;/strong&gt; that determines how the parameter is passed and what the function may do with it.&lt;/p&gt;
&lt;p&gt;Two modes exist:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;read mode&lt;/strong&gt; (default, inferred): the function reads the parameter; it does not mutate, take ownership of, or store it. Read mode is inferred from the function body — if the body never mutates the parameter, never passes it to a &lt;code&gt;mut&lt;/code&gt; parameter elsewhere, and never returns or stores it in a &lt;code&gt;var&lt;/code&gt; binding, the parameter is read mode.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;mut mode&lt;/strong&gt; (declared explicitly with &lt;code&gt;mut&lt;/code&gt;): the function takes ownership of the parameter and may mutate it. Required if the body does any of the actions ruled out by read mode.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// read mode (inferred)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// mut mode (declared)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Mode mismatches at call sites:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Passing a &lt;code&gt;val&lt;/code&gt; array to a &lt;code&gt;mut&lt;/code&gt; parameter requires a clone (compiler inserts it automatically; see chapter 8).&lt;/li&gt;
&lt;li&gt;Passing a &lt;code&gt;var&lt;/code&gt; array to a &lt;code&gt;mut&lt;/code&gt; parameter moves ownership — the caller cannot use the array after the call unless the function returns it.&lt;/li&gt;
&lt;li&gt;Passing any array to a &lt;code&gt;read&lt;/code&gt; parameter never moves or mutates it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Modes apply only to array and struct types. Scalar parameters (&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;complex&lt;/code&gt;, &lt;code&gt;unit&lt;/code&gt;) are always pass-by-value.&lt;/p&gt;
&lt;h2 id=&quot;6-5-default-values-and-named-arguments&quot;&gt;6.5 Default values and named arguments&lt;/h2&gt;
&lt;p&gt;A parameter may carry a default expression in its declaration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;greeting&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;greeting&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factor&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;factor&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;offset&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Default-valued parameters must form a &lt;strong&gt;contiguous trailing run&lt;/strong&gt; — once one parameter has a default, every later parameter must also have one. This guarantees that a positional caller can simply drop trailing arguments.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;World&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// &amp;quot;Hello, World!&amp;quot; — greeting defaulted&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;//  20.0 — factor + offset defaulted&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;//  30.0 — offset defaulted&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Named arguments&lt;/strong&gt; at the call site identify a parameter by name with &lt;code&gt;name = expr&lt;/code&gt;. Named arguments may appear in any order, may skip over middle parameters that have defaults, and must come &lt;em&gt;after&lt;/em&gt; any positional arguments:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Nex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// &amp;quot;Hello, Nex!&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;greeting&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// &amp;quot;Hi, Foo!&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;offset&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;//  25.0 — factor defaulted&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;factor&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;offset&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;//  72.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Resolution rule (compile-time):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;All positional arguments fill leading parameter positions in order.&lt;/li&gt;
&lt;li&gt;Each named argument fills the parameter slot matching its name.&lt;/li&gt;
&lt;li&gt;Any unfilled slot uses that parameter’s declared default; an unfilled slot whose parameter has no default is a compile error.&lt;/li&gt;
&lt;li&gt;Supplying the same parameter twice (positional + named, or named twice) is a compile error.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The default expression is &lt;strong&gt;captured untouched at declaration time and re-evaluated at every call site that uses it&lt;/strong&gt; (Scala / JavaScript semantics, not Python’s evaluate-once). A default like &lt;code&gt;id: integer = next_id()&lt;/code&gt; calls &lt;code&gt;next_id()&lt;/code&gt; fresh on each call.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Restrictions.&lt;/strong&gt; Defaults and named arguments are only supported when the callee is a single, non-overloaded &lt;code&gt;def&lt;/code&gt; referenced by name. Overload sets (multiple &lt;code&gt;def&lt;/code&gt;s sharing a name) require fully-positional calls. Computed callees (lambda values, function-typed parameters, returned closures) likewise require positional arguments. Method-call syntax (&lt;code&gt;receiver.f(args)&lt;/code&gt;) does not yet support named arguments — use the function-call form &lt;code&gt;f(receiver, ...)&lt;/code&gt; if you need them.&lt;/p&gt;
&lt;h2 id=&quot;6-6-closures&quot;&gt;6.6 Closures&lt;/h2&gt;
&lt;p&gt;Function values may be created with lambda syntax (chapter 4) and passed as arguments or returned from functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_adder&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;add5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;make_adder&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;add5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// 8.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Closures capture lexical bindings. Capture of &lt;code&gt;val&lt;/code&gt; bindings is unrestricted. Capture of &lt;code&gt;var&lt;/code&gt; array bindings moves the binding into the closure (uniqueness preserved).&lt;/p&gt;
&lt;h2 id=&quot;6-7-higher-order-functions&quot;&gt;6.7 Higher-order functions&lt;/h2&gt;
&lt;p&gt;Functions are first-class values. They may be passed as arguments and returned from other functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// 9.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// 4.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;6-8-recursion&quot;&gt;6.8 Recursion&lt;/h2&gt;
&lt;p&gt;Direct and mutual recursion are supported. Mutually-recursive functions must appear in the same module; forward declarations are not required.&lt;/p&gt;
&lt;h2 id=&quot;6-9-return&quot;&gt;6.9 &lt;code&gt;return&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; expression terminates the enclosing function with the given value (or &lt;code&gt;()&lt;/code&gt; if omitted). It is useful for early exit:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;first_negative&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A function’s final expression provides its return value implicitly without &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;6-10-generic-functions&quot;&gt;6.10 Generic functions&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;def&lt;/code&gt; may introduce one or more &lt;strong&gt;type parameters&lt;/strong&gt; in square brackets after its name. Each type parameter stands for a type fixed at the call site; the body of the function is type-checked against the parameter, and the compiler generates a specialized clone for every distinct type the function is called with.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Ord&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bracketed list may be a mix of bounded and unbounded entries: &lt;code&gt;[T]&lt;/code&gt; is shorthand for &lt;code&gt;[T: Any]&lt;/code&gt;, and &lt;code&gt;[T: Float, U: Numeric, V]&lt;/code&gt; is a three-parameter list with two bounds.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Kind constraints.&lt;/strong&gt; A bare &lt;code&gt;[T]&lt;/code&gt; allows any type. A bound &lt;code&gt;[T: Kind]&lt;/code&gt; restricts &lt;code&gt;T&lt;/code&gt; to a fixed list of admissible types, picked from the closed set below. Stage 1 of the generics feature does not allow user-defined constraints — the set is hard-wired.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Constraint&lt;/th&gt;&lt;th&gt;Admitted types&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Any&lt;/code&gt;&lt;/td&gt;&lt;td&gt;every type (the default for &lt;code&gt;[T]&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Numeric&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;complex&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Real&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Float&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;real&lt;/code&gt; (kept distinct from &lt;code&gt;Real&lt;/code&gt; so split-precision additions extend it cleanly)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Complex&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;complex&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Ord&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt; (the types that support &lt;code&gt;&amp;lt;&lt;/code&gt; / &lt;code&gt;&amp;lt;=&lt;/code&gt; / &lt;code&gt;&amp;gt;&lt;/code&gt; / &lt;code&gt;&amp;gt;=&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;Eq&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;complex&lt;/code&gt; (the types with structural &lt;code&gt;==&lt;/code&gt; / &lt;code&gt;!=&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A call site whose argument type isn’t admitted by the constraint is rejected at elaboration time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Call-site inference.&lt;/strong&gt; The caller never writes the type arguments — the compiler infers them by unifying each formal parameter against its actual:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// T := integer; result type integer&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pickMax&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// T := real; result type real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// T := integer, U := string; result integer&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the same type parameter appears in multiple formal positions, all the corresponding actuals must unify to a single type. For &lt;code&gt;Numeric&lt;/code&gt;-constrained variables the unifier widens through the numeric tower (so &lt;code&gt;f[T: Numeric](x: T, y: T)&lt;/code&gt; called as &lt;code&gt;f(1, 2.0)&lt;/code&gt; widens to &lt;code&gt;real&lt;/code&gt;); for non-numeric constraints, mismatched actuals are a hard error.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Overload resolution.&lt;/strong&gt; A name may simultaneously bind a generic &lt;code&gt;def&lt;/code&gt; and one or more concrete overloads. &lt;strong&gt;Concrete overloads win when they apply&lt;/strong&gt; — a generic candidate is consulted only when no concrete overload accepts the call site’s argument types:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;200&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 100 — concrete integer overload&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// 200 — falls through to the generic&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Within the concrete bucket the existing numeric-promotion ranking still picks the closest match (Functions §6.5 already covers this). Two generic candidates both applicable to the same call is an unambiguity error — the user must add a concrete overload or further constrain one of the generics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Specialization.&lt;/strong&gt; Generic templates have no executable form on their own. The compiler walks every reachable call site, deduces the type arguments, and emits a specialized clone with the parameter substituted. The clones (named &lt;code&gt;pickMax$real&lt;/code&gt;, &lt;code&gt;id$string&lt;/code&gt;, etc.) are what backends actually compile and what the linker sees. A generic that is never called produces no code — there is no abstract “generic dispatcher” floating around at runtime.&lt;/p&gt;
&lt;p&gt;Specialization recurses: a generic body that itself calls a generic produces a chain of specializations, with the outer call’s type arguments threaded through to the inner call.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Generic structs and enums.&lt;/strong&gt; The same constraint set, call-site inference, and specialization model extend to user-declared &lt;code&gt;struct&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; types. See &lt;a href=&quot;/spec/03-types/#36-generic-structs&quot;&gt;§3.6&lt;/a&gt; and &lt;a href=&quot;/spec/03-types/#38-generic-enums&quot;&gt;§3.8&lt;/a&gt; for declaration syntax and the rules around bare-variant inference.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Limitations.&lt;/strong&gt; User-defined trait/type-class constraints are deferred — the constraint set is the closed list above. Lambda bodies that thread a kind variable through to a nested generic call are supported; arrays and aggregates parameterized by a kind variable inherit the limitations of the current array-element-type rules.&lt;/p&gt;
&lt;h2 id=&quot;6-11-intrinsic-declarations&quot;&gt;6.11 &lt;code&gt;@intrinsic&lt;/code&gt; declarations&lt;/h2&gt;
&lt;p&gt;A bodyless &lt;code&gt;def&lt;/code&gt; annotated with &lt;code&gt;@intrinsic(&amp;quot;opId&amp;quot;)&lt;/code&gt; declares a function whose implementation is supplied by the compiler — typically a libm bridge or a runtime helper — rather than by Nex source. The &lt;code&gt;opId&lt;/code&gt; string names the lowering: &lt;code&gt;@intrinsic(&amp;quot;libm.sqrt&amp;quot;)&lt;/code&gt; lowers to a direct call to the host’s libm &lt;code&gt;sqrt&lt;/code&gt; primitive.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;intrinsic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;libm.sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is how the standard prelude bridges the real-libm transcendentals (see the Prelude chapter); user code generally has no reason to write &lt;code&gt;@intrinsic&lt;/code&gt; directly. The attribute is the only sanctioned escape hatch — any other bodyless &lt;code&gt;def&lt;/code&gt; is a parse error.&lt;/p&gt;
&lt;h2 id=&quot;6-12-test-functions&quot;&gt;6.12 Test functions&lt;/h2&gt;
&lt;p&gt;A function declared with the &lt;code&gt;@test&lt;/code&gt; attribute is a unit test: it takes no arguments, returns &lt;code&gt;unit&lt;/code&gt;, and is discovered automatically by the test runner.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_addition_is_commutative&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_eq&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-function&quot;&gt;@test&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;test_normalize_produces_unit_vector&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;normalize&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;assert_approx&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1e-12&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Test functions are stripped from non-test builds — they do not appear in production binaries.&lt;/p&gt;
&lt;p&gt;Tests in a regular module can see every binding in that module, including &lt;code&gt;private&lt;/code&gt; ones. This makes it natural to test internal helpers without widening their public surface.&lt;/p&gt;
&lt;p&gt;A test passes by returning normally and fails by trapping. The prelude provides assertion functions (see the Prelude chapter) that trap with structured failure information the test runner catches and reports.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Your First Program</title>
    <link href="https://nexlang.org/getting-started/first-program/"/>
    <id>https://nexlang.org/getting-started/first-program/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Write a hello-world, run it under the interpreter, see the output.</summary>
    <content type="html">&lt;h2 id=&quot;write-a-program&quot;&gt;Write a program&lt;/h2&gt;
&lt;p&gt;Create a file &lt;code&gt;hello.nex&lt;/code&gt; anywhere on your filesystem:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;Hello, Nex!&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two top-level pieces: a &lt;code&gt;def main()&lt;/code&gt; that’s the program’s entry point, and a &lt;code&gt;for&lt;/code&gt; loop that prints &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;20&lt;/code&gt;, … &lt;code&gt;50&lt;/code&gt;. Everything in the body uses two-space indentation; there are no braces or semicolons. &lt;code&gt;1..=5&lt;/code&gt; is an inclusive range; &lt;code&gt;1..5&lt;/code&gt; would stop at 4.&lt;/p&gt;
&lt;h2 id=&quot;run-it&quot;&gt;Run it&lt;/h2&gt;
&lt;p&gt;From the &lt;code&gt;nex&lt;/code&gt; repository root, invoke the interpreter via sbt:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run run /path/to/hello.nex&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Hello, Nex!
10
20
30
40
50
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;run&lt;/code&gt; parses, elaborates (type-checks), and executes the program with the tree-walking interpreter. It’s the fastest edit/run cycle for development — no native binary involved.&lt;/p&gt;
&lt;h2 id=&quot;what-else-run-accepts&quot;&gt;What else &lt;code&gt;run&lt;/code&gt; accepts&lt;/h2&gt;
&lt;p&gt;The same invocation works on any of the &lt;a href=&quot;/examples/&quot;&gt;examples&lt;/a&gt; in the repository:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run run examples/fft/main.nex&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If your program imports other modules (multi-file projects), point &lt;code&gt;run&lt;/code&gt; at the entry file and the import graph is discovered from its directory upward — see the &lt;a href=&quot;/spec/09-modules/&quot;&gt;Modules chapter&lt;/a&gt; in the spec for layout rules.&lt;/p&gt;
&lt;h2 id=&quot;next-step&quot;&gt;Next step&lt;/h2&gt;
&lt;p&gt;Now &lt;a href=&quot;/getting-started/03-compile-to-native/&quot;&gt;compile that same program to a native binary&lt;/a&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>FFT</title>
    <link href="https://nexlang.org/examples/fft/"/>
    <id>https://nexlang.org/examples/fft/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Cooley-Tukey radix-2 recursive FFT for any power-of-2 N — complex numbers, strided slicing for the even/odd split, element-wise array arithmetic, and slice assignment to stitch the two output halves.</summary>
    <content type="html">&lt;p&gt;A recursive discrete Fourier transform that showcases Nex’s complex number support, per-element array-literal coercion, strided slices for the even/odd split, and open-bound slice assignment for stitching the two output halves. Works for any power-of-2 length; the recursion bottoms out at the trivial one-point transform.&lt;/p&gt;
&lt;p&gt;The DFT of length &lt;span class=&quot;math inline&quot;&gt;\(N\)&lt;/span&gt; is&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[X_k = \sum_{n=0}^{N-1} x_n \, e^{-2\pi i\, k n / N}, \qquad k = 0, 1, \ldots, N-1.\]&lt;/div&gt;
&lt;p&gt;A direct evaluation is &lt;span class=&quot;math inline&quot;&gt;\(\Theta(N^2)\)&lt;/span&gt;. Cooley-Tukey splits the sum by even / odd index — &lt;span class=&quot;math inline&quot;&gt;\(E_k\)&lt;/span&gt; is the DFT of the even-indexed samples, &lt;span class=&quot;math inline&quot;&gt;\(O_k\)&lt;/span&gt; the DFT of the odd-indexed ones — and reuses each half twice:&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[X_k         = E_k + W_N^{\,k}\, O_k, \qquad
X_{k+N/2}   = E_k - W_N^{\,k}\, O_k,\]&lt;/div&gt;
&lt;p&gt;for &lt;span class=&quot;math inline&quot;&gt;\(k \in [0, N/2)\)&lt;/span&gt;, where &lt;span class=&quot;math inline&quot;&gt;\(W_N = e^{-2\pi i / N}\)&lt;/span&gt; is the principal &lt;span class=&quot;math inline&quot;&gt;\(N\)&lt;/span&gt;-th root of unity. Each recursive level halves the problem, giving &lt;span class=&quot;math inline&quot;&gt;\(\Theta(N \log N)\)&lt;/span&gt; overall.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;fft&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// Split into even/odd-indexed sub-arrays via strided slices.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ef&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fft&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// even-indexed: 0, 2, 4, ...&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fft&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// odd-indexed:  1, 3, 5, ...&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// Build the twiddled odd half as a vector first: t[k] = W_N^k · O[k].&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;angle&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;to_real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// Stitch the two output halves into `y` with element-wise array ops&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// (`ef + t`, `ef - t` on `[complex]`) plus a slice assignment per&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// half — no per-index `y[k] = ...; y[k + half] = ...` shuffle.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ef&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// open lo: same as 0..half&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ef&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// open hi: same as half..n&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// The `: [complex]` annotation pushes the element type into each&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// literal, so the real values 0.0 / 1.0 coerce per-element via&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;// `to_complex(...)`.&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;
  &lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;fft&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;FFT of [1, 1, 1, 1, 0, 0, 0, 0]:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output (the DC term &lt;span class=&quot;math inline&quot;&gt;\(X_0\)&lt;/span&gt; is the sum of inputs; real input gives the Hermitian symmetry &lt;span class=&quot;math inline&quot;&gt;\(X_k = \overline{X_{N-k}}\)&lt;/span&gt; for &lt;span class=&quot;math inline&quot;&gt;\(k &amp;gt; 0\)&lt;/span&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FFT of [1, 1, 1, 1, 0, 0, 0, 0]:
4.0+0.0i
1.0-2.414213562373095i
0.0+0.0i
1.0-0.4142135623730949i
0.0+0.0i
0.9999999999999999+0.4142135623730949i
0.0+0.0i
0.9999999999999997+2.414213562373095i
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The full source lives at &lt;code&gt;examples/fft/main.nex&lt;/code&gt;. Change &lt;code&gt;x&lt;/code&gt; to any power-of-2 length and the same code transforms it.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Expressions</title>
    <link href="https://nexlang.org/spec/expressions/"/>
    <id>https://nexlang.org/spec/expressions/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Operators and precedence, arithmetic, element-wise arrays, comparison, conditionals, lambdas, ranges, array literals and slicing, structs, tuples, blocks.</summary>
    <content type="html">&lt;p&gt;Nex is expression-oriented: nearly every syntactic construct is an expression with a type and a value.&lt;/p&gt;
&lt;h2 id=&quot;4-1-literal-expressions&quot;&gt;4.1 Literal expressions&lt;/h2&gt;
&lt;p&gt;Each literal form (chapter 2) is an expression of the corresponding type.&lt;/p&gt;
&lt;h2 id=&quot;4-2-variable-references&quot;&gt;4.2 Variable references&lt;/h2&gt;
&lt;p&gt;A bare identifier refers to a binding in scope. The expression has the type of the binding.&lt;/p&gt;
&lt;h2 id=&quot;4-3-operators-and-precedence&quot;&gt;4.3 Operators and precedence&lt;/h2&gt;
&lt;p&gt;Operators, in order from highest to lowest precedence:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Level&lt;/th&gt;&lt;th&gt;Operators&lt;/th&gt;&lt;th&gt;Associativity&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;&lt;code&gt;.&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;field / method access&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;&lt;code&gt;f(...)&lt;/code&gt;, &lt;code&gt;a[...]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;application, indexing&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;unary &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;right&lt;/td&gt;&lt;td&gt;unary minus, logical not&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;&lt;code&gt;^&lt;/code&gt;&lt;/td&gt;&lt;td&gt;right&lt;/td&gt;&lt;td&gt;exponentiation&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;juxtaposition (&lt;code&gt;2x&lt;/code&gt;, &lt;code&gt;2(e)&lt;/code&gt;, &lt;code&gt;2pi&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;implicit multiplication (§4.4)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;&lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, &lt;code&gt;@&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;multiplication, division, integer division, modulo, matrix multiply&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;addition, subtraction&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;&lt;code&gt;..&lt;/code&gt;, &lt;code&gt;..=&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;range constructors&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;&lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt;&lt;/td&gt;&lt;td&gt;non-associative&lt;/td&gt;&lt;td&gt;comparison&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;&lt;code&gt;and&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;logical and&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;&lt;code&gt;or&lt;/code&gt;&lt;/td&gt;&lt;td&gt;left&lt;/td&gt;&lt;td&gt;logical or&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;&lt;code&gt;-&amp;gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;right&lt;/td&gt;&lt;td&gt;function / lambda&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;&lt;code&gt;,&lt;/code&gt;&lt;/td&gt;&lt;td&gt;right&lt;/td&gt;&lt;td&gt;tuple constructor (§4.16)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;User-defined operators are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Comma binds looser than every other operator on a single line. That means &lt;code&gt;if cond then a else b, c&lt;/code&gt; parses as &lt;code&gt;(if cond then a else b), c&lt;/code&gt; — a 2-tuple. To put the comma inside an &lt;code&gt;else&lt;/code&gt; branch, either parenthesize (&lt;code&gt;if cond then (a, c) else (b, c)&lt;/code&gt;) or use the indented block form, whose last expression accepts a paren-less tuple naturally:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same single-line vs. block-form distinction applies to lambda bodies (&lt;code&gt;f = (x) -&amp;gt; (x, -x)&lt;/code&gt; vs. a block body whose last item is &lt;code&gt;x, -x&lt;/code&gt;) and to &lt;code&gt;match&lt;/code&gt; arm bodies. See §4.17.&lt;/p&gt;
&lt;h2 id=&quot;4-4-arithmetic-operators&quot;&gt;4.4 Arithmetic operators&lt;/h2&gt;
&lt;p&gt;The arithmetic operators &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt; apply to numeric operands with the following semantics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt; — work on &lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;complex&lt;/code&gt;. Promote per §3.2.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/&lt;/code&gt; — &lt;em&gt;real&lt;/em&gt; division. Both operands promoted to at least &lt;code&gt;real&lt;/code&gt;. &lt;code&gt;integer / integer&lt;/code&gt; produces &lt;code&gt;real&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;div&lt;/code&gt; — &lt;em&gt;integer&lt;/em&gt; division (keyword, not a symbol — &lt;code&gt;//&lt;/code&gt; is reserved for line comments). Both operands must be &lt;code&gt;integer&lt;/code&gt;. Truncates toward zero. Example: &lt;code&gt;7 div 3 == 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;%&lt;/code&gt; — &lt;em&gt;modulo&lt;/em&gt;. Both operands must be &lt;code&gt;integer&lt;/code&gt;. Result has sign of dividend.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;^&lt;/code&gt; — exponentiation. &lt;code&gt;real ^ real → real&lt;/code&gt;, &lt;code&gt;complex ^ complex → complex&lt;/code&gt;, &lt;code&gt;integer ^ integer → integer&lt;/code&gt; (only for non-negative exponent; negative exponent traps).&lt;/li&gt;
&lt;li&gt;unary &lt;code&gt;-&lt;/code&gt; — negation. Works on any numeric type.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Integer arithmetic traps on overflow. Real arithmetic follows IEEE 754 (NaN, infinities propagate; no traps from arithmetic itself).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Juxtaposition multiplication.&lt;/strong&gt; A numeric literal (integer or real) immediately followed by an identifier or an opening parenthesis — with or without intervening whitespace — denotes implicit multiplication. This makes mathematical notation read naturally:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;x                  &lt;span class=&quot;hl-comment&quot;&gt;// 2 * x&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// 2 * (x + 1)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;pi                 &lt;span class=&quot;hl-comment&quot;&gt;// 2 * pi&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;sin&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// 3 * sin(t)&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i                  &lt;span class=&quot;hl-comment&quot;&gt;// 2 * i    = (0.0 + 2.0i)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rule applies &lt;em&gt;only&lt;/em&gt; when the left operand is a numeric literal — not when it is an identifier and not when it is a parenthesized expression. Therefore:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;xy&lt;/code&gt; is the identifier &lt;code&gt;xy&lt;/code&gt;, &lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;x * y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(x)(y)&lt;/code&gt; is a function call (&lt;code&gt;x&lt;/code&gt; applied to &lt;code&gt;y&lt;/code&gt;), &lt;strong&gt;not&lt;/strong&gt; multiplication.&lt;/li&gt;
&lt;li&gt;To multiply two identifier or parenthesized expressions, write &lt;code&gt;*&lt;/code&gt; explicitly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Per the precedence table (§4.3), juxtaposition binds tighter than &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, and &lt;code&gt;%&lt;/code&gt;, but looser than &lt;code&gt;^&lt;/code&gt;. This matches how the math reads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;2x^3&lt;/code&gt;   is &lt;code&gt;2 * (x^3)&lt;/code&gt;        — &lt;code&gt;^&lt;/code&gt; binds tighter than juxtaposition&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1/2x&lt;/code&gt;   is &lt;code&gt;1 / (2 * x)&lt;/code&gt;      — juxtaposition binds tighter than &lt;code&gt;/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2x * 3&lt;/code&gt; is &lt;code&gt;(2 * x) * 3&lt;/code&gt;      — juxtaposition binds tighter than &lt;code&gt;*&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The juxtaposition rule subsumes Nex’s complex-number construction: &lt;code&gt;2i&lt;/code&gt;, &lt;code&gt;3 + 4i&lt;/code&gt;, and &lt;code&gt;(a + b)i&lt;/code&gt; are all ordinary expressions under this rule, with &lt;code&gt;i&lt;/code&gt; being a prelude constant of type &lt;code&gt;complex&lt;/code&gt; (see the Prelude chapter).&lt;/p&gt;
&lt;h2 id=&quot;4-5-element-wise-array-operators&quot;&gt;4.5 Element-wise array operators&lt;/h2&gt;
&lt;p&gt;When an arithmetic operator is applied with one or both operands being an array, the operation is &lt;strong&gt;element-wise&lt;/strong&gt;. The rules are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[T] op [T]&lt;/code&gt; — element-wise on rank-1, requires same length, returns &lt;code&gt;[T]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[[T]] op [[T]]&lt;/code&gt; — element-wise on rank-2, requires same shape, returns &lt;code&gt;[[T]]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;arr op T&lt;/code&gt; and &lt;code&gt;T op arr&lt;/code&gt; — broadcasts the scalar over the array (any rank), returns an array of the same shape and rank&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Shape mismatch (length for rank-1; shape for rank-2) is a runtime trap.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// [5.0, 7.0, 9.0]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// [2.0, 4.0, 6.0]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// [6.0, 9.0, 12.0]   (juxtaposition)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// 2×2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;N&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// [[11.0, 22.0], [33.0, 44.0]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;Q&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;M                               &lt;span class=&quot;hl-comment&quot;&gt;// [[2.0, 4.0], [6.0, 8.0]]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Element-wise expressions are guaranteed to fuse: &lt;code&gt;2a + b&lt;/code&gt; and its rank-2 equivalents lower to a single loop nest with no intermediate arrays allocated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Matrix multiplication&lt;/strong&gt; uses the &lt;code&gt;@&lt;/code&gt; operator (not element-wise). It is defined between rank-1 and rank-2 arrays as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[[T]] @ [[T]]    shapes (m, k) @ (k, n)  →  (m, n)     matrix-matrix
[[T]] @ [T]      shape  (m, k) @ k       →  m          matrix-vector
[T]  @ [[T]]     shape  k     @ (k, n)   →  n          vector-matrix
[T]  @ [T]       shape  k     @ k        →  T          dot product
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inner-dimension mismatch traps.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// 2×2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;Av&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// [17.0, 39.0]   (rank-1)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;AA&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;A&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// [[7.0, 10.0], [15.0, 22.0]]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;                           &lt;span class=&quot;hl-comment&quot;&gt;// 61.0           (scalar = dot)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;4-6-comparison-operators&quot;&gt;4.6 Comparison operators&lt;/h2&gt;
&lt;p&gt;The operators &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt; produce &lt;code&gt;bool&lt;/code&gt;. Comparison is defined for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bool == bool&lt;/code&gt;, &lt;code&gt;bool != bool&lt;/code&gt; only&lt;/li&gt;
&lt;li&gt;All ordered numeric comparisons between numeric types after promotion&lt;/li&gt;
&lt;li&gt;&lt;code&gt;complex == complex&lt;/code&gt; and &lt;code&gt;complex != complex&lt;/code&gt; (no ordered comparison on complex)&lt;/li&gt;
&lt;li&gt;Element-wise array comparison: &lt;code&gt;[T] op [T] → [bool]&lt;/code&gt; (same broadcast rules as §4.5)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string == string&lt;/code&gt;, &lt;code&gt;string != string&lt;/code&gt; (no ordered comparison on string)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;chained-comparisons&quot;&gt;Chained comparisons&lt;/h3&gt;
&lt;p&gt;Two or more comparison operators in a row form a chained comparison:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;lo&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hi&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;a OP1 b OP2 c ... OPn z&lt;/code&gt; is equivalent to&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;OP1&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;OP2&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;OPn&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with the additional guarantee that &lt;strong&gt;every inner operand is evaluated at most once and only if every earlier comparison succeeded&lt;/strong&gt;. Concretely, &lt;code&gt;0 &amp;lt; f() &amp;lt; 10&lt;/code&gt; calls &lt;code&gt;f()&lt;/code&gt; exactly once when &lt;code&gt;0 &amp;lt; f()&lt;/code&gt; is true, and not at all when it is false. The operators may be mixed freely — &lt;code&gt;0 &amp;lt;= i &amp;lt; n&lt;/code&gt; and &lt;code&gt;a &amp;lt; b == c&lt;/code&gt; are both well-formed — and any number of comparisons may be chained.&lt;/p&gt;
&lt;p&gt;Each pairwise comparison must independently satisfy the type rules above; in particular a chain that mixes ordered and equality operators on values for which the relevant pairwise comparison isn’t defined is rejected by the type checker.&lt;/p&gt;
&lt;h2 id=&quot;4-7-logical-operators&quot;&gt;4.7 Logical operators&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt; work on &lt;code&gt;bool&lt;/code&gt; (or &lt;code&gt;[bool]&lt;/code&gt; element-wise). &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; are short-circuiting on scalar &lt;code&gt;bool&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;4-8-function-calls&quot;&gt;4.8 Function calls&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;f&lt;/code&gt; has type &lt;code&gt;(T1, T2, ..., Tn) -&amp;gt; R&lt;/code&gt;, the call has type &lt;code&gt;R&lt;/code&gt;. Arguments are evaluated left-to-right.&lt;/p&gt;
&lt;h2 id=&quot;4-9-method-like-notation&quot;&gt;4.9 Method-like notation&lt;/h2&gt;
&lt;p&gt;The expression &lt;code&gt;e.name(args...)&lt;/code&gt; is sugar for &lt;code&gt;name(e, args...)&lt;/code&gt; when &lt;code&gt;e.name&lt;/code&gt; does not refer to a struct field. The compiler resolves &lt;code&gt;e.name&lt;/code&gt; as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;e&lt;/code&gt;‘s type is a struct with a field named &lt;code&gt;name&lt;/code&gt;, treat as field access.&lt;/li&gt;
&lt;li&gt;Otherwise, if a function &lt;code&gt;name&lt;/code&gt; is in scope whose first parameter matches the type of &lt;code&gt;e&lt;/code&gt;, treat as &lt;code&gt;name(e, args...)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise, error.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This allows the natural chaining style:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// equivalent to sum(map(arr, x -&amp;gt; x * 2.0))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;4-10-conditionals&quot;&gt;4.10 Conditionals&lt;/h2&gt;
&lt;p&gt;An &lt;code&gt;if&lt;/code&gt; expression has the form:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;expr1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;expr2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is an expression whose type is the common type of &lt;code&gt;expr1&lt;/code&gt; and &lt;code&gt;expr2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;then&lt;/code&gt; is &lt;strong&gt;required for an inline body&lt;/strong&gt; and &lt;strong&gt;optional when the body starts on a new indented line&lt;/strong&gt;. Multi-line form (with or without &lt;code&gt;then&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// `then` optional when body is indented:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr2&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `then` still works for the multi-line form too:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For chained conditionals — both &lt;code&gt;else if&lt;/code&gt; and &lt;code&gt;elif&lt;/code&gt; (single-token shorthand) are accepted, with identical semantics:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond1&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond2&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr3&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Same shape with `elif`:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond1&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond2&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr2&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;expr3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An &lt;code&gt;if&lt;/code&gt; without an &lt;code&gt;else&lt;/code&gt; is an expression of type &lt;code&gt;unit&lt;/code&gt;. Both branches in that case must also have type &lt;code&gt;unit&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// type unit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;4-11-lambdas&quot;&gt;4.11 Lambdas&lt;/h2&gt;
&lt;p&gt;Lambda expressions use &lt;code&gt;-&amp;gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// optional parameter type annotation&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Single-parameter lambdas may omit parentheses. Multi-parameter lambdas require them. Parameter types are inferred from context where possible; otherwise must be annotated.&lt;/p&gt;
&lt;p&gt;Block-form lambdas:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Lambdas capture surrounding lexical bindings. Captured &lt;code&gt;val&lt;/code&gt; bindings are captured by value (or by shared reference, per ARC). Captured &lt;code&gt;var&lt;/code&gt; bindings are captured uniquely — only one closure may capture a given &lt;code&gt;var&lt;/code&gt; binding, and the binding is moved into the closure (see chapter 8).&lt;/p&gt;
&lt;h2 id=&quot;4-12-ranges&quot;&gt;4.12 Ranges&lt;/h2&gt;
&lt;p&gt;The range expressions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;lo..hi&lt;/code&gt; — half-open: &lt;code&gt;[lo, hi)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lo..=hi&lt;/code&gt; — closed: &lt;code&gt;[lo, hi]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Operands are &lt;code&gt;integer&lt;/code&gt;. The result is a &lt;code&gt;range&lt;/code&gt;, a built-in iterable type used in &lt;code&gt;for&lt;/code&gt; loops and convertible to &lt;code&gt;[integer]&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// 0, 1, ..., n-1&lt;/span&gt;
&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// 0, 1, ..., n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Materialization to &lt;code&gt;[integer]&lt;/code&gt; is lazy — a range used in a &lt;code&gt;for&lt;/code&gt; loop iterates without allocating an array.&lt;/p&gt;
&lt;h2 id=&quot;4-13-array-literals-and-construction&quot;&gt;4.13 Array literals and construction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Rank-1 literals&lt;/strong&gt; use square brackets:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// [real]&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// [integer]&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [bool]&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// [complex]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All elements must have the same type. Empty rank-1 literals require a type annotation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Rank-2 literals&lt;/strong&gt; are nested. Each inner array is a row of the resulting matrix:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// [[real]], 2×3 matrix&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;grid&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// [[integer]], 3×3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inner arrays must all have the same length (rectangularity); a jagged literal traps at runtime.&lt;/p&gt;
&lt;p&gt;Arrays may also be constructed from ranges via the &lt;code&gt;range&lt;/code&gt; function (rank-1) and from the standard-library constructors &lt;code&gt;zeros&lt;/code&gt;, &lt;code&gt;ones&lt;/code&gt;, &lt;code&gt;fill&lt;/code&gt;, &lt;code&gt;identity&lt;/code&gt;, &lt;code&gt;linspace&lt;/code&gt; (see the Prelude chapter).&lt;/p&gt;
&lt;h2 id=&quot;4-14-indexing-and-slicing&quot;&gt;4.14 Indexing and slicing&lt;/h2&gt;
&lt;p&gt;Array indexing is &lt;strong&gt;0-based&lt;/strong&gt;. Out-of-bounds indexing traps.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Negative indices&lt;/strong&gt; count from the end of the axis: &lt;code&gt;a[-1]&lt;/code&gt; is the last element, &lt;code&gt;a[-2]&lt;/code&gt; is the second-to-last, and so on. The wrap is &lt;code&gt;i + length&lt;/code&gt; for &lt;code&gt;i &amp;lt; 0&lt;/code&gt;; the bounds check then runs against the wrapped value, so &lt;code&gt;a[-10]&lt;/code&gt; on a 3-element array still traps. Negative indices apply to both axes of a rank-2 index (&lt;code&gt;m[-1, -1]&lt;/code&gt; is the bottom-right element), to the single-index row form (&lt;code&gt;m[-1]&lt;/code&gt; is the last row), AND to slice and axis-range bounds (&lt;code&gt;a[-3..length(a)]&lt;/code&gt; is the last 3 elements; &lt;code&gt;m[:, -1]&lt;/code&gt; is the last column).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rank-1 indexing&lt;/strong&gt; with a single integer:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 10.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 30.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// 30.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// 10.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Rank-1 slicing&lt;/strong&gt; with a range expression returns a freshly owned array (copying elements):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// [10.0, 20.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// [20.0, 30.0]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Open-ended slice bounds&lt;/strong&gt; omit either side; the missing bound fills from the array’s runtime extent (&lt;code&gt;0&lt;/code&gt; for the lower side, &lt;code&gt;length(a)&lt;/code&gt; for the upper). Each side is independent and combines with negative indices and the inclusive form:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// [30.0]        — lo given, hi defaults to length(a)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// [10.0, 20.0]  — hi given, lo defaults to 0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// full copy     — both ends default&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// last 3 elements&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// all but the last&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// inclusive — full array&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The open forms are only legal inside an index list; using &lt;code&gt;..&lt;/code&gt; or &lt;code&gt;..=&lt;/code&gt; outside a slice context (e.g. as a value on the right of &lt;code&gt;=&lt;/code&gt;) is a parse error.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Strided slices&lt;/strong&gt; sample every k-th element from the selected window via a trailing &lt;code&gt;by k&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// every other element from indices 0..9 (exclusive)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// 1, 4, 7&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// inclusive — same as the first form on a length-10 array&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// combines with open bounds&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// combines with negative bounds&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The stride must be a positive integer; a stride of &lt;code&gt;0&lt;/code&gt; or any negative value traps. Negative strides (reverse iteration) are &lt;em&gt;deferred&lt;/em&gt;. Strided rank-2 axis ranges (&lt;code&gt;m[lo..hi by k, :]&lt;/code&gt;) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Strided forms are also valid &lt;strong&gt;assignment targets&lt;/strong&gt; (spec §4.15): the RHS supplies one element per selected position. Length mismatch and non-positive stride trap the same way they do on read:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// every-other slot&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//  a == [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;70&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;800&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;//  b == [10, 200, 30, 40, 500, 60, 70, 800, 90, 100]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Rank-2 indexing&lt;/strong&gt; uses two integer indices separated by a comma:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// 1.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// 6.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The single-index form &lt;code&gt;m[i]&lt;/code&gt; returns the i-th row as a freshly-owned rank-1 array:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [1.0, 2.0, 3.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// [4.0, 5.0, 6.0]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Rank-2 slicing&lt;/strong&gt; uses &lt;code&gt;:&lt;/code&gt; to mean “all of this axis” and range expressions for sub-extents. Result rank depends on what’s collapsed: an integer along an axis collapses it; &lt;code&gt;:&lt;/code&gt; or a range preserves it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// column 1 (rank-1)        → [2.0, 5.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// row 0, cols 0-1 (rank-1) → [1.0, 2.0]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// sub-matrix (rank-2)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// full copy (rank-2)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All slice forms return freshly-owned arrays. View-style slicing (returning a borrow into the source array without copying) is &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;4-15-slice-assignment&quot;&gt;4.15 Slice assignment&lt;/h2&gt;
&lt;p&gt;Slice forms (the same shapes documented in §4.14) are also valid on the &lt;strong&gt;left&lt;/strong&gt; of &lt;code&gt;=&lt;/code&gt;. This is the Fortran-90 array-section assignment idiom: a single statement overwrites an entire sub-extent of a &lt;code&gt;var&lt;/code&gt; array in place.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// xs = [10, 200, 300, 400, 50]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..=&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// xs = [1, 2, 3, 400, 50]&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// replace row 1&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// replace column 0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]]&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// replace a 2×2 sub-matrix&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Shape conformance.&lt;/strong&gt; The right-hand side must match the slice’s shape:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A rank-1 slice (&lt;code&gt;a[lo..hi]&lt;/code&gt;, &lt;code&gt;a[lo..=hi]&lt;/code&gt;) expects a rank-1 array of the same length.&lt;/li&gt;
&lt;li&gt;A rank-2 slice with both axes preserved (&lt;code&gt;m[r1..r2, c1..c2]&lt;/code&gt;, &lt;code&gt;m[:, :]&lt;/code&gt;) expects a rank-2 array of matching shape.&lt;/li&gt;
&lt;li&gt;A rank-2 slice with one axis collapsed (&lt;code&gt;m[i, c1..c2]&lt;/code&gt;, &lt;code&gt;m[:, j]&lt;/code&gt;, &lt;code&gt;m[i, :]&lt;/code&gt;) expects a rank-1 array of matching length.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Length / shape mismatches trap at runtime, as does any out-of-bounds slice bound.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In-place mutation.&lt;/strong&gt; The underlying buffer is rewritten — the &lt;code&gt;var&lt;/code&gt; binding does not acquire a new identity. The assignment is observable through every binding that aliases the same underlying array, and no clone fires from this construct alone.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When to reach for slice assignment.&lt;/strong&gt; Three common cases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Replacing a contiguous prefix / suffix / middle of a rank-1 buffer&lt;/strong&gt;, where you’d otherwise write an indexed loop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Writing one row, column, or sub-block of a rank-2 matrix&lt;/strong&gt; that you’ve computed independently — &lt;code&gt;result[py, :] = row_vector&lt;/code&gt; is the working idiom (see the Mandelbrot example).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoiding a &lt;code&gt;mut&lt;/code&gt; parameter&lt;/strong&gt; for “replace this region with that data” APIs, since slice assignment mutates a binding directly in its own scope without the auto-clone interaction that follows passing a &lt;code&gt;var&lt;/code&gt; array to a &lt;code&gt;mut&lt;/code&gt; parameter (§8.3).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Strided slice forms (&lt;code&gt;xs[lo..hi by k] = rhs&lt;/code&gt;) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;4-16-struct-construction-and-access&quot;&gt;4.16 Struct construction and access&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// 3.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Struct field reassignment is allowed only on &lt;code&gt;var&lt;/code&gt; bindings (see chapter 5):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;4-17-tuple-construction-and-access&quot;&gt;4.17 Tuple construction and access&lt;/h2&gt;
&lt;p&gt;Tuples are constructed by &lt;strong&gt;comma-separated expressions&lt;/strong&gt; (length ≥ 2). Parentheses are not required:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;three&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// type: integer, real, string&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pair&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;           &lt;span class=&quot;hl-comment&quot;&gt;// type: real, real&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parentheses are optional grouping; both forms are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;three&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parentheses become &lt;strong&gt;required&lt;/strong&gt; when the surrounding context already uses commas for another purpose:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;                   &lt;span class=&quot;hl-comment&quot;&gt;// 3 arguments&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;                 &lt;span class=&quot;hl-comment&quot;&gt;// 1 argument, the tuple (1, 2, 3)&lt;/span&gt;

&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// [integer] of 3 elements&lt;/span&gt;
&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// [(integer, integer)] of 2 tuples&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tuple elements are accessed by 0-based numeric field:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// 1&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// 2.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// &amp;quot;three&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tuple destructuring in &lt;code&gt;val&lt;/code&gt; / &lt;code&gt;var&lt;/code&gt; bindings — no parens needed unless type-annotated:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;t&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// bind components&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;ignored&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// _ discards&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;     &lt;span class=&quot;hl-comment&quot;&gt;// parens required when annotating types&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Single-element tuples do not exist; &lt;code&gt;1&lt;/code&gt; is just &lt;code&gt;1&lt;/code&gt;. The empty tuple &lt;code&gt;()&lt;/code&gt; is already the unit literal (chapter 2) — there is no zero-arity tuple distinct from &lt;code&gt;unit&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tuples as block results.&lt;/strong&gt; Inside an indented block — a &lt;code&gt;def&lt;/code&gt; body, a &lt;code&gt;val&lt;/code&gt;/&lt;code&gt;var&lt;/code&gt;/&lt;code&gt;const&lt;/code&gt; binding RHS that uses block form, a &lt;code&gt;then&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt; / &lt;code&gt;do&lt;/code&gt; body, a &lt;code&gt;match&lt;/code&gt; arm body, or a lambda body that uses block form — the block’s last item may be a paren-less tuple:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;make_box&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;d&lt;/span&gt;                            &lt;span class=&quot;hl-comment&quot;&gt;// returns a 3-tuple&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;first_positive&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;    &lt;span class=&quot;hl-comment&quot;&gt;// returns a 2-tuple&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;
  &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;                          &lt;span class=&quot;hl-comment&quot;&gt;// fallback 2-tuple&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;return&lt;/code&gt; is a statement, so its value also accepts a paren-less tuple (&lt;code&gt;return a, b, c&lt;/code&gt; returns the 3-tuple &lt;code&gt;(a, b, c)&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&quot;4-18-block-expressions&quot;&gt;4.18 Block expressions&lt;/h2&gt;
&lt;p&gt;A sequence of statements followed by a final expression is itself an expression with the type and value of the final expression. The block form arises from indentation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;                      &lt;span class=&quot;hl-comment&quot;&gt;// type of the block is the type of z + 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the final form is a statement (e.g., assignment), the block has type &lt;code&gt;unit&lt;/code&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Deferred</title>
    <link href="https://nexlang.org/spec/deferred/"/>
    <id>https://nexlang.org/spec/deferred/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Features intentionally excluded from the current language, split into near-term additions and long-term work.</summary>
    <content type="html">&lt;p&gt;The features deferred from the current language are split into two tiers by scope:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Near-term additions&lt;/strong&gt; — small, additive, non-breaking. Planned as natural follow-ups without redesigning anything in the core.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Long-term work&lt;/strong&gt; — major features, possibly breaking, requiring substantive design and implementation effort.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;11-1-near-term-additions&quot;&gt;11.1 Near-term additions&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;(All previously-listed near-term items are now shipped.)&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;11-2-long-term-work&quot;&gt;11.2 Long-term work&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Type system:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sized numeric variants (&lt;code&gt;integer8&lt;/code&gt;, &lt;code&gt;integer32&lt;/code&gt;, &lt;code&gt;real32&lt;/code&gt;, &lt;code&gt;complex32&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Higher-rank arrays (rank-3 and beyond) and a static-shape language (&lt;code&gt;[real; M, N]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Pattern matching beyond enums (literal patterns, guards, range patterns) — current &lt;code&gt;match&lt;/code&gt; shape is defined in &lt;a href=&quot;/spec/07-control-flow/#75-match-expressions&quot;&gt;§7.5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Traits / type classes (Stage 3 of the generics roadmap)&lt;/li&gt;
&lt;li&gt;Type aliases&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Syntax / expressions:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;User-defined operators (with precedence inheritance)&lt;/li&gt;
&lt;li&gt;Operator definitions on existing types&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Memory / runtime:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;RAII for &lt;code&gt;var&lt;/code&gt; arrays (currently ARC)&lt;/li&gt;
&lt;li&gt;Cycle detection for ARC&lt;/li&gt;
&lt;li&gt;Parallel-for / parallel reductions&lt;/li&gt;
&lt;li&gt;SIMD intrinsics&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Modules / build:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Module interface/implementation split (Fortran-2008-style submodules)&lt;/li&gt;
&lt;li&gt;Package manager&lt;/li&gt;
&lt;li&gt;Conditional compilation&lt;/li&gt;
&lt;li&gt;FFI / C ABI compatibility&lt;/li&gt;
&lt;li&gt;Calling external Fortran / BLAS / LAPACK&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Standard library:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;geometry3&lt;/code&gt; stdlib module — quaternions plus the other 3D-rotation representations (rotation matrices, axis–angle, Euler angles) and the conversions between them. Quaternions are &lt;em&gt;not&lt;/em&gt; planned as a built-in primitive: they don’t extend the integer → real → complex promotion lattice cleanly (complex has no canonical embedding into quaternions — which of &lt;code&gt;i&lt;/code&gt; / &lt;code&gt;j&lt;/code&gt; / &lt;code&gt;k&lt;/code&gt;?), their multiplication is non-commutative (which breaks the element-wise / broadcast story for &lt;code&gt;*&lt;/code&gt;), and the workloads that need them (3D graphics, robotics, attitude estimation) sit outside the Fortran / numerical-array audience the language is shaped around. The library home is the right one once generics (&lt;code&gt;def foo[T](...)&lt;/code&gt;) and structs with methods both ship — until then, users who need a &lt;code&gt;Q4&lt;/code&gt; can roll a 4-field struct and write the ops by hand.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Backend:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GPU / accelerator targets&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Tooling:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Language server / IDE integration&lt;/li&gt;
&lt;li&gt;Debugger&lt;/li&gt;
&lt;li&gt;Profiler&lt;/li&gt;
&lt;li&gt;Documentation generator&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>Control Flow</title>
    <link href="https://nexlang.org/spec/control-flow/"/>
    <id>https://nexlang.org/spec/control-flow/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>if / else, for, while, match, block scoping.</summary>
    <content type="html">&lt;h2 id=&quot;7-1-if-else&quot;&gt;7.1 &lt;code&gt;if&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;See the Expressions chapter. &lt;code&gt;if&lt;/code&gt; is an expression.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;then&lt;/code&gt; is &lt;strong&gt;required for an inline body&lt;/strong&gt; and &lt;strong&gt;optional when the body starts on a new indented line&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Inline — `then` required:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sign&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Block body — `then` optional:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;do_something&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;do_more&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;fallback&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;7-2-for-loops&quot;&gt;7.2 &lt;code&gt;for&lt;/code&gt; loops&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop iterates over an iterable. Iterables are ranges and arrays:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// `do` required when the body is inline:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// `do` optional when the body starts on a new indented line:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;end for&lt;/code&gt; marker is optional in both forms.&lt;/p&gt;
&lt;p&gt;A &lt;code&gt;for&lt;/code&gt; loop is an expression of type &lt;code&gt;unit&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Tuple destructuring in the loop variable — no parens needed:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;7-3-while-loops&quot;&gt;7.3 &lt;code&gt;while&lt;/code&gt; loops&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;while&lt;/code&gt; follows the same rule as &lt;code&gt;for&lt;/code&gt;: &lt;code&gt;do&lt;/code&gt; required for inline bodies, optional when the body starts on a new indented line.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;cond&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;body&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt;

&lt;span class=&quot;hl-comment&quot;&gt;// Inline form:&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;keep_going&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop is an expression of type &lt;code&gt;unit&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;7-4-block-scoping&quot;&gt;7.4 Block scoping&lt;/h2&gt;
&lt;p&gt;Each control-flow body introduces a new scope. Bindings declared in a body are not visible outside it.&lt;/p&gt;
&lt;h2 id=&quot;7-5-match-expressions&quot;&gt;7.5 &lt;code&gt;match&lt;/code&gt; expressions&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;match&lt;/code&gt; inspects a sum-type value (&lt;a href=&quot;/spec/03-types/#37-sum-types&quot;&gt;§3.7&lt;/a&gt;) and dispatches on its variant. The scrutinee sits to the &lt;em&gt;left&lt;/em&gt; of &lt;code&gt;match&lt;/code&gt;; arms follow on indented lines, each &lt;code&gt;pattern -&amp;gt; body&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;iters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Solver&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Converged&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Diverged&lt;/span&gt;          &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;MaxIters&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;last&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;match&lt;/code&gt; is an expression — all arm bodies must yield the same type, and the &lt;code&gt;match&lt;/code&gt; itself evaluates to that type.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Optional &lt;code&gt;end match&lt;/code&gt;.&lt;/strong&gt; The block can be closed with an &lt;code&gt;end match&lt;/code&gt; marker. The marker is purely visual; the parser doesn’t enforce it, and the same indented &lt;code&gt;Dedent&lt;/code&gt; boundary closes the block whether or not it appears:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Red&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Green&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;go&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;hl-type&quot;&gt;Blue&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Patterns.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Variant&lt;/code&gt; — matches a bare variant by name.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Variant(p1, p2, ...)&lt;/code&gt; — matches a fielded variant and recursively binds each sub-pattern to the corresponding field.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;name&lt;/code&gt; — matches anything and binds the scrutinee to &lt;code&gt;name&lt;/code&gt;. Acts as a catch-all when used as a top-level pattern.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;_&lt;/code&gt; — matches anything without binding. Same role as &lt;code&gt;name&lt;/code&gt; but without introducing a name in scope.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When used inside a variant pattern, sub-patterns are restricted to plain bindings or &lt;code&gt;_&lt;/code&gt; — nested variant patterns are &lt;em&gt;deferred&lt;/em&gt; until variant fields can carry aggregate types.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Exhaustiveness.&lt;/strong&gt; Every variant of the scrutinee’s enum must be covered, either by an explicit &lt;code&gt;Variant(...) -&amp;gt;&lt;/code&gt; arm or by a catch-all (&lt;code&gt;_ -&amp;gt;&lt;/code&gt; or &lt;code&gt;name -&amp;gt;&lt;/code&gt;). The compiler rejects a match that leaves variants uncovered:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Red&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Green&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Blue&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;isRed&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;match&lt;/span&gt;
    &lt;span class=&quot;hl-type&quot;&gt;Red&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;_&lt;/span&gt;   &lt;span class=&quot;hl-keyword&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;false&lt;/span&gt;             &lt;span class=&quot;hl-comment&quot;&gt;// catch-all closes the set&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Duplicate arms (two arms covering the same variant) and arms after a catch-all are also rejected. A non-exhaustive match without a catch-all is a compile error, not a runtime trap.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Restrictions.&lt;/strong&gt; The scrutinee must have an enum type — &lt;code&gt;match&lt;/code&gt; on integers, reals, strings, structs, etc. is not supported. Guards (&lt;code&gt;Variant(x) if x &amp;gt; 0 -&amp;gt; ...&lt;/code&gt;) are &lt;em&gt;deferred&lt;/em&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Complex Numbers and the Quadratic Formula</title>
    <link href="https://nexlang.org/examples/complex/"/>
    <id>https://nexlang.org/examples/complex/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Complex arithmetic, then a quadratic solver returning a pair of complex roots.</summary>
    <content type="html">&lt;h2 id=&quot;complex-numbers&quot;&gt;Complex numbers&lt;/h2&gt;
&lt;p&gt;A complex number &lt;span class=&quot;math inline&quot;&gt;\(z = a + bi\)&lt;/span&gt; has modulus &lt;span class=&quot;math inline&quot;&gt;\(\lvert z \rvert = \sqrt{a^2 + b^2}\)&lt;/span&gt; and argument &lt;span class=&quot;math inline&quot;&gt;\(\arg(z) = \operatorname{atan2}(b, a)\)&lt;/span&gt;, related by the polar form &lt;span class=&quot;math inline&quot;&gt;\(z = \lvert z \rvert \, e^{i\arg(z)}\)&lt;/span&gt; — i.e. Euler’s formula &lt;span class=&quot;math inline&quot;&gt;\(e^{i\theta} = \cos\theta + i\sin\theta\)&lt;/span&gt;. The prelude exposes both the field-style accessors &lt;code&gt;.re&lt;/code&gt; / &lt;code&gt;.im&lt;/code&gt; and the polar-form helpers &lt;code&gt;abs(z)&lt;/code&gt; / &lt;code&gt;arg(z)&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;i             &lt;span class=&quot;hl-comment&quot;&gt;// (1.0 + 2.0i)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;i             &lt;span class=&quot;hl-comment&quot;&gt;// (3.0 - 1.0i)&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;z1 + z2 = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;z1 * z2 = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;z2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;|z1| = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, arg(z1) = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;z1.re = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, z1.im = &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;z1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;im&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;quadratic-formula&quot;&gt;Quadratic formula&lt;/h2&gt;
&lt;p&gt;The roots of &lt;span class=&quot;math inline&quot;&gt;\(a x^2 + b x + c = 0\)&lt;/span&gt; are&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[x \;=\; \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}.\]&lt;/div&gt;
&lt;p&gt;The sign of the discriminant &lt;span class=&quot;math inline&quot;&gt;\(\Delta = b^2 - 4ac\)&lt;/span&gt; decides whether the roots are real (&lt;span class=&quot;math inline&quot;&gt;\(\Delta \ge 0\)&lt;/span&gt;) or a complex-conjugate pair (&lt;span class=&quot;math inline&quot;&gt;\(\Delta &amp;lt; 0\)&lt;/span&gt;). The function below returns both roots as &lt;code&gt;complex&lt;/code&gt; regardless, so callers see a single result type.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-comment&quot;&gt;// Solve ax² + bx + c = 0; returns a pair of complex roots.&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// Each indented if-branch is a block; the block&apos;s last expression is&lt;/span&gt;
&lt;span class=&quot;hl-comment&quot;&gt;// a paren-less tuple — no wrapping parens are needed here.&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;solve_quadratic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;disc&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4&lt;/span&gt;a&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;disc&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sd&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;disc&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;i&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;i
  &lt;span class=&quot;hl-keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;real_part&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;imag_part&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;disc&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;hl-variable&quot;&gt;real_part&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;imag_part&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;real_part&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;imag_part&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;if&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;r2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;solve_quadratic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// x² - 3x + 2 = 0&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;real roots: &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;c2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;solve_quadratic&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// x² + 1 = 0&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;s&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;complex roots: &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;hl-string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;c2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the math-flavored juxtaposition: &lt;code&gt;4a*c&lt;/code&gt;, &lt;code&gt;(2a)&lt;/code&gt;. The &lt;code&gt;imag_part*i&lt;/code&gt; uses explicit &lt;code&gt;*&lt;/code&gt; because &lt;code&gt;imag_part&lt;/code&gt; is an identifier (no juxtaposition between two identifiers).&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Compile to a Native Binary</title>
    <link href="https://nexlang.org/getting-started/compile-to-native/"/>
    <id>https://nexlang.org/getting-started/compile-to-native/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Use the AOT compiler to produce a standalone executable; verify it matches the interpreter.</summary>
    <content type="html">&lt;h2 id=&quot;compile&quot;&gt;Compile&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;compile&lt;/code&gt; subcommand emits LLVM IR text and shells out to &lt;code&gt;clang -O1&lt;/code&gt; to produce a native binary:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run compile /path/to/hello.nex&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting binary lands next to the source, with the &lt;code&gt;.nex&lt;/code&gt; extension stripped:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/path/to/hello
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run it directly — no JVM, no sbt, no dependencies beyond the system C runtime:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;/path/to/hello
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output is identical to what the interpreter produced.&lt;/p&gt;
&lt;h2 id=&quot;verify-parity&quot;&gt;Verify parity&lt;/h2&gt;
&lt;p&gt;The intended way to use Nex during development is to write code, run it under the interpreter for the fast edit/run cycle, then occasionally compile to native to confirm the AOT path produces the same output. Every commit to the compiler runs the full unit-test suite (over two thousand tests on JVM today) plus an interpreter-vs-AOT parity sweep that compares the two paths byte-for-byte on a shared corpus. The compiled binary’s real-number printing matches Java’s &lt;code&gt;Double.toString&lt;/code&gt; — non-whole reals like &lt;code&gt;0.1 + 0.2&lt;/code&gt; print as &lt;code&gt;0.30000000000000004&lt;/code&gt;, magnitudes like &lt;code&gt;1e20&lt;/code&gt; print as &lt;code&gt;1.0E20&lt;/code&gt;, and runtime traps (assertions, array-index OOB, division by zero, slice OOB) all carry the same human-readable messages as the interpreter, written to stderr as &lt;code&gt;trap: &amp;lt;msg&amp;gt;\n&lt;/code&gt; with exit code 1, so &lt;code&gt;assert_traps(fn, substring)&lt;/code&gt; matches the same trap text on both sides and a shell pipeline that captures stderr sees identical bytes from either path.&lt;/p&gt;
&lt;h2 id=&quot;when-you-d-reach-for-which&quot;&gt;When you’d reach for which&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;run&lt;/code&gt;&lt;/strong&gt; while writing code. Sub-second turnaround. Same output as the binary for everything you care about.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;compile&lt;/code&gt;&lt;/strong&gt; when shipping, benchmarking, or producing a self-contained executable to share. The native binary is roughly an order of magnitude faster on most numerical workloads (no interpreter dispatch overhead, real LLVM optimization at &lt;code&gt;-O1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;test&lt;/code&gt;&lt;/strong&gt; to discover and run every &lt;code&gt;@test&lt;/code&gt;-annotated function in a project — see the &lt;a href=&quot;/tooling/01-cli/&quot;&gt;Tooling chapter&lt;/a&gt; for details and the &lt;a href=&quot;/examples/11-test-modules/&quot;&gt;Test modules example&lt;/a&gt; for the convention.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;next-step&quot;&gt;Next step&lt;/h2&gt;
&lt;p&gt;You’re done with the getting-started path. From here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/examples/&quot;&gt;Examples&lt;/a&gt; — a feature tour plus a dozen complete programs from hello-world through a recursive FFT.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/spec/&quot;&gt;Specification&lt;/a&gt; — the chapter-by-chapter language reference.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/tooling/&quot;&gt;Tooling&lt;/a&gt; — CLI details, verification model, supported targets.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>CLI</title>
    <link href="https://nexlang.org/tooling/cli/"/>
    <id>https://nexlang.org/tooling/cli/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>The `nex` subcommands — tokens, parse, elaborate, run, test, compile — and what each one does.</summary>
    <content type="html">&lt;h2 id=&quot;subcommands&quot;&gt;Subcommands&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nex tokens    &amp;lt;file&amp;gt;                # lex; print the token stream
nex parse     &amp;lt;file&amp;gt;                # lex + parse; print the AST
nex elaborate &amp;lt;file&amp;gt;                # parse + name resolution; print the typed AST
nex run       &amp;lt;file&amp;gt;                # parse, elaborate, execute via the tree-walking interpreter
nex test      &amp;lt;file&amp;gt;                # discover and run every @test function reachable from &amp;lt;file&amp;gt;&apos;s project root
nex compile [--backend B] &amp;lt;file&amp;gt;    # emit IR and invoke a backend toolchain to produce a native binary
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;file&amp;gt;&lt;/code&gt; is always a single &lt;code&gt;.nex&lt;/code&gt; source. For &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;compile&lt;/code&gt;, the directory containing &lt;code&gt;&amp;lt;file&amp;gt;&lt;/code&gt; is the project root — the import graph is discovered from there.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;--backend&lt;/code&gt; (only for &lt;code&gt;compile&lt;/code&gt;) selects the codegen pipeline:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;llvm&lt;/code&gt; (default) — emits LLVM IR text and invokes &lt;code&gt;clang -O1&lt;/code&gt;. Covers the full language surface.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mlir&lt;/code&gt; — emits MLIR via &lt;code&gt;linalg&lt;/code&gt; / &lt;code&gt;arith&lt;/code&gt; / &lt;code&gt;scf&lt;/code&gt; / &lt;code&gt;tensor&lt;/code&gt; / &lt;code&gt;func&lt;/code&gt; dialects and runs &lt;code&gt;mlir-opt&lt;/code&gt; → &lt;code&gt;mlir-translate&lt;/code&gt; → &lt;code&gt;clang -O1&lt;/code&gt;. &lt;strong&gt;Experimental&lt;/strong&gt; and growing on the strangler-fig pattern: scalar arithmetic (&lt;code&gt;+ - * / div % ^&lt;/code&gt;, unary &lt;code&gt;-&lt;/code&gt;), comparisons (scalar and array-element-wise, returning &lt;code&gt;[bool]&lt;/code&gt;) + bool + &lt;code&gt;and&lt;/code&gt; / &lt;code&gt;or&lt;/code&gt; / &lt;code&gt;not&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt; / &lt;code&gt;max&lt;/code&gt; / &lt;code&gt;abs&lt;/code&gt;, the libm transcendentals (&lt;code&gt;sqrt&lt;/code&gt; / &lt;code&gt;exp&lt;/code&gt; / &lt;code&gt;log&lt;/code&gt; / &lt;code&gt;sin&lt;/code&gt; / &lt;code&gt;cos&lt;/code&gt; / …), the explicit conversions &lt;code&gt;to_real&lt;/code&gt; / &lt;code&gt;to_integer&lt;/code&gt;, array literals + &lt;code&gt;length&lt;/code&gt; + indexing, array-array element-wise binops (matching element types, or mixed integer / real promoting up the lattice), scalar–array broadcasts (arithmetic &lt;em&gt;and&lt;/em&gt; comparison), and the &lt;code&gt;@&lt;/code&gt; operator (matmul, matvec, dot) all parity-check against the interpreter. Control flow lowers via the &lt;code&gt;scf&lt;/code&gt; dialect — scalar &lt;code&gt;if&lt;/code&gt;-expressions, statement-position &lt;code&gt;if&lt;/code&gt; / &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; (lowered to a no-result &lt;code&gt;scf.if&lt;/code&gt; with optional else region), &lt;code&gt;while cond do ...&lt;/code&gt; loops, and &lt;code&gt;for&lt;/code&gt; loops in both forms (&lt;code&gt;for i in lo..hi do ...&lt;/code&gt; over an integer range, &lt;code&gt;for x in xs do ...&lt;/code&gt; over a rank-1 array) are wired up. Mutable scalar &lt;code&gt;var&lt;/code&gt; bindings lower to &lt;code&gt;memref.alloca&lt;/code&gt; slots so a counter-style &lt;code&gt;var n = 0; while ... do n = n + 1&lt;/code&gt; works end-to-end. Mutable &lt;em&gt;tensor&lt;/em&gt; &lt;code&gt;var&lt;/code&gt; bindings rebind through fresh SSA via &lt;code&gt;tensor.insert&lt;/code&gt; / &lt;code&gt;tensor.insert_slice&lt;/code&gt; / &lt;code&gt;scf.for&lt;/code&gt; accumulators, covering whole-tensor reassign, scalar index-assign at rank-1 and rank-2 (negative-index wrap + OOB trap), rank-1 slice-assign (open bounds, inclusive, stride &amp;gt; 1), and rank-2 row/column replace (&lt;code&gt;m[i, :] = rhs&lt;/code&gt; / &lt;code&gt;m[:, j] = rhs&lt;/code&gt;); auto-clone insertions (spec §8.3) become emit-inner identities because SSA tensor semantics already diverge the bindings on the first mutation. Rank-1 and rank-2 slicing (&lt;code&gt;xs[2..5]&lt;/code&gt;, &lt;code&gt;m[:, 1]&lt;/code&gt;, &lt;code&gt;m[0..2, 1..3]&lt;/code&gt;, etc.) routes through &lt;code&gt;tensor.extract_slice&lt;/code&gt; for both literal &lt;em&gt;and&lt;/em&gt; runtime bounds, including the strided forms (&lt;code&gt;xs[0..n by 2]&lt;/code&gt;, &lt;code&gt;m[..r by k, c]&lt;/code&gt;) — the rank-reducing form drops a dimension whenever an axis collapses to a scalar index. Rank-1 &lt;code&gt;filter(arr, pred)&lt;/code&gt; and &lt;code&gt;flatMap(arr, x -&amp;gt; [...])&lt;/code&gt; lower to two-pass &lt;code&gt;tensor.empty&lt;/code&gt; + &lt;code&gt;tensor.insert&lt;/code&gt; builders so the output shape is sized exactly. Index-style traps match the interpreter: negative-index wrap on &lt;code&gt;TIndex&lt;/code&gt;, plus out-of-bounds traps on both indexing and slice bounds. The MLIR C runtime now carries the same &lt;code&gt;setjmp&lt;/code&gt;/&lt;code&gt;longjmp&lt;/code&gt; trap-catching infrastructure the LLVM backend has had since milestone 1 — a thread-local &lt;code&gt;nex_trap_buf&lt;/code&gt; plus a &lt;code&gt;nex_trap_with(msg)&lt;/code&gt; shim that longjmps when an &lt;code&gt;assert_traps&lt;/code&gt; is on the stack and aborts to stderr otherwise — so &lt;code&gt;assert&lt;/code&gt; lowers to &lt;code&gt;scf.if !cond { call nex_assert_failed }&lt;/code&gt; with the message extracted from the optional &lt;code&gt;nex_str&lt;/code&gt; at compile time, &lt;code&gt;assert_traps(fn, sub?)&lt;/code&gt; lowers to a &lt;code&gt;nex_assert_traps&lt;/code&gt; call that pulls &lt;code&gt;(fn_addr, env_addr)&lt;/code&gt; out of the closure descriptor via the existing &lt;code&gt;nex_closure_fn&lt;/code&gt; / &lt;code&gt;nex_closure_env&lt;/code&gt; helpers (with the thunk cast to &lt;code&gt;int64_t (*)(int64_t)&lt;/code&gt; so unit / int / pointer return types all flow), and integer &lt;code&gt;div&lt;/code&gt;/&lt;code&gt;%&lt;/code&gt;, real &lt;code&gt;/&lt;/code&gt;, and int-div-int-to-real &lt;code&gt;/&lt;/code&gt; get an &lt;code&gt;scf.if&lt;/code&gt;-guarded &lt;code&gt;nex_trap_int_div_zero&lt;/code&gt; check before the divide. &lt;code&gt;assert_traps&lt;/code&gt; cases that close over an array value inside the closure are still deferred until the tensor-capture closure ABI lands, and &lt;code&gt;assert_approx&lt;/code&gt; (rank-1, rank-2, and complex variants) is also deferred pending dedicated emitters. User-defined &lt;code&gt;def&lt;/code&gt; functions lower to &lt;code&gt;func.func @nex_user_&amp;lt;name&amp;gt;_&amp;lt;id&amp;gt;&lt;/code&gt; at module level — scalar (i64 / f64 / i1) and string parameters and return types are supported, plus tensor params / tensor returns (including tensor-returning &lt;code&gt;if&lt;/code&gt;-expressions) and unit-returning defs (&lt;code&gt;def scale(v: mut [real], k: real) = ...&lt;/code&gt;); recursion, mutual recursion, forward references, and prelude consts (&lt;code&gt;pi&lt;/code&gt; / &lt;code&gt;e&lt;/code&gt;) referenced from inside a user def all flow naturally because module-level ops in MLIR are order-independent. Value-returning defs with early &lt;code&gt;return&lt;/code&gt; inside a loop or branch are desugared to an equivalent &lt;code&gt;if&lt;/code&gt;-expression chain before codegen, so &lt;code&gt;return&lt;/code&gt; doesn’t need a dedicated MLIR shape. &lt;code&gt;mut&lt;/code&gt; scalar parameters lower to &lt;code&gt;memref&amp;lt;T&amp;gt;&lt;/code&gt; ref slots — the callee binds the param as a varSlot, reads and assignments flow through &lt;code&gt;memref.load&lt;/code&gt; / &lt;code&gt;memref.store&lt;/code&gt; on the same memref the caller’s slot uses, and the call site forwards its slot register directly so writes are visible on both sides without a copy. &lt;code&gt;mut&lt;/code&gt; rank-1 and rank-2 tensor parameters take a different shape: they are admitted as plain &lt;code&gt;tensor&amp;lt;...&amp;gt;&lt;/code&gt; by-value rather than boxed through &lt;code&gt;memref&amp;lt;tensor&amp;lt;...&amp;gt;&amp;gt;&lt;/code&gt;, and the callee seeds them into its &lt;code&gt;var&lt;/code&gt;-tensor map so the same index-assign / slice-assign machinery covers writes inside the function body. The elaborator’s &lt;code&gt;NexLifetime&lt;/code&gt; auto-clone pass (spec §8.3) wraps the call-site arg in a &lt;code&gt;TClone&lt;/code&gt; whenever the caller’s &lt;code&gt;var&lt;/code&gt; is read after the &lt;code&gt;mut&lt;/code&gt; call, so visible-aliasing cases are diverged at the language level before MLIR ever sees them. Compound assignment (&lt;code&gt;+= -= *= /= %=&lt;/code&gt;) rides the existing &lt;code&gt;var&lt;/code&gt;-slot and index-assign machinery for free, because the elaborator already desugars &lt;code&gt;n += k&lt;/code&gt; to &lt;code&gt;TAssign(n, n + k)&lt;/code&gt; before the backend sees it. Top-level &lt;code&gt;const X = ...&lt;/code&gt; inits flow through the same emitter as ordinary expressions, so &lt;code&gt;const X = sqrt(2.0)&lt;/code&gt; and recursive-&lt;code&gt;fact&lt;/code&gt; / &lt;code&gt;cos(pi/2)&lt;/code&gt; style initializers compile without a dedicated shape. Closures lower end-to-end: each &lt;code&gt;TLambda&lt;/code&gt; whose signature and captures fit i64-shaped LLVM-compatible slots becomes a synthetic top-level &lt;code&gt;llvm.func @nex_lambda_&amp;lt;N&amp;gt;(%env, args...)&lt;/code&gt;; free-var captures are classified ByVal (immutable) or ByRef (the source is a &lt;code&gt;var&lt;/code&gt;), and any &lt;code&gt;var&lt;/code&gt; referenced inside any lambda is “boxed” out of its parent’s &lt;code&gt;memref.alloca&lt;/code&gt; into a heap cell so the box outlives the parent frame and mutations through either side stay visible. Closure construction sites build a heap descriptor &lt;code&gt;{fn_ptr, env_ptr}&lt;/code&gt; via &lt;code&gt;llvm.mlir.addressof&lt;/code&gt; + &lt;code&gt;llvm.ptrtoint&lt;/code&gt;, the closure type lowers as an opaque &lt;code&gt;i64&lt;/code&gt;, and indirect call extracts &lt;code&gt;(fn, env)&lt;/code&gt; from the descriptor and dispatches through &lt;code&gt;llvm.call&lt;/code&gt;. Top-level &lt;code&gt;def&lt;/code&gt;s reached in value position (passed to a HOF, bound to a &lt;code&gt;val&lt;/code&gt;, returned) lower to a closure descriptor with a synthetic thunk function pointer and a null env, so a top-level def can be passed wherever a closure is expected. Arrays of closures work (&lt;code&gt;[f, g, h]&lt;/code&gt; and &lt;code&gt;for f in fs do f(x)&lt;/code&gt;), and &lt;code&gt;map(xs, f)&lt;/code&gt; accepts a val- or arg-bound closure value rather than only an inline lambda — the rank-1 driver extracts &lt;code&gt;(fn, env)&lt;/code&gt; once outside the &lt;code&gt;scf.for&lt;/code&gt; and dispatches per-iteration. &lt;code&gt;if&lt;/code&gt;-expressions can return string values (&lt;code&gt;if cond then &amp;quot;yes&amp;quot; else &amp;quot;no&amp;quot;&lt;/code&gt;) since the i64 string-descriptor encoding matches the closure-style boundary type the &lt;code&gt;scf.if&lt;/code&gt; regions already use. User-defined &lt;strong&gt;generic functions&lt;/strong&gt; (Spec &lt;a href=&quot;/spec/06-functions/#610-generic-functions&quot;&gt;§6.10&lt;/a&gt;) flow through to MLIR transparently: &lt;code&gt;NexMonomorphize&lt;/code&gt; runs in the elaborator before any backend sees the typed IR, so identity / numeric-constrained / ord-constrained / two-type-param / generic-lambda / overload-resolved call sites all reach codegen as concrete monomorphized defs without any MLIR-side dispatch logic. Structs and tuples lower to &lt;code&gt;!llvm.struct&amp;lt;(...)&amp;gt;&lt;/code&gt; from the LLVM dialect — the pass pipeline already runs &lt;code&gt;convert-func-to-llvm&lt;/code&gt;, so LLVM-dialect types flow through func-boundary signatures unchanged. Struct construction is an &lt;code&gt;llvm.mlir.undef&lt;/code&gt; of the struct type folded with &lt;code&gt;llvm.insertvalue&lt;/code&gt; per declared slot (int → real promoted at the slot boundary); field reads emit &lt;code&gt;llvm.extractvalue&lt;/code&gt; at the declared field index; nested-field writes (&lt;code&gt;o.i.v = 99&lt;/code&gt;) walk the &lt;code&gt;TField&lt;/code&gt; chain back to its &lt;code&gt;TVarRef&lt;/code&gt; root, recursively &lt;code&gt;extractvalue&lt;/code&gt; along the path, perform the leaf &lt;code&gt;insertvalue&lt;/code&gt;, then &lt;code&gt;insertvalue&lt;/code&gt; the updated sub-struct back out, with the final SSA rebound at the var. &lt;code&gt;var&lt;/code&gt;-bound structs and tuples track per-var SSA registers parallel to the &lt;code&gt;var&lt;/code&gt;-tensor scheme, so auto-clone falls out for free: &lt;code&gt;var c = b&lt;/code&gt; copies the SSA register but the first write through either binding produces a fresh value, leaving the other binding unaffected. Tuples are the positional twin (no field names); &lt;code&gt;TTupleProj&lt;/code&gt; lowers to &lt;code&gt;extractvalue&lt;/code&gt; and the &lt;code&gt;print(tuple)&lt;/code&gt; / &lt;code&gt;s&amp;quot;$tup&amp;quot;&lt;/code&gt; paths share an &lt;code&gt;emitTupleToString&lt;/code&gt; formatter. Tuple-returning and struct-returning &lt;code&gt;if&lt;/code&gt;-expressions flow through the typed-if scaffold via parallel &lt;code&gt;MTuple&lt;/code&gt; / &lt;code&gt;MStruct&lt;/code&gt; arms. Scalar &lt;strong&gt;&lt;code&gt;complex&lt;/code&gt;&lt;/strong&gt; values lower as &lt;code&gt;!llvm.struct&amp;lt;(f64, f64)&amp;gt;&lt;/code&gt; from the same LLVM-dialect path — a pack/unpack helper pair feeds per-component lowerings for &lt;code&gt;+ - * /&lt;/code&gt; (with a zero-denominator trap on &lt;code&gt;/&lt;/code&gt;), equality and unary minus, the field projections &lt;code&gt;.re&lt;/code&gt; and &lt;code&gt;.im&lt;/code&gt;, the &lt;code&gt;to_complex&lt;/code&gt; conversion, the modulus &lt;code&gt;|z|&lt;/code&gt;, complex-arm &lt;code&gt;if&lt;/code&gt;-expressions, and &lt;code&gt;print&lt;/code&gt; / &lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt; formatting; a branchless &lt;code&gt;sign(x)&lt;/code&gt; built-in is along for the ride and unlocks the prelude’s source-defined complex extensions for &lt;code&gt;sqrt&lt;/code&gt; / &lt;code&gt;exp&lt;/code&gt; / &lt;code&gt;log&lt;/code&gt; / &lt;code&gt;sin&lt;/code&gt; / &lt;code&gt;cos&lt;/code&gt; / &lt;code&gt;tan&lt;/code&gt;, which lower through the same monomorphized-&lt;code&gt;def&lt;/code&gt; machinery as any other prelude function. User-defined &lt;strong&gt;generic structs&lt;/strong&gt; (Spec &lt;a href=&quot;/spec/03-types/#36-generic-structs&quot;&gt;§3.6&lt;/a&gt;) flip transparently for the same monomorphization reason — &lt;code&gt;Box[T]&lt;/code&gt;, &lt;code&gt;Pair[A, B]&lt;/code&gt;, nested generic structs, generic-into-generic-fn, ord-constrained generic struct, explicit-annotation construction, and generic-struct field write all reach codegen as concrete &lt;code&gt;MStruct(...)&lt;/code&gt; lowerings. String values are represented as opaque i64 descriptor pointers — literals share a module-level &lt;code&gt;memref.global&lt;/code&gt; pool, the runtime ships refcounted &lt;code&gt;concat&lt;/code&gt; / &lt;code&gt;eq&lt;/code&gt; / &lt;code&gt;from_i64&lt;/code&gt; / &lt;code&gt;from_bool&lt;/code&gt; / &lt;code&gt;from_double&lt;/code&gt; helpers, and refcount discipline (–1 immortal for literals, +1 heap for concat / value-to-string results, dec on every consume site) mirrors LLVM exactly; &lt;code&gt;print(&amp;quot;...&amp;quot;)&lt;/code&gt;, string &lt;code&gt;==&lt;/code&gt; / &lt;code&gt;!=&lt;/code&gt; / &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt; interpolation with &lt;code&gt;integer&lt;/code&gt; / &lt;code&gt;bool&lt;/code&gt; / &lt;code&gt;real&lt;/code&gt;, and &lt;code&gt;f&amp;quot;...&amp;quot;&lt;/code&gt; format-spec values (&lt;code&gt;%d&lt;/code&gt; / &lt;code&gt;%f&lt;/code&gt; / &lt;code&gt;%e&lt;/code&gt; / &lt;code&gt;%g&lt;/code&gt; / &lt;code&gt;%x&lt;/code&gt; / &lt;code&gt;%X&lt;/code&gt; / &lt;code&gt;%o&lt;/code&gt; / &lt;code&gt;%b&lt;/code&gt; with width / &lt;code&gt;0&lt;/code&gt; / &lt;code&gt;-&lt;/code&gt; flags and &lt;code&gt;.precision&lt;/code&gt;) all parity-check against the interpreter, and the real-value branch uses a shortest-round-trip helper that matches Java’s &lt;code&gt;Double.toString&lt;/code&gt; byte-for-byte. The array builders &lt;code&gt;range&lt;/code&gt; / &lt;code&gt;zeros&lt;/code&gt; / &lt;code&gt;ones&lt;/code&gt; / &lt;code&gt;linspace&lt;/code&gt; plus &lt;code&gt;transpose&lt;/code&gt; round out the rank-1 / rank-2 surface. The corpus opts cases in one at a time via a per-case &lt;code&gt;mlir = true&lt;/code&gt; flag in &lt;code&gt;NexProgramCorpus&lt;/code&gt; — see &lt;code&gt;NexCorpusMlirParityTests&lt;/code&gt; for what’s currently covered. User-defined &lt;strong&gt;enums&lt;/strong&gt; (Spec &lt;a href=&quot;/spec/03-types/#37-sum-types&quot;&gt;§3.7&lt;/a&gt; / &lt;a href=&quot;/spec/03-types/#38-generic-enums&quot;&gt;§3.8&lt;/a&gt;) lower as tagged unions &lt;code&gt;!llvm.struct&amp;lt;(i32, !llvm.array&amp;lt;N x i64&amp;gt;)&amp;gt;&lt;/code&gt; where &lt;code&gt;N&lt;/code&gt; is the max field count across variants and each field occupies one i64 payload slot — int as identity, real ↔ i64 via &lt;code&gt;llvm.bitcast&lt;/code&gt;, bool ↔ i64 via &lt;code&gt;llvm.zext&lt;/code&gt; / &lt;code&gt;llvm.trunc&lt;/code&gt;, string as the i64 descriptor it already is. &lt;code&gt;match&lt;/code&gt; walks arms in source order as a chain of &lt;code&gt;scf.if -&amp;gt; (T)&lt;/code&gt; (or no-result &lt;code&gt;scf.if&lt;/code&gt; for unit-typed arms), each variant pattern testing &lt;code&gt;arith.cmpi eq %tag, idx&lt;/code&gt; and binding sub-pattern fields by &lt;code&gt;llvm.extractvalue [1, i]&lt;/code&gt; + per-type decode; var- and wildcard-patterns terminate as the default arm. A per-enum &lt;code&gt;func.func @nex_enum_str_&amp;lt;Name&amp;gt;&lt;/code&gt; flushed after &lt;code&gt;@main&lt;/code&gt; powers &lt;code&gt;print(enum)&lt;/code&gt; and &lt;code&gt;s&amp;quot;${enum}&amp;quot;&lt;/code&gt;. &lt;strong&gt;Generic enums&lt;/strong&gt; (&lt;code&gt;Opt[T]&lt;/code&gt;, &lt;code&gt;Result[T, E]&lt;/code&gt;, ord-constrained generic enum) flip transparently for the same monomorphization reason generic structs already do. Aggregate field types inside a variant (struct- or tensor-typed fields) stay rejected at codegen — a separate lowering sub-project. Arrays of structs are also deferred (a separate struct-element tensor sub-project). The elaborator’s &lt;code&gt;NexFusion&lt;/code&gt; pass is now wired into the LLVM compile path (so &lt;code&gt;2 * a + b&lt;/code&gt; lowers to a single fused loop on the default backend), but is intentionally &lt;strong&gt;not&lt;/strong&gt; run before MLIR codegen yet — the MLIR &lt;code&gt;TFusedLoop&lt;/code&gt; lowering only covers rank-1 fused loops with statically-known lengths, so any program reaching MLIR with fused IR (a broadcast inside a &lt;code&gt;def&lt;/code&gt;, any rank-2 fusion) would raise the “not yet supported” diagnostic. Picking those up is a strangler-fig follow-up. Anything outside the opted-in surface raises a compile-time “not yet supported” diagnostic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;locating-the-source-prelude&quot;&gt;Locating the source prelude&lt;/h2&gt;
&lt;p&gt;The scalar transcendentals plus their complex extensions live in a &lt;code&gt;prelude/*.nex&lt;/code&gt; directory and are auto-imported on every elaboration (see &lt;a href=&quot;/spec/10-prelude/&quot;&gt;§10 of the spec&lt;/a&gt;). The CLI locates that directory at startup; the precedence order is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;--prelude-path &amp;lt;dir&amp;gt;&lt;/code&gt; — an explicit CLI flag (highest priority).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NEX_HOME&lt;/code&gt; environment variable — looks at &lt;code&gt;$NEX_HOME/lib/prelude/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Dnex.prelude.path=&amp;lt;dir&amp;gt;&lt;/code&gt; JVM system property. &lt;code&gt;build.sbt&lt;/code&gt; sets this automatically so &lt;code&gt;sbt run&lt;/code&gt; and &lt;code&gt;sbt test&lt;/code&gt; use the in-repo &lt;code&gt;prelude/&lt;/code&gt; with no extra wiring.&lt;/li&gt;
&lt;li&gt;Walk up from the current working directory — at each ancestor, check for a &lt;code&gt;prelude/&lt;/code&gt; sibling and then &lt;code&gt;lib/prelude/&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If none of the four locate a directory, the compiler falls back to the compiler-built-in name table and proceeds silently. This was the design during the Stage 1/2 migration and remains the fallback today for environments without a sysroot.&lt;/p&gt;
&lt;h2 id=&quot;running-the-cli-from-sbt&quot;&gt;Running the CLI from sbt&lt;/h2&gt;
&lt;p&gt;There is no shipped binary yet; the canonical entry point is sbt’s &lt;code&gt;runMain&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run &amp;lt;subcommand&amp;gt; &amp;lt;file&amp;gt;&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run run     examples/fft/main.nex&amp;quot;
sbt &amp;quot;nexJVM/runMain io.github.edadma.nex.run compile examples/fft/main.nex&amp;quot;
./examples/fft/main
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;run&lt;/code&gt; is also a published &lt;code&gt;@main&lt;/code&gt; entry point on the JVM JAR — once packaged, &lt;code&gt;java -jar nex.jar &amp;lt;subcommand&amp;gt; &amp;lt;file&amp;gt;&lt;/code&gt; does the same thing.&lt;/p&gt;
&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JVM 17+&lt;/strong&gt; to invoke the CLI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;clang&lt;/code&gt; on &lt;code&gt;$PATH&lt;/code&gt;&lt;/strong&gt; for the &lt;code&gt;compile&lt;/code&gt; subcommand. The compiler writes LLVM IR text to a temp file and shells out to &lt;code&gt;clang -O1&lt;/code&gt; to produce a native binary.&lt;/li&gt;
&lt;li&gt;No other native dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-each-subcommand-prints&quot;&gt;What each subcommand prints&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;tokens&lt;/code&gt; — one token per line, with source position.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;parse&lt;/code&gt; — the untyped AST as a pretty-printed tree.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;elaborate&lt;/code&gt; — the elaborated (typed, name-resolved, lowered) AST. Useful for debugging type inference or fusion-pass output.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;run&lt;/code&gt; — the program’s &lt;code&gt;stdout&lt;/code&gt; (whatever it &lt;code&gt;print&lt;/code&gt;s); exit status reflects whether a trap occurred.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;test&lt;/code&gt; — one line per discovered test, with a pass/fail marker; a summary at the bottom; non-zero exit on any failure.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;compile&lt;/code&gt; — diagnostics during code generation, then either a native binary at &lt;code&gt;&amp;lt;file-without-extension&amp;gt;&lt;/code&gt; or a non-zero exit on error.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>Bindings</title>
    <link href="https://nexlang.org/spec/bindings/"/>
    <id>https://nexlang.org/spec/bindings/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>const, val, and var — what they bind, when they&apos;re evaluated, scoping and shadowing.</summary>
    <content type="html">&lt;p&gt;Nex has three binding kinds: &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;val&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt;. All three may appear at any scope (module level or inside a function/block). They differ in &lt;em&gt;what&lt;/em&gt; they bind:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Keyword&lt;/th&gt;&lt;th&gt;Mutability&lt;/th&gt;&lt;th&gt;Storage&lt;/th&gt;&lt;th&gt;Evaluated&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;const&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Immutable&lt;/td&gt;&lt;td&gt;None (may be inlined)&lt;/td&gt;&lt;td&gt;Compile time&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;val&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Immutable&lt;/td&gt;&lt;td&gt;Has storage&lt;/td&gt;&lt;td&gt;Runtime, at point of declaration&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;var&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Mutable&lt;/td&gt;&lt;td&gt;Has storage&lt;/td&gt;&lt;td&gt;Runtime, at point of declaration&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;5-1-val-bindings&quot;&gt;5.1 &lt;code&gt;val&lt;/code&gt; bindings&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;val&lt;/code&gt; binding introduces an immutable binding to a value. The bound name cannot be reassigned, and (for array and struct types) the bound value’s contents cannot be mutated through this binding. The right-hand side may be any expression, evaluated once at the point of declaration (module load, function entry, or block entry).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// RHS may include function calls&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Type annotation is optional and inferred from the initializer when omitted:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;integer&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[]&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// empty literals require an annotation&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For &lt;code&gt;val&lt;/code&gt; arrays: contents are immutable, the buffer may be freely shared, no uniqueness constraint applies (see chapter 8).&lt;/p&gt;
&lt;h2 id=&quot;5-2-var-bindings&quot;&gt;5.2 &lt;code&gt;var&lt;/code&gt; bindings&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;var&lt;/code&gt; binding introduces a mutable binding. For scalar types, the binding may be reassigned. For array types and struct types, the contents may also be mutated (via &lt;code&gt;a[i] = ...&lt;/code&gt; or &lt;code&gt;s.field = ...&lt;/code&gt;). The right-hand side may be any expression.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// ok&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;               &lt;span class=&quot;hl-comment&quot;&gt;// ok: mutate contents&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// ok: rebind&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Module-level &lt;code&gt;var&lt;/code&gt;s are permitted (for caches, RNG state, configuration set at startup) but should be used sparingly — most program state belongs in function-local bindings or in &lt;code&gt;const&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;var&lt;/code&gt; arrays are subject to the uniqueness rule (chapter 8).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Compound assignment.&lt;/strong&gt; The four arithmetic ops &lt;code&gt;+ - * /&lt;/code&gt; and the integer &lt;code&gt;%&lt;/code&gt; op pair with &lt;code&gt;=&lt;/code&gt; to form &lt;code&gt;+=&lt;/code&gt;, &lt;code&gt;-=&lt;/code&gt;, &lt;code&gt;*=&lt;/code&gt;, &lt;code&gt;/=&lt;/code&gt;, &lt;code&gt;%=&lt;/code&gt;. Each is desugar-equivalent to evaluating the binop against the current value of the l-value and assigning the result back:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// identical to: n = n + 5&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// index target&lt;/span&gt;
&lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3&lt;/span&gt;       &lt;span class=&quot;hl-comment&quot;&gt;// field target&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The l-value must be a &lt;code&gt;var&lt;/code&gt;, a field of a &lt;code&gt;var&lt;/code&gt;, or an index into a &lt;code&gt;var&lt;/code&gt; array (the same set of forms &lt;code&gt;=&lt;/code&gt; already accepts). The right-hand side is parsed as a full expression — &lt;code&gt;n += 2 * 3 + 4&lt;/code&gt; is &lt;code&gt;n = n + (2 * 3 + 4)&lt;/code&gt;. Compound assignment is a &lt;em&gt;statement form&lt;/em&gt;: it only appears at block-item position, never inside an expression. There is no &lt;code&gt;^=&lt;/code&gt;, no &lt;code&gt;div=&lt;/code&gt;, and no boolean / bitwise compound (&lt;code&gt;and=&lt;/code&gt;, &lt;code&gt;or=&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&quot;5-3-const-bindings&quot;&gt;5.3 &lt;code&gt;const&lt;/code&gt; bindings&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;const&lt;/code&gt; binding introduces a compile-time constant. The right-hand side must be a &lt;em&gt;constant expression&lt;/em&gt; — evaluable entirely at compile time. The compiler may inline the value at use sites; there is no guaranteed storage location.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;PI&lt;/span&gt;       &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.14159265358979&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;TAU&lt;/span&gt;      &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;PI&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// arithmetic on consts is constant&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;MAX_ITER&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;ENABLED&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;em&gt;constant expression&lt;/em&gt; is one of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A numeric or boolean literal&lt;/li&gt;
&lt;li&gt;An application of &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, juxtaposition, &lt;code&gt;not&lt;/code&gt;, &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, or comparison operators to constant expressions&lt;/li&gt;
&lt;li&gt;A reference to another &lt;code&gt;const&lt;/code&gt; binding&lt;/li&gt;
&lt;li&gt;A reference to a prelude constant (&lt;code&gt;pi&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, &lt;code&gt;inf&lt;/code&gt;, &lt;code&gt;nan&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Unary &lt;code&gt;-&lt;/code&gt; of a constant expression&lt;/li&gt;
&lt;li&gt;A call to a &lt;em&gt;pure function&lt;/em&gt; (prelude or user-defined) whose every argument is itself a constant expression and whose return type is scalar (&lt;code&gt;integer&lt;/code&gt;, &lt;code&gt;real&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, or &lt;code&gt;complex&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A function is &lt;em&gt;pure&lt;/em&gt; when it has no observable side effects: no I/O, no mutation of &lt;code&gt;var&lt;/code&gt; bindings, and every function it calls is itself pure. Prelude math functions (&lt;code&gt;sqrt&lt;/code&gt;, &lt;code&gt;sin&lt;/code&gt;, &lt;code&gt;cos&lt;/code&gt;, &lt;code&gt;abs&lt;/code&gt;, &lt;code&gt;hypot&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, &lt;code&gt;floor&lt;/code&gt;, &lt;code&gt;ceil&lt;/code&gt;, &lt;code&gt;round&lt;/code&gt;, &lt;code&gt;trunc&lt;/code&gt;, the conversions, the complex helpers, …) are pure; &lt;code&gt;print&lt;/code&gt; and the &lt;code&gt;assert_*&lt;/code&gt; family are impure (they emit output or trap).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;SQRT_2&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;hl-comment&quot;&gt;// OK — sqrt is pure&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;HYP&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;hypot&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;hl-comment&quot;&gt;// OK — 5.0&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;TWO_PI&lt;/span&gt;  &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;pi&lt;/span&gt;                &lt;span class=&quot;hl-comment&quot;&gt;// OK — arithmetic on prelude const&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;NINE&lt;/span&gt;    &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;hl-comment&quot;&gt;// OK — square is pure&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;BAD&lt;/span&gt;     &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;hl-comment&quot;&gt;// ERROR — print is impure&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two practical restrictions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The return type of a forward-referenced user function (one declared &lt;em&gt;after&lt;/em&gt; the const) must be written explicitly. Otherwise the const’s static type can’t be resolved before the function’s body is type-inferred. Move the &lt;code&gt;def&lt;/code&gt; above the const or add a declared return type.&lt;/li&gt;
&lt;li&gt;The call result must be a scalar — &lt;code&gt;[T]&lt;/code&gt;, tuples, structs, and strings escape the inline-friendly model. A pure helper that returns an array can still be used inside a normal &lt;code&gt;val&lt;/code&gt; binding.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For runtime-computed module-level values that aren’t compile-time constants (a call that needs an array argument, a struct construction, an &lt;code&gt;if&lt;/code&gt;), use &lt;code&gt;val&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;SAMPLES&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;hl-comment&quot;&gt;// construction → must be val&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prefer &lt;code&gt;const&lt;/code&gt; over &lt;code&gt;val&lt;/code&gt; whenever the value is a true compile-time fact (mathematical constants, physical constants, configuration flags, fixed tolerances). It signals intent and lets the compiler inline at use sites.&lt;/p&gt;
&lt;h2 id=&quot;5-4-scoping&quot;&gt;5.4 Scoping&lt;/h2&gt;
&lt;p&gt;Bindings are lexically scoped. A binding is visible from its declaration to the end of the enclosing block.&lt;/p&gt;
&lt;h2 id=&quot;5-5-shadowing&quot;&gt;5.5 Shadowing&lt;/h2&gt;
&lt;p&gt;A new binding may shadow an earlier binding with the same name in an enclosing scope. Shadowing within the same block is not permitted.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;                  &lt;span class=&quot;hl-comment&quot;&gt;// ok: shadows outer x in foo&apos;s scope&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;                    &lt;span class=&quot;hl-comment&quot;&gt;// error: redeclaration in same scope&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Arrays and Structs</title>
    <link href="https://nexlang.org/examples/arrays/"/>
    <id>https://nexlang.org/examples/arrays/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Element-wise array operations that fuse to one loop, and a small struct with a function over it.</summary>
    <content type="html">&lt;h2 id=&quot;element-wise-array-operations-the-fusion-sweet-spot&quot;&gt;Element-wise array operations (the fusion sweet spot)&lt;/h2&gt;
&lt;p&gt;For arrays &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; of the same length, an element-wise expression like&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[r_i \;=\; 2 a_i + b_i - 1 \qquad i = 0, \ldots, n-1\]&lt;/div&gt;
&lt;p&gt;compiles to a single loop — no temporary array for &lt;code&gt;2a&lt;/code&gt;, no separate &lt;code&gt;+ b&lt;/code&gt;, no separate &lt;code&gt;- 1.0&lt;/code&gt;. The whole expression is the fusion unit.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;20.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;30.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;40.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;50.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;hl-comment&quot;&gt;// Fuses to a single loop, no intermediate arrays:&lt;/span&gt;
  &lt;span class=&quot;hl-comment&quot;&gt;//   for i in 0..5: result[i] = 2 * a[i] + b[i] - 1.0&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;a &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;1.0&lt;/span&gt;

  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;hl-comment&quot;&gt;// [11.0, 23.0, 35.0, 47.0, 59.0]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;a-struct-and-a-function-over-it&quot;&gt;A struct and a function over it&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;Point&lt;/code&gt; is a record of two reals, and &lt;code&gt;distance&lt;/code&gt; is the Euclidean norm of the difference,&lt;/p&gt;
&lt;div class=&quot;math display&quot;&gt;\[d(p_1, p_2) \;=\; \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}.\]&lt;/div&gt;
&lt;p&gt;The function reads back as one line per term in the formula.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nex&quot;&gt;&lt;span class=&quot;hl-keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;real&lt;/span&gt;
&lt;span class=&quot;hl-keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;dx&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;dy&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;y&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;dx&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;hl-keyword&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hl-keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl-function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;origin&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;hl-keyword&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;hl-type&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;hl-variable&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hl-variable&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;hl-variable&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;hl-punctuation&quot;&gt;))&lt;/span&gt;        &lt;span class=&quot;hl-comment&quot;&gt;// 5.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <title>Appendix B: Open Design Questions</title>
    <link href="https://nexlang.org/spec/appendix-b-open-questions/"/>
    <id>https://nexlang.org/spec/appendix-b-open-questions/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Small items not yet pinned to a single answer in the reference.</summary>
    <content type="html">&lt;p&gt;The original draft’s question list has been mostly settled during implementation. Items that remain open:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Whether unused &lt;code&gt;val&lt;/code&gt; bindings produce a warning (currently silent — the compiler does not warn).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;resolved&quot;&gt;Resolved&lt;/h2&gt;
&lt;p&gt;The following items from the original draft have been resolved by the implementation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Shape-mismatch traps include source location.&lt;/strong&gt; Element-wise rank-1 and rank-2 operations trap with a message such as &lt;code&gt;element-wise &apos;+&apos;: shape mismatch&lt;/code&gt; plus the source position of the operator. Rank-1 length mismatches in &lt;code&gt;dot&lt;/code&gt; and similar prelude functions include both lengths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;for x in arr&lt;/code&gt; iterates by value.&lt;/strong&gt; Each loop iteration binds &lt;code&gt;x&lt;/code&gt; to a copy of the next element. This matches the value-semantics design of the language and avoids a “borrow that escapes the loop” footgun.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String formatting via f-strings.&lt;/strong&gt; Scala-style &lt;code&gt;f&amp;quot;...&amp;quot;&lt;/code&gt; (sibling of &lt;code&gt;s&amp;quot;...&amp;quot;&lt;/code&gt;) covers width, precision, alignment, padding, hex / octal / binary conversions, and the &lt;code&gt;%%&lt;/code&gt;/&lt;code&gt;$$&lt;/code&gt; escapes — replacing the earlier deferred &lt;code&gt;format()&lt;/code&gt; builtin. See spec §10.6.&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
  <entry>
    <title>Appendix A: Naming Conventions</title>
    <link href="https://nexlang.org/spec/appendix-a-naming/"/>
    <id>https://nexlang.org/spec/appendix-a-naming/</id>
    <updated>2026-05-22T19:26:20.099529896Z</updated>
    <summary>Recommended (but not enforced) conventions for modules, types, functions, and constants.</summary>
    <content type="html">&lt;p&gt;The following naming conventions are recommended but not enforced:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Module and file names: lowercase, underscore-separated (&lt;code&gt;linalg&lt;/code&gt;, &lt;code&gt;dense_matrix&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Type names (structs): UpperCamelCase (&lt;code&gt;Point&lt;/code&gt;, &lt;code&gt;LineSegment&lt;/code&gt;, &lt;code&gt;Matrix&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Function and binding names: lowercase, underscore-separated (&lt;code&gt;compute_norm&lt;/code&gt;, &lt;code&gt;dense_solve&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Constants: UPPER_SNAKE_CASE (&lt;code&gt;PI&lt;/code&gt;, &lt;code&gt;MAX_ITERATIONS&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
</feed>
