[Haskell-cafe] indexed writer monad

Nicholls, Mark nicholls.mark at vimn.com
Thu Mar 26 08:26:27 UTC 2015


It does

So im not barking up the wrong tree?

Let me have a go, understand though that haskell comes about 5th in my list of languages by proffieciency, turning type cartwheels in fbound oo types is how my brain is wired, not seemingly trivial handstands in haskell

It may take me several days (this is not part of my day job)!


> On 25 Mar 2015, at 21:31, Roman Cheplyaka <roma at ro-che.info> wrote:
> 
> Alright, this does look like an indexed writer monad.
> 
> Here's the indexed writer monad definition:
> 
> newtype IxWriter c i j a = IxWriter { runIxWriter :: (c i j, a) }
> 
> Here c is a Category, ie. an indexed Monoid.
> 
> Under that constraint, you can make this an indexed monad (from the
> 'indexed' package). That should be a good exercise that'll help you to
> understand how this all works.
> 
> In practice, you'll probably only need c = (->), so your writer becomes
> isomorphic to (i -> j, a). This is similar to the ordinary writer monad
> that uses difference lists as its monoid.
> 
> The indexed 'tell' will be
> 
>  tell :: a -> IxWriter (->) z (z, a) ()
>  tell a = IxWriter (\z -> (z,a), ())
> 
> And to run an indexed monad, you apply that i->j function to your end
> marker.
> 
> Does this help?
> 
>> On 25/03/15 18:27, Nicholls, Mark wrote:
>> Ok...
>> 
>> Well lets just take the indexed writer
>> 
>> So....for a writer we're going...
>> 
>> e.g.
>> 
>> (a,[c]) -> (a -> (b,[c])) -> (b,[c])
>> 
>> If I use the logging use case metaphor...
>> So I "log" a few Strings (probably)....and out pops a (t,[String])
>> ?
>> 
>> (I've never actually used one!)
>> 
>> But what if I want to "log" different types...
>> 
>> I want to "log" a string, then an integer bla bla...
>> 
>> So I won't get the monoid [String]
>> 
>> I should get something like a nest 2 tuple.
>> 
>> do
>> log 1
>> log "a"
>> log 3
>> log "sdds"
>> return 23
>> 
>> I could get a 
>> (Integer,(String,(Integer,(String,END))))
>> 
>> and then I could dereference this indexed monoid(?)...by splitting it into a head of a specific type and a tail...like a list.
>> 
>> Maybe my use of "indexed" isn't correct.
>> 
>> So it seems to me, that writer monad is dependent on the monoid append (of lists usually?)....and my "special" monad would be dependent on a "special" monoid append of basically 
>> 
>> (these are types)
>> 
>> (a,b) ++ (c,d) = (a,b,c,d)
>> (a,b,c,d) ++ (e) = (a,b,c,d,e)
>> 
>> Which are encoded as nested 2 tuples (with an End marker)
>> 
>> (a,(b,End) ++ (c,(d,End)) = (a,(b, (c,(d,End)))
>> ?
>> 
>> That sort of implies some sort of type family trickery...which isnt toooo bad (I've dabbled).
>> 
>> Looks like an ixmonad to me....In my pigoeon Haskell I could probably wrestle with some code for a few days....but does such a thing already exist?...I don't want to reinvent the wheel, just mess about with it, and turn it into a cog (and then fail to map it back to my OO world).
>> 
>> -----Original Message-----
>> From: Roman Cheplyaka [mailto:roma at ro-che.info] 
>> Sent: 25 March 2015 4:03 PM
>> To: Nicholls, Mark; haskell-cafe at haskell.org
>> Subject: Re: [Haskell-cafe] indexed writer monad
>> 
>> Sorry, I didn't mean to scare you off.
>> 
>> By Category, I didn't mean the math concept; I meant simply the class from the Control.Category module. You don't need to learn any category theory to understand it. Since you seem to know already what Monoid is, try to compare those two classes, notice the similarities and see how (and why) one could be called an indexed version of the other.
>> 
>> But if you don't know what "indexed" means, how do you know you need it?
>> Perhaps you could describe your problem, and we could tell you what abstraction would fit that use case, be it indexed monad or something else.
>> 
>>> On 25/03/15 17:22, Nicholls, Mark wrote:
>>> Ah that assumes I know what a category is! (I did find some code that claimed the same thing)....maths doesn't scare me (much), and I suspect its nothing complicated (sounds like a category is a tuple then! Probably not), but I don't want to read a book on category theory to write a bit of code...yet.
>>> 
>>> Ideally there would be a chapter in something like "learn you an indexed Haskell for the great good".
>>> 
>>> Then I could take the code, use it...mess about with it...break it...put it back together in a slightly different shape and....bingo...it either works...or I find theres a good reason why it doesn't....(or I post a message to the café). 
>>> 
>>> -----Original Message-----
>>> From: Roman Cheplyaka [mailto:roma at ro-che.info]
>>> Sent: 25 March 2015 2:46 PM
>>> To: Nicholls, Mark; haskell-cafe at haskell.org
>>> Subject: Re: [Haskell-cafe] indexed writer monad
>>> 
>>> An indexed monoid is just a Category.
>>> 
>>>> On 25/03/15 16:32, Nicholls, Mark wrote:
>>>> Anyone?
>>>> 
>>>> 
>>>> 
>>>> I can handle monads, but I have something (actually in F#) that feels 
>>>> like it should be something like a indexed writer monad (which F# 
>>>> probably wouldn't support).
>>>> 
>>>> 
>>>> 
>>>> So I thought I'd do some research in Haskell.
>>>> 
>>>> 
>>>> 
>>>> I know little or nothing about indexed monad (though I have built the 
>>>> indexed state monad in C#).
>>>> 
>>>> 
>>>> 
>>>> So I would assume there would be an indexed monoid (that looks at bit 
>>>> like a tuple?).
>>>> 
>>>> 
>>>> 
>>>> e.g.
>>>> 
>>>> 
>>>> 
>>>> (a,b) ++ (c,d) = (a,b,c,d)
>>>> 
>>>> (a,b,c,d) ++ (e) = (a,b,c,d,e)
>>>> 
>>>> 
>>>> 
>>>> ?
>>>> 
>>>> 
>>>> 
>>>> There seems to be some stuff about "update monads", but it doesn't 
>>>> really look like a writer.
>>>> 
>>>> 
>>>> 
>>>> I could do with playing around with an indexed writer, in order to 
>>>> get my head around what I'm doing..then try and capture what I'm 
>>>> doing.then try (and fail) to port it back.
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> CONFIDENTIALITY NOTICE
>>>> 
>>>> This e-mail (and any attached files) is confidential and protected by 
>>>> copyright (and other intellectual property rights). If you are not 
>>>> the intended recipient please e-mail the sender and then delete the 
>>>> email and any attached files immediately. Any further use or 
>>>> dissemination is prohibited.
>>>> 
>>>> While MTV Networks Europe has taken steps to ensure that this email 
>>>> and any attachments are virus free, it is your responsibility to 
>>>> ensure that this message and any attachments are virus free and do 
>>>> not affect your systems / data.
>>>> 
>>>> Communicating by email is not 100% secure and carries risks such as 
>>>> delay, data corruption, non-delivery, wrongful interception and 
>>>> unauthorised amendment. If you communicate with us by e-mail, you 
>>>> acknowledge and assume these risks, and you agree to take appropriate 
>>>> measures to minimise these risks when e-mailing us.
>>>> 
>>>> MTV Networks International, MTV Networks UK & Ireland, Greenhouse, 
>>>> Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions 
>>>> International, Be Viacom, Viacom International Media Networks and 
>>>> VIMN and Comedy Central are all trading names of MTV Networks Europe.  
>>>> MTV Networks Europe is a partnership between MTV Networks Europe Inc. 
>>>> and Viacom Networks Europe Inc.  Address for service in Great Britain 
>>>> is
>>>> 17-29 Hawley Crescent, London, NW1 8TT.
>>>> 
>>>> 
>>>> 
>>>> _______________________________________________
>>>> Haskell-Cafe mailing list
>>>> Haskell-Cafe at haskell.org
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>> 
>>> CONFIDENTIALITY NOTICE
>>> 
>>> This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
>>> 
>>> While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
>>> 
>>> Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
>>> 
>>> MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
>> 
>> CONFIDENTIALITY NOTICE
>> 
>> This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
>> 
>> While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
>> 
>> Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
>> 
>> MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
> 
CONFIDENTIALITY NOTICE

This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.

While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.

Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.

MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.


More information about the Haskell-Cafe mailing list