+ - 0:00:00
Notes for current slide
Notes for next slide

CS720: Lecture 13  ❧  Tiago Cogumbreiro

CS720

Logical Foundations of Computer Science

Lecture 13: Program equivalence

Tiago Cogumbreiro

1 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Summary

  • Behavioral equivalence
  • Properties on behavioral equivalence
  • Program transformations
2 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Program equivalence

  • A framework to compare "equivalent" programs, notation PQP \equiv Q
  • The notion of equivalent is generic
  • Program equivalence can be used to reason about correctness of algorithms
  • Program equivalence can be used to reason about the correctness of program transformations

Examples:

  • compilable programs
  • programs that produce the same output
  • programs that perform the same assignments
  • programs that read the same variables
3 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Usual equivalence properties

  • Reflexive: PPP \equiv P
  • Symmetric: PQ    QPP \equiv Q \implies Q \equiv P
  • Transitive: PQ    QR    PRP \equiv Q \implies Q \equiv R \implies P \equiv R
  • Congruence: PQ    C(P)C(Q)P\equiv Q \implies \mathcal{C}(P) \equiv \mathcal{C}(Q) where C:PP\mathcal{C}: \mathcal{P} \rightarrow \mathcal{P} is known as a context, a program with a "whole" that is filled with the input program, outputting a "complete" program; it is expected that the input occurs in the output.
4 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Syntactic equivalence

If two programs are textually equal (are the same syntactic term), then we say that the two programs are syntactically equivalent.

Example: APlus (ANum 3) (ANum 0) is syntactically equivalent to APlus (ANum 3) (ANum 0).

Behavioral equivalence

If two programs start from an initial state and reach the same final state, then we say that the two programs are behaviorally equivalent.

Example:

X:=3;; WHILE 1<=X DO Y:=Y+1;; X:=X-1 END

is behaviorally equivalent to

X:=0 ;; Y:=3
5 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

How do we formalize behavioral equivalence for

arithmetic expressions, boolean expressions, commands?

6 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Behavioral equivalence

For arithmetic expressions a1a2a_1 \equiv a_2, e.g., xxx - x \equiv 0:

7 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Behavioral equivalence

For arithmetic expressions a1a2a_1 \equiv a_2, e.g., xxx - x \equiv 0:

s ⁣:aeval(s,a1)=aeval(s,a2)a1a2\frac{ \forall s\colon \mathtt{aeval}(s, a_1) = \mathtt{aeval}(s, a_2) }{ a_1 \equiv a_2 }

For boolean expressions b1b2b_1 \equiv b_2, e.g., (xx=0)(x - x = 0) \equiv \top:

7 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Behavioral equivalence

For arithmetic expressions a1a2a_1 \equiv a_2, e.g., xxx - x \equiv 0:

s ⁣:aeval(s,a1)=aeval(s,a2)a1a2\frac{ \forall s\colon \mathtt{aeval}(s, a_1) = \mathtt{aeval}(s, a_2) }{ a_1 \equiv a_2 }

For boolean expressions b1b2b_1 \equiv b_2, e.g., (xx=0)(x - x = 0) \equiv \top:

s ⁣:beval(s,b1)=beval(s,b2)b1b2\frac{ \forall s\colon \mathtt{beval}(s, b_1) = \mathtt{beval}(s, b_2) }{ b_1 \equiv b_2 }

For commands c1c2c_1 \equiv c_2:

7 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Behavioral equivalence

For arithmetic expressions a1a2a_1 \equiv a_2, e.g., xxx - x \equiv 0:

s ⁣:aeval(s,a1)=aeval(s,a2)a1a2\frac{ \forall s\colon \mathtt{aeval}(s, a_1) = \mathtt{aeval}(s, a_2) }{ a_1 \equiv a_2 }

For boolean expressions b1b2b_1 \equiv b_2, e.g., (xx=0)(x - x = 0) \equiv \top:

s ⁣:beval(s,b1)=beval(s,b2)b1b2\frac{ \forall s\colon \mathtt{beval}(s, b_1) = \mathtt{beval}(s, b_2) }{ b_1 \equiv b_2 }

For commands c1c2c_1 \equiv c_2:

