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 if

2011

Question 6 (2011):

Submission reference: IN2035

Hi, While attempting to test the output from the integrate process for q2, I keep getting this error message whenever I try to run the program:

    Running: q2.tbc
    Error at ../../../../../../../modules/course/libsrc/demo_cycles.occ:50
    Program failed, state = e, eflags = 00000000
    q2.tbc exited with error code: 1

I can't figure out what I'm doing wrong to cause this error :(. Thanks.

Editor: the following question has been rolled into this, since the answer is the same.

Hello, I'm doing Q1 and am on the bit about filtering to remove multiples of 5. I've got some code that I think would work, but my program compiles with no warnings (except that keyboard and error aren't used), but when I run it, it displays:

    Error at [file location]:88
    Program failed, state E =, eflags = 00000001
    q1.tbc exited with error code: 1

If the 88 refers to line 88, I don't understand why as line 88 is simply:

    INT input, remainder:

Can you give any reasons why this might occur (I expect the error message means far more to you than it does to me)?

Answer 6:

These look like possible problems with the Transterpreter? Do you get the same problem when running from a University PC in a terminal room?

Please send me, <p.h.welch@kent.ac.uk>, your source code and say what operating system (and version) you are running. Also, what Java (JDK) version do you have (since the jEdit part of the Transterpreter runs on Java)?

Sorry, doing this will identify yourself to me ... but finding bugs that are our fault will do you no harm! Same also, if the bug turns out to be yours!! :)

Added later: solution to the second question, at least, is below.

Thanks for sending your code – it's your fault!   ;)   But also our fault for giving such a terrible run-time error message and pointing to the wrong line number ...

Check out slides 63-64 of Basics – on the IF process. Also, look here in the occam-pi reference wiki.

Unlike in most programming languages, there is no default do-nothing if an IF-condition fails ... so:

    INT x:
    SEQ
      n := 42
      IF
        n = 43
	  out.string ("All is well", 0, out!)
      n := 99

causes a crash, since none of the IF-conditions (there is only one above) is true.

There is a proper engineering reason for this language decision. If you want a default do-nothing, you must say so:

    INT n:
    SEQ
      n := 42
      IF
        n = 43
	  out.string ("All is well", 0, out!)
        TRUE
	  SKIP
      n := 99

Saying so declares that you have programmed to manage all conditions that are necessary for that IF and that no action need be taken if none of them apply. Such an explicit declaration begs to be defended in any code review.

The INMOS team who first developed occam did not have this rule in the language originally. Their first project was writing the occam compiler in occam and, then, an operating system / IDE for a micro-computer (this was 1984). Their code had plenty of implicit do-nothings at the end of IFs. Someone noticed that many bugs were down to omitted tests (that should not have been omitted). Execution passed through such IFs without failing and crashes occured later that were difficult to trace back to the real source.

They changed the language to the present rule for IF-semantics. Now, explicit do-nothings (TRUE-SKIPs) had to be defended and missing tests were found. IFs without explicit do-nothings were either correct or crashed the first time they executed in a state when the missing test should have been present. Because the crashes were at the actual causes of failure, the bugs were easy to detect and fix.

See also Question 2 (2006), Question 70 (2000) and Question 46 (2000).

It may, or may not, be consoling to know that if you had a Linux or Mac OS and could install KRoC successfully, you could compile with the debug flag:

    kroc -d q1.occ

so that when your execution fails, you are told:

    KRoC: error in PROC "filter" in file "q1.occ" line 96
    KRoC: application error, stopped.

which takes you to the line with the offending IF ...

Keywords: if , stop , transterpreter

Referrers: Question 31 (2012)

2009

Question 11 (2009):

Submission reference: IN1810

How do you compare BOOLs, say in an IF statement? For example, if I have:

    INITIAL BOOL b IS TRUE:
    SEQ
      ...  code where the value of b can change
      IF
        b = TRUE   -- This always says "expected "?", found end of line"
          ...  do something

Can you tell me what I'm doing wrong?

Answer 11:

There is nothing wrong with your code fragment about which the compiler should complain ... unless within the hidden code "where the value of b can change", b is acttually re-declared as a channel! Please mail your seminar leader the whole code if your problem has not disappeared.

However, there is something about your code that is a bit weak. The condition(s) associated with an IF are, of course, BOOL(s). In your code, b is a BOOL (unless re-declared as something else!). Assuming it is a BOOL, then the expression:

        b = TRUE

is a clumsy way of saying:

        b

So, your code would be much neater as:

    INITIAL BOOL b IS TRUE:
    SEQ
      ...  code where the value of b can change
      IF
        b
          ...  do something

Last thought: your IF sturcture had better have some more conditions in case b turns out to be FALSE. Otherwise, there will be a run-time error – see Question 2 (2006) and Question 46 (2000).

Keywords: if , bool

2006

Question 4 (2006):

Submission reference: IN750

With IF statements, is it possible to have a number of statements executed in the body, for example:

    IF
      value <> 0
        out ! 42
        out ! -99
      TRUE
        SKIP

This however give a compile time error. Is there a way to allow multiple statements to be executed.

Answer 4:

The structure of an IF is:

    IF
      condition
        process
      ...
        ...

What you have in your code is two processes indented under the condition -- so you need to say how you want them executed. Sequentially would make sense here, e.g.:

    IF
      value <> 0
        SEQ
          out ! 42
          out ! -99
      TRUE
        SKIP

Missing SEQs are a common cause of the "incorrect indentation" error from the compiler.

Keywords: if , incorrect-indentation


Question 2 (2006):

Submission reference: IN743

I'm trying to write the "filter" process for exercise 1. This has to pass through only numbers not divisible by 5. Here is my test:

    IF
      (n \ 5) <> 0
	out ! n

where "n" is holding the latest number that's been input and "out" is the channel to which numbers are being passed. My process compiles but crashes when run? Why?!! Thanks.

Answer 2:

Please see slides 74-75 in "basics.ppt/pdf". If none of the IF conditions are TRUE, a run-time error is raised.

In the above, the first number arriving that *is* divisible by 5 means that the (single) IF condition above is FALSE. With no further conditions, the IF construct throws an error. You need another condition to catch the case when the first condition fails - in your example, it should be simply TRUE - e.g.

    IF
      (n \ 5) <> 0
        out ! n
      TRUE
        SKIP            -- do nothing

Keywords: if

Referrers: Question 6 (2011) , Question 11 (2009) , Question 59 (2008)

2000

Question 46 (2000):

I am confused with the IF ... TRUE statement. Does TRUE act like the ELSE (where ELSE is used in Java's IF statements)? Or does occam extend its IF statement vocabulary to include ELSE as well?

Answer 46:

There is no such thing as the IF ... TRUE statement in occam. There is an IF constructor that takes a sequence of indented conditional processes. Each condiional process consists of a boolean condition followed by an indented process (which could be a single primitive statement, with or without local declarations, or some compound statement).

The rule is that the conditions in each conditional process are tested in the sequence listed. The process behind the first one that turns out to be TRUE is executed. If none turn out TRUE, this is a run-time error.

The IF has no ELSE part (to do if all the conditions fail). But we get the same thing if the last condition we write is just TRUE. See slide 4-44 and section 4.4 of the occam2 checklist.

Keywords: if

Referrers: Question 6 (2011) , Question 11 (2009)


Question 44 (2000):

I am currently doing the pairs toggling bit of Q4. I am trying to say

  IF there is a toggle input
  THEN switched := NOT switched
  ELSE carry on excuting the rest of the code.

where toggle is an input channel and switched is a boolean variable.

Answer 44:

You are describing a poll on the toggle channel. See slide 5-37 in the notes. You may consider the use of polling absolutely essential (see the comment on slide 5-37) for your solution to this part of the question - even though it's not!

Keywords: q4 , inverting , if

Referrers: Question 47 (2000)


Question 28 (2000):

Is there a way of allowing two commands to be carried out within an IF statement's condition in occam?

Answer 28:

See the answer to Question 1 (2000) of these anonymous questions. When there are two things to do in occam, we have to say whether they are to be done in SEQuence or in PARallel. Probably, you want then to be done in the SEQuence they are written - in which case:

  IF
    some.test > or.other
      SEQ
        ...  first thing to do
        ...  second thing to do
    another.test
      SEQ
        ...  another first thing to do
        ...  another second thing to do

Keywords: if

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:27 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.