Dynamic programming using histomorphisms Jevgeni Kabanov Viinistu, - - PowerPoint PPT Presentation

dynamic programming using histomorphisms
SMART_READER_LITE
LIVE PREVIEW

Dynamic programming using histomorphisms Jevgeni Kabanov Viinistu, - - PowerPoint PPT Presentation

Dynamic programming using histomorphisms Jevgeni Kabanov Viinistu, 2005 Catamorphism (fold) Structural recursion combinator Generic foldr (Haskell) Eats (folds) trees from bottom-up, producing combined result Similar to Visitor


slide-1
SLIDE 1

Dynamic programming using histomorphisms

Jevgeni Kabanov Viinistu, 2005

slide-2
SLIDE 2

Catamorphism (fold)

  • Structural recursion combinator
  • Generic foldr (Haskell)
  • Eats (folds) trees from bottom-up, producing

combined result

  • Similar to Visitor pattern in OOP, but doesn’t

update structures

slide-3
SLIDE 3

Sum fold animation

  • 1
  • 5
  • 3
  • 7

Let’s count this tree sum...

slide-4
SLIDE 4

Sum fold animation

  • 1
  • 5
  • 3
  • 7
slide-5
SLIDE 5

Sum fold animation

  • 1
  • 5
  • 3
  • 7
slide-6
SLIDE 6

Sum fold animation

  • 1

5

  • 3
  • 7
slide-7
SLIDE 7

Sum fold animation

  • 1
  • 5
  • 3
  • 7
slide-8
SLIDE 8

Sum fold animation

slide-9
SLIDE 9

Sum fold animation

  • 6
  • 3
  • 7
slide-10
SLIDE 10

Sum fold animation

  • 6
  • 3
  • 7
slide-11
SLIDE 11

Sum fold animation

  • 6
  • 3
  • 7
slide-12
SLIDE 12

Sum fold animation

  • 6
  • 3

7

slide-13
SLIDE 13

Sum fold animation

  • 6
  • 3
  • 7
slide-14
SLIDE 14

Sum fold animation

  • 6
slide-15
SLIDE 15

Sum fold animation

  • 6
  • 10
slide-16
SLIDE 16

Sum fold animation

slide-17
SLIDE 17

Sum fold animation

  • 16

And the result is 16 = 1 + 5 + 3 + 7

slide-18
SLIDE 18

Histomorphism

  • Introduced by Varmo & Tarmo in 1999
  • Course-of-value structural recursion combinator
  • Inspired by dynamic programming technique
  • Moves bottom-up annotating the tree with results
  • Allows to reuse sub(-sub)* node results
  • Finally collapses the tree producing the end result
slide-19
SLIDE 19

Funny sum histo animation

  • 1
  • 5
  • 3
  • 7

Let’s count this tree (funny) sum...

slide-20
SLIDE 20

Funny sum histo animation

  • 1
  • 5
  • 3
  • 7
slide-21
SLIDE 21

Funny sum histo animation

  • 1
  • 5
  • 3
  • 7
slide-22
SLIDE 22

Funny sum histo animation

  • 1

5

  • 3
  • 7
slide-23
SLIDE 23

Funny sum histo animation

  • 1
  • 5
  • 3
  • 7
slide-24
SLIDE 24

Funny sum histo animation

slide-25
SLIDE 25

Funny sum histo animation

  • 6
  • 1
  • 5
  • 3
  • 7
slide-26
SLIDE 26

Funny sum histo animation

  • 6
  • 1
  • 5

3

  • 7
slide-27
SLIDE 27

Funny sum histo animation

  • 6
  • 1
  • 5
  • 3
  • 7
slide-28
SLIDE 28

Funny sum histo animation

  • 6
  • 1
  • 5
  • 3

7

slide-29
SLIDE 29

Funny sum histo animation

  • 6
  • 1
  • 5
  • 3
  • 7
slide-30
SLIDE 30

Funny sum histo animation

  • 6
slide-31
SLIDE 31

Funny sum histo animation

  • 6
  • 10
  • 1
  • 5
  • 3
  • 7
slide-32
SLIDE 32

Funny sum histo animation

slide-33
SLIDE 33

Funny sum histo animation

  • 24
  • 6
  • 10
  • 1
  • 5
  • 3
  • 7
