Skip to content

Commit e1edb5f

Browse files
committed
libexpr: Document requirements for comparator passed to builtins.sort
1 parent c47970a commit e1edb5f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/libexpr/primops.cc

+26
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,32 @@ static RegisterPrimOp primop_sort({
36743674
36753675
This is a stable sort: it preserves the relative order of elements
36763676
deemed equal by the comparator.
3677+
3678+
*comparator* must impose a strict weak ordering on the set of values
3679+
in the *list*. This means that for any elements *a*, *b* and *c* from the
3680+
*list*, *comparator* must satisfy the following relations:
3681+
3682+
1. Transitivity
3683+
3684+
```nix
3685+
comparator a b && comparator b c -> comparator a c
3686+
```
3687+
3688+
1. Irreflexivity
3689+
3690+
```nix
3691+
comparator a a == false
3692+
```
3693+
3694+
1. Transitivity of equivalence
3695+
3696+
```nix
3697+
let equiv = a: b: (!comparator a b && !comparator b a); in
3698+
equiv a b && equiv b c -> equiv a c
3699+
```
3700+
3701+
If the *comparator* violates any of these properties, then `builtins.sort`
3702+
reorders elements in an unspecified manner.
36773703
)",
36783704
.fun = prim_sort,
36793705
});

0 commit comments

Comments
 (0)