Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typechecking in function compositions

Tags:

haskell

Why is not ghci throwing a type error when I do the following?

let d = tail . head

Should this not say that tail wants [a] while head gives it only a?

like image 395
user3139545 Avatar asked Feb 12 '23 17:02

user3139545


1 Answers

If the types were more specialized, like

head :: [Int] -> Int
tail :: [Int] -> [Int]  

then tail.head would be ill typed indeed. But as it stands, the types are

head :: [a] -> a
tail :: [b] -> [b]

I have used the type variables "a" and "b" so that you don't think they have to be the same. In that case, having a = [b], the types become

head :: [[b]] -> [b]
tail :: [b] -> [b]

and they are composable.

like image 97
mr- Avatar answered Feb 20 '23 23:02

mr-