Mutable Hastable:非法类型同义词系列应用程序实例

问题描述

我正在尝试使用此库中的 Mutable BasicHashTable: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 )

编译器抱怨我试图在类型类实例中使用 A

 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,MonadState A )
   |                                                   ^^^^^^^^^^^^

解决方法

我觉得这很可怕,因为 PrimState 是一个类型家族。试试这个:

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

您得到的编译错误告诉我们它无法处理类型系列。如果您查看哈希表类型的定义,您会发现它抱怨的 PrimState 用法:

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

所以你可以直接自己使用,because

type instance PrimState IO = RealWorld

事实上。我什至会在上游提交 PR 并修复:

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

因为没有充分的理由像现在这样定义它