• Keine Ergebnisse gefunden

Verifying Functions

N/A
N/A
Protected

Academic year: 2022

Aktie "Verifying Functions"

Copied!
9
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

5. Verifying Functions 5.0

Chapter 5

Verifying Functions

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 248

5. Verifying Functions 5.0

Overview of Chapter

5. Verifying Functions 5.1 Introduction

5.2 Case study: Greatest common devisor 5.3 Well-definedness of total recursive functions 5.4 Case study: Quicksort

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 249

5. Verifying Functions 5.1 Introduction

Section 5.1

Introduction

5. Verifying Functions 5.1 Introduction

Motivation

Verifying properties of functions

Verifying properties of functions is a fundamental task in theorem proving and software engineering:

Functions allow to express recursive algorithms

Functions can be used to model systems (e.g., a compiler is essentially a function)

Functions are used to specify input/output behavior of procedures, so calledIO-properties

Verifying recursive functions is related to terminiation proofs

(2)

5. Verifying Functions 5.1 Introduction

Specification

Kinds of specifications:

specification = model + properties

=⇒ verify that model has the properties or

specification = model1+ model2+ relationship

=⇒ verify that models are in the relationship

Here:

specification = function definition + property of function

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 252

5. Verifying Functions 5.1 Introduction

Basic proof techniques

Verify:

well-definedness of function by:

I structural induction according to parameter types

I more general: well-founded ordering on parameter space:

“show that parameters get smaller”

property of defined function:

I structural induction according to parameter types

I in general, proof technique depends on properties

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 253

5. Verifying Functions 5.1 Introduction

Discussion

Verification

checks for consistency of models and properties

I models may not reflect what designer/programmer had in mind

I properties may not reflect what designer/programmer had in mind

works for the full parameter space (in contrast to testing)

discovers also “pathological” problems

uses redundancy to find errors

helps to improve the descriptions

Formalverification avoids misunderstanding, allows using tools, and avoids errors in proofs.

5. Verifying Functions 5.2 Case study: Greatest common devisor

Section 5.2

Case study: Greatest common devisor

(3)

5. Verifying Functions 5.2 Case study: Greatest common devisor

Mathematical specification of gcd

Sepcification

The functiongcdshould have the following property:

Form, nwithm ≥0, n≥0, m,nnot both zero, it holds:

gcd m n=max{k |k dividesm andn}

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 256

5. Verifying Functions 5.2 Case study: Greatest common devisor

Algorithm and property

Function definition

fun gcd :: "nat ⇒ nat ⇒ nat" where

"gcd m 0 = m" |

"gcd m n = gcd n (m mod n)"

Property of function theorem gcd_greatest:

"(k dvd m ∧ k dvd n ∧ (0<m ∨ 0<n)) −→ (k ≤ gcd m n)"

Proofs:

» Gcd.thy

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 257

5. Verifying Functions 5.2 Case study: Greatest common devisor

Mathematical proof of gcd

Lemma:

Form ≥0,n>0 we have:

k dividesmandn ⇔ k dividesnandk divides (mmodn) Proof of gcd by structural induction:

We show:

a) gcdis correct forn=0 and arbitrarym.

b) Induction hypothesis:

gcdis correct for all pairs(m,k) for arbitraryk ≤nandm;

Show:

gcdis correct for all pairs(m,n+1)for arbitrarym.

5. Verifying Functions 5.2 Case study: Greatest common devisor

Mathematical proof of gcd (2)

(a) Induction base:

gcd m 0

= m

=

max { k | k divides m }

=

max { k | k divides m and 0 }

(4)

5. Verifying Functions 5.2 Case study: Greatest common devisor

Mathematical proof of gcd (3)

(b) Induction step:

Assumptions: nis given.

For all pairs(m,k)withk ≤nit holds: gcdis correct for(m,k) Show: For allmit holds:gcdis correct for(m,n+1)!

gcd m (n+1)

= (* Declaration of gcd *) gcd (n+1) (m mod (n+1))

= (* m mod (n+1) ≤ n and induction hypothesis *) max { k | k divides (n+1) and (m mod (n+1)) }

= (* Lemma *)

max { k | k divides m and (n+1)}

QED.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 260

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Section 5.3

Well-definedness of total recursive functions

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 261

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Outline

Well-definedness proofs:

Show that there exists a well-founded relationwfon the arguments

Show that arguments in recursive calls are smaller w.r.t.wf

What we need:

Well-founded relations and induction

Relations: Relations are sets in Isabelle/HOL

Sets

» Sections 6.1, 6.2, 6.4 of Isabelle/HOL Tutorial

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Sets in HOL

Introduction

Sets in HOL differ from sets in set theory:

