functional data structures · functional data structures with isabelle/hol tobias nipkow fakult at...

118
Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult¨ at f¨ ur Informatik Technische Universit¨ at M¨ unchen 2017-2-3 1

Upload: others

Post on 05-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Functional Data Structureswith Isabelle/HOL

Tobias Nipkow

Fakultat fur InformatikTechnische Universitat Munchen

2017-2-3

1

Page 2: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Part II

Functional Data Structures

2

Page 3: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Chapter 1

Binary Trees

3

Page 4: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

4

Page 5: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

5

Page 6: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Library/Tree.thy

6

Page 7: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Binary trees

datatype ′a tree = Leaf | Node ( ′a tree) ′a ( ′a tree)

Abbreviations:

〈〉 ≡ Leaf〈l, a, r〉 ≡ Node l a r

In the sequel: tree = binary tree

7

Page 8: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

8

Page 9: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Tree traversal

inorder :: ′a tree ⇒ ′a list

inorder 〈〉 = []inorder 〈l, x, r〉 = inorder l @ [x] @ inorder r

preorder :: ′a tree ⇒ ′a list

preorder 〈〉 = []preorder 〈l, x, r〉 = x # preorder l @ preorder r

postorder :: ′a tree ⇒ ′a list

postorder 〈〉 = []postorder 〈l, x, r〉 = postorder l @ postorder r @ [x]

9

Page 10: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Sizesize :: ′a tree ⇒ nat

|〈〉| = 0|〈l, , r〉| = |l| + |r| + 1

size1 :: ′a tree ⇒ nat|t|1 = |t| + 1

=⇒|〈〉|1 = 1|〈l, x, r〉|1 = |l|1 + |r|1

Lemma The number of leaves in t is |t|1.

Warning: |.| and |.|1 only on slides10

Page 11: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Height

height :: ′a tree ⇒ nat

h(〈〉) = 0h(〈l, , r〉) = max (h(l)) (h(r)) + 1

Warning: h(.) only on slides

Lemma h(t) ≤ |t|

Lemma |t|1 ≤ 2h(t)

11

Page 12: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

12

Page 13: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

3 Interlude: Arithmetic in IsabelleNumeric TypesChains of (In)EquationsProof Automation

13

Page 14: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Numeric types: nat, int, real

Need conversion functions (inclusions):

int :: nat ⇒ intreal :: nat ⇒ real

real of int :: int ⇒ real

If you need type real,import theory Complex Main instead of Main

14

Page 15: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Numeric types: nat, int, real

Isabelle inserts conversion functions automatically(with theory Complex Main)

If there are multiple correct completions,Isabelle chooses an arbitrary one

Examples(i::int) + (n::nat) i + int n

((n::nat) + n) :: real real(n+n), real n + real n

15

Page 16: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Numeric types: nat, int, real

Coercion in the other direction:

nat :: int ⇒ natfloor :: real ⇒ int

ceiling :: real ⇒ int

16

Page 17: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Overloaded arithmetic operations

• Numbers are overloaded: 0, 1, 2, ... :: ′a

• Basic arithmetic functions are overloaded:op +, op −, op ∗ :: ′a ⇒ ′a ⇒ ′a− :: ′a ⇒ ′a

• Division on nat and int:op div, op mod :: ′a ⇒ ′a ⇒ ′a

• Division on real: op / :: ′a ⇒ ′a ⇒ ′a

• Exponentiation with nat: op ˆ :: ′a ⇒ nat ⇒ ′a

• Exponentiation with real: op powr :: ′a ⇒ ′a ⇒ ′a

• Absolute value: abs :: ′a ⇒ ′a

17

Page 18: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

3 Interlude: Arithmetic in IsabelleNumeric TypesChains of (In)EquationsProof Automation

18

Page 19: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Chains of equationsTextbook proof

t1 = t2 〈justification〉= t3 〈justification〉...= tn 〈justification〉

In Isabelle:

have "t1 = t2" 〈proof〉also have "... = t3" 〈proof〉

...also have "... = tn" 〈proof〉finally have "t1 = tn" .

