The PL0 language

This page formally defines the language PL0 considered in the lecture. It is similar to the Viper language, but not identical.

In particular, the formalization includes

  1. variables and constants,
  2. the syntax of PL0 programs,
  3. our notion of program states and program configurations,
  4. the formal semnatics of PL0, including the semantics of arithmetic expressions, Boolean expressions, predicates, and statements.

Variables & Constants

We denote by Var the set of (program) variables, such as x, y, z, etc. Moreover, we typically denote integer-valued constants (of type Int) by c, u, v, w, etc. Analogously, we denote truth values (of type Bool) by t.

Syntax

The syntax of PL0 is composed of

Arithmetic Expressions

The set AExpr of arithmetic expressions is given by the grammar:

a    ::=    c           (integer-valued constants)
         |  x           (variables)
         |  a + a       (addition)
         |  a * a       (multiplication)
         |  a - a       (subtraction)
         |  a / a       (integer division)
         |  a % a       (remainder)

Boolean Expressions

The set BExpr of Boolean expressions is given by the grammar:

b    ::=    true        (truth)
         |  false       (falsety)
         |  a == a      (equality between arithmetic expressions)
         |  a != a      (inequality between arithmetic expressions)
         |  a <= a      (less-or-equal for arithmetic expressions)
         |  a >= a      (greater-or-equal for arithmetic expressions)
         |  a < a       (less-than for arithmetic expressions)
         |  a > a       (greater-than for arithmetic expressions)
         |  !b          (negation)
         |  b && b      (conjunction)
         |  b || b      (disjunction)

Predicates

The set Pred of predicates P is given by the following grammar:

P    ::=    b                   (Boolean expressions)
         |  exists x :: P       (existential quantification)
         |  forall x :: P       (universal quantification)
         |  !P                  (negation)
         |  P && P              (conjunction)
         |  P || P              (disjunction)
         |  P ==> P             (implication)

PL0 Statements

The set Stmtof program statements S written in PL0 is given by the following grammar:

S    ::=    var x       (local variable declaration)
         |  x := a      (assignment)
         |  assert P    (assertion)
         |  assume P    (assumption)
         |  S;S         (sequential composition)
         |  S [] S      (nondeterministic choice)

Program States

The set States of program states consists of all mappings from finitely many variables to (integer) values, that is,

We denote by the state is equal to except that we assign value to variable , that is,

Notice that the domain of a state , , can be enlarged through the update , that is, .

Operational Semantics

We will define the semantics of statements using four execution relations:

  1. a relation for evaluating arithmetic expressions in a program state;
  2. a relation for evaluating Boolean expressions in a program state;
  3. a relation for evaluating predicates in a program state; and
  4. a relation for performing one execution step from one program configuration (a statement coupled with a program state) to another configuration.

Evaluation of arithmetic expressions

For arithmetic expressions a in AExpr, we denote by that evaluating expression a in state yields integer value v.

Formally, the evaluation relation is given by the following inference rules:

Notice that the last rule from above uses the standard arithmetic operators on integers v1 and v2 with one exception: we assume that all arithmetic operators are defined for every input, that is, they are total functions. If an operation is not well-defined, then it returns some value but we do not know which one. For example, evaluating 5 / 0 yields such an unknown value. We will address how to spot and report such runtime errors later in the course.

Evaluation of Boolean expressions

For Boolean expressions b in BExpr, we denote by that evaluating expression b in state yields truth value t.

Formally, the evaluation relation is defined by structural induction using the following inference rules:

In the above rules, we write to denote the value obtained from performing a standard comparison between two integer values.

Evaluation of predicates

For predicates P in Pred, we denote by that evaluating predicate P in state yields truth value t.

Formally, the evaluation relation is defined by structural induction using the following inference rules:

Program Configurations

A program configuration is either a pair consisting of a statement and a program state , or a pair indicating termination with final state , or , which indicates the occurrence of a runtime error, or indicating that we ended up in a magical place where anything is possible (i.e., we can verify any property, even wrong ones). Formally, the set of program configurations is given by

Execution relation for statements

For PL0 statements S in Stmt, we denote by that executing one step starting in configuration results in configuration . Formally, the execution relation is defined by structural induction using the following inference rules:

Notice that we can always perform at least one step of the execution relation until we reach a final configuration of the form error, magic, or (done, _).