[Haskell-cafe] Refactoring status

Henning Thielemann lemming at henning-thielemann.de
Fri Jan 4 04:50:16 EST 2008

On Thu, 3 Jan 2008, Peter Verswyvelen wrote:

> > I believe type signatures are the very essence of Haskell documentation!
> > I'd much rather see a program with type signatures for functions and
> > little (or no) comments over programs with no type signatures and
> > ambigious comments (if any comments at all!).
> Okay, but when using a syntax directed editor, type signatures can be
> automatically provided because the types are known.

Types cannot always be derived automatically, especially when coming to
Haskell extensions. Sometimes you also want to restrict the type. E.g. for

  asTypeOf _ y = y

you explicitly want the type

  asTypeOf :: a -> a -> a

not the automatically derived one:

  asTypeOf :: b -> a -> a

> Furthermore, IMHO, type signatures alone are not enough, a good parameter
> name says at least as much as the type.
> E.g. what does a function Int -> Int -> Bool do? I have no idea. A good
> function name helps, e.g. isDivisible:: Int -> Int -> Bool. But then I still
> don't know which parameter is the numerator and denominator. So good names
> for the parameters are at least as important, e.g. isDivisible ::
> numerator:Int -> denonimator:Int -> Bool

It's a problem in Haskell that there are no unique parameter names, due to
pattern matching. E.g.

isDivisible _ 0 = error "division by zero"
isDivisible x y = ...

I'm tempted to write Haddock comments like

{- | check whether @x@ can be divided by @y@ -}
isDivisible :: Integral a => a -> a -> a

But this does not work, because unique parameter names cannot be extracted
from the code and are thus missing in Haddock documentation. If there
would not be pattern matching but only 'case' there wouldn't be a problem.

isDivisible x y =
   case (x,y) of
      (_,0) -> error "division by zero"
      (x',y') -> ...

Or even better, with a fictitious anonymous 'case' you could write:

isDivisible = curry $
      (_,0) -> error "division by zero"
      (x,y) -> ...

> > Type signatures really does make dealing with someone elses code that
> > much easier.
> Yes, as is good documentation, which unfortunately is still limited to
> ASCII. I would prefer to have rich documentation right inside my source
> code, with math symbols, drawings, pictures, animations, whatever...

... interactive Haskell sandbox ...

More information about the Haskell-Cafe mailing list