I am trying to convert a tuple into HList.Record, and there is something really strange. I created a class ToRecordthat fastens a tuple of things and a tuple of labels for writing. Everything compiles, but the instances seem to disappear. When I try to use them, the GHC complains that the instance I am asking for does not exist.
{-# LANGUAGE TypeFamilies, DataKinds, TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
-- standard
import GHC.TypeLits (Symbol)
-- third-party
import Data.HList.FakePrelude
import Data.HList.Record
import Data.HList.Labelable
-- local
-- * Tuples <-> Records Conversion
class ToRecord a b where
type Result a b :: *
toRecord :: a -> b -> Result a b -- Result a b
instance ToRecord (Label t1, Label t2) (v1, v2) where
type Result (Label t1, Label t2) (v1, v2) = Record '[Tagged (t1) v1, Tagged (t2) v2]
toRecord (t1, t2) (v1, v2) = t1 .=. v1 .*. t2 .=. v2 .*. emptyRecord
instance ToRecord (Label t1, Label t2, Label t3) (v1, v2, v3) where
type Result (Label t1, Label t2, Label t3) (v1, v2, v3) =
Record '[Tagged (t1) v1, Tagged (t2) v2, Tagged t3 v3]
toRecord (t1, t2, t3) (v1, v2, v3) = t1 .=. v1 .*. t2 .=. v2 .*. t3 .=. v3 .*. emptyRecord
-- dummy instance to check GHC behavior
instance ToRecord Char Integer where
type Result Char Integer = (Char, Integer)
toRecord c i = (c, i)
value = Label :: Label "value"
name = Label :: Label "name"
test = toRecord (value, name) (5 :: Int, "age")
Error message:
Utils.hs:34:8:
No instance for (ToRecord
(Label Symbol "value", Label Symbol "name") (Int, [Char]))
arising from a use of `toRecord'
Possible fix:
add an instance declaration for
(ToRecord
(Label Symbol "value", Label Symbol "name") (Int, [Char]))
In the expression: toRecord (value, name) (5 :: Int, "age")
In an equation for `test':
test = toRecord (value, name) (5 :: Int, "age")
Alternatively, if you delete the line testand upload the file to GHC. There are no instances (using :i ToRecord), only dummy appears ToRecod Char Integer.
Utils Database.Harehouse.SQLFragment>:i ToRecord
class ToRecord a b where
type family Result a b :: *
toRecord :: a -> b -> Result a b
-- Defined at Database/Harehouse/Utils.hs:15:7
instance ToRecord Char Integer
-- Defined at Database/Harehouse/Utils.hs:27:10
Any idea (I am using GHC 7.6.3)?