fromInteger and friends

Simon Peyton-Jones [email protected]
Fri, 13 Jul 2001 06:04:40 -0700


Folks

On my bicycle this morning I wondered whether I'd ever advertised the
following feature of GHC, which various people (notably Sergey I think)=20
have asked for.

In Haskell you can *almost* get all your own arithmetic, by not
importing
the Prelude, and instead importing (say) MyPrelude.  But that doesn't
take account of literals: when you say "1" in Haskell you get=20
	(Prelude.fromInteger (1::Integer))
fromInteger doesn't need to be in scope; you don't even have to import
the Prelude; but you get the fromInteger from the Prelude regardless.


In the current GHC 5, if you give the flag -fno-implicit-prelude then
a) the prelude is not imported by default and (more important)
b) instead of PrelNum.fromInteger (1:Integer) you get simply
	(fromInteger (1::Integer))

That is, the fromInteger is picked up from whatever is in scope.

This rebinding of implicitly mentioned identifiers applies to:

	* fromInteger and fromRational used in literals
	* negate, used when you say "- x"
	* subtraction, used when you have an n+k pattern

This should make it possible to completely decouple arithmetic
from the Prelude.

Booleans, lists, tuples etc are still all bound to their Prelude=20
versions -- it's harder to uncouple them, and there is less demand.

This feature is documented, but I don't know whether anyone has ever
tried it out, perhaps because I never told anyone that it existed.  This
message is to rectify that omission.


I think there's a bug in the 5.00 implementation that means it'll only
work well if you define fromInteger etc and then import then; it won't
work properly if you define fromInteger etc in the same module as
the literals that are meant to bind to them.  This'll be fixed in the
next
release, but meanwhile shouldn't be much of a bother.

Let me know if you try it out.

Simon