opaleye basic tutorial提供了一个如何在记录类型和查询中使用用户定义类型的示例:
data Birthday' a b = Birthday { bdName :: a, bdDay :: b }
type Birthday = Birthday' String Day
type BirthdayColumn = Birthday' (Column PGText) (Column PGDate)
birthdayTable :: Table BirthdayColumn BirthdayColumn
birthdayTable = table "birthdayTable"
(pBirthday Birthday { bdName = tableColumn "name"
, bdDay = tableColumn "birthday" })使用TemplateHaskell生成函数pBirthday
$(makeAdaptorAndInstance "pBirthday" ''Birthday')其中,makeAdaptorAndInstance是在Data.Functor.Product.TH中定义的。
我想避免使用TemplateHaskell。opaleye教程简单地参考了Data.Functor.Product.TH的文档,其中只解释了由makeAdaptorAndInstance生成的实例将是:
instance (ProductProfunctor p, Default p a a', Default p b b', Default p c c')
=> Default p (Birthday a b c) (Birthday a' b' c')pBirthday的类型为:
pBirthday :: ProductProfunctor p =>
Birthday (p a a') (p b b') (p c c') -> p (Birthday a b c) (Birthday a' b' c')但是我找不到任何关于如何手动填充实现这些函数的信息。
发布于 2018-01-09 19:39:33
GHC有一个-ddump-splices option来查看用我认为这应该是有用的,因为它看起来可能不是太糟糕。(使用-ddump-to-file和-dumpdir控制输出位置。)
以下是编写它的一种方法:
instance (ProductProfunctor p, Default p a a', Default p b b') => Default p (Birthday' a b) (Birthday' a' b') where
def :: p (Birthday' a b) (Birthday' a' b')
def = pBirthday (Birthday def def)
pBirthday :: ProductProfunctor p =>
Birthday' (p a a') (p b b') -> p (Birthday a b) (Birthday a' b')
pBirthday (Birthday pa pb) =
Birthday `rmap` lmap bdName pa **** lmap bdDay pb
-- It generalizes the applicative construct
-- "Birthday <$> pa <*> pb"https://stackoverflow.com/questions/48167003
复制相似问题