Michael Marte wrote:

> (First I defined
> 
> class Eager a where
>     eager :: a -> a
> 
> and some instances:
> 
> instance Eager Int where
>     eager i = i `seq` i

For Int (and other base types), forcing to WHNF is as far as you can go,
so I think you can simplify the above to

  > instance Eager Int where
  >     eager i = i

> instance Eager a => Eager [a] where
>     eager [] = []
>     eager (x : xs) = eager x `seq` eager xs `seq` (x : xs)

If you want eager to preserve identity (i.e. eager x `unsafePtrEq` x),
as well as to avoid duplicating data, you might prefer: 

  > instance Eager a => Eager [a] where
  >     eager xs@[] = xs
  >     eager xs@(x : xs') = eager x `seq` eager xs' `seq` xs


> instance Eager a => Eager (a, a) where
>     eager (x, y) = eager x `seq` eager y `seq` (x, y)

I think the following is more general:

  > instance (Eager a, Eager b) => Eager (a, b) where
  >     eager xy@(x, y) = eager x `seq` eager y `seq` xy

Ultimately, generic Haskell may come handy.

-- Zhanyong Wan
Dept of Computer Science, Yale University

Reply via email to