atoms vs strings

Fergus Henderson fjh at
Tue Apr 29 01:38:46 AEST 1997


Just thought of another use for that `object' module: emulating
Prolog atoms.

	:- module atom.
	:- interface.

	:- type atom.
	:- func atom(string) = atom.
	:- func atom_name(atom) = string.

	:- implementation.
	:- import_module object.

	:- type atom == object(string).
	atom(String) = canonical_object(String).
	atom_name(Atom) = object_value(Atom).

Nifty, hey? ;-)

The only drawback is that the performance won't be very good.  To make
this efficient enough, I think we would have to teach the compiler to
automatically memoize the results of any expensive expression or goal
(i.e. one that involves a procedure call) if it has no input variables --
for that trivial case, memoizing reduces to just checking a boolean
flag.  Then an expression like `atom("Hello world")' wouldn't have to
search the table every time it is evaluated.

Prolog compilers do even better: they build the atom table at
link time.  We could perhaps do that too, but it doesn't work
very well with dynamic linking.

Fergus Henderson <fjh at>   |  "I have always known that the pursuit
WWW: <>   |  of excellence is a lethal habit"
PGP: finger fjh at         |     -- the last words of T. S. Garp.

More information about the developers mailing list