Kleene Algebra with Tests and Demonic ... - Semantic Scholar

Report 3 Downloads 136 Views
Kleene Algebra with Tests and Demonic Refinement Algebras Alasdair Armstrong

Victor B. F. Gomes

Georg Struth

September 19, 2015

Abstract We formalise Kleene algebra with tests (KAT) and demonic refinement algebra (DRA) in Isabelle/HOL. KAT is relevant for program verification and correctness proofs in the partial correctness setting. While DRA targets similar applications in the context of total correctness. Our formalisation contains the two most important models of these algebras: binary relations in the case of KAT and predicate transformers in the case of DRA. In addition, we derive the inference rules for Hoare logic in KAT and its relational model and present a simple formally verified program verification tool prototype based on the algebraic approach.

Contents 1 Basis for Demonic Refinement Algebras

2

2 Demonic Refinement Algebras

4

3 Test Dioids

10

4 Kleene Algebra with Tests

21

5 Demonic Refinement Algebra with Tests

23

6 Models for Demonic Refinement Algebra with Tests

28

7 Models for Kleene Algebra with Tests

31

8 Pre-Conway Algebra

32

9 Transformation Theorem for while Loops

35

10 Hoare Logic

38

1

11 Verification Tool Prototype

39

12 Two sorted Kleene Algebra with Tests

42

13 Two sorted Demonic Refinement Algebras

47

1

Basis for Demonic Refinement Algebras

theory DRA-Base imports ../Kleene-Algebra/Kleene-Algebra begin

Demonic refinement algebra is based on a Kleene algebra without the right annihilation law x · 0 = 0. In the Archive [1], only left Kleene algebras without the right annihilation law exist. So we need to define an expansion. class kleene-algebra-zerol = left-kleene-algebra-zerol + assumes star-inductr : z + y · x ≤ y −→ z · x ? ≤ y begin

These lemmas were copied from AFP (Kleene Algebra). They are also valid without right annihilation. lemma star-inductr-var : y · x ≤ y =⇒ y · x ? ≤ y by (metis add-lub order-refl star-inductr ) lemma star-inductr-var-equiv : y · x ≤ y ←→ y · x ? ≤ y by (metis order-trans mult-isol star-ext star-inductr-var ) lemma star-sim2 : z · x ≤ y · z =⇒ z · x ? ≤ y ? · z proof − assume z · x ≤ y · z hence y ? · z · x ≤ y ? · y · z by (metis distrib-left less-eq-def mult.assoc) also have ... ≤ y ? · z by (metis (full-types) mult-isor star-1l star-slide-var ) hence z + y ? · z · x ≤ y ? · z by (metis add-lub-var calculation mult-isor mult-onel order-trans star-ref ) thus z · x ? ≤ y ? · z by (metis mult.assoc star-inductr ) qed lemma star-sim3 : z · x = y · z =⇒ z · x ? = y ? · z by (metis eq-iff star-sim1 star-sim2 ) lemma star-sim4 : x · y ≤ y · x =⇒ x ? · y ? ≤ y ? · x ? by (metis star-sim1 star-sim2 ) lemma star-inductr-eq: z + y · x = y =⇒ z · x ? ≤ y by (metis eq-iff star-inductr )

2

lemma star-inductr-var-eq: y · x = y =⇒ y · x ? ≤ y by (metis eq-iff star-inductr-var ) lemma star-inductr-var-eq2 : y · x = y =⇒ y · x ? = y by (metis mult-onel star-one star-sim3 ) lemma bubble-sort: y · x ≤ x · y =⇒ (x + y)? = x ? · y ? by (metis church-rosser star-sim4 ) lemma independence1 : x · y = 0 =⇒ x ? · y = y proof − assume x · y = 0 also have x ? · y = y + x ? · x · y by (metis distrib-right mult-onel star-unfoldr-eq) thus x ? · y = y by (metis add-0-left add .commute add-ub1 calculation eq-iff star-inductl-eq) qed lemma independence2 : x · y = 0 =⇒ x · y ? = x by (metis annil mult-onel star-sim3 star-zero) lemma lazycomm-var : y · x ≤ x · (x + y)? + y ←→ y · x ? ≤ x · (x + y)? + y proof let ?t = x · (x + y)? assume y · x ≤ ?t + y also have (?t + y) · x = ?t · x + y · x by (metis distrib-right) moreover have ... ≤ ?t · x + ?t + y by (metis add-iso-var calculation le-less add .assoc) moreover have ... ≤ ?t + y by (metis add-iso-var add-lub-var mult.assoc mult-isol order-refl prod-star-closure star-subdist-var-1 ) hence y + (?t + y) · x ≤ ?t + y by (metis add .commute add-lub-var add-ub1 calculation less-eq-def mult.assoc distrib-left star-subdist-var-1 star-trans-eq) thus y · x ? ≤ x · (x + y)? + y by (metis star-inductr ) next assume y · x ? ≤ x · (x + y)? + y thus y · x ≤ x · (x + y)? + y by (metis mult-isol order-trans star-ext) qed lemma arden-var : (∀ y v . y ≤ x · y + v −→ y ≤ x ? · v ) =⇒ z = x · z + w −→ z = x? · w by (metis add-comm eq-iff star-inductl-eq) lemma (∀ x y. y ≤ x · y −→ y = 0 ) =⇒ y ≤ x · y + z =⇒ y ≤ x ? · z

3

by (metis eq-refl mult-onel ) end end

2

Demonic Refinement Algebras

theory DRA imports DRA-Base begin

A demonic refinement algebra [8] is a Kleene algebra without right annihilation plus an operation for possibly infinite iteration. class dra = kleene-algebra-zerol + fixes strong-iteration :: 0a ⇒ 0a (-∞ [101 ] 100 ) assumes iteration-unfoldl [simp] : 1 + x · x ∞ = x ∞ and coinduction: y ≤ z + x · y −→ y ≤ x ∞ · z and isolation [simp]: x ? + x ∞ ·0 = x ∞ begin

> is an abort statement, defined as an infinite skip. It is the maximal element of any DRA. abbreviation top-elem :: 0a (>) where > ≡ 1 ∞

Simple/basic lemmas about iteration operator lemma iteration-refl : 1 ≤ x ∞ by (metis add-ub1 iteration-unfoldl ) lemma top-ref : x ≤ > by (metis add-idem 0 add-lub add-ub1 mult-1-left mult-1-right coinduction) lemma top-mult-annil [simp]: >·x = > by (metis add-ub2 eq-iff mult-1-left coinduction top-ref ) lemma top-add-annil [simp]: > + x = > by (metis add .commute less-eq-def top-ref ) lemma top-elim: x ·y ≤ x ·> by (metis mult-isol top-ref ) lemma iteration-unfoldl-distl : y·x ∞ = y + y·x ·x ∞ by (metis distrib-left mult.assoc mult-oner iteration-unfoldl ) lemma iteration-unfoldl-distr : x ∞ ·y = y + x ·x ∞ ·y by (metis distrib-right 0 mult-1-left iteration-unfoldl ) lemma iteration-unfoldl 0: z ·x ∞ ·y = z ·y + z ·x ·x ∞ ·y by (metis distrib-left mult.assoc iteration-unfoldl-distr )

4

lemma iteration-idem[simp]: x ∞ ·x ∞ = x ∞ by (metis add-zerol annil isolation mult.assoc iteration-refl iteration-unfoldl-distr star-inductl-var-eq2 star-invol star-sum-unfold sup-id-star1 ) lemma iteration-induct: x ·x ∞ ≤ x ∞ ·x by (metis eq-iff mult.assoc coinduction iteration-unfoldl-distl ) lemma iteration-ref-star : x ? ≤ x ∞ by (metis eq-refl iteration-unfoldl star-inductl-one) lemma iteration-subdist: x ∞ ≤ (x + y)∞ by (metis add-assoc 0 distrib-right 0 mult-oner coinduction add-ub1 iteration-unfoldl ) lemma iteration-iso: x ≤ y =⇒ x ∞ ≤ y ∞ by (metis iteration-subdist order-prop) lemma iteration-unfoldr : 1 + x ∞ · x = x ∞ by (metis add-0-left annil eq-refl isolation mult.assoc iteration-idem iteration-unfoldl iteration-unfoldl-distr star-denest star-one star-prod-unfold star-slide tc) lemma iteration-unfoldr-distl : y·x ∞ = y + y·x ∞ ·x by (metis distrib-left mult.assoc mult-oner iteration-unfoldr ) lemma iteration-unfoldr-distr : x ∞ ·y = y + x ∞ ·x ·y by (metis iteration-unfoldl-distr iteration-unfoldr-distl ) lemma iteration-unfold-eq: x ∞ ·x = x ·x ∞ by (metis iteration-unfoldl-distr iteration-unfoldr-distl ) lemma iteration-unfoldr 0: z ·x ∞ ·y = z ·y + z ·x ∞ ·x ·y by (metis distrib-left mult.assoc iteration-unfoldr-distr ) lemma iteration-double[simp]: (x ∞ )∞ = > by (metis less-eq-def iteration-iso iteration-refl top-add-annil ) lemma star-iteration[simp]: (x ? )∞ = > by (metis less-eq-def iteration-iso star-ref top-add-annil ) lemma iteration-star [simp]: (x ∞ )? = x ∞ by (metis boffa less-eq-def iteration-idem iteration-refl ) lemma iteration-star2 [simp]: x ? ·x ∞ = x ∞ by (metis add .assoc boffa isolation mult-1-right iteration-idem iteration-unfoldl star-denest star-denest-var-2 star-invol star-slide star-zero) lemma iteration-zero[simp]: 0 ∞ = 1 by (metis add-zeror annil iteration-unfoldl )

5

lemma iteration-annil [simp]: (x ·0 )∞ = 1 + x ·0 by (metis annil iteration-unfoldl mult.assoc) lemma iteration-subdenest: x ∞ ·y ∞ ≤ (x + y)∞ by (metis add .commute mult-isol-var iteration-idem iteration-subdist) lemma sup-id-top: 1 ≤ y =⇒ y · > = > by (metis antisym-conv iteration-unfold-eq mult-isol-var top-mult-annil top-ref ) lemma iteration-top[simp]: x ∞ ·> = > by (metis iteration-refl sup-id-top)

