Questions tagged [applicative]

In Haskell, Applicative functors are functors such that two functorial values can be combined into one, whilst the two values inside are combined via a functional application. An applicative functor has more structure than a functor but less than a monad.

2
votes
2answers
99 views

What is the idiomatic way to write this function (which might normally be called filterA)?

I am going through this course. There is a section for Applicative and I am being asked to implement a function with the following behaviour and type -- | Filter a list with a predicate that ...
0
votes
1answer
41 views

Cats ValidatedNec mapN with Case Class Error Types

I'd like to use the Cats ValidatedNec data type similarly to the example in the Cats documentation for Validated in the section Meeting applicative-- in my case, I'm parsing Strings from a file, ...
0
votes
3answers
103 views

Understanding Functions as Applicatives in Haskell

I've recently been trying to learn Haskell with the "Learn You a Haskell" and have been really struggling with understanding functions as Applicatives. I should point out that using other types of ...
7
votes
2answers
194 views

What can the Reader monad do that applicative functions cannot?

Having read http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors , I can provide an example of the use of functions as applicative functors: Let's say res is a ...
1
vote
1answer
67 views

Cartesian product not working for applicative

I was trying to understand applicative and how i can use it as a cartesian product between K functions and N parameters ,and i can't comprehend why i can't do the following: [Just (+1),Just (+2)] &...
1
vote
1answer
41 views

Scala, cats - how to do not use Applicative[F] explicity?

