Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell can't match type, claims rigid variable
    primarykey
    data
    text
    <p>I am new to Haskell, and I am playing around with creating a typeclass for graphs and the nodes in them. Since I want both directed and undirected graphs, I have</p> <pre><code>data Node = Node { label :: Char , index :: Int } deriving (Ord, Eq) type Graph edgeType = ([Node], [edgeType]) data Edge = DirectedEdge {h :: Node, t :: Node} | UndirectedEdge {a :: Node, b :: Node} instance Show Node where show n = ['(', label n, ')'] instance Show Edge where show (DirectedEdge h t) = show h ++ "-&gt;" ++ show t show (UndirectedEdge a b) = show a ++ "-" ++ show b </code></pre> <p>So I am distinguishing between directed and undirected edges. A graph must only have edges of either type. I also have the following:</p> <pre><code>nodes :: [Node] nodes = zipWith Node ['a'..] [0..] emptyGraph :: [Node] -&gt; Graph edgeType emptyGraph ns = (ns, []) </code></pre> <p>So far so good, however I am writing a function <code>connect</code>, with connects a node to an existing graph. Ideally, I only want it to apply to undirected graphs, but that doesn't seem to be an option. Instead, I have something like this:</p> <pre><code>connect :: Graph edgeType -&gt; Node -&gt; Graph edgeType connect (ns, es) n = (n:ns, e:es) where e = UndirectedEdge n (head ns) </code></pre> <p>But this gives the following error:</p> <pre><code>Couldn't match type `edgeType' with `Edge' `edgeType' is a rigid type variable bound by the type signature for connect :: Graph edgeType -&gt; Node -&gt; Graph edgeType </code></pre> <p>What is the best way to accomplish what I am trying to achieve?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload