Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1

We present here a mechanically verified Isabelle/HOL falseExpr == Const falseVal


proof that a program assertion will have the same true1Expr :: Expr (true1)
truth value whether it is interpreted in a classical 2- true1 == Const trueVal1
not :: Expr ⇒ Expr
valued logic or 3-valued logic provided it is defined— not e == If e false true1
in accordance with the definedness predicate trans- trueExpr :: Expr (true)
former detailed in the paper. In fact, we prove both trueExpr == not false
the soundness and completeness of our approach. Note
that this appendix is automatically generated from the Using the if-then-else operator we define conditional
Isabelle/HOL theory files. conjunction and strict conjunction along with abbrevi-
ations using the familiar Java syntax && and &, respec-
As can be expected, the proof is syntax directed. We start
tively.
by defining our expression language syntax.
constdefs
cconj :: Expr ⇒ Expr ⇒ Expr (infix && 60)
A E XPRESSION S YNTAX — conditional conjunction
theory Syntax imports Main begin e1 && e2 == If e1 e2 false
conj :: Expr ⇒ Expr ⇒ Expr (infix & 60) — strict conjunction
Our logic language is inspired by C expressions in that e1 & e2 == If e1 e2 (If e2 false false)
it consists of integer expressions which can also be For strict conjunction, both operands need to be defined
interpreted as boolean expressions by following the C for the overall expression to be defined. Hence, even
convention of 0 as false and anything else as true. if the first operand is false, we must still evaluate the
types Val = int — universe of values second operand.
end
Identifiers, used to name program variables, function
and predicate symbols, are simply represented by an
index into a symbol set. B S EMANTIC D OMAINS
types Ident = nat theory SemanticDomain imports Syntax begin

Though the expression language is minimal, it can be


B.1 Lifted Value Domain
used to embed first order predicate logic with function
symbols, predicate symbols and equality. In a three-valued logic, an expression can either evaluate
to a value or be undefined. In Isabelle, one typically
datatype Expr
= Undef — an expression that is always undefined models this as an option type.
| Const Val — constants syntax
| Var Ident — variables option :: type => type (-⊥ [1000] 1000)
| Fun Ident Expr — strict functions and predicates notation
| IfExpr Expr Expr Expr (If ) — non-strict if-then-else None (⊥) and Some (-) and the (-)
| AllExpr Ident Expr (All) — universal quantifier
Thus, in Val⊥ , ⊥ represents the undefined value; given a
Without loss of generality, we model functions and value v, its lifted form is represented by v. The inverse
predicates as having a single argument. Note how the of lift is represented as -.
if-then-else term constructor is named IfExpr and then
given the abbreviation If rather than directly naming the In our expression language, multiple values represent
constructor If. This has been done because If has a special true; the following predicate tests for truth over Val⊥ .
internal meaning for Isabelle which causes problems if fun isTrue :: Val⊥ ⇒ bool where
the type constructor is directly named If but it is safe to isTrue ⊥ = False | isTrue (v) = (v = false)
use as an abbreviation. This idiom is repeated here for
lemma isTrue x = (x = ⊥ ∧ x = false) by (case-tac x, auto)
AllExpr and elsewhere—e.g. falseVal and falseExpr below,
even overloading abbreviations at times.
B.2 Denotations for Functions
While we do not have a single representation for ‘true’, Total functions in our language map Vals into Vals.
we sometimes return 1 when no other value is readily Functions in Isabelle are total. We follow the common
available. idiom of modeling partial functions by making the range
constdefs an option type.
falseVal :: Val (false) falseVal == 0
types
trueVal1 :: Val (true1) trueVal1 == 1
TFunDen = Val ⇒ Val — total functions
PFunDen = Val ⇒ Val⊥ — partial functions
We define syntatic sugars for true and false (as expres-
sions) and some of the propositional connectives. In general in our expression language, a function symbol
constdefs represents a partial function. Given a function identifier,
falseExpr :: Expr (false) the following map returns the function’s denotation.
2

consts C.2 An Encoding of Truth Tables


pFunVal :: Ident ⇒ PFunDen ([[-]])
Basic sanity “tests”: essentially representing the truth
The domain of a partial function is the set of values over tables for each propositional connective.
which it is defined. lemma E 2 [[false]]s = false ∧ E 2 [[true]]s = true1 by auto
fun pFunDom :: Ident ⇒ Val set (dom) where
dom f = { v . ([[f ]] v) = ⊥} There is not much that can be said in classical two valued
logic about Undef other than it has some value.
We can “totalize” a given partial function by mapping
elements outside its domain to arbitrary values. lemma E 2 [[Undef ]]s = E 2 [[Undef ]]s by auto

fun tFunVal :: Ident ⇒ TFunDen ([[-]]total ) where lemma E 2 [[not true]]s = false
[[f ]]total v = (if v ∈ dom f then [[f ]] v else arbitrary) ∧ E 2 [[not false]]s = true1 by auto

It follows from the definition that a function matches its lemma E 2 [[false && e]]s = false
∧ E 2 [[true && true]]s = true1
totalized version for values over its domain.
∧ E 2 [[true && false]]s = false by simp
lemma v ∈ dom f =⇒ [[f ]]total v = [[f ]] v by (simp)
lemma E 2 [[true & true]]s = true1
B.3 Program State ∧ E 2 [[true & false]]s = false
∧ E 2 [[false & true]]s = false
We model program state (used to hold the values of ∧ E 2 [[false & false]]s = false by simp
program variables that occur in expressions) as a simple
lemma E 2 [[false & e]]s = false
mapping from identifiers to their values.
∧ E 2 [[e & false]]s = false by simp
types
State = Ident ⇒ Val lemma E 2 [[All i true]]s = true1 ∧ E 2 [[All i false]]s = false by auto

