[MLton] Conversions and the Basis Library

Vesa Karvonen vesa.a.j.k at gmail.com
Wed Nov 28 11:36:30 PST 2007


On Nov 28, 2007 8:04 PM, Wesley W. Terpstra <wesley at terpstra.ca> wrote:
> On Nov 28, 2007, at 6:25 PM, Vesa Karvonen wrote:
> > val convert1 = Int64.fromFixed o IntX.toFixed
> > val convert2 = Int64.fromFixed o Real.toFixedInt IEEReal.TO_ZERO
>
> This is what we all do in practice ATM anyway.

Indeed, *almost*, but not quite.  The problem is that currently only a
part of such conversions are available.  Here is, IMO, an insightful
blog post on the topic of standards:

  http://www.artima.com/weblogs/viewpost.jsp?thread=4840

In particular, one part that I'm thinking about is:

  "Let's get back to first principles...standards have historically
played two roles in our industry. The first role has been to codify
what is already common practice in the industry. Such standards are
attempts to capture what everyone is already doing, perhaps ironing
out some minor inconsistencies along the way."

Doing conversions via FixedInt is what everyone already *wants* to do,
but it is difficult to do and even more difficult to do portably.
Adding the conversions to the Basis library spec would codify existing
practice and iron out a lot of inconsistencies and complexity.

> Converting in and out  of the largest available integer type is
> certainly faster than IntInf, but it's still not as good as composing
> the conversions during SSA after inlining.

Nothing prevents an optimizing compiler from applying the same kind of
optimizations to conversions to/from FixedInt.  If anything, it will
be easier to optimize for the conversion to/from FixedInt rather than
IntInf.

> Also, I could imagine a
> case where the integer being converted actually *was* an IntInf, but I
> didn't know this in my functorized code.

That is why the INTEGER signature specifies values such as maxInt as
an options.  IOW, it is easy to write a correct conversion routine
given two modules implementing the INTEGER signature.

> Conversions through IntInf
> are thus still safer than through the largest fixed type.

That is only in the case that both of the types are the IntInf type.
Note that, by definition, FixedInt is the largest fixed precision
integer.  Even in the case that both types are IntInf, you would get
an Overflow exception rather than the wrong result.  Finally, like I
said before, writing the conversion correctly is not difficult.  Here
is how it looks like:

val AtoB =
   if isSome A.maxInt
   then B.fromFixedInt o A.toFixedInt
   else B.fromLargeInt o A.toLargeInt

> Weren't you the one talking about the Genius Tailor? ;-)

Yes.  The lesson of the story is that you need to think and not accept
things as they are.

-Vesa Karvonen



More information about the MLton mailing list