a & # 8594; [b] & # 8594; a" definition I am trying to figure out the definitions of Haskell functi...">

How to read "Num (a & # 8594; b & # 8594; a) => a & # 8594; [b] & # 8594; a" definition

I am trying to figure out the definitions of Haskell functions. I took the foldl function in which this definition was

:t foldl foldl :: (a -> b -> a) -> a -> [b] -> a 

Now the first argument is a function, so if I pass it, here is what remains:

 :t foldl (+) foldl (+) :: Num b => b -> [b] -> b 

which I read as "a function that takes a numerical value and returns a function that takes an array."

Now, is the first argument really a function? Can I read it like

 foldl :: a -> b -> a -> a -> [b] -> a 

instead

I tried to pass only one numerical value, and here is what I got:

 foldl 4 :: Num (a -> b -> a) => a -> [b] -> a 

And the question is ... How do you read this? I don’t understand what I have.

+6
source share
2 answers

The first argument is a function and therefore cannot be read as: foldl :: a -> b -> a -> a -> [b] -> a . Since -> is the correct associative, which means a -> b -> c is a -> (b -> c) and differs from (a -> b) -> c

+8
source

"Num" got involved in this because you went from (+) to foldl . So you created a function with a signature

 foldl (+) :: Num b => b -> [b] -> b 

This is a function that takes a numeric number and a list of Numerics and returns a numeric value.

 Num b => b 

doesn't mean "A function that returns a numeric is what it will mean in Scala. Num b means that all instances of b in the rest of this signature are numbers. annotation is done in Haskell.

 foldl :: (a -> b -> a) -> a -> [b] -> a 

Indicates a function that takes 3 arguments (one of which is a function) and returns a value of the same type as the second argument. Signature launch ...

  • The first argument is a function that itself takes two arguments and returns a value of the same type as the first argument. It is shown in brackets that this is just one argument and the function - the thing in brackets is its function signature. Let me call this function func
  • The second argument can be anything if it is the same type as the first argument accepted by func .
  • The third argument must be a sequence of the same type as the second argument accepted by func
  • The result must be of the same type as the second argument (and the first argument accepted by func ).

These are the only expectations that are made up of arguments. We see a and b in the signature because we can have different types as the first and second arguments to func , but a and b can be of any type, including both types of the same type. But when you pass the foldl function, creating a new function that applies func to the rest of the arguments, you add expectations like any func . Since you passed a numerical function, this annotation is added to the resulting signature. And since + expects both arguments to be numerals, we will see only one type variable in the signature (only b , not a and b ).

+1
source

Source: https://habr.com/ru/post/955398/


All Articles