# Algebraic Semantics of Imperative Programs

### An executable course on the semantics and verification of imperative programs

We have used OBJ3 in teaching a MSc course on the semantics of imperative programs at Oxford University. Click here for the original Oxford University syllabus. The book for this course, Algebraic Semantics of Imperative Programs, by Joseph Goguen and Grant Malcolm, is published by MIT Press, ISBN 0-262-07172-X.

Specific goals of this course include the following:

• improve intuition and ability in imperative programming, through understanding the semantics of programs and seeing numerous examples;
• teach how to prove properties of programs;
• develop relevant mathematical background;
• present the OBJ system and use it for all proofs; and
• show that all this can be done in a way that is completely rigorous, yet not too difficult or too abstract, by using equational logic, which is simply the logic of substituting equals for equals.

Our semantic approach gives an equational specification for a class of abstract machines and specifies programming language features by their effect on these machines. The features treated in the course are assignment, sequential composition, conditional, while-loop, procedure definition, and procedure call; we also give semantics for the array data structure and for programs that manipulate arrays. The equational axioms describing these features are used in proving program correctness.

Thus, this course both presents a rigorous semantics for imperative programs, and uses that semantics to prove properties of programs. Most comparable courses concentrate on one or the other of these aims, either presenting mathematical theories of semantics of programming languages, or presenting a logical calculus (such as Hoare-triples or weakest preconditions) and using this to prove properties of programs. In both cases, the course work is done on paper, since the semantics of programs and proofs of their properties are presented on paper.

By contrast, our Algebraic Semantics of Imperative Programs course uses an executable presentation of program semantics in OBJ3, an implemented specification language that can be used for theorem proving and rapid prototyping, and which itself has a formal semantics based on equational logic. OBJ is not just another functional programming language, although it does have an executable functional sublanguage. OBJ was designed for algebraic semantics; its declarations introduce symbols for sorts and functions, its statements are equations, and its computations are equational proofs. OBJ also has a sublanguage of "theories" for declaring properties. Thus, an OBJ "program" actually is an equational theory, and every OBJ computation actually proves some theorem about such a theory. This means that an OBJ program used for defining the semantics of a program already has a precise mathematical meaning. Moreover, standard techniques for mechanising equational reasoning can be used for verifying programs.

A detailed treatment of our use of OBJ in teching appears in An Executable Course in the Algebraic Semantics of Imperative Programs, by Joseph Goguen and Grant Malcolm, in Teaching and Learning Formal Methods, edited by Michael Hinchey and C. Nevill Dean, Academic Press, 1996, pages 161-179.

An algebraic treatment of non-determinism following the style of the course is given in a PRG technical report, Algebraic Semantics of Non-determinism by Grant Malcolm and Joseph Goguen.