리더, 라이터 모나드


17

CCCC 라고 하자 . (×) 의 제품 보조 기능으로 합시다 C. 으로 고양이 CCC는, 우리는 카레 수 (×) :

curry(×):C(CC)

curry(×)A=λB.A×B

Functor 범주 에는 일반적인 단일체 구조가 있습니다. CC 모노 이드에서 의 모나드 인 C . CCC 유한 제품은 단일체 구조로 간주됩니다 .C

curry(×)1id

A B.curry(×)(A×B)(curry(×)A)(curry(×)B)

따라서 는 단일체 구조를 유지하므로, 단일체를 모나드로, 코모 노이드를 코 모나드로 운반합니다. 즉, 임의의 모노 이드 w( W r i t e r w ) 모나드로 전송합니다 (정의를보십시오 -w 는 모노 아이드 여야합니다). 마찬가지로 대각선 코모 노이드Coreader모나드 로 전송합니다 .(curry(×))w(Writer w)w

이제는 구체적으로 Writer의 구성을 전개합니다.

시작하십시오. 실제로 , 그들은 단순히 Haskell에서 고유 한 이름을가집니다. 우리는이 하스켈 모노 이드 w를 , 해요 P의 P의 E N D , m의 전자 m의 P는 t의 예를 :Writer=Coreader=curry(×) w,mappend,mempty

mappend:w×ww

mempty:1w

Writer는 functor이므로 m e m p t y 와 같은 형태도 매핑해야합니다 . Haskell에서는 유효하지 않지만 다음과 같이 작성합니다.mappendmempty

Writer mappend:Writer(w×w)Writer w

는 자연 변환이며 C C의 형태 입니다. 의 속성으로 C U R R Y ( × ) 가 취하는 함수이다O B ( C를 ) 과의 morphism에 제공 C를 :Writer mappendCCcurry(×)aOb(C)C

Writer mappend a=mappend×(id(a)):Writer(w×w)aWriter w a

비공식적 유형의 합 성분 w를 펌프 을 그대로. 이것은 Haskell에서 Writer의 정의입니다. 하나 개의 장애물은 모나드입니다 W R I t 전자 R w , μ , η 우리가 필요Writer mappend awaWriter w,μ,η

μ:Writer wWriter wWriter w

즉, 유형의 비 호환성. 그러나이 펑 터는 동형입니다 : 자연 동형이다 유한 제품에 대한 일반적인 associator에 의해 λ . w × ( w × a ) = W r i t e r w W r i t e r wWriter(w×w)=λa.(w×w)×aλa.w×(w×a)=Writer wWriter w. 그럼 정의 통해 W R I t E R m P의 P의 E N D를 . 나는 m e m p t y 를 통한 η 의 구성을 생략한다 .μWriter mappendηmempty

라이터는 펑되는, 즉 보존 모노 이드 등식 가환도를 유지 우리 부여 입증 등식 위해 그래서 에서 = A 모노 이드 ( C C ) 에서 = 모나드 C . 종료.Writer w,μ,η(CC)C

무엇에 대한 독자 및 Cowriter? 리더는 Coreader 정의에 설명 된대로 Coreader와 인접합니다 (위 링크 참조). 마찬가지로 Cowriter는 Writer와 인접합니다. Cowriter에 대한 정의를 찾지 못 했으므로 표에 표시된 유추로 구성했습니다.

alt text

{- base, Hackage.category-extras -}
import Control.Comonad
import Data.Monoid
data Cowriter w a = Cowriter (w -> a)
instance Functor (Cowriter w) where
    fmap f (Cowriter g) = Cowriter (f . g)
instance Monoid w => Copointed (Cowriter w) where
    extract (Cowriter g) = g mempty
instance Monoid w => Comonad (Cowriter w) where
    duplicate (Cowriter g) = Cowriter
        (\w' -> Cowriter (\w -> g (w `mappend` w')))

아래는 이들 (공) 모 노드의 단순화 된 정의입니다. fr_ob F는 객체에 대한 functor F의 매핑을 나타내고 fr_mor F는 형태에 대한 functor F의 매핑을 나타냅니다. 거기에 모노 이드 개체입니다 C .w,+^,0^C

  • 작가
    • fr_ob(Writer w)a=a×w
    • fr_mor(Writer w)f=λa0,w2.a0,f w2
    • ηa=λa0.a0,0^
    • μa=λa0,w1,w0.a0,w0+^w1
  • 리더
    • fr_ob(Reader r)a=ra
    • fr_mor(Reader r)f=λg r0.f(g r0)
    • ηa=λa0 r0.a0
    • μa=λf r0.f r0 r0
  • 코리더
    • fr_ob(Coreader r)a=r×a
    • fr_mor(Coreader r)f=λr0,a0.f r0,a0
    • ηa=λr0,a0.a0
    • μa=λr0,a0.r0,r0,a0
  • Cowriter
    • fr_ob(Cowriter w)a=wa
    • fr_mor(Cowriter w)f=λg r0.f(g r0)
    • ηa=λf.f 0^
    • μa=λf w1w0.f(w0+^w1)

The question is that the adjunction in C relates functors, not monads. I do not see how the adjunction implies "Coreader is a comonad" "Reader is a monad" and "Writer is a monad" "Cowriter is a comonad".

Remark. I am struggling to provide more context. It requires some work. Especially, if you require categorical purity and those (co)monads were introduced for programmers. Keep nagging! ;)


