Repository navigation
types: Numeric bound for arithmetic, and sum - #274
Merged
Merged
Conversation
Numeric is a second entry in the constraint table: exactly Int! and Float! (whyNotNumeric), enabling + - * / %. A rigid `a!` bounded by it takes the arithmetic operators and unary minus, with only the numeric meaning of +. The result follows the numbers: a! with a! or Int! is a!, anything with Float! is Float!, and two different Numeric parameters are rejected, since one may be Int and the other Float. A bare `a` may be null and is refused. Comparisons mix a Numeric a! with numbers the same way, so `[a: Ordered & Numeric]` can write `x < 0`. Arithmetic or unary minus on an unbounded variable now says `declare it [a: Numeric]` (not beside a string or list, or a Numeric variable, where the bound wouldn't help), and a variable with other bounds is told to add the missing one (`[a: Numeric & Ordered]`). Signed-off-by: Alex Suraci <[email protected]>
`[T]!.sum` adds up a list of Int! or Float!. Its element variable is bounded by Numeric, like sort's by Ordered, so any other list (nullable elements included) is an error where it's called, and an element type learned later is checked where it's learned. An empty list sums to 0, or 0.0 when its runtime element type is Float. So that a [Float!]! passed for a generic [a!]! parameter keeps that, list materialization now keeps the list's own element type when the target's mentions type variables. A [] typed only by its surroundings (`xs ?? []`) or a list built in a generic body still carries no concrete element type at runtime and sums to 0. Signed-off-by: Alex Suraci <[email protected]>
The type-params section of nullability.md covers Numeric, its result types and combining it with Ordered; the collections page, operators, grammar and the dang-language skill cover sum and the new bound. Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
17d73e2
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://4c2b8c68.dang-3kk.pages.dev |
| Branch Preview URL: | https://numeric-constraint.dang-3kk.pages.dev |
vito
force-pushed
the
numeric-constraint
branch
from
November 8, 2026 23:56
c7cba4a to
4412195
Compare
vito
force-pushed
the
numeric-constraint
branch
from
November 9, 2026 17:38
4412195 to
5eb0f1b
Compare
vito
marked this pull request as ready for review
November 9, 2026 17:39
vito
force-pushed
the
numeric-constraint
branch
from
November 9, 2026 17:42
5eb0f1b to
9a3168a
Compare
Pin that .sum over a list of Decoded data is a compile error. Signed-off-by: Alex Suraci <[email protected]>
vito
force-pushed
the
numeric-constraint
branch
3 times, most recently
from
November 10, 2026 00:26
d4abeea to
17d73e2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Numeric, a second built-in constraint for type parameters, and thesumlist method bounded by it. Users can now write their own numeric folds:sumreplaces theBuiltinDef.ElemTypes/Select.listElemTypehook that #263 had and dropped. It goes through #272's constraint table instead, the same waysortusesOrdered.Wiring
Numericis a newBuiltinConstraintentry inconstraints.go: name, predicatewhyNotNumeric, and the operatorsaddition,subtraction,multiplication,divisionandmodulo. Its members are exactlyInt!andFloat!. Like Ordered, the bound ignores nullability. Bound parsing, the "constraint, not a type" error and the unknown-bound list (known bounds: Numeric, Ordered) all pick it up from the table.resolveOperandsasksresolveNumericVarsfirst. When an operand is a rigid variable bounded by Numeric, only the numeric meaning applies, so+doesn't concatenate strings or lists. Unary minus accepts a boundeda!too.checkConstrainedOperandsmixes a Numerica!with numbers the way arithmetic does (numbersMix), so[a: Ordered & Numeric]can writex < 0.sum.Method(ListTypeModule, "sum").Returns(NumericVar('a')): the bound on the element variable goes throughboundedElems, likesort's. So any other list,[Int]!included, is an error at the call, and a deferred element type (let xs = []; xs.sum; xs += ["a"]) fails where it's learned.Maintainer decisions
a! op a!anda! op Int!(either way round) area!. Int op Int is Int and Float op Int is Float, so it's sound, and/is Int division whenais Int, as it is without generics.a! op Float!isFloat!.a! + b!) are an error, because one could be Int and the other Float.[a: Ordered & Numeric]. The hint keeps the bounds the variable already has:declare it [a: Numeric & Ordered].declare it [a: Numeric]. The exceptions are a string or list operand ("n=" + x) and an unbounded operand beside a Numeric one, where the bound wouldn't help. Two pre-existing goldens (arithmetic_on_generic_type,generic_annotation_escapes_rigid) now carry the hint.sumuses the runtime element type. An empty list sums to0.0when the list is known to hold Floats at runtime (float literals, an annotated[], a list passed for a[Float!]!parameter, or one filtered or sliced from those) and to0otherwise. To keep that through generic code, list materialization now keeps the list's ownElemTypewhen the target is generic ([a!]!) instead of overwriting it with the variable. This touches every list passed to a generic parameter, not only forsum. Still0while typedFloat!: a[]typed only by its surroundings (xs ?? [], anifbranch, areduce([])accumulator) and a list built inside a generic body (xs.map { x => x * 2 }there). This is documented on the collections page and in the skill; dang: empty collections carry their inferred type #278 (stacked above) fixes these cases at the root.Open question (answered: Q-27, option C)
An empty
sumcould produce anIntvalue in aFloat!slot here. The maintainer picked option C: fix the root cause. That fix is #278, stacked on this PR: empty[]/[:]literals and lists built inside generic bodies now carry their inferred element type at runtime, so an emptyFloat!sum is0.0in the cases above. With #278, the "still0" caveat in decision 4 narrows to a type variable that a generic call's arguments don't decide.Tests and docs
tests/test_numeric.dang: a user-written sum, mean, scale, halve, rem, negation,+=,Ordered & Numeric(sort plus add, abs).tests/test_list_sum.dang: Int and Float lists, derived lists, genericxs.sum, typed empty lists (including through a generic parameter), deferred element types, nullable receivers.sumon[String!]!, on[Int]!, on[Decoded!]!(list_sum_decoded_element), and deferred; arithmetic and negation on an unbounded variable; string concatenation on an unbounded variable (no hint); a nullable boundeda, operand and negation; two Numeric parameters; a Numeric and an unbounded operand;+ "s"on a Numeric value;<on Numeric alone; a non-numeric argument at the call.types/nullability.md(type-params section, retitled "Type parameters,OrderedandNumeric"),data/collections.md(sum),language/operators.md,reference/grammar.md, and the dang-language skill (types, syntax, objects, stdlib, SKILL routing).pkg/dang,pkg/hm,pkg/lsp,docs/go,tests,docsandfmt/stalepass.