XML

kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for sequential-execution

2003

Question 56 (2003):

Why can't you have multiple lines under a guard for example? It always says incorrect indentation.

    IF
      (equal.string(name, ""))
        copy.string(temp.name, name)
        copy.string(temp.score, score)
        copy.string(temp.name.length, name.length)

(just example code)

How can I get it to do all of these functions?

Answer 56:

See the answer to Question 1 (2000).

Keywords: sequential-execution

2000

Question 83 (2000):

How can I implement an ALT without it being in a SEQ? I have the following:

    WHILE TRUE
      ...
      PAR
        SEQ
          ...
        SEQ
          ...
        SEQ
          ALT
            over channel 1
              ...
            over channel 2
              ...
      ...

I can think of how to stick the ALT into a loop and yet read over both channels until both actions from the channels are complete.

Answer 83:

I'm afraid I don't understand the questions. An ALT construct can occur anywhere any other occam statement can occur - on its own, as part of a SEQ, as part of a PAR, as part of an IF etc.

Your code fragment above is illegal because there are three processes in the body of the WHILE-loop (the first set of dots, the PAR, and the last set of dots) that are all at the same level of indentation but have no controlling SEQ or PAR.

Should your "can" in your second question have been "can't"? Servicing multiple input channels is one of the basic capabilities of an ALT. Each ALT waits for and services just one input completely. Put it in a loop to service them all continuously.

Keywords: alternation , sequential-execution


Question 23 (2000):

In the example below, how can I execute the quoted code after any of the two guards is executed? I could put the code with a SEQ in the process part of both guards, but that wouldn't look nice. If I indent the quoted code as in the example below, the compiler complains.

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    PRI ALT
      inA ? x
        outA ! 1
      inB ? z
        outB ! 2
  " SEQ
      ...  do in all cases"

Answer 23:

This (I think) is what you want but wouldn't look nice:

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    PRI ALT
      inA ? x
        SEQ
          outA ! 1
          ...  do in all cases
      inB ? z
        SEQ
          outB ! 2
          ...  do in all cases

If so, the common code can be factored out (so that it appears only once) by moving the SEQ outwards:

  PROC priAlt (CHAN OF INT inA, inB, outA, outB)
    INT x, z:
    SEQ
      PRI ALT
        inA ? x
          outA ! 1
        inB ? z
          outB ! 2
      ...  do in all cases

Now, the PRI ALT executes one of its guarded processes (when one or both of them become ready). Then, the do in all cases code gets executed.

Keywords: alternation , sequential-execution


Question 1 (2000):

What *exactly* does SEQ do and when is it *not* necessary?

Answer 1:

occam lets us write parallel code with the same fluency as it (and other languages) lets us write sequential code. So, whenever we have a collection of statements, we have to say whether they are to be executed in SEQuence (in the order written) or in PARallel (in which case, the order written is irrelevant). For example, the statements:

    in ? n           -- input from channel, in, to variable, n
    a := 2*n         -- multiply n by 2 and assign the answer to a

only make sense for SEQuential execution. The way we specify SEQuential execution is to indent them as the body of a SEQ construct:

  SEQ
    in ? n           -- input from channel, in, to variable, n
    a := 2*n         -- multiply n by 2 and assign the answer to a

On the other hand, the following statements:

    in ? n           -- input from channel, in, to variable, n
    a := 2*b         -- multiply b by 2 and assign the answer to a

are logically independent from each other. In this case, we can choose whether to have them executed in SEQuence (as above) or in PARallel:

  PAR
    in ? n           -- input from channel, in, to variable, n
    a := 2*b         -- multiply b by 2 and assign the answer to a

Aside: occam does not allow us to specify PARallel execution of statements that interfere with each other's variables (or channels) - these rules are made precise in section 4.2 of the occam2.1 Checklist. So, the construct:

  PAR
    in ? n           -- input from channel, in, to variable, n
    a := 2*n         -- multiply n by 2 and assign the answer to a

would be rejected by the compiler.

Note: the statements controlled by a SEQ (or a PAR) must be indented by two spaces. There can, of course, be any number of statements -- not just two as in the above examples. The end of the construct is indicated by outdenting back to the level of the controlling SEQ (or PAR).

Keywords: seq , sequential-execution

Referrers: Question 28 (2003) , Question 56 (2003) , Question 3 (2000) , Question 28 (2000)

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:32 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.