Next, we prove some simulation laws for data refinement. lemma iteration-sim: z ·y ≤ x ·z =⇒ z ·y ∞ ≤ x ∞ ·z proof − assume assms: z ·y ≤ x ·z have z ·y ∞ = z + z ·y·y ∞ by (metis distrib-left mult.assoc mult-oner iteration-unfoldl ) also have ... ≤ z + x ·z ·y ∞ by (metis assms add .commute add-iso mult-isor ) finally show z ·y ∞ ≤ x ∞ ·z by (metis mult.assoc coinduction) qed

Nitpick gives a counterexample to the dual simulation law. lemma y·z ≤ z ·x =⇒ y ∞ ·z ≤ z ·x ∞ oops

Next, we prove some sliding laws. lemma iteration-slide-var : x ·(y·x )∞ ≤ (x ·y)∞ ·x by (metis eq-refl iteration-sim mult.assoc) lemma iteration-prod-unfold : (y·x )∞ = 1 + y·(x ·y)∞ ·x apply (rule antisym, metis iteration-unfoldl add-iso-var eq-refl iteration-slide-var mult.assoc mult-isol ) by (metis add-iso-var iteration-refl iteration-slide-var iteration-unfoldr iteration-zero mult.assoc mult-isol mult-isol-var mult-oner ) lemma iteration-slide: x ·(y·x )∞ = (x ·y)∞ ·x by (metis iteration-prod-unfold iteration-unfoldl-distr distrib-left mult-1-right mult.assoc)

Nitpick refutes the next lemma. lemma (x ? ·y ? )∞ = (x ? ·y)∞ oops lemma star-iteration-slide: (x ? ·y)∞ = y ? ·(x ? ·y)∞ proof − have y ? ·(x ? ·y)∞ ≤ 1 + (x ? ·y)·(x ? ·y)∞ + x ? ·y·y ? ·(x ? ·y)∞ by (metis star-unfoldl-eq distrib-right 0 eq-refl iteration-unfoldl star-ref mult-1-left mult-isor add-iso-var )

6

hence y ? ·(x ? ·y)∞ ≤ 1 + x ? ·y·y ? ·(x ? ·y)∞ by (metis less-eq-def add .assoc distrib-left distrib-right mult-1-left mult.assoc star-ref ) thus ?thesis by (metis mult-1-right mult.assoc coinduction star-ref mult-1-left mult-isor add .commute less-eq-def ) qed

The following laws are called denesting laws. lemma iteration-sub-denest: (x + y)∞ ≤ x ∞ ·(y·x ∞ )∞ proof − have (x + y)∞ = x ·(x + y)∞ + y·(x + y)∞ + 1 by (metis add .commute distrib-right 0 iteration-unfoldl ) hence (x + y)∞ ≤ x ∞ ·(y·(x + y)∞ + 1 ) by (metis add-assoc 0 add-lub-var add-ub1 add-ub2 coinduction) moreover hence x ∞ ·(y·(x + y)∞ + 1 ) ≤ x ∞ ·(y·x ∞ )∞ by (metis add-iso mult.assoc mult-isol add .commute coinduction mult-oner mult-isol ) ultimately show ?thesis by (metis dual-order .trans) qed lemma iteration-denest: (x + y)∞ = x ∞ ·(y·x ∞ )∞ proof − have x ∞ ·(y·x ∞ )∞ ≤ x ·x ∞ ·(y·x ∞ )∞ + y·x ∞ ·(y·x ∞ )∞ + 1 by (metis add .commute iteration-unfoldl-distr add-assoc 0 add .commute iteration-unfoldl order-refl ) thus ?thesis by (metis add .commute iteration-sub-denest order .antisym coinduction distrib-right 0 iteration-sub-denest mult.assoc mult-oner order .antisym) qed lemma iteration-denest2 : (x + y)∞ = y ? ·x ·(x + y)∞ + y ∞ proof − have (x + y)∞ = y ∞ ·x ·(y ∞ ·x )∞ ·y ∞ + y ∞ by (metis add .commute iteration-denest iteration-slide iteration-unfoldl-distr ) also have ... = y ? ·x ·(y ∞ ·x )∞ ·y ∞ + y ∞ ·0 + y ∞ by (metis isolation mult.assoc distrib-right 0 annil mult.assoc) also have ... = y ? ·x ·(y ∞ ·x )∞ ·y ∞ + y ∞ by (metis add .assoc distrib-left mult-1-right add-0-left mult-1-right) finally show ?thesis by (metis add .commute iteration-denest iteration-slide mult.assoc) qed lemma iteration-denest3 : (x + y)∞ = (y ? ·x )∞ ·y ∞ apply (rule antisym, metis add .commute iteration-denest2 eq-refl coinduction) by (metis add .commute iteration-denest iteration-slide mult-isor iteration-iso iteration-ref-star )

Now we prove separation laws for reasoning about distributed systems 7

in the framework of action systems. lemma iteration-sep: y·x ≤ x ·y =⇒ (x + y)∞ = x ∞ ·y ∞ proof − assume y·x ≤ x ·y hence y ? ·x ≤ x ·(x + y)? by (metis star-sim1 add .commute mult-isol order-trans star-subdist) hence y ? ·x ·(x + y)∞ + y ∞ ≤ x ·(x + y)∞ + y ∞ by (metis mult-isor mult.assoc iteration-star2 add-iso-var eq-refl ) thus ?thesis by (metis iteration-denest2 add .commute coinduction add .commute less-eq-def iteration-subdenest) qed lemma iteration-sim2 : y·x ≤ x ·y =⇒ y ∞ ·x ∞ ≤ x ∞ ·y ∞ by (metis add .commute iteration-sep iteration-subdenest) lemma iteration-sep2 : y·x ≤ x ·y ? =⇒ (x + y)∞ = x ∞ ·y ∞ proof − assume y·x ≤ x ·y ? hence y ? ·(y ? ·x )∞ ·y ∞ ≤ x ∞ ·y ? ·y ∞ by (metis mult.assoc mult-isor iteration-sim star-denest-var-2 star-sim1 star-slide-var star-trans-eq tc-eq) moreover have x ∞ ·y ? ·y ∞ ≤ x ∞ ·y ∞ by (metis eq-refl mult.assoc iteration-star2 ) moreover have (y ? ·x )∞ ·y ∞ ≤ y ? ·(y ? ·x )∞ ·y ∞ by (metis mult-isor mult-onel star-ref ) ultimately show ?thesis by (metis antisym iteration-denest3 iteration-subdenest order-trans) qed lemma iteration-sep3 : y·x ≤ x ·(x + y) =⇒ (x + y)∞ = x ∞ ·y ∞ proof − assume y·x ≤ x ·(x + y) hence y ? ·x ≤ x ·(x + y)? by (metis star-sim1 ) hence y ? ·x ·(x + y)∞ + y ∞ ≤ x ·(x + y)? ·(x + y)∞ + y ∞ by (metis add-iso mult-isor ) hence (x + y)∞ ≤ x ∞ ·y ∞ by (metis mult.assoc iteration-denest2 iteration-star2 add .commute coinduction) thus ?thesis by (metis add .commute less-eq-def iteration-subdenest) qed lemma iteration-sep4 : [[y·0 = 0 ; z ·x = 0 ; y·x ≤ (x + z )·y ? ]] =⇒ (x + y + z )∞ = x ∞ ·(y + z )∞ proof − assume assms: y·0 = 0 z ·x = 0 y·x ≤ (x + z )·y ? have y·y ? ·z ≤ y ? ·z ·y ? by (metis mult-isor star-1l mult-oner order-trans star-plus-one subdistl )

8

have y ? ·z ·x ≤ x ·y ? ·z by (metis zero-least assms(1 ) assms(2 ) independence1 mult.assoc) have y·(x + y ? ·z ) ≤ (x + z )·y ? + y·y ? ·z by (metis assms(3 ) distrib-left mult.assoc add-iso) also have ... ≤ (x + y ? ·z )·y ? + y·y ? ·z by (metis star-ref add-iso-var eq-refl mult-1-left mult-isor ) also have ... ≤ (x + y ? ·z )·y ? + y ? ·z ·y ? using hy·y ? ·z ≤ y ? ·z ·y ? i by (metis add .commute add-iso) finally have y·(x + y ? ·z ) ≤ (x + y ? ·z )·y ? by (metis add .commute add-idem 0 add .left-commute distrib-right) moreover have (x + y + z )∞ ≤ (x + y + y ? ·z )∞ by (metis star-ref add-iso-var eq-refl mult-1-left mult-isor iteration-iso) moreover have ... = (x + y ? ·z )∞ ·y ∞ by (metis add .assoc add .commute calculation iteration-sep2 ) moreover have ... = x ∞ ·(y ? ·z )∞ ·y ∞ using hy ? ·z ·x ≤ x ·y ? ·z i by (metis iteration-sep mult.assoc) ultimately have (x + y + z )∞ ≤ x ∞ ·(y + z )∞ by (metis add .commute mult.assoc iteration-denest3 ) thus ?thesis by (metis add .commute add .left-commute less-eq-def iteration-subdenest) qed

Finally, we prove some blocking laws. Nitpick refutes the next lemma. lemma x ·y = 0 =⇒ x ∞ ·y = y oops lemma iteration-idep: x ·y = 0 =⇒ x ·y ∞ = x by (metis add-zeror annil iteration-unfoldl-distl )

Nitpick refutes the next lemma. lemma y·w ≤ x ·y + z =⇒ y·w ∞ ≤ x ∞ ·z oops

At the end of this file, we consider a data refinement example from von Wright [8]. lemma data-refinement: assumes s 0 ≤ s·z z ·e 0 ≤ e z ·a 0 ≤ a·z z ·b ≤ z b ∞ = b ? shows s 0·(a 0 + b)∞ ·e 0 ≤ s·a ∞ ·e proof − have z ·b ? ≤ z by (metis assms(4 ) star-inductr-var ) have (z ·a 0)·b ? ≤ (a·z )·b ? by (metis assms(3 ) mult.assoc mult-isor ) hence z ·(a 0·b ? )∞ ≤ a ∞ ·z using hz ·b ? ≤ z i by (metis mult.assoc mult-isol order-trans iteration-sim mult.assoc) have s 0·(a 0 + b)∞ ·e 0 ≤ s 0·b ? ·(a 0·b ? )∞ ·e 0 by (metis add .commute assms(5 ) eq-refl iteration-denest mult.assoc) also have ... ≤ s·z ·b ? ·(a 0·b ? )∞ ·e 0

9

by (metis assms(1 ) mult-isor ) also have ... ≤ s·z ·(a 0·b ? )∞ ·e 0 using hz ·b ? ≤ z i by (metis mult.assoc mult-isol mult-isor ) also have ... ≤ s·a ∞ ·z ·e 0 using hz ·(a 0·b ? )∞ ≤ a ∞ ·z i by (metis mult.assoc mult-isol mult-isor ) finally show ?thesis by (metis assms(2 ) mult.assoc mult-isol mult.assoc mult-isol order-trans) qed

end end

3

Test Dioids

theory Test-Dioids imports ../../Kleene-Algebra/Dioid begin

Tests are embedded in a weak dioid, a dioid without the right annihilation and left distributivity axioms, using an operator t defined by a complementation operator. This allows us to use tests in weak settings, such as Probabilistic Kleene Algebra and Demonic Refinement Algebra. class near-dioid-tests-zerol = ab-near-semiring-one-zerol + plus-ord + fixes comp-op :: 0a ⇒ 0a (n- [90 ] 91 ) assumes test-one: nn1 =1 and test-mult: n n (n n x · n n y) = n n y · n n x and test-mult-comp: n x · n n x = 0 and test-de-morgan: n x + n y = n (n n x · n n y) begin lemma add-idem 0 [simp]: x + x = x by (metis annil distrib-right 0 mult-1-left test-de-morgan test-mult-comp test-one) subclass near-dioid-one-zerol by (unfold-locales, simp) lemma x · (y + z ) = x · y + x · z oops lemma n n x · (y + z ) = n n x · y + n n x · z oops

A test operator t can then be defined as an abbreviation of applying n twice. The elements of the image, t(K), form a Boolean algebra, but we do not express this in Isabelle. Instead, we prove all the obvious laws of Boolean algebra. 10

abbreviation test-operator :: 0a ⇒ 0a (t- [100 ] 101 ) where t x ≡ n (n x ) lemma test-zero[simp]: t 0 = 0 by (metis mult-1-left test-mult-comp test-one) lemma test-distrib-left: t x · (t y + t z ) = (t x · t y) + (t x · t z ) by (metis add .commute distrib-right 0 test-de-morgan test-mult) lemma test-distrib-right: (t x + t y) · t z = (t x · t z ) + (t y · t z ) by (metis distrib-right 0) lemma test-mult-idem[simp]: t x · t x = t x by (metis add-0-right test-distrib-left mult-1-right test-de-morgan test-mult-comp test-one) lemma test-idem[simp]: t t x = t x by (metis add-idem 0 test-de-morgan test-mult-idem) lemma test-add-closed [simp]: t (t x + t y) = t x + t y by (metis add .commute test-de-morgan test-mult) lemma test-mult-comm: t x · t y = t y · t x by (metis test-mult test-idem) lemma test-add-comm: t x + t y = t y + t x by (metis add-comm) lemma test-mult-assoc: t x · (t y · t z ) = (t x · t y) · t z by (metis mult.assoc) lemma test-add-assoc: t x + (t y + t z ) = (t x + t y) + t z by (metis add .assoc) lemma test-add-comp: n x + t x = 1 by (metis test-de-morgan test-mult-comp test-one mult-1-left) lemma n x · t x = 0 by (metis test-mult-comp) lemma test-mult-lb1 : t x · t y ≤ t x by (metis add .commute add-ub1 mult-1-left mult-isor test-add-comp test-de-morgan test-mult) lemma test-mult-lb2 : t x · t y ≤ t y by (metis test-mult-comm test-mult-lb1 ) lemma test-add-lb: t x · (t x + t y) = t x by (metis add .commute less-eq-def test-distrib-left test-mult-idem test-mult-lb1 )

11

lemma test-leq-mult-def : (t x ≤ t y) = (t x · t y = t x ) by (metis less-eq-def test-add-lb test-mult-comm test-mult-lb1 ) lemma test-mult-glbI : [[t z ≤ t x ; t z ≤ t y]] =⇒ t z ≤ t x · t y by (metis mult-isor test-leq-mult-def ) lemma test-mult-glb: t z ≤ t x ∧ t z ≤ t y ←→ t z ≤ t x · t y by (metis (full-types) order-trans test-mult-glbI test-mult-lb1 test-mult-lb2 ) lemma test-add-distl : (t x · t y) + t z = (t x + t z ) · (t y + t z ) proof (rule antisym) have t x · t y ≤ (t x + t z ) · (t y + t z ) by (metis add-lub mult-isor order-prop test-distrib-left) thus t x · t y + t z ≤ (t x + t z ) · (t y + t z ) by (metis add-lub-var add-ub2 distrib-right 0 test-add-comm test-add-lb) next show (t x + t z ) · (t y + t z ) ≤ t x · t y + t z by (metis add .commute test-add-lb test-de-morgan test-distrib-left test-mult test-mult-lb2 ) qed lemma test-add-distr : t x + (t y · t z ) = (t x + t y) · (t x + t z ) by (metis add-comm test-add-distl ) lemma test-add-zerol : 0 + t x = t x by (metis add-zerol ) lemma test-add-zeror : t x + 0 = t x by (metis add-zeror ) lemma test-mult-onel : 1 · t x = t x by (metis mult-onel ) lemma test-mult-oner : t x · 1 = t x by (metis mult-oner ) lemma test-lb: t x ≥ 0 by (metis zero-least) lemma test-ub: t x ≤ 1 by (metis add-ub1 test-add-comp)

A test is an element p where t p = p. definition test :: 0a ⇒ bool where test p ≡ t p = p notation comp-op (!- [101 ] 100 )

12

lemma test-add-closed-var : [[test p; test q]] =⇒ test (p + q) by (metis test-add-closed test-def ) lemma test-mult-closed : [[test p; test q]] =⇒ test (p · q) by (metis test-def test-mult test-mult-comm) lemma test-comp-closed : test p =⇒ test (!p) by (metis test-def ) lemma test-ub-var : test p =⇒ p ≤ 1 by (metis test-def test-ub) lemma test-lb-var : test p =⇒ p ≥ 0 by (metis zero-least) lemma test-zero-var : test 0 by (metis test-def test-zero) lemma test-one-var : test 1 by (metis test-def test-one) lemma test-not-one: !1 = 0 by (metis mult-oner test-mult-comp test-one) lemma test-add-idem: test p =⇒ p + p = p by (metis add-idem 0) lemma test-mult-idem-var [simp]: test p =⇒ p · p = p by (metis test-def test-mult-idem) lemma test-add-comm-var : [[test p; test q]] =⇒ p + q = q + p by (metis add .commute) lemma test-mult-comm-var : [[test p; test q]] =⇒ p · q = q · p by (metis test-def test-mult-comm) lemma test-distrib-left-var : [[test p; test q; test r ]] =⇒ p·(q + r ) = p·q + p·r by (metis distrib-right 0 test-add-closed-var test-mult-comm-var ) lemma test-distrib-right-var : [[test p; test q; test r ]] =⇒ (p + q)·r = p·r + q·r by (metis distrib-right 0) lemma test-add-distl-var : [[test p; test q; test r ]] =⇒ p·q + r = (p + r )·(q + r ) using test-add-distl [of p q r ] by (simp add : test-def ) lemma test-add-distr-var : [[test p; test q; test r ]] =⇒ p + q·r = (p + q)·(p + r ) by (metis add-comm test-add-distl-var ) lemma test-absorb1 : [[test p; test q]] =⇒ p + p · q = p

13

by (metis test-add-distr-var test-add-idem test-add-lb test-def ) lemma test-absorb2 : [[test p; test q]] =⇒ p · (p + q) = p by (metis test-distrib-left-var test-mult-idem-var test-absorb1 ) lemma test-absorb3 : [[test p; test q]] =⇒ (p + q) · q = q by (metis add .commute test-absorb2 test-add-closed-var test-mult-comm-var ) lemma test-leq-mult-def-var : [[test p; test q]] =⇒ (p ≤ q) = (p · q = p) by (metis add .commute less-eq-def test-absorb1 test-absorb2 test-mult-comm-var ) lemma test-double-comp-var : test p =⇒ p = !(!p) by (metis test-def ) lemma test-comp: test p =⇒ ∃ q. test q ∧ p + q = 1 ∧ p · q = 0 by (metis test-add-comp test-def test-mult-comp) lemma test-dist-var : [[test p; test q]] =⇒ (test r ∧ r · p = r · q ∧ r + p = r + q −→ p = q) by (metis add .commute test-absorb1 test-add-distr-var test-mult-comm-var ) lemma test-comp-uniq: test p =⇒ ∃ !q. test q ∧ p + q = 1 ∧ p · q = 0 by (safe, metis test-comp, metis test-dist-var ) lemma test-comp-mult [simp]: test p =⇒ p · !p = 0 by (metis test-double-comp-var test-mult-comp) lemma test-comp-mult2 [simp]: test p =⇒ !p · p = 0 by (metis test-double-comp-var test-mult-comp) lemma test-comp-add [simp]: test p =⇒ p + !p = 1 by (metis test-double-comp-var test-add-comp) lemma test-comp-closed-var : test p =⇒ test (!p) by (metis test-def ) lemma de-morgan1 : [[test p; test q]] =⇒ !p + !q = !(p · q) by (metis test-de-morgan test-def ) lemma de-morgan2 : [[test p; test q]] =⇒ !p · !q = !(p + q) by (metis add .commute add-idem 0 opp-mult-def test-de-morgan test-double-comp-var test-mult) lemma de-morgan3 : [[test p; test q]] =⇒ !(!p + !q) = p · q by (metis de-morgan1 test-double-comp-var test-mult-closed ) lemma de-morgan4 : [[test p; test q]] =⇒ !(!p · !q) = p + q by (metis de-morgan2 test-add-closed-var test-def )

14

lemma test-comp-anti : [[test p; test q]] =⇒ (p ≤ q) = (!q ≤ !p) by (metis add .commute de-morgan1 test-double-comp-var test-mult-closed less-eq-def test-leq-mult-def ) lemma ba-1 : [[test p; test q; test r ]] =⇒ p + q + !q = r + !r by (metis add .assoc mult-onel test-absorb1 test-add-comp test-def test-one-var ) lemma ba2 : [[test p; test q]] =⇒ p + p = p + !(q + !q) by (metis add-idem add-zeror ba-1 test-not-one test-one-var ) lemma ba3 : [[test p; test q]] =⇒ p = (p · q) + (p · !q) by (metis test-distrib-left-var mult-oner test-add-comp test-def ) lemma ba4 : [[test p; test q]] =⇒ p = (p + !q) · (p + q) by (metis add .commute add-zerol test-add-distr-var test-comp-mult test-def ) lemma ba5 : [[test p; test q]] =⇒ (p + q) · !p = q · !p by (metis distrib-right 0 test-comp-mult add-zerol ) lemma ba6 : test p =⇒ !p · p = 0 by (metis test-def test-mult-comp) lemma ba7 : [[test p; test q]] =⇒ !p = !(p + q) + !(p + !q) by (metis ba3 de-morgan2 test-comp-closed-var test-double-comp-var ) lemma test-restrictl : test p =⇒ p · x ≤ x by (metis distrib-right 0 mult-onel order-prop test-comp-uniq)

Nitpick refutes the next lemma. lemma test-restrictr : test p =⇒ x · p ≤ x oops lemma [[test p; test q; test r ]] =⇒ p·q ≤ r ←→ p ≤ !q + r proof auto assume assms: test p test q test r p·q ≤ r hence p ≤ r + p ·!q by (metis add-iso ba3 distrib-left distrib-left) thus p ≤ r + !q by (metis add-iso-var assms(1 ) dual-order .trans order-refl test-restrictl ) next assume test p test q test r p ≤ r + !q thus p · q ≤ r by (metis add .commute mult-isor distrib-right 0 add-zeror test-comp-mult2 order-trans test-mult-comm-var test-restrictl ) qed

Next, we prove lemmas mixing the embedded tests and any element of the carrier set. lemma test-eq1 : test p =⇒ y ≤ x ←→ p·y ≤ x ∧ !p·y ≤ x

15

apply standard apply (metis order-trans test-comp-closed-var test-restrictl ) apply (metis add-iso-var add-idem 0 distrib-right 0 mult-onel test-comp-add ) done

Nitpick refutes the next four lemmas. lemma test-eq2 : test p =⇒ z ≤ p·x + !p·y ←→ p·z ≤ p·x ∧ !p·z ≤ !p·y oops lemma test-eq3 : [[test p; test q]] =⇒ p·x = p·x ·q ←→ p·x ≤ x ·q oops lemma test1 : [[test p; test q; p·x ·!q = 0 ]] =⇒ p·x = p·x ·q oops lemma [[test p; test q; x ·!q = !p·x ·!q]] =⇒ p·x = p·x ·q oops lemma test-eq4 : [[test p; test q]] =⇒ x ·!q = !p·x ·!q ←→ p·x ·!q = 0 apply standard apply (metis annil mult.assoc test-comp-mult) apply (metis add-zerol distrib-right 0 mult-onel test-comp-add ) done lemma test2 : [[test p; test q]] =⇒ p·q·p = p·q by (metis mult.assoc test-mult-comm-var test-mult-idem-var ) lemma test3 : [[test p; test q]] =⇒ p·q·!p = 0 by (metis ba5 test-absorb1 test-comp-mult test-mult-closed ) lemma test4 : [[test p; test q]] =⇒ !p·q·p = 0 by (metis annil ba6 mult.assoc test-mult-comm-var )

Nitpick refutes the next two lemmas. lemma comm-add : [[test p; p·x = x ·p; p·y = y·p]] =⇒ p·(x + y) = (x + y)·p oops lemma comm-add-var : [[test p; test q; test r ; p·x = x ·p; p·y = y·p]] =⇒ p·(q·x + r ·y) = (q·x + r ·y)·p oops lemma comm-mult: [[test p; test q; q·x = x ·q]] =⇒ p·q·x = q·p·x by (metis mult.assoc test-mult-comm-var ) lemma de-morgan-var1 : [[test p; test q; test r ]] =⇒ (!p + q)·(p + r ) = p·q + !p·r proof − assume tests: test p test q test r hence (!p + q)·(p + r ) = !p·p + !p·r + q·p + r ·q by (metis add-assoc 0 distrib-right 0 test-comp-closed-var test-distrib-left-var test-mult-comm-var )

16

also have ... = !p·r + q·p + (p + !p)·r ·q by (metis add-zerol mult-onel test-comp-add test-comp-mult2 tests(1 )) also have ... = !p·r + q·p + p·r ·q + !p·r ·q by (metis add-assoc 0 test-comp-closed-var test-distrib-right-var test-mult-closed tests) also have ... = !p·r + p·q + p·q·r + !p·r ·q by (metis mult.assoc test-mult-comm-var tests) also have ... = !p·r ·1 + !p·r ·q + p·q·1 + p·q·r by (metis add .commute add .left-commute mult-oner ) also have ... = !p·r ·(1 + q) + p·q·(1 + r ) by (metis add-assoc 0 test-comp-closed-var test-distrib-left-var test-mult-closed test-one-var tests) finally show ?thesis by (metis add .commute less-eq-def mult-oner test-ub-var tests(2 −3 )) qed lemma de-morgan-var2 : [[test p; test q; test r ]] =⇒ !(p·q + !p·r ) = (p·!q + !p·!r ) by (metis de-morgan1 de-morgan2 de-morgan-var1 test-def )

Nitpick refutes the next two lemmas. lemma test p =⇒ p · x = x · p =⇒ p · x = p · x · p ∧ !p · x = !p · x · !p oops lemma test-distrib: [[test p; test q]] =⇒ (p + q)·(q·y + !q·x ) = q·y + !q·p·x oops end

We now make the assumption that tests distribute over finite sums of arbitrary elements from the left. This can be justified in models such as multirelations and probabilistic predicate transformers. class near-dioid-test-zerol-dist = near-dioid-tests-zerol + assumes weak-distrib-left: t x · (y + z ) = t x · y + t x · z begin lemma weak-distrib-left-var : test p =⇒ p · (x + y) = p · x + p · y by (metis weak-distrib-left test-double-comp-var ) lemma weak-subdistl : test p =⇒ p · x ≤ p · (x + y) by (metis order-prop weak-distrib-left-var ) lemma weak-subdistl-var : test p =⇒ p · x + p · y ≤ p · (x + y) by (metis add .commute add-lub weak-subdistl ) lemma weak-mult-isol : test p =⇒ x ≤ y −→ p · x ≤ p · y by (metis less-eq-def weak-subdistl ) lemma weak-mult-isol-var : [[test p; test q]] =⇒ p ≤ x ∧ q ≤ y −→ p · q ≤ x · y by (metis weak-mult-isol mult-isor order-trans)

17

lemma weak-mult-double-iso: test p =⇒ x ≤ y −→ p · x · z ≤ p · y · z by (metis weak-mult-isol mult-isor )

Nitpick refutes the next lemma. lemma test-restrictr : test p =⇒ x · p ≤ x oops lemma test-eq2 : test p =⇒ z ≤ p·x + !p·y ←→ p·z ≤ p·x ∧ !p·z ≤ !p·y proof auto assume assms: test p z ≤ p·x + !p·y hence p·(p·x + !p·y) ≤ p·x by (metis add-zeror annil mult.assoc weak-distrib-left-var test-comp-mult test-restrictl ) thus p·z ≤ p·x by (metis assms weak-mult-isol order-trans) next assume assms: test p z ≤ p·x + !p·y hence !p·(p·x + !p·y) ≤ !p·y by (metis mult.assoc test-comp-closed-var weak-distrib-left-var add-zerol annil assms(1 ) ba4 test-zero-var order-refl test-eq1 ) thus !p·z ≤ !p·y by (metis assms test-comp-closed weak-mult-isol order-trans) next assume assms: test p p·z ≤ p·x !p·z ≤ !p·y thus z ≤ p · x + !p · y by (metis assms(2 ,3 ) add-iso-var distrib-right 0 mult-onel test-comp-add ) qed

Nitpick refutes the next three lemmas. lemma test-eq3 : [[test p; test q]] =⇒ p·x = p·x ·q ←→ p·x ≤ x ·q oops lemma test1 : [[test p; test q; p·x ·!q = 0 ]] =⇒ p·x = p·x ·q oops lemma [[test p; test q; x ·!q = !p·x ·!q]] =⇒ p·x = p·x ·q oops

Next, we study tests with commutativity conditions. lemma comm-add : [[test p; p·x = x ·p; p·y = y·p]] =⇒ p·(x + y) = (x + y)·p by (metis distrib-right 0 weak-distrib-left-var ) lemma comm-add-var : [[test p; test q; test r ; p·x = x ·p; p·y = y·p]] =⇒ p·(q·x + r ·y) = (q·x + r ·y)·p by (metis comm-add comm-mult mult.assoc) lemma test-distrib: [[test p; test q]] =⇒ (p + q)·(q·y + !q·x ) = q·y + !q·p·x proof − assume tests: test p test q

18

hence (p + q)·(q·y + !q·x ) = p·q·y + p·!q·x + q·q·y + q·!q·x by (metis add-assoc 0 distrib-right 0 mult.assoc weak-distrib-left-var ) also have ... = p·q·y + p·!q·x + q·y by (metis add .commute add-zerol annil test-comp-mult test-mult-idem-var tests(2 )) also have ... = (p + 1 )·q·y + p·!q·x by (metis add .commute add .left-commute distrib-right 0 mult-oner test-mult-comm-var test-one-var tests(2 )) finally show ?thesis by (metis mult-oner test-absorb3 test-comp-closed-var test-mult-comm-var test-one-var tests(1 ) tests(2 )) qed end

The following class is relevant for probabilistic Kleene algebras. class pre-dioid-test-zerol = near-dioid-test-zerol-dist + pre-dioid begin subclass pre-dioid-one-zerol by (unfold-locales) lemma test-restrictr : test p =⇒ x · p ≤ x by (metis mult-oner subdistl test-comp-uniq) lemma test-eq3 : [[test p; test q]] =⇒ p·x = p·x ·q ←→ p·x ≤ x ·q apply standard apply (metis mult.assoc test-restrictl ) apply (metis eq-iff mult.assoc mult-isol test-mult-idem-var test-restrictr ) done lemma test1 : [[test p; test q; p·x ·!q = 0 ]] =⇒ p·x = p·x ·q oops lemma [[test p; test q; x ·!q = !p·x ·!q]] =⇒ p·x = p·x ·q oops lemma [[test p; test q]] =⇒ x · (p + q) ≤ x · p + x · q oops end

The following class is relevant for Demonic Refinement Algebras. class dioid-tests-zerol = dioid-one-zerol + pre-dioid-test-zerol begin lemma test1 : [[test p; test q; p·x ·!q = 0 ]] =⇒ p·x = p·x ·q by (metis add-0-left add .commute distrib-left mult-oner test-comp-add )

Nitpick refutes the following five lemmas. 19

lemma [[test p; test q; p·x ·!q = 0 ]] =⇒ !p·x ·q = 0 oops lemma [[test p; test q; p·x = p·x ·q]] =⇒ x ·!q = !p·x ·!q oops lemma [[test p; test q; p·x = p·x ·q]] =⇒ p·x ·!q = 0 oops lemma [[test p; test q; p·x = p·x ·q]] =⇒ !p·x ·q = 0 oops lemma [[test p; test q; x ·!q = !p·x ·!q]] =⇒ !p·x ·q = 0 oops lemma [[test p; test q; x ·!q = !p·x ·!q]] =⇒ p·x = p·x ·q by (metis annil mult.assoc test1 test-comp-mult)

Nitpick refutes the following four lemmas. lemma [[test p; test q; !p·x ·q = 0 ]] =⇒ p·x = p·x ·q oops lemma [[test p; test q; !p·x ·q = 0 ]] =⇒ x ·!q = !p·x ·!q oops lemma [[test p; test q; !p·x ·q = 0 ]] =⇒ p·x ·!q = 0 oops lemma test p =⇒ p · x = p · x · p ∧ !p · x = !p · x · !p =⇒ p · x = x · p oops lemma assumes test p and p·x = x ·p shows p·x = p·x ·p ∧ !p·x = !p·x ·!p proof show p·x = p·x ·p by (metis assms eq-refl test-eq3 ) next have !p·x = !p·x ·(p + !p) by (metis assms(1 ) mult-oner test-comp-add ) thus !p·x = !p·x ·!p by (metis assms distrib-left mult.assoc add-zerol annil test-comp-mult2 ) qed end

The following class is relevant for Kleene Algebra with Tests. class dioid-tests = dioid-tests-zerol + dioid-one-zero begin

20

lemma kat-eq1 : [[test p; test q]] =⇒ (p·x ·!q = 0 ) = (p·x = p·x ·q) by (metis annir mult.assoc test1 test-comp-mult) lemma kat-eq2 : [[test p; test q]] =⇒ (p·x ·!q = 0 ) = (p·x ≤ x ·q) by (metis kat-eq1 test-eq3 ) lemma kat-eq3 : [[test p; test q]] =⇒ (p·x = p·x ·q) = (x ·!q = !p·x ·!q) by (metis kat-eq1 test-eq4 )

Nitpick refutes the next lemma. lemma [[test p; test q]] =⇒ (p·x ·!q = 0 ) =⇒ (!p·x ·q = 0 ) oops lemma comm-eq1 : test b =⇒ (p·b = b·p) = (b·p·!b + !b·p·b = 0 ) apply standard apply (metis add-0-left annil annir test-double-comp-var test-mult-comp mult.assoc) apply (metis add-0-left ba6 de-morgan1 distrib-right 0 test-double-comp-var kat-eq1 test-one mult.assoc mult-onel no-trivial-inverse test-comp-closed-var test-not-one) done lemma comm-eq2 : test b =⇒ (p·!b = !b·p) = (b·p·!b + !b·p·b = 0 ) by (metis add-comm comm-eq1 test-comp-closed-var test-double-comp-var ) lemma comm-eq3 : test b =⇒ (p·b = b·p) = (p·!b = !b·p) by (metis comm-eq1 comm-eq2 ) lemma comm-pres: test p =⇒ p·x = p·x ·p ∧ !p·x = !p·x ·!p ←→ p·x = x ·p apply standard apply (metis comm-eq3 kat-eq3 ) apply (metis annil ba6 comm-eq3 mult.assoc test-eq4 test-mult-idem-var ) done end end

4

Kleene Algebra with Tests

theory KAT imports ../DRA-Base Test-Dioids begin

First, we study left Kleene algebras with tests which also have only a left zero. These structures can be expanded to demonic refinement algebras. class left-kat-zerol = left-kleene-algebra-zerol + dioid-tests-zerol begin lemma star-test-export1 : test p =⇒ (p·x )? ·p ≤ p·x ? by (metis mult-isol mult-oner star-iso star-slide test-eq3 test-one-var )

21

lemma star-test-export2 : test p =⇒ (p·x )? ·p ≤ x ? ·p by (metis mult-isor star2 star-denest star-invol star-iso star-slide star-subdist-var-2 star-subid test-ub-var ) lemma star-test-export-left: [[test p; x ·p ≤ p·x ]] =⇒ x ? ·p = p·(x ·p)? apply (rule antisym) apply (metis mult.assoc mult-isol-var star-sim1 test-double-comp-var test-mult-idem-var test-mult-lb1 ) by (metis star-slide star-test-export2 ) lemma star-test-export-right: [[test p; x ·p ≤ p·x ]] =⇒ x ? ·p = (p·x )? ·p by (metis star-slide star-test-export-left) lemma star-test-export2-left: [[test p; p·x = x ·p]] =⇒ x ? ·p = p·(p·x )? by (metis order-refl star-test-export-left) lemma star-test-export2-right: [[test p; p·x = x ·p]] =⇒ x ? ·p = (x ·p)? ·p by (metis star-slide star-test-export2-left) lemma star-test-folk : [[test p; p·x = x ·p; p·y = y·p]] =⇒ (p·x + !p·y)? ·p = p·(p·x )? proof − assume assms: test p p·x = x ·p p·y = y·p hence (p·x + !p·y)? ·p = p·(p·p·x + p·!p·y)? by (metis comm-add-var test-comp-closed-var star-test-export2-left distrib-left mult.assoc) thus ?thesis by (metis assms(1 ) test-double-comp-var test-mult-comp test-mult-idem-var add-zeror annil ) qed end class kat-zerol = kleene-algebra-zerol + dioid-tests-zerol begin subclass left-kat-zerol by (unfold-locales) lemma star-sim-right: [[test p; p·x = x ·p]] =⇒ p·x ? = (p·x )? ·p by (metis mult.assoc star-sim3 test-mult-idem-var ) lemma star-sim-left: [[test p; p·x = x ·p]] =⇒ p·x ? = p·(x ·p)? by (metis star-sim-right star-slide) lemma comm-star : [[test p; p·x = x ·p; p·y = y·p]] =⇒ p·x ·(p·y)? = p·x ·y ? by (metis star-sim-right mult.assoc star-slide) lemma star-sim-right-var : [[test p; p·x = x ·p]] =⇒ x ? ·p = p·(x ·p)?

22

by (metis mult.assoc star-sim3 test-mult-idem-var ) lemma star-folk-var [simp]: [[test p; p·x = x ·p; p·y = y·p]] =⇒ (p·x + !p·y)? ·p = p·x ? by (metis star-test-folk comm-star mult-onel mult-oner ) lemma star-folk-var2 [simp]: [[test p; !p·x = x ·!p; !p·y = y·!p]] =⇒ (p·x + !p·y)? ·!p = !p·y ? by (metis star-folk-var add .commute test-def ) end

Finally, we define Kleene algebra with tests. class kat = kleene-algebra + dioid-tests begin subclass kat-zerol apply (unfold-locales) by (metis star-inductr ) end end

5

Demonic Refinement Algebra with Tests

theory DRAT imports Test-Dioids ../DRA begin

In this section, we define demonic refinement algebras with tests and prove the most important theorems from the literature. In this context, tests are also known as guards. class dra-tests = dra + dioid-tests-zerol begin

An assertion is a mapping from a guard to a subset similar to tests, but it aborts if the predicate does not hold. definition assertion :: 0a ⇒ 0a (-o [101 ] 100 ) where test p =⇒ p o = !p·> + 1 lemma asg: [[test p; test q]] =⇒ q ≤ 1 ∧ 1 ≤ p o by (metis add .commute add-ub1 assertion-def test-ub-var ) lemma assertion-isol : test p =⇒ y ≤ p o ·x ←→ p·y ≤ x proof assume assms: test p y ≤ p o ·x hence p·y ≤ p·!p·>·x + p·x by (metis mult.assoc mult-isol assertion-def assms(1 ) distrib-left distrib-right 0 mult-1-left mult.assoc)

23

also have ... ≤ x by (metis assms(1 ) distrib-right 0 mult.assoc add-zerol annil test-comp-mult eq-refl test-eq1 ) finally show p·y ≤ x by metis next assume assms: test p p·y ≤ x hence p o ·p·y = !p·>·p·y + p·y by (metis assertion-def distrib-right 0 mult-1-left mult.assoc) also have ... = !p·> + p·y by (metis mult.assoc top-mult-annil ) moreover have p o ·p·y ≤ p o ·x by (metis assms(2 ) mult.assoc mult-isol ) moreover have !p·y + p·y ≤ !p·> + p·y by (metis add .commute assms(1 ) order-refl test-eq2 top-elim) ultimately show y ≤ p o ·x by (metis add .commute assms(1 ) distrib-right 0 mult-1-left order-trans test-comp-add ) qed lemma assertion-isor : test p =⇒ y ≤ x ·p ←→ y·p o ≤ x proof assume assms: test p y ≤ x ·p hence y·p o ≤ x ·p·!p·> + x ·p by (metis mult-isor assertion-def assms(1 ) distrib-left mult-1-right mult.assoc) also have ... ≤ x by (metis assms(1 ) add-zerol annil distrib-left mult.assoc test-comp-mult distrib-left mult-1-right order-prop test-comp) finally show y·p o ≤ x by metis next assume assms: test p y·p o ≤ x have y ≤ y·(!p·> + p) by (metis add-iso-var mult-isol order-refl order-refl top-elim add .commute assms(1 ) mult-1-right test-comp-add ) also have ... = y·p o ·p by (metis assertion-def assms(1 ) distrib-right 0 mult-1-left mult.assoc top-mult-annil ) finally show y ≤ x ·p by (metis assms(2 ) mult-isor order-trans) qed lemma assertion-iso: [[test p; test q]] =⇒ x ·q o ≤ p o ·x ←→ p·x ≤ x ·q by (metis assertion-isol assertion-isor mult.assoc) lemma total-correctness: [[test p; test q]] =⇒ p·x ·!q = 0 ←→ x ·!q ≤ !p·> apply standard apply (metis mult.assoc test-eq1 top-elim zero-least) apply (metis annil test-comp-mult zero-unique mult.assoc mult-isol ) done

