proposal for anonymous-sum syntax

Conal Elliott conal@conal.net
Fri, 21 Feb 2003 20:22:55 -0800


Looks nice to me.  For consistency, how about "(||c)" instead of
"[||c]", of type (a|b|c).  Also, I think you mean

   mapSum f g [|b] = [| g b ]


     - Conal


-----Original Message-----
From: haskell-admin@haskell.org [mailto:haskell-admin@haskell.org] On
Behalf Of Richard Nathan Linger
Sent: Friday, February 21, 2003 4:28 PM
To: Haskell Mailing List
Subject: proposal for anonymous-sum syntax


Haskell has nice syntactic support for unnamed product types (tuples).
It is as though there were builtin several datatype definitions of the
form:

	data (a,b) = (a,b)
	data (a,b,c) = (a,b,c)
	data (a,b,c,d) = (a,b,c,d)
	...

But for sum types, there is only one generic definition:

	data Either a b = Left a 
	                | Right b

If needed, we could define our own versions for larger sum types.

	data Either3 a b c = Left3 a 
	                   | Mid3 b
	                   | Right3 c

	data Either4 a b c d = Left4 a 
	                     | LMid4 b
	                     | RMid4 c
	                     | Right4 d

But the naming is already getting pretty ugly.  

Why the bias in syntactic support for products over sums?  I propose
syntactic support for unnamed sums.  Here is one way to remedy the
asymmetry.

	data (a|b) = [a|]
	           | [|b]

	data (a|b|c) = [a||]
	             | [|b|]
	             | [||c]

	data (a|b|c|d) = [a|||]
	               | [|b||]
	               | [||c|]
	               | [|||d]
	...

Here are some functions defined in this extended syntax for sums, along 
with their duals for products.

{- the either function from the Prelude -}
either :: (a -> c) -> (b -> c) -> (a|b) -> c
either f g [a|] = f a
either f g [|b] = g b

both :: (a -> b) -> (a -> c) -> a -> (b,c)
both f g a = (f a, g a)

mapSum  :: (a -> b) -> (c -> d) -> (a|b) -> (c|d)
mapSum f g [a|] = [ f a |]
mapSum f g [|b] = [| f a ]

mapProd :: (a -> b) -> (c -> d) -> (a,b) -> (c,d)
mapProd f g (a,b) = (f a, g b)

distProd :: (a,(b|c)) -> ((a,b)|(a,c))
distProd (a,[b|]) = [(a,b)|]
distProd (a,[|c]) = [|(a,c)]

At this point, the parenthesis in the types are kind of bulky.  It might

be nicer to write products with * and sums with +.

distProd :: a*(b+c) -> a*b + a*c

What do people think about this?
Has anyone else ever wished they had such support for unnamed sums?
Does this syntax grate on people or does it look reasonable?
Does anyone have thoughts on why Haskell is biased towards products?

Nathan Linger
Student at OGI
www.cse.ogi.edu

_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell