0

Why i get this error in my code: code:

module Task5 where

import Prelude

data Stream a = a :& Stream a

infixl 4 :&

add :: Num a => a -> a -> a
add a b = a + b

instance Num (Stream a ) where
    (+) (ia:&a) (ib :& b) = (ia + ib) :& ((+) a b)

error:

Task5.hs:14:33:
    No instance for (Num a) arising from a use of `+'
    Possible fix:
      add (Num a) to the context of the instance declaration
    In the first argument of `(:&)', namely `(ia + ib)'
    In the expression: (ia + ib) :& ((+) a b)
    In an equation for `+':
        + (ia :& a) (ib :& b) = (ia + ib) :& ((+) a b)

I don't understand how to correct this , i tried it for all week , but not find any solution. So , can you say me what i should correct?

1 Answer 1

4

As the error suggests, you need to add Num a to the "context" of your instance declaration:

instance Num a => Num (Stream a) where

Otherwise the ia + ib operation doesn't have any (+) operation available, as that is acting on the individual stream members.

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

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.