[Haskell-cafe] Why aren't there anonymous sum types in Haskell?
aslatter at gmail.com
Tue Jun 21 23:47:28 CEST 2011
On Tue, Jun 21, 2011 at 3:36 PM, Matthew Steele <mdsteele at alum.mit.edu> wrote:
> On Jun 21, 2011, at 4:02 PM, Malcolm Wallace wrote:
>> On 21 Jun 2011, at 20:53, Elliot Stern wrote:
>>> A tuple is basically an anonymous product type. It's convenient to not
>>> have to spend the time making a named product type, because product types
>>> are so obviously useful.
>>> Is there any reason why Haskell doesn't have anonymous sum types? If
>>> there isn't some theoretical problem, is there any practical reason why they
>>> haven't been implemented?
>> The Either type is the nearest Haskell comes to having anonymous sum
>> If you are bothered because Either has a name and constructors, it does
>> not take long before you realise that (,) has a name and a constructor too.
> Yes, Either is to sum types what (,) is to product types. The difference is
> that there is no "anonymous" sum type equivalent to (,,) and (,,,) and
> (,,,,) and so on, which I think is what the original question is getting at.
> Indeed, I sometimes wish I could write something like (straw-man syntax):
> foo :: (Int | Bool | String | Double) -> Int
> foo x =
> case x of
> 1stOf4 i -> i + 7
> 2ndOf4 b -> if b then 1 else 0
> 3rdOf4 s -> length s
> 4thOf4 d -> floor d
> bar :: Int
> bar = foo (2ndOf4 True)
> and have that work for any size of sum type. But I can't.
Why not just do what we do for tuples? Define a bunch of generic types up front:
data Choice2 a b = OneOf2 a | TwoOf2 b
data Choice3 a b c = OneOf3 a | TwoOf3 b | ThreeOf3 c
Then you wouldn't need any special compiler support, and you can
release it on its own library on Hackage.
I don't think I would use it, be to each their own!
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe