comonad

Writing cojoin or cobind for n-dimensional grid type

北城余情 提交于 2019-11-28 14:34:54
问题 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 where Point :: x -> U Z x Dimension :: [U n x] -> U n x -> [U n x] -> U (S n) x dmap :: (U n x -> U m r) -> U (S n) x -> U (S m) r dmap f (Dimension ls mid rs) = Dimension (map f ls) (f mid) (map f rs) instance Functor (U n) where fmap f (Point x) = Point

What is the Comonad typeclass in Haskell?

雨燕双飞 提交于 2019-11-27 05:59:00
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 it is that is provides extract :: w a -> a , sort of a parallel to Monad's return :: a -> m a . Bonus points for noting "real life" uses of Comonad in "real" code. These links may be helpful: Evaluating cellular automata is comonadic . In particular, "whenever you see large datastructures pieced together from lots of small but similar computations

Zipper Comonads, Generically

独自空忆成欢 提交于 2019-11-26 21:21:37
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 a) | Leaf a deriving Functor with the following zipper data Dir = L | R data Step a = Step a Dir (Bin a) deriving Functor data Zip a = Zip [Step a] (Bin a) deriving Functor instance Comonad Zip where ... It is the case that Zip is a Comonad though the construction of its instance is a little hairy. That said, Zip can be completely mechanically

How to extract value from monadic action

萝らか妹 提交于 2019-11-26 20:41:36
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? A monad only supplies two functions: return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b Both of these return something of type m a , so there is no way to combine these in any way to get a function of type Monad m => m a -> a . To do that, you'll need more than these two functions, so you need to know more about m than that it's a monad. For example, the Identity monad has runIdentity :: Identity a -> a , and several monads have

What is the Comonad typeclass in Haskell?

余生颓废 提交于 2019-11-26 12:51:01
问题 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 it is that is provides extract :: w a -> a , sort of a parallel to Monad\'s return :: a -> m a . Bonus points for noting \"real life\" uses of Comonad in \"real\" code. 回答1: These links may be helpful: Evaluating cellular automata is comonadic.

Zipper Comonads, Generically

自闭症网瘾萝莉.ら 提交于 2019-11-26 07:55:08
问题 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 a) | Leaf a deriving Functor with the following zipper data Dir = L | R data Step a = Step a Dir (Bin a) deriving Functor data Zip a = Zip [Step a] (Bin a) deriving Functor instance Comonad Zip where ... It is the case that Zip is a Comonad though

How to extract value from monadic action

眉间皱痕 提交于 2019-11-26 07:40:25
问题 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? 回答1: A monad only supplies two functions: return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b Both of these return something of type m a , so there is no way to combine these in any way to get a function of type Monad m => m a -> a . To do that, you'll need more than these two functions, so you need to know more about m than that it's