• Keine Ergebnisse gefunden

The Dual of Short Cut Deforestation [Svenningsson 2002]

N/A
N/A
Protected

Academic year: 2022

Aktie "The Dual of Short Cut Deforestation [Svenningsson 2002]"

Copied!
26
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Hilft foldr/build denn immer?

Mal ein anderes Beispiel:

fromTo:: (Ordα,Enum α)⇒α→α→[α]

fromTon m=go n

where go i=if i >m then [ ]

else i : (go (succi))

zip:: [α]→[β]→[(α, β)]

zip[ ] [ ] = [ ]

zip(a:as) (b:bs) = (a,b) : (zip as bs)

Und dann ein Ausdruck mitzweiZwischenergebnissen:

zip(fromTo1 10) (fromTo0a0 0j0) Was nun?

(2)

The Dual of Short Cut Deforestation [Svenningsson 2002]

Lösung: 1. SchreibefromTomittelsunfoldr.

2. Schreibezip mittelsdestroy.

3. Benutze folgende Regel:

destroy cons (unfoldrf b) cons f b Zunächstunfoldr:

data Maybeα=Nothing |Justα

unfoldr:: (β→Maybe (α, β))→β →[α]

unfoldr f b=case f b of Nothing →[ ]

Just (a,b0)→a: (unfoldr f b0) Damit zum Beispiel:

fromTo:: (Ordα,Enumα)⇒α→α→[α]

fromTon m=unfoldrstep n

wherestep i =if i >m thenNothing else Just (i,succi)

2

(3)

Und die destroy-Funktion. . .

Definition:

destroy:: (∀β.(β→Maybe (α, β))→β →γ)→[α]→γ destroy cons =cons match

wobei:

match:: [α]→Maybe(α,[α]) match[ ] =Nothing match(a:as) =Just(a,as) Dann zum Beispiel:

zip:: [α]→[β]→[(α, β)]

zipas bs =destroy(λp x →destroy (λq y →zipD p q x y) bs)as wherezipD =λp q x y →

case(p x,q y) of

(Nothing ,Nothing )→[ ]

(Just(a,x0),Just (b,y0))→(a,b) : (zipD p q x0 y0)

(4)

Und warum ist das jetzt „besser“ als foldr/build?

Aus dem problematischen Beispiel:

zip(fromTo1 10) (fromTo0a0 0j0) wird:

destroy(λp x →destroy (λq y →zipD p q x y) (unfoldr step2 0a0)) (unfoldrstep1 1)

wherezipD =λp q x y →

case(p x,q y) of

(Nothing ,Nothing )→[ ]

(Just(a,x0),Just (b,y0))→(a,b) : (zipD p q x0 y0) step1 i =if i >10 then Nothing

else Just(i,succi) step2 i =if i >0j0 then Nothing

else Just(i,succ i)

4

(5)

Und warum ist das jetzt „besser“ als foldr/build?

. . . und damit lässt sich mittels

destroy cons (unfoldrf b) cons f b wie folgt fortfahren:

destroy(λp x →destroy (λq y →zipD p q x y) (unfoldr step2 0a0)) (unfoldrstep1 1)

where · · ·

destroy(λp x →(λq y →zipD p q x y) step2 0a0) (unfoldrstep1 1)

where · · ·

(λp x→(λq y →zipD p q x y)step2 0a0)step1 1 where · · ·

zipD step1 step2 10a0 where · · ·

(6)

Was heißt hier eigentlich „Dual“?

Zur Erinnerung:

foldr:: (α→β →β)→β →[α]→β foldr f k [ ] =k

foldr f k (a:as) =f a(foldrf k as) unfoldr:: (β→Maybe (α, β))→β →[α]

unfoldr f b=case f b of Nothing →[ ]

Just (a,b0)→a: (unfoldr f b0) Etwas „Transformiererei“ anfoldr:

foldr:: (α→β →β)→β →[α]→β foldrf k l =foldr0 l f k

foldr0 :: [α]→(α→β →β)→β →β

foldr0 l f k =foldr00 l (λp→casep of {Nothing →k;

Just (a,b)→f a b}) foldr00:: [α]→(Maybe (α, β)→β)→β

foldr00 [ ] f0=f0 Nothing

foldr00 (a:as) f0=f0 (Just (a,foldr00 as f0))

6

(7)

Eine nützliche Sicht auf foldr/build

Zur Erinnerung:

build::(∀β.(α→β→β)→β →β)→[α]

build prod =prod (:) [ ] und:

foldr:: (α→β →β)→β→[α]→β foldr f k [ ] =k

foldr f k (a:as) =f a(foldr f k as) bzw.:

foldr0:: [α]→(∀β.(α→β →β)→β →β) foldr0 [ ] f k =k

foldr0 (a:as)f k =f a (foldr0 as f k) Es gelten:

foldr0 ◦build = id build◦foldr0 = id

(8)

Analog für destroy/unfoldr

Zur Erinnerung:

unfoldr:: (β→Maybe (α, β))→β →[α]

unfoldr f b=case f b of Nothing →[ ]

Just (a,b0)→a: (unfoldr f b0) unfoldr0 :: ([α]→γ)→(∀β.(β →Maybe(α, β))→β →γ) unfoldr0 g f b=g (unfoldrf b)

und:

destroy::(∀β.(β→Maybe (α, β))→β →γ)→[α]→γ destroy cons =cons match

wherematch [ ] =Nothing match (a:as) =Just(a,as) Es gelten:

unfoldr0 ◦destroy = id destroy◦unfoldr0 = id

8

(9)

Eine destroy/build-Regel?

Laut Definitionen ist

destroy cons (build prod) das Gleiche wie

cons match (prod (:) [ ]), wobei:

match [ ] =Nothing match (a:as) =Just (a,as)

Warum dann nicht einfach

destroy cons (build prod)

cons id(prod (λa as →Just(a,as)) Nothing) ? Erhält diese Regel die Semantik?

(10)

Beweis der Korrektheit

Alles, was wir übercons und prod wissen, sind ihre Typen:

cons ::∀β.(β →Maybe(τ, β))→β →τ0 und

prod ::∀β.(τ →β→β)→β →β

Aber dies sollte doch wohl reichen, Dank freier Theoreme?

Im Folgenden, eine Beweisskizze.

10

(11)

Wo beginnen?

Das freie Theorem für

cons ::∀β.(β →Maybe(τ, β))→β →τ0, spezialisiert auf die Ebene von Funktionen, ist:

∀τ1, τ2,f ::τ1→τ2.

∀p::τ1 →Maybe(τ, τ1),q::τ2 →Maybe(τ, τ2).

(∀x ::τ1.(p x,q (f x))∈liftMaybe(lift(,)(id,f)))

⇒ ∀y ::τ1. cons p y = cons q (f y)

Zur Erinnerung, wir wollen beweisen:

cons match (prod (:) [ ])

=

cons id(prod (λa as →Just(a,as)) Nothing)

(12)

Wie weiter?

Alles, was wir brauchen, ist eine Funktionf so dass:

1. ∀x :: [τ].(match x,id (f x))∈liftMaybe(lift(,)(id,f)) 2. f (prod (:) [ ]) = prod (λa as →Just(a,as))Nothing

Das freie Theorem für

prod ::∀β.(τ →β→β)→β →β, spezialisiert auf die Ebene von Funktionen, ist:

12

(13)

Wie weiter?

Alles, was wir brauchen, ist eine Funktionf so dass:

1. ∀x :: [τ].(match x,id (f x))∈liftMaybe(lift(,)(id,f)) 2. f (prod (:) [ ] ) = prod (λa as →Just(a,as))Nothing Das freie Theorem für

prod ::∀β.(τ →β→β)→β →β, spezialisiert auf die Ebene von Funktionen, ist:

∀τ1, τ2,f ::τ1→τ2.

∀p::τ →τ1→τ1,q::τ →τ2→τ2.

