Skip to content

Instantly share code, notes, and snippets.



Created Mar 19, 2018
What would you like to do?
Generic read for enum types (based on lowercase constructor names) using generics-sop
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module EnumRead where
import Data.Char
import qualified Data.Map
import Generics.SOP
import Generics.SOP.NS
enumRead :: forall a . (IsEnumType a, HasDatatypeInfo a) => String -> Maybe a
enumRead = flip Data.Map.lookup table
table =
(\ c (K r) -> K (map toLower (constructorName c), to (SOP r)))
(constructorInfo (datatypeInfo (Proxy @a))) -- metadata for all constructors
(apInjs'_NP (hcpure (Proxy @((~) '[])) Nil)) -- representation of all values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment