[Haskell-cafe] Why aren't there anonymous sum types in Haskell?
mike.erickson at gmail.com
Tue Jun 21 23:31:59 CEST 2011
On Tue, Jun 21, 2011 at 1: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.
The syntax is truly awful, but this doesn't seem that far from
foo :: Either Int (Either Bool (Either String Double)) -> Int
foo (Left e) = e + 7
foo (Right (Left e )) = if e then 1 else 0
foo (Right (Right (Left e))) = length e
foo (Right (Right (Right e))) = floor e
foo . Right . Left $ True
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe