• No results found

Formal Methods for Software Development

N/A
N/A
Protected

Academic year: 2021

Share "Formal Methods for Software Development"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

Development

Till Mossakowski, Lutz Schr ¨oder

(2)

Monadic QuickCheck

• extension of QuickCheck for monadic (= imperative) programs

• specifications are equations between monadic actions

• equations are interpreted observationally, i.e. p1 = p2 if p1 and p2 admit the same observations in each context

(3)

The State Monad

newtype State s a =

State { runState :: s -> (a, s) }

instance Monad (State s) where

return a = State $ \s -> (a, s)

m >>= k = State $ \s -> let

(a, s’) = runState m s in runState (k a) s’

(4)

The Queue Example

type Queue = Int

type QueueState a = (Int->[a],Int) type QS a = State (QueueState a)

initState :: QueueState a

empty :: QS a Queue

add :: Queue -> a -> QS a ()

remove :: Queue -> QS a ()

(5)

Algebraic specification of queues

q <- empty; x <- front q

= q <- empty; x <- return Nothing

q <- empty; add q m; x <- front q

= q <- empty; add q m; x <- return (Just m)

add q m; add q n; x <- front q = add q m; x <- front q; add q n

q <- empty; add q m; remove q = q <- empty

add q m; add q n; remove q = add q m; remove q; add q n

(6)

Observational Equivalence

• A context C is a “program with a hole”. We write the result of “filling the hole” with a term e as C[e].

• We write p ⇓ o when program p computes an observable result o.

• Two terms e and e0 are observationally equivalent if, for every context C,

(7)

How to Implement Observational

Equivalence with QuickCheck?

• Problem: how to enumerate all contexts?

• Solution: only work with sequences of basic operations of the queue monad

• Represent these as sequences of actions

• Be careful that only well-formed sequences are taken into account (e.g. those not removing something from the empty queue)

(8)

Queue Actions as a Datatype

data Action a = Add a | Remove |

Front | Return (Maybe a) deriving (Eq, Show)

(9)

Executing Actions

perform :: Queue -> [Action a] -> QS a [Maybe a]

perform q [] = return [] perform q (a : as) =

case a of

Add n -> add q n >> perform q as

Remove -> remove q >> perform q as Front -> liftM2 (:) (front q)

(perform q as)

(10)

Well-formed Action Sequences

actions :: (Arbitrary a, Num b) => b -> Gen [Action a]

actions n = oneof

([return [],

liftM2 (:) (liftM Add arbitrary) (actions (n + 1)),

liftM (Front :) (actions n)] ++ if n == 0 then [] else

(11)

Counting the Number of Queue Elements

delta :: [Action a] -> Int delta = sum . map deltaAux

where deltaAux a = case a of Add _ -> 1

Remove -> -1 _ -> 0

(12)

Observational Equivalence

(===) :: (Eq a,Show a,Arbitrary a) => [Action a] -> [Action a] -> Property c === c’ =

forAll (actions 0) $ \ pref ->

forAll (actions (delta (pref ++ c))) $ \ suff -> let observe x = fst (runState ( do q <- empty perform q (pref++x++suff)) initState )

(13)

Example Properties

prop_FrontAdd m n =

[Add m, Add n, Front] === [Add m, Front, Add n]

prop_AddRemove m n =

[Add m, Add n, Remove] === [Add m, Remove, Add n]

(14)

Observational Equivalence, Modified

c ==ˆ c’ =

forAll (actions (delta c)) $ \ suff -> let observe x = fst (runState ( do q <- empty perform q (x ++ suff )) initState) in observe c == observe c’

(15)

Remaining Properties

prop_FrontEmpty =

[Front] ==ˆ [Return Nothing]

prop_FrontAddEmpty m =

[Add m, Front] ==ˆ [Add m, Return (Just m)]

prop_AddRemoveEmpty m = [Add m, Remove] ==ˆ []

(16)

Model-based Testing

• The imperative queue is tested against an abstract (functional) model of queues

• using an abstraction function

emptyS = []

addS a q = ((), q ++ [a]) removeS (_ : q) = ((), q) frontS [] = (Nothing, [])

(17)

Hoare triples

{p} x ← e {q x}

read: under precondition p, program e delivers a value x and ends in a state such that q x holds.

In QuickCheck:

pre p

x <- run e assert q

(18)

Alternative Definition of Observat. Equality

(====) :: (Eq a,Show a,Arbitrary a) =>

[Action a] -> [Action a] -> Property c ==== c’ = imperative(

forAllM (actions 0) $ \ pref ->

forAllM (actions (delta (pref ++ c))) $ \ suff ->

do q <- run empty

obs1 <- run (perform q (pref++c++suff)) obs2 <- run (perform q (pref++c++suff)) assert (obs1==obs2)

(19)

Dynamic Logic

• [x ← p]ϕ (read: after all possible runs of p, ϕ holds)

• < x ← p > ϕ (read: after some possible run of p, ϕ holds)

• This makes a difference for non-deterministic programs (e.g. ParSec combinator parser)

• danymic logic has highest degree of flexibility, but not testing support (yet)

(20)

Summary

• QuickCheck allows for testing monadic code

• Pre- and postconditions can be formulated, as in Hoare triples

• Sequences of monadic actions and their

well-formedness have to be implemented in an ad-hoc way

• Dynamic logic overcomes this problem, but only provides theorem proving, no testing

References

Related documents

Global biodiversity, stoichiometry and ecosystem function responses to human- 2 induced C-N-P imbalances.. Changes resulting from the publishing process, such as peer

During the Welcome Programme, you will be informed of the Undergraduate Office for your course (this information is also detailed on your offer letter) and the member of teaching

bool priority queue:: empty () const ; C ontainer::size type. priority queue:: size () const

The System Security Awareness for Transit Employees course is designed for front-line employees and supervisors who have direct contact with the public or the vehicles and

Finally, PMRQ approximation of the average waiting time in TES+/G/1, MMPR, was compared with the analytic values of the same value in the spectral solution of the Lindley’s

Curriculum and Instruction Advanced – Science Education Option page 10 M.Ed.. Curriculum and Instruction Advanced – Special Education Option page 11

$ If all agents are logged out of an ACD Queue, a transferred call to the ACD Pilot number will be placed in queue if an Overflow Destination is setup (Version 3000 or higher

Regional Cooperative Agreements (RCA): RCAs awarded by the County of Orange are intended to be used as cooperative agreements against which individual