B.4 Syntactic sugar end

We define an Isabelle syntactic shorthand that will allow


us to make the definitions of our semantic functions D S EMANTICS FOR A T HREE - VALUED L OGIC
more readable. It is an approximation of a monadic bind
theory Semantics3 imports Syntax SemanticDomain begin
operator:
syntax
-bind :: patterns ⇒ Val option ⇒ Val ⇒ Val ((- := -;;//-) 0) D.1 Main Semantic Function
translations We now define the interpretation of expressions as for-
v := E;; E  => (let v = E in if v = ⊥ then ⊥ else E )
mulae in a three-valued logic:
In an earlier version of the theory we used syntax based fun Eval :: Expr ⇒ State ⇒ Val⊥ (E 3 [[-]]- ) where
on a real monadic bind, but unfortunately this rendered E 3 [[Undef ]]s = ⊥
the proofs more complex. | E 3 [[Const c]]s = c
| E 3 [[Var i]]s = s i
end | E 3 [[Fun f e]]s = (v := E 3 [[e]]s ;; [[f ]]v)
| E 3 [[If c t e]]s = (v := E 3 [[c]]s ;;
if v = false then E 3 [[e]]s else E 3 [[t]]s )
C C LASSICAL S EMANTICS | E 3 [[All i e]]s = (if (∃ v . E 3 [[e]]s(i:=v) = ⊥) then ⊥
theory Semantics2 imports Syntax SemanticDomain begin else if (∃ v. E 3 [[e]]s(i:=v) = false) then false
else true1)
In this section we present an interpretation of our ex-
pression language as formulae in classical two-valued
D.2 An Encoding of Truth Tables
logic.
Truth tables for the propositional connectives as inter-
preted in three-valued logic:
C.1 Main Semantic Function
declare Let-def [simp]
Since expressions contain program variables, their inter-
pretation must be done relative to a state. Note that there lemma E 3 [[false]]s = false
is no case for Undef, hence its value is left unspecified. ∧ E 3 [[true]]s = true1
∧ E 3 [[Undef ]]s = ⊥ by auto
fun Eval2 :: Expr ⇒ State ⇒ Val (E 2 [[-]]- ) where
E 2 [[Const c]]s = c lemma E 3 [[not true]]s = false
| E 2 [[Var i]]s = s i ∧ E 3 [[not false]]s = true1
| E 2 [[Fun f e]]s = [[f ]]total (E 2 [[e]]s ) ∧ E 3 [[not Undef ]]s = ⊥ by auto
| E 2 [[If c t e]]s = (if (E 2 [[c]]s ) = false then E 2 [[e]]s else E 2 [[t]]s )
| E 2 [[All i e]]s = (if (∃ v. E 2 [[e]]s(i:=v) = false) lemma E 3 [[Undef && e]]s = ⊥
then false else true1) ∧ E 3 [[false && e]]s = false
3

∧ E 3 [[true && true]]s = true1


∧ E 3 [[true && false]]s = false
∧ E 3 [[true && Undef ]]s = ⊥ by simp

lemma E 3 [[true & true]]s = true1


∧ E 3 [[true & false]]s = false
∧ E 3 [[true & Undef ]]s = ⊥
∧ E 3 [[false & true]]s = false
∧ E 3 [[false & false]]s = false
∧ E 3 [[false & Undef ]]s = ⊥ by simp

Conjunction (op &) is strict:


lemma E 3 [[Undef & e]]s = ⊥
∧ E 3 [[e & Undef ]]s = ⊥ by auto

D.3 The Definedness Predicate


The definedness predicate, as expressed in three-valued
logic, holds when a given expression evaluates to a
defined value:
fun D :: State ⇒ Expr ⇒ bool (D- -) where
Ds e = (E 3 [[e]]s = ⊥)

The following predicate holds when an expression is


defined in all states.
fun isDefAll :: Expr ⇒ bool (D∀ -) where
D∀ e = (∀ s . Ds e)

As expected, the undefined expression is never defined,


constants and variables are always defined:
lemma ¬ D∀ Undef ∧ D∀ (Const c) ∧ D∀ (Var i) by auto

Some basic results about quantifiers:


lemma E 3 [[All i Undef ]]s = ⊥
∧ E 3 [[All i true]]s = true1
∧ E 3 [[All i false]]s = false by auto

end

E E QUIVALENCE
theory Equiv imports Semantics2 Semantics3 begin

This theory presents our main theorem, namely, that if an


expression is defined, then interpreting it in a 2-valued
or 3-valued logic yields the same result.

theorem soundness: ∀ s . Ds e −→ E 3 [[e]]s = E 2 [[e]]s 


proof (induct e)
case Undef show ?case by simp
next case Const show ?case by simp
next case Var show ?case by simp
next case (Fun i e) thus ?case by auto
next case (IfExpr c t e) thus  ?case by auto
next case (AllExpr i e) thus i e.
∀ s. Ds e −→ E 3 [[e]]s = E 2 [[e]]s  =⇒
∀ s. Ds (All i e) −→ E 3 [[All i e]]s = E 2 [[All i e]]s 
by (auto, rule-tac x=v in exI, auto)
qed

In fact, if the 2-valued and 3-valued interpretations of a


formula coincide, then the formula must be defined.
theorem completeness: ∀ s . E 3 [[e]]s = E 2 [[e]]s  −→ Ds e
by (induct e, auto)
end

You might also like