“...” is literally three dots19

Page 20: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Chains of equations and inequations

Instead of = you may also use ≤ and <.

Examplehave "t1 < t2" 〈proof〉also have "... = t3" 〈proof〉

...also have "... ≤ tn" 〈proof〉finally have "t1 < tn" .

20

Page 21: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

How to interpret “...”

have "t1 ≤ t2" 〈proof〉also have "... = t3" 〈proof〉Here “...” is internally replaced by t2

In general, if this is the formula p t1 t2 where p is someconstant, then “...” stands for t2.

21

Page 22: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

3 Interlude: Arithmetic in IsabelleNumeric TypesChains of (In)EquationsProof Automation

22

Page 23: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Linear formulasOnly:

variables

numbers

number ∗ variable

+, −=, ≤, <

¬, ∧, ∨, −→, ←→

ExamplesLinear: 3 ∗ x + 5 ∗ y ≤ z −→ x < zNonlinear: x ≤ x ∗ x

23

Page 24: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Extended linear formulas

Also allowed:

min, max

even, odd

t div n, t mod n where n is a number

conversion functions

nat, floor, ceiling, abs

24

Page 25: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Automatic proofof arithmetic formulas

by arith

Proof method arith tries to solve arithmetic formulas.

• Succeeds or fails

• Decision procedure for extended linear formulas;for types nat and int, the extended linear formulasmay also contain ∀ and ∃

• Nonlinear subformulas are viewed as (new) variables;for example, x ≤ x ∗ x is viewed as x ≤ y

25

Page 26: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Automatic proofof arithmetic formulas

by (simp add: algebra simps)

• The lemmas list algebra simps helps to simplifyarithmetic formulas

• It applies associativity, commutativity anddistributivity of + and ∗.

• This may prove the formula, may make it simpler,or may make it unreadable.

• It is a decision procedure for equations over rings(e.g. int)

26

Page 27: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Automatic proofof arithmetic formulas

by (simp add: field simps)

• The lemmas list field simps extends algebra simpsby rules for /

• Can only cancel common terms in a quotient,e.g. x ∗ y / (x ∗ z), if x 6= 0 can be proved.

27

Page 28: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

End of interlude, back to trees . . .

28

Page 29: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Tree.thy

|t|1 ≤ 2h(t)

29

Page 30: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

30

Page 31: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Minimal height

min height :: ′a tree ⇒ nat

mh(〈〉) = 0mh(〈l, , r〉) = min (mh(l)) (mh(r)) + 1

Warning: mh(.) only on slides

Lemma mh(t) ≤ h(t)

Lemma 2mh(t) ≤ |t|1

31

Page 32: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Internal path length

ipl :: ′a tree ⇒ nat

ipl 〈〉 = 0ipl 〈l, , r〉 = ipl l + |l| + ipl r + |r|

Why relevant?

Upper bound?

32

Page 33: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

1 Binary Trees

2 Basic Functions

3 Interlude: Arithmetic in Isabelle

4 More Basic Functions

5 Complete and Balanced Trees

33

Page 34: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Complete treecomplete :: ′a tree ⇒ boolcomplete 〈〉 = Truecomplete 〈l, , r〉 =(complete l ∧ complete r ∧ h(l) = h(r))

Lemma complete t = (mh(t) = h(t))

Lemma complete t =⇒ |t|1 = 2h(t)

Lemma |t|1 = 2h(t) =⇒ complete tLemma |t|1 = 2mh(t) =⇒ complete t

Corollary ¬ complete t =⇒ |t|1 < 2h(t)

Corollary ¬ complete t =⇒ 2mh(t) < |t|134

Page 35: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Complete tree: iplLemma A complete tree of height h has internal pathlength (h − 2) ∗ 2h + 2.

In a search tree, finding the node labelled x takes asmany steps as the path from the root to x is long.Thus the average time to find an element that is in thetree is ipl t / |t|.

Lemma Let t be a complete search tree of height h.The average time to find a random element that is in thetree is asymptotically h − 2 (as h approaches ∞):

ipl t / |t| ∼ h − 2

35

