How to calculate types in Haskell

Let's say

flip :: (a->b->c) ->b->a->c const ::d->e->d 

type (flip const) will be

  a=d,b=e,c=d 

in

  b->a->c 

so the type will be

  e->d->d 

But for (map take) it

  [Int]->[[a]]->[[a]] 

therefore, I did not understand how ghci is calculated. I understood [[a]] → [[a]], but why and how [Int]?

edit: For example, if we write in ghci

  :t flip const it would return b->c->c 

and ghci would figure it out, like me.

But

  map :: (a->b)->[a]->[b] take :: Int->[c]->[c] 

so why is it taken

  [Int]->[[a]->[a]] 

why [int] how ghci calculated that

+6
source share
2 answers

You should copy and paste the types you see, and not re-enter them into the question. The reason is that you were mistaken. Type for map take :

 map take :: [Int] -> [[a] -> [a]] 

In other words, unification works as such:

 :t map map :: (a -> b) -> [a] -> [b] :t take take :: Int -> [c] -> [c] 

therefore, applying take as the first argument to map , you get a ~ Int and b ~ [c] -> [c] (note that this is a function). Performing these replacements in the map type and applying the first argument:

 map take :: [a] -> [b] (for some specific 'a' and 'b') -- recall a ~ Int map take :: [Int] -> [b] (for some specific 'b') -- recall b ~ [c] -> [c] map take :: [Int] -> [[c] -> [c]] 

Yay, map take is exactly what you expect. A function that works on Ints lists and leads to a list of functions that will take a number of elements from the very beginning of the list.

+12
source

Do the same analysis:

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

AND

 take :: Int -> [x] -> [x] 

But it really means

 take :: Int -> ([x] -> [x]) 

So, with a=Int and b=([x] -> [x]) you get

 map take :: [Int] -> [ [x] -> [x] ] 

List function list!

+14
source

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