Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
Last active August 30, 2023 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanHowell/6201625 to your computer and use it in GitHub Desktop.
Save NathanHowell/6201625 to your computer and use it in GitHub Desktop.
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