comonad

Random walk on a pointed container

北慕城南 提交于 2021-02-08 08:19:15
问题 Let us consider a dwarf wandering in a tunnel. I will define a type that represents this situation thusly: data X a = X { xs :: [a], i :: Int } display :: X Bool -> IO () display X{..} = putStrLn (concatMap f xs) where { f True = "*" ; f False = "-" } Here you see a dwarf in a section of a tunnel: λ display x -*--- It is discovered that a pointed container is an instance of Comonad. I can use this instance here to define a function that simulates my dwarf moving right: shiftRight :: X Bool ->

Type Variable Location in Transformers

老子叫甜甜 提交于 2020-01-23 17:02:23
问题 Consider the State type - or at least a simplified version: newtype State s a = State { runState :: s -> (a, s) } Now, let's say we want to derive the StateT monad transformer. transformers defines it as follows: newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } Here, the m has been placed on the right of the function arrow, but outside the tuple. However, if we didn't know the correct answer, we might instead put m somewhere else: newtype StateT s m a = StateT { runStateT :: m (s

Writer monad and unsequence

你说的曾经没有我的故事 提交于 2020-01-06 18:10:14
问题 I am using the Writer monad to keep track of an error ("collision") flag on arbitrary values (such as Int ). Once the flag is set it is "sticky" and attaches itself to all values produced as a result of any operation with the marked one. Sometimes the collision flag is associated with individual values, sometimes I would like to associate with composite structures such as lists. Of course, once the collision flag is set for a whole list, it also makes sense to assume it is set for an

Pithy summary for codata (Where a comonad is a 'type for input impurity')

自作多情 提交于 2019-12-24 05:31:36
问题 In terms of pithy summaries - this description of Comonads seems to win - describing them as a 'type for input impurity'. What is an equivalent pithy (one-sentence) description for codata? 回答1: "Codata are types inhabited by values that may be infinite" This contrasts with "data" which is inhabited only by finite values. For example, if we take the "data" definition of lists, it is inhabited by lists of finite length (as in ML), but if we take the "codata" definition it is inhabited also by

Pithy summary for comonad. (Where a monad is a 'type for impure computation')

时光怂恿深爱的人放手 提交于 2019-12-23 16:43:34
问题 In terms of pithy summaries - this description of Monads seems to win - describing them as a 'type for impure computation'. What is an equivalent pithy (one-sentence) description of a comonad? 回答1: "A type for context-dependent computation" Alternatively, a better "pithy description" for monads might be a 'type for output impurity', in which case then the pithy description for comonads is a 'type for input impurity'. (If you are interested in comonads, some more introduction is given in some

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

白昼怎懂夜的黑 提交于 2019-12-20 10:00:29
问题 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 function create a monad? 回答1: No, not a monad, but a comonad! It turns a function that takes a wrapped object and returns a normal value into a function that both takes and returns a wrapped object. As a Haskell type signature that would be: (Wrapped a -> b) ->

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

独自空忆成欢 提交于 2019-12-20 08:42:45
问题 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 a = Leaf a | Branch a (BinTree a) (BinTree a) deriving (Functor, Show, Eq) data Dir = L | R deriving (Show, Eq) -- an incomplete binary tree, aka data context data Partial a = Missing Dir (BinTree a) a deriving (Show, Eq, Functor) -- BTZ for

How to work with AST with Cofree annotation?

拈花ヽ惹草 提交于 2019-12-17 23:15:46
问题 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 = Const Int | Add r r deriving ( Show, Eq, Ord, Functor, Prelude.Foldable ) type Expr = Fix ExprF testExpr = Fix $ Add (Fix (Const 1)) (Fix (Const 2)) convertToString :: Expr -> String convertToString = cata $ \case e@(Const x) -> show x e@(Add x y) ->

Lenses over Comonads or Representable

℡╲_俬逩灬. 提交于 2019-12-08 20:15:28
Here's a more specific variant of this question: Mutate only focus of Store Comonad? , for the benefit of not asking more than one question at once. Are there any lenses compatible with Control.Lens which allow me to interact with the focus of a comonad (the value from extract ) or with the index/value of the Store Comonad ( pos )? It seems like lenses may be of some use here, but I have been unable to find anything which fits; any help would be appreciated, thanks! Comonad doesn't give you any way to write back to the comonad's focus, so you can't write a general Lens for extract . But it is

Lenses over Comonads or Representable

社会主义新天地 提交于 2019-12-08 07:44:11
问题 Here's a more specific variant of this question: Mutate only focus of Store Comonad?, for the benefit of not asking more than one question at once. Are there any lenses compatible with Control.Lens which allow me to interact with the focus of a comonad (the value from extract ) or with the index/value of the Store Comonad ( pos )? It seems like lenses may be of some use here, but I have been unable to find anything which fits; any help would be appreciated, thanks! 回答1: Comonad doesn't give