I want to recursively collect arguments of a function to a tuple with nesting (the general idea is taken from here).
I have a class with a function with polymorphic parameter (a, b)
. I want to make an instance for this class so that its parameters should be the same a
and b
:
class MyClass r where
my_fun :: (a, b) -> r
instance MyClass (a, b) where
my_fun x = x -- problematic place
instance MyClass r => MyClass (s -> r) where
my_fun (a,b) s = my_fun (ff (a,b) s)
ff :: (a,b) -> s -> ((a,b), s)
ff x s = (x, s)
But I get a compilation error:
* Couldn't match type `a1' with `a'
Expected: (a, b)
Actual: (a1, b1)
`a1' is a rigid type variable bound by
the type signature for:
my_fun :: forall a1 b1. (a1, b1) -> (a, b)
at class.hs:5:5-10
`a' is a rigid type variable bound by
the instance declaration
at class.hs:4:10-23
How can I instruct the compiler that a
and b
in the instance are the same as in function declaration?
myFun
?class MyClass r where my_fun :: (a, b) -> r instance MyClass (a, b) where my_fun x = x -- problematic place instance MyClass r => MyClass (s -> r) where my_fun (a,b) s = my_fun (ff (a,b) s) ff :: (a,b) -> s -> ((a,b), s) ff x s = (x, s)