Haskell fromList使用难题:函数类型不符预期(附代码链接)
fromList in Haskell Hey there! Let's work through this type mismatch issue you're hitting with Haskell's fromList.
The core problem here is that somewhere in your code, you're wrapping the Map Role [Permission] part in an extra list ([]), which is why you're getting ([Permission], [Map Role [Permission]]) instead of the desired ([Permission], Map Role [Permission]) for each entry in your Map User ....
How to Fix It
Let's break this down with concrete examples to clarify:
Check your entry structure
You likely have code that looks like this (the problematic version):-- This creates the unwanted [Map Role [Permission]] type let userEntry = (myUser, (myPermissions, [myRoleMap])) let myMap = fromList [userEntry]Instead, remove the extra brackets around
myRoleMapto get the correct type:-- This gives you the target (Permissions, Map Role [Permission]) type let userEntry = (myUser, (myPermissions, myRoleMap)) let myMap = fromList [userEntry]If coming from an aggregation step
If you're generating these entries from a fold, group, or other aggregation, you might be accidentally collecting a singleMap Role [Permission]into a list. For example, ifgroupByleaves you with a list containing one map, extract it safely (preferably with a safe function instead ofheadto avoid runtime errors):-- Safe helper to extract a single map from a list extractSingleMap :: [Map k v] -> Maybe (Map k v) extractSingleMap [m] = Just m extractSingleMap _ = Nothing -- Use it when building your entry case extractSingleMap roleMapsList of Just roleMap -> (myUser, (myPermissions, roleMap)) Nothing -> -- handle empty/multiple map cases appropriately
Verify the Input Type
Double-check the type of the list you're passing to fromList by running :t myList in GHCi. It should match [(User, ([Permission], Map Role [Permission]))]—this ensures fromList will produce the exact Map User ([Permission], Map Role [Permission]) you need.
内容的提问来源于stack exchange,提问作者Alexia Desouza

