Skip to content

Instantly share code, notes, and snippets.

@shhyou
Created November 17, 2013 04:22
Show Gist options
  • Save shhyou/7509167 to your computer and use it in GitHub Desktop.
Save shhyou/7509167 to your computer and use it in GitHub Desktop.
Typeclass hacks to automatically injects elements into a list
{-# LANGUAGE FlexibleInstances #-}
data AllType = TypeA Char
| TypeB String
| TypeC Bool
deriving (Show)
newtype S = S { unS :: String }
data Ii = Ii { runIi :: [AllType] }
list1 = runIi $ iI '5' '1' '4' (S "Hello World") True '8'
list2 = runIi $ iI (S "Just") False '8' (S "Some") (S "Sugar")
class ASTAll a where
inj :: a -> AllType
instance ASTAll Char where
inj = TypeA
instance ASTAll S where
inj = TypeB . unS
instance ASTAll Bool where
inj = TypeC
class Heterogeneous a where
iI' :: [AllType] -> a
iI :: Heterogeneous a => a
iI = iI' []
instance Heterogeneous Ii where
iI' xs = Ii (reverse xs)
instance (ASTAll a, Heterogeneous as) => Heterogeneous (a -> as) where
iI' xs a = iI' (inj a : xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment