Haskell Typeclass for Tuples

You can use type families like so (A different take on what Edward wrote):

{-# LANGUAGE TypeFamilies #-}

class Firstable a where
  type First a :: *
  fst :: a -> First a

class Secondable a where
  type Second a :: *
  snd :: a -> Second a

instance Firstable (a,b) where
  type First (a, b) = a
  fst (x, _) = x

instance Secondable (a,b) where
  type Second (a, b) = b
  snd (_, y) = y

A version with worse parametricity guarantees can be had with MPTCS and Fundeps or with TypeFamilies.

type family Fst p
type instance Fst (a,b) = a
type instance Fst (a,b,c) = a

...

class First p where
   fst :: p -> Fst p

instance Fst (a,b) where
   fst (a,_) = a

instance Fst (a,b,c) where
   fst (a,_,_) = a

...

but ultimately, you'll need to use some extensions.


class Firstable f where
    fst :: f a b -> a

class Secondable f where
    snd :: f a b -> b