2

My code is this:

type Code = [Inst]

data Inst = PUSH Int
          | PUSHV Name
          | POP Name
          | DO Op
          | JUMP Label
          | JUMPZ Label
          | LABEL Label
          deriving Show

doJump :: Inst -> Code -> Code
doJump l c = case (elemIndex l c) of
                 Just n -> drop (n+1) c
                 Nothing -> c

And GHC returns me this error, which has got me completely stumped... What I'm trying to do is return the list from the point after a specific element occurs.

No instance for (Eq Inst) arising from a use of `elemIndex'
Possible fix: add an instance declaration for (Eq Inst)
In the expression: (elemIndex l c)
In the expression:
  case (elemIndex l c) of {
    Just n -> drop (n + 1) c
    Nothing -> c }
In an equation for `doJump':
    doJump l c
      = case (elemIndex l c) of {
          Just n -> drop (n + 1) c
          Nothing -> c }

Any ideas?

2
  • Don't use tabs in Haskell. They don't work with significant indentation. Commented Apr 1, 2014 at 13:11
  • 2
    @rightfold There are arguments against using tabs in Haskell, but that isn't one of them. Commented Apr 1, 2014 at 13:20

1 Answer 1

5

elemIndex requires the elements of the list to be equality-comparable, so you need to add an instance for Eq Inst.

Use deriving Eq to have it generated automatically, for example:

data Inst = PUSH Int
          | PUSHV Name
          | POP Name
          | DO Op
          | JUMP Label
          | JUMPZ Label
          | LABEL Label
          deriving (Show, Eq)
Sign up to request clarification or add additional context in comments.

3 Comments

Fantastic, that worked thanks. would it be possible to add an Instance of Eq Inst for just that function. Or must I do it for the whole data type?
@stell1315 Instances are always program-global in Haskell.
@stell1315 If you do not want to add deriving Eq to Inst, you could use findIndex instead of elemIndex.

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.