• Keine Ergebnisse gefunden

Case study: Greatest common devisor

N/A
N/A
Protected

Academic year: 2022

Aktie "Case study: Greatest common devisor"

Copied!
7
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

5. Verifying Functions 5.2 Case study: Greatest common devisor

Section 5.2

Case study: Greatest common devisor

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 255

5. Verifying Functions 5.2 Case study: Greatest common devisor

Case study: greatest common devisor

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 256

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}

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 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.

(2)

Mathematical proof of gcd (2)

(a) Induction base:

gcd m 0

= m

=

max { k | k divides m }

=

max { k | k divides m and 0 }

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 259

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

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

(3)

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.

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 263

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)

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}

(4)

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 ∈(Sx

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

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 267

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=⇒r0 O 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=s−1 O r−1"

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)

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

Inverse image

Let

r of typeα×αand

f be be a function of typeβ⇒α Theinverse imageofr w.r.t. tof is:

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

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

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 271

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 proof 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.

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

Example Let

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

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

Proof:

lemma " shorter = inv_image less_than length "

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.

(6)

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 275

Section 5.4

Case study: Quicksort

©Arnd Poetzsch-Heffter et al. TU Kaiserslautern 276

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

(7)

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 279

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 280

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

So to conclude this talk: Minority languages need language technology badly but very few have the human and linguistic resources needed to get going and the

A Lead Role for the PD on the European Left: The European Parliament elections in Italy brought a resounding victory for the centre-left Democratic Party (PD), to the surprise

In the aftermath of NATO’s 2008 Bucharest summit, the Alliance’s deliberations on taking in new members centred on the Western Balkans, a region which includes five non-NATO and

The chosen field frequency f ¼ 160 kHz corresponds to a situation where the particles rapidly align and self-assemble in response to the field without exhibiting any apparent drift due

Besides the concentration of the building blocks, the emulsifier concentration presents a second important parameter in cluster fabrication. Formation of larger

Up to now KCTP has been combined with controlled radical polymerizations like atom transfer radical polymerization (ATRP), 63 reversible addition fragmentation

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

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