I would like to use Applicative[F] in some other way then explicity. Currently I have a simple code: class BettingServiceMock[F[_] : Async] extends BettingService[F] { override def put(bet: ...
3
votes
4answers
136 views

Applicative typeclass based on two different functors

Is there something similar to the Applicative type class, but where there are two functors for each side of the application which are different? i.e. (<*>) :: (Functor f, Functor g) => f (a -...
4
votes
1answer
92 views

Cats: mapping over tuples with the same applicative

Let's say I have: val x1: Either[String, Int] = Right(1) val x2: Either[String, Float] = Left("Nope") val x3: Either[String, Double] = Left("Not Today") I want to combine these together and get a ...
7
votes
2answers
102 views

What does Traversable is to Applicative contexts mean?

I am trying to understand Traversable with the help of https://namc.in/2018-02-05-foldables-traversals. Somewhere the author mentioned the following sentence: Traversable is to Applicative ...
11
votes
1answer
191 views

Does * in (<*>) have a special meaning?

Trying to expand my understanding about symbols in Haskell : ($) : Function Application operator (Allow you to apply arguments over a function) (&) : flipped version of Function Application ...
0
votes
1answer
97 views

Why does applicative work default only for Maybe

I am trying to understand why do applicative functors work by default (no implementation needed) for some functors like Maybe but for others don't: Example: Just (+3) <*> (Just 3) works fine "...
8
votes
2answers
129 views

Resolving the type of `f = f (<*>) pure`

Recently I noticed that humourously liftA can be written as liftA (<*>) pure I thought this was neat and so as a bit of a joke I thought I would make a new "definition" of liftA based on this ...
15
votes
3answers
733 views

Is `x >> pure y` equivalent to `liftM (const y) x`

The two expressions y >> pure x liftM (const x) y have the same type signature in Haskell. I was curious whether they were equivalent, but I could neither produce a proof of the fact nor a ...
1
vote
2answers
86 views

How can I use functors or applicatives to rewrite this Haskell function over lists of tuples

Is there a nicer way to write the following function fs' with functors or applicatives? fncnB = (* 2) fncnA = (* 3) fs' fs = zip (map (fncnA . fst) fs) $ map (fncnB . snd) fs I see from this ...
24
votes
3answers
669 views

Are free monads also zippily applicative?

I think I've come up with an interesting "zippy" Applicative instance for Free. data FreeMonad f a = Free (f (FreeMonad f a)) | Return a instance Functor f => Functor (...
1
vote
2answers
56 views

How to compose functions with applicative effects for Validation in the Cats in Scala

Here is an example from the Scala with Cats book: object Ex { import cats.data.Validated type FormData = Map[String, String] type FailFast[A] = Either[List[String], A] def getValue(name: ...
3
votes
1answer
96 views

MaybeT applicative instance without monad constraint for the outer monad

I am implementing the monad transformer of the Maybe (aka Option) type in Javascript (please note that I use type-dictionary passing): const optOfT = of => x => of(optOf(x)); const optMapT = ...
5
votes
1answer
147 views

Making instance of Applicative

Still not a hundred percent shure how to make instances of the more complex types. Have this: data CouldBe a = Is a | Lost deriving (Show, Ord) Made an instance of Functor, using Maybe as an ...
1
vote
2answers
69 views

Haskell do (syntactically) inline monad unpacking with left arrow

I can't find where I saw this but I found somewhere on a reddit discussion the following syntax in a do block: do case (<- fooM) of Foo x -> .. ... instead of the usual do foo <...
3
votes
4answers
169 views

Applicative functor evaluation is not clear to me

I am currently reading Learn You a Haskell for Great Good! and am stumbling on the explanation for the evaluation of a certain code block. I've read the explanations several times and am starting to ...
1
vote
1answer
76 views

Understanding how haskell applicative functors are applied

I just have a quick question about applicative functors to help me get a grasp on them. This is just stuff I am applying in ghci. [(+3),((-) 3),(*3)] <*> [4] [7,-1,12] This makes sense to me. ...
3
votes
1answer
114 views

Haskell example where pure and return not interchangeable

Is Haskell's pure function the same as return? I just can make a type an instance of Monad if it is already an instance of Applicative, right? So I wonder that Applicative's pure is everytime ...
3
votes
1answer
69 views

How to implement a coroutine for applicative computations?

Here is a coroutine that avoids nested patterns like (chain(m) (chain(...)) for monadic computations: const some = x => none => some => some(x); const none = none => some => none; ...
3
votes
1answer
83 views

Understand the Applicative problem (#7) in Chapter 12 of “Programming In Haskell”

I've been working through the excellent Programming in Haskell (2nd ed). I'm a bit flummoxed by the question on Applicatives, though. Given the following type: data Expr a = Var a | Val Int | Add (...
0
votes
2answers
90 views

Generalization of the `Functor` class to become a `MultiFunctor`?

I am learning from the "Free Applicative Functors". Surely, the question I am going to ask is kind of aside with respect to main idea of the paper, but still... ...on the page 6 there is an attempt ...
0
votes
0answers
75 views

Is there a better way to add values of two custom data types where both are instances of MonoFunctor?

I have some data defined as {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} module Test where import Control.Lens import Data....
0
votes
1answer
42 views

How do I add an Applicative context to a type expected by Lens' MyDataType?

I have a function generalized over a function func to be applied on MyDataType1 and another MyDataType1 like this setThis :: (Functor f0, Applicative f0) => Lens' MyDataType1 (f0 Float) -> ...
1
vote
1answer
101 views

Validation of multiple ADTs with Scala Cats Validated

I am trying to validate a config in scala. First I convert the config json to the respective case class and then I validate it. As I want to fail slow I collect all the validations that fail rather ...
7
votes
1answer
134 views

Why is this applicative instance unlawful?

I was reading about monad transformers and found this apparently well known article - A Gentle Introduction to Monad Transformers. What caught my attention was the part where the author describes an ...
6
votes
2answers
116 views

Every monad is an applicative functor — generalizing to other categories

I can readily enough define general Functor and Monad classes in Haskell: class (Category s, Category t) => Functor s t f where map :: s a b -> t (f a) (f b) class Functor s s m => ...
6
votes
4answers
177 views

`(<*>)` definition for the Applicative functor?

Some Haskell source code (see ref): -- | Sequential application. -- -- A few functors support an implementation of '<*>' that is more -- efficient than the default one. (<*>) :: f (a ->...
1
vote
1answer
45 views

Implementing sequenceAL for lists

Typeclassopedia (Haskell Wiki) has this exercise in Applicative's section: Implement a function sequenceAL :: Applicative f => [f a] -> f [a] I could only do it, if used the do notation (...
1
vote
1answer
99 views

ApplicativeDo pragma and Applicative Functor in Haskell

An example from Functor, Applicative, and Monad slightly changed: {-# LANGUAGE ApplicativeDo #-} import Safe (readMay) -- import Control.Applicative ((<$>), (<*>)) displayAge maybeAge =...
2
votes
3answers
131 views

Nested applicative functors of different types in Haskell

I'd like to make the nested applicative functors of different types. For example, nested simple functors of different types (in ghci) work fine: Prelude> ((+2) <$>) <$> (Just [1..4]) ...
0
votes
1answer
85 views

Haskell: Is it possible to define a datatype that can encapsulate Maybe, String & Double with ability to apply value applicatively?

I'm a Haskell beginner and while building a simple project I came across this odd problem. I have a list of functions e.g. [f1, f2, f3 ... fn] Which doesn't work because they don't have same type. ...
0
votes
1answer
106 views

Haskell: Parsing an object that could be multiple types into one single type

I'm a haskell beginner going through aeson, learning more about both by parsing some data files. Usually when there's a data file, may it be .json, a lua table, .csv format or others, and you want to ...
1
vote
0answers
49 views

Haskell: Will Kurt exercise - getting more info from dictionary

Exercise 28.3 "get programming with haskell" from Will Kurt. Idea is to use the cheapest robot part using applicative functors. Now I wrote the code below, which does exactly that, but I don't want to ...
4
votes
2answers
158 views

Trying to implement “the essence of the iterator pattern”

I came across the paper "https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf" which has code examples in quite an abstract pseudo haskell syntax. I'm struggeling to implement the example ...
1
vote
2answers
90 views

Instance applicative on datatype `List`

The Haskell book Haskell Programming from First Principles has an exercise which asks me to instance Applicative on the datatype List: data List a = Nil | Cons a (List a) deriving (Eq, Show) ...
2
votes
2answers
63 views

Why the `pure` of `Applicative Maybe` be defined as `pure = Just` and ignores `Nothing`?

In the source code of GHC.Base, Applicative Maybe is defined as: instance Applicative Maybe where pure = Just ... I am wondering why the pure's definition ignores Nothing? According to this ...
2
votes
2answers
144 views

How to define the `pure` function on `Applicative`?

The book Haskell Programming from First Principles has an exercise which asks me to instance Applicative on Constant a. I tried this: newtype Constant a b = Constant { getConstant :: a } ...
3
votes
1answer
77 views

Question about applicative and nested Maybe

I wrote this function: appFunc :: Integer -> Integer -> Bool -> Maybe (Integer,Integer) appFunc i1 i2 b = if b then Just (i1,i2) else Nothing And then I use it as such in GHCi: > ...
5
votes
0answers
114 views

A categorical view on applicative containers

This answer from Conor McBride (pigworker) discusses Applicative functors that are also containers (data types given by a set of shapes and a family of positions). Among other things, he mentions that:...
7
votes
2answers
106 views

Why doesn't this simple composition work?

I was recently in need of putting head in between two monadic operations. Here's the SSCCE: module Main where f :: IO [Int] f = return [1..5] g :: Int -> IO () g = print main = do putStrLn "...
3
votes
3answers
94 views

Implementing applicative functor for custom data type with two identical type classes

I'm trying to make Twice a member of applicative, but I am getting "Conflicting definitions for `a'" in the functor implementation: Also I'm not sure how to implement the <*> correctly :/ data ...
4
votes
1answer
352 views

Scala cats and traverse syntax for Either - doesn't compile

I am trying to use traverse (or sequence which is pretty much the same for my task) from cats library https://typelevel.org/cats/typeclasses/traverse.html . I want to traverse a List[A] with function ...
16
votes
1answer
235 views

What does “lax” mean in “lax monoidal functor”?

I know that the Applicative class is described in category theory as a "lax monoidal functor" but I've never heard the term "lax" before, and the nlab page on lax functor a bunch of stuff I don't ...
1
vote
0answers
143 views

Why does the Functor class in Haskell not include a function on objects? Is `pure` that function? [duplicate]

In Category theory, it is very conspicuous that a definition of a functor should include two functions: on objects and on arrows. However, the usual Haskell Prelude.Functor does not make any mention ...
5
votes
3answers
129 views

How does <*> derived from pure and (>>=)?

class Applicative f => Monad f where return :: a -> f a (>>=) :: f a -> (a -> f b) -> f b (<*>) can be derived from pure and (>>=): fs <*> as = ...
1
vote
3answers
76 views

What does <*> do in addRecip x y = fmap (+) (recipMay x) <*> recipMay y?

addRecip :: Double -> Double -> Maybe Double addRecip x y = fmap (+) (recipMay x) <*> recipMay y where recipMay a | a == 0 = Nothing | otherwise = Just (1 / a) I look ...