Мне нужна структура данных, которая содержит, возможно, A
, возможно, B
и определенно один из них. Если бы я взломал общий тип данных для этой вещи, это, вероятно, выглядело бы так:
data OneOrBoth a b = A a | B b | AB a b
maybeA :: OneOrBoth a b -> Maybe a
maybeB :: OneOrBoth a b -> Maybe b
eitherL :: OneOrBoth a b -> Either a b -- Prefers a
eitherR :: OneOrBoth a b -> Either a b -- Prefers b
hasBoth, hasExactlyOne, hasA, hasB :: OneOrBoth a b -> Bool
Имеет ли эта структура данных имя? Существует ли канонический способ обработки одно- или двух структур в Haskell?