applicative

Is it possible to encode a generic “lift” function in Haskell?

百般思念 提交于 2019-11-28 20:47:21
I'm not the biggest fan of varargs, but I always thought both the applicative ( f <$> x <*> y ) and idiom ( [i| f x y |] ) styles have too many symbols. I usually prefer going the liftA2 f x y way, but I, too, think that A2 is a little ugly. From this question , I've learned it is possible to implement vararg functions in Haskell. This way, is it possible to use the same principle in order implement a lift function, such that: lift f a b == pure f <*> a <*> b I've tried replacing the + by <*> on the quoted code: class Lift r where lift :: a -> r instance Lift a where lift = id instance (Lift r

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

What are the benefits of applicative parsing over monadic parsing?

老子叫甜甜 提交于 2019-11-28 15:30:59
There seems to be a consensus that you should use Parsec as an applicative rather than a monad. What are the benefits of applicative parsing over monadic parsing? style performance abstraction Is monadic parsing out? The main difference between monadic and applicative parsing is in how sequential composition is handled. In the case of an applicative parser, we use (<*>) , whereas with a monad we use (>>=) . (<*>) :: Parser (a -> b) -> Parser a -> Parser b (>>=) :: Parser a -> (a -> Parser b) -> Parser b The monadic approach is more flexible, because it allows the grammar of the second part to

What are practical uses of applicative style?

旧街凉风 提交于 2019-11-28 15:03:33
问题 I am a Scala programmer, learning Haskell now. It's easy to find practical use cases and real world examples for OO concepts, such as decorators, strategy pattern etc. Books and interwebs are filled with it. I came to the realization that this somehow is not the case for functional concepts. Case in point: applicatives . I am struggling to find practical use cases for applicatives. Almost all of the tutorials and books I have come across so far provide the examples of [] and Maybe . I

instance Alternative ZipList in Haskell?

▼魔方 西西 提交于 2019-11-28 12:08:02
ZipList comes with a Functor and an Applicative instance ( Control.Applicative ) but why not Alternative? Is there no good instance? What about the one proposed below? Is it flawed? is it useless? Are there other reasonable possibilities (like Bool can be a monoid in two ways) and therefore neither should be the instance I searched for "instance Alternative ZipList" (with the quotes to find code first) and only found the library, some tutorials, lecture notes yet no actual instance. Matt Fenwick said ZipList A will only be a monoid if A is. See here . Lists are monoids though, regardless of

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

functions as applicative functors (Haskell / LYAH)

℡╲_俬逩灬. 提交于 2019-11-28 04:07:54
Chapter 11 of Learn You a Haskell introduces the following definition: instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x) Here, the author engages in some uncharacteristic hand-waving ("The instance implementation for <*> is a bit cryptic, so it's best if we just [show it in action without explaining it]"). I'm hoping someone here might help me figure it out. According to the applicative class definition, (<*>) :: f (a -> b) -> f a -> f b In the instance, substituting ((->)r) for f : r->(a->b)->(r->a)->(r->b) So the first question, is how do I get from that type

Why does importing Control.Applicative allow this bad code to type check?

China☆狼群 提交于 2019-11-28 03:20:46
问题 I'm helping a friend learn Haskell and he recently created code like this, which type checks and produces a CPU-burning loop at runtime. I'm completely baffled by this. import Control.Monad import Control.Applicative main = forever putStrLn "Hello, infinity" That shouldn't type check, but does. The correct version would clearly be: main = forever $ putStrLn "Hello, infinity" What's weird and surprising to me is that you get different results with and without importing Control.Applicative.

How to interpret bind/>>= of the function instance?

拟墨画扇 提交于 2019-11-28 01:56:40
I'm trying to improve my understanding of Applicative s and Monad s by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all. Here are my implementations of fmap , <*> and >>= for the Functor , Applicative and Monad typeclasses in Javascript: const fmap = f => g => x => f(g(x)); // B combinator const apply = f => g => x => f(x) (g(x)); // S combinator const bind = f => g => x => g(f(x)) (x); // ? I am not sure whether bind is the correct translation of the Haskell implementation: (>>=) :: (r -> a) -> (a -> (r -> b