next up previous
Next: Variable Memory Up: CCL The Computation and Previous: Including Libraries


Programs

Programs are the heart of CCL. A program consists of two parts: A set of initializers, and a set of guarded commands. Programs are intended to be the guts of a while statement. As in

x := 1;
while true do
  if x > 0 then x := x + 1 end;
  print ( x );
end while
which is not a ccli program, by the way. In this psuedocode snippet, the line x := 1 is an initializer and the if-statement is a guarded command. In CCL you write

include standard.ccl

program main() := {
  x := 1;
  x > 0 : {
    x := x + 1,
    print ( x, "\n" )
  };
};
which is a ccli program. If you make a file with the program above in it, and then run ccli on the file, you will get
2
3
4
5
6
...
ad infinitum (press cntl-C to halt ccli). It starts by setting x to 1 and then it continuously executes the guarded command over and over again.

The general syntax for a program declaration is

program p ( param_1, ..., param_n ) := {
  statement_1
  statement_2
  ...
  statement_m
}
where the statements are either assignments of values to variables (and therefore initializers) or they are guarded commands. Guarded commands have the form
boolexpr : {
  command_1,
  command_2,
  ...
  command_k
}
which means, to ccli anyway, that while executing the program main, if boolexpr is true, then execute all the commands in the body of the guarded command. In each iteration of the program, ccli considers the guarded commands in order and then, in those whose guards are true, executes the assignments in order as well. A command is either an assignment or an expression. In the latter case the expression is evaluated and its result is thrown out (which is useful when the expression has side effects, as with the external function print).

Initializer statements can also have the form:

needs x, y, z;
which tells ccli that the enclosing programs needs the values of $x$, $y$ and $z$ to be initialized elsewhere. This will make more sense after you read Section [*], about program composition.

As we have noted, ccli looks for a program called main() to execute. So if you want something to happen, you need a main program defined somewhere.

The parameters can be used to create a whole family of programs, one for each set of values of the parameters. For example, you might write

include standard.ccl

program p ( x0, delta ) := {
  x := x0;
  x > 0 : {
    x := x + delta,
    print ( x, "\n" )
  };
};

program main() := p ( 5.35, 0.01 );
which produces the output
5.36
5.37
5.38
...
ad infinitum. The program main should not have parameters.



Subsections
next up previous
Next: Variable Memory Up: CCL The Computation and Previous: Including Libraries
Eric Klavins 2003-12-03