2005-06-19 · in Ideas · 205 words

As a language and runtime environment, occam provides low-cost switching between multiple processes. Continuations -- such as those provided by Scheme's call/cc -- are used to implement this in other languages. We could thus experiment with providing continuations in occam; this should be relatively low-cost, at least for a subset implementation.

An obvious use would be to avoid deeply-nested control structures. For example, it's not uncommon to see code like this in bits of RMoX that are trying to obtain multiple resources:

try.to.obtain (resource.1, ok)
IF
  ok
    SEQ
      try.to.obtain (resource.2, ok)
      IF
        ok
          SEQ
            ...
        TRUE
          die ("resource.2 failed")
  TRUE
    die ("resource.1 failed")

With an extremely limited form of continuations -- equivalent approximately to C's setjmp -- we could do this:

MOBILE []BYTE var:
CONTINUE cont TO var
  SEQ
    try.to.obtain (resource.1, ok)
    IF
      ok
        SKIP
      TRUE
        cont ("resource.1 failed")
    try.to.obtain (resource.2, ok)
    IF
      ok
        SKIP
      TRUE
        cont ("resource.2 failed")
  die (var)

The CONTINUE block has two children: a block to run first, and another to run if the continuation is called. This would of course be cleaner with a more concise one-branch IF syntax, but it does at least avoid the deep nesting when many such resources must be obtained.

The one-branch IF would look like this:

IF condition
  process