[Haskell-cafe] Re: How do I simulate dependent types using phantom types?

DavidA polyomino at f2s.com
Sat Aug 18 16:27:30 EDT 2007


Twan van Laarhoven <twanvl <at> gmail.com> writes:

> The solution is to use a dummy parameter:
>  > class IntegerType a where
>  >     value :: a -> Integer
> And call it like:
>  > f = value (undefined :: Two)
> 
> So for instance:
>  > instance IntegerType d => Show (QF d) where
>  >     show (QF a b) = show a ++ " + " ++ show b ++ " sqrt "
>  >                   ++ show (value (undefined::d))

Thanks to all respondents for this suggestion. That works great.

> 
> The problem is that this doesn't work, because d is not in scope, you 
> need the scoped type variables extension:
> 
>  > valueOfQF :: forall a. IntegerType a => QF a -> Integer
>  > valueOfQF qf = value (undefined :: a)

Well actually, your first attempt *did* work for me (using GHC 6.6.1). Is this 
not behaviour that I can rely on?



More information about the Haskell-Cafe mailing list