Haskell 是否可以在超类约束中引入其他类型变量?
在处理类型族时,通常可以方便地使用相等约束,以避免在签名中重复某些类型函数的名称:Haskell 是否可以在超类约束中引入其他类型变量?,haskell,typeclass,type-constraints,type-families,Haskell,Typeclass,Type Constraints,Type Families,在处理类型族时,通常可以方便地使用相等约束,以避免在签名中重复某些类型函数的名称: class Foo f where type BulkyAssociatedType f :: * foo :: BulkyAssociatedType f -> f ... bar :: forall m f b . ( Monad m, Foo f, b ~ BulkyAssociatedType f , Monoid b, Monoid (m b)
class Foo f where
type BulkyAssociatedType f :: *
foo :: BulkyAssociatedType f -> f
...
bar :: forall m f b .
( Monad m, Foo f, b ~ BulkyAssociatedType f
, Monoid b, Monoid (m b)
) => f -> m f
即使缩写词没有出现在签名本身中,也可以这样做,只是出现在约束中
在课堂上,这显然是不可能的
class ( Foo f, b ~ BulkyAssociatedType f, Monoid b, ...) => Bar f
抱怨类型变量b
不在范围内
这是实现类似目标的某种方法,以避免重复样板文件吗?我惊讶地发现,您无法做到这一点(我使用了相同的技术,并且知道它可以在实例声明中工作),但支持这一点似乎由来已久 也许你可以使用它来获得同样的好处:
{-# LANGUAGE TypeFamilies , FlexibleContexts , ConstraintKinds #-}
import Data.Monoid
class Foo f where
type BulkyAssociatedType f :: *
type B f = (Monoid (BulkyAssociatedType f))
class ( Foo f, B f) => Bar f
这是个好问题。我怀疑答案可能是“不”,但我的怀疑还不够强烈,无法回答这个问题。。。