有没有办法使用派生和模板 Haskell 或其他方式为黑胶唱片类型派生二进制实例



我一直在尝试 Vinyl 包,它使用类型级类型来创建具有场级多态性的记录结构,并自动提供镜头。这两个功能对我的项目都非常方便,因为前者允许记录结构彼此的子类型而没有名称冲突,而后者大大简化了嵌套结构的更新。

问题在于序列化生成的结构。通常我使用 Data.DeriveTH 来自动派生二进制实例,但它似乎无法处理这些结构。以下代码

{-# 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)

导致 GHCI 中的此错误

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.

这似乎与派生转换模块中的这段代码有关:

instance Convert TH.Dec HS.Decl where
conv x = case x of
DataD cxt n vs con ds -> f DataType cxt n vs con ds
NewtypeD cxt n vs con ds -> 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) []

现在我真的不知道如何阅读模板Haskell,所以我不能在这里取得太大进展。我突然想到我正在输入派生一个类型同义词而不是数据类型,这可能会破坏它,所以我尝试将其包装在 newtype 中:

newtype Entity2 = Entity2 {entity :: Entity}
$(derive makeBinary ''Entity2)

这导致了这个更迟钝的错误:

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

在 转换.hs 中寻找我们有

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 -> TyTuple b $ xs ++ [c y]
x -> TyApp x $ c y

现在我猜出了问题,GHC 7.6引入了新的语言结构,而派生模板Haskell没有考虑到这些结构,导致了非详尽的模式。

所以我的问题是,是否有办法通过添加到 Derive,或者从黑胶唱片类型编写我自己的派生,或者类似的东西?如果黑胶唱片的好处不得不与手写所有的连载相权衡,那将是一种耻辱。

我预计在编写带有所有类型欺骗的Binary实例时会遇到一些问题,但这再简单不过了:

instance Binary (Rec '[]) where
put RNil = return ()
get = return RNil
instance (Binary t, Binary (Rec fs)) => Binary (Rec ((sy ::: t) ': fs)) where
put ((_,x) :& xs) = put x >> put xs
get = do
x <- get
xs <- get
return ((Field, x) :& xs)

最新更新