Page 36: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Complete tree: ipl

A problem: (h − 2) ∗ 2h + 2 is only correct ifinterpreted over type int, not nat.

Correct version:Lemma complete t =⇒int (ipl t) = (int (h(t)) − 2) ∗ 2h(t) + 2

We do not cover the Isabelle formalization of limits.

36

Page 37: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Balanced tree

balanced :: ′a tree ⇒ bool

balanced t = (h(t) − mh(t) ≤ 1)

Balanced trees have optimal height:Lemma If balanced t ∧ |t| ≤ |t ′| then h(t) ≤ h(t ′).

37

Page 38: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Warning

• The terms complete and balancedare not defined uniquely in the literature.

• For example,Knuth calls complete what we call balanced.

38

Page 39: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Chapter 2

Search Trees

39

Page 40: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BST

7 AVL Trees

8 Red-Black Trees

40

Page 41: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Most of the material focuses onBSTs = binary search trees

41

Page 42: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

BSTs represent sets

Any tree represents a set:

set tree :: ′a tree ⇒ ′a set

set tree 〈〉 = {}set tree 〈l, x, r〉 = set tree l ∪ {x} ∪ set tree r

A BST represents a set that can be searched in timeO(h(t))

Function set tree is called an abstraction functionbecause it maps the implementationto the abstract mathematical object

42

Page 43: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

bst

bst :: ′a tree ⇒ bool

bst 〈〉 = Truebst 〈l, a, r〉 =(bst l ∧ bst r ∧(∀ x∈set tree l. x < a) ∧(∀ x∈set tree r. a < x))

Type ′a must be in class linorder ( ′a :: linorder) wherelinorder are linear orders (also called total orders).

Note: nat, int and real are in class linorder

43

Page 44: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Interface

An implementation of sets of elements of type ′a mustprovide

• An implementation type ′s

• empty :: ′s

• insert :: ′a ⇒ ′s ⇒ ′s

• delete :: ′a ⇒ ′s ⇒ ′s

• isin :: ′s ⇒ ′a ⇒ bool

44

Page 45: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Alternative interface

Instead of a set, a search tree can also implement a mapfrom ′a to ′b:

• An implementation type ′m

• empty :: ′m

• update :: ′a ⇒ ′b ⇒ ′m ⇒ ′m

• delete :: ′a ⇒ ′m ⇒ ′m

• lookup :: ′m ⇒ ′a ⇒ ′b option

Sets are a special case of maps

45

Page 46: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Comparison of elements

We assume that the element type ′a is a linear order

Instead of using < and ≤ directly:

datatype cmp val = LT | EQ | GT

cmp x y =(if x < y then LT else if x = y then EQ else GT)

46

Page 47: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BST

7 AVL Trees

8 Red-Black Trees

47

Page 48: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Implementation

Implementation type: ′a tree

empty = Leaf

insert x 〈〉 = 〈〈〉, x, 〈〉〉insert x 〈l, a, r〉 = (case cmp x a of

LT ⇒ 〈insert x l, a, r〉| EQ ⇒ 〈l, a, r〉| GT ⇒ 〈l, a, insert x r〉)

48

Page 49: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Implementation

isin 〈〉 x = Falseisin 〈l, a, r〉 x = (case cmp x a of

LT ⇒ isin l x| EQ ⇒ True| GT ⇒ isin r x)

49

Page 50: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Implementation

delete x 〈〉 = 〈〉delete x 〈l, a, r〉 =(case cmp x a of

LT ⇒ 〈delete x l, a, r〉| EQ ⇒ if r = 〈〉 then l

else let (a ′, r ′) = del min r in 〈l, a ′, r ′〉| GT ⇒ 〈l, a, delete x r〉)

del min 〈l, a, r〉 =(if l = 〈〉 then (a, r)else let (x, l ′) = del min l in (x, 〈l ′, a, r〉))

50

Page 51: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BSTCorrectnessCorrectness Proof Method Based on Sorted Lists

51

Page 52: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Why is this implementationcorrect?

Because empty insert delete isinsimulate {} ∪ {.} − {.} ∈:

