我无法重现该问题。
如果没有 for 的签名trie,我会得到 - 不出所料 - 一个模棱两可的类型变量错误(嗯,两个),由于问题中所述的原因,以及一个可能的原因(这确实是原因,它是单态性限制),以及两个可能的修复(禁用单态限制使其编译,但给出明确的类型签名是更好的修复)。错误消息附加在下面,用于过度感兴趣的灵魂(它来自 7.6.1)。
使用类型签名,它可以干净地编译。
鉴于此,我无法确定地诊断问题,但经验与您收到的错误消息一起提供了可能的原因。
您收到一条非常具体的消息,表明所需的实例不在范围内:
No instance for (Data.ListTrie.Base.Map.Map Map.Map Char) arising from a use of TrieM.fromListWith'
Possible fix: add an instance declaration for (Data.ListTrie.Base.Map.Map Map.Map Char)
另一方面,list-tries 包明确提供了这样一个实例。
这种情况的典型原因是涉及两个不同的类或两种不同的类型,它们来自同一包的不同版本。
在这种情况下,您可能已经构建list-tries了 的一个版本containers,然后安装了不同版本的containers,并且从较新的包版本import qualified Data.Map as Map导入了类型,而 in中的实例用于旧版本。Map.Maplist-tries
containers检查您是否安装了多个版本
ghc-pkg list containers
检查containers您list-tries依赖的版本
ghc-pkg describe list-tries
在depends该输出的字段中,会出现类似
containers-0.5.0.0-e49be7a240765a4edc5c09f677ec6a81
列出包版本和构建containers包的 ABI 哈希。list-tries
如果版本与您安装的最新版本不对应,containers则会出现上述情况,除非containers在调用 GHC 时明确指定要使用的版本,直接使用-package标志或间接通过.cabal文件。
如果是这种情况,您可以
- containers每次使用时明确指定要使用的版本- list-tries
- 注销较新版本的containers(可能会破坏其他软件包)
- list-tries针对新版本重建- containers
TrieTest.hs:12:8:
    No instance for (Data.ListTrie.Base.Map.Map map0 Char)
      arising from a use of TM.fromListWith'
    The type variable `map0' is ambiguous
    Possible cause: the monomorphism restriction applied to the following:
      trie :: TM.TrieMap map0 Char (TS.TrieSet map1 Char)
        (bound at TrieTest.hs:12:1)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction
    Note: there are several potential instances:
      instance Eq k =>
               Data.ListTrie.Base.Map.Map Data.ListTrie.Base.Map.AList k
        -- Defined in `Data.ListTrie.Base.Map'
      instance Ord k => Data.ListTrie.Base.Map.Map Map.Map k
        -- Defined in `Data.ListTrie.Base.Map'
      instance Enum k =>
               Data.ListTrie.Base.Map.Map Data.ListTrie.Base.Map.WrappedIntMap k
        -- Defined in `Data.ListTrie.Base.Map'
    Possible fix:
      add an instance declaration for
      (Data.ListTrie.Base.Map.Map map0 Char)
    In the expression: TM.fromListWith' (flip TS.union)
    In the expression:
      TM.fromListWith' (flip TS.union)
      $ map (\ v -> (sort v, TS.singleton v)) vocabs
    In an equation for `trie':
        trie
          = TM.fromListWith' (flip TS.union)
            $ map (\ v -> (sort v, TS.singleton v)) vocabs
TrieTest.hs:12:31:
    No instance for (Data.ListTrie.Base.Map.Map map1 Char)
      arising from a use of `TS.union'
    The type variable `map1' is ambiguous
    Possible cause: the monomorphism restriction applied to the following:
      trie :: TM.TrieMap map0 Char (TS.TrieSet map1 Char)
        (bound at TrieTest.hs:12:1)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction
    Note: there are several potential instances:
      instance Eq k =>
               Data.ListTrie.Base.Map.Map Data.ListTrie.Base.Map.AList k
        -- Defined in `Data.ListTrie.Base.Map'
      instance Ord k => Data.ListTrie.Base.Map.Map Map.Map k
        -- Defined in `Data.ListTrie.Base.Map'
      instance Enum k =>
               Data.ListTrie.Base.Map.Map Data.ListTrie.Base.Map.WrappedIntMap k
        -- Defined in `Data.ListTrie.Base.Map'
    Possible fix:
      add an instance declaration for
      (Data.ListTrie.Base.Map.Map map1 Char)
    In the first argument of `flip', namely `TS.union'
    In the first argument of TM.fromListWith', namely `(flip TS.union)'
    In the expression: TM.fromListWith' (flip TS.union)