(∀x ::τ.∀y ::τ1.f (p x y) =q x (f y))

⇒ ∀z ::τ1.f (prod p z) = prod q (f z)

(14)

Fast geschafft

Alles, was wir brauchen, ist eine Funktionf so dass:

1. ∀x :: [τ].(match x,id (f x))∈liftMaybe(lift(,)(id,f))

?

2. ∀x ::τ,y :: [τ].f ( (:) x y) = (λa as →Just(a,as)) x (f y) 3. f [ ] = Nothing

Die letzten beiden Bedingungen lassen keine andere Wahl als: f [ ] =Nothing

f (x :y) =Just (x,f y)

13

(15)

Fast geschafft

Alles, was wir brauchen, ist eine Funktionf so dass:

1. ∀x :: [τ].(match x,id (f x))∈liftMaybe(lift(,)(id,f))

?

2. ∀x ::τ,y :: [τ].f ( (:) x y) = (λa as →Just(a,as)) x (f y) 3. f [ ] = Nothing

Die letzten beiden Bedingungen lassen keine andere Wahl als:

f [ ] =Nothing f (x :y) =Just (x,f y)

(16)

Fast geschafft

Alles, was wir brauchen, ist:

1. ∀x :: [τ].(match x,id (f x))∈liftMaybe(lift(,)(id,f)) ?

2. ∀x ::τ,y :: [τ].f ( (:) x y) = (λa as →Just(a,as)) x (f y) 3. f [ ] = Nothing

Die letzten beiden Bedingungen lassen keine andere Wahl als:

f [ ] =Nothing f (x :y) =Just (x,f y)

13

(17)

Schließlich . . .

Wir haben:

liftMaybe(lift(,)(id,f)) ={(Nothing,Nothing)} ∪

{(Justx1,Justy1) |(x1,y1)∈lift(,)(id,f)} lift(,)(id,f) ={( (x1,x2),(y1,y2) ) |x1 =y1∧f x2 =y2}

Um zu zeigen, dass

∀x :: [τ].(match x,id(f x))∈liftMaybe(lift(,)(id,f)), untersuchen wir alle Fälle für Eingaben vonmatchund f, unter Verwendung der Definitionen:

match [ ] = Nothing f [ ] = Nothing match (a:as) = Just (a,as) f (x :y) = Just (x,f y) Fertig!

(18)

Short Cut Deforestation auf anderen Datentypen?

Zum Beispiel:

dataTreea = Leaf a |Node(Treea) (Treea) incr::Tree Int→Tree Int

incr(Leaf a) =Leaf (a+1)

incr(Nodet1 t2) =Node(incrt1) (incr t2) Ziel:

incr(incrt) ?

Zunächst ein geeignetesfold:

foldTree:: (α→β)→(β →β→β)→Treeα→β foldTreel n (Leaf a) =l a

foldTreel n (Nodet1 t2) =n (foldTreel n t1) (foldTreel n t2)

15

(19)

Short Cut Deforestation auf anderen Datentypen?

Außerdem ein geeignetesbuild:

buildTree:: (∀β.(α→β)→(β →β→β)→β)→Treeα buildTreeprod =prod Leaf Node

Sowie einefoldTree/buildTree-Regel:

foldTreel n (buildTree prod) prod l n Nun:

incr::Tree Int→Tree Int

incrt =buildTree (λl n→foldTree(λa→l (a+1)) (λt1 t2 →n t1 t2) t)

Und schließlich:

incr(incrt) = · · · foldTree · · · (buildTree (λl n→ · · · t)) · · ·

· · ·

(20)

Circular Short Cut Deforestation [Fernandes et al. 2007]

Manchmal möchte man zusätzliche Ausgabe/Information erzeugen:

filterAndCount:: (α→Bool)→[α]→([α],Int) und diese auch weiterverarbeiten:

normalise:: ([Int],Int)→[Float]

Zur Erzeugung:

buildp:: (∀β.(α→β →β)→β→γ →(β, δ))→γ →([α], δ) buildp g c=g (:) [ ]c

