"No instance for (Ord k)" when implementing Functor over Data.Map.Map

I am trying to implement functor fmap over Data.Map.Map, but I am getting an error. I am sure that I do not need to convert the map to and from the list for this to work, but this is the best I have come up with so far.

class Functor' f where fmap' :: (a -> b) -> fa -> fb instance Functor' (Map.Map k) where fmap' fm | Map.null m = Map.empty | otherwise = let x:xs = Map.toList m mtail = Map.fromList xs a = fst x b = snd x in Map.insert a (fb) (fmap f mtail) 

Error:

 No instance for (Ord k) arising from a use of `Map.fromList' In the expression: Map.fromList xs In an equation for `mtail': mtail = Map.fromList xs In the expression: let x : xs = Map.toList m mtail = Map.fromList xs a = fst x .... in Map.insert a (fb) (fmap f mtail) 

Any ideas?

+6
source share
1 answer

The error is due to the fact that the predicate Ord does not assign a variable of type k. Just do the following:

 instance Ord k => Functor' (Map.Map k) where 
+2
source

Source: https://habr.com/ru/post/919294/


All Articles