24

lemma test-iteration-sim: [[test p; p·x ≤ x ·p]] =⇒ p·x ∞ ≤ x ∞ ·p by (metis iteration-sim) lemma test-iteration-annir : test p =⇒ !p·(p·x )∞ = !p by (metis annil ba6 iteration-idep mult.assoc)

Next we give an example of a program transformation from von Wright [8]. lemma loop-refinement: [[test p; test q]] =⇒ (p·x )∞ ·!p ≤ (p·q·x )∞ ·!(p·q)·(p·x )∞ ·!p proof − assume assms: test p test q hence (p·x )∞ ·!p = ((p·q) + !(p·q))·(p·x )∞ ·!p by (metis de-morgan3 mult-onel test-add-comp) also have ... = (p·q)·(p·x )∞ ·!p + !(p·q)·(p·x )∞ ·!p by (metis distrib-right 0) also have ... = (p·q)·!p + (p·q)·(p·x )·(p·x )∞ ·!p + !(p·q)·(p·x )∞ ·!p by (metis iteration-unfoldr-distr mult.assoc iteration-unfold-eq distrib-left mult.assoc) also have ... = (p·q)·(p·x )·(p·x )∞ ·!p + !(p·q)·(p·x )∞ ·!p by (metis assms less-eq-def test3 zero-least) finally have (p·x )∞ ·!p ≤ p·q·x ·(p·x )∞ ·!p + !(p·q)·(p·x )∞ ·!p by (metis assms mult.assoc test2 eq-iff ) thus ?thesis by (metis coinduction add .commute mult.assoc) qed

Finally, we prove different versions of Back’s atomicity refinement theorem for action systems. lemma atom-step1 : r ·b ≤ b·r =⇒ (a + b + r )∞ = b ∞ ·r ∞ ·(a·b ∞ ·r ∞ )∞ apply (subgoal-tac (a + b + r )∞ = (b + r )∞ ·(a·(b + r )∞ )∞ ) apply (metis iteration-sep mult.assoc) by (metis add-assoc 0 add .commute iteration-denest)

lemma atom-step2 : assumes s = s·q q·b = 0 r ·q ≤ q·r q·l ≤ l ·q r ∞ = r ? test q shows s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ ≤ s·l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ proof − have s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ ≤ s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·q·r ∞ )∞ by (metis assms(3 ) assms(5 ) star-sim1 mult.assoc mult-isol iteration-iso) also have ... ≤ s·q·l ∞ ·b ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(1 ,6 ) test-ub-var mult-double-iso mult-oner ) also have ... ≤ s·l ∞ ·q·b ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(4 ) iteration-sim mult.assoc mult-double-iso mult-double-iso) also have ... ≤ s·l ∞ ·r ∞ ·q·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(2 ) zero-least iteration-sim mult.assoc mult-double-iso) also have ... ≤ s·l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(6 ) mult.assoc mult-isol test-restrictl iteration-idem mult.assoc) finally show s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ ≤ s·l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by metis qed

25

lemma atom-step3 : assumes r ·l ≤ l ·r a·l ≤ l ·a b·l ≤ l ·b q·l ≤ l ·q b ∞ = b ? shows l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ = (a·b ∞ ·q + l + r )∞ proof − have (a·b ∞ ·q + r )·l ≤ a·b ∞ ·l ·q + l ·r by (metis distrib-right add-iso-var assms(1 ,4 ) mult.assoc mult-isol ) also have ... ≤ a·l ·b ∞ ·q + l ·r by (metis assms(3 ) assms(5 ) star-sim1 add-iso mult.assoc mult-double-iso) also have ... ≤ l ·(a·b ∞ ·q + r ) by (metis add-iso assms(2 ) mult-isor distrib-left mult.assoc) finally have (a·b ∞ ·q + r )·l ≤ l ·(a·b ∞ ·q + r ) by metis moreover have l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ = l ∞ ·(a·b ∞ ·q + r )∞ by (metis add .commute mult.assoc iteration-denest) ultimately show ?thesis by (metis add .commute add .left-commute iteration-sep) qed

This is Back’s atomicity refinement theorem, as specified by von Wright [8]. theorem atom-ref-back : assumes s = s·q a = q·a q·b = 0 r ·b ≤ b·r r ·l ≤ l ·r r ·q ≤ q·r a·l ≤ l ·a b·l ≤ l ·b q·l ≤ l ·q r ∞ = r ? b ∞ = b ? test q shows s·(a + b + r + l )∞ ·q ≤ s·(a·b ∞ ·q + r + l )∞ proof − have (a + b + r )·l ≤ l ·(a + b + r ) by (metis add-iso-var distrib-right 0 assms(5 ) assms(7 ) assms(8 ) distrib-left) hence s·(l + a + b + r )∞ ·q = s·l ∞ ·(a + b + r )∞ ·q by (metis add .commute add .left-commute mult.assoc iteration-sep) also have ... ≤ s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ by (metis assms(2 ,4 ,10 ,11 ) atom-step1 iteration-slide eq-refl mult.assoc) also have ... ≤ s·l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(1 ) assms(10 ) assms(12 ) assms(3 ) assms(6 ) assms(9 ) atom-step2 ) also have ... ≤ s·(a·b ∞ ·q + l + r )∞ by (metis assms(11 ) assms(5 ) assms(7 ) assms(8 ) assms(9 ) atom-step3 eq-refl mult.assoc) finally show ?thesis by (metis add .commute add .left-commute) qed

This variant is due to H¨ ofner, Struth and Sutcliffe [4]. theorem atom-ref-back-struth: assumes s ≤ s·q a ≤ q·a q·b = 0 r ·b ≤ b·r r ·q ≤ q·r (a + r + b)·l ≤ l ·(a + r + b) q·l ≤ l ·q r∞ = r? q ≤ 1 shows s·(a + b + r + l )∞ ·q ≤ s·(a·b ∞ ·q + r + l )∞

26

proof − have s·(a + b + r + l )∞ ·q = s·l ∞ ·(a + b + r )∞ ·q by (metis add .commute add .left-commute assms(6 ) iteration-sep mult.assoc) also have ... = s·l ∞ ·(b + r )∞ ·(a·(b + r )∞ )∞ ·q by (metis add-assoc 0 add .commute iteration-denest add .commute mult.assoc) also have ... = s·l ∞ ·b ∞ ·r ∞ ·(a·b ∞ ·r ∞ )∞ ·q by (metis assms(4 ) iteration-sep mult.assoc) also have ... ≤ s·l ∞ ·b ∞ ·r ∞ ·(q·a·b ∞ ·r ∞ )∞ ·q by (metis assms(2 ) iteration-iso mult-isol-var eq-refl order-refl ) also have ... = s·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ by (metis iteration-slide mult.assoc) also have ... ≤ s·q·l ∞ ·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ by (metis assms(1 ) mult-isor ) also have ... ≤ s·l ∞ ·q·b ∞ ·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ by (metis assms(7 ) iteration-sim mult.assoc mult-double-iso) also have ... ≤ s·l ∞ ·q·r ∞ ·q·(a·b ∞ ·r ∞ ·q)∞ by (metis assms(3 ) iteration-idep mult.assoc order-refl ) also have ... ≤ s·l ∞ ·q·r ∞ ·q·(a·b ∞ ·r ? ·q)∞ by (metis assms(8 ) eq-refl ) also have ... ≤ s·l ∞ ·q·r ∞ ·q·(a·b ∞ ·q·r ? )∞ by (metis assms(5 ) iteration-iso mult.assoc mult-isol star-sim1 ) also have ... = s·l ∞ ·q·r ∞ ·q·(a·b ∞ ·q·r ∞ )∞ by (metis assms(8 )) also have ... ≤ s·l ∞ ·r ∞ ·q·(a·b ∞ ·q·r ∞ )∞ by (metis assms(9 ) mult-1-right mult-double-iso mult-isor ) also have ... ≤ s·l ∞ ·r ∞ ·(a·b ∞ ·q·r ∞ )∞ by (metis assms(9 ) mult-1-right mult-double-iso) also have ... = s·l ∞ ·(a·b ∞ ·q + r )∞ by (metis add .commute mult.assoc iteration-denest) also have ... ≤ s·(a·b ∞ ·q + r + l )∞ by (metis add .commute iteration-subdenest mult.assoc mult-isol ) finally show ?thesis . qed

