可表示的仿函数同构为(Bool-> a)。
||
我以为我会尝试有趣的Representable-functors包来为
data Pair a = Pair a a
给出的函子定义一个Monad
和Comonad
实例,该实例可由Bool
表示;如我对向量monad的先前问题的回答中所述。
我注意到的第一件事是要使我的类型成为Representable
的实例,我不仅应定义tabulate
和index
,而且还应确保我的类型是Indexable
,Distributive
,Keyed
,Apply
,Applicative
和Functor
类型类的实例。好吧,index
完成了Indexable
的定义,Apply
的<.>
功能可以使用Applicative
中的<*>
;并且不需要Functor
实例也就不足为奇了。然而,我对我的instances9ѭ和Distributive
的实例表示怀疑。
data Pair a = Pair a a
deriving (Show,Eq)
instance Functor Pair where
fmap f (Pair x y) = Pair (f x) (f y)
type instance Key Pair = Bool
instance Keyed Pair where
mapWithKey f (Pair x y) = Pair (f False x) (f False y)
instance Indexable Pair where
index (Pair x _) False = x
index (Pair _ y) True = y
instance Applicative Pair where
pure a = Pair a a
Pair f g <*> Pair x y = Pair (f x) (g y)
instance Apply Pair where
(<.>) = (<*>)
instance Distributive Pair where
collect f x = Pair (getL . f <$> x) (getR . f <$> x)
where getL (Pair x _) = x
getR (Pair _ y) = y
instance Representable Pair where
tabulate f = Pair (f False) (f True)
我的mapWithKey
定义借鉴了Keyed
的for24ѭ实例:尽管我不明白为什么每次迭代都在其中使用there26ѭ。我对Pair
的每个学期也使用False
。
正如我通过定义Monad
和ѭ1concluded实例得出的结论,我发现Bool
需要Extend
的Semigroup
定义和Comonad
的Monoid
定义。我遵循Semigroup
的Semigroup
实例,它与(||)
同构,并为mempty
选择False
:
instance Monad Pair where
return = pureRep
(>>=) = bindRep
instance Monoid Bool where
mempty = False
mappend = (||)
instance Semigroup Bool where
(<>) = mappend
instance Extend Pair where
extend = extendRep -- needs Bool Semigroup
instance Comonad Pair where
extract = extractRep -- needs Bool Monoid
那么,我是否正确且习惯地满足了Representable
类的要求?
没有找到相关结果
已邀请:
1 个回复
棠媳鳖
甚至更容易
同样
给定
和
,您可以在
模块中使用各种
方法来提供所有其他超类的定义。 “ 33”和“ 1”的定义实际上并不是“ 4”的要求的一部分。不过,它们也包括在内,因为可表示性意味着您与函数同构,这使您可以将\“ exponential \”ѭ1aka(又名cowriter,或Traced comonad)的定义也回收为become1ѭ,并赋予一些monoid。您的代表。不过,这不是必需的,主要是因为鉴于所涉及的类型,我无法将其限制为保留。 您可能想将
和
降为
,然后手动实施
和
。这很容易。
同样,这种类型由可表示的尝试包提供,其中包括许多其他实例。 和,
显示您推测时有4种可能的monoid,如果您只想要扩展实例,则有8个半群可用。