Using Circular Programs to Deforest in
Transcrição
Using Circular Programs to Deforest in
ASIA-PEPM, September 14, 2002 Using Circular Programs to Deforest in Accumulating Parameters Janis Voigtländer 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. 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 Add sum : 1 Num [2] 0 ⇒ [2] Add Num Add ⇒ Num Num Num Num 0 1 0 1 2 unp :: Term → String → String unp (Num x ) z = show x ++ z unp (Add x1 x2 ) z = ( : unp x1 ( + : unp x2 ( ) : z )) 1 Intermediate Results : : ( unp unp sum : 1 ”” Num [2] 0 ( ++ : show Add ⇒2sum Add ”” Num Num Num 0 1 2 ⇒5unp 0 ++ + 1 = ”((0 + 1) + 2)” : show ) : ++ + : show 2 ) ”” 2 Deforestation [Wad90, HJ92] unp Key-ideas: folding sum z sumunp to , and “translating” y xs z y xs right-hand sides of sum with rules of unp: : 1. ( unp sumunp [x] Add = y z y z Num x : unp : y ⇒unp ( y ⇒unp unp + : Num x unp ) : ++ + : show z x ) z 3 unp 2. sumunp : y z sum = sumunp z Add xs Add xs ; y x Num y xs z Num x x Deforestation eliminated only part of the intermediate result: : ( sumunp unp sumunp : 1 Num [2] 0 ”” ⇒ [2] ”” Add : Add ⇒ Num Num Num Num 0 1 0 1 ⇒ ··· ++ + : show 2 ) ”” 4 How to Deforest in Accumulating Parameters ? sumunp unp Approach: replace sum z by xs unp z , and hence assume y xs ? y that sumunp has as second argument the correct translation of sum’s accumulating parameter with unp: : 1. ( unp sumunp [x] Add = y z y z Num x unp : y ⇒unp : ; unp + y : Num x ( ) z 5 sumunp unp 2. sumunp : x z y sum = Add xs y xs z Num x xs unp Add ; y z ? Num 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 i.e., sum xs z sumunp will then be replaced by xs unp z . y y snd 6 Lazy Composition , 1. : , unp sumunp [x] = y Add ⇒unp z ; unp + Num x ) ( y unp + : Num : Num x : : : y z y , unp ( ? ? x ) z , : ⇒unp ( : y ++ + : show x ) z 7 z , 2. fst , , unp sumunp : x y z fst sum = y sumunp ? ; xs unp Add Num y x : xs sumunp z Add xs xs ? ? unp ( ⇒unp z z : y snd unp + Num ) x , , : fst ; sumunp xs ( : z y : Num x unp + ⇒unp : Num x : fst ) sumunp xs snd ( : z y ++ + : show x ) snd 8 snd 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 , fst 1 unp [2] Num 0 : fst sumunp : [2] sumunp ”” ⇒ [2] : snd ”” Num 0 : show unp ( snd 1 ⇒ ) ”” unp ( ++ + : Num 0 snd , ⇒ ··· : snd ++ + : show 1 ) snd 9 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 fst [2] : : : unp ( ”” unp : 1 [2] Num 0 ”” snd ⇒ 0 unp ( ( : Num sumunp ++ + 0 1 ++ + : show 1 ) 0 ) ++ + : show : 1 ++ + snd : ⇒ : show : ++ show ⇒ Num : ( 2 ) : ++ + : show ) : show ”” 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 x z1 · · · z y1 · · · y f1 ; x f2 ··· f2 z1 · · · z f1 f2 f2 y1 ? · · · ? y ? · · · ? 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. 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änder and A. Kühnemann. Composition of functions with accumulating parameters. Technical Report TUD-FI01-08, Dresden University of Technology, 2001. [Wad90] P. Wadler. Deforestation: Transforming programs to eliminate trees. Theoret. Comput. Sci., 73:231–248, 1990.