All elements of a set have the same type, sayα.

Sets are typed:αset

Only some values are sets in HOL.

(5)

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Intersection, complement, difference

Sample deduction rules for intersection:

~c ∈A;c ∈B=⇒cAB (IntI) c ∈AB=⇒cA (IntD1) c ∈AB=⇒cB (IntD2)

Set complement and difference:

(c ∈ −A) = (c <A) (Compl_iff)

−(A∪B) =−A ∩ −B (Compl_Un) A ∩(B−A) ={} (Diff_disjoint) A ∪ −A =UNIV (Compl_partition)

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 264

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Subsets, extensionality, equality

Subsets:

(Vx.x ∈A =⇒xB) =⇒AB (subsetI)

~A ⊆B;c ∈A=⇒cB (subsetD) (A∪BC) = (A ⊆CBC) (Un_subset_iff)

Extensionality and equality of sets:

(Vx.(x ∈A) = (x ∈B)) =⇒ A =B (set_ext)

~A ⊆B;B ⊆A=⇒A =B (equalityI)

~A =B;~A ⊆B;B ⊆A=⇒P=⇒P (equalityE)

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 265

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Set comprehension

Subsets:

(a ∈ {x.P x}) =P a (mem_Collect_eq) {x.x ∈A}=A (Collect_mem_eq) Some simple facts:

lemma "{x. P xxA}={x. P x} ∪A"

lemma "{x. P x−→Q x}=−{x. P x} ∪ {x. Q x}"

More convenient syntax, example:

{pq|p q.p∈primeqprime}

={z.∃p q.z =p∗qpprimeqprime}

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Binding operators

Universal and existential quantification:

(Vx.x ∈A =⇒ P x) =⇒ ∀xA.P x (ballI)

~∀xA.P x; x ∈A=⇒P x (bspec)

~P x; x ∈A=⇒ ∃xA.P x (bexI)

~∃xA.P x; Vx.~x ∈A; P x=⇒ Q=⇒Q (bexE) Unions over parameterized sets, writtenSx

A.B x. There is one basic law and two natural deduction rules:

(b ∈(Sx

A.B x)) = (∃xA.b ∈B x) (UN_iff)

~a ∈A; b ∈B a=⇒b ∈(Sx

A.B x) (UN_I)

~b ∈(SxA.B x); Vx.~x ∈A; b ∈B x=⇒R=⇒R (UN_E)

(6)

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Relations in HOL

Introduction

A relation in Isabelle/HOL is a set of pairs.

Relations are often defined by

composition

closure of another relation

inverse image of a relation w.r.t. a function

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 268

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Relation basics

Identity and composition of relations:

Id ≡ {p.∃x.p= (x,x)} (Id_def) r O s ≡ {(x,z).∃y.(x,y)∈s∧(y,z)∈r } (rel_comp_def)

R O Id =R (R_O_Id)

~r0r; s0s=⇒r0O s0r O s (rel_comp_mono)

The converse or inverse of a relation exchanges the operands:

((a,b)∈r1) = ((b,a)∈r) (converse_iff)

Here is a typical lemma proved about converse and composition:

lemma converse_rel_comp: "(r O s)1=s1 O r1"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 269

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Closures

Isabelle/HOL defines the reflexive transitive closurerof a relation as the least solution/fixpointof the equation:

r =Id∪(r O r) (rtrancl_unfold) Basic properties:

(a,a)∈r (rtrancl_refl)

p∈r =⇒ pr (r_into_rtrancl)

~(a,b)∈r; (b,c)∈r=⇒ (a,c)∈r (rtrancl_trans)

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Inverse image

Let

r of type(α×α)setand

fbe be a function of typeβ⇒α Theinverse imageofr w.r.t. tofis:

inv_image r f ≡ {(x,y).(f x, f y)∈r } (inv_image_def) Remark

Inverse images are helpful for defining a new well-founded relation from a known well-founded relationr.

(7)

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Well-founded relations

Intuitively, a relation≺iswell-foundedif every descending chain of elements is finite; i.e., there is no infinite descending chain of elements a0, a1. . .:

· · · ≺a2a1a0

Isabelle/HOL provides a predicatewfthat asserts that a relation is well-founded; e.g., forless_than:: (nat×nat)set:

((x,y)∈less_than) = (x <y) (less_than_iff) wf less_than (wf_less_than)

Problem

It can be difficult to provewf r for a relationr.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 272

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Proving well-foundedness

Proof method

To prove that a relationr is well-founded, show that it is an inverse image of a well-founded relation w.r.t. to some “measure” functionf.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 273

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Example proof of well-foundedness

Let

definition shorter :: "(’a list × ’a list) set" where

