[MLton] Continuations and MLton.Vector.create

Vesa Karvonen vesa.karvonen@cs.helsinki.fi
Wed, 29 Mar 2006 00:48:20 +0300


Quoting Henry Cejtin <henry.cejtin@sbcglobal.net>:
[...]
> The only ugliness to me in the create proposal is the fact  that  update  and
> sub might be squirreled away, so you have to make them raise Fail if they are
> called after create returns.
[...]

Looking at the proposed create function made me think about
continuations. So, I devised the following program:

val () =
    let
       val v' = ref (Vector.fromList [])
       val c = ref NONE
       val v = Vector.tabulate
                  (9, fn i =>
                         if i <> 4 then 1
                         else SMLofNJ.Cont.callcc (fn k => (c := SOME k ; 1)))
    in
       if isSome (!c) then
          let val k = valOf (!c)
          in v' := v
           ; c := NONE
           ; SMLofNJ.Cont.throw k 2 end
       else
          ()
     ; Vector.app (print o Int.toString)   v   ; print "\n"
     ; Vector.app (print o Int.toString) (!v') ; print "\n"
    end

Under MLton it prints

  111121111
  111121111

and under SML/NJ (v110.57) it prints

  111121111
  111111111

With s/Vector/Array both SML/NJ and MLton produce the same output.

-Vesa Karvonen