如何在多态字段中使用HaskellDB? (重叠实例的问题)

时间:2011-11-12 14:49:53

标签: haskell haskelldb

我有一个有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,但这没有帮助(我真的想明白为什么用类型变量替换具体类型会导致这个问题如此成问题。)

非常感谢任何帮助和建议!

1 个答案:

答案 0 :(得分:2)

随着这个问题的出现,对你来说可能为时已晚,但也许其他人也会遇到类似的问题...

归结为这样一个事实:当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更改为

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。