How does fmap fmap apply to functions (as arguments)?

I am trying to figure out how to fmap fmapapply to a function like say (*3).

Type fmap fmap:

(fmap fmap):: (Functor f1, Functor f) => f (a -> b) -> f (f1 a -> f1 b)

Type (*3):

(*3) :: Num a => a -> a

That means the signature a -> amatches f (a -> b), right?

Prelude> :t (fmap fmap (*3))
(fmap fmap (*3)):: (Num (a -> b), Functor f) => (a -> b) -> f a -> f b

I tried to create a simple test:

test :: (Functor f) => f (a -> b) -> Bool 
test f = True

And serving (*3)in it, but I get:

*Main> :t (test (*3))

<interactive>:1:8:
    No instance for (Num (a0 -> b0)) arising from a use of β€˜*’
    In the first argument of β€˜test’, namely β€˜(* 3)’
    In the expression: (test (* 3))

Why is this happening?

+4
source share
1 answer

, , . fmap (*) , (, , ) . :

, * + Haskell?

, , , . fmap:

fmap :: Functor f => (a -> b) -> f a -> f b
                     |______|    |________|
                         |            |
                      domain      codomain

fmap . a b , , (, , , ..).

"domain" "codomain" "" "" . , , , fmap fmap:

fmap :: Functor f => (a -> b) -> f a -> f b
                     |______|
                         |
                       fmap :: Functor g => (x -> y) -> g x -> g y
                                            |______|    |________|
                                                |            |
                                                a    ->      b

, a := x -> y b := g x -> g y. , Functor g. fmap fmap:

fmap fmap :: (Functor f, Functor g) => f (x -> y) -> f (g x -> g y)

, fmap fmap? fmap fmap f. , f Maybe. , :

fmap fmap :: Functor g => Maybe (x -> y) -> Maybe (g x -> g y)

, fmap fmap Maybe . fmap fmap, , Maybe g. , g []. , :

fmap fmap :: Maybe (x -> y) -> Maybe ([x] -> [y])

fmap fmap Nothing, Nothing. , Just (+1), , , Just (.. Just (fmap (+1))).

fmap fmap . , f ( , f ) f g.

. ? , fmap fmap (*3). , . , . (*3):

(*3) :: Num a => a -> a

fmap fmap (*3), f (->) r (.. ). . fmap (->) r - . , fmap fmap:

fmap fmap :: Functor g => (r -> x -> y) -> r -> g x -> g y

-- or

(.)  fmap :: Functor g => (r -> x -> y) -> r -> g x -> g y
                          |___________|
                                |
                              (*3) :: Num a => a ->    a
                                               |       |
                                               |    ------
                                               |    |    |
                                               r -> x -> y

, ?

  • , , (r -> x -> y) , (*3) :: Num a => a -> a.
  • , (*3) . , , - . , , Num (x -> y), , - .

, r := a := x -> y. , :

fmap . (*3) :: (Num (x -> y), Functor g) => (x -> y) -> g x -> g y

, :

  fmap . (*3)
= \x -> fmap (x * 3)
             |_____|
                |
                +--> You are trying to lift a number into the context of a functor!

, fmap fmap (*), :

(.) fmap :: Functor g => (r -> x -> y) -> r -> g x -> g y
                         |___________|
                               |
                              (*) :: Num a => a -> a -> a
                                              |    |    |
                                              r -> x -> y

, r := x := y := a. :

fmap . (*) :: (Num a, Functor g) => a -> g a -> g a

, :

  fmap . (*)
= \x -> fmap (x *)

, fmap fmap (*) 3 fmap (3*).

, test:

test :: Functor f => f (a -> b) -> Bool

f - (->) r :

test :: (r -> a -> b) -> Bool
        |___________|
              |
            (*3) :: Num x => x ->    x
                             |       |
                             |    ------
                             |    |    |
                             r -> a -> b

, r := x := a -> b. , :

test (*3) :: Num (a -> b) => Bool

a, b, Num (a -> b) . a b, , Num (a -> b). , , , Num (a -> b) ; Num (a -> b) - , , .

test (*), , .

+3

All Articles