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
113
votes
1 answer

Understanding why Zipper is a Comonad

This is a follow-up to the answer to my previous question. Suppose I need to map each item a:A of List[A] to b:B with function def f(a:A, leftNeighbors:List[A]): B and generate List[B]. Obviously I cannot just call map on the list but I can use the…
Michael
  • 41,026
  • 70
  • 193
  • 341
109
votes
2 answers

What is the Comonad typeclass in Haskell?

What is the Comonad typeclass in Haskell? As in Comonad from Control.Comonad in the comonad package (explanations of any other packages that provide a Comonad typeclass are also welcome). I've vaguely heard about Comonad, but all I really know about…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
83
votes
3 answers

Zipper Comonads, Generically

Given any container type we can form the (element-focused) Zipper and know that this structure is a Comonad. This was recently explored in wonderful detail in another Stack Overflow question for the following type: data Bin a = Branch (Bin a) a (Bin…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
56
votes
3 answers

Writing cojoin or cobind for n-dimensional grid type

Using the typical definition of type-level naturals, I've defined an n-dimensional grid. {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE TypeFamilies #-} data Nat = Z | S Nat data U (n :: Nat) x…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
50
votes
2 answers

What is the Store comonad?

Having some idea of what the Comonad typeclass is in Haskell, I've heard about the Store comonad. But looking at Control.Comonad.Store.Lazy, I don't really get it. What does it mean? What is it for? I've heard that Store = CoState, the dual of the…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
49
votes
8 answers

How to extract value from monadic action

Is there a built-in function with signature :: (Monad m) => m a -> a ? Hoogle tells that there is no such function. Can you explain why?
42
votes
1 answer

What are some motivating examples for Cofree CoMonad in Haskell?

I've been playing with Cofree, and can't quite grok it. For example, I want to play with Cofree [] Num in ghci and can't quite get any interesting examples. For example, if I construct a Cofree type: let a = 1 :< [2, 3] I would expect extract a ==…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
31
votes
1 answer

Can you define `Comonads` based on `Monads`?

Okay, so let's say you have the type newtype Dual f a = Dual {dual :: forall r. f(a -> r)->r} As it turns out, when f is a Comonad, Dual f is a Monad (fun exercise). Does it work the other way around? You can define fmap ab (Dual da) = Dual $ \fb…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
30
votes
1 answer

Does the chain function in underscore.js create a monad?

In the chain documentation you find: Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, use value to retrieve the final value. So does the chain…
jcubic
  • 61,973
  • 54
  • 229
  • 402
26
votes
1 answer

How to make a binary tree zipper an instance of Comonad?

I want to make a binary tree zipper an instance of comonad, but I can't figure out how to implement duplicate properly. Here is my attempt: {-# LANGUAGE DeriveFunctor #-} import Data.Function import Control.Arrow import Control.Comonad data BinTree…
Javran
  • 3,394
  • 2
  • 23
  • 40
21
votes
3 answers

Comonad example in Scala

What is Comonad, if it's possible describe in Scala syntax. I found scalaz library implementation, but it's not clear where it can be useful.
Stas
  • 751
  • 1
  • 11
  • 22
17
votes
1 answer

How to Factorize Continuation Monad into Left & Right Adjoints?

As State monad can be factorized into Product (Left - Functor) and Reader (Right - Representable). Is there a way to factorize Continuation Monad? Below code is my attempt, which wont type check -- To form a -> (a -> k) -> k {-# LANGUAGE…
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
17
votes
3 answers

Comonadically finding all the ways to focus on a grid

{-# LANGUAGE DeriveFoldable #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE DeriveTraversable #-} import Control.Comonad import Data.Functor.Reverse import Data.List (unfoldr) First some context (ha ha). I have a zipper over non-empty lists. data…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
16
votes
2 answers

Why IO is a monad instead of a comonad?

An output is an effectful computation. It thus makes sense to encapsulate it into a monad. But an input is a context-sensitive computation. It would thus make more sense to encapsulate it into a comonad. However in Haskell input and output are both…
Bob
  • 1,713
  • 10
  • 23
15
votes
2 answers

Theoretically, is this a valid comonad instance for a list?

I'm trying to grasp the concept of comonads, and after reading this blog post, I think I have a solid understand of what they do and how they are related to monads. But, I thought I would delve into the subject a little bit and just think about what…
Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
1
2 3 4 5