[英]Why use “or” instead of “xor” in definitions?

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?


3 个解决方案



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.

這個數據X = |b表示法不應該被真正理解為一個邏輯或根本不應該被理解為一個邏輯(盡管這與直覺意義相當吻合)。它真正的意思是X是a和B的和,也就是一個共管。現在,布爾函數的乘積運算是,所以對偶函數很自然地是。

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 .

我只是不太了解這個。|只是一個符號;在c語言中也意味着位或發生,但實際邏輯或通常是表示不同,無論是| |或∨。



| has a long history of being used to separate items in a list of mutually exclusive choices:


  1. Regular expressions: a* | b* means a string can either be 0 or more as or 0 or more bs, but not both.

    正則表達式:a* | b*表示一個字符串可以是0或更多,也可以是0或更多,但不能同時是這兩個。

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


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




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.




粤ICP备14056181号  © 2014-2020 ITdaan.com