RTA, July 23, 2002 Conditions for Efficiency Improvement by Tree Transducer Composition Janis Voigtl¨ ander Dresden University of Technology voigt@tcs.inf.tu-dresden.de Supported by the “Deutsche Forschungsgemeinschaft” under grant KU 1290/2-1 and by the “European Commission – DG Information Society” with a FLoC’02 travel grant.
Macro Tree Transducers [Eng80] data Term = Term ⊗ Term | Term ⊕ Term | A | B data Ins = Mul Ins | Add Ins | Load A Ins | Load B Ins | Nil data Nat = Succ Nat | Zero pre :: Term → Ins → Ins pre ( x 1 ⊗ x 2 ) y = Mul ( pre x 1 ( pre x 2 y )) pre ( x 1 ⊕ x 2 ) y = Add ( pre x 1 ( pre x 2 y )) y = Load A y A pre B y = Load B y pre Mul Mul Mul pre Load A pre Load A ⊗ Nil Add pre ⇒ ⇒ pre A ⇒ 3 ⊕ Load B A ⊕ ⊕ Nil Nil B A Load A B A B A Nil ops :: Ins → Nat ( Mul x ) = Succ ( ops x ) ops ops ( Add x ) = Succ ( ops x ) ops ( Load A x ) = ops x ops ( Load B x ) = ops x = Zero Nil ops 1
✂ �✁ �☎ ✄ Intermediate Results ops Mul ops pre Load A Succ ⇒ 5 ⇒ 6 ⊗ Nil Add Succ ⊕ Load B A Zero B A Load A Nil Inefficient ! 2
Tree Transducer Composition [EV85]: Example ops Succ Succ preops ops preops Mul = ⇒ ⇒ pre pre ops ⊗ y ops x 1 pre pre pre x 1 x 1 x 1 x 2 x 2 y x 2 y x 2 y Succ Succ preops preops ⇒ ⇒ preops x 1 preops x 1 ops x 2 y ops x 2 y Replace occurrences of ( ops ( pre t Nil )) by ( preops t ( ops Nil )). 3
Transformed Program: preops :: Term → Nat → Nat preops ( x 1 ⊗ x 2 ) y ops = Succ ( preops x 1 ( preops x 2 y ops )) preops ( x 1 ⊕ x 2 ) y ops = Succ ( preops x 1 ( preops x 2 y ops )) A y ops = y ops preops y ops = y ops B preops Succ preops Succ preops Succ ⊗ preops Zero preops ⇒ ⇒ A ⇒ 3 Succ ⊕ ⊕ Zero A ⊕ Zero Zero B A B A B A No intermediate result is produced ! Transformed program requires fewer reduction steps ! 4
Formal Efficiency Analysis should be: • with respect to call-by-need reduction steps • input-independent • based on original program before transformation 5
✁ ✁ ✁ � � ✁ Ticking of Producer: � ( x 1 ⊗ x 2 ) y = ⋄ ( Mul ( pre � x 1 ( pre � x 2 y ))) pre � ( x 1 ⊕ x 2 ) y = ⋄ ( Add ( pre � x 1 ( pre � x 2 y ))) pre y = ⋄ ( Load A y ) A pre B y = ⋄ ( Load B y ) pre ⋄ Mul ⋄ ⋄ ⋄ Mul Load A Mul pre ⋄ ⋄ pre ⊗ Nil Load A Add ⇒ ⇒ ⇒ 3 A pre A ⊕ ⋄ pre ⊕ Nil B A Load B ⊕ Nil B A ⋄ B A Load A Nil 6
� � � Ticking of Consumer: � ( Mul x ) � x )) = • ( Succ ( ops ops � ( Add x ) � x )) = • ( Succ ( ops ops � ( Load A x ) = • ( ops � x ) ops � ( Load B x ) = • ( ops � x ) ops � Nil = • Zero ops � ( ⋄ x 1 ) � x 1 ) = • ( ops ops • • • ops • • ops ⋄ Succ Succ Mul Mul ops • ⋄ ⋄ • ⋄ Load A Load A • Load A • ⋄ ⋄ ⇒ ⇒ ⋄ ⇒ 9 Succ Add Add Add ⋄ ⋄ • ⋄ • Load B Load B Load B • ⋄ ⋄ ⋄ • Load A Load A • Load A Nil Nil Zero Nil 7
� � ✁ Steps of Original Program Reflected in Output (Lemma 2) The number of • -symbols in the reduction result of: ops pre ⊗ Nil ⊕ A B A is equal to the number of call-by-need reduction steps of: ops pre Succ ⊗ Nil ⇒ Succ ⊕ A Zero B A 8
� � � � � � � � � � � � Ticking of Composed Program: ( x 1 ⊗ x 2 ) y ops = ◦ ( Succ ( preops x 1 ( preops x 2 y ops ))) preops ( x 1 ⊕ x 2 ) y ops = ◦ ( Succ ( preops x 1 ( preops x 2 y ops ))) preops y ops = ◦ y ops A preops B y ops = ◦ y ops preops ◦ ◦ ◦ Succ Succ Succ preops ◦ preops ◦ ◦ ⊗ Zero ⇒ ⇒ ⇒ 3 preops Succ preops A ⊕ A ◦ ⊕ Zero ⊕ B A Zero ◦ B A B A Zero 9
� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � Annotation through Composition (Lemma 4) ( x 1 ⊗ x 2 ) y = ⋄ ( Mul ( pre x 1 ( pre x 2 y ))) pre ( x 1 ⊕ x 2 ) y = ⋄ ( Add ( pre x 1 ( pre x 2 y ))) pre y = ⋄ ( Load A y ) A pre y = ⋄ ( Load B y ) B pre ( Mul x ) = Succ ( ops x ) ops ( Add x ) = Succ ( ops x ) ops ( Load A x ) = ops ops x ( Load B x ) = ops ops x Nil = Zero ops ( ⋄ x 1 ) = ◦ ( ops x 1 ) ops composed into: ( x 1 ⊗ x 2 ) y ops = ◦ ( Succ ( pre x 1 ( pre ))) pre ops ops ops x 2 y ops ( x 1 ⊕ x 2 ) y ops = ◦ ( Succ ( pre x 1 ( pre ))) pre ops ops ops x 2 y ops = ◦ y ops A pre ops y ops = ◦ y ops B pre ops y ops 10
� � � ✁ Steps of Composed Program Reflected in Output (Lemma 5) The number of ◦ -symbols in the reduction result of: ops pre ⊗ Nil ⊕ A B A is greater or equal to the number of call-by-need reduction steps of: preops Succ ⊗ Zero ⇒ Succ ⊕ A Zero B A 11
� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � Compare Annotated Programs: ( x 1 ⊗ x 2 ) y = ⋄ ( Mul ( pre x 1 ( pre x 2 y ))) pre ( x 1 ⊕ x 2 ) y = ⋄ ( Add ( pre x 1 ( pre x 2 y ))) pre y = ⋄ ( Load A y ) A pre B y = ⋄ ( Load B y ) pre ( Mul x ) = • ( Succ ( ops x )) ( Mul x ) = Succ ( ops x ) ops ops ( Add x ) = • ( Succ ( ops x )) ( Add x ) = Succ ( ops x ) ops ops ( Load A x ) = • ( ops x ) ( Load A x ) = ops ops ops x ( Load B x ) = • ( ops x ) ( Load B x ) = ops ops ops x Nil = • Zero Nil = Zero ops ops ( ⋄ x 1 ) = • ( ops x 1 ) ( ⋄ x 1 ) = ◦ ( ops x 1 ) ops ops Always more • - than ◦ -symbols ! 12
Abstracting from the Example (Theorem 1) The composed program is at least as efficient as the original program, provided that: 1. the producer is context-linear or basic 2. the consumer is recursion-linear 3. the consumer is context-linear or basic 13
Further Results: • Weaker pre-conditions by counting only steps of the con- sumer (Lemma 3, Lemma 6, Lemma 7, Theorem 2) • Application to special cases of classical deforestation [Wad90] (Corollary 1) • Analysis technique scales for the case that both involved transducers use context parameters [VK01]; work in progress 14
References [Eng80] J. Engelfriet. Some open questions and recent results on tree transducers and tree languages. In R.V. Book, editor, Formal language theory; perspectives and open problems , pages 241–286. New York, Academic Press, 1980. [EV85] J. Engelfriet and H. Vogler. Macro tree transducers. J. Comput. Syst. Sci. , 31:71–145, 1985. [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, August 2001. http://wwwtcs.inf.tu-dresden.de/ ∼ voigt/TUD-FI01-08.ps.gz . [Wad90] P. Wadler. Deforestation: Transforming programs to eliminate trees. Theoret. Comput. Sci. , 73:231–248, 1990.
Recommend
More recommend