[Haskell-cafe] Generic "to tuple" function

Michael Orlitzky michael at orlitzky.com
Sat Dec 27 15:45:13 UTC 2014


On 12/26/2014 05:39 PM, Roman Cheplyaka wrote:
> On 26/12/14 20:18, Michael Orlitzky wrote:
>>
>> But I'm wondering, is there any generic function that
>> will take a simple data type and cram it into a tuple?
> 
> Should be easy to do with the generics-sop package.
> 

Thanks, I was able to get this working in ghci:

  import qualified GHC.Generics as GHC
  import Generics.SOP

  data Foo = Bar Int Int Int Int
    deriving (Show, GHC.Generic)

  instance Generic Foo

  to_tuple :: Foo -> (Int, Int, Int, Int)
  to_tuple (Bar w x y z) = (w,x,y,z)

  toTupleG = to . from

An example:

  > let b = Bar 1 2 3 4
  > to_tuple b
  (1,2,3,4)
  > toTupleG b :: (Int,Int,Int,Int)
  (1,2,3,4)



More information about the Haskell-Cafe mailing list