gpt4 book ai didi

haskell - 如何将 HaskellDB 与多态字段一起使用? (实例重叠的问题)

转载 作者:行者123 更新时间:2023-12-03 06:06:07 25 4
gpt4 key购买 nike

我有一个模式,其中有 6 种不同类型的实体,但它们都有很多共同点。我想我可能可以在类型级别抽象出很多这种共性,但我遇到了 HaskellDB 和重叠实例的问题。这是我开始使用的代码,运行良好:

import Database.HaskellDB
import Database.HaskellDB.DBLayout

data Revision a = Revision deriving Eq
data Book = Book

instance FieldTag (Revision a) where
fieldName _ = "rev_id"

revIdField :: Attr (Revision Book) (Revision Book)
revIdField = mkAttr undefined

branch :: Table (RecCons (Revision Book) (Expr (Revision Book)) RecNil)
branch = baseTable "branch" $ hdbMakeEntry undefined
bookRevision :: Table (RecCons (Revision Book) (Expr (Revision Book)) RecNil)
bookRevision = baseTable "book_revision" $ hdbMakeEntry undefined

masterHead :: Query (Rel (RecCons (Revision Book) (Expr (Revision Book)) RecNil))
masterHead = do
revisions <- table bookRevision
branches <- table branch
restrict $ revisions ! revIdField .==. branches ! revIdField
return revisions

这工作正常,但 branch 太具体了。我其实想表达的是:

branch :: Table (RecCons (Revision entity) (Expr (Revision entity)) RecNil)
branch = baseTable "branch" $ hdbMakeEntry undefined

但是,通过此更改,我收到以下错误:

Overlapping instances for HasField
(Revision Book)
(RecCons (Revision entity0) (Expr (Revision entity0)) RecNil)
arising from a use of `!'
Matching instances:
instance [overlap ok] HasField f r => HasField f (RecCons g a r)
-- Defined in Database.HaskellDB.HDBRec
instance [overlap ok] HasField f (RecCons f a r)
-- Defined in Database.HaskellDB.HDBRec
(The choice depends on the instantiation of `entity0'
To pick the first instance above, use -XIncoherentInstances
when compiling the other instance declarations)
In the second argument of `(.==.)', namely `branches ! revIdField'
In the second argument of `($)', namely
`revisions ! revIdField .==. branches ! revIdField'
In a stmt of a 'do' expression:
restrict $ revisions ! revIdField .==. branches ! revIdField

我尝试过盲目地抛出 -XOverlappingInstances-XIncoherentInstances ,但这并没有帮助(而且我想真正理解为什么要替换混凝土带有类型变量的类型会导致这个问题)。

任何帮助和建议将不胜感激!

最佳答案

随着这个问题的出现,现在回答可能已经太晚了,无法对您产生任何影响,但也许如果其他人也遇到类似的问题......

归结为这样一个事实:无法推断出您想要 entity引用Bookbranch用于masterHead 。错误消息的部分内容为

The choice depends on the instantiation of `entity0'

告诉您需要消除歧义的地方,特别是您需要提供有关 entity0 的更多信息。应该。您可以提供一些类型注释来帮助解决问题。

首先,定义branch作为

type BranchTable entity = Table (RecCons (Revision entity) (Expr (Revision entity)) RecNil)
branch :: BrancTable entity
branch = baseTable "branch" $ hdbMakeEntry undefined

然后更改masterHead阅读

masterHead :: Query (Rel (RecCons (Revision Book) (Expr (Revision Book)) RecNil))
masterHead = do
revisions <- table bookRevision
branches <- table (branch :: BranchTable Book)
restrict $ revisions ! revIdField .==. branches ! revIdField
return revisions

注意应用于 branch 的类型注释:branch :: BranchTable Book这有助于消除导致类型错误的歧义。

制作 masterHead适用于任何带有Revision e的东西在其中的字段中,您可以使用以下定义:

masterHead :: (ShowRecRow r, HasField (Revision e) r) => Table r -> e -> Query (Rel r)
masterHead revTable et =
do revisions <- table revTable
branches <- table branch'
restrict $ revisions ! revIdField' .==. branches ! revIdField'
return revisions
where (branch', revIdField') = revBundle revTable et
revBundle :: HasField (Revision e) r => Table r -> e -> (BranchTable e, Attr (Revision e) (Revision e))
revBundle table et = (branch, revIdField)

et需要参数来指定 e 的内容类型应该并且可以是 undefined归因于正确的类型,如

masterHead bookRevision (undefined :: Book)

生成 SQL

SELECT rev_id1 as rev_id
FROM (SELECT rev_id as rev_id2
FROM branch as T1) as T1,
(SELECT rev_id as rev_id1
FROM book_revision as T1) as T2
WHERE rev_id1 = rev_id2

这确实需要 FlexibleContexts虽然如此,但它可以应用于提问者的模块,而无需重新编译 HaskellDB。

关于haskell - 如何将 HaskellDB 与多态字段一起使用? (实例重叠的问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8105170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com