Search code examples
functionhaskellsyntaxdefinitionfunction-declaration

difference between (a -> a) and a -> a


I've noticed that (although I was once told that (a -> a) and a -> a meant the same thing), I get error messages when I use the (a -> a). Should I only use (a -> a) when using brackets amongst the types? (i.e. (5 + 3) instead of 5 + 3)? Just not quite certain of when it's necessary


Solution

  • (a -> a) and a -> a are the same alone,

    ff :: (a -> a)   -- this compiles
    ff = id
    
    gg :: a -> a
    gg = id
    
    h :: a -> a -> Bool
    h _ _ = True
    
    i = h ff gg   -- this compiles => ff & gg are of the same type.
    

    but will be different when combined with more types like:

     a -> a  -> b
    (a -> a) -> b
    

    This is because -> is right-associative, so a -> a -> b actually means a -> (a -> b) (take an a and return a function), which is different from (a -> a) -> b (take a function and return a b).

    This is like (1+2)*3 is different from 1+2*3.