www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should this invariant always hold? equal(range.retro.array,

reply FeepingCreature <feepingcreature gmail.com> writes:
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!)
Oct 17 2022
next sibling parent reply ag0aep6g <anonymous example.com> writes:
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.
Oct 17 2022
parent FeepingCreature <feepingcreature gmail.com> writes:
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));
Oct 17 2022
prev sibling next sibling parent Dukc <ajieskola gmail.com> writes:
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.
Oct 17 2022
prev sibling parent reply FeepingCreature <feepingcreature gmail.com> writes:
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](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.`
Oct 17 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Oct 17 2022