Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way of deriving Binary instances for Vinyl record types using Derive and Template Haskell or otherwise
    primarykey
    data
    text
    <p>I have been trying out the <a href="http://hackage.haskell.org/package/vinyl-0.1.0.0">Vinyl package</a>, which uses type level kinds to create record structures with field level polymorphism and automatically provided lenses. Both of these features would be very handy to my project, as the former allows for record structures that are sub-types of each other without name clashes, and the latter simplifies updates on nested structures dramatically.</p> <p>The problem comes with serialising the resultant structures. Normally I use Data.DeriveTH to automagically derive Binary instances, but it doesn't seem to be able to cope with these structures. The following code</p> <pre><code>{-# LANGUAGE DataKinds, TypeOperators #-} {-# LANGUAGE FlexibleContexts, NoMonomorphismRestriction #-} {-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} import Data.Vinyl import Data.Binary import Data.DeriveTH eID = Field :: "eID" ::: Int location = Field :: "location" ::: (Double, Double) type Entity = Rec [ "eID" ::: Int , "location" ::: (Double, Double) ] $(derive makeBinary ''Entity) </code></pre> <p>results in this error in GHCI</p> <pre><code>Exception when trying to run compile-time code: Could not convert Dec to Decl TySynD Main.Entity [] (AppT (ConT Data.Vinyl.Rec.Rec) (AppT (AppT PromotedConsT (AppT (AppT (ConT Data.Vinyl.Field.:::) (LitT (StrTyLit "eID"))) (ConT GHC.Types.Int))) (AppT (AppT PromotedConsT (AppT (AppT (ConT Data.Vinyl.Field.:::) (LitT (StrTyLit "location"))) (AppT (AppT (TupleT 2) (ConT GHC.Types.Double)) (ConT GHC.Types.Double)))) PromotedNilT))) Language/Haskell/Convert.hs:(37,14)-(40,8): Non-exhaustive patterns in case Code: derive makeBinary ''Entity Failed, modules loaded: none. </code></pre> <p>This seems to be related to this piece of code in the Derive Convert module:</p> <pre><code>instance Convert TH.Dec HS.Decl where conv x = case x of DataD cxt n vs con ds -&gt; f DataType cxt n vs con ds NewtypeD cxt n vs con ds -&gt; f NewType cxt n vs [con] ds where f t cxt n vs con ds = DataDecl sl t (c cxt) (c n) (c vs) (c con) [] </code></pre> <p>Now I don't really know how to read Template Haskell so I can't make much progress here. It occurred to me that I am feeding derive a type synonym rather than a data type and that could be breaking it, so I tried wrapping it in a newtype:</p> <pre><code>newtype Entity2 = Entity2 {entity :: Entity} $(derive makeBinary ''Entity2) </code></pre> <p>which leads to this even more obtuse error:</p> <pre><code>Exception when trying to run compile-time code: Could not convert Type to Type AppT (AppT PromotedConsT (AppT (AppT (ConT Data.Vinyl.Field.:::) (LitT (StrTyLit "eID"))) (ConT GHC.Types.Int))) (AppT (AppT PromotedConsT (AppT (AppT (ConT Data.Vinyl.Field.:::) (LitT (StrTyLit "location"))) (AppT (AppT (TupleT 2) (ConT GHC.Types.Double)) (ConT GHC.Types.Double)))) PromotedNilT) Could not convert Type to Type AppT PromotedConsT (AppT (AppT (ConT Data.Vinyl.Field.:::) (LitT (StrTyLit "eID"))) (ConT GHC.Types.Int)) Could not convert Type to Type PromotedConsT Language/Haskell/Convert.hs:(71,5)-(80,26): Non-exhaustive patterns in function conv </code></pre> <p>Looking in Convert.hs we have</p> <pre><code>instance Convert TH.Type HS.Type where conv (ForallT xs cxt t) = TyForall (Just $ c xs) (c cxt) (c t) conv (VarT x) = TyVar $ c x conv (ConT x) | ',' `elem` show x = TyTuple Boxed [] | otherwise = TyCon $ c x conv (AppT (AppT ArrowT x) y) = TyFun (c x) (c y) conv (AppT ListT x) = TyList $ c x conv (TupleT _) = TyTuple Boxed [] conv (AppT x y) = case c x of TyTuple b xs -&gt; TyTuple b $ xs ++ [c y] x -&gt; TyApp x $ c y </code></pre> <p>Now I'm guessing that what is going wrong is that GHC 7.6 has introduced new language constructs that the Derive template Haskell is not taking into account, leading to the non-exhaustive patterns. </p> <p>So my question is, is there some way forward by either adding to Derive, or writing my own derivation from Vinyl record types, or something similar? It would be a shame if the benefits of Vinyl had to traded off against hand writing all the serialisation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload