Instance not being exported?
haskell
Solution
The error is very clear: the compiler infers that the instance must be `ToRecord (Label Symbol "value", Label Symbol "name") (Int, [Char])`, yet no such instance exists. The instance that you wrote implicitly has another parameter: the kind of the arguments. In this case, what you have is `ToRecord (Label (t1 :: *), Label (t2 :: *)) (v1, v2)` - unless you use `PolyKinds`, all polymorphic kinds become `*`. For example:
l0 = Label :: Label Int
l1 = Label :: Label Bool
test = toRecord (l0, l1) (5 :: Int, "age")
will compile just fine. If you turn on `PolyKinds`, you can write
instance ToRecord (Label (t1 :: k), Label (t2 :: k)) (v1, v2) where
Then your example will work:
>toRecord (value, name) (5 :: Int, "age")
Record{value=5,name="age"}
I can't replicate the issue with some instances not showing up.
Clarifying remarks:
Maybe I shouldn't say it is very clear. Kinds, which are the types of types, like types, are arguments to constructors. `Label` actually has two parameters: the type parameter, and the kind of the type parameter. The kind parameter is written first - therefore, you have `value :: Label Symbol "value"` (Symbol is the kind of string type literals). Of course, you can't write it like that, but that is the way GHC prints it.
Kinds can be inferred like types. By default, the inferred kind will be `*`. In your code, the kind of `t1` and `t2` in `ToRecord (Label t1, Label t2) (v1, v2)` is `*`. Therefore, `Label ("value" :: Symbol)` and `Label (t1 :: *)` cannot be unified.
`t1 :: k` and `t1 :: Symbol` both work just like `(f :: Integral a => a -> a) (1 :: Int)` and `(f :: Int -> Int) (1 :: Int)` both work. You need the `KindSignatures` extension to write `t1 :: Symbol`, but `KindSignatures` is implied by `DataKinds` and `PolyKinds`.
Problem
I'm trying to convert a tuple to an `HList.Record` and there is something really strange. I created a `ToRecord` class which zip a tuple of things and a tuple of labels to a record. Everything compiles but the instances seems to vanish. When I try to use them, GHC complains the instance I'm asking for doesn't 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") ``` Moreover, If remove the `test` line and load the file into GHC. The instances are missing (using `:i ToRecord`), only the dummy one `ToRecod Char Integer` is showing up. ``` 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'm using GHC 7.6.3)?