haskellhashtabletype-familiestype-synonyms

Mutable Hastable : Illegal type synonym family application in instance


I'm trying to use Mutable BasicHashTable from this lib : https://github.com/gregorycollins/hashtables

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import qualified Data.HashTable.IO as H
import Control.Monad.State.Strict
import Control.Monad.IO.Class (MonadIO)

type A  =  H.BasicHashTable Int String

newtype MyWrapper a = MyWrapper { runM :: StateT A IO a  }
  deriving (Functor, Applicative, Monad, MonadIO, MonadState A )

The compiler is complaining on my attempts to use A in a typeclass instance:

 error:
    • Illegal type synonym family application ‘Control.Monad.Primitive.PrimState
                                                 IO’ in instance:
        MonadState A MyWrapper
    • In the newtype declaration for ‘MyWrapper’
   |
10 |   deriving (Functor, Applicative, Monad, MonadIO, MonadState A )
   |                                                   ^^^^^^^^^^^^

Solution

  • I think it freaks out because PrimState is a type family. Try this:

    import Control.Monad.ST (RealWorld)
    import qualified Data.HashTable.ST.Basic as B
    type A = B.HashTable Int String RealWorld
    

    The compilation error you get tells us that it can't handle the type family. If you look at definition of hash table type you'll find the PrimState usage it complains about:

    import qualified Data.HashTable.ST.Basic as B
    type BasicHashTable k v = IOHashTable (B.HashTable) k v
    type IOHashTable tabletype k v = tabletype (PrimState IO) k v
    

    Therefore you can use it directly yourself, because:

    type instance PrimState IO = RealWorld
    

    In fact. I'd even submit a PR upstream with a fix:

    - type IOHashTable tabletype k v = tabletype (PrimState IO) k v
    + type IOHashTable tabletype k v = tabletype RealWorld k v
    

    because there is no good reason to have it defined in the way it is now