" shorter = { (xl ,yl) . length xl < length yl }"

Lemma: shorteris well-founded, i.e.,"wf shorter"

Proof: Show thatshorteris inverse image of measure functionlength:

" shorter = inv_image less_than length "

Then,"wf less_than"and

theorem wf_inv_image: "wf r =⇒ wf ( inv_image r f)"

imply"wf shorter"

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Proving well-definedness of functions

Well-definedness

A recursively defined function iswell-definedif the arguments in all recursive calls are smaller w.r.t. some well-founded relation.

Proving well-definedness

Provide a so-calledmeasurefunctionf from the arguments tonat.

Any such function defines a well-founded relation on the argument space:

measure ≡ inv_image less_than (measure_def) wf (measure f) (wf_measure)

Show that the arguments of the recursive calls get smaller w.r.t.f.

(8)

5. Verifying Functions 5.3 Well-definedness of total recursive functions

Well-founded induction

Induction proofs based on well-founded relations Well-founded relationsr can be used for induction proofs:

A property holds for all elements iff we can show that it holds for an elementx assuming it holds for all predecessors.

In Isabelle/HOL:

~wf r; Vx.∀y.( (y,x)∈r −→P y) =⇒ P x=⇒ P a (wf_induct) Remark

Note that in well-founded inductions, there is no explicit induction base.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 276

5. Verifying Functions 5.4 Case study: Quicksort

Section 5.4

Case study: Quicksort

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 277

5. Verifying Functions 5.4 Case study: Quicksort

Analysing algorithms

Case study Quicksort

We analyse a functional version of the quicksort algorithm.

function qsort :: "(’a::linorder ) list ⇒ ’a list" where

"qsort [] = []"

| "qsort (p#l) = qsort ( qsplit (op <) p l)

@ p # qsort ( qsplit (op ≥) p l)"

wherelinorderis a type class supporting "<" and "≥" and

primrec qsplit :: "(’a ⇒ ’a ⇒ bool) ⇒ ’a :: linorder ⇒

’a list ⇒ ’a list" where

" qsplit cr p [] = []"

| " qsplit cr p (h # t) =

(if cr h p then h # qsplit cr p t else qsplit cr p t)"

5. Verifying Functions 5.4 Case study: Quicksort

Properties to prove

Properties:

1. Well-definedness of qsort 2. (Well-definedness of qsplit) 3. Sortedness of result

4. Result is a permutation of input list

(9)

5. Verifying Functions 5.4 Case study: Quicksort

Specifying sortedness

Sortedness:

fun qsorted :: "’a :: linorder list ⇒ bool" where

" qsorted [] = True"

| " qsorted [x] = True"

| " qsorted (a # b # l) = (b ≥ a ∧ qsorted (b # l))"

lemma qsort_sorts: " qsorted (qsort xl)"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 280

5. Verifying Functions 5.4 Case study: Quicksort

Specifying the permutation property

Permutation using a multiset abstraction:

primrec count :: "’a list ⇒ ’a ⇒ nat" where

"count [] = (λ x. 0)"

| "count (h # t) = (count t) (h := count t h + 1)"

lemma qsort_preserves: "count (qsort xl) = count xl"

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 281

5. Verifying Functions 5.4 Case study: Quicksort

Verification

The proofs for the properties are presented step by step in the lecture.

Resulting theory with proofs:

» Quicksort.thy

Referenzen

ÄHNLICHE DOKUMENTE

In this paper, we introduce a model-based code mining framework, which allows to identify the variability between related variants realized in source code of OOP languages..

(s3) Here, we essentially use the property x#(rev yl) = rev (yl@[x]) and the fact that by taking the tail of the list reachable from appl, the corresponding prefix gets longer by

5.2 Case study: Greatest common devisor 5.3 Well-definedness of recursive functions 5.4 Case study: Quicksort.. ©Arnd Poetzsch-Heffter

A recursively defined function is well-defined if the arguments in all recursive calls are smaller w.r.t. some

XPath has a large set of built-in functions (even more in XPath 2.0) that can be used in XPath predicates and in XSLT scripts for computing values from document nodes.

1) Karten mit schwarzen Zahlen der Reihe nach auslegen. Jeder schwarzen Zahl eine passende rote Zahl zuordnen, so dass das Ergebnis immer 5 ist. 2) Karten an zwei Spieler

Die Kärtchen von 1-10 werden ausgedruckt (dickeres Papier, Karton, etc. verwenden) und anschließend ausgeschnitten.. Die Größe der Kärtchen

Removing intermediate data structures is called deforestation, since data structures are tree-shaped in the general case. In our case, we would like to fuse an unfold followed by