[Haskell-cafe] Formatting function types

Lauri Alanko la at iki.fi
Thu Dec 30 18:38:33 CET 2010


On Thu, Dec 30, 2010 at 10:39:29AM -0600, Larry Evans wrote:
> Lauri, I assume then that you want to draw special attention to
> the return type instead of the first argument type.

Only to the fact that the return type is of a different nature than
the argument types, and that all the argument types are of the same
nature. (More technically, the argument types occur in negative
positions within the function type, whereas the return type occurs in
a positive position.)

> So, with this "operator postfix" formatting, the special
> attention to the return type is achieved by *not* suffixing it
> with ->.

Partly, but more by the fact that it is the last item in the list. Of
course there are ways to make it even more prominent, e.g. by
inserting an empty line before it. The arrows are not really essential
for readability, although they can help a bit if they are aligned:

openTempFile ::
    FilePath ->
    String   ->
    IO (FilePath, Handle)

> OK, but then why not just find the last argument and forget about
> finding the missing postfix ->?  Well, in that case, the "operator
> prefix" formatting would serve just as well.

Except that it falsely suggests that the last argument types are of a
similar nature as the return type. Here's what went in my head when I
first read Haskell code:

openTempFile ::
       FilePath -- All right, this first part is probably the argument.
    -> String -- this comes after the arrow, so this must be the return type.
    -> IO (FilePath, Handle) -- And then we have another return type? Huh?

It took me quite a while to understand that -> associates to the
right, because the layout so strongly suggested otherwise. Obviously,
with time one can get used to anything, but I still stand by my
opinion that the above convention is inherently misleading.

> [1]:
>   openTempFile
>      :: FilePath -- ^ filename, of course
>      -> String -- ^ comment for 2nd arg.
>      -> IO (FilePath, Handle) -- ^ comment for 3ird arg

3rd arg? This is just the sort of lapse that the above syntax induces.

> [2]:
>   openTempFile::
>      FilePath {- ^ filename, of course -} ->
>      String {- ^ comment for 2nd arg. -} ->
>      IO (FilePath, Handle) -- ^ comment for 3ird arg

This is admittedly ugly. I'd prefer:

openTempFile ::
    FilePath ->            -- ^ foo
    String ->              -- ^ bar
    IO (FilePath, Handle)  -- ^ baz

If Haddock doesn't support an intervening -> between the type and the
documentation comment, it probably should.

(Personally, I don't like end-of-line comments because they quickly run
out of space and extending them to multiple lines is awkward. So maybe
even better would be:

openTempFile ::
    FilePath ->
    -- ^ foo
    String ->
    -- ^ bar
    IO (FilePath, Handle)
    -- ^ baz

But this is no longer relevant to the issue at hand.)


Lauri



More information about the Haskell-Cafe mailing list