Feature #7511

short-circuiting logical implication operator

Added by First Last over 2 years ago. Updated 6 months ago.

[ruby-core:50558]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto

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 2 years ago

How about a ? b : true

#2 Updated by First Last over 2 years 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 2 years 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 2 years 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 2 years 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 2 years 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 over 1 year 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.

required_work && respond_to?(optional_work) => send(optional_work)

#8 Updated by Martin Dürst over 1 year 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.

required_work && respond_to?(optional_work) => send(optional_work)

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

required_work and (not respond_to?(optional_work) or send(optional_work))

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

On the other hand, reading "respond_to?(optional_work) => 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.

#9 Updated by Alexander Moore-Niemi 6 months ago

to be precise, implies is a derived operator, not a core one.

racket has this operator, as does eiffel, which supplies it to, afaik, help make design by contract easier (targeting assertions as a core responsibility of the language)

but given ruby has not committed to that, this would probably make more sense to recommend to contracts.ruby library :)

in the meantime consider just refining TrueClass and FalseClass to receive a custom defined xnor if you want to be able to readably infix your predicates, ie. (quick and dirty monkeypatch, use refinements instead!!)

2.1.3 :053 > false.class
 => FalseClass
2.1.3 :054 > class FalseClass
2.1.3 :055?>   def xnor(q)
2.1.3 :056?>       not self or q
2.1.3 :057?>     end
2.1.3 :058?>   end
 => :xnor
2.1.3 :059 > false.class
 => FalseClass
2.1.3 :060 > false.respond_to?(:xnor)

then you could do p.xnor(q) which i think is more readable than using the other recommended method

Also available in: Atom PDF