3

I recently learned that I can do the following in Haskell:

{-# LANGUAGE DeriveDataTypeable #-}

import Data.Data

data MyRecord = MyRecord
  { field1 :: Int
  , field2 :: String
  , field3 :: String
  } deriving (Show,Eq,Data,Typeable)

main = print $ constrFields (toConstr (MyRecord 5 "Hello" "World"))

This will give me the following:

["field1","field2","field3"]

How can I do the same thing for the values in a record, like this:

["5","Hello","World"]

I'm asking because I'm using Aeson to take simple JSON like this:

{
  "field1":5,
  "field2":"Hello",
  "field3":"World"
}

And generate Haskell code like this:

field1 :: Int
field1 = 5

field2 :: String
field2 = "Hello"

field3 :: String
field3 = "World"

How can I generically unwrap all the values in a given record in the same way I can unwrap the field names of my records?

3
  • Is there a reason why you don't want to unwrap the JSON directly into MyRecord? Once you do, you can get at the values by calling field1, etc. on the record value.
    – Ecognium
    Apr 2, 2016 at 5:36
  • I'm already deriving ToJSON and FromJSON for MyRecord, but I'd like to be able to unwrap all the values in the record without calling field1, field2, etc explicitly in order to access the values. I would like to just generically get all values in the form of a list of strings, just like I was able to do with the field names.
    – carpemb
    Apr 2, 2016 at 5:41
  • 3
    What would you expect as an output of such a function, your example already shows that a list would not work for your fields have different types. Same goes for Map String, also your field types can possibly have a nonshowable type like functions so a string representation is not generically possible Apr 2, 2016 at 8:19

1 Answer 1

9

Your first question can be answered. If you're happy to convert all the values of a datatype to strings, then you can indeed produce such a list with a generic programming library such as e.g. generics-sop:

{-# LANGUAGE DeriveGeneric, FlexibleContexts #-}

import qualified GHC.Generics as G
import Generics.SOP

data MyRecord = MyRecord
  { field1 :: Int
  , field2 :: String
  , field3 :: String
  } deriving (Show, Eq, G.Generic)

instance Generic MyRecord

stringValues ::
     (Generic a, All2 Show (Code a))
  => a -> [String]
stringValues a =
  hcollapse (hcmap (Proxy :: Proxy Show) (\ (I x) -> K (show x)) (from a))

test :: [String]
test = stringValues (MyRecord 5 "Hello" "world")

Now in GHCi:

GHCi> test
["5","\"Hello\"","\"world\""]

However, if your goal is to maintain the original types, then this is more difficult, as the result type will have to be a heterogeneous list (which in fact generics-sop uses internally, before it is converted back into a normal list using hcollapse).

It's not entirely clear to me what you really want to achieve. It's quite possible that there's a much more straight-forward solution.

2
  • 1
    Cool example! Can the machinery in generics-sop be used to define extensible records a la vinyl?
    – danidiaz
    Apr 2, 2016 at 9:53
  • 4
    @danidiaz In principle, yes. At least since vinyl-0.5, their Rec type is structurally isomorphic to NP in generics-sop. So conversion should be easy, and in theory, both libraries could be merged. But so far, doing extensible records has not been the primary focus of generics-sop.
    – kosmikus
    Apr 2, 2016 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.