[Haskell] Re: No fun with phantom types

Chung-chieh Shan ccshan at post.harvard.edu
Thu Oct 23 18:44:06 EDT 2008

Michael Marte <marte at pms.informatik.uni-muenchen.de> wrote in article <200810232156.19080.marte at pms.informatik.uni-muenchen.de> in gmane.comp.lang.haskell.general:
> *FD> :type let x = (1::Int) in x #+ x #+ x
> let x = (1::Int) in x #+ x #+ x :: (MakeAExp (AExp s) s1) => AExp s1
> It appears to me that ghci generates two phantom types s and s1 and fails to 
> unify them.

Because you may define other MakeAExp instances elsewhere, ghc can't
unify s and s1.  For example, if you were to define

    instance MakeAExp (FDVar s) [s] ...
    instance MakeAExp (AExp [s]) s ...

then s could be [s1] in your type!

You can probably add functional dependencies to fix this problem, but
I would suggest that you get rid of MakeAExp altogether.  Just give
(#+) and its friends the type "AExp s -> AExp s -> AExp s", with no
type-class constraint.  You need to explicitly inject FDVar into AExp,
but most of the time you probably just need to handle AExp values
without caring that they are constructed with Variable.  You also need
to inject Int into AExp, unless you simply make "instance Num (AExp s)".

Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
Lemonade was a popular drink and it still is.

More information about the Haskell mailing list