I play with core recursive data structures, and pretty early in my code, I get an error like:
module Graph where import Data.Map data Node a = Node { getLabel :: a, getInEdges :: [Edge a], getOutEdges :: [Edge a] } data Edge a = Edge { getStart :: Node a, getEnd :: Node a } data Graph a = Graph { getNodes :: [Node a], getEdges :: [Edge a] } mkGraph :: (Ord a) => [(a,a)] -> Graph a mkGraph pairs = Graph (elems nodes) edges where nodes :: Map a (Node a) edges :: [Edge a] (nodes, edges) = foldr addEdge (empty,[]) pairs addEdge :: (a,a) -> (Map a (Node a), [Edge a]) -> (Map a (Node a), [Edge a]) addEdge (startLabel, endLabel) = undefined
When I try to upload this to ghci , I get
graph.hs:13:25: Couldn't match expected type `forall a. Map a (Node a)' against inferred type `Map a (Node a)' Expected type: (forall a1. Map a1 (Node a1), forall a1. [Edge a1]) Inferred type: (Map a (Node a), [Edge a]) In the expression: foldr addEdge (empty, []) pairs In a pattern binding: (nodes, edges) = foldr addEdge (empty, []) pairs
If I remove signatures like nodes :: Map a (Node a) and edges :: [Edge a] , the error disappears.
What am I doing wrong here? I assume that a variable of type a not bound by the tick signature of mkGraph , but the definition of mkGraph should not force a in the nodes and edges signature to be the same a ?
source share