[Haskell-cafe] syntactic sugar for comonads
Scherrer, Chad
Chad.Scherrer at pnl.gov
Fri Dec 2 17:03:35 EST 2005
Hi,
I'm wondering about a syntactic sugar for comonads. These are still very
new to me, but it seems like their usage will become much more common
once manipulations are more convenient (I believe this was the case with
monads and arrows, correct?).
Here's an example for motivation. Consider the data type
data Stream a = a :< Stream a
and suppose we want to write a function
movingWindow :: Stream a -> Stream [a]
so that, for example,
movingWindow 3 (1 :< 2 :< 3 :< ...)
evaluates to
[1,2,3] :< [2,3,4] :< [3,4,5] :< ...
Recognizing Stream as a comonad, this is pretty easy:
movingWindow n s = s =>> (take n . toList)
toList :: Stream a -> [a]
toList (x :< xs) = x : toList xs
If the second argument of (=>>) is written in lambda form, this comes
out as
movingWindow n s = s =>> \x ->
take n $ toList x
This looks analogous to the way do is translated for monads, so a sugary
alternative could be
movingWindow n s = do x -< s
take n $ toList x
Note that the presence of "-<" tells us we're in a comonad, rather than
a monad.
I'm not at all stuck on this, but I think it would be good to get the
ball rolling. What do you think?
Chad Scherrer
Computational Mathematics Group
Pacific Northwest National Laboratory
"Time flies like an arrow; fruit flies like a banana." -- Groucho Marx
More information about the Haskell-Cafe
mailing list