C++ Logo

std-discussion

Advanced search

Re: On P2971R0 and implication operator

From: Ell <ell_se_at_[hidden]>
Date: Thu, 24 Apr 2025 13:39:31 +0300
On 4/24/25 00:43, Farid Mehrabi via Std-Discussion wrote:
> Hi,
>
> A coffee time alternative missing from the proposal:
> Boolean "Less than or equal" is implication:
>
> bool implies(bool a, bool b){
> //mathematicaly: a => b
> return a <= b; // inverted syntax!
> };

It is mentioned in the proposal, though missing the irony.

>
> The only drawback is absence of shortcut semantics.
> But given the asymmetry of implication, shortcut semantics may be
> difficult to justify.

Short circuiting is part of the appeal:

    void f (std::optional<int> x = {})
        // if x is provided, it must be positive
        pre (x => *x > 0);

Without short circuiting, this is potentially UB.

> This, for shortcut semantics, proper disjunction or conjunction
> equivalent can be used.
>
> how am I supposed to end the twisted road of your hair in such a dark
> night??
> unless the candle of your face does shed some light upon my way!!!
>

Since we're on the subject of P2971, I have a different quibble: the
paper suggests that an empty fold expression involving implication
evaluate to `false`, on the basis that `a => b => ... => z` is
equivalent to `!a || !b || ... || z`, and that an empty disjunction is
`false`. But this specific disjunction singles out the rightmost
operand, so the pattern doesn't extend to zero operands. It also only
applies to right-fold expressions.

More specifically, unlike conjunction and disjunction, there is no
neutral "init" value that could be added to a (right-folded) implication
chain without affecting the result. That is, `(p => ...)` is not
equivalent to `(p => ... => false)`. OTOH, in the case of a left-fold,
`true` is the neutral value: `(... => p)` is equivalent to
`(true => ... => p)`.

So either an empty implication-fold should only be well-formed for a
left fold, and evaluate to `true`, or, given that left-associative
implication doesn't seem very useful, it should be ill-formed either way
(i.e., the status quo for most operators).

Received on 2025-04-24 10:39:37