[Haskell-cafe] multiple declarations error in Haskell - should it be relaxed?

11 views
Skip to first unread message

Andrew Butterfield

unread,
Nov 28, 2014, 6:42:43 AM11/28/14
to Haskell Cafe
Condsider the following Haskell program (fragment):

f 0 = “zero”
g 0 = “NULL”
f n = “non-zero”
g n = “PRESENT”

This will result in two “Multiple Declaration” errors.
There is a good motivation for this - disallowing such an interleaving
of declarations makes it easy for the compiler to capture a common
typo, namely errors of the following form - here an attempt to define
a single function called myFun.

myFun 0 = “zero”
myfun 1 = “one”
myFun n = “too big!”

However I have use-cases where it would be nice to interleave as per
the first example above - with markedly different function names.
It invokes a large case analysis, where I have other auxiliary functions
associated with each case, but which I’d like to
(1) have at the top-level for testability
(2) keep textually local to the case with which they are associated.

I don’t think there is a language extension to disable the multiple
declaration check - but would such a feature we possible. I’d see it
as one which still performs the check, but issues a warning rather
than an error - particularly if it notices that the interleaved names
are very similar.

Is this a reasonable suggestion, or are there other reasons for not doing
this that I’ve missed?

Maybe there is a better way to satisfy (1) and (2) above?

Regards,
Andrew
--------------------------------------------------------------------
Andrew Butterfield Tel: +353-1-896-2517 Fax: +353-1-677-2204
Lero@TCD, Head of Foundations & Methods Research Group
School of Computer Science and Statistics,
Room G.39, O'Reilly Institute, Trinity College, University of Dublin
http://www.scss.tcd.ie/Andrew.Butterfield/
--------------------------------------------------------------------

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Tom Ellis

unread,
Nov 28, 2014, 6:53:45 AM11/28/14
to haskel...@haskell.org
On Fri, Nov 28, 2014 at 11:44:16AM +0000, Andrew Butterfield wrote:
> Condsider the following Haskell program (fragment):
>
> f 0 = “zero”
> g 0 = “NULL”
> f n = “non-zero”
> g n = “PRESENT”
>
> This will result in two “Multiple Declaration” errors.

Here's a somewhat silly answer, but it may be of help.

data T = F | G

fun :: T -> Int -> String
fun F 0 = "zero"
fun G 0 = "NULL"
fun F n = "non-zero"
fun G n = "PRESENT"

Jeffrey Brown

unread,
Nov 28, 2014, 4:09:29 PM11/28/14
to Tom Ellis, haskell-cafe
The problem seems to boil down to the fact that (at least in every IDE I am aware of) the order in which the compiler reads your code is the same as the order in which the code is displayed to you as you write it. 

A general solution would be to extend the IDE to allow hyperlinks, ignored by the compiler, from one part of the code to another. It is something one could write in Emacs Lisp, to work across multiple languages.

Richard Eisenberg

unread,
Nov 30, 2014, 3:05:49 PM11/30/14
to Andrew Butterfield, Haskell Cafe
This seems reasonable to me. If you want this, post a feature request at https://ghc.haskell.org/trac/ghc/newticket

Richard
Reply all
Reply to author
Forward
0 new messages