I’m using the list-tries package:
import qualified Data.ListTrie.Patricia.Map as TM
import qualified Data.ListTrie.Patricia.Set as TS
import qualified Data.Map as Map
I have a list of strings, which I want to store in a Trie-Map consisting of Trie-Sets, like this:
-- vocabs :: [String]
trie = TM.fromListWith' (flip TS.union) $ map (\v->(sort v, TS.singleton v)) vocabs
(this is for looking up a set of letters in the outer trie, then looking up a specific combination of letters in the resulting trie).
I can understand that Haskell does not know which exact type the trie has, because I have nowhere specified which type of map to use. I thought I could do this by adding the declaration
trie :: TM.TrieMap Map.Map Char (TS.TrieSet Map.Map Char)
but it seems not to be enough. I’m still getting error messages like
No instance for
(Data.ListTrie.Base.Map.Map Map.Map Char)
arising from a use ofTrieM.fromListWith'Possible fix:
add an instance declaration for
(Data.ListTrie.Base.Map.Map Map.Map Char)In the expression:
TrieM.fromListWith' TrieS.unionIn the expression: …
and similar ones for the of TS.singleton, TM.lookup, TS.member and TS.findMin.
I don’t seem to get the actual problem, and also don’t know how to add type declaration to [inline] expressions (I’ve seen it a few times, but didn’t get the syntax). Could you please help me out?
I can’t reproduce the problem.
Without the signature for
trie, I get – unsurprisingly – an ambiguous type variable error (well, two), for the reason stated in the question, together with a possible cause (that really is the cause, it’s the monomorphism restriction), and two probable fixes (disabling the monomorphism restriction makes it compile, but giving an explicit type signature is the better fix). The error message is appended below for overinterested souls (it’s from 7.6.1).With the type signature, it compiles cleanly.
In the light of that, I cannot diagnose the problem with certainty, but experience together with the error message you got provide a probable cause.
You got a very specific message that the required instance is not in scope:
On the other hand, the list-tries package explicitly provides such an instance.
The typical cause for such a situation is that there are two distinct classes or two distinct types involved that come from different versions of the same package.
In this case, you have likely built
list-triesagainst one version ofcontainers, and later installed a different version ofcontainers, and theimport qualified Data.Map as Mapimports theMap.Maptype from the newer package version, while the instance inlist-triesis for the older version.Check whether you have more than one version of
containersinstalled withCheck on which version of
containersyourlist-triesdepends withIn the
dependsfield of the output of that, there will appear something likelisting the package version and the ABI hash of the
containerspackagelist-trieswas built with.If the version doesn’t correspond to the newest version of
containersyou have installed, the above situation arises unless the version ofcontainersto use is explicitly specified when invoking GHC, either directly with a-packageflag, or indirectly via a.cabalfile.If that is the case, you can
containersto use every time you uselist-triescontainers(could break other packages)list-triesagainst the newer version ofcontainers