[Haskell-cafe] [solved] forall disappear from type signature

Takayuki Muranushi muranushi at gmail.com
Fri Nov 2 11:10:44 CET 2012


Today, I encountered a strange trouble with higher-rank polymorphism. It
was finally solved by nominal typing. Was it a bug in type checker? lack of
power in type inference? or lack of my understanding? I'll submit this in
hope that it will at least result in better GHC error message some day:

My following code

https://github.com/sugoi/sugoi/blob/0aec19b29274ddb7c5ae46b9b733e91bae0790b2/Sugoi/Types.hs

> runDB :: Lens NetworkState RIB
> runDB = lens (f::NetworkState -> RIB) (\x s -> s { _runDB = x })
>  where f :: NetworkState -> RIB

Didn't typecheck with following error:

Sugoi/Types.hs:38:15:
    Couldn't match expected type `RIB'
                with actual type `DB.DBMT (Maybe Int) IO a0
                                  -> IO (StM (DB.DBMT (Maybe Int) IO) a0)'
    Expected type: NetworkState -> RIB
      Actual type: NetworkState
                   -> DB.DBMT (Maybe Int) IO a0
                   -> IO (StM (DB.DBMT (Maybe Int) IO) a0)
    In the first argument of `lens', namely
      `(f :: NetworkState -> RIB)'
    In the expression:
      lens (f :: NetworkState -> RIB) (\ x s -> s {_runDB = x})


How come an expression `(f :: NetworkState -> RIB)' is not of type
NetworkState -> RIB ?
When I changed the definition of runDB as follows,

> runDB = lens (\NetworkState{_runDB = x} -> x) (\x s -> s { _runDB = x })

The source of error was more clear:

    Expected type: DB.DBMT (Maybe Int) IO a1
                   -> IO (StM (DB.DBMT (Maybe Int) IO) a1)
      Actual type: DB.DBMT (Maybe Int) IO a
                   -> IO (StM (DB.DBMT (Maybe Int) IO) a)

So, ghc couldn't unify these two types. By using the nominal typing, namely

> - type RIB = RunInBase (DB.DBMT (Maybe Int) IO) IO
> + newtype RIB = RIB (RunInBase (DB.DBMT (Maybe Int) IO) IO)

the problem was solved.

Always grateful to haskell-cafe from being there,
-- 
Takayuki MURANUSHI
The Hakubi Center for Advanced Research, Kyoto University
http://www.hakubi.kyoto-u.ac.jp/02_mem/h22/muranushi.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121102/4ee150f4/attachment.htm>


More information about the Haskell-Cafe mailing list