Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

It might be a strange question, but why there is no implication as a logical operator in many languages (Java, C, C++, Python Haskell - although as last one have user defined operators its trivial to add it)? I find logical implication much clearer to write (particularly in asserts or assert-like expressions) then negation with or:

encrypt(buf, key, mode, iv = null) {
    assert (mode != ECB --> iv != null);
    assert (mode == ECB || iv != null);
    assert (implies(mode != ECB, iv != null)); // User-defined function
}
share|improve this question
18  
Because it wasn't put in C; if it have been put then C++, Java and C# would have had it. –  m3th0dman Jan 18 '13 at 13:17
3  
But C doesn't have it, in turn, because it's not a common part of assembly language. Whereas as all instruction sets I know of include and, or and not, I know of none that include implies - no doubt someone will be along to tell me of some obscure instruction set that does shortly... –  Jack Aidley Jan 18 '13 at 14:30
2  
Maybe it was considered redundant because (1) it is less primitive than "^", "v", "~", (2) it saves very little typing since "A => B" is equivalent to "~A v B". –  Giorgio Jan 18 '13 at 14:50
3  
Languages that deal with logical constructions are more likely to have it. Chiefly among these is prolog –  MichaelT Jan 18 '13 at 15:22
4  
I'm surprised that you think assert #1 is clearer than #2. I have stared at #1 for some time and still can't see how it's behaviour could be considered intuitive. (especially with the extra != flipping the logic) –  Chris Burt-Brown Feb 14 '13 at 16:07
show 5 more comments

6 Answers

up vote 36 down vote accepted

