category-theory

Are all fixed size containers strong monoidal functors, and/or vice versa?

北城余情 提交于 2020-03-21 11:38:05
问题 The Applicative typeclass represents lax monoidal functors that preserve the cartesian monoidal structure on the category of typed functions. In other words, given the canonical isomorphisms witnessing that (,) forms a monoidal structure: -- Implementations left to the motivated reader assoc_fwd :: ((a, b), c) -> (a, (b, c)) assoc_bwd :: (a, (b, c)) -> ((a, b), c) lunit_fwd :: ((), a) -> a lunit_bwd :: a -> ((), a) runit_fwd :: (a, ()) -> a runit_bwd :: a -> (a, ()) The typeclass and its laws

What is the main difference between Free Monoid and Monoid?

一个人想着一个人 提交于 2020-03-18 11:10:37
问题 Looks like I have a pretty clear understanding what a Monoid is in Haskell, but last time I heard about something called a free monoid. What is a free monoid and how does it relate to a monoid? Can you provide an example in Haskell? 回答1: In a programming context, I usually translate free monoid to [a] . In his excellent series of articles about category theory for programmers, Bartosz Milewski describes free monoids in Haskell as the list monoid (assuming one ignores some problems with

What is the general case of QuickCheck's promote function?

Deadly 提交于 2020-01-11 04:50:06
问题 What is the general term for a functor with a structure resembling QuickCheck's promote function, i.e., a function of the form: promote :: (a -> f b) -> f (a -> b) (this is the inverse of flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b) ). Are there even any functors with such an operation, other than (->) r and Id ? (I'm sure there must be). Googling 'quickcheck promote' only turned up the QuickCheck documentation, which doesn't give promote in any more general context AFAICS; searching SO

Once I have an F-Algebra, can I define Foldable and Traversable in terms of it?

爱⌒轻易说出口 提交于 2020-01-10 14:08:57
问题 I have defined an F-Algebra , as per Bartosz Milewski's articles (one, two): (This is not to say my code is an exact embodiment of Bartosz's ideas, it's merely my limited understanding of them, and any faults are mine alone.) module Algebra where data Expr a = Branch [a] | Leaf Int instance Functor Expr where fmap f (Branch xs) = Branch (fmap f xs) fmap _ (Leaf i ) = Leaf i newtype Fix a = Fix { unFix :: a (Fix a) } branch = Fix . Branch leaf = Fix . Leaf -- | This is an example algebra.

Reader monad - how does it conform to Monad interface?

元气小坏坏 提交于 2020-01-03 02:10:14
问题 I'm learning category theory. I understand the concept of reader monad, it's even pretty easy to implement: case class Reader[DEP, A](g: DEP => A) { def apply(dep: DEP): A = g(dep) def map[B](f: A => B): Reader[DEP, B] = Reader(dep => f(apply(dep))) def flatMap[B](f: A => Reader[DEP, B]): Reader[DEP, B] = Reader(dep => f(apply(dep)) apply dep) } However, I have problems implementing it with constraint to some generic Monad interface, i.e trait Monad[A] { def pure(a: A): Monad[A] def map[B](f:

Inverse of the absurd function

China☆狼群 提交于 2020-01-02 03:17:09
问题 Is there an inverse to the absurd function from Data.Void ? If it exists, how is it implemented and what is it used for? 回答1: This function does not exist. (assuming strict semantics) Looking at the algebra of types, the function type is equivalent to exponentiation. Now the function absurd , which has the type Void -> a corresponds to the operation a ^ 0 which equals 1 . This means that there is exactly one implementation of absurd , which can be found in Data.Void . Reversing the arrow, you

Is there any intuition to understand join two functions in Monad?

前提是你 提交于 2020-01-01 04:34:07
问题 join is defined along with bind to flatten the combined data structure into single structure. From type system view, (+) 7 :: Num a => a -> a could be considered as a Functor , (+) :: Num a => a -> a -> a could be considered as a Functor of Functor , how to get some intuition about it instead of just relying on type system? Why join (+) 7 === 14 ? Even though it is possible to get the final result through manually stepping by the function binding process, it would be great if some intuition

What's the relationship between profunctors and arrows?

戏子无情 提交于 2019-12-29 19:58:50
问题 Apparently, every Arrow is a Strong profunctor. Indeed ^>> and >>^ correspond to lmap and rmap . And first' and second' are just the same as first and second . Similarly every ArrowChoice is also Choice. What profunctors lack compared to arrows is the ability to compose them. If we add composition, will we get an arrow? In other words, if a (strong) profunctor is also a category, is it already an arrow? If not, what's missing? 回答1: What profunctors lack compared to arrows is the ability to

Can a monad be a comonad?

天大地大妈咪最大 提交于 2019-12-29 11:49:09
问题 I know what a monad is. I think I have correctly wrapped my mind around what a comonad is. (Or rather, what one is seems simple enough; the tricky part is comprehending what's useful about this...) My question is: Can something be a monad and a comonad? I foresee two possible answers: Yes, this is common and widely useful. No, they do such different jobs that there would be no reason to want something to be both. So, which is it? 回答1: Yes. Turning some comments into an answer: newtype

Is there a monad that doesn't have a corresponding monad transformer (except IO)?

喜你入骨 提交于 2019-12-28 03:31:09
问题 So far, every monad (that can be represented as a data type) that I have encountered had a corresponding monad transformer, or could have one. Is there such a monad that can't have one? Or do all monads have a corresponding transformer? By a transformer t corresponding to monad m I mean that t Identity is isomorphic to m . And of course that it satisfies the monad transformer laws and that t n is a monad for any monad n . I'd like to see either a proof (ideally a constructive one) that every