Skip to content

Instantly share code, notes, and snippets.

@neongreen
Created March 14, 2018 23:42
Show Gist options
  • Save neongreen/1c6d9601265d43902bdf436387cd3316 to your computer and use it in GitHub Desktop.
Save neongreen/1c6d9601265d43902bdf436387cd3316 to your computer and use it in GitHub Desktop.
{-# 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