When I asked this question, one of the answers, now deleted, was suggesting that the type Either
corresponds to XOR, rather than OR, in the Curry-Howard correspondence, because it cannot be Left
and Right
at the same time.
Where is the truth?
If you have a value of type P
and a value of type Q
(that is, you have both a proof of P
and a proof of Q
), then you are still able to provide a value of type Either P Q
.
Consider
x :: P
y :: Q
...
z :: Either P Q
z = Left x -- Another possible proof would be `Right y`
While Either
does not have a specific case that explicitly represents this situation (unlike These
), it does not do anything to exclude it (as in exclusive OR).
This third case where both have proofs is a bit different than the other two cases where only one has a proof, which reflects the fact that "not excluding" something is a bit different than "including" something in intuitionistic logic, since Either
does not provide a particular witness for this fact. However Either
is not an XOR in the way that XOR would typically work since, as I said, it does not exclude the case where both parts have proofs. What Daniel Wagner proposes in this answer, on the other hand, is much closer to an XOR.
Either
is kind of like an exclusive OR in terms of what its possible witnesses are. On the other hand, it is like an inclusive OR when you consider whether or not you can actually create a witness in four possible scenarios: having a proof of P and a refutation of Q, having a proof of Q and a refutation of P, having a proof of both or having a refutation of both.[1] While you can construct a value of type Either P Q
when you have a proof of both P and Q (similar to an inclusive OR), you cannot distinguish this situation from the situation where only P has a proof or only Q has a proof using only a value of type Either P Q
(kind of similar to an exclusive OR). Daniel Wagner's solution, on the other hand, is similar to exclusive OR on both construction and deconstruction.
It is also worth mentioning that These
more explicitly represents the possibility of both having proofs. These
is similar to inclusive OR on both construction and deconstruction. However, it's also worth noting that there is nothing preventing you from using an "incorrect" constructor when you have a proof of both P and Q. You could extend These
to be even more representative of an inclusive OR in this regard with a bit of additional complexity:
data IOR a b
= OnlyFirst a (Not b)
| OnlySecond (Not a) b
| Both a b
type Not a = a -> Void
The potential "wrong constructor" issue of These
(and the lack of a "both" witness in Either
) doesn't really matter if you are only interested in a proof irrelevant logical system (meaning that there is no way to distinguish between any two proofs of the same proposition), but it might matter in cases where you want more computational relevance in the logic.[2]
In the practical situation of writing computer programs that are actually meant to be executed, computational relevance is often extremely important. Even though 0
and 23
are both proofs that the Int
type is inhabited, we certainly like to distinguish between the two values in programs, in general!
Essentially, I just mean "creating values of a type" by construction and "pattern matching" by destruction (sometimes people use the words "introduction" and "elimination" here, particularly in the context of logic).
In the case of Daniel Wagner's solutions:
Construction: When you construct a value of type Xor A B
, you must provide a proof of exactly one of A
or B
and a refutation of the other one. This is similar to exclusive or. It is not possible to construct a value of this unless you have a refutation of either A
or B
and a proof of the other one. A particularly significant fact is that you cannot construct a value of this type if you have a proof of both A
and B
and you don't have a refutation of either of them (unlike inclusive OR).
Destruction: When you pattern match on a value of type Xor A B
, you always have a proof of one of the types and a refutation of the other. It will never give you a proof of both of them. This follows from its definition.
In the case of IOR
:
Construction: When you create a value of type IOR A B
, you must do exactly one of the following: (1) provide only a proof of A
and a refutation of B
, (2) provide a proof of B
and a refutation of B
, (3) provide a proof of both A
and B
. This is like inclusive OR. These three possibilities correspond exactly to each of the three constructors of IOR
, with no overlap. Note that, unlike the situation with These
, you cannot use the "incorrect constructor" in the case where you have a proof of both A
and B
: the only way to make a value of type IOR A B
in this case is to use Both
(since you would otherwise need to provide a refutation of either A
or B
).
Destruction: Since the three possible situations where you have a proof of at least one of A
and B
are exactly represented by IOR
, with a separate constructor for each (and no overlap between the constructors), you will always know exactly which of A
and B
are true and which is false (if applicable) by pattern matching on it.
IOR
Pattern matching on IOR
works exactly like pattern matching on any other algebraic datatype. Here is an example:
x :: IOR Char Int
x = Both 'c' 3
y :: IOR Char Void
y = OnlyFirst 'a' (\v -> v)
f :: Not p -> IOR p Int
f np = OnlySecond np 7
z :: IOR Void Int
z = f notVoid
g :: IOR p Int -> Int
g w =
case w of
OnlyFirst p q -> -1
OnlySecond p q -> q
Both p q -> q
-- We can show that the proposition represented by "Void" is indeed false:
notVoid :: Not Void
notVoid = \v -> v
Then a sample GHCi session, with the above code loaded:
ghci> g x
3
ghci> g z
7
[1]This gets a bit more complex when you consider that some statements are undecidable and therefore you cannot construct a proof or a refutation for them.
[2]Homotopy type theory would be one example of a proof relevant system, but this is reaching the limit of my knowledge as of now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With