如何将连续 Monad 分解为左右伴随?

Paw*_*mar 18 monads continuations haskell category-theory comonad

由于状态 monad 可以分解为产品(左 - 函子)和阅读器(右 - 可表示)。

  1. 有没有办法分解 Continuation Monad?下面的代码是我的尝试,它不会类型检查
-- To form a -> (a -> k) -> k
{-# LANGUAGE MultiParamTypeClasses, TypeOperators, InstanceSigs, TypeSynonymInstances #-}
type (<-:) o i = i -> o
-- I Dont think we can have Functor & Representable for this type synonym

class Isomorphism a b where
   from :: a -> b
   to :: b -> a

instance Adjunction ((<-:) e) ((<-:) e) where
   unit :: a -> (a -> e) -> e
   unit a handler = handler a

   counit :: (a -> e) -> e -> a
   counit f e = undefined -- If we have a constraint on Isomorphism a e then we can implement this
Run Code Online (Sandbox Code Playgroud)
  1. 是否有形成 monad 的 Left & Right Adjoints 列表?

  2. 我读过,给定一对伴随物,它们形成一个独特的 Monad & Comonad 但是,给定一个 Monad,它可以被分解为多个因子。有没有这样的例子?

Li-*_*Xia 18

这不会进行类型检查,因为该类Adjunction仅表示附属的一小部分子集,其中两个函子都是Hask 上的内函子。

事实证明,这不是附件的情况(<-:) r -| (<-:) r。这里有两个微妙不同的函子:

  • f = (<-:) r,从 Hask 到 Op(Hask) 的函子(Hask 的对立范畴,有时也表示为 Hask^op)
  • g = (<-:) r,从 Op(Hask) 到 Hask 的函子

特别是,这counit应该是 Op(Hask) 类别中的自然转换,它会翻转箭头:

unit   :: a -> g (f a)
counit :: f (g a) <-: a
Run Code Online (Sandbox Code Playgroud)

事实上,counit恰逢unit在这个附属。

为了正确捕捉这一点,我们需要对FunctorAdjunction类进行泛化,以便我们可以对不同类别之间的附属进行建模:

class Exofunctor c d f where
  exomap :: c a b -> d (f a) (f b)

class
  (Exofunctor d c f, Exofunctor c d g) =>
  Adjunction
    (c :: k -> k -> Type)
    (d :: h -> h -> Type)
    (f :: h -> k)
    (g :: k -> h) where
  unit :: d a (g (f a))
  counit :: c (f (g a)) a
Run Code Online (Sandbox Code Playgroud)

然后我们再次得到它Compose是一个 monad(如果我们翻转附加词则是一个 comonad):

newtype Compose f g a = Compose { unCompose :: f (g a) }
Run Code Online (Sandbox Code Playgroud)
adjReturn :: forall c f g a. Adjunction c (->) f g => a -> Compose g f a
adjReturn = Compose . unit @_ @_ @c @(->)

adjJoin :: forall c f g a. Adjunction c (->) f g => Compose g f (Compose g f a) -> Compose g f a
adjJoin = Compose . exomap (counit @_ @_ @c @(->)) . (exomap . exomap @(->) @c) unCompose . unCompose
Run Code Online (Sandbox Code Playgroud)

并且Cont只是其中的一个特例:

type Cont r = Compose ((<-:) r) ((<-:) r)
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,另请参阅此要点:https : //gist.github.com/Lysxia/beb6f9df9777bbf56fe5b42de04e6c64


我读过,给定一对伴随物,它们形成一个独特的 Monad & Comonad,但给定一个 Monad,它可以分解为多个因子。有没有这样的例子?

分解通常不是唯一的。一旦你像上面那样概括M了附属,那么你至少可以将任何 monad分解为它的 Kleisli 类别和它的基本类别(在本例中为 Hask)之间的附属。

Every monad M defines an adjunction
  F -| G
where

F : (->) -> Kleisli M
  : Type -> Type                -- Types are the objects of both categories (->) and Kleisli m.
                                -- The left adjoint F maps each object to itself.
  : (a -> b) -> (a -> M b)      -- The morphism mapping uses return.

G : Kleisli M -> (->)
  : Type -> Type                -- The right adjoint G maps each object a to m a
  : (a -> M b) -> (M a -> M b)  -- This is (=<<)
Run Code Online (Sandbox Code Playgroud)

我不知道延续 monad 是否对应于 Hask 上的内函子之间的附属。

另请参阅关于 monad 的 nCatLab 文章:https ://ncatlab.org/nlab/show/monad#RelationToAdjunctionsAndMonadicity

与附加词和一元性的关系

每个附加词 (L ? R) 诱导一个单子 R?L 和一个共子 L?R。通常有不止一个附加词以这种方式产生给定的 monad,事实上,给定的 monad 有一类附加词。该类别中的初始对象是 monad 的 Kleisli 类别上的附属,终端对象是代数的 Eilenberg-Moore 类别上的附属。(eg Borceux, vol. 2, prop. 4.2.2) 后者被称为一元附属。