Created
March 11, 2014 13:59
-
-
Save MgaMPKAy/9486171 to your computer and use it in GitHub Desktop.
Various ways to get first component of tuples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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