Search code examples
syntaxoperatorshistorybitwise-orlogical-or

Why is "||" the symbol for or?


I know that || represents the logical operation "or", but I'm curious if anyone knows the history of choosing that symbol. Was it just because it happened to be an unused symbol on the keyboard?


Solution

  • Origins of the single vertical line "|" as indicating the disjunctive "or".

    From ASCII character history:

    It has been conjectured that the vertical line character was introduced to the area of computing with the Backus-Naur Form metalanguage for describing programming languages. It was also taken into use in APL in early 1960's and incorporated into PL/I at about the same time as the OR operator and, doubled, as the concatenation operator.

    John Warner Backus (December 3, 1924 – March 17, 2007) was an American computer scientist. He directed the team that invented the first widely used high-level programming language (FORTRAN) and was the inventor of the Backus-Naur form (BNF), the almost universally used notation to define formal language syntax. He also did research in function-level programming and helped to popularize it.

    A programming language designer at IBM, he proposed "metalinguistic formulas" to describe the syntax of the new programming language IAL, known today as ALGOL 58, using the BNF notation.

    In Backus-Naur form, an expression consists of sequences of symbols and/or sequences separated by '|', indicating a choice, the whole being a possible substitution for the symbol on the left.

    <personal-name> ::= <name> | <initial>

    In the ALGOL 58 specification, Backus did not originally use the vertical line; He used the word "or" with a line over it (as well as the logic symbol OR symbol). Peter Naur, a Danish computer scientist who contributed to ALGOL 60, modified several symbols to those that could be typed at a standard keyboard. Among his changes was the addition of the vertical line. (Source: "History of Programming Languages", Richard L. Wexelblat)

    Even after the ALGOL 60 specification, however, there are plenty of examples of the OR symbol symbol still being used with ALGOL. In fact, in 1961 the ASCII character set got its backslash added so ALGOL's logical operators could be typed with slashes, like this: \/ /\ The symbols can be found on the IBM 2741 keyboard, available in the mid sixties:

    IBM Keyboard

    But there's clear evidence that Naur added the vertical line to ALGOL 60. In an article published in 1964, (Knuth D., “Backus Normal Form vs. Backus Naur Form” Letters to the Editor, Communications of the ACM, Vol. 7 (1964). pp. 735-736, available here), Donald Knuth argued that "Backus Normal Form" should be referred to as "Backus Naur Form" due to Naur's contributions. Among the contributions he referred to was the addition of the vertical line as an or operator.

    Here's an image of a portion of the 1964 article. Note on the right side, the bulleted item (iv).

    enter image description here

    The article goes on to describe Naur as having been responsible for that semantic change as part of his editing responsibilities for the 1960 Algol report:

    enter image description here

    Origins of the double vertical line "||"

    In The Development of the C Language, Dennis M. Ritchie describes why the double vertical line operator was added:

    Rapid changes continued after the language had been named, for example the introduction of the && and || operators. In BCPL and B, the evaluation of expressions depends on context: within if and other conditional statements that compare an expression's value with zero, these languages place a special interpretation on the and (&) and or (|) operators. In ordinary contexts, they operate bitwise, but in the B statement

    if (e1 & e2) ...

    the compiler must evaluate e1 and if it is non-zero, evaluate e2, and if it too is non-zero, elaborate the statement dependent on the if. The requirement descends recursively on & and | operators within e1 and e2. The short-circuit semantics of the Boolean operators in such `truth-value' context seemed desirable, but the overloading of the operators was difficult to explain and use. At the suggestion of Alan Snyder, I introduced the && and || operators to make the mechanism more explicit.

    (Thanks to Richard Brown for this section's research).