Questions tagged [comonad]

The inverse of a monad. A monad is a way to structure computations in terms of values and sequences of computations using those values. Monads allow the programmer to build up computations using sequential building blocks, which can themselves be sequences of computations.

63 questions
14
votes
1 answer

Comonad duplicate function

Why when you define the function duplicate duplicate :: w a -> w (w a) for the Comonad typeclass (link) you have to modify all the elements "in the context" (i.e. change elements other than the current value of the context). Why not just…
Jackie
  • 218
  • 1
  • 6
14
votes
3 answers

Is the concept of an "interleaved homomorphism" a real thing?

I am in need of the following class of functions: class InterleavedHomomorphic x where interleaveHomomorphism :: (forall a . f a -> g a) -> x f -> x g Obviously the name I invented for it is not in any way an official term for anything, and the…
dflemstr
  • 25,947
  • 5
  • 70
  • 105
13
votes
1 answer

Tree Functor and Foldable but with Nodes. Is there any generalization over it?

data Tree t = Empty | Node t (Tree t) (Tree t) We can create Functor instance and use fmap :: (t -> a) -> Tree t -> Tree a But what if instead of (t -> a) I want (Tree t -> a) so I could have access to a whole (Node t) not just t treeMap :: (Tree…
ais
  • 2,514
  • 2
  • 17
  • 24
12
votes
2 answers

Applicative is to monad what X is to comonad

Can we solve this equation for X ? Applicative is to monad what X is to comonad
nicolas
  • 9,549
  • 3
  • 39
  • 83
12
votes
1 answer

Every free monad over a ??? functor yields a comonad?

In this answer to "Can a monad be a comonad?" we see that Every Cofree Comonad over an Alternative functor yields a Monad. What would be the dual to this? Is there a class of functors that automatically make a free monad over them a comonad?
Petr
  • 62,528
  • 13
  • 153
  • 317
11
votes
1 answer

What would be the methods of a bi-comonad?

While musing what more useful standard class to suggest to this one class Coordinate c where createCoordinate :: x -> y -> c x y getFirst :: c x y -> x getSecond :: c x y -> y addCoordinates :: (Num x, Num y) => c x y -> c x y -> c x y it…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
8
votes
1 answer

Fixed-point of a monadic and comonadic computation

In Haskell, Given a monad m, there is mfix :: (a -> m a) -> m a that computes the fixed-point of a monadic computation. Dually, given a comonad w, there is cofix :: w (w a -> a) -> a that computes the fixed-point of a comonadic computations. Now…
Bob
  • 1,713
  • 10
  • 23
8
votes
1 answer

Alpha Beta Pruning with Recursion Schemes

I'm trying to get more proficient with recursion schemes as they have so far been really helpful for turning gnarly explicit recursion code into something less spike-y. One of the other tools I tend to reach for when implementing algorithms that can…
8
votes
2 answers

Why can't I find any law violations for the NotQuiteCofree not-quite-comonad?

On Twitter, Chris Penner suggested an interesting comonad instance for "maps augmented with a default value". The relevant type constructor and instance are transcribed here (with cosmetic differences): data MapF f k a = f a :< Map k (f a) …
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
8
votes
2 answers

Which Algebraic Pattern fits this type of tree?

I've got a puzzle for you, I managed to write some code that would do these things using recursion-schemes, but it's incredibly messy and that usually means that I missed a useful abstraction somewhere. I'm designing a layout system for my text…
Chris Penner
  • 1,881
  • 11
  • 15
7
votes
1 answer

How to work with AST with Cofree annotation?

I have this simple Expr AST and I can easily convert it to String. import Prelude hiding (Foldable) import qualified Prelude import Data.Foldable as F import Data.Functor.Foldable import Data.Monoid import Control.Comonad.Cofree data ExprF r =…
ais
  • 2,514
  • 2
  • 17
  • 24
6
votes
1 answer

Deriving a monad from a cofree comonad

Edward Kmett writes on his blog that using the Co newtype (from the kan-extensions package), it's possible to derive a Monad from any Comonad. I'd like to learn how to mechanically do this for any Cofree f a, as for some Cofree types I don't know a…
Johannes Riecken
  • 2,301
  • 16
  • 17
6
votes
1 answer

Understanding Comonad's <$$>

Given the following from fp-course: class Functor f where (<$>) :: (a -> b) -> f a -> f b class Functor f => Extend f where (<<=) :: (f a -> b) -> f a -> f b I defined <$$> as so: (<$$>) :: Comonad f => (a -> b) …
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
6
votes
1 answer

Is there a generic way to decompose the free comonad over a failure monad into a “values stream and final error”?

The Cofree comonad is useful for iterating partial functions in a way that's polymorphic on the error type. Its coiter resembles forM-looping in an error monad, but it gathers the produced values in a pure/lazy manner and you only see an error at…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
6
votes
1 answer

What benefits do I get from creating an instance of Comonad

In my application, I'm trying to implement an animation system. In this system, animations are represented as a cyclic list of frames: data CyclicList a = CL a [a] We can (inefficiently) advance the animation as follows: advance :: CyclicList a ->…
Mokosha
  • 2,737
  • 16
  • 33