[Haskell-beginners] Identical function and variable names and type inference

Antoine Latter aslatter at gmail.com
Wed Sep 2 17:30:55 EDT 2009


On Wed, Sep 2, 2009 at 4:24 PM, aditya siram<aditya.siram at gmail.com> wrote:
> Hi all,
> Recently I wrote a function that takes a unique identifier that I called
> 'id'. I then tried to apply the 'id' function to it and GHC did not like
> that. But it should.
>
> For example in 'test' I have told the compiler that the id argument is an
> Int. So type inference should be able to determine the first 'id' in 'id id'
> couldn't possibly be an Int, but it complains. So I explicitly told the
> compiler the type of 'id' in test1 - this didn't work either. The final
> function 'test3' works as expected. Is there something I am not understand
> about the way type inference is supposed to work?
>
> test :: Int -> Int
> test id = id id
>

Type inference plays no role in figuring out what your identifiers
refer to. In the body of your function, the only identifier named "id"
that's in scope is the argument to the function.

There's a compiler warning for GHC you can use to be warned we you
introduce identifiers which "shadow" existing identifiers, like in
your function.

Does my explanation make sense?

Antoine


More information about the Beginners mailing list