User-defined "builtin" types (was: diff: module_qual.m import warnings)

Fergus Henderson fjh at cs.mu.oz.au
Mon May 26 15:52:48 AEST 1997


Peter Schachte, you wrote:
> 
> > +		% (The types `int', `float', and `string' are defined
> > +		% in mercury_builtin.m, but arguably they ought to be
> > +		% defined in int.m, float.m, and string.m, and so if someone
> > +		% uses the type `int' in the interface, then we don't warn
> > +		% to warn about `import_module int' in the interface.)
> 
> It would be seriously nifty if these types were defined in their
> respective modules (likewise term and list).

Term and list are indeed defined in their respective modules.
(As of last week ;-)

> Then these types wouldn't
> have to be known by the compiler (except as used in implementing the
> compiler).  This would mean I could define my own primitive types (say I
> wanted ints to be bignums, or I really wanted to use BCD for compatibility
> with COBOL :-), or I wanted to implement strings as cords).  And then, of
> course, I would want to have manifest constants of these types appearing
> in my source code, which would require the ability to specify the
> appropriate translation, performed by the compiler, from the const type to
> the appropriate primitive constant.

Haskell has a mechanism a bit like this.
Each integer literal is treated as an implicit call to the FromInteger
function, which is an overloaded function (overloaded in the Haskell sense,
i.e. it is a member of the type class Integral).
Similarly for floating point literals.  (Offhand I'm not sure what it
does with string literals.)

It has some disadvantages, e.g.

	foo = 42

is (according to Hugs 1.3) now ambiguous.
Haskell has a keyword "default" that you can use to define
the default resolution of overloaded integer literals for
a particular module, in order to avoid having to resolve
each such ambiguity with an explicit type declaration.
However, this is a bit inelegant, and makes some things
(e.g. cross-module type inference) significantly harder...

> I think this would be a really good thing.  Are you really contemplating
> all this? 

Contemplating, yes.

I'd like to at least leave that option open.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list