我有一个模式,它有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,但这没有什么帮助(我想真正理解为什么用类型变量替换具体类型会造成如此大的问题)。
任何帮助和建议都将不胜感激!
发布于 2012-07-09 14:34:22
以这个问题的年龄,现在回答可能已经太晚了,可能对你没有任何影响,但如果其他人也遇到了类似的问题……
归根结底,当entity在Book中使用时,不能推断您希望branch引用masterHead。错误消息的一部分:
选择取决于“entity0”的实例化。
告诉您需要在何处消除歧义,特别是您需要提供有关entity0应该是什么的更多信息。您可以提供一些类型注释来帮助解决问题。
首先,将branch定义为
type BranchTable entity = Table (RecCons (Revision entity) (Expr (Revision entity)) RecNil)
branch :: BrancTable entity
branch = baseTable "branch" $ hdbMakeEntry undefined然后将masterHead更改为read
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。
https://stackoverflow.com/questions/8105170
复制相似问题