Hi,<br><br>I declared a multi param type class as below:<br><b>class MyString m c where<br> zIndex :: m -> Int -> c<br> zLength :: m -> Int<br> zPack :: [c] -> m<br> zEquals :: c ->c -> Bool<br>
zWrap :: [c] -> (m,AnExistingDataType)</b><br><br>In the end I did not needed it for my program, but for those few minutes I played with the idea, I came across a zillion questions. <br>Can some body help me here please? <br>
When I defined a function like below,<br>\begin{code}<br>compareStr::(MyString m c) => (m,Int) ->(m,Int)->Int->Int<br>compareStr (s1,pos1) (s2,pos2) soFar<br> | (pos1 < zLength s1) && (pos2 < zLength s2) = let<br>
c1 = zIndex s1 pos1<br> c2 = zIndex s2 pos2<br> in<br>
if(zEquals c1 c2) then<br> compareStr (s1,(pos1 + 1)) (s2, (pos2 + 1)) (soFar + 1)<br> else soFar<br>
<br> | otherwise = soFar<br>\end{code}<br><br>when I loaded it on ghci ( invoked with -XMultiParamTypeClasses ), I got an essay in greek (or is it latin?), which started something like below:<br>
Could not deduce (MyString m c) from the context (MyString m c4)<br> arising from a use of `zLength' at GenericZAlgo.lhs:42:21-31<br> Possible fix:<br> add (MyString m c) to the context of<br> the type signature for `compareStr'<br>
In the second argument of `(<)', namely `zLength s1'<br> In the first argument of `(&&)', namely `(pos1 < zLength s1)'<br> In a pattern guard for<br> the definition of `compareStr':<br>
(pos1 < zLength s1) && (pos2 < zLength s2)<br><br>And if I let the type inference run the show, the type it shows is :<br>compareStr :: (MyString t c2,<br> MyString m c2,<br> Num a,<br>
MyString t1 c2,<br> MyString t c,<br> MyString t1 c1) =><br> (t, Int) -> (t1, Int) -> a -> a<br><br>And puzzlingly, if I try the same thing myself and reload it on the ghci,I still get similar error. I can go on about other 'puzzles'.<br>
<br>Can some one please tell me whats going on here?<br><br>Apologies, if it is something that's been documented left to right, top to bottom, but I could not get anywhere readily. Any pointers to the right documentation will be much appreciated.<br>
On the surface it looks like something that n00bs like yours truly better stay away. But I have a feeling that this one may throw some more light on type inference.<br><br>Many thanks<br><br>