ok

From the SETL Wiki

Jump to: navigation, search
ok
Category control
Syntax nullary
Compatibility
CIMS SETL yes
SETL-S no
SETL2 no
GNU SETL no

Contents

Purpose

Make a non-deterministic boolean choice

Synopsis

 ok

Description

Simulates a non-deterministic choice (between returning true or false.)

ok works by effectively forking the current process into two sub-processes, with ok returning true in one and false in the other.

The two sub-processes are executed in sequence rather than in parallel. At first only the true branch executes immediately, with the false branch initially suspended. If control later reaches a fail statement, the true branch is terminated and the false branch begins executing.

These sub-processes are similar to unix processes in the fact that they both begin execution at the same point in the program, with copies of the same procedure call stack. However they are like threads in that all memory is shared between the two sub-processes by default (this can be overridden with a back declaration on a variable, causing each sub-process to get a private copy of that variable.)

ok is very similar to the AMB special form

Examples

  • Generate single-word anagrams
     
    dictionary := {'ascertain', 'ascertained', 'ascending', 'cartesian',
                   'cartwheel', 'certain', 'sect', 'sectarian', 'section'};
    letters := 'aaceinrst';
    words := {};
    
    (while ok)
      word := ;
      (while exists letter = letters(i) | ok)
        word +:= letter;
      end;
      if letters = '' and word in dictionary then
        words with:= word;
      end if;
      fail;
    end;
    
    print(words);
    

    Output:

    {ascertain cartesian sectarian}
    


Notes

Possible methods for implementing ok in a SETL compiler or interpreter include:

  • Using the unix fork function, with the child process initially waiting until the parent has exited before it resumes execution. Explicitly allocating shared memory for variables that require it.
  • Using continuations, either by translating to a target language that natively supports them or by translating SETL procedures into continuation passing style. Variables declared back would be pushed onto a special stack with each invocation of ok and then either restored on the following invocation of fail or discarded on invocation of succeed.

See also

Personal tools