Skip to content

Instantly share code, notes, and snippets.

@NathanHowell

NathanHowell/cn.hs

Last active Dec 20, 2015
Embed
What would you like to do?
Printing out data constructor names using GHC.Generics
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeOperators #-}
import Data.Proxy
import GHC.Generics
data Foo = Bar Int | Baz Float deriving Generic
class CN (f :: * -> *) where
constructorNames :: proxy f -> [String]
instance CN f => CN (D1 c f) where
constructorNames _ = constructorNames (Proxy :: Proxy f)
instance (CN x, CN y) => CN (x :+: y) where
constructorNames _ = constructorNames (Proxy :: Proxy x) ++ constructorNames (Proxy :: Proxy y)
instance Constructor c => CN (C1 c f) where
constructorNames _ = [conName (undefined :: t c f a)]
main :: IO ()
main = do
print $ constructorNames (Proxy :: Proxy (Rep Foo))
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.