Skip to main content

6  Operators, Sections & Underscore

Edva uses Pratt parsing to resolve operator precedence and associativity. This chapter specifies all operator tiers, comparison and range semantics, operator and field-accessor sections, and the scoping rules of the implicit placeholder _ and cycle index _i.

6.1 Precedence and binding power table

Tier LBP RBP Operators Associativity
cycle 10 20 @ @@ @> @@> >-- --> --^ left
sequence 20 30 ; left
apply 30 30 $ right
feed 40 50 $> $>> $+> $-> .> left
choice 50 60 | [pattern] !| |-> |+> |--> |++> left
assignment 60 50 = := += ::= right
range 70 80 .. left
logical or 80 90 || left
logical and 90 100 && left
bitwise or 100 105 .|. left
bitwise xor 105 110 .^. left
bitwise and 110 120 .&. left
comparison 120 130 < > == != !< !> left, same-line
shift 130 140 .<. .>. none
addition 140 150 + - left
multiplication 150 160 * / % left
unary prefix 160 180 ! + & ++ .!. ~~ ? ?? -x #size right
postfix / apply 170 180 call () . :: : cast {N} @[...] .expect("reason") --! left
primary 180 180 primary left
Parsing Rule Behavior
Left-associative tiers (RBP > LBP) Operator binds tightly on the left (a - b - c \(\equiv\) (a - b) - c)
Right-associative tiers (RBP <= LBP) Operator extends rightward (f $ g $ x \(\equiv\) f(g(x)))
Non-associative shifts (.<., .>.) Chaining shifts without parentheses (a .<. b .>. c) is rejected with E2025; parenthesize explicitly ((a .<. b) .>. c)
Choice absorption ($ / feed tier) $ (LBP 30) and feed-tier operators ($>, $>>, $+>, $->, .>, LBP 40) sit below choice (LBP 50), absorbing unparenthesized RHS choices (a $> b | c | d \(\equiv\) a $> (b | c | d))
Multiline pipeline continuation Indented lines beginning with $>, $>>, $+>, $->, or .> continue the preceding expression
Operator sections (op rhs) 13 binary operators with a leading operand omitted and mandatory trailing space form a 1-arg function over _ with RBP 130

6.2 Relational, equality, and chained comparisons

Comparisons sit at LBP 120, RBP 130 and return a bare flag <> (<+> or <->). Edva writes greater-or-equal as !< (“not less”) and less-or-equal as !> (“not greater”); <= and >= are not comparison operators (=> is the function arrow).

Operator Meaning Valid Operand Types Floating-Point / Special Semantics
< Less than Numeric, Rune, String, Addr Ordered (OLT); <-> if either operand is NaN. Rejected on rams/enums (E3127)
> Greater than Numeric, Rune, String, Addr Ordered (OGT); <-> if either operand is NaN. Rejected on rams/enums (E3127)
!< Greater than or equal (\(\ge\)) Numeric, Rune, String, Addr Ordered (OGE); <-> if either operand is NaN (distinct from !(a < b))
!> Less than or equal (\(\le\)) Numeric, Rune, String, Addr Ordered (OLE); <-> if either operand is NaN (distinct from !(a > b))
== Equal Scalars, String, records, enums, Type, two bare Flags <> Deep structural equality on records and enums; on two Flags, logical XNOR (E3027 on other rams, E3084 on fn payloads); zero-alloc segment check on str == (p1, p2, ...)
!= Not equal Scalars, String, records, enums, Type, two bare Flags <> Logical negation of == (UNE on floats: <+> if either operand is NaN); on two Flags, logical XOR

6.2.1 Chained comparisons

Relational operators (<, >, !<, !>) on the same source line chain mathematically, evaluating interior operands at most once:

Chained Expression Equivalent Short-Circuit Lowering Middle Operand Evaluation
a < x < b t = x; (a < t) && (t < b) Evaluated at most once
a !> x < b t = x; (a !> t) && (t < b) Evaluated at most once
a !> x !> b t = x; (a !> t) && (t !> b) Evaluated at most once
b > f(x) > a t = f(x); (b > t) && (t > a) f(x) called at most once
#use io

in_half_open: Int, Int, Int => <>
in_half_open = x, lo, hi => lo !> x < hi

io::out $ in_half_open(5, 0, 10)   // <+>
io::out $ in_half_open(10, 0, 10)  // <->

6.3 Range operators (.., ..., ..=)

Range operators sit at LBP 70, RBP 80. Endpoints must be adjacent to the operator without whitespace (E2025). Ranges are consumed by choice guards [...], cycles @, and slice/substring calls x(...):

Operator Interval Kind Math Notation Lower Bound a Upper Bound b Choice Guard [...] Cycle @ Slice / View x(...)
a..b Half-open (left-closed, right-open) \([a, b)\) Closed (\(a \le x\)) Open (\(x < b\)) a !> x < b a through b - 1 Elements [a, b)
a...b / a..=b Closed (both inclusive) \([a, b]\) Closed (\(a \le x\)) Closed (\(x \le b\)) a !> x !> b a through b Elements [a, b]
a.. Lower-bounded closed \([a, \infty)\) Closed (\(a \le x\)) Unbounded x !< a Unbounded from a From a to ?x (-k.. from ?x - k)
..b Upper-bounded open \((-\infty, b)\) Unbounded Open (\(x < b\)) x < b 0 through b - 1 Elements [0, b)
...b / ..=b Upper-bounded closed \((-\infty, b]\) Unbounded Closed (\(x \le b\)) x !> b 0 through b Elements [0, b]
.. Unbounded \((-\infty, \infty)\) Unbounded Unbounded <+> Unbounded from 0 Full view [0, ?x)

6.4 Arithmetic, bitwise, shift, logical, and map operators

Category Operators Operand Types Result Type & Semantics
Arithmetic + - * / % Numeric (Int, Float, i8..u64, f32, SIMD vectors) Same numeric type (+ also concatenates String, Rune, and Builder)
Bitwise .|. .^. .&. .!. Integerish (Int, i8..u64, integer SIMD vectors) Same integer type (.!. is unary bitwise NOT)
Bit shift .<. .>. Integerish Non-associative left shift (.<.) and right shift (.>.); unparenthesized chaining emits E2025; sections infer Int => Int
Logical && || <> (Flag) or compatible heap ramifications Short-circuiting AND / OR
Map access m ^ k
m ^? k
m ^- k
Map K ^ V, key K ^: < V | Error > lookup (m ^ k = v inserts)
^?: <> key existence check
^-: <> key removal
Pipeline & composition $> $>>
$+> $->
.>
Any value, ramification, or function stage $>: feed (x $> f(a) \(\equiv\) f(x, a))
$>>: monadic ram bind
$+> / $->: positive / negative ram functor map
.>: left-to-right function composition ((f .> g)(x) \(\equiv\) g(f(x)))

6.5 Operator sections and field-accessor sections

An operator section writes a 1-argument function value => value op rhs by omitting the left operand of a supported binary operator. A field-accessor section writes a 1-argument projection r => r.field using a leading dot inside parentheses.

6.5.1 Section syntax and rules

Section Form Syntax Desugaring Rules & Diagnostics
Additive section (+ rhs), (- rhs) _ => _ + rhs, _ => _ - rhs Mandatory space after operator (-3 is negative literal; +b is Builder freeze)
Multiplicative section (* rhs), (/ rhs), (% rhs) _ => _ * rhs, _ => _ / rhs, _ => _ % rhs RHS binds with RBP 130 (* 2 + 3 \(\equiv\) _ => _ * (2 + 3))
Comparison section (< rhs), (> rhs), (== rhs) _ => _ < rhs, _ => _ > rhs, _ => _ == rhs Missing space (>3) emits E2031. !=, !<, !> do not form sections; use (_ != rhs)
Shift section (.<. rhs), (.>. rhs) _ => _ .<. rhs, _ => _ .>. rhs Automatically infers Int => Int when unannotated
Bitwise section (.&. rhs), (.^. rhs), (.|. rhs) _ => _ .&. rhs, etc. Since bitwise LBP < 130, parenthesize bitwise RHS inside arithmetic sections (+ (3 .&. 1))
Excluded operators ||, &&, $, $>, $>>, !=, !<, !> — Do not form sections; write (_ != 0) or x => ...
Field-accessor section (.field)
(.0)
(.outer.inner)
r => r.field
r => r.0
r => r.outer.inner
Must be parenthesized ((.) or bare .field emits E2064). Uses a fresh internal parameter, so it never shadows an enclosing _
#use io
#use fp

#type User (name: String, age: Int)

double = * 2
shl1 = .<. 1
get_age: User => Int
get_age = (.age)

xs = Int{}
xs += 3
xs += 5
mapped = fp::map(double, xs)

io::out $ shl1(mapped(0).expect("missing mapped"))                 // 12
io::out $ get_age(User("Alice", 30))        // 30
f = >3
x = (.)

6.5.2 Section evaluation by syntactic context

