Expressing range constraints in CNF form

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 11 09:28:23 PDT 2017


On 11.06.2017 17:25, Andrei Alexandrescu wrote:
> On 6/11/17 11:11 AM, Nick Treleaven wrote:
>> On Sunday, 11 June 2017 at 00:28:58 UTC, Andrei Alexandrescu wrote:
>>> https://github.com/dlang/phobos/pull/5461
>>>
>>> There's many good advantages to this. The immediate one is the 
>>> constraint is better structured and easier to understand. Then, the 
>>> compiler can print the exact clause that failed, which improves the 
>>> precision and quality of the error message.
>>
>> Great!
> 
> Thanks.
> 
>>> // Also possible (no change to the language)
>>> enum bool isInputRange(R) =
>>>     is(typeof((ref R r) => r)) && msg("must be copyable")
>>>     && is(ReturnType!((R r) => r.empty) == bool) && msg("must support 
>>> bool empty")
>>>     && is(typeof(lvalueOf!R.front)) && msg("must support front")
>>>     && is(typeof(lvalueOf!R.popFront)) && msg("must support back");
>>
>> I'm not getting how this works.
> 
> Ostensibly the function is trivial:
> 
> bool msg(string) { return true; }
> 
> It doesn't change the semantics. The compiler would recognize it as an 
> intrinsic and would print the message if the clause to its left has failed.
> 
> 
> Andrei

I'd prefer

bool msg(bool constraint, string message){ return constraint; }

This does not require the compiler to dive into a branch it wouldn't 
consider otherwise, and the pairing of constraint to message is less ad-hoc.


More information about the Digitalmars-d mailing list