On 11-May-1998, Simon L Peyton Jones <[EMAIL PROTECTED]> wrote:
> 
> Since GHC keeps the types right through the compiler, it
> really can't do CSE on two terms of type
> 
>       Either Int  Int
>       Either Bool Int
> 
> even if they are both applications of Right.
>
> Actually, GHC does finally discard type information right at the
> end, so we could do an extra bit of CSE there, but frankly I doubt
> it would buy very much.

A simpler way of doing this is for the CSE pass to just insert a call
to a compiler builtin function `$unsafe_cast' if the types don't
match.  At the very end when you finally discard type information you
can then optimize away the call to `$unsafe_cast'.  This provides the
same benefits without the need for an extra pass. 

It probably doesn't buy a lot, but IMHO it's easy enough that it's worth
doing anyway.

> Incidentally, I don't think it would be sensible to change
> the type system to allow the 
> 
>  demo1 :: (a -> b) -> Either a c -> Either b c
>  demo1 f (Left  a)   = Left (f a)
>  demo1 _ r@(Right c) = r
>
> What type does r have?  Either a c.
> What type does the result of the fn have?  Either b c.
> Different types.

I agree, this code should be disallowed.

Note that the different types can lead to different semantics.
Consider the following code, which is similar to the code above:

        foo :: (Int -> Float) -> Either Int Char -> Either Float Char
        foo f (Left  a)   = Left (f a)
        foo _ r@(Right c) = classmethod r

        class Demo t where
                classmethod :: t -> Either Float Char

        instance Demo Either Int Char where
                classmethod = m1

        instance Demo Either Float Char where
                classmethod = m2

Here `r' has type `Either Int Char', not `Either a Char',
and this determines which class method is called.
Note that writing

        foo _ r@(Right c) = classmethod (Right c)

would result in compile error (or worse) due to an uninstantiated
type variable.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.


Reply via email to