Finally, we prove Cohen’s [2] variation of the atomicity refinement theorem. lemma atom-ref-cohen: assumes r ·p·y ≤ y·r y·p·l ≤ l ·y r ·p·l ≤ l ·r p·r ·!p = 0 p·l ·!p = 0 !p·l ·p = 0 y·0 = 0 r ·0 = 0 test p shows (y + r + l )∞ = (p·l )∞ ·(y + !p·l + r ·!p)∞ ·(r ·p)∞ proof − have (y + r )·p·l ≤ l ·y + l ·r by (metis distrib-right 0 add-iso-var assms(2 ) assms(3 )) hence stepA: (y + r )·p·l ≤ (p·l + !p·l )·(y + r )? by (metis assms(9 ) distrib-left distrib-right 0 mult-1-left mult-isol order-trans star-ext test-comp-add ) have subStepB : (!p·l + y + p·r + !p·r )∞ = (!p·l + y + r ·p + r ·!p)∞ by (metis add-assoc 0 annil assms(8 ) assms(9 ) distrib-left distrib-right 0 star-slide

27

star-subid test-add-comp test-double-comp-var zero-least) have r ·p·(y + r ·!p + !p·l ) ≤ y·(r ·p + r ·!p) by (metis assms(1 ,4 ,9 ) distrib-left add-0-left add .commute annil mult.assoc test-comp-mult distrib-left mult-oner test-comp-add ) also have ... ≤ (y + r ·!p + !p·l )·(r ·p + (y + r ·!p + !p·l )) by (metis add-lub add-ub1 add-ub2 mult-isol-var ) finally have r ·p·(y + r ·!p + !p·l ) ≤ (y + r ·!p + !p·l )·(y + r ·!p + !p·l + r ·p) by (metis add .commute) hence stepB : (!p·l + y + p·r + !p·r )∞ = (y + !p·l + r ·!p)∞ ·(r ·p)∞ by (metis subStepB iteration-sep3 [of r ·p y + r ·!p + !p·l ] add-assoc 0 add .commute) have (y + r + l )∞ = (p·l + !p·l + y + r )∞ by (metis add-comm add .left-commute assms(9 ) distrib-right 0 mult-onel test-comp-add ) also have ... = (p·l )∞ ·(!p·l + y + r )∞ using stepA by (metis assms(6 −8 ) annil add .assoc add-0-left distrib-right 0 add .commute mult.assoc iteration-sep4 [of y+r !p·l p·l ]) also have ... = (p·l )∞ ·(!p·l + y + p·r + !p·r )∞ by (metis add .commute assms(9 ) combine-common-factor mult-1-left test-comp-add ) finally show ?thesis using stepB by (metis mult.assoc) qed end end

6

Models for Demonic Refinement Algebra with Tests

theory DRA-Models imports DRAT Test-Dioids begin

We formalise the predicate transformer model of demonic refinement algebra. Predicate transformers are formalised as strict and additive functions over a field of sets, or alternatively as costrict and multiplicative functions. In the future, this should be merged with Preoteasa’s more abstract formalisation [6]. no-notation plus (infixl + 65 ) and less-eq ((-/ ≤ -) [51 , 51 ] 50 ) notation comp (infixl · 55 ) type-synonym 0a bfun = 0a set ⇒ 0a set

Definitions of signature: definition top :: 0a bfun where top ≡ λx . UNIV definition bot :: 0a bfun where bot ≡ λx . {} definition adjoint :: 0a bfun ⇒ 0a bfun where adjoint f ≡ (λp. − f (−p))

28

definition fun-inter :: 0a bfun ⇒ 0a bfun ⇒ 0a bfun (infix u 51 ) where f u g ≡ λp. f p ∩ g p definition fun-union :: 0a bfun ⇒ 0a bfun ⇒ 0a bfun (infix + 52 ) where f + g ≡ λp. f p ∪ g p definition fun-order :: 0a bfun ⇒ 0a bfun ⇒ bool (infix ≤ 50 ) where f ≤ g ≡ ∀ p. f p ⊆ g p definition fun-strict-order :: 0a bfun ⇒ 0a bfun ⇒ bool (infix ss addsimps Named-Theorems.get ctxt @{named-theorems hoare-simp}) ctxt)) ii declare hoare-seq [hoare-rule] method-setup hoare-auto = hh Scan.succeed (fn ctxt => SIMPLE-METHOD (REPEAT (CHANGED (hoare-tac ctxt 1 )))) ii

41

declare assign-fn-def [hoare-simp] declare image-def [hoare-simp]

We add some syntactic sugar. abbreviation assign-sugar :: string ⇒ string ⇒ state relation (infix := 99 ) where x := y ≡ x := (λσ. σ y) abbreviation hoare-sugar :: 0a set ⇒ 0a relation ⇒ 0a set ⇒ bool ({|-|}-{|-|}) where {|p|} c {|q|} ≡ {|assert p|} c {|assert q|} abbreviation mod-sugar :: string ⇒ string ⇒ state ⇒ nat (infix mod 100 ) where x mod y ≡ λσ. (σ x ) mod (σ y) abbreviation while-inv-sugar :: 0a set ⇒ 0a set ⇒ 0a relation ⇒ 0a relation (while - inv - do - [64 ,64 ,64 ] 63 ) where while b inv i do p ≡ while (assert b) inv (assert i ) do p

As a complex example, we verify the partial correctness of Euclid’s algorithm. lemma euclids-algorithm: {|{σ. σ 00x 00 = x ∧ σ 00y 00 = y}|} while {σ. σ 00y 00 6= 0 } inv {σ. gcd (σ 00x 00) (σ 00y 00) = gcd x y} do ( 00 00 z := 00y 00; 00 00 y := 00x 00 mod 00y 00; 00 00 x := 00z 00 ) {|{σ. σ 00x 00 = gcd x y}|} apply hoare-auto by (metis gcd-red-nat) end

12

Two sorted Kleene Algebra with Tests

theory KAT2 imports ../../Kleene-Algebra/Kleene-Algebra begin

As an alternative to the one-sorted implementation of tests, we provide a two-sorted, more conventional one. In this setting, Isabelle’s Boolean algebra theory can be used. This alternative can be developed further along the lines of the one-sorted implementation. syntax -kat :: 0a ⇒ 0a (‘-‘ )

42

named-theorems kat-hom KAT test homomorphism rules ML hh val kat-test-vars = [p,q,r ,s,t,p 0,q 0,r 0,s 0,t 0,p 00,q 00,r 00,s 00,t 00] fun map-ast-variables ast = case ast of (Ast.Variable v ) => if exists (fn tv => tv = v ) kat-test-vars then Ast.Appl [Ast.Variable test, Ast.Variable v ] else Ast.Variable v | (Ast.Constant c) => Ast.Constant c | (Ast.Appl []) => Ast.Appl [] | (Ast.Appl (f :: xs)) => Ast.Appl (f :: map map-ast-variables xs) fun kat-hom-tac ctxt n = let val rev-rules = map (fn thm => thm RS @{thm sym}) (Named-Theorems.get ctxt @{named-theorems kat-hom}) in asm-full-simp-tac (put-simpset HOL-basic-ss ctxt addsimps rev-rules) n end ii method-setup kat-hom = hh Scan.succeed (fn ctxt => SIMPLE-METHOD (CHANGED (kat-hom-tac ctxt 1 ))) ii parse-ast-translation hh let fun kat-tr ctxt [t] = map-ast-variables t in [(@{syntax-const -kat}, kat-tr )] end ii named-theorems vcg verification condition generator rules ML hh fun vcg-tac ctxt n = let fun vcg 0 [] = no-tac | vcg 0 (r :: rs) = resolve-tac ctxt [r ] n ORELSE vcg 0 rs; in REPEAT (CHANGED (kat-hom-tac ctxt n THEN REPEAT (vcg 0 (rev (Named-Theorems.get ctxt @{named-theorems vcg}))) THEN kat-hom-tac ctxt n THEN TRY (resolve-tac ctxt @{thms order-refl } n ORELSE asm-full-simp-tac (put-simpset HOL-basic-ss ctxt) n))) end

43

ii method-setup vcg = hh Scan.succeed (fn ctxt => SIMPLE-METHOD (CHANGED (vcg-tac ctxt 1 ))) ii locale dioid-tests = fixes test :: 0a::boolean-algebra ⇒ 0b::dioid-one-zerol and not :: 0b::dioid-one-zerol ⇒ 0b::dioid-one-zerol (−) assumes test-sup [simp,kat-hom]: test (sup p q) = ‘p + q‘ and test-inf [simp,kat-hom]: test (inf p q) = ‘p · q‘ and test-top [simp,kat-hom]: test top = 1 and test-bot [simp,kat-hom]: test bot = 0 and test-not [simp,kat-hom]: test (− p) = ‘ −p‘ and test-iso-eq [kat-hom]: p ≤ q ←→ ‘p ≤ q‘ begin notation test (ι) lemma test-eq [kat-hom]: p = q ←→ ‘p = q‘ by (metis eq-iff test-iso-eq) lemma test-iso: p ≤ q =⇒ ‘p ≤ q‘ by (simp add : test-iso-eq)

lemma test-meet-comm: ‘p · q = q · p‘ by kat-hom (fact inf-commute) lemmas test-one-top[simp] = test-iso[OF top-greatest, simplified ] lemma [simp]: ‘ −p + p = 1‘ by kat-hom (metis compl-sup-top) lemma [simp]: ‘p + (−p) = 1‘ by kat-hom (metis sup-compl-top) lemma [simp]: ‘ (−p) · p = 0‘ by (metis inf .commute inf-compl-bot test-bot test-inf test-not) lemma [simp]: ‘p · (−p) = 0‘ by (metis inf-compl-bot test-bot test-inf test-not) end locale kat = fixes test :: 0a::boolean-algebra ⇒ 0b::kleene-algebra and not :: 0b::kleene-algebra ⇒ 0b::kleene-algebra (!)

44

assumes is-dioid-tests: dioid-tests test not sublocale kat ⊆ dioid-tests using is-dioid-tests . context kat begin notation test (ι) lemma test-eq [kat-hom]: p = q ←→ ‘p = q‘ by (metis eq-iff test-iso-eq) lemma test-iso: p ≤ q =⇒ ‘p ≤ q‘ by (simp add : test-iso-eq)

lemma test-meet-comm: ‘p · q = q · p‘ by kat-hom (fact inf-commute) lemmas test-one-top[simp] = test-iso[OF top-greatest, simplified ] lemma test-star [simp]: ‘p ? = 1‘ by (metis star-subid test-iso test-top top-greatest) lemmas [kat-hom] = test-star [symmetric] lemma [simp]: ‘ !p + p = 1‘ by kat-hom (metis compl-sup-top) lemma [simp]: ‘p + !p = 1‘ by kat-hom (metis sup-compl-top) lemma [simp]: ‘ !p · p = 0‘ by (metis inf .commute inf-compl-bot test-bot test-inf test-not) lemma [simp]: ‘p · !p = 0‘ by (metis inf-compl-bot test-bot test-inf test-not) definition hoare-triple :: 0b ⇒ 0b ⇒ 0b ⇒ bool ({|-|} - {|-|}) where {|p|} c {|q|} ≡ p·c ≤ c·q declare hoare-triple-def [iff ] lemma hoare-triple-def-var : ‘p·c ≤ c·q ←→ p·c·!q = 0‘ apply (intro iffI antisym) apply (rule order-trans[of - ‘c · q · !q‘ ]) apply (rule mult-isor [rule-format]) apply (simp add : mult.assoc)+

45

