[MLton] Conversions and the Basis Library

Vesa Karvonen vesa.a.j.k at gmail.com
Wed Nov 28 09:25:25 PST 2007


On Nov 28, 2007 6:23 PM, Wesley W. Terpstra <wesley at terpstra.ca> wrote:
[...]
> Since the TIME signature creates value via the IntInf conversion path,
> I doubt changing the representation will help without first solving
> the more crucial failure to catch (IntX.fromLarge o IntY.toLarge).
> I've looked into how to do this and the main problem seems to be that
> all the Overflow handling logic is inside the basis. This prevents a
> rewrite rule from being written in the SSA stages.
>
> As a recap, the basis recommends programmers to use idioms like:
> val convert1 = Int64.fromLarge o IntX.toLarge
> val convert2 = Int64.fromLarge o Real.toLargeInt IEEReal.TO_ZERO
> val convert3 = Int16.fromInt o Int8.toInt
> val convert4 = Char.chr o Char.ord
[...]

I got to know these issues fairly intimately while working on my
generics library.  I believe I have a technically (but not politically)
much simpler solution that should also give a noticeable speedup.

I think that the gist of the problem is that one needs to go through
LargeInt, which is inherently inefficient.  It would be better to go
through FixedInt.  IOW, the first two would be written as:

val convert1 = Int64.fromFixed o IntX.toFixed
val convert2 = Int64.fromFixed o Real.toFixedInt IEEReal.TO_ZERO

This means that FixedInt would be made non-optional in the Basis library
spec and that conversion to/from FixedInt would be added to the INTEGER,
WORD, and REAL signatures.  The Basis spec does not currently require any
fixed precision int type to be provided.  However, I'm not aware of any
maintained SML implementation that does not provide at least a single
fixed precision int type.

In addition, for similar reasons, there should also be conversions to/from
Word8 in the WORD signature.

My Extended Basis library already provides these operations:

http://mlton.org/cgi-bin/viewsvn.cgi/*checkout*/mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/intable.sig
http://mlton.org/cgi-bin/viewsvn.cgi/*checkout*/mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/wordable.sig

While I have spent some tweaking the implementations of the conversions,
they are not necessarily optimal (even with respect to what is possible
with the current Basis spec).  A compiler could easily provide efficient
implementations of the conversions.

I believe that with these additions to the Basis library, most performance
issues with integer conversions could be eliminated with a relatively
small effort.

I think that SML implementations should take initiative in adding some
useful features (like this :-) to the Basis library.

-Vesa Karvonen



More information about the MLton mailing list