Having some fun (and pain) with generics
Decided to try and wrap my head around generic programming and ended up sinking too much time trying to understand (or not?) some of the basic ideas. Just for fun, I tried to think how it would be possible to go from a homogenous list back to possibly a data structure.
module Foo where
import Data.Maybe
import Generics.SOP
import Generics.SOP.TH
data Foo = F String Int
deriving (Eq, Show)
deriveGeneric ''Foo
homoLstToFoo :: [String] -> Maybe Foo
homoLstToFoo xs
= case iterP homoP of
Nothing -> Nothing
Just ps -> Just $ (to . SOP . Z) ps
where
homoP :: xs ~ [String, String] => Maybe (NP (K String) xs)
homoP = fromList xs
iterP :: Maybe (NP (K String) xs) -> Maybe (NP I '[String, Int])
iterP mLst = case mLst of
Nothing -> Nothing
Just (x :* y :* _) -> case readMay (unK y) :: Maybe Int of
Just z -> Just $ I (unK x :: String) :* I z :* Nil
Nothing -> Nothing