
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

HW 11 Prof. Caldwell Due: 19 February 2009 COSC 4780
In class we noted that semantics can be used to reason abut programs. Already in the core language we can do some simple heuristic analysis to try to catch looping programs. Consider the following simple analysis.
We know that [while 0 = 0 do Skip od : comm] = ⊥
Can we generalize this observation to try to design a piece of code than analyzes programs in the core language to detect possible loops? Of course, we will not be able to detect all looping behavior, this would be the equivalent of solving the halting problem. But given a while statement of the form while E do C od : comm
When do we know it will loop forever? Certainly, if [E : boolexp] = true for all s. But de- termining whether E evaluates to the constant true in all stores could be hard. Lets consider something easier. If [E : boolexp] = true for some store s and the command C does not update any location referenced by E, then the program will loop. So, if we could, for while statements, check to see of the locations updated by the body C are disjoint from the locations referenced in the expression E, we would have identified a possible looping statement. Here a are some definitions:
Say a location is active in a command C if it occurs on the left side of an assignment statement in C. A location is referenced in an expression if it occurs in the expression. Assignment: Write functions exp refs and active locs that recursively collect lists of location references in expressions and a lists of active locations in a command. Write a function analyze that reports possible looping behavior in a program by checking all while statements to see if the set of location referenced in the condition overlap with the active locations in the body. If so, report it Good. If not, report the violating while statement as Possible C. You can use the following type to encode return values form the analyze function.
type report = Good | Possible of command;;
Here are the expected types of your functions:
exp refs : expression -> loc list active locs : command -> loc list analyze : command -> report
You should add some tests cases that show that your code really recursivley descends though more complex commands and expressions.