[Haskell-cafe] Re: If wishes were horses...

Ketil Malde ketil at malde.org
Fri Mar 12 05:38:08 EST 2010

Johannes Waldmann <waldmann at imn.htwk-leipzig.de> writes:

> Well, meaningful identifier names is nice, but I think
> here we have a case of the code smell "type info embedded in the name".
> Strictness of a function should be expressed in the function's type instead.

I've stumbled into this sentiment before, and it's one of those "I must
be dumb because everybody else takes this for granted"-issues.¹  Are
there any explanation of how/why this is a type issue?  To me it appears
natural to use the same higher order functions (say, foldr) with strict
operations or lazy ones.  OTOH, I can't off-hand think of a reasonable
use for foldl or foldr', so you're probably right they should be separated.

> Prelude Data.List> :t foldl'
> foldl' :: (a -> b -> a) -> a -> [b] -> a

> Prelude Data.List> :t foldl
> foldl :: (a -> b -> a) -> a -> [b] -> a

What should the type look like?  If memory serves, Clean allows bangs in
type signatures, something like:
  foldl' :: (a -> b -> a) -> !a -> [b] -> a

but I thought it just added a seq under the hood, much like bang
patterns like

  foldl' f !z xs = ...

do in Haskell, so it's not like !a is a separate type.  Or?

Let me explore a bit here.  In deference to the time-honored if
ill-advised tradition of abusing the apostrophe, let a' be the type a,
but strictly evaluated.  Perhaps things could look something like:

  foldl' :: (a' -> b' -> a) -> a' -> b
  (+) :: a' -> a' -> a     -- strict
  (:) :: a  -> [a] -> [a]  -- non-strict

  foldl' (+) 0 [1..10]         -- okay
  foldl' (flip (:)) [] [1..10] -- illegal

Would something like this work?  It seems to me that strictness doesn't
apply to result types (x `seq` x === x), and you need to track the
relationship between a and a', since you probably want to allow a lazy
value as e.g. the second parameter of foldl'.

I'm sure this has been discussed to death ages ago, is there a simple
overview I might understand that discusses or summarizes the issue?


¹ As opposed to the "everybody else must be dumb, because they disagree
with me"-issues.  Perhaps I can work out my IQ from the ratio?
If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list