stell1315
stell1315

Reputation: 153

Instance Declaration Haskell

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?

Upvotes: 2

Views: 116

Answers (1)

user1804599
user1804599

Reputation:

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)

Upvotes: 5

Related Questions