[Haskell-cafe] Why aren't there anonymous sum types in Haskell?

David Barbour dmbarbour at gmail.com
Wed Jun 22 01:34:28 CEST 2011


On Tue, Jun 21, 2011 at 1:36 PM, Matthew Steele <mdsteele at alum.mit.edu>wrote:

> 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.
>
>
Haskell operators are pretty flexible. You can get something really close
without much effort. Consider:

 import Data.Either
 type (:|:) a b = Either a b
 (???) = either

 foo :: (Int :|: Bool :|: String :|: Double) -> Int
 foo =
    \ i  -> i + 7  ???
    \ b -> if b then 1 else 0 ???
    \ s -> length s ???
    \ d -> floor d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110621/da24cd2e/attachment.htm>


More information about the Haskell-Cafe mailing list