[英]Existentially quantified type class
What is the type-class equivalent to the following existentially quantified dictionary, inspired by the Pipe
type: 受以下
Pipe
启发,等效于以下存在量化字典的类型类是什么:
{-# LANGUAGE ExistentialQuantification, PolymorphicComponents #-}
data PipeD p = forall cat . PipeD {
isoI :: forall a b m r . Iso (->) (p a b m r) (cat m r a b),
categoryI :: forall m r . (Monad m) => CategoryI (cat m r) ,
monadI :: forall a b m . (Monad m) => MonadI (p a b m) ,
monadTransI :: forall a b . MonadTransI (p a b) }
The rough idea I'm going for is trying to say that given the (PipeLike p)
constraint, we can then infer (MonadTrans (pab), Monad (pabm)
and (using pseudo-code) (Category "\\ab -> pabmr")
. 我要提出的一个粗略想法是尝试说,给定
(PipeLike p)
约束,然后我们可以推断出(MonadTrans (pab), Monad (pabm)
和(使用伪代码) (Category "\\ab -> pabmr")
。
The CategoryI
and MonadI
are just the dictionary equivalents of those type-classes that I use to express the idea that Category
, Monad
, and MonadTrans
are (sort of) super-classes of this PipeLike
type. CategoryI
和MonadI
只是这些类型类的字典等效项,我用来表达Category
, Monad
和MonadTrans
是此PipeLike
类型的(某种)超类的PipeLike
。
The Iso
type is just the following dictionary storing an isomorphism: Iso
类型只是以下存储同构的字典:
data Iso (~>) a b = Iso {
fw :: a ~> b ,
bw :: b ~> a }
If this is indeed a type class, then the dictionary value is determined solely by the type p
. 如果确实是类型类,则字典值仅由类型
p
确定。 In particular, the type cat
is determined solely by p
. 特别是,类型
cat
仅由p
决定。 This can be expressed using an associated data type . 这可以使用关联的数据类型表示。 In a class definition, an associated data type is written like a data definition without a right-hand side.
在类定义中,关联的数据类型像没有右侧的数据定义一样编写。
Once you express cat
as a type, the other members can easily be changed to type classes, as I've shown for Monad
and MonadTrans
. 一旦将
cat
表示为一种类型,其他成员就可以轻松地更改为类型类,如我在Monad
和MonadTrans
所示。 Note that I prefer to use explicit kind signatures for complicated kinds. 请注意,我更喜欢对复杂种类使用显式种类签名。
{-# LANGUAGE TypeFamilies, FlexibleInstances, UndecidableInstances #-}
class Pipe (p :: * -> * -> (* -> *) -> * -> *) where
data Cat p :: (* -> *) -> * -> * -> * -> *
isoI :: forall a b m r. Iso (->) (p a b m r) (Category p m r a b)
categoryI :: forall a b m. Monad m => CategoryI (Category p m r)
instance (Pipe p, Monad m) => Monad (p a b m)
instance Pipe p => MonadTrans (p a b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.