slide-34
SLIDE 34

Funny sum histo animation

  • 24

And the result is 24 = 1 × 2 + 5 + 3 + 7 × 2

slide-35
SLIDE 35

Generic hylomorphism

  • General recursion combinator
  • 2 stages:
  • 1. Build an intermediate structure using unfold
  • 2. Collapse the intermediate structure using fold
  • The intermediate structure corresponds to the

implicit call tree

  • The intermediate structure does not really have to be

built

slide-36
SLIDE 36

Dynamic hylomorphism

  • Dynamic recursion combinator
  • The fold is replaced by the histomorphism

FA

F[ (ϕ) ]

  • A

ϕ

  • [

(ϕ) ]

  • f
  • FµF

in

  • F[

({ | ψ | },in−1) ]

  • µF

{ | ψ | }

  • F(Fν(B))

ψ

B

slide-37
SLIDE 37

Challenges

  • Histomorphism expressive power
  • Dynamic hylomorphism expressive power
  • Properties of transformation to dynamic recursion
  • Deriving dynamic definition
slide-38
SLIDE 38

Case study

  • Fibonacci numbers
  • Binary partition number
  • Levenshtein (Edit) distance
  • Longest common subsequence
  • Only first two can be defined as pure histomorphisms
  • General recursion is needed
slide-39
SLIDE 39

Inspiration

Fibonacci dependency tree n

  • n − 1
  • n − 2
  • n − 2

n − 3 n − 3 n − 4 Collapsed dependency graph n

  • n − 1
  • n − 2
  • n − 3

· · ·

slide-40
SLIDE 40

Inspiration (2)

Levenshtein (Edit) distance dependency tree

Dij

  • Di−1j
  • Di−1j−1
  • Dij−1
  • Di−2j Di−2j−1 Di−1j−1 Di−2j−1 Di−2j−2 Di−1j−2 Di−1j−1 Di−1j−2 Dij−2
slide-41
SLIDE 41

Inspiration (3)

Levenshtein (Edit) distance collapsed dependency graph Dij

  • Di−1j
  • Dij−1
  • Di−2j
  • Di−1j−1
  • Dij−2
  • Di−3j

Di−2j−1 Di−1j−2 Dij−3

slide-42
SLIDE 42

Transformation

  • Original definition: f = ψ ◦ Tf ◦ ϕ
  • Dynamic definition: f = ψ ◦ σ ◦ T′[

(f, in−1) ] ◦ ϕ′ – ϕ′ generates more compact intermediate structure – T′ defines the structure recursive pattern – σ restores one level of the old structure – σ and T′ are uniquely determined by ϕ′

  • The consumer (algebra) part is preserved
  • The producer (coalgebra) part is consistently

updated

slide-43
SLIDE 43

Dependency algebra

Let

  • Original dependency producers: hi : A → A
  • Dynamic dependency producers: h′

j : A → A

  • Projections: πi : Tν(C) → Tν(C),

πi = [in, outi ◦ outr] ◦ in−1

  • Deep projections:

π∗

i = outl ◦π′ kl ◦ π′ kl−1 ◦ · · · ◦ π′ k2 ◦ outk1

  • Induction indicator: p : A → Bool
slide-44
SLIDE 44

Dependency algebra (2)

Then

  • ϕ = (id + id, h1, h2, . . . , hn) ◦ p?
  • ϕ′ = (id + id, h′

1, h′ 2, . . . , h′ m) ◦ p′?

  • σ = [inl, (out0 +out0, π∗

1, π∗ 2, . . . , π∗ n) ◦ (p ◦ out0)?]

And ϕ′ has to satisfy following for each i ∈ I, each s ∈ S: P(s, i) = k1, k2, . . . , kl ∈ J∗

  • utl ◦πi ◦ [

(id, ϕ) ] = outl ◦π′

kl ◦ π′ kl−1 ◦ · · · ◦ π′ k1 ◦ [

(id, ϕ′) ] hi(s) = h′

kl ◦ h′ kl−1 ◦ · · · ◦ h′ k1(s)

slide-45
SLIDE 45

Future work

  • More categorical approach to transformation
  • A solid proof for dependency algebra
  • (Semi)-automatical derivation for restricted cases