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¨ orderern der TU Dresden” with a travel grant.
� � � � � � Functions with Accumulating Parameters data Term = Num Int | Add Term Term sum :: [ Int ] → Term → Term [ x ] y = Add y ( Num x ) sum sum ( x : xs ) y = sum xs ( Add y ( Num x )) sum Add sum [2] Add Add Num : Num ⇒ ⇒ 2 Num Num Num Num [2] 1 0 0 1 0 1 unp :: Term → String → String ( Num x ) z = show x + + z unp unp ( Add x 1 x 2 ) z = ( : unp x 1 ( + : unp x 2 ( ) : z )) 1
� � � � � � � � � � � � Intermediate Results : : ( ( + + unp unp : show ”” Add sum ”” + + 0 + ⇒ 5 ⇒ 2 = ”((0 + 1) + 2)” Add Num unp sum : : Num show 2 Num Num ) : 1 [2] 1 0 0 1 + + + : show ) 2 ”” 2
� � � � � � � � � � � � Deforestation [Wad90, HJ92] unp sumunp Key-ideas: folding to z , and “translating” sum z y xs y xs right-hand sides of sum with rules of unp : : : 1. unp unp ( ( unp sumunp : : y y Add z ⇒ unp ⇒ unp = unp + + + + [ x ] y z Num y : : Num show x ) ) x z x z 3
� � � � � � � unp 2. sumunp sumunp sum z Add xs z = : Add y xs ❀ z y Num Num y x xs x x Deforestation eliminated only part of the intermediate result: : unp ( sumunp sumunp : Add [2] ”” Add : ”” Num ⇒ ⇒ · · · ⇒ + + + Num Num Num Num [2] 1 0 : 0 1 show 0 1 ) 2 ”” 4
� � � � � � � � � � How to Deforest in Accumulating Parameters ? sumunp unp z , and hence assume Approach: replace by unp sum z xs y ? xs y that sumunp has as second argument the correct translation of sum ’s accumulating parameter with unp : : 1. unp ( unp sumunp : : y Add z ⇒ unp = ❀ unp + ( [ x ] y z y Num y : Num x ) x z 5
� sumunp unp 2. sumunp unp sum z xs z = : Add ? Add xs ❀ z y Num y Num y x xs x x Idea: let sumunp return a tuple, consisting of the composition of sum and unp (as before) and additionally the parameter value with which unp “arrives” at the accumulating parameter of sum , fst unp sumunp i.e., will then be replaced by . sum z unp xs z y xs y snd 6
� � � � � � � � � � � � � � � � � � � � � Lazy Composition , 1. : ? , , : : unp ( unp ? sumunp unp ( + : ⇒ unp y = y Add z ❀ [ x ] z y unp : Num + y Num : ) Num x x z ) x z , : : ( + + + ⇒ unp y : show ) x z 7
� � � � � � � � � � � � � � � � � � � � � , 2. fst ? , , sumunp fst ? unp ? : xs z sumunp sumunp sum z unp ( ⇒ unp = unp : ❀ xs z z y Add xs : y Add snd x xs y Num unp + y Num x : Num x ) snd x , , : : fst fst sumunp unp sumunp ⇒ unp + + + + ❀ : : : : Num show xs z xs z ( ) ( ) snd snd y x y x 8
� � � � � � � � � � � � � � � � � � � � � � � � � � � � � 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 fst [2] : , ”” fst : fst unp ( sumunp sumunp + + + Num snd unp : ”” ⇒ ⇒ · · · ⇒ [2] : : ”” show 0 , [2] 1 Num snd : unp ( ) 1 snd 0 + + + snd Num : show 0 ) 1 snd 9
� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � Using Optimized Tuple Updates [Gro99] [ x ] z = ( ( : y + : show x + + ) : z ) sumunp y , sumunp ( x : xs ) y z = ( fst v , + : show x + + ) : snd v ) where v = sumunp xs ( ( : y ) z : : : : ( ( fst sumunp unp ( ( + + fst [2] : : : ”” Num show sumunp + + + + + + 0 0 unp ( unp : ”” ⇒ ⇒ ⇒ : : show show : Num [2] 1 Num snd ) : ) : 1 1 + + 0 + 0 : + + + + + + show : : ) show show 1 snd ) ) 2 2 ”” ”” 10
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) 11
✁ ✁ � � Applicability of Lazy Composition (2) In order to ensure that the ?-values in f 2 f 1 f 2 · · · · · · · · · x f 2 f 2 f 1 ❀ z 1 z z 1 z · · · · · · · · · y 1 ? ? ? ? y 1 y y x are always uniquely determined, and (as a consequence) that the resulting circular program terminates, the producer f 1 must be linear in its accumulating parameters and the consumer f 2 must be linear in its recursion variables. 12
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 • . . . ? 13
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.
Recommend
More recommend