[Haskell-cafe] returning a polymorphic function

Andres Löh andres at well-typed.com
Mon Jun 15 14:20:07 UTC 2015


Hi.

The most robust way to achieve this is to define

> {-# LANGUAGE RankNTypes #-}
>
> newtype PolyId = PolyId (forall a. a -> a)
>
> f :: IO PolyId
> f = return (PolyId id)
>
> main = do
>   PolyId g <- f
>   print $ g 1
>   print $ g "ciao"

You can also do this more directly using ImpredicativeTypes, but that
language extension is quite fragile and not really supported.

Cheers,
  Andres

-- 
Andres Löh, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com


More information about the Haskell-Cafe mailing list