Purely Functional Data Structures

Post on 31-May-2015

96 views 1 download

Tags:

description

Original slides of my 2013-05-21 talk at Riviera Func. I've got a much improved release of it here: https://docs.google.com/presentation/d/11jFHionOcw-TPII8WaLG-enFKdCCollgZXZEkBYjjcM/pub?start=false&loop=false&delayms=3000 (I still haven't found the right way to copy it over to slideshare while keeping the animations tolerable—get in touch if you know how)

Transcript of Purely Functional Data Structures

Purely Functional Data StructuresJean-Baptiste MazonRiviera Func 2013-05-21

OHAI

Jean-Baptiste Mazon@jbmazon

Sophia-AntipolisPL

(Google Docs)

Purely Functional Data Structures

About — What — Why — How

Persistence

● ephemeral● partially persistent● fully persistent● confluently persistent

immutable data ⇒ fully persistent structures

Persistence: linked lists

Persistence: concatenated lists

Persistence: binary search tree

Persistence: insertion

Amortization

Mutable textbook example: autosized vector

Okasaki examples:● queues● binomial heaps● splay heaps● pairing heaps

Okasaki examples:● queues● binomial heaps● splay heaps● pairing heaps

Naive queue

1 2 3 n-2 n-1 n

Amortized queue

...breaks with persistence

1 2 3

n-2n-1n

Persistence and Amortization

execution traceexpensive operation

call-by-valuecall-by-namecall-by-need

stream

Lazy Evaluation

1 2 3

321

f

r

Persistent Amortized Queue

1 1f r

better than amortized?

Realtime Queue

problem with reverse

rotate(f,r,a) = f ++ reverse(r) ++ arotate(f|fs,r|rs,a) = f | rotate(fs,rs,r|a)

“scheduling”

Numerics

Lists

List =● Empty● Cons(e,List)

Peano arithmetic

Nat =● Zero● Succ(Nat)

Numerics: random access list

… but cons, head, tail are O(lg N)

0 1 2 3 4 5 6, ,

Numerics: zeroless

how

consequence on listconsequence on first tree

head O(1)tail and cons O(1)?

Numerics: redundancy

22222111111011111

Numerics: quaternary

But wait, there's more!

● not only queues● skew binary● bootstrapping● implicit recursive

slowdown

KTHXBYE

Image Credits

Alexberlioz [CC-BY-SA-3.0], from Wikimedia CommonsMarc NL [Public domain], from Wikimedia CommonsJ.J. [CC-BY-SA-3.0], from Wikimedia Commons