haskell - 有没有一种方法可以使用 Derive 和 Template Haskell 或其他方式派生 Vinyl 记录类型的二进制实例

标签 haskell template-haskell derived-instances

我一直在尝试 Vinyl package ,它使用类型级别种类来创建具有字段级别多态性和自动提供的镜头的记录结构。这两个功能对我的项目来说都非常方便,因为前者允许记录结构彼此为子类型而不会发生名称冲突,而后者则极大地简化了嵌套结构的更新。

问题在于序列化结果结构。通常我使用 Data.DeriveTH 自动派生 Binary 实例,但它似乎无法处理这些结构。代码如下

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

这似乎与 Derive Convert 模块中的这段代码有关:

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) []

现在我真的不知道如何阅读 Template Haskell,所以我在这里无法取得太大进展。我突然想到,我正在提供一个类型同义词而不是一个数据类型,这可能会破坏它,所以我尝试将它包装在一个新类型中:

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

查看 Convert.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 引入了 Derive 模板 Haskell 没有考虑到的新语言结构,从而导致了非穷举模式。

所以我的问题是,是否有办法通过添加到 Derive,或者编写我自己的 Vinyl 记录类型的派生,或类似的东西?如果 Vinyl 的好处必须以手写所有连载为代价,那将是一种耻辱。

最佳答案

我预计在编写带有所有类型欺骗的 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)

关于haskell - 有没有一种方法可以使用 Derive 和 Template Haskell 或其他方式派生 Vinyl 记录类型的二进制实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020491/

相关文章:

haskell - 如何安全地分解单一的 mkYesod block

haskell - Haskell中的多项式分解

optimization - 循环和递归展开

haskell - 构造函数中的不可见/隐藏字段

c++ - 在 C++ 中,为什么转换为派生类型的引用有效?

haskell - 如何证明类型级 bool 值的双重否定?

scala - 名称值/表达式保存在函数程序中的什么位置?

haskell - 如何在haskell中编写递归函数

haskell - 使用 Template Haskell,如何将相同的类型拼接到多个位置?