In Sum of Products
approach, how would one retrieve the record function? An example code below with record datatype (ghc 7.10.3
):
{-# LANGUAGE DeriveGeneric #-}
import qualified GHC.Generics as GHC
import Generics.SOP
data Rec = Rec { frec :: Int, srec :: Maybe String}
deriving (Show, GHC.Generic)
instance Generic Rec -- empty
instance HasDatatypeInfo Rec
Let us see DataTypeInfo
at ghci prompt:
*Main> datatypeInfo (Proxy :: Proxy Rec)
ADT "Main" "Rec" (Record "Rec" (FieldInfo "frec" :* (FieldInfo "srec" :* Nil)) :* Nil)
We see that frec
and srec
are both of type FieldInfo
which has a constructor FieldInfo
which takes the fieldName
as string. So, I don't see any way to get the actual functions frec :: Rec -> Int
and srec :: Rec -> Maybe String
. I also looked at show example but it doesn't use record functions.
Will appreciate pointers on how to get the record functions (could be HList of type HList '[(Rec -> Int), (Rec -> Maybe String)]
)).
Addendum to the question
I am tied up in the type knots about how to get the functions out of the projections using the approach user2407038 has laid out. So, I will like to add to the question further: how do we build a function like below using SOP
approach on Rec
constructor - we use both record field name as well as the function here:
[ ("frec" ++) . show . frec, ("srec" ++) . show . srec]
projections
here isNP '[Code Rec -> Int, Code Rec -> Maybe String]
. Also,Rep a = SOP I (Code a))
. Pairing that withto
should give usa
which isRec -> Int
, a function, no? That is why I am confused about need for:*:
. Will appreciate further insights here. – Lotz