Reputation: 1109
This might be a trivial question but I really can't find the answer anywhere. There is a convention in computer science which I find peculiar.
In haskell datatypes can be defined like this:
data Bool = False | True
In xml qualified names are defined like this:
QName ::= PrefixedName | UnprefixedName
There are probably more similar examples but this should suffice.
Usually it is well understood that |
(pipe or bar) should be read as or
. But this seems strange. A or B
is true also when both A and B are true. While it makes sense in the first example (there is a possibility that something is True
and False
at the same time, but we implicitly assume the law of non-contradiction), it doesn't in the second: something is either a PrefixedName
or an UnprefixedName
it can't be both.
So why is this often put like this? Why not use exclusive or? Are there any non-conventional reasons?
Upvotes: 1
Views: 587
Reputation: 532003
|
has a long history of being used to separate items in a list of mutually exclusive choices:
Regular expressions: a* | b*
means a string can either be 0 or more a
s or 0 or more b
s, but not both.
Backus-Naur form for representing context-free grammars:
Expr ::= Term | Expr AddOp Term
in which an expression can either be a single term, or another expression combined with a term with an addition operator. (It cannot be both simultaneously.)
Usage messages for command-line programs:
git branch (-d | -D) [-r] <branchname>...
Here the git branch
command can take either the -d
or the -D
option, but not both in the same invocation.
The data
statement in Haskell continues this tradition; it is unrelated to the use of |
as a logical or bit-wise operator.
(If anything, it is possible that the use of |
for bitwise OR
was inspired by Backus-Naur form, in which case you could be asking why |
is used for OR
instead of XOR
.)
Upvotes: 2
Reputation: 120741
This data X = A | B
notation should not really be understood as a logical OR at all (though that corresponds quite well to the intuitive meaning). What it really means is that X
is a sum type of A
and B
, i.e. a coproduct. Now, the product operation on booleans is in fact AND, and so the dual would quite naturally be OR.
Though then again, the sum operation on a vector space of booleans is actually XOR so we're turning circles a bit...
I just wouldn't read too much into this. |
is simply a symbol; in C-like languages it happens to also mean bitwise OR, but the actual logical OR is generally denoted differently, be it ||
or ∨
.
Upvotes: 8
Reputation: 2928
I think the meaning of "or" here is in a literal sense as opposed to a mathematical sense. Using the term "or" means that the value can have one value, or another. It is not an operator which aims to determine a truth value.
While it may not be entirely logical to use an "or" operator sign for this, it does a good enough job at getting the point across for a reader. This clarity is ultimately the thing a language is striving for. As long as a greater proportion of readers is capable of interpreting it as a "literal" usage as opposed to a "mathematical" operator usage, using or in a literal sense means you, as a language, are doing a better job at getting your point across, making the method superior to using something such as XOR.
Upvotes: 1