Using Circular Programs to Deforest in Accumulating Parameters - - PowerPoint PPT Presentation

using circular programs to deforest in accumulating
SMART_READER_LITE
LIVE PREVIEW

Using Circular Programs to Deforest in Accumulating Parameters - - PowerPoint PPT Presentation

ASIA-PEPM, September 14, 2002 Using Circular Programs to Deforest in Accumulating Parameters Janis Voigtl ander Dresden University of Technology http://wwwtcs.inf.tu-dresden.de/ voigt Supported by the Deutsche


slide-1
SLIDE 1

ASIA-PEPM, September 14, 2002

Using Circular Programs to Deforest in Accumulating Parameters

Janis Voigtl¨ ander

Dresden University of Technology http://wwwtcs.inf.tu-dresden.de/

∼voigt

Supported by the “Deutsche Forschungsgemeinschaft” under grant KU 1290/2-1 and by the “Gesellschaft von Freunden und F¨

  • rderern der TU Dresden” with a travel grant.
slide-2
SLIDE 2

1

Functions with Accumulating Parameters

data Term = Num Int | Add Term Term sum :: [Int] → Term → Term sum [x] y = Add y (Num x) sum (x : xs) y = sum xs (Add y (Num x)) sum : 1 [2] Num ⇒ sum [2] Add Num Num 1 ⇒ Add Add Num Num 1 Num 2 unp :: Term → String → String unp (Num x) z = show x + + z unp (Add x1 x2) z =

  • (
  • : unp x1 (
  • +
  • : unp x2 (
  • )
  • : z))
slide-3
SLIDE 3

2

Intermediate Results

unp sum : 1 [2] Num ”” ⇒2

sum

unp Add Add Num Num 1 Num 2 ”” ⇒5

unp

:

  • (
  • :
  • (
  • +

+ show :

  • +
  • +

+ show 1 :

  • )
  • :
  • +
  • +

+ show 2 :

  • )
  • ””

= ”((0 + 1) + 2)”

slide-4
SLIDE 4

3

Deforestation [Wad90, HJ92]

Key-ideas: folding

unp sum xs y z

to

sumunp xs y z , and “translating”

right-hand sides of sum with rules of unp: 1.

sumunp [x] y z = unp Add y Num x z ⇒unp :

  • (
  • unp

y :

  • +
  • unp

Num x :

  • )
  • z

⇒unp :

  • (
  • unp

y :

  • +
  • +

+ show x :

  • )
  • z
slide-5
SLIDE 5

4

2.

sumunp : x xs y z = unp sum xs Add y Num x z ❀ sumunp xs Add y Num x z

Deforestation eliminated only part of the intermediate result:

sumunp : 1 [2] Num ”” ⇒ sumunp [2] Add Num Num 1 ”” ⇒ :

  • (
  • unp

Add Num Num 1 :

  • +
  • +

+ show 2 :

  • )
  • ””

  • · · ·
slide-6
SLIDE 6

5

How to Deforest in Accumulating Parameters ?

Approach: replace

unp sum xs y z

by

sumunp xs unp y ? z , and hence assume

that sumunp has as second argument the correct translation of sum’s accumulating parameter with unp: 1.

sumunp [x] y

  • z

= unp Add y Num x z ⇒unp :

  • (
  • unp

y :

  • +
  • unp

Num x :

  • )
  • z

❀ :

  • (
  • y
slide-7
SLIDE 7

6

2.

sumunp : x xs y

  • z

= unp sum xs Add y Num x z ❀ sumunp xs unp Add y Num x ? z

Idea: let sumunp return a tuple, consisting of the composition

  • f sum and unp (as before) and additionally the parameter value

with which unp “arrives” at the accumulating parameter of sum, i.e.,

unp sum xs y z

will then be replaced by

fst sumunp xs unp y snd z

.

slide-8
SLIDE 8

7

Lazy Composition

1.

sumunp [x] y

  • z

= , unp Add y Num x z ? ⇒unp , :

  • (
  • unp

y :

  • +
  • unp

Num x :

  • )
  • z

? ❀ , :

  • (
  • y
  • :
  • +
  • unp

Num x :

  • )
  • z

⇒unp , :

  • (
  • y
  • :
  • +
  • +

+ show x :

  • )
  • z
slide-9
SLIDE 9

8

2.

sumunp : x xs y

  • z

= , unp sum xs Add y Num x z ? ❀ , fst sumunp xs unp Add y Num x snd z ? ⇒unp , fst sumunp xs :

  • (
  • unp

y :

  • +
  • unp

Num x :

  • )
  • snd

z ? ❀ , fst sumunp xs :

  • (
  • y
  • z

:

  • +
  • unp

Num x :

  • )
  • snd

⇒unp , fst sumunp xs :

  • (
  • y
  • z

:

  • +
  • +

+ show x :

  • )
  • snd
slide-10
SLIDE 10

9

Evaluation of Transformed Program

sumunp :: [Int] → String → String → (String, String) sumunp [x] y

  • z = (
  • (
  • : y
  • ,
  • +
  • : show x +

+

  • )
  • : z)

sumunp (x : xs) y

  • z = (fst v ,
  • +
  • : show x +

+

  • )
  • : snd v)

