Re: learning to love laziness

2003-09-24 Thread Richard Nathan Linger


On Wed, 24 Sep 2003, Norman Ramsey wrote:

 Consider the following Haskell function:
 
  asPair x = (fst x, snd x)
 
 This function has type forall a b. (a, b) - (a, b)
 and is almost equivalent to the identity function, except it
 can be used to make programs terminate that might otherwise fall
 into a black hole.
 

What is an example program that asPair rescues from nontermination?

Nathan


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


proposal for anonymous-sum syntax

2003-02-21 Thread Richard Nathan Linger

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
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell