[Haskell-cafe] generics-sop : deep flattening of nested algebraic values ?

Marco Zocca zocca.marco at gmail.com
Sat Jan 26 12:43:31 UTC 2019


Hi all,

 I'm a bit at a loss regarding this: say we have a value from a nested
algebraic type (definitions at the bottom)

testT2 :: T2
testT2 = T2 B (Left 42)

a generic encoding computed with `from` only goes one level deep,
leaving the sub-terms as they are :

λ> from testT2
SOP (Z (I B :* (I (Left 42) :* Nil)))

However, is it possible to recursively encode and collect all the
sub-terms, until primitive types or enumerations are encountered? The
encoded sub-terms from the example above are as follows:

λ> from B
SOP (S (Z Nil))
λ> from (Left 32 :: Either Int Char)
SOP (Z (I 32 :* Nil))

where

{-# language DeriveGeneric #-}
import qualified GHC.Generics as G
import Generics.SOP

data T1 = A | B | C deriving (G.Generic)
instance Generic T1

data T2 = T2 { t21 :: T1 , t22 :: Either Int Char } deriving (G.Generic)
instance Generic T2


More information about the Haskell-Cafe mailing list