Context Example Behavior
Direct binding / call / pipeline f = * 2; 5 $> (* 2) $> (+ 1) Evaluates RHS each time the section is called
Choice predicate guard x [< 0] "neg" [> 0] "pos" | "zero" Applies predicate section to scrutinee x
Bare ram payload branch opt | (+ 2) | 0 / opt | (.name) | "" Applies section directly to the unwrapped slot payload
Enum payload branch box [Item] (+ 2) | 0 Applies section directly to variant payload (E3084 on payload-free variant)
Ordinary value/predicate branch mode [> 0] (* 2) | (+ 1) Returns the section as a function value (Int => Int) without applying to mode
Ram bind ($>>) opt $>> (x => <+ x + 1 +>) Continuation must return a ramification; opt $>> (+ 1) emits E3091
Ram functor map ($+>, $->) opt $+> (+ 1)
res $-> ("err: " + _)
Maps section, function, choice [guard] ..., or free-_ expression over positive ($+>) or negative ($->) ram payload, preserving the ramification wrapper
#type Option < Int | >
some = Option(<+ 7 +>)
res = some $>> (+ 2)

6.6 Implicit underscore (_) and cycle index (_i)

The exact identifier _ has context-dependent implicit roles, governed by strict static scoping rules:

Context Syntax Meaning of _ / _i Diagnostic on Misuse
Grouped implicit lambda (_ * 2), (10 - _), ((a = _, b = 1)) Innermost enclosing (...) group containing a free _ becomes a 1-arg function (_ repeated inside the same group refers to the same single parameter) —
Pipeline RHS & record update next(s) $> _(cursor = _.cursor + 1)
res $+> _.val * 2
Free _ or _(field = val, ...) on RHS of $>, $+>, $-> binds to LHS (evaluated once into a temporary when referenced multiple times) —
Partial application s $> str::split(_, ",")
opt $>> sub_opt(_, 3)
apply(sub(_, 5), 20)
k [gt(_, 5)] "big" \| "small"
A call with _ as a direct (positional or name = _) argument. On the RHS of $> / $>> the LHS / positive payload fills the slot. As a call argument or a [predicate] guard, where _ is not otherwise bound, it is the closure _ => f(…, _, …). A bound _ (payload, cycle element) keeps its meaning E3084 if the slot is bound and a function was expected
Operator section (* 2), (* _) Implicit left operand (* _ \(\equiv\) (_ * _)) E2031 if space omitted
Headless choice / guard [< 0] -1 | 1, ['a'...'z'] Wraps choice/guard into a 1-arg function over _ (outside statement @ body) E3003 if omitted subject has no _
Implicit cycle body 10..12 @ io::out(_ + _i) _ is current element/byte/map-value; _i is 0-based index (or map key K in m @). Saved/restored across nested cycles; does not survive cycle exit E3004: reading _ or _i after cycle exit
Ram payload branch r | _ + 1 | 0 _ is the selected slot’s payload (E4011 fatal abort if _ extracts negative Error slot on failure) E3004: _ on unit/absence slot (<Int|> 2nd branch)
Enum payload branch e [Item] _ + 1 | 0 _ is the matched variant’s payload E3004: _ on unit variant ([Empty] _)
Ordinary guarded branch x [> 0] x + 1 | 0 Does not bind _ in the branch body (preserves function-valued branches) E3004 on 7 [> 0] _ + 1 | 0
Structured pattern wildcard p [(a = _, b = 20)] ... Matches component without binding a variable ([Item _] is rejected) E2109: redundant _ in [Item _]
Explicit parameter _ => expr / a, _ => expr Forbidden across all lambdas, functions, cycles, and #param params E2040: explicit _ parameter
Assignment target _ = expr / _ := expr Forbidden E2044: cannot assign to _
Unbound value reference x = _ Forbidden outside an active implicit binding scope E3004: Undefined: _
#use io

square = (_ * _)
sub10 = (10 - _)
io::out $ square(4) + sub10(3)   // 23

#type Scores String ^ Int
m = Scores()
m ^ "alice" = 95
m @
   io::out $ (_i, _)                // ("alice", 95)
_ = 7
f = _ => 42
x: Int
x = _

6.7 Preferred idiom per operation

When multiple symbolic spellings exist for a task, Edva standardizes on one preferred idiom to keep precedence and intent predictable across codebases, edva fmt, and edva lsp:

