Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: continuatio...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 7 Topic 2716 of 3037
Post > Topic >>

Re: continuations in SML

by Vesa Karvonen <vesa.karvonen@[EMAIL PROTECTED] > Jan 6, 2008 at 12:00 PM

Michele Simionato <michele.simionato@[EMAIL PROTECTED]
> wrote:

> I see that both SML/NJ and MLTon provide continuations, however their
> usage is somewhat different than in Scheme.

Well, a difference is that callcc p***** the continuation as a value of an
abstract type, rather than as a function, to the given function.  Then you
use throw instead of function application to jump to a continuation.  One
reason for this arrangement is allowing throw to be called from a context
of any type.  See the following article:

  Typing First-Class Continuations in ML
  Robert Harper, Bruce F. Duba, and David MacQueen
  http://citeseer.comp.nus.edu.sg/11210.html

> Can somebody provide a translation of the following toy Scheme program
> into SML?

> (define-macro (store/cc! name . body)
>    (let ((k (gensym)))
>     `(call-with-current-continuation
>       (lambda (,k) (set! ,name ,k) ,@[EMAIL PROTECTED]
))))

open SMLofNJ.Cont

fun storecc r th = callcc (fn k => (r := SOME k ; th ()))

> (define cont #f); make room for the continuation

val cont : int cont option ref = ref NONE

> (display (+ 1 (store/cc! cont 0))) ;=> displays 1
> (cont 1) ;=> displays 2

The above two lines form a loop, although, if you run the above from
Bigloo's REPL, the continuation captured on the display expression seems
to be limited to that expression.  Running the above directly, as two
top-level expressions,

print (Int.toString (1 + storecc cont (fn () => 0))) ;
throw (valOf (!cont)) 1 ;

in SML/NJ's REPL gives

  Error: throw from one top-level expression into another

So, in SML/NJ's REPL they need to be merged into a single top-level
expression and make sure it doesn't loop:

val () =
    (print (Int.toString (1 + storecc cont (fn () => 0)))
   ; case !cont
      of SOME k => (cont := NONE ; throw k 1)
       | NONE => ())

-Vesa Karvonen
 




 7 Posts in Topic:
continuations in SML
Michele Simionato <mic  2008-01-05 22:15:11 
Re: continuations in SML
Vesa Karvonen <vesa.ka  2008-01-06 12:00:29 
Re: continuations in SML
Michele Simionato <mic  2008-01-06 04:32:28 
Re: continuations in SML
Benedikt Rosenau <rose  2008-01-11 17:45:58 
Re: continuations in SML
Abdulaziz Ghuloum <agh  2008-01-12 03:14:19 
Re: continuations in SML
Abdulaziz Ghuloum <agh  2008-01-12 03:48:47 
Re: continuations in SML
Benedikt Rosenau <rose  2008-01-13 22:58:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 12:57:46 CDT 2008.