Add a new logical operators imply, which follow this logical table:
Motivation
Let’s use a dummy exemple:
from typing import Literal
def dummy(mode: Literal[0,1,2,3], a=None):
"""
The mode 3 requiert the paramater 'a' to be passed
"""
assert (mode!=3) or (a is not None), "You need to pass a parameter a, when using the mode 3!"
...
In my opinion, these logical operations are not easily readable, so I propose we use a logical operator ‘imply’ to make it clearer:
from typing import Literal
def dummy(mode: Literal[0,1,2,3], a=None):
"""
The mode 3 requiert the paramater 'a' to be passed
"""
assert (mode==3) imply (a is not None), "You need to pass a parameter a, when using the mode 3!"
...
Logically, imply: p => q is the same as not(p) or q
Techical side
imply can be build-in from or, and, not, so by construction, there should be no issue.
(Side note: Don’t use assert to test your arguments, outside of simple/trivial demos for the purpose of argument. In real code, be sure to use actual tests and actual exception raising.)
I read all your comments, thanks for giving you’re opinion
I think we kinda lose the focus of the discussion about ‘imply’, because of ‘assert’ was replaced by ‘if’, probably because my example wasn’t that good
So I would like to show you a new concrete use case where ‘imply’ would be a good fit:
# 1) Without imply
def dummy(show_errors, is_verbose):
if (not show_errors) or is_verbose:
raise ValueError("When 'show_errors' is true, 'is_verbose' should be true!")
...
# 2) Without imply
def dummy(show_errors, is_verbose):
if not (show_errors and not is_verbose):
raise ValueError("When 'show_errors' is true, 'is_verbose' should be true!")
...
# 3) With imply
def dummy(show_errors, is_verbose):
if show_errors imply is_verbose:
raise ValueError("When 'show_errors' is true, 'is_verbose' should be true!")
...
I think, in my POV that it is much more simpler and natural to understand with imply in the 3)…
This seems like it could be fixed with a simple user-defined function. Something like:
def imply(gate: bool, test: bool) -> bool:
return gate and not test
def dummy(show_errors, is_verbose):
if imply(show_errors, is_verbose):
raise ValueError("When 'show_errors' is true, 'is_verbose' should be true!")
...
I’ve TA’d enough undergraduates who struggled with material implication[1] that I don’t think adding this complexity would be a benefit. The word “imply” has “plain language” connotations that are really not helpful here (e.g. of a causal relationship between the “if” part and the “then” part).
I’ve never personally seen it called material “conditional” ↩︎
No we are focused, we simply do not agree that this is a needed addition.
Its not intuitive and has to be learnt, would be a maintenance burden.
Its easier to understand using the current boolean operations.
I think we are focused, it’s just that your idea of creating a material implication operator, when it can be perfectly defined using boolean algebra, is not that helpful. Mainly because it’s easier for most to understand as a boolean algebra statement. I don’t think a lot of CS people learn traditional logic? I mean, hey if we do a material implication operator, why not a biconditional operator then <=>, we already have that, ==