首页 > 解决方案 > 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, Applicative, Monad, MonadIO, MonadState A )
   |                                                   ^^^^^^^^^^^^

标签: haskellhashtabletype-familiestype-synonyms

解决方案


我认为它吓坏了,因为它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

因此您可以自己直接使用它,因为

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

因为没有充分的理由以现在的方式定义它


推荐阅读