Created
March 14, 2018 23:42
-
-
Save neongreen/1c6d9601265d43902bdf436387cd3316 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ScopedTypeVariables #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE MonoLocalBinds #-} | |
import Generics.Eot | |
import Data.Char | |
greadMaybe :: forall a. (HasEot a, EnumType (Eot a)) => String -> Maybe a | |
greadMaybe = (`lookup` table) | |
where | |
table :: [(String, a)] | |
table = zip (map (map toLower) (enumNames (Proxy @a))) | |
enumValues | |
enumNames :: HasEot a => Proxy a -> [String] | |
enumNames = map constructorName . constructors . datatype | |
enumValues :: (HasEot a, EnumType (Eot a)) => [a] | |
enumValues = map fromEot enumEot | |
-- | A class for things with several zero-field constructors. | |
class EnumType eot where | |
-- | Get all EOT-representations of constructors of the type. | |
enumEot :: [eot] | |
instance EnumType Void where | |
enumEot = [] | |
instance EnumType () where | |
enumEot = [()] | |
instance EnumType eot => EnumType (Either () eot) where | |
enumEot = Left () : map Right enumEot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment