[Haskell-cafe] Can not use ST monad with polymorphic function

Heinrich Apfelmus apfelmus at quantentunnel.de
Sun Dec 2 11:48:36 CET 2012


David Menendez wrote:
> On Thu, Nov 29, 2012 at 7:50 AM, Dmitry Kulagin <dmitry.kulagin at gmail.com>wrote:
> 
>> Thank you, MigMit!
>>
>> If I replace your type FoldSTVoid with:
>> data FoldMVoid = FoldMVoid {runFold :: Monad m => (Int -> m ()) -> m ()}
>>
>> then everything works magically with any monad!
>> That is exactly what I wanted, though I still do not quite understand why
>> wrapping the type solves the problem
>>
> 
> Short answer: It's because GHC's type system is predicative.
> 
> Basically, quantified types can't be given as arguments to type
> constructors (other than ->, which is its own thing). I'm not entirely sure
> why, but it apparently makes the type system very complicated from a
> theoretical standpoint. By wrapping the quantified type in a newtype, the
> argument to IO becomes simple enough not to cause problems.

GHC has an extension -XImpredicativeTypes that lifts this restriction, 
but in my experience, it doesn't work very well. A newtype

      data Foo = Foo { bar :: forall a . baz a }

usually works best.


Best regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com




More information about the Haskell-Cafe mailing list