<div dir="ltr">I am pretty sure I have a good handle on type classes, but this one is perplexing me in Haskell.  I created custom Tuples called Tuple2 and Tuple3 (I know Haskell already has Tuples, just thought I would create my own for this exercise). Then I wanted to create a type class that would have a method called first that would get the first element of the tuple regardless of what kind of Tuple it is. Tuple2, Tuple3, etc.<div><br></div><div>Here is what I have:</div><div><div><br></div><div><font face="monospace, monospace">data Tuple3 a b c = Tuple3 a b c deriving (Show)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">data Tuple2 a b = Tuple2 a b deriving (Show)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class Indexable idx where</font></div><div><font face="monospace, monospace">   first :: idx c -> a</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">instance Indexable (Tuple2 a) where</font></div><div><font face="monospace, monospace">   first (Tuple2 a b) = a</font></div><div><br></div></div><div>In my main, I try to get call <font face="monospace, monospace">putStrLn $ show $ first $ Tuple2 1 "One"</font></div><div><br></div><div>I was greeted with the following trace:</div><div><div><font face="monospace, monospace">    Couldn't match expected type ‘a1’ with actual type ‘a’</font></div><div><font face="monospace, monospace">      ‘a’ is a rigid type variable bound by</font></div><div><font face="monospace, monospace">          the instance declaration at TypeClasses.hs:35:10</font></div><div><font face="monospace, monospace">      ‘a1’ is a rigid type variable bound by</font></div><div><font face="monospace, monospace">           the type signature for first :: Tuple2 a c -> a1</font></div><div><font face="monospace, monospace">           at TypeClasses.hs:36:4</font></div><div><font face="monospace, monospace">    Relevant bindings include</font></div><div><font face="monospace, monospace">      a :: a (bound at TypeClasses.hs:36:18)</font></div><div><font face="monospace, monospace">      first :: Tuple2 a c -> a1 (bound at TypeClasses.hs:36:4)</font></div><div><font face="monospace, monospace">    In the expression: a</font></div><div><font face="monospace, monospace">    In an equation for ‘first’: first (Tuple2 a b) = a</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">Help is appreciated.</font></div><div><br></div></div>