where v = sumunp xs (

  • (
  • : y
  • ) z

fst sumunp : 1 [2] unp Num snd ”” ⇒ fst , fst sumunp [2] :

  • (
  • unp

Num snd ”” :

  • +
  • +

+ show 1 :

  • )
  • snd

⇒ fst sumunp [2] :

  • (
  • unp

Num snd , :

  • +
  • +

+ show 1 :

  • )
  • snd

”” ⇒

  • · · ·
slide-11
SLIDE 11

10

Using Optimized Tuple Updates [Gro99]

sumunp [x] y

  • z = (
  • (
  • : y
  • ,
  • +
  • : show x +

+

  • )
  • : z)

sumunp (x : xs) y

  • z = (fst v ,
  • +
  • : show x +

+

  • )
  • : snd v)

where v = sumunp xs (

  • (
  • : y
  • ) z

fst sumunp : 1 [2] unp Num snd ”” ⇒ fst sumunp [2] :

  • (
  • unp

Num :

  • +
  • +

+ show 1 :

  • )
  • snd

”” ⇒ :

  • (
  • :
  • (
  • unp

Num :

  • +
  • +

+ show 1 :

  • )
  • :
  • +
  • +

+ show 2 :

  • )
  • ””

⇒ :

  • (
  • :
  • (
  • +

+ show :

  • +
  • +

+ show 1 :

  • )
  • :
  • +
  • +

+ show 2 :

  • )
  • ””
slide-12
SLIDE 12

11

Applicability of Lazy Composition (1)

MTT-functions (cf. macro tree transducers [EV85]):

  • first-order
  • defined by structural recursion on one principal argument
  • pattern-matching only possible on this recursion argument
  • calls to external functions allowed in consumer (e.g. unp),

but not in producer (e.g. sum)

  • no mutual recursion (yet)
slide-13
SLIDE 13

12

Applicability of Lazy Composition (2)

In order to ensure that the ?-values in

f2 f1 x y1 y

  • z1

z

❀ f1f2 x f2 y1 ? ? f2 y

  • ?

? z1 z

· · · · · · · · · · · · · · · · · ·

are always uniquely determined, and (as a consequence) that the resulting circular program terminates, the producer f1 must be linear in its accumulating parameters and the consumer f2 must be linear in its recursion variables.

slide-14
SLIDE 14

13

Possible Extensions

  • mutual recursion
  • relaxing linearity restrictions (a bit)
  • handle external calls also in the producer (using laws)
  • conditional expressions
  • zip-like functions as producers
  • . . . ?
slide-15
SLIDE 15

References

[EV85]

  • J. Engelfriet and H. Vogler. Macro tree transducers. J. Comput. Syst. Sci.,

31:71–145, 1985. [Gro99]

  • J. Groningen. Optimising recursive functions yielding multiple results in tuples

in a lazy functional language. In Implementation of Functional Languages, Lochem, The Netherlands, Proceedings, volume 1868 of LNCS, pages 59–76. Springer-Verlag, 1999. [HJ92]

  • G. Hamilton and S. Jones. Extending deforestation for first order functional
  • programs. In 1991 Glasgow Workshop on Functional Programming, Portree,

Scotland, Proceedings, Series of Workshops in Computing, pages 134–145. Springer-Verlag, 1992. [VK01]

  • J. Voigtl¨

ander and A. K¨

  • uhnemann. Composition of functions with accumulat-

ing parameters. Technical Report TUD-FI01-08, Dresden University of Tech- nology, 2001. [Wad90] P. Wadler. Deforestation: Transforming programs to eliminate trees. Theoret.

  • Comput. Sci., 73:231–248, 1990.