apply (simp add : mult.assoc[symmetric]) apply (rule order-trans[of - ‘p·c·(!q + q)‘ ]) apply simp apply (simp only: distrib-left add-zerol ) apply (rule order-trans[of - ‘1 · c · q‘ ]) apply (simp only: mult.assoc) apply (rule mult-isor [rule-format]) by simp-all lemmas [intro!] = star-sim2 [rule-format] lemma hoare-weakening: p ≤ p 0 =⇒ q 0 ≤ q =⇒ ‘ {|p 0|} c {|q 0|}‘ =⇒ ‘ {|p|} c {|q|}‘ by auto (metis mult-isol mult-isor order-trans test-iso) lemma hoare-star : ‘ {|p|} c {|p|}‘ =⇒ ‘ {|p|} c ? {|p|}‘ by auto lemmas [vcg] = hoare-weakening[OF order-refl - hoare-star ] lemma hoare-test [vcg]: ‘p · t ≤ q‘ =⇒ ‘ {|p|} t {|q|}‘ by auto (metis inf-le2 le-inf-iff test-inf test-iso-eq) lemma hoare-mult [vcg]: ‘ {|p|} x {|r |}‘ =⇒ ‘ {|r |} y {|q|}‘ =⇒ ‘ {|p|} x ·y {|q|}‘ proof auto assume [simp]: ‘p · x ≤ x · r‘ and [simp]: ‘r · y ≤ y · q‘ have ‘p · (x · y) ≤ x · r · y‘ by (auto simp add : mult.assoc[symmetric] intro!: mult-isor [rule-format]) also have ‘ ... ≤ x · y · q‘ by (auto simp add : mult.assoc intro!: mult-isol [rule-format]) finally show ‘p · (x · y) ≤ x · y · q‘ . qed lemma [simp]: ‘ !p · !p = !p‘ by (metis inf .idem test-inf test-not) lemma hoare-plus [vcg]: ‘ {|p|} x {|q|}‘ =⇒ ‘ {|p|} y {|q|}‘ =⇒ ‘ {|p|} x + y {|q|}‘ by (auto simp add : distrib-left distrib-right add-iso-var ) definition While :: 0b ⇒ 0b ⇒ 0b (While - Do - End [50 ,50 ] 51 ) where While t Do c End = (t·c)? ·!t lemma hoare-while: ‘ {|p · t|} c {|p|}‘ =⇒ ‘ {|p|} While t Do c End {|!t · p|}‘ unfolding While-def by vcg (metis inf-commute order-refl ) lemma [vcg]: ‘ {|p · t|} c {|p|}‘ =⇒ ‘ !t · p ≤ q‘ =⇒ ‘ {|p|} While t Do c End {|q|}‘ by (metis hoare-weakening hoare-while order-refl test-inf test-iso-eq test-not) definition If :: 0b ⇒ 0b ⇒ 0b ⇒ 0b (If - Then - Else - [50 ,50 ,50 ] 51 ) where If p Then c1 Else c2 ≡ p·c1 + !p·c2

46

lemma hoare-if [vcg]: ‘ {|p · t|} c1 {|q|}‘ =⇒ ‘ {|p · !t|} c2 {|q|}‘ =⇒ ‘ {|p|} If t Then c1 Else c2 {|q|}‘ unfolding If-def by vcg assumption end end

13

Two sorted Demonic Refinement Algebras

theory DRAT2 imports ../DRA begin

As an alternative to the one-sorted implementation of demonic refinement algebra with tests, we provide a two-sorted, more conventional one. This alternative can be developed further along the lines of the one-sorted implementation. syntax -dra :: 0a ⇒ 0a (‘-‘ ) named-theorems kat-hom KAT test homomorphism rules ML hh val dra-test-vars = [p,q,r ,s,t,p 0,q 0,r 0,s 0,t 0,p 00,q 00,r 00,s 00,t 00] fun map-ast-variables ast = case ast of (Ast.Variable v ) => if exists (fn tv => tv = v ) dra-test-vars then Ast.Appl [Ast.Variable test, Ast.Variable v ] else Ast.Variable v | (Ast.Constant c) => Ast.Constant c | (Ast.Appl []) => Ast.Appl [] | (Ast.Appl (f :: xs)) => Ast.Appl (f :: map map-ast-variables xs) fun dra-hom-tac ctxt n = let val rev-rules = map (fn thm => thm RS @{thm sym}) (Named-Theorems.get ctxt @{named-theorems kat-hom}) in asm-full-simp-tac (put-simpset HOL-basic-ss ctxt addsimps rev-rules) n end ii method-setup kat-hom = hh Scan.succeed (fn ctxt => SIMPLE-METHOD (CHANGED (dra-hom-tac ctxt 1 )))

47

ii parse-ast-translation hh let fun dra-tr ctxt [t] = map-ast-variables t in [(@{syntax-const -dra}, dra-tr )] end ii named-theorems vcg verification condition generator rules ML hh fun vcg-tac ctxt n = let fun vcg 0 [] = no-tac | vcg 0 (r :: rs) = resolve-tac ctxt [r ] n ORELSE vcg 0 rs; in REPEAT (CHANGED (dra-hom-tac ctxt n THEN REPEAT (vcg 0 (rev (Named-Theorems.get ctxt @{named-theorems vcg}))) THEN dra-hom-tac ctxt n THEN TRY (resolve-tac ctxt @{thms order-refl } n ORELSE asm-full-simp-tac (put-simpset HOL-basic-ss ctxt) n))) end ii method-setup vcg = hh Scan.succeed (fn ctxt => SIMPLE-METHOD (CHANGED (vcg-tac ctxt 1 ))) ii locale drat = fixes test :: 0a::boolean-algebra ⇒ 0b::dra and not :: 0b::dra ⇒ 0b::dra (!) assumes test-sup [simp,kat-hom]: test (sup p q) = ‘p + q‘ and test-inf [simp,kat-hom]: test (inf p q) = ‘p · q‘ and test-top [simp,kat-hom]: test top = 1 and test-bot [simp,kat-hom]: test bot = 0 and test-not [simp,kat-hom]: test (− p) = ‘ !p‘ and test-iso-eq [kat-hom]: p ≤ q ←→ ‘p ≤ q‘ begin notation test (ι) lemma test-eq [kat-hom]: p = q ←→ ‘p = q‘ by (metis eq-iff test-iso-eq) lemma test-iso: p ≤ q =⇒ ‘p ≤ q‘ by (simp add : test-iso-eq)

48

lemma test-meet-comm: ‘p · q = q · p‘ by kat-hom (fact inf-commute) lemmas test-one-top[simp] = test-iso[OF top-greatest, simplified ] lemma test-star [simp]: ‘p ? = 1‘ by (metis star-subid test-iso test-top top-greatest) lemmas [kat-hom] = test-star [symmetric] lemma test-comp-add1 [simp]: ‘ !p + p = 1‘ by kat-hom (metis compl-sup-top) lemma test-comp-add2 [simp]: ‘p + !p = 1‘ by kat-hom (metis sup-compl-top) lemma test-comp-mult1 [simp]: ‘ !p · p = 0‘ by (metis inf .commute inf-compl-bot test-bot test-inf test-not) lemma test-comp-mult2 [simp]: ‘p · !p = 0‘ by (metis inf-compl-bot test-bot test-inf test-not) lemma test-eq1 : ‘y ≤ x‘ ←→ ‘p·y ≤ x‘ ∧ ‘ !p·y ≤ x‘ apply standard apply (metis mult-isol-var mult-onel test-not test-one-top) apply (subgoal-tac ‘ (p + !p)·y ≤ x‘ ) apply (metis mult-onel sup-compl-top test-not test-sup test-top) apply (metis add-lub distrib-right 0) done lemma ‘p·x = p·x ·q‘ =⇒ ‘p·x ·!q = 0‘ nitpick oops lemma test1 : ‘p·x ·!q = 0‘ =⇒ ‘p·x = p·x ·q‘ by (metis add-0-left distrib-left mult-oner test-comp-add1 ) lemma test2 : ‘p·q·p = p·q‘ by (metis inf .commute inf .left-idem test-inf ) lemma test3 : ‘p·q·!p = 0‘ by (metis inf .assoc inf .idem inf .left-commute inf-compl-bot test-bot test-inf test-not) lemma test4 : ‘ !p·q·p = 0‘ by (metis double-compl test3 test-not) lemma total-correctness: ‘p·x ·!q = 0‘ ←→ ‘x ·!q ≤ !p·>‘ apply standard apply (metis mult.assoc test-eq1 top-elim zero-least) apply (metis annil test-comp-mult2 zero-unique mult.assoc mult-isol )

49

done lemma test-iteration-sim: ‘p·x ≤ x ·p‘ =⇒ ‘p·x ∞ ≤ x ∞ ·p‘ by (metis iteration-sim) lemma test-iteration-annir : ‘ !p·(p·x )∞ = !p‘ by (metis (no-types) monoid-add-class.add .left-neutral double-compl iteration-idep monoid-mult-class.mult.right-neutral test-comp-add2 test-inf test-not top-elim total-correctness) end end

References [1] A. Armstrong, G. Struth, and T. Weber. Kleene algebra. Archive of Formal Proofs, 2013. [2] E. Cohen. Separation and reduction. In R. C. Backhouse and J. N. Oliveira, editors, MPC, volume 1837 of LNCS, pages 45–59. Springer, 2000. [3] J. H. Conway. Regular Algebra and Finite Machines. Chapman and Hall, 1971. [4] P. H¨ ofner, G. Struth, and G. Sutcliffe. Automated verification of refinement laws. Ann. Mathematics and Artificial Intelligence, 55(1-2):35–62, 2009. [5] D. Kozen. Kleene algebra with tests. ACM TOPLAS, 19(3):427–443, 1997. [6] V. Preoteasa. Algebra of monotonic boolean transformers. In A. S. Sim˜ao and C. Morgan, editors, SBMF, volume 7021 of LNCS, pages 140–155. Springer, 2011. [7] K. Solin. Normal forms in total correctness for while programs and action systems. J. Logic and Algebraic Programming, 80(6):362–375, 2011. [8] J. von Wright. From Kleene algebra to refinement algebra. In E. A. Boiten and B. M¨ oller, editors, MPC, volume 2386 of LNCS, pages 233– 262. Springer, 2002.

50