It could be useful to have sometimes, no doubt. Several points argue against such an operator:

  • The characters - and > are valuable, both in isolation and combined. Many languages already use them to mean other things. (And many can't use unicode character sets for their syntax.)
  • Implication is very counter-intuitive, even to logic-minded people such as programmers. The fact that three out of the four truth table entries are true surprises many people.
  • All other logical operators are symmetrical, which would make -> an exception to orthogonality.
  • At the same time, there is an easy workaround: use operators ! and ||.
  • Implication is used vastly less often than logical and/or.

This is probably why the operator is rare today. Certainly it could become more common, as new dynamic languages use Unicode for everything and operator overloading becomes more fashionable again.

share|improve this answer
15  
Your first point only argues against using '-->' as the symbol for the operator, not against actually having the operator. The others are good points. –  AShelly Jan 18 '13 at 14:21
9  
"At the same time, there is an easy workaround: use ! and &&.": In fact, the simplest way of simulating "->" (or "=>", as it is more common in Math), is to use ! and ||, not ! and &&: A -> B is equivalent to !A || B which is equivalent to !(A && !B). –  Giorgio Jan 18 '13 at 14:53
12  
"The fact that three out of the four truth table entries are true surprises many people." Wat. I know another operator with the same feature which is used rather often... –  l0b0 Jan 18 '13 at 18:09
1  
@svick: These blanket statements about other developers is a bit strange. I'd expect people who have studied CS to think something like "anything can follow from a false premise, while a true premise can only lead to a true result." –  l0b0 Jan 20 '13 at 9:06
2  
(a) In C -> is used as an operator. No one complains about that. I would use => anyway. (b) Counter-intuitive to whom? The definition of implication is just that, a definition. (c) subtraction is not commutative. No one complains about that. (d) I think you mean ! and ||. Because of precedence ( and ) are often also needed. The same argument could be used against && or ||; take your pick. (e) Perhaps that is because it is not in most languages. Many uses of || could be concisely and (to me at least) intuitively replaced by implication. –  Theodore Norvell Sep 15 '13 at 20:30
show 8 more comments

I believe the answer lies in the mathematical foundations. Implication is usually considered and defined as an derived, not elementary, operation of boolean algebras. Programming languages follow this convention.

share|improve this answer
    
+1 - solid answer. Boolean algebra and the simplifications in logic that it allows drives a lot of programming methodology. Using an "implies ..." would potentially interfere with "don't care" conditions. –  GlenH7 Jan 18 '13 at 14:34
    
1. That depend on definition. It is possible to define or as a || b = !(!a && !b) (I met or as derived operator in logic). 2. The reason why it is done is usually to simplify induction proves I believe (we know that derived operators are just short-cuts so we don't need to have separate case for them). @GlenH7: What "don't care" conditions? a --> b is the same as !a || b. –  Maciej Piechotka Jan 18 '13 at 14:55
2  
Also - we have other derived operators like xor (!=) nxor (==)... –  Maciej Piechotka Jan 18 '13 at 14:57
3  
Actually, you can derive everything from either NAND !(a && b) or NOR !(a || b). For example, NOT a == a NAND a, a AND b == NOT(a NAND b), a OR b == (NOT a) NAND (NOT b). But providing only a NAND operator puts you in the realm of esoteric languages (which fits surprisingly well, given the answerer's username ;-) ). –  Stefan Majewsky Jan 18 '13 at 22:17
    
@MaciejPiechotka: True, this depends on definition. Just about everything in mathematics depends on definitions. And sure, (!=) and (==) could be interpreted as xor and xnor (though I wouldn't consider them boolean operators at all). What I wanted to point out is that there are conventions about definitions and specifically about definitions of Boolean algebras. Furthermore, there are reasons and history behind these conventions, and that history goes way beyond the scope of modern programming languages. –  COME FROM Jan 22 '13 at 11:13
show 3 more comments

Visual Basic 6 (and VBScript) had Imp and Eqv operators. No one used them. Both were removed in VB.NET; to my knowledge, no one complained.

I worked on the Visual Basic compiler team (as an intern) for a full year and never once noticed that VB even had those operators. Had I not been the guy writing the VBScript compiler, and therefore had to test their implementations, I probably would not have noticed them. If the guy on the compiler team doesn't know about the feature and doesn't use it, that tells you something about the popularity and usefulness of the feature.

You mentioned C-like languages. I can't speak to C or C++ or Java but I can speak to C#. There is a list of user-suggested features for C# literally longer than your arm. To my knowledge, no user has ever proposed such an operator to the C# team, and I've gone over that list many, many times.

Features that exist and are unused in one language, and are never requested in another are unlikely candidates to make it into modern programming languages. Particularly when there is not enough time and effort and money available in the budget to make features that people do want.

share|improve this answer
    
IMP goes back as an operator to GWBASIC days. I remember it from 1979. –  zarchasmpgmr Jan 21 '13 at 21:15
add comment

I can only guess, but a reason might be that there are workarounds which are quite simple and readable. Let's consider your example:

if (mode != ECB) assert (iv != null);

assert (mode != ECB --> iv != null);  // <-- that's only one character shorter

If you need an expression, the inline-if operator available in most languages (often called the ternary operator) can be used. Granted, this is not as elegant as A --> B, but the number of use cases might not justify adding (and maintaining!) another operator:

assert (mode != ECB ? iv != null : true);
share|improve this answer
2  
As of asserts - there is good reason. First one will produce (in many implementations) message "assertion failure: iv != null" while second "assertion failure: mode != ECB --> iv != null". –  Maciej Piechotka Jan 18 '13 at 14:56
2  
+1, I was about to ask this same thing on the question (since I'm not very familiar with "implication", it never comes up day-to-day). The if statement is far, far clearer to just about everyone. –  Izkata Jan 18 '13 at 19:16
add comment

Maybe it really wasn't needed, and was kind of already there. Implication is equivalent to less than or equal to. I made a chart mapping all of the mathematical Boolean operators to programming language constructs. Maybe it is useful.

share|improve this answer
1  
The OP mentions Java, C, C++, Python and Haskell. In Java, you can not apply <= to booleans. In C, C++, and Python you have to be very careful that the operands are already in {0,1} (or, in C++ in {true,false}); this is in contrast to && and ||, which work for any operand that can be used as a boolean. As you say, "kind of". In Haskell, yes, it works. Sadly <= means implies rather than follows from; this violates the law of least surprise. Also, in Haskell, x <= y is strict in x and y, whereas (not x) || y is nonstrict in y. –  Theodore Norvell Oct 27 '13 at 3:43
    
I would not use this in any code, but I figured I would at least put this info out there. Thanks for the all the additional information! –  kzh Oct 28 '13 at 15:19
add comment

Eiffel has an "implies" operator and it is very nice for writting pre- and postconditions. It makes code more readable, unfortunately that was never a fundamental intension for C like language and too few people used eiffel so the beauty of "implies" as operator is not well known.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.