[Haskell-cafe] Data constructors

Hamilton Richards ham at cs.utexas.edu
Sun Apr 25 14:48:22 EDT 2004

At 12:25 PM -0400 4/25/04, Mark Carroll wrote:
>I keep running into annoyance in having to name data constructors
>differently if they're for different types if they're in the same module
>or something. I wish that something like some Type.Constructor syntax
>worked in order to disambiguate. Or, better still, I have that problem
>with function names too (e.g. Data.List.union, Data.Set.union, IIRC) and
>it occurs to me that a lot of this can be resolved automatically because
>the types only make sense with one of the choices.
>I'm not really proposing any changes; more, I'm wondering what others'
>thinking is about this sort of thing - what annoys them, how they get
>around it, etc.

The nonreusability of function and constructor names comes (AFAIK) 
from the need to infer types in the absence of type declarations. For 
example, if we have types

	data A = X Int | Y String

	data B = X Float | Y String

then it's not possible to determine whether f as defined by these equations

	f (X x) = X (x+3)
	f (Y s) = Y (reverse s)

has type A -> A or B -> B, and thus whether the type of that (+) is 
Int -> Int -> Int or Float -> Float -> Float.

So, in order to preserve type inference in the absence of type 
declarations, we put up with the nuisance of name collisions.

Whether the payoff is worth the price is open to question. It seems 
to be widely agreed that although type declarations aren't strictly 
necessary, they're nevertheless highly advisable as aids to the human 
reader --as verified comments, if you will. In this respect the 
Standard Prelude sets an excellent example.

If our code is going to include type declarations anyway, couldn't a 
type checker use them to resolve ambiguities created by reuse of 
names? Continuing the example, declaring

	f :: A->A

removes the ambiguity.

If this capability were added to some future version of Haskell, 
programmers would be required to declare functions' types only when 
they want to reuse names of constructors and other functions.

Of course, if thisd idea is all wet because I'm missing something, 
I'll be most grateful to be enlightened.

Regards to all,

Hamilton Richards, PhD           Department of Computer Sciences
Senior Lecturer                  The University of Texas at Austin
512-471-9525                     1 University Station C0500
Taylor Hall 5.138                Austin, Texas 78712-0233
ham at cs.utexas.edu                hrichrds at swbell.net

More information about the Haskell-Cafe mailing list