Offer: You can take a screenshot of the table, and put the image here.
M.S. Dousti

You should copy the question here.
Dave Clarke

2
people downvoting should post a comment explaining why.
Suresh Venkat

1
@Ohad. I confess that I introduced that change to try to provide the question with more context (as was originally found in the blog post originally referenced). I think beroal should spend more effort making his question self contained, for example, by defining what Reader and Writer and Coreader and Cowriter are in categorical terms or in Haskell or both, rather than assuming that we all know what is being referred to.
Dave Clarke

2
@beroal: What I meant was that, as I don't use Haskell on a day to day basis, parsing the Haskell code and making the transition into CT is non-trivial for me, and perhaps others. By rephrasing the question in purely categorical terms, you are more likely to receive an answer quicker...
Ohad Kammar

답변:


13

Yes, if a monad M:CC has a right adjoint N, then N automatically inherits a comonad structure.

The general category-theoretic setting to understand this is as follows. Let C and D be two categories. Write Fun(C,D) for the categeory of functors from C to D; Its objects are functors and its morphisms natural transformations. Write FunL(C,D) for the full subcategory of Fun(C,D) on the functors which have right adjoints (in other words, we consider functors CD with right adjoints and arbitrary natural transformations between them). Write FR:DC for the right adjoint of a functor F:CD. Then R:FunL(C,D)Fun(D,C) is a contravariant functor: if α:FG is a natural transformation then there is an induced natural transformation αR:GRFR.

If C=D, then Fun(C,C) has a monoidal structure given by composition and so does FunL(C,D), because the composition of left adjoints is a left adjoint. Specifically, (FG)R=GRFR, so R is an antimonoidal contravariant functor. If you apply R to the structural natural transformations which equip a functor M with the structure of a monad, what you get out is a comonad.


1
And one should mention that some of these functors, for example R is not really a functor but rather something like a pseudo-functor because it typically satisfies functoriality only up to canonical isomorphisms. Nevertheless, the main point is valid.
Andrej Bauer

7

By the way, this:

Let (×) be a product bifunctor in C. As C is CCC, we can curry (×)

is slightly incorrect. For one, the usual terminology would be (if I'm not mistaken) that × is a bifunctor over or on C. "In" typically means constructions using the arrows and objects of a category, whereas functors "on" categories refer to constructions relating multiple categories. And the product bifunctor isn't a construction within a Cartesian category.

And this relates to the larger inaccuracy: the ability to curry the product bifunctor has nothing to do with C being Cartesian closed. Rather, it is possible because Cat, the category of categories (insert caveats) is Cartesian closed. So the currying in question is given by:

HomCat(C×D,E)HomCat(C,ED)

where C×D is a product of categories, and ED is the category of functors F:DE. This works regardless of whether C, D and E are Cartesian closed, though. When we let C=D=E, we get:

×:C×CC
curry×:CCC

But this is merely a special case of:

F:C×DE
curryF:CED

2 Dan Doel: Yes, yes, yes, thanks. I did the mistake while translating from the original post beroal.livejournal.com/23223.html .
beroal

4

Consider the adjunction F,G,ϵ,η. For every such adjunction we have a monad GF,η,GϵF and also a comonad FG,ϵ,FηG. Notably, F and G need not be endofunctors, and in general they aren't (e.g., the list monad is an adjuction between the free and forgetful functors between Set and Mon).

So, what you want to do is take Reader (or Writer) and decompose it into the adjoint functors which give rise to the monad and the corresponding comonad. Which is to say that the connection between Reader and Coreader (or Writer and Cowriter) isn't the one you're looking for.

And it's probably better to think of currying as :hom(×A,=)hom(,=A), i.e. X,Y. {f:X×AY}{f:XYA}. Or if it helps, :hom(×A,=×1)hom(1,=A)


2 wren ng thornton: I am not aware of any defining adjunction for Reader and Writer similar to adjunctions between Set and a category of algebraic structures. Or do you mean that every monad is defined by an adjunction as in "MacLane. Categories for the Working Mathematician. VI. Monads and Algebras. 2. Algebras for a Monad. Theorem 1 (Every monad is defined by its T-algebras)."? Can you be more specific? Actually my question is the conclusion of an attempt to define those (co)monads in elegant words as the list monad is.
beroal

@beroal: I'm pretty sure Reader and Writer aren't adjoint, or at least I've yet to find a way to get the categories to work out for it. No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above. I don't have a copy of MacLane, but yes T-algebras are the standard name for the trick above (but then again, all sorts of unrelated things are called "X-algebras", "Y-algebras",...).
wren romano

Which description of the list monad are you trying to match the eloquence of? Given the free monoid functor F:SetMon, the forgetful functor U:MonSet, the unit transformation η:idSetUF, and the counit transformation ϵ:FUidMon you have an adjunction F,U,η,ϵ. Which means you have a monad UF,η,UϵF, namely the list monad in Set. And you get the list comonad in Mon: FU,ϵ,FηU. Eloquent?
wren romano

Functors (Reader a) and (Writer a) are adjoint, and that adjunction gives rise to the (State a) monad.
beroal

"No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above". If you get the monad and the comonad from the adjunction between categories Set and Mon, you get the monad on Set and the comonad on Mon — different categories. But Reader and Writer are on the same CCC category.
beroal
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.