[MLton] MLton.Thread.atomically

Stephen Weeks MLton@mlton.org
Fri, 26 Mar 2004 11:16:12 -0800


> It be nicer to have
> 
> val MLton.Thread.make_atomic : ('a -> 'b) -> 'a -> 'b
> 
> fun atomically f = MLton.Thread.make_atomic f ()

It is certainly a matter of taste, since each is expressible in terms
of the other.

fun make_atomic f x = atomically (fn () => f x)

Or, more generally, the types are equivalent.

signature D =
   sig
      val f: ('a -> 'b) -> 'a -> 'b
   end

signature S =
   sig
      val f: (unit -> 'a) -> 'a
   end

functor DToS (D: D): S =
   struct
      val f = fn th => D.f th ()
   end

functor SToD (S: S): D =
   struct
      val f = fn g => fn x => S.f (fn () => g x)
   end

I find the type ('a -> 'b) -> 'a -> 'b less transparent than the type
(unit -> 'a) -> 'a, since make_atomic doesn't really care about about
the argument you are passing to f.  Also, the currying (in my
programming style) implies there is a staging going on, but there
isn't.  But I even prefer (unit -> 'a) -> 'a to ('a -> 'b) * 'a -> 'b
because of the first reason.