可表示的仿函数同构为(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
类的要求?     
已邀请:
        是的,你有。尽管您的ѭ9实例已关闭。
instance Keyed Pair where
    mapWithKey f (Pair x y) = Pair (f False x) (f True y)
甚至更容易
instance Keyed Pair where
    mapWithKey = mapWithKeyRep
同样
instance Distributive Pair where
    distribute = distributeRep
给定
index
tabulate
,您可以在
Representable
模块中使用各种
fooRep
方法来提供所有其他超类的定义。 “ 33”和“ 1”的定义实际上并不是“ 4”的要求的一部分。不过,它们也包括在内,因为可表示性意味着您与函数同构,这使您可以将\“ exponential \”ѭ1aka(又名cowriter,或Traced comonad)的定义也回收为become1ѭ,并赋予一些monoid。您的代表。不过,这不是必需的,主要是因为鉴于所涉及的类型,我无法将其限制为保留。 您可能想将
Semigroup
Monoid
降为
Bool
,然后手动实施
extend
extract
。这很容易。
instance Extend Pair where
    extend f p@(Pair a b) = Pair (f p) (f (Pair b a))

instance Comonad Pair where
    extract (Pair a b) = a
同样,这种类型由可表示的尝试包提供,其中包括许多其他实例。 和,
import Control.Applicative
bool = [True, False]
f tt _tf _ft _ff True  True  = tt
f _tt tf _ft _ff True  False = tf
f _tt _tf ft _ff False True  = ft
f _tt _tf _ft ff False False = ff
associative f = and (assoc <$> bool <*> bool <*> bool) where 
    assoc a b c = f (f a b) c == f a (f b c)
semigroups = filter associative 
    [ f tt tf ft ff | tt <- bool, tf <- bool, ft <- bool, ff <- bool ]
unital (u, f) = all unit bool where 
    unit a = f u a == a && f a u == a
monoids = filter unital 
    [ (u, f) | u <- bool, f <- semigroups ]
显示您推测时有4种可能的monoid,如果您只想要扩展实例,则有8个半群可用。     

要回复问题请先登录注册