category-theory

What's the relation of fold on Option, Either etc and fold on Traversable?

会有一股神秘感。 提交于 2019-11-29 14:05:49
问题 Scalaz provides a method named fold for various ADTs such as Boolean , Option[_] , Validation[_, _] , Either[_, _] etc. This method basically takes functions corresponding to all possible cases for that given ADT. In other words, a pattern match shown below: x match { case Case1(a, b, c) => f(a, b, c) case Case2(a, b) => g(a, b) . . case CaseN => z } is equivalent to: x.fold(f, g, ..., z) Some examples: scala> (9 == 8).fold("foo", "bar") res0: java.lang.String = bar scala> 5.some.fold(2 *, 2)

Why is `pure` only required for Applicative and not already for Functor? [duplicate]

帅比萌擦擦* 提交于 2019-11-29 02:54:21
问题 This question already has answers here : Why Functor class has no return function? (4 answers) Closed 4 months ago . Reading this Wikibook about Haskell and Category Theory basics, I learn about Functors: A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D maps any object A in C to F(A), in D. maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D. ... which sounds all nice. Later an example is provided: Let's have a sample

Are there contravariant monads?

◇◆丶佛笑我妖孽 提交于 2019-11-29 01:13:58
Functors can be covariant and contravariant. Can this covariant/contravariant duality also be applied to monads? Something like: class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b class ContraMonad m where return :: a -> m a contrabind :: m a -> (b -> m a) -> m b Does ContraMonad class make sense? Any examples? Well, of course, it's possible to define it, but I doubt it would be of any use. There is a popular saying that "monad is just a monoid in a category of endofunctors". What it means is, first of all, that we have a category of endofunctors (meaning, (covariant)

Foldable, Monoid and Monad

此生再无相见时 提交于 2019-11-28 20:05:06
问题 Consider the following signature of foldMap foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m This is very similar to "bind", just with the arguments swapped: (>>=) :: Monad m => m a -> (a -> m b) -> m b It seems to me that there therefore must be some sort of relationship between Foldable , Monoid and Monad , but I can't find it in the superclasses. Presumably I can transform one or two of these into the other but I'm not sure how. Could that relationship be detailed? 回答1: Monoid and

What is Applicative Functor definition from the category theory POV?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 17:17:02
I was able to map Functor's definition from category theory to Haskell's definition in the following way: since objects of Hask are types, the functor F maps every type a of Hask to the new type F a by, roughly saying, prepending "F " to it. maps every morphism a -> b of Hask to the new morphism F a -> F b using fmap :: (a -> b) -> (f a -> f b) . So far, so good. Now I get to the Applicative , and can't find any mention of such a concept in textbooks. By looking at what it adds to Functor , ap :: f (a -> b) -> f a -> f b , I tried to come up with my own definition. First, I noticed that since

Where do values fit in Category of Hask?

守給你的承諾、 提交于 2019-11-28 17:16:54
So we have Category of Hask, where: Types are the objects of the category Functions are the morphisms from object to object in the category. Similarly for Functor we have: a Type constructor as the mapping of objects from one category to another fmap for the mapping of morphisms from one category to another. Now, when we write program we basically transform values (not types) and it seems that the Category of Hask doesn't talk about values at all. I tried to fit values in the whole equation and came up with the following observation: Each Type is a category itself. Ex: Int is a category of all

Difference between free monads and fixpoints of functors?

我只是一个虾纸丫 提交于 2019-11-28 15:49:42
问题 I was reading http://www.haskellforall.com/2013/06/from-zero-to-cooperative-threads-in-33.html where an abstract syntax tree is derived as the free monad of a functor representing a set of instructions. I noticed that the free monad Free is not much different from the fixpoint operator on functors Fix. The article uses the monad operations and do syntax to build those ASTs (fixpoints) in a concise way. I'm wondering if that's the only benefit from the free monad instance? Are there any other

What does “coalgebra” mean in the context of programming?

筅森魡賤 提交于 2019-11-28 14:53:51
I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these structures which is pretty much incomprehensible to me. Can anyone please explain what coalgebras mean in the context of programming, what is their significance, and how they relate to objects and comonads? Algebras I think the place to start would be to understand the idea of an algebra . This is just a generalization of algebraic structures like groups

How much is applicative really about applying, rather than “combining”?

淺唱寂寞╮ 提交于 2019-11-28 12:02:16
For an uncertainty-propagating Approximate type , I'd like to have instances for Functor through Monad . This however doesn't work because I need a vector space structure on the contained types, so it must actually be restricted versions of the classes. As there still doesn't seem to be a standard library for those (or is there? please point me. There's rmonad , but it uses * rather than Constraint as the context kind, which seems just outdated to me), I wrote my own version for the time being. It all works easy for Functor class CFunctor f where type CFunctorCtxt f a :: Constraint cfmap ::

To what extent are Applicative/Monad instances uniquely determined?

浪尽此生 提交于 2019-11-28 05:21:54
As described this question/answers , Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList . So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension? ). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at least two Applicative instances? Are there any such examples that only involve finite data structures