Thread overview
Should this invariant always hold? equal(range.retro.array, range.array.retro)
Oct 17, 2022
FeepingCreature
Oct 17, 2022
ag0aep6g
Oct 17, 2022
FeepingCreature
Oct 17, 2022
Dukc
Solution
Oct 18, 2022
FeepingCreature
Oct 18, 2022
Ali Çehreli
October 17, 2022

Trolling a bit: Should this invariant always hold?

assert(equal(range.retro.array, range.array.retro));

Can you find a definition of range, using only std.algorithm verbs (ie. no custom ranges), that violates it?

(And no peeking at my recently-filed bug reports!)

October 17, 2022

On Monday, 17 October 2022 at 11:02:07 UTC, FeepingCreature wrote:

>

Trolling a bit: Should this invariant always hold?

assert(equal(range.retro.array, range.array.retro));

No, it shouldn't. The first expression might consume range. So it might be empty in the second expression.

October 17, 2022

On Monday, 17 October 2022 at 11:46:35 UTC, ag0aep6g wrote:

>

On Monday, 17 October 2022 at 11:02:07 UTC, FeepingCreature wrote:

>

Trolling a bit: Should this invariant always hold?

assert(equal(range.retro.array, range.array.retro));

No, it shouldn't. The first expression might consume range. So it might be empty in the second expression.

Ah, good idea!

assert(equal(range.save.retro.array, range.save.array.retro));

October 17, 2022

On Monday, 17 October 2022 at 11:02:07 UTC, FeepingCreature wrote:

>

Trolling a bit: Should this invariant always hold?

assert(equal(range.retro.array, range.array.retro));

Posting this without having looked at other replies.

No. The range could be a reference range, in which case this does not hold unless the range is already empty.

The invariant would have to be assert(equal(range.save.retro.array, range.save.array.retro)); instead.

Also this won't hold if the range contains floating point NaNs, or other elements that are not equal to themselves.

There may be other corner cases too I didn't think of.

October 18, 2022

On Monday, 17 October 2022 at 11:02:07 UTC, FeepingCreature wrote:

>

Trolling a bit: Should this invariant always hold?

assert(equal(range.retro.array, range.array.retro));

Can you find a definition of range, using only std.algorithm verbs (ie. no custom ranges), that violates it?

(And no peeking at my recently-filed bug reports!)

Solution, or rather, Phobos code that violates it (even the strengthened assert(equal(range.save.retro.array, range.save.array.retro))):

rot13: nhgb enatr = bayl(1, 2).havd!"gehr"; // havd onpxjneq vgrengvba purpxf ryrzragf sbe havdhrarff va gur onpxjneq beqre bs gur haqreylvat enatr.

October 17, 2022
On 10/17/22 23:19, FeepingCreature wrote:

> [rot13](https://rot13.com/): `nhgb enatr = bayl(1, 2).havd!"gehr"; //
> havd onpxjneq vgrengvba purpxf ryrzragf sbe havdhrarff va gur onpxjneq
> beqre bs gur haqreylvat enatr.`

Not an entirely unique case. ;)

  https://issues.dlang.org/show_bug.cgi?id=16588

Ali