Zur Verarbeitung:

pfold:: (α→β→δ →β)→(δ→β)→([α], δ)→β pfoldh1 h2 (as,z) =foldr (λa b→h1 a b z) (h2 z)as

Fusions-Regel:

pfold h1 h2 (buildpg c)

let(b,z) =g (λa b→h1 a b z) (h2 z) c in b

17

(21)

Circular Short Cut Deforestation [Fernandes et al. 2007]

Zur Erzeugung:

buildp:: (∀β.(α→β →β)→β→γ →(β, δ))→γ →([α], δ) buildp g c=g (:) [ ]c

Zur Verarbeitung:

pfold:: (α→β→δ →β)→(δ→β)→([α], δ)→β pfoldh1 h2 (as,z) =foldr (λa b→h1 a b z) (h2 z)as Fusions-Regel:

pfold h1 h2 (buildpg c)

let(b,z) =g (λa b→h1 a b z) (h2 z) c in b

(22)

Circular Short Cut Deforestation [Fernandes et al. 2007]

Zur Erzeugung:

buildp:: (∀β.(α→β →β)→β→γ →(β, δ)) →γ →([α], δ) buildp g c=g (:) [ ]c

Typ vong erzwingt (semantisch gesehen) folgende Form:

λf k c → ,

f a1 f

a2

f an k

z

18

(23)

Circular Short Cut Deforestation [Fernandes et al. 2007]

Zur Verarbeitung:

pfold:: (α→β→δ →β)→(δ→β)→([α], δ)→β pfoldh1 h2 (as,z) =foldr (λa b→h1 a b z) (h2 z)as Für ein konkretes Zwischenergebnis(buildpg c) bedeutet dies:

,

: a1 :

a2

: an [ ]

z

7→

h1

a1 h1

a2

h1

an h2

z z z

z

(24)

Circular Short Cut Deforestation [Fernandes et al. 2007]

pfold h1 h2 (g (:) [ ]c) let(b,z) =g (λa b→h1 a b z) (h2 z) c inb

h1

a1 h1

a2

h1

an h2

z z z

z

fst ,

h1

a1 h1

a2

h1

an h2

z snd

20

(25)

Circular Short Cut Deforestation [Fernandes et al. 2007]

pfold h1 h2 (g (:) [ ]c) let(b,z) =g (λa b→h1 a b z) (h2 z) c inb

h1

a1 h1

a2

h1

an h2

z z z

z

snd ,

h1

a1 h1

a2

h1

an h2

z

(26)

Circular Short Cut Deforestation [Fernandes et al. 2007]

pfold h1 h2 (g (:) [ ]c) let(b,z) =g (λa b→h1 a b z) (h2 z) c inb

h1

a1 h1

a2

h1

an h2

z z z

z h1

a1 h1

a2

h1

an h2

z

20

Referenzen

ÄHNLICHE DOKUMENTE

Alles, was wir über cons und prod wissen, sind ihre Typen:.. cons ::

Dezember 2019 können die Mitgliedstaaten beschließen, bis zu 15 % oder im Falle von Bulgarien, Estland, Spanien, Lettland, Litauen, Polen, Portugal, Rumänien, der Slowakei,

Schmidhauser Corinne Ja/Oui Zimmerli Christoph Ja/Oui Bohnenblust Peter Nein/Non Elsaesser Michael Nein/Non Vogt Hans Rudolf Nein/Non. Flück Peter

Card survey – pros and cons of having siblings1 Task Describe positive and negative things about having siblings. Think of

Given the shortcomings of diversity experiments with respect to the situation in mature stands, it seems straightforward to combine research in synthetic tree stands with

This section presents the three statistical models that we compare: the logistic regression model, the tree & forest model (combining classication trees with random forests),

Animal (3) ___ ( industry ), such as the fur industry and factory farms, try to discredit animal (4) ___ ( protect ) groups by claiming that activists want to take people’s

agreement agreements agrees disagree disagreers disagrees. 2 were