How to create a Foldable instance of a type with multiple parameters in Haskell?

Viewed 695

I have a data type:

data Box a b = Box a b

I want to create a Foldable instance of Box and since the Foldable instance has to be given something of kind * -> *, I'll declare the instance as:

instance Foldable (Box a) where
  foldr f x (Box r s) = undefined

Now I can only do something like:

foldr f x (Box r s) = f s x

in the definition of foldr but what if instead of operating on s, I want to to something like:

foldr f x (Box r s) = f r x

The compiler doesn't let me do this so what is the proper way to go about it?

2 Answers

You must declare the instance as: instance Foldable Box where ... (Box a):: *

Related