Bug?

Brian Huffman bhuffman@galois.com
Wed, 17 Apr 2002 11:00:26 -0700


On Tuesday 16 April 2002 07:15 pm, Jorge Adriano wrote:
> [1] Bug1?
>
> This declaration:
> > A = (,) Int Int
>
> is accepted by ghci. Is this behaveour correct,
> 1. It kind of shadows (,) is defined in PrelTup meaning that you can no
> longer use (,) prefix to refer to tuples - like (,) 1 2.
> 2. Seems to me like (,) is not correct syntax for a consym as defined in
> the H98 Report so we shouldn't be able to redefine it.
> ...

I found that ghci (I'm using version 5.02.2) will also allow you to reuse [] 
as a constructor, as in:

> data Foo a = [] a deriving Show

For some reason, though, this has the side effect of causing a segmentation 
fault whenever I type in a literal list like [1,2,3]. Here are some other 
weird things you can try:

> data [] a = [] Int deriving Show
 --ghci complains about duplicate instances

> data [] = [] Int deriving Show

Test> [] 6543
[] 6543
Test> :i []
-- PrelBase.[] is a data constructor
PrelBase.[] :: Int*** Exception: Prelude.head: empty list

>data (,) a b = (,) b a

Test> ('a',72::Int)
('a',72)
Test> (,) 'a' (72::Int)
(97,'H')

>data (,) a b = (,) b a
>typecast :: a -> b
>typecast x = fst ((,) x undefined)

Evidently, when you redefine the datatypes [] or (,), you can change the 
internal representation of those types, without affecting their class 
instances or other functions that may have depended on the original 
representation. Obviously this does not preserve type safety.

I see two possible remedies: (1) Simply disallow [] and (,) as user-defined 
types, or (2) Allow users to define their own versions of [] and (,), but use 
qualified names to distinguish them from the originals.

- Brian Huffman