سؤال

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?

هل كانت مفيدة؟

المحلول

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).

نصائح أخرى

I believe it was BCPL (wp) which introduced it. Its predecessor CPL had the symbol | but used it as an alternate til-end-of-line comment.

I have found no evidence that the reason was ever written up anywhere, but we can think about whether it was the only choice (which would have made a good reason):

They did not choose the word "or" because the concept of context-free grammars (wp) was becoming very popular in language design, so they chose something that could not be a valid symbol (symbols being defined as [a-zA-Z_]+[a-zA-Z0-9_]* or additionally with '-' typically).

So they looked at all the characters that were defined in previously defined standards. (Keep in mind that the character had to already exist in a standard so they could not just decide and paint their own. ) They looked especially hard at the 3-year-old ASCII but also at EBCDIC. They found that there was not much too choose from:

  • aside from the alphabet, underscore, numbers and space there were:
  • NUL SOH STX ETX EOT ENQ ACK BEL BS TAB LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US which were all used as terminal-control characters (and unprintable).
  • "" ( ) { } [ ] were semantically unfit, as was & (been used as 'and' for >100 years)
  • all of these were used ALGOL and/or CPL: ! # $ % ' * + , - . / : ; < = > ? @ \ ^ _ ~
  • so what remained were ` and | but the backtick could not be used because it was not available everywhere even where claimed ascii-compliant.

From: http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

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.

The oldest usage of an | as or, I could find after googling a while was in the Backus Naur Form.

Which seems to have first appeared in the paper The syntax and semantics of the proposed international algebraic language of the Zuerich ACM-GRAMM conference by J.W. Backus in 1959, however he did not use the | notation there. Instead he used an overlined "or".

Then in 1960 he wrote the Report on the algorithmic language ALGOL 60 , which already used the |-notation.

So he invented it somewhere between 1959 and 1960.

The use of the vertical bar to express an "or" operation (|| for logical, or | for bit-wise) may have sprung from BNF. BNF is not a programming language, but it was developed in the late 1950s as a way to create a formal specification for programming language syntax. The vertical bar character in BNF indicates a choice between valid expansions for nonterminal symbol. It's highly likely that programming language designers in the '60s and later years were familiar with this notation and were influenced by it.

The logical OR || symbol actually came about from a splitting of the + plus symbol. The horizontal bar was rotated 90 degrees and thus was the birth of the logical OR || symbol. You may or may not find this documented within the internet because it happened so long ago. For the bitwise OR | , (you guessed it) the crossbar was removed from the + plus symbol.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top