[Haskell-cafe] Trouble with types

Stefan O'Rear stefanor at cox.net
Tue Dec 25 00:45:06 EST 2007

On Tue, Dec 25, 2007 at 08:11:34AM +0300, Konstantin Vladimirov wrote:
> [haskell]
> module TypeTrouble where
> class FirstClass a where
>     firstFunction :: (SecondClass b) => a -> b
> class SecondClass a where
>     secondFunction :: a -> Double
> [/haskell]
> I need, the firstFunction of FirstClass types to return a value of a
> SecondClass type. For example SecondData for FirstData, but for some
> FirstClass ThirdData, some SecondClass FourthData, etc.

The problem, as is often the case, is that that which is unwritten does
not resolve in the way you expect and require it to.

FirstClass' true signature is

class FirstClass a where
    firstFunction :: a -> forall b. SecondClass b => b

That is to say, any implementation of firstFunction must work for ANY
instance of SecondClass.  But you want SOME, not ANY.  And SOME
(normally notated exists tvar. tspec) is not supported in any known
dialect of Haskell.  It's possible to get fairly close with GHC
Haskell's fundeps / type families:

-- fundep version
class SecondClass b => FirstClass a b where
    firstFunction :: a -> b
-- type family version
class SecondClass (Cod a) => FirstClass a where
    type Cod a :: *
    firstFunction :: a -> Cod a

It is almost certainly possible to accomplish your goals within
standard Haskell, but the best approach is not obvious at the current
level of detail.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071224/9ae8bd8e/attachment.bin

More information about the Haskell-Cafe mailing list