News for hackers. Stuff that matters.
Re:The Case Against Formal Verification, 50 Years Later by inigyou on Sunday August 16, 2026 @08:55PM (#49325427)
You know, the last time someone brought up formal verification of sorting I said what the trivial spec was, and then someone else pointed out why it's actually completely wrong.

So for pedagogical purposes, can you tell us what you think the trivial spec is?

Re:The Case Against Formal Verification, 50 Years Later by pastel8739 on Sunday August 16, 2026 @09:10PM (#49325506)
Ok, I’ll bite, why is this wrong?

For a list of items I and an operator LEQ which returns bool for any pair of items in I, SORT() returns a list S such that:

1. Every item in I is present exactly once in S

2. For each consecutive pair of items (S_i, S_j) in S, LEQ(S_i, S_j) is true.

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Sunday August 16, 2026 @09:23PM (#49325588)
SORT(1,2,3,4,5,5,6) = 1,2,3,4,5,6
Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @09:27PM (#49325609)
I'm sorry, do all 5's look the same to you!! /s

aka, one item in I is missing in your output.

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Sunday August 16, 2026 @09:36PM (#49325665)
No, if it had one more 5 it would violate your specification that every time must occur exactly once.

Also, SORT(1,2,3,4) = 1,2,3,4,7

Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @09:50PM (#49325748)
Not my specification (drive by third party)

but I do take the view that ( 1, 2, 3, 4, 5, 5, 6 ) is a list of seven values (perhaps the number of dollars in the pockets of seven distinct unique people) and when sorted the output should also have seven items that correspond to the seven input items.

> Also ...

Yeah, that needs tightening up by pastel8739

Re:The Case Against Formal Verification, 50 Years Later by Jtsummers on Sunday August 16, 2026 @09:56PM (#49325777)
You need a way to differentiate the two 5s, that isn't present. If you had a list like:

  L = [(5,foo), (2,bar), (2,baz),...]
And did a:

  SORT(L, key=first) # or however it'd be specified
Then the duplicate 2s would be fine, because they're no longer duplicates, only duplicate keys. But it would still fail if (2,baz) showed up twice in the source and destination even though we've asked for SORT, not UNIQSORT.
Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @10:28PM (#49325938)
More seriously,

> You need a way to differentiate the two 5s

As there's no unique filtering or other reduction going on here, there's a permutation chain from input to output.

Re:The Case Against Formal Verification, 50 Years Later by Jtsummers on Sunday August 16, 2026 @11:15PM (#49326197)
But that's not in the specification given above. That specification is entirely wrong to specify SORT. It requires no duplicates survive the sorting process.
Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @10:12PM (#49325855)
In the cases of

  SORT ( 3, 2, 5, 5 ) ->> ( 2, 3, 5, 5 ) and
  SORT ( 3, 2, 5, 5 ) ->> ( 2, 3, 5, 5 )
one or both of those might be incorrect ?

( I'm teasing, perhaps )

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Sunday August 16, 2026 @10:07PM (#49325829)
The specification said

    Every item in I is present exactly once in S
5 is an item in I, and it is present exactly once in S.
Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @10:14PM (#49325863)
and 5 is another item in I, and it's not present in S.
Re:The Case Against Formal Verification, 50 Years Later by inigyou on Sunday August 16, 2026 @10:17PM (#49325876)
Yes it is, it's right there, between the 4 and the 6.
Re:The Case Against Formal Verification, 50 Years Later by defrost on Sunday August 16, 2026 @10:29PM (#49325944)
That's not the same one - track the permutation chain.
Re:The Case Against Formal Verification, 50 Years Later by Veserv on Sunday August 16, 2026 @11:11PM (#49326176)
What is the "same one"? Define item in "I" formally.

Are we talking about Values? Then inigyou is correct.

Memory locations? Then it is trivially true, but that does not prevent me from writing 0 into every memory location.

Value + Memory location? Then it does not work for arrays since we are modifying the memory locations by moving the values between them.

The abstract notion of manipulable things in a indexable order? You need to show how that correlates to reality in a way where you can not put in a hole even larger than this one you are trying to close.

To loop back, this very discussion shows how non-trivial it really is and how much thought actually needs to be put into handling even "trivial" problems. Almost everybody who talks about how we can replace these complex implementations with simpler, understandable specifications has little to no experience with the difficulties of actually creating correct specifications. Anybody who would bring up sorting as "trivial" either has no idea what they are talking about or is so far ahead that they have weird ideas as to what constitutes as "trivial". In both cases, their opinion is highly divorced from practical reality.

That is not to say that it is not worthwhile or even that the specifications are "more complex". It is quite possible the specification is still simpler despite the difficulty, but it is also likely the complex implementation was already totally incomprehensible and a simplified specification is also incomprehensible, it is now just formally incomprehensible.

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Monday August 17, 2026 @06:59AM (#49329028)
The specification was extremely clear on this point. 5 is in the input, so 5 must be in the output exactly once. And 5 is in the input, so 5 must be in the output exactly once. It doesn't say anything about "tracking a permutation chain". Any output containing exactly two 5s violates the spec. You need a different spec because this one is clearly not what you intended, which is the point.
Re:The Case Against Formal Verification, 50 Years Later by Jtsummers on Sunday August 16, 2026 @09:40PM (#49325682)
> I'm sorry, do all 5's look the same to you!! /s

You have that /s tag, but this is actually the problem with pastel8739's spec as written.

>> 1. Every item in I is present exactly once in S

This actually does require inigyou's example to be the result of calling SORT when you cannot distinguish repeated items from each other.

  SORT([1,1]) => [1,1]
The item 1 (which one? doesn't matter, they both do but we only need one to fail the post-condition to invalidate the result) in the source list has a count of 2 in the destination list, so this is an invalid result by the supplied spec.

pastel8739's spec also doesn't exclude the possibility of inserting new values (so long as they aren't duplicates of items in the source list).

Re:The Case Against Formal Verification, 50 Years Later by edflsafoiewq on Sunday August 16, 2026 @09:58PM (#49325784)
1. The output is a permutation of the input.

2. If the comparison implements a strict total order, the output is sorted according to it.

Re:The Case Against Formal Verification, 50 Years Later by Veserv on Sunday August 16, 2026 @10:37PM (#49325981)
You are correct.

However, that specification is not trivial. Almost nobody correctly articulates property 1 when first encountering the problem if they do not already know the answer or are already aware it is a trick question (and even then most software developers still fail).

Furthermore, that also sidesteps the problem of formally specifying what a permutation is. Unless you have a grab bag of already proven powerful theorems, the author is most likely also going to make a error doing that as well even if we start at a proof abstraction level comparable to normal programming.

Reality is that trivial problems admit trivially wrong specifications exceedingly easily. There is little reason to assume that much more complicated problems that are hard to even articulate will magically support obviously correct specifications that are simpler and more understandable than the code.

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Monday August 17, 2026 @07:01AM (#49329039)
Note this doesn't make it useless, just not watertight. Proving that the output of a sort algorithm is a sorted list is genuinely useful and catches a lot of potential bugs. If your sort algorithm is "return []" you'll certainly notice that while writing the proof and fix it. It'll also be caught easily by any unit test.

It wouldn't catch all bugs - merge sort recursing on the same half of the list both times would return one item N times, which is sorted, and is a plausible enough mistake to make.

Re:The Case Against Formal Verification, 50 Years Later by esafak on Sunday August 16, 2026 @10:34PM (#49325969)
See how easy it is once you have right terms ;)
Re:The Case Against Formal Verification, 50 Years Later by inigyou on Monday August 17, 2026 @08:19AM (#49329674)
I think this works, but I could be proven wrong yet again.

You might also want to prove that the comparison implements a strict total order, but that would be part of the comparison's spec, not the sorting function's. There is another possibility for a mistake there: if you don't use exactly the same test, you might prove that your < implements a strict total order by its definition, while also proving it doesn't according to the sort function spec and thus allowing the sort to return anything.

Re:The Case Against Formal Verification, 50 Years Later by samus on Monday August 17, 2026 @02:12AM (#49327073)
Trivial one: `forall i, j. 0 <= i < j < |sorted| -> sorted[i] <= sorted[j]`, which can be satisfied by copying a single element from `input` or by `sorted` being empty.

It can be fixed (feedback welcome) by adding: `forall i, j. 0 <= i < |sorted| -> |indexof_id(sorted, input[i])| = 1`, with `indexof_id` using equality by identity, which is crucial in practice.

Note that the amended definition implies quadratic runtime, which is the crucial difference between a specification and an efficient implementation.

Re:The Case Against Formal Verification, 50 Years Later by inigyou on Monday August 17, 2026 @08:11AM (#49329597)
you have the common problems with non-uniqueness and with extra elements. Your spec is met by:

    SORT(1,2,3,4,5,5,6) = 1,2,3,4,5,6

    SORT(1,2,3,4,5,5,6) = 1,2,3,4,5,6,7
and a bounds checking bug unique to your formal spec because you only check inputs up to the length of the output

    // first 0 elements of input must occur in output
    SORT(1,2,3,4,5,5,6) = empty list

    // first 6 elements of input must occur in output
    SORT(1,2,3,4,5,5,6) = 1,2,3,4,5,7
Re:The Case Against Formal Verification, 50 Years Later by samus on Tuesday August 25, 2026 @12:41PM (#49436884)
My second condition would reject the additional element, as it would not be found in the input. have to be duplicated to However, I was lazy about the `|input| = |output|` constraints. With those constraints it would not be possible to omit elements from the input in the output.