[Haskell-cafe] Avoiding parametric function binding

Yucheng Zhang yczhang89 at gmail.com
Sun Jan 1 06:48:09 CET 2012

On Sat, Dec 31, 2011 at 11:09 PM, Kevin Quick <quick at sparq.org> wrote:
>> varElem :: forall x . (Show x) => Var -> x
>> varElem (V1 x) = x
>> varElem (V2 x) = x
>> main = putStrLn . elemStr . varElem $ test

The problem here is that you want the return type to depend on the
'value' of Var,
which is not known until runtime.

Maybe you can wrap the 'conflicting' return type inside an existential type:

data VarWrap = forall x . (Show x) => Wrap x

instance Show VarWrap where
    show (Wrap x) = show x

varElem :: Var -> VarWrap
varElem (V1 x) = Wrap x
varElem (V2 x) = Wrap x

You will need an 'ExistentialQuantification' language extension to do this.

More information about the Haskell-Cafe mailing list