Feature #7511

short-circuiting logical implication operator

Added by First Last over 1 year ago. Updated 4 months ago.

[ruby-core:50558]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:Next Major

Description

I find I need logical implication (will use => here) in boolean expressions fairly often

and even though a => b is equivalent to !a || b, this substitute is more difficult to read and make sense of in long expressions

History

#1 Updated by Yui NARUSE over 1 year ago

How about a ? b : true

#2 Updated by First Last over 1 year ago

naruse (Yui NARUSE) wrote:

How about a ? b : true

One can get along without the implication operator, but for better readability / quicker mental parsing, it would be better than the existing alternatives, including ternary.

#3 Updated by Alexey Muranov over 1 year ago

I have no opinion about whether it is needed, but a usual mathematical symbol would be x -> y. So it could be --> for boolean and -> for bitwise. But this may be confusing because of other existing or possible uses of ->, <- etc.

Maybe the comparison operator <= could be defined for booleans:

false <= true # => true
?

Then to write that a implies b, one could do:

!!a <= !!b

#4 Updated by Charlie Somerville over 1 year ago

alexeymurnov: This syntax is ambiguous. For example: a -> b {}

I don't think this would be a worthwhile syntax addition. It isn't something I can imagine would see much use.

#5 Updated by First Last over 1 year ago

charliesome (Charlie Somerville) wrote:

It isn't something I can imagine would see much use.

implication is very common, the presence or validity of something requiring some condition to hold.

if a && b && c && (d => e) && (f => g)
...
end

is a lot more clear than

if a && b && c && (d ? e : true) && (!f || g)
...
end

especially if some of a, b, c, d, e, f, g are non trivial expressions

#6 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto
  • Target version set to Next Major

Please show a more concrete example.

Yusuke Endoh mame@tsg.ne.jp

#7 Updated by First Last 5 months ago

mame (Yusuke Endoh) wrote:

Please show a more concrete example.

logical implication is a core operation of boolean algebra, do you also need concrete examples for || ?

ok if you insist, lets say your code needs to perform some optional work but successfully, i.e. the existence of the method must imply it's successful execution.

requiredwork && respondto?(optionalwork) => send(optionalwork)

#8 Updated by Martin Dürst 4 months ago

rits (First Last) wrote:

logical implication is a core operation of boolean algebra, do you also need concrete examples for || ?

Logical implication is actually NOT a core operation of Boolean algebra. There are two wikipedia articles about Boolean algebra, http://en.wikipedia.org/wiki/Boolean_algebra and http://en.wikipedia.org/wiki/Boolean_algebra_(structure). In both, and/or/not are the core operations. This corresponds well with the fact that these three operations are available in virtually any programming language.

On the other hand, implication doesn't appear in the later article, and only appears in the section on propositional logic in the former article. This corresponds well with the fact that there are very few programming languages that have an implication operator. Actually, I have to admit that I don't know a single programming language with such an operator. Do you?

ok if you insist, lets say your code needs to perform some optional work but successfully, i.e. the existence of the method must imply it's successful execution.

requiredwork && respondto?(optionalwork) => send(optionalwork)

How frequent are such examples? I'd guess they are very infrequent. Also, implication used as an operation isn't that easy to understand because common sense doesn't easily grok the fact that false can imply anything.

And rewriting the above as

requiredwork and (not respondto?(optionalwork) or send(optionalwork))

isn't actually too difficult to understand: Either the object doesn't respond to 'optionalwork', or the 'optionalwork' method returns a trueish value. This gives the whole picture.

On the other hand, reading "respondto?(optionalwork) => send(optional_work)" directly as "the existence of the method must imply it's successful execution" leaves the reader with the job of filling in "but if the method doesn't exist, we are still good".

My guess is that these kinds of difficulties also contributed to the fact that virtually no programming language has such an operator.

Also available in: Atom PDF