[英]How to Factorize Continuation Monad into Left & Right Adjoints?
由於 State monad 可以分解為 Product(左 - Functor)和 Reader(右 - 可表示)。
-- 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
是否有形成單子的左右伴隨的列表?
我讀過,給定一對伴隨詞,它們形成一個獨特的 Monad 和 Comonad,但是,給定一個 Monad,它可以分解為多個因子。 有這方面的例子嗎?
這不會進行類型檢查,因為 class Adjunction
僅代表一小部分附加功能,其中兩個函子都是Hask 上的 endofunctors 。
事實證明, (<-:) r -| (<-:) r
不是這種情況。 (<-:) 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
事實上, counit
與這個附加詞中的unit
是一致的。
為了正確捕捉這一點,我們需要泛化Functor
和Adjunction
類,以便我們可以在不同類別之間進行 model 附加:
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
然后我們再次得到Compose
是一個單子(如果我們翻轉附加詞,也是一個共單子):
newtype Compose f g a = Compose { unCompose :: f (g a) }
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
而Cont
只是其中的一個特例:
type Cont r = Compose ((<-:) r) ((<-:) r)
有關更多詳細信息,另請參閱此要點:https://gist.github.com/Lysxia/beb6f9df9777bbf56fe5b42de04e6c64
我讀過,給定一對伴隨詞,它們形成一個獨特的 Monad 和 Comonad,但給定一個 Monad,它可以分解為多個因子。 有這方面的例子嗎?
因式分解通常不是唯一的。 一旦你如上所述概括了附加詞,那么你至少可以將任何 monad M
作為其 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 (=<<)
我不知道延續單子是否對應於 Hask 上的 endofunctors 之間的附屬物。
另請參閱有關 monad 的 nCatLab 文章: https://ncatlab.org/nlab/show/monad#RelationToAdjunctionsAndMonadicity
與附加詞和一元性的關系
每個附加詞 (L ⊣ R) 誘導出一個單子 R∘L 和一個共單子 L∘R。 通常有不止一個附加詞以這種方式產生一個給定的單子,實際上對於一個給定的單子有一類附加詞。 該類別中的初始 object 是單子的 Kleisli 類別的附屬物,而終端 object 是代數的 Eilenberg-Moore 類別的附屬物。 (例如 Borceux, vol. 2, prop. 4.2.2) 后者被稱為一元附加語。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.