Operation Preferred Idiom Avoid / Deprecated Form Why
Less-or-equal / greater-or-equal a !> b, a !< b <=, >=, !(a > b) Single comparison-tier tokens (<=/>= are rejected; avoids NaN inversion ambiguity)
Chained interval test lo !> x < hi x !< lo && x < hi Evaluates middle operand x once on one line
Multi-value equality guard x [a, b, c] / x ![a, b] x == a \|\| x == b, !x [a, b] Zero-allocation OR-list; negated ![...] prevents precedence mistakes
Optional / fallible default opt \|_\| fallback (opt \| v => v \| fallback) Direct unwrap-or-default sugar with clear fallback RHS
Propagate failure up call stack val = step()--! Manual r \| x => x \| e => --\| <- e -> Preserves positive value cleanly while propagating error early
Map positive / negative payload res $+> fn, res $-> map_err Nested choice + re-wrapping Functor map preserves outer ramification shape without boilerplate
Sequence fallible operations a $>> step2 $>> step3 Deeply nested a \| x => step2(x) \| ... Linear left-to-right monadic feed (LBP 40 / RBP 50)
Map / filter / fold over collection xs @> f, xs @> x => p \| <+ x +>, (0, xs) @> (+) Mutable 'acc loop + push Expression cycles allocate directly into result collection or Builder
Direct search early exit xs @ x, i => hit \| --\| <+ i +> found' = <-> + --> flags Immediate exit prevents trailing loop-state bugs (never place --\| on a function’s final line)

6.8 Disambiguating combined symbolic expressions

Three precedence boundaries govern almost all multi-operator expressions: 1. $ (LBP 30) and feed operators ($>, $>>, $+>, $->, .>, LBP 40) sit below choice (|, LBP 50), so a trailing choice attaches to the RHS unless parenthesized. 2. Comparison operators (<, >, !<, !>, ==, !=, LBP 120) sit above &&/|| (LBP 80–100) and above choice (|, LBP 50). 3. Formatter (edva fmt) and LSP (edva lsp) aids: edva fmt normalizes +3 block indents, +2 continuation/inline choice indents, leading [guard] / | / !| spacing, and $>> / |_| multi-line alignment; edva lsp provides precedence/meaning hover on symbolic operators and textDocument/inlayHint type hints for unannotated bindings.

#use io
#use str

parse_pos: Int => < Int | String >
parse_pos = n =>
   n !> 0 | --| <- "bad" +>
   <+ n ->

// 1. `$` absorbs trailing choice: 4 [1] 10 [4] 40 | 0 == 40
io::out $ 4 [1] 10 [4] 40 | 0

// 2. `$>>` bind, `$+>` positive map, `$->` negative map, and `|_|` unwrap-or-default
ok_val = (parse_pos(6) $>> (x => <+ x * 3 ->) $+> (+ 2)) |_| 0
err_txt = (parse_pos(0) $-> ("err:" + _)) | v => str::from_int(v) | msg => msg
io::out $ "${str::from_int(ok_val)}:${err_txt}"

// 3. Chained comparison guard + @> filter-map and fold
xs = Int{}(1, 3, 6, 9)
in_mid = xs @> x => (0 < x < 7) | <+ x +>
total = (0, in_mid) @> (+)
io::out $ total

6.9 Symbolic comprehension baseline & evaluation rubric

To evaluate reading cost before adding any surface syntax, Edva tracks a fixed 10-task comprehension baseline pairing raw compact snippets against formatted (edva fmt) + LSP-annotated (textDocument/hover on operators + textDocument/inlayHint) presentation:

Task Symbolic Construct Tested Primary Ambiguity Risk Baseline Tooling Aid
T1 a + b [7] "x" \| "y" Choice vs arithmetic scrutinee binding Choice LBP 50 < + LBP 140 hover note
T2 f $ x > 0 \| 1 \| 2 $ choice absorption Apply LBP 30 < choice LBP 50
T3 opt $>> parse $+> (* 2) \|_\| 0 Bind vs map vs unwrap-or-default chaining Feed LBP 40 + operator hover snippets
T4 lo !> x < hi / x ![1, 3] ! in !</!> vs negation !x Preferred-idiom table + operator hover
T5 foo \| a = 1; a + 2 \| 0 Inline choice arm indentation (+2 vs +3) Canonical edva fmt +2 alignment rule
T6 [Tag (a, b)] a + b vs [Tag] 1 Enum payload vs wildcard prohibition (E2109) Guard table formatting + error explanation
T7 shout = s => str::upper(s) Omitted signature on helper functions textDocument/inlayHint (: (String) => String)
T8 n @ i => hit \| --> >-- "ok" Cycle escape < T \| > result vs break Escape >-- hover + inlay return hint
T9 a .<. b .>. c (rejected E2025) Non-associative shift associativity Shift hover + explicit paren requirement
T10 r = cfg_load()--!; x = r + 1 Early return propagation vs unwrapped value type --! postfix hover + local inlay hint

No new surface operators are introduced until comparative runs against this 10-task rubric demonstrate an objective reduction in misreads on T1–T10.