為什么在定義中使用“or”而不是“xor”?

[英]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:

haskell數據類型的定義如下:

data Bool = False | True

In xml qualified names are defined like this:

在xml中,限定名的定義如下:

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.

通常,|(管道或酒吧)應該被理解為或。但這似乎很奇怪。當A和B都為真時,A或B也是正確的。雖然在第一個例子中它是有意義的(有可能某些東西同時是真的和假的,但是我們隱式地假設了不矛盾的法則),但是在第二個例子中它不是:事物不是一個前綴名,就是一個UnprefixedName,它不可能同時是兩個。

So why is this often put like this? Why not use exclusive or? Are there any non-conventional reasons?

為什么這個經常是這樣的呢?為什么不使用獨占或?有什么非常規性的原因嗎?

3 个解决方案

#1


8  

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

盡管如此,布爾空間上的和運算實際上是XOR,所以我們有點…

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語言中也意味着位或發生,但實際邏輯或通常是表示不同,無論是| |或∨。

#2


2  

| 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:

    代表上下文無關語法的Backus-Naur形式:

    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.

    在這里,git分支命令可以選擇-d或-d選項,但不能同時調用這兩個選項。

The data statement in Haskell continues this tradition; it is unrelated to the use of | as a logical or bit-wise operator.

Haskell的數據聲明延續了這一傳統;它與使用|作為邏輯運算符或位運算符無關。

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

(如果有的話,|在位元上的使用可能是受到了Backus-Naur表單的啟發,在這種情況下,您可能會問為什么|用於或代替了XOR。)

#3


1  

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.

雖然使用“或”操作符符號可能並不完全符合邏輯,但它在讓讀者理解這一點上做得很好。這種清晰性最終是一種語言所追求的東西。只要更大比例的讀者能夠解釋它作為一個使用“文字”而不是“數學”操作符使用,使用或在字面意義上意味着你,作為一個語言,正在做一份更好的工作,讓你的觀點,使該方法優於使用XOR等東西。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/29/57bbe34c7816999371cdd5334f3e5d04.html



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