s1,s2 ⁣:s1= ⁣ ⁣ ⁣[c1] ⁣ ⁣ ⁣s2    s1= ⁣ ⁣ ⁣[c2] ⁣ ⁣ ⁣s2c1c2\newcommand{\Aand}{\mathbin{/}} \newcommand{\Aev}{\mathbin{\char`\\\char`\\}} \newcommand{\update}[2]{\mathbin{\&} \{{#1}\rightarrow{#2}\}} \newcommand{\Red}[2]{{#2}=\!\!\![{#1}]\!\!\!\Rightarrow} \frac{ \forall s_1,\forall s_2\colon \Red{c_1} {s_1} {s_2} \iff \Red{c_2} {s_1} {s_2} }{ c_1 \equiv c_2 }
7 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Exercise: skip

Prove that

skip;cc\mathtt{skip};c \equiv c
Theorem skip_left: forall c,
cequiv <{skip; c}> c.
8 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Exercise: if

If bb \equiv \top, then if b then c1 else c2 endc1\mathtt{if}\ b\ \mathtt{then}\ c_1\ \mathtt{else}\ c_2\ \mathtt{end} \equiv c_1.

Theorem if_true: forall b c1 c2,
bequiv b <{true}> ->
cequiv
<{ if b then c1 else c2 end }>
c1.

What could bb in bb \equiv \top be? For instance, the following statement holds. (By using lemmas Nat.add_0_r, Nat.eqb_refl.)

(x+x=2x)(x + x = 2 * x) \equiv \top
Require Import PeanoNat.
Goal forall x, bequiv (x + x = 2 * x) BTrue.
9 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Exercise: while

Theorem: If bb \equiv \bot, then while b do c endskip\mathtt{while}\ b\ \mathtt{do}\ c\ \mathtt{end} \equiv \mathtt{skip}.

Theorem: If bb \equiv \top, then for all ss and ss', we have ¬s= ⁣ ⁣ ⁣[while b do c end] ⁣ ⁣ ⁣s\neg s =\!\!\![ \mathtt{while}\ b\ \mathtt{do}\ c\ \mathtt{end} ]\!\!\!\Rightarrow s'.

Theorem: while b do c endif b then c;while b do c end else skip end\mathtt{while}\ b\ \mathtt{do}\ c\ \mathtt{end} \equiv \mathtt{if}\ b\ \mathtt{then}\ c ; \mathtt{while}\ b\ \mathtt{do}\ c\ \mathtt{end}\ \mathtt{else}\ \mathtt{skip}\ \mathtt{end}.

10 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Properties of equivalences

An equivalence relation is:

  • reflexive
  • symmetric
  • transitive

Show that aquiv, bequiv, and cequiv each is an equivalence relation.

Lemma refl_cequiv : forall (c : com), cequiv c c.
Lemma sym_cequiv : forall (c1 c2 : com), cequiv c1 c2 -> cequiv c2 c1.
Lemma trans_cequiv : forall (c1 c2 c3 : com), cequiv c1 c2 -> cequiv c2 c3 -> cequiv c1 c3.
11 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

\equiv is a congruence

Generally a congruence can be described as

cc    C(c)C(c)c \equiv c' \implies \mathcal{C}(c) \equiv \mathcal{C}(c')

For commands this corresponds to proving

aa(x::=a)(x::=a)c1c1c2c2(c1;;c2)(c1;;c2)bbc1c1c2c2if b then c1 else c2 endif b then c1 else c2 endbbccwhile b do c endwhile b do c end\frac{ a \equiv a' }{ (x ::= a) \equiv (x ::= a') } \qquad \frac{ c_1 \equiv c_1' \qquad c_2 \equiv c_2' }{ (c_1;; c_2) \equiv (c_1';;c_2') } \\[1em] \frac{ b \equiv b' \qquad c_1 \equiv c_1' \qquad c_2 \equiv c_2' }{ \mathtt{if}\ b\ \mathtt{then}\ c_1\ \mathtt{else}\ c_2\ \mathtt{end} \equiv \mathtt{if}\ b'\ \mathtt{then}\ c_1'\ \mathtt{else}\ c_2'\ \mathtt{end} } \\[1em] \frac{ b \equiv b' \qquad c \equiv c' }{ \mathtt{while}\ b\ \mathtt{do}\ c\ \mathtt{end} \equiv \mathtt{while}\ b'\ \mathtt{do}\ c'\ \mathtt{end} }
12 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Congruence example

Program equivalence

Example congruence_example:
cequiv
(* Program 1: *)
<{ X := 0;
if (X = 0)
then Y := 0
else Y := 42 end }>
(* Program 2: *)
<{ X := 0;
if (X = 0)
then Y := X - X
else Y := 42 end }>.
13 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Sound transformations

  • We can specify the notion of a transformation that is sound
  • Example: source-to-source compiler, code optimizer.
Definition atrans_sound (atrans : aexp -> aexp) : Prop :=
forall (a : aexp),
aequiv a (atrans a).
Definition btrans_sound (btrans : bexp -> bexp) : Prop :=
forall (b : bexp),
bequiv b (btrans b).
Definition ctrans_sound (ctrans : com -> com) : Prop :=
forall (c : com),
cequiv c (ctrans c).
14 / 14

CS720: Lecture 13  ❧  Tiago Cogumbreiro

Summary

  • Behavioral equivalence
  • Properties on behavioral equivalence
  • Program transformations
2 / 14
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow