Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created March 11, 2014 13:59
Show Gist options
  • Save MgaMPKAy/9486171 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/9486171 to your computer and use it in GitHub Desktop.
Various ways to get first component of tuples.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
import Prelude hiding (fst)
class Fst a b | a -> b where
fst :: a -> b
instance Fst (a, b) a where
fst (a, _) = a
instance Fst (a, b, c) a where
fst (a, _, _) = a
instance Fst (a, b, c, d) a where
fst (a, _, _, _) = a
{-# LANGUAGE TypeFamilies #-}
import Prelude hiding (fst)
type family FstType a
type instance FstType (a1, a2) = a1
type instance FstType (a1, a2, a3) = a1
type instance FstType (a1, a2, a3, a4) = a1
class Fst a where
fst :: a -> FstType a
instance Fst (a, b) where
fst (a, b) = a
instance Fst (a, b, c) where
fst (a, b, c) = a
instance Fst (a, b, c, d) where
fst (a, b, c, d) = a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment