seq as a class method

Wolfgang Jeltsch wolfgang at jeltsch.net
Thu Mar 23 09:52:54 EST 2006


Hello,

it seems that there is not yet a ticket about putting seq into a type class 
(again).

In my opinion, having seq available for every type is a serious flaw.  One 
problem is that the law f = \x -> f x doesn't hold anymore since the equation 
is false for f = _|_.  There was also a discussion on one of the mailing 
lists some time ago which revealed that the Monad instance for IO doesn't 
satisfy the monad laws because of the availability of seq for IO, I think.

In addition, the automatic definition of seq for every type can make 
implementation details visible which were thought of as completely hidden.  
For example, it might make a difference whether one uses data or newtype for 
a one-alternative-one-field datatype, even if the data constructor is hidden.

I therefore propose to declare a class like this:

	class Seq a where
		seq :: a -> b -> b

(Perhaps the class name should be chosen better.)  Generation of standard 
instances should be supported via deriving clauses or Template Haskell.  For 
an algebraic datatype declared via

	data T a1 ... an = C1 t11 ... t1m1 | ... | Ck tk1 ... tkmk

the implementation of seq should be something like this:

	seq (C1 _ ... _) = id
	seq _ = id

What do others think?

Best wishes,
Wolfgang


More information about the Haskell-prime mailing list