3

I'm trying different data structures for implementing Prim's algorithm. So I made a class to abstract what I want to do:

class VertexContainer a where
  contains :: a -> Vertex -> Bool
  insert :: a -> WeightedEdge -> a
  numVertices :: a -> Int

Now I want to use a heap (from Data.Heap) as my vertex container. But I can't for the life of me figure out the syntax. As you can see from the insert declaration, the container can only hold WeightedEdges, which are a data type. So I tried:

instance VertexContainer (Heap MinPolicy WeightedEdge) where
  contains _ _ = True

It tells me it's an illegal type synonym. I've tried various other permutations, and none of them seem to work. Can anyone help me?

2 Answers 2

6

If you read the entire error message you'll find that it tells you how to be able to use a type synonym in an instance declaration, namely by using the language extension TypeSynonymInstances. E.g., you can pass -XTypeSynonymInstances on the command line.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, should've explained the error more. I actually did pass this in, and the full error message that I got is "Illegal type synonym family application in instance:"
Can you declare the instance on HeapT rather than the synonym Heap? Scanning the source the associated type Prio is possibly doing something "unusual".
1

I got it working by wrapping this into a newtype. Considered ugly. I guess you have to wait for one of the Haskell gurus to answer this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.