如果您想要“零、一或两者”,您将拥有1 + A + B + A*B = (1 + A) * (1 + B)
or (Maybe A, Maybe B)
。
您可以A + B + A*B = (1+A)*(1+B)-1
通过包装(Maybe A, Maybe B)
anewtype
并使用智能构造函数来删除(Nothing,Nothing)
:
module Some (
Some(),
this, that, those, some,
oror, orro, roro, roor,
swap
) where
import Control.Applicative ((<|>))
newtype Some a b = Some (Maybe a, Maybe b) deriving (Show, Eq)
-- smart constructors
this :: a -> Some a b
this a = Some (Just a,Nothing)
that :: b -> Some a b
that b = Some (Nothing, Just b)
those :: a -> b -> Some a b
those a b = Some (Just a, Just b)
-- catamorphism/smart deconstructor
some :: (a -> r) -> (b -> r) -> (a -> b -> r) -> Some a b -> r
some f _ _ (Some (Just a, Nothing)) = f a
some _ g _ (Some (Nothing, Just b)) = g b
some _ _ h (Some (Just a, Just b)) = h a b
some _ _ _ _ = error "this case should be unreachable due to smart constructors"
swap :: Some a b -> Some b a
swap ~(Some ~(ma,mb)) = Some (mb,ma)
-- combining operators
oror, orro, roro, roor :: Some a b -> Some a b -> Some a b
-- prefer the leftmost A and the leftmost B
oror (Some (ma,mb)) (Some (ma',mb')) = Some (ma <|> ma', mb <|> mb')
-- prefer the leftmost A and the rightmost B
orro (Some (ma,mb)) (Some (ma',mb')) = Some (ma <|> ma', mb' <|> mb)
-- prefer the rightmost A and the rightmost B
roro = flip oror
-- prefer the rightmost A and the leftmost B
roor = flip orro
组合运算符很有趣:
λ this "red" `oror` that "blue" `oror` those "beige" "yellow"
Some (Just "red",Just "blue")
λ this "red" `orro` that "blue" `orro` those "beige" "yellow"
Some (Just "red",Just "yellow")
λ this "red" `roor` that "blue" `roor` those "beige" "yellow"
Some (Just "beige",Just "blue")
λ this "red" `roro` that "blue" `roro` those "beige" "yellow"
Some (Just "beige",Just "yellow")