如何对相关数据设置约束?

时间:2012-06-29 08:35:43

标签: haskell type-constraints type-families

我想声明关联数据始终是某个类的实例。

class (Context (Associated a b)) => Class a where
  data Associated a :: * -> *

instance Context (Associated a b) where
  func1 = error "func1"

但是,不在范围内的自由变量b会阻止我这样做。其中一个解决方案是从Context复制类函数,但它看起来很难看。

class Class a where
  data Associated a :: * -> *
  -- duplicate all functions from class Context
  contextFunc1 :: Associated a b -> String

instance Class a => Context (Associated a b) where
  func1 = contextFunc1

是否存在一种惯用的方法来对关联数据类型设置约束,这些数据类型包含头部未提及的变量?

编辑:我想保持与GHC 7.0.3的兼容性

3 个答案:

答案 0 :(得分:6)

pointed out by @SjoerdVisscher一样,在forall=> is actually not okclass左侧使用instance,至少还没有,尽管我的具体例子在ghc-7.4中有效。


这种方式似乎有效:

{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE Rank2Types           #-}
{-# LANGUAGE ConstraintKinds      #-}
{-# LANGUAGE UndecidableInstances #-}

class Context c where
  func1 :: c -> String

class (forall b. Context (Associated a b)) => Class a where
  data Associated a :: * -> *

newtype ClassTest = ClassTest { runClassTest :: String }

instance (forall b. Context (Associated ClassTest b)) => Class ClassTest where
  data Associated ClassTest b = ClassTestAssoc b (b -> ClassTest)

instance Context (Associated ClassTest b) where
  func1 (ClassTestAssoc b strFunc) = runClassTest $ strFunc b

main = putStr . func1 $ ClassTestAssoc 37 (ClassTest . show)

实例中的额外forall b约束似乎有点丑陋和多余,但显然它是必要的。

  

$ runghc-7.4.1 tFamConstraint0.hs
  37

答案 1 :(得分:5)

我没有GHC 7.0.3可用,但我认为这应该可以使用它。

您可以像这样手动传递词典(以Context = Show为例):

{-# LANGUAGE ScopedTypeVariables, TypeFamilies, ExistentialQuantification #-}

data ShowDict a = Show a => ShowDict

class Class a where
  data Associated a :: * -> *

  getShow :: ShowDict (Associated a b)

-- Convenience function
getShowFor :: Class a => Associated a b -> ShowDict (Associated a b)
getShowFor _ = getShow

showAssociated :: Class a => Associated a b -> String
showAssociated a = 
  case getShowFor a of
    ShowDict -> -- Show (Associated a b) is made available by this pattern match 
      show a

instance Class Int where
  data Associated Int b = Foo deriving Show

  getShow = ShowDict

main = print $ showAssociated Foo

这有点类似于您建议的复制功能,但优点是:

  • 避免重复(“Context”的方法签名)
  • 在上下文中显示“显示Baz”比仅具有显示“Baz”的函数更强大,因为它允许您调用(库)需要“显示Baz”的函数,或者使用隐含的实例,如`Show [巴兹]`:
showAssociateds :: forall a b. Class a => [Associated a b] -> String
showAssociateds as = 
  case getShow :: ShowDict (Associated a b) of
    ShowDict ->
      show as

主要的缺点是使用getShow始终需要显式类型签名(像getShowFor这样的函数可以缓解这种情况。)

答案 2 :(得分:0)

一种惯用的方法是创建一个Context1类。假设我们有

class Context a where
    func :: a -> String

我们可以概括为:

class Context1 f where
    func1 :: Context a => f a -> String

然后为所有Associated s:

提供一个实例
instance (Context1 (Associated a), Context b) => Context (Associated a b) where
    func = func1

现在可以轻松编写所需的课程

instance Context1 (Associated a) => Class a where
    data Associated a :: * -> *

您可以确定给定的Context1 (Associated a)上下文确保了所需的forall b. Context b => Context (Associated a b)上下文。

Hackage上有很多这种模式的例子,例如Show1Foldable1Traversable1