set tree empty = {}set tree (insert x t) = set tree t ∪ {x}set tree (delete x t) = set tree t − {x}isin t x = (x ∈ set tree t)

Under the assumption bst t

52

Page 53: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Also: bst must be invariant

bst emptybst t =⇒ bst (insert x t)bst t =⇒ bst (delete x t)

53

Page 54: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BSTCorrectnessCorrectness Proof Method Based on Sorted Lists

54

Page 55: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

sorted :: ′a list ⇒ bool

sorted [] = Truesorted [x] = Truesorted (x # y # zs) = (x < y ∧ sorted (y # zs))

No duplicates!

55

Page 56: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Structural invariant

The proof method works not just for unbalanced trees.We assume that there is some structural invariant on thesearch tree:

inv : ′s ⇒ bool

e.g. some balance criterion.

56

Page 57: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness of insert

inv t ∧ sorted (inorder t) =⇒inorder (insert x t) = ins list x (inorder t)

where

ins list :: ′a ⇒ ′a list ⇒ ′a list

inserts an element into a sorted list.

Also covers preservation of bst

57

Page 58: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness of delete

inv t ∧ sorted (inorder t) =⇒inorder (delete x t) = del list x (inorder t)

where

del list :: ′a ⇒ ′a list ⇒ ′a list

deletes an element from a sorted list.

Also covers preservation of bst

58

Page 59: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness of isin

inv t ∧ sorted (inorder t) =⇒isin t x = (x ∈ elems (inorder t))

where

elems :: ′a list ⇒ ′a set

converts a list into a set.

59

Page 60: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BST

7 AVL Trees

8 Red-Black Trees

60

Page 61: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Data_Structures/AVL_Set.thy

61

Page 62: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

6 Unbalanced BST

7 AVL Trees

8 Red-Black Trees

62

Page 63: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Data_Structures/RBT_Set.thy

63

Page 64: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Relationship to 2-3-4 trees

64

Page 65: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Red-black trees

datatype color = Red | Black

datatype′a rbt = Leaf | Node color ( ′a tree) ′a ( ′a tree)

Abbreviations:

〈〉 ≡ Leaf〈c, l, a, r〉 ≡ Node c l a r

R l a r ≡ Node Red l a rB l a r ≡ Node Black l a r

65

Page 66: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Color

color :: ′a rbt ⇒ color

color 〈〉 = Blackcolor 〈c, , , 〉 = c

paint :: color ⇒ ′a rbt ⇒ ′a rbt

paint c 〈〉 = 〈〉paint c 〈 , l, a, r〉 = 〈c, l, a, r〉

66

Page 67: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Invariants

rbt :: ′a rbt ⇒ bool

rbt t = (invc t ∧ invh t ∧ color t = Black)

invc :: ′a rbt ⇒ bool

invc 〈〉 = Trueinvc 〈c, l, , r〉 =(invc l ∧ invc r ∧(c = Red −→ color l = Black ∧ color r = Black))

67

Page 68: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Invariants

invh :: ′a rbt ⇒ bool

invh 〈〉 = Trueinvh 〈 , l, , r〉 = (invh l ∧ invh r ∧ bh(l) = bh(r))

bheight :: ′a rbt ⇒ nat

bh(〈〉) = 0bh(〈c, l, , 〉) =(if c = Black then bh(l) + 1 else bh(l))

68

Page 69: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Exercise

Is invh what we want?

Define a function Bpl :: ′a rbt ⇒ nat setsuch that Bpl t (“black path lengths”) is the set of all nsuch that there is a path from the root of t to a leaf thatcontains exactly n black nodes.

Prove invh t =⇒ Bpl t = {bh(t)}

69

Page 70: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Logarithmic height

Lemmarbt t =⇒ h(t) ≤ 2 ∗ log2 |t|1

70

Page 71: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Insertioninsert :: ′a ⇒ ′a rbt ⇒ ′a rbt

insert x t = paint Black (ins x t)

ins :: ′a ⇒ ′a rbt ⇒ ′a rbt

ins x 〈〉 = R 〈〉 x 〈〉ins x (B l a r) = (case cmp x a of

LT ⇒ baliL (ins x l) a r| EQ ⇒ B l a r| GT ⇒ baliR l a (ins x r))

ins x (R l a r) = (case cmp x a ofLT ⇒ R (ins x l) a r| EQ ⇒ R l a r| GT ⇒ R l a (ins x r))

71

Page 72: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Adjusting colorsins x (B l a r) = ... bal (ins x l) a r ... bal l a (ins x r) ...

baliL, baliR :: ′a rbt ⇒ ′a ⇒ ′a rbt ⇒ ′a rbt

• Combine arguments l a r into tree, ideally 〈l, a, r〉• Treat invariant violation Red-Red in l/r

baliL (R (R t1 a1 t2) a2 t3) a3 t4 =R (B t1 a1 t2) a2 (B t3 a3 t4)baliL (R t1 a1 (R t2 a2 t3)) a3 t4 =R (B t1 a1 t2) a2 (B t3 a3 t4)

• Principle: replace Red-Red by Red-Black

• Last equation: baliL l a r = B l a r

• Symmetric: baliR72

Page 73: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness via sorted lists

Lemmainorder (baliL l a r) = inorder l @ a # inorder rinorder (baliR l a r) = inorder l @ a # inorder r

Lemmasorted (inorder t) =⇒inorder (ins x t) = ins list x (inorder t)

Corollarysorted (inorder t) =⇒inorder (insert x t) = ins list x (inorder t)

Proofs easy!73

Page 74: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Preservation of invariant

Theoremrbt t =⇒ rbt (insert x t)

74

Page 75: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Chapter 3

Priority Queues

75

Page 76: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

9 Priority Queues

10 Leftist Heap

11 Skew Heap

12 Priority Queues Based on Braun Trees

76

Page 77: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

9 Priority Queues

10 Leftist Heap

11 Skew Heap

12 Priority Queues Based on Braun Trees

77

Page 78: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Priority queue informally

Collection of elements with priorities

Operations:

• empty• emptiness test• insert• get element with minimal priority• delete element with minimal priority

We focus on the priorities:element = priority

78

Page 79: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Priority queues are multisets

The same element can be contained multiple timesin a priority queue

=⇒The abstract view of a priority queue is a multiset

79

Page 80: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Multisets in Isabelle

Import "Library/Multiset"

80

Page 81: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Interface of implementation

The type of elements (= priorities) ′a is a linear order

An implementation of a priority queue of elements oftype ′a must provide

• An implementation type ′q

• empty :: ′q

• is empty :: ′q ⇒ bool

• insert :: ′a ⇒ ′q ⇒ ′q

• get min :: ′q ⇒ ′a

• del min :: ′q ⇒ ′q

81

Page 82: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

More operations

• merge :: ′q ⇒ ′q ⇒ ′qOften provided

• decrease key/priorityNot easy in functional setting

82

Page 83: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness of implementation

A priority queue represents a multiset of priorities.Correctness proof requires:

Abstraction function: mset :: ′q ⇒ ′a multisetInvariant: invar :: ′q ⇒ bool

83

Page 84: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Correctness of implementationMust prove invar q =⇒mset empty = {#}is empty q = (mset q = {#})mset (insert x q) = mset q + {#x#}mset (del min q) = mset q − {#get min q#}q 6= empty =⇒get min q ∈ set q ∧ (∀ x ∈ set q. get min q ≤ x)where set q = set mset (mset q)

invar emptyinvar (insert x q)invar (del min q)

84

Page 85: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Terminology

A tree is a heap if for every subtreethe root is ≥ all elements in the subtrees.

The term “heap” is frequently used synonymously with“priority queue”.

85

Page 86: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Priority queue via heap

• empty = 〈〉• is empty h = (h = 〈〉)• get min 〈 , a, 〉 = a

• Assume we have merge

• insert a t = merge 〈〈〉, a, 〈〉〉 t

• del min 〈l, a, r〉 = merge l r

86

Page 87: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Priority queue via heap

A naive merge:

merge t1 t2 = (case (t1,t2) of(〈〉, ) ⇒ t2 |( , 〈〉) ⇒ t1 |(〈l1,a1,r1〉, 〈l2,a2,r2〉) ⇒

if a1 ≤ a2 then 〈merge l1 r1, a1, t2〉else 〈t1, a2, merge l2 r2〉

Challenge: how to maintaining some kind of balance

87

Page 88: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

9 Priority Queues

10 Leftist Heap

11 Skew Heap

12 Priority Queues Based on Braun Trees

88

Page 89: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Data_Structures/Leftist_Heap.thy

89

Page 90: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Leftist tree informally

The rank of a tree is the depth of the rightmost leaf.

In a leftist tree, the rank of every left child is ≥ the rankof its right sibling

90

Page 91: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Implementation type

datatype′a lheap = Leaf | Node nat ( ′a tree) ′a ( ′a tree)

Abbreviations 〈〉 and 〈h, l, a, r〉 as usual

Abstraction function:mset tree :: ′a lheap ⇒ ′a multiset

mset tree 〈〉 = {#}mset tree 〈 , l, a, r〉 ={#a#} + mset tree l + mset tree r

91

Page 92: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Heap

heap :: ′a lheap ⇒ bool

heap 〈〉 = Trueheap 〈 , l, a, r〉 = (heap l ∧ heap r ∧(∀ x ∈# mset tree l + mset tree r. a ≤ x))

92

Page 93: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Leftist tree

rank :: ′a lheap ⇒ nat

rank 〈〉 = 0rank 〈 , , , r〉 = rank r + 1

Node 〈n, l, a, r〉: n = rank of node

ltree :: ′a lheap ⇒ bool

ltree 〈〉 = Trueltree 〈n, l, , r〉 =(n = rank r + 1 ∧ rank r ≤ rank l ∧ ltree l ∧ ltree r)

93

Page 94: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Leftist heap invariant

invar h = (heap h ∧ ltree h)

94

Page 95: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Why leftist tree?

Lemma ltree t =⇒ 2rank t ≤ |t|1

Lemma Execution time of merge t1 t2 is bounded byrank t1 + rank t2

95

Page 96: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

mergePrinciple: descend on the right

merge 〈〉 t2 = t2merge t1 〈〉 = t1merge 〈n1, l1, a1, r1〉 〈n2, l2, a2, r2〉 =(if a1 ≤ a2 then node l1 a1 (merge r1 〈n2, l2, a2, r2〉)else node l2 a2 (merge r2 〈n1, l1, a1, r1〉))

node :: ′a lheap ⇒ ′a ⇒ ′a lheap ⇒ ′a lheap

node l a r =(let rl = rk l; rr = rk rin if rr ≤ rl then 〈rr + 1, l, a, r〉 else 〈rl + 1, r, a, l〉)

where rk 〈n, , , 〉 = n

96

Page 97: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

merge

merge 〈n1, l1, a1, r1〉 〈n2, l2, a2, r2〉 =(if a1 ≤ a2 then node l1 a1 (merge r1 〈n2, l2, a2, r2〉)else node l2 a2 (merge r2 〈n1, l1, a1, r1〉))

Function merge terminates because

rank t1 + rank t2

decreases with every recursive call.

97

Page 98: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Functional correctness proofsincluding preservation of invar

Straightforward

98

Page 99: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Logarithmic complexity

Complexity measures t merge, t insert t del min:count calls of merge.

Lemma t merge l r ≤ rank l + rank r + 1Lemma ltree l ∧ ltree r =⇒t merge l r ≤ log2 |l|1 + log2 |r|1 + 1

Lemmaltree t =⇒ t insert x t ≤ log2 |t|1 + 2

Lemmaltree t =⇒ t del min t ≤ 2 ∗ log2 |t|1 + 1

99

Page 100: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Can we avoid the rank info in each node?

100

Page 101: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

9 Priority Queues

10 Leftist Heap

11 Skew Heap

12 Priority Queues Based on Braun Trees

101

Page 102: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Archive of Formal Proofs

https:

//www.isa-afp.org/entries/Skew_Heap.shtml

102

Page 103: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Note: merge is called meld

103

Page 104: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Implementation type

Ordinary binary trees

Invariant: heap

104

Page 105: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

meldPrinciple: swap subtrees when descending

meld h1 h2 =(case h1 of〈〉 ⇒ h2

| 〈l1, a1, r1〉 ⇒case h2 of〈〉 ⇒ h1

| 〈l2, a2, r2〉 ⇒if a1 ≤ a2 then 〈meld h2 r1, a1, l1〉else 〈meld h1 r2, a2, l2〉)

Function meld terminates because . . .105

Page 106: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Functional correctness proofsincluding preservation of heap

Straightforward

106

Page 107: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Logarithmic complexity

Amortized only:Theorem A sequence of n insert, del min and meldoperations runs in time O(n ∗ log n).=⇒Average cost of each operation is O(log n)(even in the worst case)

107

Page 108: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

9 Priority Queues

10 Leftist Heap

11 Skew Heap

12 Priority Queues Based on Braun Trees

108

Page 109: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Archive of Formal Proofs

https://www.isa-afp.org/entries/Priority_

Queue_Braun.shtml

109

Page 110: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

What is a Braun tree?

braun :: ′a tree ⇒ bool

braun 〈〉 = Truebraun 〈l, x, r〉 =(|r| ≤ |l| ∧ |l| ≤ Suc |r| ∧ braun l ∧ braun r)

Lemma braun t =⇒ 2h(t) ≤ 2 ∗ |t| + 1

110

Page 111: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Priority queue implementation

Implementation type: ordinary binary trees

Invariants: heap and braun

No merge — insert and del min defined explicitly

111

Page 112: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

insert

insert :: ′a ⇒ ′a tree ⇒ ′a tree

insert a 〈〉 = 〈〈〉, a, 〈〉〉insert a 〈l, x, r〉 =(if a < x then 〈insert x r, a, l〉 else 〈insert a r, x, l〉)

Correctness and preservation of invariant straightforward.

112

Page 113: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

del min

del min :: ′a tree ⇒ ′a tree

del min 〈〉 = 〈〉del min 〈〈〉, x, r〉 = 〈〉del min 〈l, x, r〉 =(let (y, l ′) = del left l in sift down r y l ′)

113

Page 114: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

sift down

sift down :: ′a tree ⇒ ′a ⇒ ′a tree ⇒ ′a tree

sift down 〈〉 a 〈〉 = 〈〈〉, a, 〈〉〉sift down 〈〈〉, x, 〈〉〉 a 〈〉 =(if a ≤ x then 〈〈〈〉, x, 〈〉〉, a, 〈〉〉else 〈〈〈〉, a, 〈〉〉, x, 〈〉〉)

sift down 〈l1, x1, r1〉 a 〈l2, x2, r2〉 =(if a ≤ x1 ∧ a ≤ x2 then 〈〈l1, x1, r1〉, a, 〈l2, x2, r2〉〉else if x1 ≤ x2 then 〈sift down l1 a r1, x1, 〈l2, x2, r2〉〉

else 〈〈l1, x1, r1〉, x2, sift down l2 a r2〉)

114

Page 115: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Functional correctness proofsfor deletion

including preservation of heap and braun

Many lemmas, mostly straightforward

115

Page 116: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Logarithmic complexity

Running time of insert, del left and sift down (andtherefore del min) bounded by height

Remember: braun t =⇒ 2h(t) ≤ 2 ∗ |t| + 1

=⇒Above running times logarithmic in size

116

Page 117: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Sorting with priority queuepq [] = emptypq (x#xs) = insert x (pq xs)

mins q =(if is empty q then []else get min h # mins (del min h))

sort pq = mins ◦ pq

Complexity of sort: O(n log n)if all priority queue functions have complexity O(log n)

117

Page 118: Functional Data Structures · Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakult at fur Informatik Technische Universit at Munchen 2017-2-3 1. Part II Functional Data

Sorting with priority queuepq [] = emptypq (x#xs) = insert x (pq xs)

Not optimal. Linear time possible.

118