Last active
August 30, 2023 11:11
-
-
Save NathanHowell/6201625 to your computer and use it in GitHub Desktop.
Printing out data constructor names using GHC.Generics
This file contains 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 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