[MLton] Time-limiting a computation?

Stephen Weeks sweeks@sweeks.com
Fri, 24 Jun 2005 13:22:53 -0700

> This is from Tom Murphy and William Lovas at CMU. We're participating in
> the ICFP programming contest this year,

Hi guys.  I'm glad to see a MLton entry (and hopefully others).  We
weren't able to pull a team together this year.

> and part of what we need to do is to limit a computation to 5
> seconds. (That is, compute better and better answers to a problem,
> but definitely report one before 5 seconds is up.)
> What's a robust way to do this with mlton? Can we safely use the
> signal mechanism?

Yes.  That's what I've done in the past for such things.  Have a look
at the timeLimit function in lib/mlton/basic/engine.{sig,sml} in the
MLton sources.

> Do we have to be careful about race conditions in that case?  

I don't think there's much to worry about if you simply stop the
computation and report the value of some global ref cell holding the
answer.  As long as the ref cell doesn't hold some mutable data
structure that is in an inconsistent state, you should be OK.