title: Retrieving record function in generic SOP
body: <p>In <a href="https://www.andres-loeh.de/TrueSumsOfProducts/" rel="nofollow"><code>Sum of Products</code></a> approach, how would one retrieve the record function? An example code below with record datatype (<code>ghc 7.10.3</code>):</p>
  
  <pre><code>{-# 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
  </code></pre>
  
  <p>Let us see <code>DataTypeInfo</code>  at ghci prompt:</p>
  
  <pre><code>*Main&gt; datatypeInfo (Proxy :: Proxy Rec)
  ADT "Main" "Rec" (Record "Rec" (FieldInfo "frec" :* (FieldInfo "srec" :* Nil)) :* Nil)
  </code></pre>
  
  <p>We see that <code>frec</code> and <code>srec</code> are both of type <code>FieldInfo</code> which has a constructor <code>FieldInfo</code> which takes the <code>fieldName</code> as string. So, I don't see any way to get the actual functions <code>frec :: Rec -&gt; Int</code> and <code>srec :: Rec -&gt; Maybe String</code>. I also looked at <a href="https://github.com/well-typed/basic-sop/blob/master/src/Generics/SOP/Show.hs" rel="nofollow">show example</a> but it doesn't use record functions. </p>
  
  <p>Will appreciate pointers on how to get the record functions (could be HList of type <code>HList '[(Rec -&gt; Int), (Rec -&gt; Maybe String)]</code>)).</p>
  
  <p><strong>Addendum to the question</strong></p>
  
  <p>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 <code>SOP</code> approach on <code>Rec</code> constructor - we use both record field name as well as the function here:</p>
  
  <pre><code>[ ("frec" ++) . show . frec, ("srec" ++) . show . srec] 
  </code></pre>