Functions as Applicatives

Hi List, I'm struggling to relate the definition of a function as a function instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x) with the following expression ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508 From chapter 11 of LYH http://goo.gl/7kl2TM . I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508." The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f? Regards, - Olumide

Hello Ollumide, this may help: it builds and runs anyway. {- instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x) with the following expression ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508 -} f::Num f => f -> f -> f f = (+) g::Num g => g -> g g = (+ 3) h::Num h => h -> h h = (* 100) fg::Num a => a -> a -> a fg = f <$> g {- fg a b = a + (b + 3) fg a = \b -> a + (b + 3) -} fgh::Num a => a -> a fgh = fg <*> h {- fgh b = fg (b * 100) fgh = \b -> fg (b * 100) -}

fgh::Num a => a -> a fgh = fg <*> h {- fgh a = fg a (a * 100) fgh = \a -> fg a (a * 100) -} , that is

.. actually, I got fg wrong. Caught it by changing g to (/ ): f::Fractional f => f -> f -> f f = (+) g::Fractional g => g -> g g a = a / 2 h::Fractional h => h -> h h = (* 10) fg::Fractional a => a -> a -> a fg = f <$> g {- fg a b = (a / 2) + b fg a = \b -> (a / 2) + b -} fgh::Fractional a => a -> a fgh = fg <*> h {- fgh a = fg a (a * 10) fgh = \a -> fg a (a * 10) -}

In f <*> g = \x -> f x (g x), g is the second argument to <*>. The result
of f <*> g is a function that takes an argument (x) and gives f x (g x). So
basically <*> combines the functions f and g in a particular way to give a
new function. In fact, it is the only way to combine them that type checks
(and doesn't use undefined or similar).
On Mon, Aug 22, 2016 at 11:13 AM Imants Cekusins
.. actually, I got fg wrong. Caught it by changing g to (/ ):
f::Fractional f => f -> f -> f f = (+)
g::Fractional g => g -> g g a = a / 2
h::Fractional h => h -> h h = (* 10)
fg::Fractional a => a -> a -> a fg = f <$> g {- fg a b = (a / 2) + b fg a = \b -> (a / 2) + b -}
fgh::Fractional a => a -> a fgh = fg <*> h {- fgh a = fg a (a * 10) fgh = \a -> fg a (a * 10) -}
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

What exactly is f x (g x)? is it (f x)(g x)?? ... Looks like two numbers to me e.g. 42 43 ... This can only make sense if they are arguments to another binary function which I don't see. Or is there something I'm missing? - Olumide On 22/08/16 22:14, Rein Henrichs wrote:
In f <*> g = \x -> f x (g x), g is the second argument to <*>. The result of f <*> g is a function that takes an argument (x) and gives f x (g x). So basically <*> combines the functions f and g in a particular way to give a new function. In fact, it is the only way to combine them that type checks (and doesn't use undefined or similar).
On Mon, Aug 22, 2016 at 11:13 AM Imants Cekusins
mailto:imantc@gmail.com> wrote: .. actually, I got fg wrong. Caught it by changing g to (/ ):
f::Fractional f => f -> f -> f f = (+)
g::Fractional g => g -> g g a = a / 2
h::Fractional h => h -> h h = (* 10)
fg::Fractional a => a -> a -> a fg = f <$> g {- fg a b = (a / 2) + b fg a = \b -> (a / 2) + b -}
fgh::Fractional a => a -> a fgh = fg <*> h {- fgh a = fg a (a * 10) fgh = \a -> fg a (a * 10) -}
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Yes, (g x) is the second argument to f. Consider the type signature: (<*>) :: Applicative f => f (a -> b) -> f a -> f b In this case, the type of f is ((->) r). Specialized to that type: (<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x) Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>. On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Here's an example that may help:
((==) <*> reverse) "radar" True
Using the definition for (<*>), we have f = (==) and g = reverse, and this
becomes:
(==) "radar" (reverse "radar")
Or, once we make (==) infix,
"radar" == reverse "radar"
So we might say:
isPalindome x = ((==) <*> reverse) x
And we can *eta reduce* that to get
isPalindrome = (==) <*> reverse
On Mon, Aug 22, 2016 at 4:54 PM Theodore Lief Gannon
Yes, (g x) is the second argument to f. Consider the type signature:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
In this case, the type of f is ((->) r). Specialized to that type:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x)
Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b
The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.
On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I must be missing something. I thought f accepts just one argument. - Olumide On 23/08/2016 00:54, Theodore Lief Gannon wrote:
Yes, (g x) is the second argument to f. Consider the type signature:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
In this case, the type of f is ((->) r). Specialized to that type:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x)
Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b
The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.
On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de mailto:50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

All functions in Haskell always take one argument. On 23/08/16 21:28, Olumide wrote:
I must be missing something. I thought f accepts just one argument.
- Olumide
On 23/08/2016 00:54, Theodore Lief Gannon wrote:
Yes, (g x) is the second argument to f. Consider the type signature:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
In this case, the type of f is ((->) r). Specialized to that type:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x)
Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b
The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.
On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de mailto:50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On 23/08/2016 12:39, Tony Morris wrote:
All functions in Haskell always take one argument.
I know that. All functions accept one argument and return a value _or_ another function. Is f the latter type? - Olumide
On 23/08/16 21:28, Olumide wrote:
I must be missing something. I thought f accepts just one argument.
- Olumide
On 23/08/2016 00:54, Theodore Lief Gannon wrote:
Yes, (g x) is the second argument to f. Consider the type signature:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
In this case, the type of f is ((->) r). Specialized to that type:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x)
Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b
The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.
On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de mailto:50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, Aug 23, 2016 at 10:30 PM, Olumide <50295@web.de> wrote:
On 23/08/2016 12:39, Tony Morris wrote:
All functions in Haskell always take one argument.
I know that. All functions accept one argument and return a value _or_ another function. Is f the latter type?
In the ((->) r) case, yes. lee
- Olumide
On 23/08/16 21:28, Olumide wrote:
I must be missing something. I thought f accepts just one argument.
- Olumide
On 23/08/2016 00:54, Theodore Lief Gannon wrote:
Yes, (g x) is the second argument to f. Consider the type signature:
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
In this case, the type of f is ((->) r). Specialized to that type:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b) f <*> g = \x -> f x (g x)
Breaking down the pieces... f :: r -> a -> b g :: r -> a x :: r (g x) :: a (f x (g x)) :: b
The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.
On Mon, Aug 22, 2016 at 9:07 AM, Olumide <50295@web.de mailto:50295@web.de> wrote:
Hi List,
I'm struggling to relate the definition of a function as a function
instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x)
with the following expression
ghci> :t (+) <$> (+3) <*> (*100) (+) <$> (+3) <*> (*100) :: (Num a) => a -> a ghci> (+) <$> (+3) <*> (*100) $ 5 508
From chapter 11 of LYH http://goo.gl/7kl2TM .
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?
Regards,
- Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (6)
-
Imants Cekusins
-
Lee Duhem
-
Olumide
-
Rein Henrichs
-
Theodore Lief Gannon
-
Tony Morris