[Haskell-beginners] Identical function and variable names and
type inference
Magnus Therning
magnus at therning.org
Thu Sep 3 02:35:56 EDT 2009
On Wed, Sep 2, 2009 at 11:33 PM, Thomas Davie<tom.davie at gmail.com> wrote:
> Ints can't make up the function part of an application, that must have type
> (a -> b).
Well, I don't think that's strictly true. foo::Int can be argued to be
a function that takes no arguments and return an Int. It is however
very convenient to call such a function a "constant". However, that
such a function actully is constant is far from guaranteed in most
programming languages. It happens to be true in Haskell though, due
to the strict separation between (proper) functions and "procedures"
(functions with side effects, i.e. stuff in the IO monad).
> In the mean time, the reason it didn't accept test id = id id is because it
> must fix the argument id to only one type. It infers that id must have type
> (a -> b), from the fact that it appears in the function position, then sees
> it in the argument position, and infers that a = (a -> b) which obviously
> causes a problem. To resolve that problem, you need rank-2 polymorphism.
Is that really correct? I suspect the only thing that causes the OP
problems is scoping. If I understand you correctly I wouldn't be able
to do the following:
Prelude> let f x = x
Prelude> :t f
f :: t -> t
Prelude> :t id
id :: a -> a
Prelude> :t id f
id f :: t -> t
Prelude> let g = id f
Prelude> :t g
g :: t -> t
Prelude> id 5
5
Prelude> f 5
5
Prelude> g 5
5
Clearly that's not a problem at all.
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnus@therning.org Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe
More information about the Beginners
mailing list