loeb

cfix

mfix

mfix

cfix

loeb

cobind

cobind

cobind

cfix

loeb

cfix

loeb

cfix

[]



> instance Show (x -> a)

> instance Eq (x -> a)



> instance (Num a,Eq a) => Num (x -> a) where

> fromInteger = const . fromInteger

> f + g = \x -> f x + g x

> f * g = \x -> f x * g x

> negate = (negate .)

> abs = (abs .)

> signum = (signum .)



> class Comonad w where

> coreturn :: w a -> a

> cobind :: (w a -> b) -> w a -> w b



> instance Comonad [] where

> coreturn (x:xs) = x

> cobind f [] = []

> cobind f (x:xs) = f (x:xs) : cobind f xs



> cfix :: Comonad d => d (d a -> a) -> a

> cfix d = coreturn d (cobind cfix d)



> ouroboros = [2*head.tail,1+head.tail,17]

> test = cfix ouroboros



test

head

tail

2*head.tail

head.tail

cfix [1+head.tail,17]

loeb

cfix

loeb

cfix

loeb

dropWhile

I'm too busy reading Bernie Pope and Russell O'Connor's articles in the latest Monad Reader to think about much else right now, so I'm only going to make a brief post. I want to mention something pointed out by Nick Frisby over here . Thefunction is in fact closely related to, the dual offor monads, defined by Dave Menendez . (Nick Frisby's statement isn't quite correct but it's interest value outweighs its truthiness.) In fact, I can borrow Russell's language to say that whereassends data back in time,andread data from the future!Think of it like this: when you usewith a comonad you provide some function that distills down a comonadic data structure into a single value and thenapplies this distillation repeatedly at every point in the comonadic structure to give you another comonadic structure. My canonical example is the cellular automaton where you give a rule that defines how to compute each individual cell as a function of the whole grid, andapplies this throughout an entire input grid to give you back an entire output grid. The functionsandallow you to give rules for each cell where you're allowed to make reference to the entire output grid. If you think of the output grid as something in the future that you haven't yet computed, thenandallow you to read from the future.Here's an example ofit in use (where I makea comonad by interpreting it as a kind of one-sided zipper):In the expressionwe can useandto refer to the sublist to the right of each element from within the list itself. For example, in the subexpressiontheis being applied toNote how in this example (1)gives you back an entire list, not just one element and (2)respects the comonadic structure in the sense that you can only refer to elements to the right of the current one whereaslets you refer to the entire output list. I think this helps to make clear the differences and similarities betweenandThat wasn't too brief. But I did cheat by copying and pasting code from an old post. Anyway, back to tinkering with my implementation of...